1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Utils module Python bindings
//
// This file contains Python bindings for utility functions including threading and cache management
// NOTE: No imports needed here as all pyo3 items are imported in mod.rs
// where this file is included
// ================================
// Cache Management Functions
// ================================
/// Get the brahe cache directory path.
///
/// The cache directory is determined by the `BRAHE_CACHE` environment variable.
/// If not set, defaults to `~/.cache/brahe`.
///
/// The directory is created if it doesn't exist.
///
/// Returns:
/// str: The full path to the cache directory.
///
/// Raises:
/// IOError: If the cache directory cannot be created or accessed.
///
/// Example:
/// ```python
/// import brahe as bh
///
/// cache_dir = bh.get_brahe_cache_dir()
/// print(f"Cache directory: {cache_dir}")
///
/// # You can also override with environment variable
/// import os
/// os.environ['BRAHE_CACHE'] = '/custom/cache/path'
/// cache_dir = bh.get_brahe_cache_dir()
/// ```
///
/// Note:
/// The directory will be created on first access if it doesn't exist.
/// Get the brahe cache directory path with an optional subdirectory.
///
/// The cache directory is determined by the `BRAHE_CACHE` environment variable.
/// If not set, defaults to `~/.cache/brahe`. If a subdirectory is provided,
/// it is appended to the cache path.
///
/// The directory is created if it doesn't exist.
///
/// Args:
/// subdirectory (str or None): Optional subdirectory name to append to cache path.
///
/// Returns:
/// str: The full path to the cache directory (with subdirectory if provided).
///
/// Raises:
/// IOError: If the cache directory cannot be created or accessed.
///
/// Example:
/// ```python
/// import brahe as bh
///
/// # Get main cache directory
/// cache_dir = bh.get_brahe_cache_dir_with_subdir(None)
/// print(f"Cache: {cache_dir}")
///
/// # Get custom subdirectory
/// custom_cache = bh.get_brahe_cache_dir_with_subdir("my_data")
/// print(f"Custom cache: {custom_cache}")
/// ```
///
/// Note:
/// The directory (and subdirectory) will be created on first access if it doesn't exist.
/// Get the EOP cache directory path.
///
/// Returns the path to the EOP (Earth Orientation Parameters) cache subdirectory.
/// Defaults to `~/.cache/brahe/eop` (or `$BRAHE_CACHE/eop` if environment variable is set).
///
/// The directory is created if it doesn't exist.
///
/// Returns:
/// str: The full path to the EOP cache directory.
///
/// Raises:
/// IOError: If the cache directory cannot be created or accessed.
///
/// Example:
/// ```python
/// import brahe as bh
///
/// eop_cache = bh.get_eop_cache_dir()
/// print(f"EOP cache: {eop_cache}")
/// ```
///
/// Note:
/// The directory will be created on first access if it doesn't exist.
/// Get the CelesTrak cache directory path.
///
/// Returns the path to the CelesTrak cache subdirectory used for storing downloaded
/// TLE data. Defaults to `~/.cache/brahe/celestrak` (or `$BRAHE_CACHE/celestrak`
/// if environment variable is set).
///
/// The directory is created if it doesn't exist.
///
/// Returns:
/// str: The full path to the CelesTrak cache directory.
///
/// Raises:
/// IOError: If the cache directory cannot be created or accessed.
///
/// Example:
/// ```python
/// import brahe as bh
///
/// celestrak_cache = bh.get_celestrak_cache_dir()
/// print(f"CelesTrak cache: {celestrak_cache}")
/// ```
///
/// Note:
/// The directory will be created on first access if it doesn't exist.
// ================================
// Threading Functions
// ================================
/// Set the number of threads for parallel computation.
///
/// Configures the global thread pool used by Brahe for parallel operations such as
/// access computations. This function can be called multiple times to dynamically
/// change the thread pool configuration - each call will reinitialize the pool with
/// the new thread count.
///
/// Args:
/// n (int): Number of threads to use. Must be at least 1.
///
/// Raises:
/// ValueError: If n < 1.
/// RuntimeError: If thread pool fails to build.
///
/// Example:
/// ```python
/// import brahe as bh
///
/// # Set to 4 threads initially
/// bh.set_num_threads(4)
/// print(f"Threads: {bh.get_max_threads()}") # Output: 4
///
/// # Reinitialize with 8 threads - no error!
/// bh.set_num_threads(8)
/// print(f"Threads: {bh.get_max_threads()}") # Output: 8
///
/// # All parallel operations (e.g., location_accesses) will now use
/// # 8 threads unless overridden with AccessSearchConfig.num_threads
/// ```
///
/// Note:
/// Unlike earlier versions, this function no longer raises an error if the
/// thread pool has already been initialized. You can safely call it at any
/// time to reconfigure the thread pool.
/// Set the thread pool to use all available CPU cores.
///
/// This is a convenience function that sets the number of threads to 100%
/// of available CPU cores. Can be called multiple times to reinitialize the
/// thread pool dynamically.
///
/// Raises:
/// RuntimeError: If thread pool fails to build.
///
/// Example:
/// ```python
/// import brahe as bh
///
/// # Use all available CPU cores
/// bh.set_max_threads()
/// print(f"Using all {bh.get_max_threads()} cores")
///
/// # Switch to 2 threads
/// bh.set_num_threads(2)
///
/// # Switch back to max - no error!
/// bh.set_max_threads()
/// print(f"Back to {bh.get_max_threads()} cores")
/// ```
///
/// Note:
/// This function can be called at any time, even after the thread pool
/// has been initialized with a different configuration.
/// LUDICROUS SPEED! GO!
///
/// Set the thread pool to use all available CPU cores (alias for `set_max_threads`).
///
/// This is a fun alias for `set_max_threads()` that sets the number of threads
/// to 100% of available CPU cores for maximum performance. Can be called multiple
/// times to dynamically reinitialize the thread pool.
///
/// Raises:
/// RuntimeError: If thread pool fails to build.
///
/// Example:
/// ```python
/// import brahe as bh
///
/// # MAXIMUM POWER! Use all available CPU cores
/// bh.set_ludicrous_speed()
/// print(f"Going ludicrous with {bh.get_max_threads()} threads!")
///
/// # Throttle down for testing
/// bh.set_num_threads(1)
///
/// # ENGAGE LUDICROUS SPEED again - no error!
/// bh.set_ludicrous_speed()
/// ```
///
/// Note:
/// This function can be called at any time to reconfigure the thread pool
/// to use maximum available cores, regardless of previous configuration.
/// Get the current maximum number of threads for parallel computation.
///
/// Returns the number of threads configured for the global thread pool.
/// If the thread pool hasn't been initialized yet, this initializes it
/// with the default (90% of available cores) and returns that value.
///
/// Returns:
/// int: Number of threads currently configured.
///
/// Example:
/// ```python
/// import brahe as bh
///
/// # Get default thread count (90% of cores, initialized on first call)
/// threads = bh.get_max_threads()
/// print(f"Default: {threads} threads")
///
/// # Set to specific value and verify
/// bh.set_num_threads(4)
/// assert bh.get_max_threads() == 4
///
/// # Reconfigure and verify again
/// bh.set_num_threads(8)
/// assert bh.get_max_threads() == 8
///
/// # Switch to max cores
/// bh.set_max_threads()
/// print(f"Max cores: {bh.get_max_threads()}")
/// ```
///
/// Note:
/// Calling this function will initialize the thread pool with default
/// settings (90% of cores) if it hasn't been configured yet. After
/// initialization, you can still reconfigure it using set_num_threads()
/// or set_max_threads().
// ================================
// Formatting Functions
// ================================
/// Format a time duration in seconds to a human-readable string.
///
/// Converts a duration in seconds to either a long format (e.g., "6 minutes and 2.00 seconds")
/// or a short format (e.g., "6m 2s").
///
/// Args:
/// seconds (float): Time duration in seconds
/// short (bool): If True, use short format; otherwise use long format (default: False)
///
/// Returns:
/// str: Human-readable string representation of the time duration
///
/// Example:
/// ```python
/// import brahe as bh
///
/// # Long format (default)
/// print(bh.format_time_string(90.0))
/// # Output: "1 minutes and 30.00 seconds"
///
/// print(bh.format_time_string(3665.0))
/// # Output: "1 hours, 1 minutes, and 5.00 seconds"
///
/// # Short format
/// print(bh.format_time_string(90.0, short=True))
/// # Output: "1m 30s"
///
/// print(bh.format_time_string(3665.0, short=True))
/// # Output: "1h 1m 5s"
/// ```