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:
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.
- 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()
orcontextlib.redirect_stderr()
to redirect streams to the given logger. Can be useful for wrapping codes which uses normalprint()
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>
- class photometry.utilities.TqdmLoggingHandler(*args, **kwargs)[source]
Bases:
Handler
- 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
anddec
. 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_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 tovalue
.- Return type:
int
- Raises:
ValueError – If
value
is NaN.
Code author: Rasmus Handberg <rasmush@phys.au.dk>
- 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.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.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.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
isNone
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)