nrcatalogtools.utils¶
Cache paths, download helpers, and unit-conversion utilities.
Download helpers¶
url_exists
¶
Check if a given URL exists on the web.
Retries up to num_retries times on network errors, with exponential
backoff (2**attempt seconds, capped at 30 s). A non-OK HTTP status
is returned immediately as False without retrying (the URL exists but
is not accessible / not found — no point retrying).
Args: link (str): Complete web URL. num_retries (int): Maximum number of attempts. Defaults to 5. verbosity (int): Print retry progress when > 0. Defaults to 0.
Returns: bool: True if the URL returned HTTP 200, False otherwise.
Source code in nrcatalogtools/utils.py
download_file
¶
download_file(url: str, path: str | Path, progress: bool = False, if_newer: bool = True, num_retries: int = 5, verbosity: int = 0) -> pathlib.Path
Download a file from the given URL to the specified local path.
This function attempts to download the file at url and save it to path.
It first tries to use the sxs.utilities.downloads.download_file utility (if available).
If that fails, it falls back to using the Python requests package, with SSL
verification disabled and up to num_retries attempts with exponential
backoff (2**attempt seconds, capped at 30 s).
Args: url (str): The URL to download the file from. path (str or pathlib.Path): The destination path where the file should be saved. progress (bool, optional): Whether to show a progress bar if supported. if_newer (bool, optional): Only download if the remote file is newer than the local file. num_retries (int): Maximum number of fallback attempts. Defaults to 5. verbosity (int): Print retry progress when > 0. Defaults to 0.
Returns: The path (as a pathlib.Path object) to the downloaded file.
Raises: ConnectionError: If the file could not be fetched after all retry attempts. RuntimeError: If the server returned a non-200 status.
Source code in nrcatalogtools/utils.py
call_with_timeout
¶
call_with_timeout(myfunc: object, args: tuple = (), _kwargs: dict = {}, timeout: float = 5) -> object
Call a function with a time limit in a separate process.
Executes the provided function myfunc with given positional (args) and keyword
arguments (kwargs) in a separate process. If the function does not complete
within timeout seconds, the process is terminated and an exception is raised.
If the function completes within the timeout, its result is returned.
Args:
myfunc (callable): The function to execute.
args (tuple, optional): Positional arguments to pass to myfunc. Defaults to ().
_kwargs (dict, optional): Reserved; keyword arguments are not currently
forwarded to the subprocess. Defaults to {}.
timeout (int or float, optional): Maximum allowed execution time in seconds. Defaults to 5.
Returns:
The result of myfunc(*args, **kwargs) if completed within the timeout.
Raises: Exception: If the function does not complete within the specified timeout.
Source code in nrcatalogtools/utils.py
Unit conversions¶
time_to_physical
¶
Factor to convert time from dimensionless units to SI units
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
M
|
float
|
|
required |
Returns:
| Type | Description |
|---|---|
converting factor
|
|
Source code in nrcatalogtools/utils.py
amp_to_physical
¶
Factor to rescale strain to mass M and distance D convert from dimensionless units to SI units
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
M
|
float
|
|
required |
D
|
float
|
|
required |
Returns:
| Type | Description |
|---|---|
Scaling factor
|
|
Source code in nrcatalogtools/utils.py
amplitude_phase_frequency_from_complex_mode
¶
Compute amplitude, phase, and instantaneous frequency from a complex mode time series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hlm
|
tuple of (real, imag) pycbc.types.TimeSeries, or a single complex pycbc.types.TimeSeries
|
Either a tuple of real and imaginary parts of a mode (as PyCBC TimeSeries with matching sample_times), or a single complex-valued PyCBC TimeSeries. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
amp |
TimeSeries
|
The instantaneous amplitude as a function of time. |
phase |
TimeSeries
|
The instantaneous phase as a function of time. |
freq |
TimeSeries
|
The instantaneous frequency (cycles per unit time) as a function of time. |