dmsky.utils subpackage

dmsky.utils.coords module

Utilities for working with coordinates.

dmsky.utils.coords.angsep(lon1, lat1, lon2, lat2)[source]

Angular separation (deg) between two sky coordinates. Faster than creating astropy coordinate objects.

Parameters:
  • lon1 (numpy.array) –
  • lat1 (numpy.array) –
  • lon2 (numpy.array) –
  • lat2 (numpy.array) – Coordinates (in degrees)
Returns:

dist – Angular seperations (in degrees)

Return type:

numpy.array

Notes

The angular separation is calculated using the Vincenty formula [1], which is slighly more complex and computationally expensive than some alternatives, but is stable at at all distances, including the poles and antipodes.

[1] http://en.wikipedia.org/wiki/Great-circle_distance

dmsky.utils.coords.cel2gal(ra, dec)[source]

Convert celestial coordinates (J2000) to Galactic coordinates. (Much faster than astropy for small arrays.)

Parameters:
  • ra (numpy.array) –
  • dec (numpy.array) – Celestical Coordinates (in degrees)
Returns:

  • glon (numpy.array)
  • glat (numpy.array) – Galactic Coordinates (in degrees)

dmsky.utils.coords.gal2cel(glon, glat)[source]

Convert Galactic coordinates to celestial coordinates (J2000). (Much faster than astropy for small arrays.)

Parameters:
  • glon (numpy.array) –
  • glat (numpy.array) – Galactic Coordinates (in degrees)
Returns:

  • ra (numpy.array)
  • dec (numpy.array) – Celestical Coordinates (in degrees)

dmsky.utils.speed module

Utilities for speed testing

dmsky.utils.speed.speedtest(func, *args, **kwargs)[source]

Test the speed of a function.

dmsky.utils.healpix module

dmsky.utils.stat_funcs module

Utilities for statistical operations

dmsky.utils.stat_funcs.gauss(x, mu, sigma=1.0)[source]

Gaussian

dmsky.utils.stat_funcs.lgauss(x, mu, sigma=1.0, logpdf=False)[source]

Log10 normal distribution…

Parameters:
  • x (numpy.array or list) – Parameter of interest for scanning the pdf
  • mu (float) – Peak of the lognormal distribution (mean of the underlying normal distribution is log10(mu)
  • sigma (float) – Standard deviation of the underlying normal distribution
  • logpdf (bool) – Define the PDF in log space
Returns:

vals – Output values, same shape as x

Return type:

numpy.array

dmsky.utils.stat_funcs.ln_log10norm(x, mu, sigma=1.0)[source]

Natural log of base 10 lognormal

dmsky.utils.stat_funcs.ln_norm(x, mu, sigma=1.0)[source]

Natural log of scipy norm function truncated at zero

dmsky.utils.stat_funcs.lngauss(x, mu, sigma=1.0)[source]

Natural log of a Gaussian

dmsky.utils.stat_funcs.lnlgauss(x, mu, sigma=1.0, logpdf=False)[source]

Log-likelihood of the natural log of a Gaussian

dmsky.utils.stat_funcs.log10norm(x, mu, sigma=1.0)[source]

Scale scipy lognorm from natural log to base 10 x : input parameter mu : mean of the underlying log10 gaussian sigma : variance of underlying log10 gaussian

dmsky.utils.stat_funcs.lognorm(x, mu, sigma=1.0)[source]

Log-normal function from scipy

dmsky.utils.stat_funcs.norm(x, mu, sigma=1.0)[source]

Scipy norm function

dmsky.utils.tools module

Random python tools

dmsky.utils.tools.get_items(items, library)[source]

Grab list items recursing through existing rosters. Careful of recursion traps.

Some prefix comprehension: ‘++’ = Always add ‘+’ = Add only if unique ‘–’ = Remove last occurence ‘-’ = Remove all occurences

dmsky.utils.tools.getnest(d, *keys)[source]

Function to return nested keys.

dmsky.utils.tools.item_prefix(item)[source]

Get the item prefix (‘+’,’++’,’-‘,’–‘,’’).

dmsky.utils.tools.item_version(item)[source]

Split the item and version based on sep ‘:’

dmsky.utils.tools.merge_dict(d0, d1, add_keys=True, append=False)[source]

Merge two target dicts into a new dict.

Parameters:
  • d0 (Base dictionary) –
  • d1 (Update dictionary) –
Returns:

d

Return type:

A new dictionary merging d and u

dmsky.utils.tools.update_dict(d0, d1, add_keys=True, append=False)[source]

Recursively update the contents of dictionary d0 with the contents of python dictionary d1.

Parameters:
  • d0 (Base dictionary) –
  • d1 (Update dictionary) –
dmsky.utils.tools.yaml_dump(x, filename)[source]

Dump object to a yaml file (use libyaml when available) x : output to dump to the file filename : output file (can be file-type or path string)

dmsky.utils.tools.yaml_load(filename)[source]

Load a yaml file (use libyaml when available)

dmsky.utils.units module

Simple module to deal with unit conversion

class dmsky.utils.units.Units[source]

Bases: object

Simple class to keep track of unit conversions

cm = 1
cm3_s = 1.0
static convert_from(value, key)[source]

Convert to cgs units from a different type of units

Parameters:
  • value (scalar or array-like, the input value(s)) –
  • key (str, a key corresponding to one of the globals defined above) –
Returns:

Return type:

the input values, converted to cgs units

static convert_to(value, key)[source]

Convert from cgs units to a different type of units

Parameters:
  • value (scalar or array-like, the input value(s)) –
  • key (str, a key corresponding to one of the globals defined above) –
Returns:

Return type:

the input values, converted to requested units

deg2 = 0.00030461741978670857
g = 1.0
g_cm3 = 1.0
static get_value(key)[source]

Get a conversion value based on a key

This is here to make it easy to automate unit conversion

Parameters:key (str, a key corresponding to one of the globals defined above) –
Returns:
Return type:the conversion constant
gev = 1.78266e-24
gev2_cm5 = 3.1778766755999995e-48
gev_cm2 = 1.78266e-24
gev_cm3 = 1.78266e-24
hr = 3600.0
k = 'gev_cm3'
km = 100000.0
kpc = 3.08568e+21
m = 100.0
m2 = 10000.0
map_from_astropy = {'GeV / cm2': 'gev_cm2', 'GeV / cm3': 'gev_cm3', 'GeV2 / cm5': 'gev2_cm5', 'cm3 / s': 'cm3_s', 'g / cm3': 'g_cm3'}
map_to_astropy = {'cm3_s': 'cm3 / s', 'g_cm3': 'g / cm3', 'gev2_cm5': 'GeV2 / cm5', 'gev_cm2': 'GeV / cm2', 'gev_cm3': 'GeV / cm3'}
msun = 1.98892e+33
msun2_kpc5 = 1.4141002588357058e-41
msun2_pc5 = 1.4141002588357058e-26
msun_kpc3 = 6.769625720905608e-32
msun_pc3 = 6.769625720905608e-23
pc = 3.08568e+18
v = 'GeV / cm3'

dmsky.utils.wcs module

Interface with wcs.

Adapted from fermipy.skymap

dmsky.utils.wcs.create_image_hdu(data, wcs, name=None)[source]

Create a astropy.io.fits.ImageHDU object

dmsky.utils.wcs.create_image_wcs(skydir, cdelt, npix, coordsys='CEL', projection='AIT')[source]

Create a blank image and associated WCS object

dmsky.utils.wcs.create_wcs(skydir, coordsys='CEL', projection='AIT', cdelt=1.0, crpix=1.0, naxis=2, energies=None)[source]

Create a WCS object. :param skydir: Sky coordinate of the WCS reference point. :type skydir: ~astropy.coordinates.SkyCoord :param coordsys: :type coordsys: str :param projection: :type projection: str :param cdelt: :type cdelt: float :param crpix: In the first case the same value is used for x and y axes :type crpix: float or (float,float) :param naxis: Number of dimensions of the projection. :type naxis: {2, 3} :param energies: Array of energies that defines the third dimension if naxis=3. :type energies: array-like

dmsky.utils.wcs.get_pixel_skydirs(npix, wcs)[source]

Get a list of sky coordinates for the centers of every pixel.

dmsky.utils.wcs.write_image_hdu(filename, data, wcs, name=None, clobber=False)[source]

Write an image to a file

Module contents