pypeit.core.parse module
parse module.
- pypeit.core.parse.binning2string(binspectral, binspatial)[source]
Convert the binning from integers to a string following the PypeIt convention order, spectral then spatial.
- Parameters:
- Returns:
Comma-separated binning along the spectral and spatial directions; e.g.,
2,1- Return type:
- pypeit.core.parse.fix_config_par_image_location(par)[source]
Fix mosaic image locations as parsed by configobj.
When, e.g., defining a slit to remove, the user sets:
rm_slits = (1,2,3):1500:331; (1,2,3):1500:635
The configobj parser turns this into
['(1', '2', '3):1500:331; (1', '2', '3):1500:635']. This function converts this back to the expected format:['(1,2,3):1500:331', '(1,2,3):1500:635'].
- pypeit.core.parse.get_dnum(det, caps=False, prefix=True)[source]
Convert a detector index into a string used by the settings dictionary or other bits of code. Best to keep at two digits
- pypeit.core.parse.load_sections(string, fmt_iraf=True)[source]
From the input string, return the coordinate sections. In IRAF format (1 index) or Python
- pypeit.core.parse.parse_binning(binning: str)[source]
Parse input binning into binspectral, binspatial
Note that for some instruments, the meaning will be swapped if parsed directly from the Header. The developer needs to react accordingly..
- Parameters:
binning (
str, numpy.ndarray,tuple) –The spectral and spatial binning. Several formats are supported, including the following examples. Note that in all examples, the binning in the spectral direction is 2, and the binning in the spatial direction is 1:
string format
comma delimited string (e.g.
2,1)x delimited string (e.g.
2x1)space delimited string (e.g.
2 1)'None'will always assume 1x1 binning
tuple format
this must be of the form of tuple, for example:
(2,1)
numpy array
this must be of the form of tuple, for example:
np.array([2,1])
- Returns:
binspectral, binspatial as integers
- Return type:
- pypeit.core.parse.parse_image_location(inp, spec)[source]
Parse a colon-separated string with a detector/mosaic identifier and a series of floats.
This function should be used to parse a single image location. Multiple image locations are generally separated by semi-colons; the
inpstring provided must not contain semi-colons.This is primarily used for two purposes: setting locations in a PypeIt-reduced image (e.g., adding or removing slits) or to define a manual extraction aperture (see Manual Extraction).
- Parameters:
inp (
str) – Colon-separated string with a detector identifier and 2 to 3 numbers. Must not contain semi-colons.spec (
Spectrograph) – Spectrograph class used to interpret the detector/mosaic identifier.
- Returns:
Flag that detector integer as negative, the detector identifier returned as a string (e.g., DET01, MSC01), and the set of floats.
- Return type:
- Raises:
PypeItError – Raised if the
inpstring contains a semi-colon, if a mosaic is identified that is not valid for the provided spectrograph, or if there is an issue constructing the detector/mosaic identifier.
Examples
Setup, where
keck_niresis just used as an example of a single detector spectrograph.>>> from pypeit.spectrographs.util import load_spectrograph >>> from pypeit.core import parse >>> spec = load_spectrograph('keck_nires')
The detector can be negative:
>>> parse.parse_image_location('-1:34.5:400.1:4', spec) (True, 'DET01', 34.5, 400.1, 4.0)
or positive:
>>> parse.parse_image_location('1:34.5:400.1:4', spec) (False, 'DET01', 34.5, 400.1, 4.0)
This will fail because
(1,2,3)is not an allowed mosaic forkeck_nires.>>> try: ... parse.parse_image_location('(1,2,3):34.5:400.1:4', spec) ... except: ... print('failed') [ERROR] :: (1, 2, 3) is not a valid mosaic for keck_nires. failed
Now for a mosaic:
>>> spec = load_spectrograph('gemini_gmos_south_ham') >>> parse.parse_image_location('(1,2,3):34.5:400.1:4', spec) (False, 'MSC01', 34.5, 400.1, 4.0) >>> parse.parse_image_location('(-1,-2,-3):34.5:400.1:4', spec) (True, 'MSC01', 34.5, 400.1, 4.0) >>> parse.parse_image_location('2:34.5:400.1', spec) (False, 'DET02', 34.5, 400.1)
- pypeit.core.parse.parse_slitspatnum(slitspatnum)[source]
Parse the
slitspatnuminto a list of detectors and SPAT_IDs.- Parameters:
slitspatnum (
str,list) – A single string or list of strings to parse.- Returns:
Two arrays with the list of 1-indexed detectors (str) and spatial pixels coordinates for each slit. The shape of each array is
(nslits,), wherenslitsis the number ofslitspatnumentries parsed (1 if only a single string is provided).- Return type:
- pypeit.core.parse.sec2slice(subarray, one_indexed=False, include_end=False, require_dim=None, binning=None)[source]
Convert a string representation of an array subsection (slice) into a list of slice objects.
- Parameters:
subarray (
str) – The string to convert. Should have the form of normal slice operation, ‘start:stop:step’. The parser ignores whether or not the string has the brackets ‘[]’, but the string must contain the appropriate ‘:’ and ‘,’ characters.one_indexed (
bool, optional) – The string should be interpreted as 1-indexed. Default is to assume python indexing.include_end (
bool, optional) – If the end is defined, adjust the slice such that the last element is included. Default is to exclude the last element as with normal python slicing.require_dim (
int, optional) – Test if the string indicates the slice along the proper number of dimensions.binning (
str, optional) – Assume the slice is for an unbinned array and adjust the returned slice for this binning in each dimension. If two dimensional, the format of this string must be, e.g., 1,2 for unbinned rows and a factor of 2 binning along columns.
- Returns:
A tuple of slice objects, one per dimension of the prospective array.
- Return type:
- Raises:
TypeError – Raised if the input subarray is not a string.
ValueError – Raised if the string does not match the required dimensionality or if the string does not look like a slice.
- pypeit.core.parse.str2list(inp, length=None)[source]
Expand a string with a comma-separated set of integers and slices into a list of the relevant integers.
Setting a maximum length of the list to 10, examples of the allowed syntax and result are:
‘all’: [0,1,2,3,4,5,6,7,8,9]
‘:4’: [0,1,2,3]
‘3:5,8:’: [3,4,8,9]
‘3,1:5,6’: [1,2,3,4,6]
Note the function removes any non-unique integers (see the last example).
- Parameters:
- Returns:
List of parsed integers.
- Return type: