sndata.utils

The utils module provides an assorted collection of general utilities used when building data access classes for a given survey / data release.

sndata.utils.build_pbar(data: iter, verbose: Union[bool, dict])[source]

Cast an iterable into a progress bar

If verbose is False, return data unchanged.

Parameters:
  • data – An iterable object
  • verbose – Arguments for tqdm.tqdm
sndata.utils.download_file(url: str, destination: Union[str, pathlib.Path, IO] = None, force: bool = False, timeout: float = 15, verbose: bool = True)[source]

Download content from a url to a file

If destination is a path but already exists, skip the download unless force is also True.

Parameters:
  • url – URL of the file to download
  • destination – Path or file object to download to
  • force – Re-Download locally available data (Default: False)
  • timeout – Seconds before raising timeout error (Default: 15)
  • verbose – Print status to stdout
sndata.utils.download_tar(url: str, out_dir: str, mode: str = 'r:gz', force: bool = False, timeout: float = 15, skip_exists: str = None)[source]

Download and unzip a .tar.gz file to a given output directory

Parameters:
  • url – URL of the file to download
  • out_dir – The directory to unzip file contents to
  • mode – Compression mode (Default: r:gz)
  • force – Re-Download locally available data (Default: False)
  • timeout – Seconds before raising timeout error (Default: 15)
  • skip_exists – Optionally skip the download if given path exists
sndata.utils.find_data_dir(survey_abbrev: str, release: str) → pathlib.Path[source]

Determine the directory where data files are stored for a data release

If the directory does not exist, create it.

Parameters:
  • survey_abbrev – Abbreviation of the survey to load data for (e.g., CSP)
  • release – Name of the data release from the survey (e.g., DR1)
Returns:

The path of the directory where

sndata.utils.hourangle_to_degrees(rah: float, ram: float, ras: float, dec_sign: str, decd: float, decm: float, decs: float) -> (<class 'float'>, <class 'float'>)[source]

Convert from hour angle to degrees

Parameters:
  • rah – RA hours
  • ram – RA arcminutes
  • ras – RA arcseconds
  • dec_sign – Sign of the declination (‘+’ or ‘-‘)
  • decd – Dec degrees
  • decm – Dec arcmin
  • decs – Dec arcsec
sndata.utils.lru_copy_cache(maxsize: int = 128, typed: bool = False, copy: bool = True)[source]

Decorator to cache the return of a function

Similar to functools.lru_cache, but allows a copy of the cached value to be returned, thus preventing mutation of the cache.

Parameters:
  • maxsize – Maximum size of the cache
  • typed – Cache objects of different types separately (Default: False)
  • copy – Return a copy of the cached item (Default: True)
Returns:

A decorator

sndata.utils.read_vizier_table_descriptions(readme_path: Union[pathlib.Path, str])[source]

Returns the table descriptions from a vizier readme file

Parameters:readme_path – Path of the file to read
Returns:<Table description (str)>}
Return type:A dictionary {<Table number (int)>
sndata.utils.register_filter_file(file_path: str, filter_name: str, force: bool = False)[source]

Registers filter profiles with sncosmo if not already registered

Assumes the file at file_path is a two column, white space delimited ascii table.

Parameters:
  • file_path – Path of ascii table with wavelength (Ang) and transmission
  • filter_name – The name of the registered filter.
  • force – Whether to re-register a band if already registered
sndata.utils.require_data_path(*data_dirs)[source]

Raise NoDownloadedData exception if given paths don’t exist

Parameters:*data_dirs – Path objects to check exists