pypeit.spec2dobj module

Module for the Spec2DObj class

class pypeit.spec2dobj.AllSpec2DObj[source]

Bases: object

Simple object to hold Spec2DObj objects and perform I/O

Anything that goes into self[‘meta’] must be parseable into a FITS Header

Restrict keys to be type int or ‘meta’ and items to be Spec2DObj

__getitem__(item)[source]

Get an item directly from the internal dict.

build_primary_hdr(raw_header, spectrograph, calib_dir=None, redux_path=None, subheader=None, history=None)[source]

Build the primary header for a spec2d file

Parameters:
  • raw_header (astropy.io.fits.Header) – Header from the raw FITS file (i.e. original header)

  • spectrograph (Spectrograph) – Spectrograph used to obtain the data.

  • calib_dir (str) – Path to the folder with processed calibration frames

  • redux_path (str, optional) – Full path to the reduction output files.

  • subheader (dict, optional) – Generated by subheader_for_spec().

Returns:

The primary header for the output fits file.

Return type:

astropy.io.fits.Header

property detectors

Return the list of detector/mosaic names, assuming they are the list of all keys except for meta.

classmethod from_fits(filename, chk_version=True)[source]
Parameters:
  • filename (str, Path) – Name of the file to read.

  • chk_version (bool, optional) – If True, demand the on-disk datamodel equals the current one. Passed to from_hdu() of DataContainer.

Returns:

The constructed object.

Return type:

AllSpec2DObj

hdr_prefix = 'ALLSPEC2D_'
property keys

Return the list of attributes

write_to_fits(outfile, pri_hdr=None, update_det=None, slitspatnum=None, overwrite=True)[source]

Write the spec2d FITS file

Parameters:
  • outfile (str, Path) – Output filename

  • pri_hdr (astropy.io.fits.Header, optional) – Header to be used in lieu of default Usually generated by build_primary_hdr()

  • slitspatnum (str or list, optional) – Restricted set of slits for reduction If provided, do not clobber the existing file but only update the indicated slits. Useful for re-running on a subset of slits

  • pri_hdr() – Baseline primary header. If None, initial primary header is empty. Usually generated by build_primary_hdr()

  • update_det (list, optional) – If the output file already exists, this sets the list of detectors/mosaics to update with the data in this object. If None, a new file is constructed from scratch, if overwrite is True. Otherwise, the existing file is read and any detectors in that file but not in this one are added to this object. I.e., if update_det is not None, this method can alter the object.

  • overwrite (bool, optional) – If true and the output file already exists, overwrite it. The combination of this and update_det may also alter this object based on the existing file.

class pypeit.spec2dobj.Spec2DObj(sciimg, ivarraw, skymodel, bkg_redux_skymodel, objmodel, ivarmodel, scaleimg, waveimg, bpmmask, detector, sci_spat_flexure, sci_spec_flexure, vel_type, vel_corr, slits, wavesol, tilts, maskdef_designtab)[source]

Bases: DataContainer

Class to handle 2D spectral image outputs of PypeIt

One generates one of these Objects for each detector in the exposure.

The datamodel attributes are:

Version: 1.1.1

Attribute

Type

Array Type

Description

bkg_redux_skymodel

numpy.ndarray

numpy.floating

2D sky model image without the background subtraction (float32)

bpmmask

ImageBitMaskArray

2D bad-pixel mask for the image

det

int

Detector index

detector

DetectorContainer, Mosaic

Detector or Mosaic metadata

ivarmodel

numpy.ndarray

numpy.floating

2D ivar model image (float32)

ivarraw

numpy.ndarray

numpy.floating

2D processed inverse variance image (float32)

maskdef_designtab

astropy.table.table.Table

Table with slitmask design and object info

med_chis

numpy.ndarray

numpy.floating

Median of the chi image for each slit/order

objmodel

numpy.ndarray

numpy.floating

2D object model image (float32)

scaleimg

numpy.ndarray

numpy.floating

2D multiplicative scale image [or a single scalar as an array] that has been applied to the science image (float32)

sci_spat_flexure

float

Shift, in spatial pixels, between this image and SlitTrace

sci_spec_flexure

astropy.table.table.Table

Global shift of the spectrum to correct for spectralflexure (pixels). This is based on the sky spectrum atthe center of each slit

sciimg

numpy.ndarray

numpy.floating

2D processed science image (float32)

skymodel

numpy.ndarray

numpy.floating

2D sky model image (float32)

slits

SlitTraceSet

SlitTraceSet defining the slits

std_chis

numpy.ndarray

numpy.floating

std of the chi image for each slit/order

tilts

numpy.ndarray

numpy.floating

2D tilts image (float64)

vel_corr

float

Relativistic velocity correction for wavelengths

vel_type

str

Type of reference frame correction (if any). Options are listed in the routine: WavelengthSolutionPar.valid_reference_frames() Current list: observed, heliocentric, barycentric

waveimg

numpy.ndarray

numpy.floating

2D wavelength image in vacuum (float64)

wavesol

astropy.table.table.Table

Table with WaveCalib diagnostic info

Args:

head0

Primary header if instantiated from a FITS file

Type:

astropy.io.fits.Header

_base_header(hdr=None)[source]

Override the base class method to add useful/identifying internals to the header.

Parameters:

hdr (astropy.io.fits.Header, optional) – Header object to update. The object is modified in-place and also returned. If None, an empty header is instantiated, edited, and returned.

Returns:

The initialized (or edited) fits header.

Return type:

astropy.io.fits.Header

_bundle()[source]

Over-write default _bundle() method to separate the DetectorContainer into its own HDU

Returns:

A list of dictionaries, each list element is written to its own fits extension. See the description above.

Return type:

list

_validate()[source]

Assert that the detector has been set and that the bitmask is correct.

calc_chi_slit(slitidx: int, pad: int = None, remove_object: bool = True)[source]

Calculate a chi map and run some stats on it for a given slit/order

Parameters:
  • slitidx (int) – Given slit/order

  • pad (int, optional) – Ignore pixels within pad of edges. Defaults to None.

  • remove_object (bool, optional) – Remove object model (if it exists)

Returns:

np.ndarray (chi image), median (float), std (float)

Return type:

tuple

datamodel = {'bkg_redux_skymodel': {'atype': <class 'numpy.floating'>, 'descr': '2D sky model image without the background subtraction (float32)', 'otype': <class 'numpy.ndarray'>}, 'bpmmask': {'descr': '2D bad-pixel mask for the image', 'otype': <class 'pypeit.images.imagebitmask.ImageBitMaskArray'>}, 'det': {'descr': 'Detector index', 'otype': <class 'int'>}, 'detector': {'descr': 'Detector or Mosaic metadata', 'otype': (<class 'pypeit.images.detector_container.DetectorContainer'>, <class 'pypeit.images.mosaic.Mosaic'>)}, 'ivarmodel': {'atype': <class 'numpy.floating'>, 'descr': '2D ivar model image (float32)', 'otype': <class 'numpy.ndarray'>}, 'ivarraw': {'atype': <class 'numpy.floating'>, 'descr': '2D processed inverse variance image (float32)', 'otype': <class 'numpy.ndarray'>}, 'maskdef_designtab': {'descr': 'Table with slitmask design and object info', 'otype': <class 'astropy.table.table.Table'>}, 'med_chis': {'atype': <class 'numpy.floating'>, 'descr': 'Median of the chi image for each slit/order', 'otype': <class 'numpy.ndarray'>}, 'objmodel': {'atype': <class 'numpy.floating'>, 'descr': '2D object model image (float32)', 'otype': <class 'numpy.ndarray'>}, 'scaleimg': {'atype': <class 'numpy.floating'>, 'descr': '2D multiplicative scale image [or a single scalar as an array] that has been applied to the science image (float32)', 'otype': <class 'numpy.ndarray'>}, 'sci_spat_flexure': {'descr': 'Shift, in spatial pixels, between this image and SlitTrace', 'otype': <class 'float'>}, 'sci_spec_flexure': {'descr': 'Global shift of the spectrum to correct for spectralflexure (pixels). This is based on the sky spectrum atthe center of each slit', 'otype': <class 'astropy.table.table.Table'>}, 'sciimg': {'atype': <class 'numpy.floating'>, 'descr': '2D processed science image (float32)', 'otype': <class 'numpy.ndarray'>}, 'skymodel': {'atype': <class 'numpy.floating'>, 'descr': '2D sky model image (float32)', 'otype': <class 'numpy.ndarray'>}, 'slits': {'descr': 'SlitTraceSet defining the slits', 'otype': <class 'pypeit.slittrace.SlitTraceSet'>}, 'std_chis': {'atype': <class 'numpy.floating'>, 'descr': 'std of the chi image for each slit/order', 'otype': <class 'numpy.ndarray'>}, 'tilts': {'atype': <class 'numpy.floating'>, 'descr': '2D tilts image (float64)', 'otype': <class 'numpy.ndarray'>}, 'vel_corr': {'descr': 'Relativistic velocity correction for wavelengths', 'otype': <class 'float'>}, 'vel_type': {'descr': 'Type of reference frame correction (if any). Options are listed in the routine: WavelengthSolutionPar.valid_reference_frames() Current list: observed, heliocentric, barycentric', 'otype': <class 'str'>}, 'waveimg': {'atype': <class 'numpy.floating'>, 'descr': '2D wavelength image in vacuum (float64)', 'otype': <class 'numpy.ndarray'>}, 'wavesol': {'descr': 'Table with WaveCalib diagnostic info', 'otype': <class 'astropy.table.table.Table'>}}

Provides the class data model. This is undefined in the abstract class and should be overwritten in the derived classes.

The format of the datamodel needed for each implementation of a DataContainer derived class is as follows.

The datamodel itself is a class attribute (i.e., it is a member of the class, not just of an instance of the class). The datamodel is a dictionary of dictionaries: Each key of the datamodel dictionary provides the name of a given datamodel element, and the associated item (dictionary) for the datamodel element provides the type and description information for that datamodel element. For each datamodel element, the dictionary item must provide:

  • otype: This is the type of the object for this datamodel item. E.g., for a float or a numpy.ndarray, you would set otype=float and otype=np.ndarray, respectively.

  • descr: This provides a text description of the datamodel element. This is used to construct the datamodel tables in the pypeit documentation.

If the object type is a numpy.ndarray, you should also provide the atype keyword that sets the type of the data contained within the array. E.g., for a floating point array containing an image, your datamodel could be simply:

datamodel = {'image' : dict(otype=np.ndarray, atype=float, descr='My image')}

More advanced examples are given in the top-level module documentation.

Currently, datamodel components are restricted to have otype that are tuple, int, float, numpy.integer, numpy.floating, numpy.ndarray, or astropy.table.Table objects. E.g., datamodel values for otype cannot be dict.

property detname
classmethod from_file(ifile, detname, chk_version=True)[source]

Instantiate the object from an extension in the specified fits file.

Over-load from_file() to specify detector to read.

Parameters:
  • ifile (str, Path) – Fits file with the data to read

  • detname (str) – The string identifier for the detector or mosaic used to select the data that is read.

  • chk_version (bool, optional) – Passed to from_hdu().

classmethod from_hdu(hdu, detname, chk_version=True)[source]

Override base-class from_hdu() to specify detector to read.

Parameters:
Returns:

2D spectra object.

Return type:

Spec2DObj

gen_qa()[source]

Generate QA for the slits/orders

Saved to the DataContainer

property hdu_prefix

Provides for a dynamic hdu_prefix based on our naming model.

Returns:

Detector/mosaic identifier

Return type:

str

internals = ['calibs', 'process_steps', 'head0']

A list of strings identifying a set of internal attributes that are not part of the datamodel.

select_flag(flag=None, invert=False)[source]

Return a boolean array that selects pixels masked with the specified bits in bpmmask.

For example, to create a bad-pixel mask based on which pixels have cosmic-ray detections, run:

cr_bpm = self.select_flag(flag='CR')

Or, to create a good-pixel mask for all pixels that are not flagged for any reason, run:

gpm = self.select_flag(invert=True)
Parameters:
  • flag (str, array-like, optional) – One or more flags to select when returning the boolean mask. If None, pixels flagged for any reason are returned as True.

  • invert (bool, optional) – If False, the return mask is True for masked pixels, False for good pixels (i.e., a bad-pixel mask). If True, invert the sense of the mask (i.e., create a good-pixel mask, True for good pixels, False for bad pixels).

Returns:

Boolean array where pixels with the selected bits flagged are returned as True (if invert is False); i.e., this is a boolean bad-pixel mask (or a good-pixel mask when invert is True). If flag is not provided, pixels flagged for any reason are returned as True.

Return type:

numpy.ndarray

update_slits(spec2DObj)[source]

Update the object at all good slits in the input object

Parameters:

spec2DObj (Spec2DObj)

Returns:

version = '1.1.1'

Provides the string representation of the class version.

This is currently put to minimal use so far, but will used for I/O verification in the future.

Each derived class should provide a version to guard against data model changes during development.