mangadap.proc.util module

Provides a set of processing utility functions for the MaNGA DAP.


License

Copyright © 2019, SDSS-IV/MaNGA Pipeline Group


mangadap.proc.util.HDUList_mask_wavelengths(hdu, bitmask, bitmask_flag, wave_limits, wave_ext='WAVE', mask_ext='MASK', invert=False)[source]

Mask pixels in a specified wavelength range by turning on the bit value in the specified extention in a provided HDUList object.

Parameters:
  • hdu (astropy.io.fits.HDUList) – HDUList to alter

  • (class (bitmask) – BitMask): Bit mask object used to turn on the named bit mask.

  • bitmask_flag (str) – Name of the bit to turn on.

  • wave_limits (list or numpy.ndarray) – Two-element array with the low and high wavelength limits.

  • wave_ext (str) – (Optional) Name of the wavelength extension in hdu.

  • mask_ext (str) – (Optional) Name of the mask extension in hdu.

  • invert (bool) – (Optional) Invert the sense of the masking. Instead of masking pixel in the wavelength interval, mask all pixels outside it.

Returns:

The modified HDUList object.

Return type:

astropy.io.fits.HDUList

Raises:

Exception – Raised if wave_limits does not have a length of two.

mangadap.proc.util.flux_to_fnu(wave, flambda, unit_norm=1e-17)[source]

Convert a spectrum with flux per unit wavelength to flux per unit frequency; i.e., calculate

\[F_{\nu} = F_{\lambda} \frac{d\lambda}{d\nu} = F_{\lambda} \frac{\lambda^2}{c},\]

where the first two arguments of the function are \(\lambda\) and \(F_{\lambda}\). The input wavelength units are expected to be angstroms, and the input flux units are expected to be \(n\ {\rm erg\ s}^{-1}\ {\rm cm}^{-2}\ {\rm A}^{-1}\), where \(n\) is the value of unit_norm. The output flux units are microjanskys, \(10^{-29} {\rm erg\ s}^{-1}\ {\rm cm}^{-2}\ {\rm Hz}^{-1}\).

Parameters:
  • wave (numpy.ndarray, list) – The vector with the wavelengths in angstroms.

  • flambda (numpy.ndarray, list) – The vector with the flux per unit wavelength (angstroms).

  • unit_norm (float) – (Optional) The unit normalization of the flux. For example, this is \(10^{-17}\) when the flux units are \(10^{-17} {\rm erg\ s}^{-1}\ {\rm cm}^{-2}\ {\rm A}^{-1}\).

Returns:

The flux in units of microjanskys.

Return type:

float,numpy.ndarray

Raises:

ValueError – Raised if the arguments do not have the same shape.

mangadap.proc.util.get_database_key(f)[source]

Construct a key from the provided file or file path.

The key is a capitalized version of the file after removing any extension.

Parameters:

f (str) – The file name or path.

Returns:

The keyword.

Return type:

str

Examples

>>> get_database_key('junk')
'JUNK'
>>> get_database_key('test.par')
'TEST'
>>> get_database_key('/path/to/test.par')
'TEST'
mangadap.proc.util.growth_lim(a, lim, fac=1.0, midpoint=None, default=[0.0, 1.0])[source]

Set the plots limits of an array based on two growth limits.

Parameters:
  • a (array-like) – Array for which to determine limits.

  • lim (float) – Percentage of the array values to cover.

  • fac (float) – (Optional) Factor to increase the range based on the growth limits. Default is no increase.

  • midpoint (float) – (Optional) Force the midpoint of the range to be centered on this value. Default is to middle of growth range.

  • default (list) – (Optional) Default range to return if a has no data. Default is 0 to 1.

Returns:

Lower and upper limits for the range of a plot of the data in a.

Return type:

list

mangadap.proc.util.inverse(d)[source]

Return 1/d, where any division by 0 returns 0 instead of NaN or Inf.

Parameters:

d (scalar-like, array-like) – Data values.

Returns:

Returns 1/d where values with d == 0. are replaced by 0. Return type matches input type: float for scalar, numpy.ndarray for array-like.

Return type:

float, numpy.ndarray

mangadap.proc.util.optimal_scale(dat, mod, wgt=None)[source]

Calculate the optimal scaling of an input model that minimizes the weighted root-mean-square difference between a set of data and a model. When defining the weighted RMS as:

\[{\rm RMS}^2 = \frac{1}{N} \sum_i w_i^2(d_i - f m_i)^2\]

The optimal renormalization factor that minimizes the RMS is:

\[f = \frac{\mathbf{d}^\prime \dot \mathbf{m}^\prime}{||\mathbf{m}^\prime||^2}\]

where \(d^\prime_i = w_i d_i\) and \(m^\prime_i = w_i m_i\).

Parameters:
  • dat (array-like) – Array of data.

  • mod (array-like) – Model to renormalize.

  • wgt (array-like) – (Optional) Array of weights to apply to each residual.

Returns:

The optimal scaling that minimizes the weighted root-mean-square difference betwen the data and the model.

Return type:

float

Raises:

ValueError – Raised if the array sizes do not match.

mangadap.proc.util.replace_with_data_from_nearest_coo(coo, data, replace)[source]

Replace data in array with the spatially nearest neighbor.

Parameters:
  • coo (numpy.ndarray) – A 2D array with the x and y coordinates of all the data. Shape must be (NDATA, 2).

  • data (numpy.ndarray) – A 1D or 2D array with data to replace. The length of the first (or only) axis must be NDATA.

  • replace (numpy.ndarray) – Boolean array that is True for elements that should be replaced on output. Shape must be (NDATA,).

Returns:

The data array with the selected rows replaced with the nearest data set.

Return type:

numpy.ndarray

Raises:

ValueError – Raised if the array sizes are inappropriate.

mangadap.proc.util.sample_growth(a, samples, default=-9999.0, use_interpolate=True)[source]
mangadap.proc.util.select_database(key, directory_path)[source]

Select a database using a keyword and directory path.

Parameters:
  • key (str) – Keyword used to select the method.

  • directory_path (str) – Full path with the valid database files. All files in the directory with a .par extension will be included.

Returns:

Returns the file with the selected database.

Return type:

str

Raises:
  • NotADirectoryError – Raised if the provided directory path does not exist.j

  • KeyError – Raised if the selected keyword cannot be associated with a file in the provided directory.

mangadap.proc.util.select_proc_method(method_key, method_type, method_list=None, available_func=None)[source]

Select a method from a list. One of method_list or available_func must be provided.

Parameters:
  • method_key (str) – Keyword used to select the method.

  • method_type (object) – Object type to check method_list against.

  • method_list (list, optional) – List of methods from which to find the selection keyword. If None, available_func must be provided.

  • available_func (callable, optional) – Callable function that returns a list of default methods in place of method_list. For example, see mangadap.proc.templatelibrary.available_template_libraries().

Returns:

An object with base class mangadap.par.ParSet, containing a set of parameters used to define a method or database.

Return type:

object

Raises:
  • KeyError – Raised if the selected keyword is not among the provided list or if the provided list has more than one identical keyword.

  • TypeError – Raised if the input method_list object is not a list or method_type, or if available function is not a callable function.