Utilities (photometry.utilities)

Collection of utility functions that can be used throughout the photometry package.

Code author: Rasmus Handberg <rasmush@phys.au.dk>

class photometry.utilities.ListHandler(*args, message_queue, **kwargs)[source]

Bases: logging.Handler

A logging.Handler that writes messages into a list object.

The standard logging.QueueHandler/logging.QueueListener can not be used for this because the QueueListener runs in a private thread, not the main thread.

Warning

This handler is not thread-safe. Do not use it in threaded environments.

__init__(*args, message_queue, **kwargs)[source]

Initialize by copying the queue and sending everything else to superclass.

emit(record)[source]

Add the formatted log message (sans newlines) to the queue.

class photometry.utilities.LoggerWriter(logger, level=20)[source]

Bases: object

File-like object which passes input into a logger.

Can be used together with contextlib.redirect_stdout() or contextlib.redirect_stderr() to redirect streams to the given logger. Can be useful for wrapping codes which uses normal print() functions for logging.

1logger = logging.getLogger(__name__)
2with contextlib.redirect_stdout(LoggerWriter(logger, logging.INFO)):
3        print("This goes into the logger instead of STDOUT")

Warning

This object is not thread-safe. Do not use it in threaded environments.

Code author: Rasmus Handberg <rasmush@phys.au.dk>

__init__(logger, level=20)[source]
flush()[source]
write(message)[source]
class photometry.utilities.TqdmLoggingHandler(*args, **kwargs)[source]

Bases: logging.Handler

__init__(*args, **kwargs)[source]

Initializes the instance - basically setting the formatter to None and the filter list to empty.

emit(record)[source]

Do whatever it takes to actually log the specified logging record.

This version is intended to be implemented by subclasses and so raises a NotImplementedError.

photometry.utilities.add_proper_motion(ra, dec, pm_ra, pm_dec, bjd, epoch=2000.0)[source]

Project coordinates (ra,dec) with proper motions to new epoch.

Parameters
  • ra (float) – Right ascension.

  • dec (float) – Declination.

  • pm_ra (float) – Proper motion in RA (mas/year).

  • pm_dec (float) – Proper motion in Declination (mas/year).

  • bjd (float) – Julian date to calculate coordinates for.

  • epoch (float, optional) – Epoch of ra and dec. Default=2000.

Returns

RA and Declination at the specified date.

Return type

(float, float)

photometry.utilities.cartesian_to_radec(xyz)[source]

Convert cartesian coordinates (x,y,z) to spherical coordinates in ra-dec form.

Parameters

radec (ndarray) – Array with ra-dec pairs.

Returns

ra-dec coordinates in degrees corresponding to input coordinates.

Return type

ndarray

photometry.utilities.download_file(url, destination, desc=None, timeout=60, position_holders=None, position_lock=None, showprogress=None)[source]

Download file from URL and place into specified destination.

Parameters
  • url (str) – URL to file to be downloaded.

  • destination (str) – Path where to save file.

  • desc (str, optional) – Description to write next to progress-bar.

  • timeout (float) – Time to wait for server response in seconds. Default=60.

  • showprogress (bool) – Force showing the progress bar. If None, the progressbar is shown based on the logging level and output type.

Code author: Rasmus Handberg <rasmush@phys.au.dk>

photometry.utilities.download_parallel(urls, workers=4, timeout=60, showprogress=None)[source]

Download several files in parallel using multiple threads.

Parameters
  • urls (iterable) – List of files to download. Each element should consist of a list or tuple, containing two elements: The URL to download, and the path to the destination where the file should be saved.

  • workers (int, optional) – Number of threads to use for downloading. Default=4.

  • timeout (float) – Time to wait for server response in seconds. Default=60.

Code author: Rasmus Handberg <rasmush@phys.au.dk>

photometry.utilities.find_catalog_files(rootdir, sector=None, camera=None, ccd=None)[source]

Search the input directory for CATALOG (sqlite) files matching constraints.

Parameters
  • rootdir (str) – Directory to search for CATALOG files.

  • sector (int, list or None, optional) – Only return files from the given sectors. If None, files from all TIC numbers are returned.

  • camera (int, list or None, optional) – Only return files from the given camera. If None, files from all cameras are returned.

  • ccd (int, list or None, optional) – Only return files from the given ccd. If None, files from all ccds are returned.

Returns

List of paths to CATALOG files matching constraints.

Return type

list

photometry.utilities.find_ffi_files(rootdir, sector=None, camera=None, ccd=None)[source]

Search directory recursively for TESS FFI images in FITS format.

The function is cached, meaning the first time it is run on a particular rootdir the list of files in that directory will be read and cached to memory and used in subsequent calls to the function. This means that any changes to files on disk after the first call of the function will not be picked up in subsequent calls to this function.

Parameters
  • rootdir (str) – Directory to search recursively for TESS FFI images.

  • sector (int or None, optional) – Only return files from the given sector. If None, files from all sectors are returned.

  • camera (int or None, optional) – Only return files from the given camera number (1-4). If None, files from all cameras are returned.

  • ccd (int or None, optional) – Only return files from the given CCD number (1-4). If None, files from all CCDs are returned.

Returns

List of full paths to FFI FITS files found in directory. The list will

be sorted accoridng to the filename of the files, i.e. primarily by time.

Return type

list

photometry.utilities.find_hdf5_files(rootdir, sector=None, camera=None, ccd=None)[source]

Search the input directory for HDF5 files matching constraints.

Parameters
  • rootdir (str) – Directory to search for HDF5 files.

  • sector (int, list or None, optional) – Only return files from the given sectors. If None, files from all TIC numbers are returned.

  • camera (int, list or None, optional) – Only return files from the given camera. If None, files from all cameras are returned.

  • ccd (int, list or None, optional) – Only return files from the given ccd. If None, files from all ccds are returned.

Returns

List of paths to HDF5 files matching constraints.

Return type

list

photometry.utilities.find_nearest(array, value)[source]

Search array for value and return the index where the value is closest.

Parameters
  • array (ndarray) – Array to search.

  • value – Value to search array for.

Returns

Index of array closest to value.

Return type

int

Raises

ValueError – If value is NaN.

Code author: Rasmus Handberg <rasmush@phys.au.dk>

photometry.utilities.find_tpf_files(rootdir, starid=None, sector=None, camera=None, ccd=None, cadence=None, findmax=None)[source]

Search directory recursively for TESS Target Pixel Files.

The function is cached, meaning the first time it is run on a particular rootdir the list of files in that directory will be read and cached to memory and used in subsequent calls to the function. This means that any changes to files on disk after the first call of the function will not be picked up in subsequent calls to this function.

Parameters
  • rootdir (str) – Directory to search recursively for TESS TPF files.

  • starid (int, optional) – Only return files from the given TIC number. If None, files from all TIC numbers are returned.

  • sector (int, optional) – Only return files from the given sector. If None, files from all sectors are returned.

  • camera (int or None, optional) – Only return files from the given camera number (1-4). If None, files from all cameras are returned.

  • ccd (int, optional) – Only return files from the given CCD number (1-4). If None, files from all CCDs are returned.

  • cadence (int, optional) – Only return files from the given cadence (20 or 120). If None, files from all cadences are returned.

  • findmax (int, optional) – Maximum number of files to return. If None, return all files.

Note

Filtering on camera and/or ccd will cause the program to read the headers of the files in order to determine the camera and ccd from which they came. This can significantly slow down the query.

Returns

List of full paths to TPF FITS files found in directory. The list will

be sorted according to the filename of the files, i.e. primarily by time.

Return type

list

photometry.utilities.integratedGaussian(x, y, flux, x_0, y_0, sigma=1)[source]

Evaluate a 2D symmetrical Gaussian integrated in pixels.

Parameters
  • x (numpy.ndarray) – x coordinates at which to evaluate the PSF.

  • y (numpy.ndarray) – y coordinates at which to evaluate the PSF.

  • flux (float) – Integrated value.

  • x_0 (float) – Centroid position.

  • y_0 (float) – Centroid position.

  • sigma (float, optional) – Standard deviation of Gaussian. Default=1.

Returns

2D Gaussian integrated pixel values at (x,y).

Return type

numpy array

Example:

>>> import numpy as np
>>> X, Y = np.meshgrid(np.arange(-1,2), np.arange(-1,2))
>>> integratedGaussian(X, Y, 10, 0, 0)
array([[ 0.58433556,  0.92564571,  0.58433556],
        [ 0.92564571,  1.46631496,  0.92564571],
        [ 0.58433556,  0.92564571,  0.58433556]])
photometry.utilities.load_ffi_fits(path, return_header=False, return_uncert=False)[source]

Load FFI FITS file.

Calibrations columns and rows are trimmed from the image.

Parameters
  • path (str) – Path to FITS file.

  • return_header (bool, optional) – Return FITS headers as well. Default is False.

Returns

Full Frame Image. list: If return_header is enabled, will return a dict of the FITS headers.

Return type

numpy.ndarray

photometry.utilities.load_sector_settings(sector=None)[source]
photometry.utilities.load_settings()[source]

Load settings.

Returns

Return type

configparser.ConfigParser

photometry.utilities.mag2flux(mag, zp=20.451)[source]

Convert from magnitude to flux using scaling relation from aperture photometry. This is an estimate.

The default scaling is based on TASOC Data Release 5 from sectors 1-5.

Parameters
  • mag (ndarray) – Magnitude in TESS band.

  • zp (float) – Zero-point to use in scaling. Default is estimated from TASOC Data Release 5 from TESS sectors 1-5.

Returns

Corresponding flux value

Return type

ndarray

photometry.utilities.move_median_central(x, width_points, axis=0)[source]
photometry.utilities.radec_to_cartesian(radec)[source]

Convert spherical coordinates as (ra, dec) pairs to cartesian coordinates (x,y,z).

Parameters

radec (ndarray) – Array with ra-dec pairs in degrees.

Returns

(x,y,z) coordinates corresponding to input coordinates.

Return type

ndarray

photometry.utilities.rms_timescale(time, flux, timescale=0.041666666666666664)[source]

Compute robust RMS on specified timescale. Using MAD scaled to RMS.

Parameters
  • time (ndarray) – Timestamps in days.

  • flux (ndarray) – Flux to calculate RMS for.

  • timescale (float, optional) – Timescale to bin timeseries before calculating RMS. Default=1 hour.

Returns

Robust RMS on specified timescale.

Return type

float

Code author: Rasmus Handberg <rasmush@phys.au.dk>

photometry.utilities.sphere_distance(ra1, dec1, ra2, dec2)[source]

Calculate the great circle distance between two points using the Vincenty formulae.

Parameters
  • ra1 (float or ndarray) – Longitude of first point in degrees.

  • dec1 (float or ndarray) – Lattitude of first point in degrees.

  • ra2 (float or ndarray) – Longitude of second point in degrees.

  • dec2 (float or ndarray) – Lattitude of second point in degrees.

Returns

Distance between points in degrees.

Return type

ndarray

photometry.utilities.sqlite_drop_column(conn, table, col)[source]

Drop table column from SQLite table.

Since SQLite does not have functionality for dropping/deleting columns in existing tables, this function can provide this functionality. This is done by temporarily copying the entire table, so this can be quite an expensive operation.

Parameters
  • conn (sqlite3.Connection) – Connection to SQLite database.

  • table (str) – Table to drop column from.

  • col (str) – Column to be dropped from table.

Code author: Rasmus Handberg <rasmush@phys.au.dk>

photometry.utilities.to_tuple(inp, default=None)[source]

Convert iterable or single values to tuple.

This function is used for converting inputs, perticularly for preparing input to functions cached with functools.lru_cache(), to ensure inputs are hashable.

Parameters
  • inp – Input to convert to tuple.

  • default – If input is None return this instead.

Returns

inp converted to tuple.

Return type

tuple

photometry.utilities.mad_to_sigma = 1.482602218505602

Constant for converting from MAD to SIGMA. Constant is 1/norm.ppf(3/4)