pygrife.extractor_main module#

Organise the forced extraction tool, and hook into grizli methods.

class pygrife.extractor_main.GrismExtractor(field_root, in_dir, out_dir, seg_path=None)#

Bases: object

A class for extracting additional objects from processed grism data.

This class enables the extraction of objects from arbitrary regions of existing slitless spectroscopic exposures. This allows the modification of both catalogues and contamination maps previously derived using grizli.

Parameters:
field_rootstr

The root name of the catalogue and processed images, e.g. '{field_root}-ir.cat.fits'.

in_dirstr or os.PathLike

The input directory, containing all the original (unmodified) grism files, mosaics, and the segmentation map. Often named 'Prep/'.

out_dirstr or os.PathLike

The output directory, where all the modified files will be saved. Any relevant files are copied from in_dir to out_dir, if they do not already exist.

seg_pathstr or os.PathLike, optional

The path pointing to the original segmentation map. If not supplied, this will have to be loaded later.

Attributes:
catalogueastropy.table.Table

The multiband catalogue used by grizli.

field_rootstr

The root name of the catalogue and processed images.

grpgrizli.multifit.GroupFLT

The container for multiple grism exposures.

in_diros.PathLike

The input directory.

out_diros.PathLike

The output directory, where all the modified files are saved.

seg_hdrastropy.io.fits.Header

The header of the current segmentation map.

seg_mapnumpy:array_like

The current segmentation map.

seg_namestr

The name of the current segmentation map.

seg_wcsastropy.wcs.WCS

The WCS of the current segmentation map.

Initialise the extractor object, and make copies of the relevant files.

load_orig_seg_map(seg_path, ext=0)#

Load a segmentation map into memory.

The stored name and WCS information are also updated.

Parameters:
seg_pathstr or os.PathLike

The path pointing to the segmentation map.

extint or str, optional

The number or name of the HDUList extension containing the segmentation data, by default 0.

regen_multiband_catalogue(**kwargs)#

Recreate the grizli catalogue for the current segmentation map.

Parameters:
**kwargsdict, optional

Keyword arguments are passed through to regen_multiband_catalogue.

Returns:
Table

The multiband catalogue.

load_grism_files(grism_files=None, detection_filter='ir', pad=800, cpu_count=4, catalog_path=None, seg_path=None, **kwargs)#

Load an existing set of grism files into memory.

Load (or reload) a set of "*GrismFLT" files into memory, keeping track of the current segmentation map, and the map used to generate the contamination model.

Parameters:
grism_filesnumpy:array_like, optional

An explicit list of the grism files to use. By default, all "*GrismFLT.fits" files in the output directory will be used.

detection_filterstr, optional

The filter image used for the source detection, by default "ir". This is used to locate the catalogue.

padint or tuple[int, int], optional

The padding in pixels, allowing modelling of sources outside the detector field of view. If a tuple is supplied, this is taken as (pady, padx). Defaults to 800pix in both axes.

cpu_countint, optional

If < 0, load files serially. If > 0, load files in cpu_count parallel processes. Use all available cores if cpu_count=0. Defaults to 4 processes.

catalog_pathstr or os.Pathlike, optional

The path of the catalogue to use when loading the grism files, Defaults to "{self.field_root}-{detection_filter}.cat.fits".

seg_pathstr or os.Pathlike, optional

The path of the segmentation map to use when loading the grism files, if different to self.seg_map. Defaults to "{self.field_root}-{detection_filter}_seg.fits".

**kwargsdict, optional

Any other keyword arguments, passed through to GroupFLT().

Returns:
GroupFLT

The container for multiple grism exposures.

Notes

Be careful with the cpu_count - the memory footprint per process is extremely high (e.g. with a 6P/8E CPU, and 32GB RAM, I typically limit this to <=6 cores).

match_objects(targets, column_names=None, return_all=False)#

Match a table of targets against the existing catalogue.

Parameters:
targetsastropy.table.Table

The targets to match.

column_namesdict | None, optional

A mapping of old and new column names for the targets table. By default None.

return_allbool, optional

Return the object table and all failed matches. By default False, in which case only the matched object IDs will be returned.

Returns:
npt.NDArray | tuple[npt.NDArray, astropy.table.Table, astropy.table.Table]

Either an array of object IDs, or also the table of matched objects, and the table of failed objects.

extract_spectra(obj_id_list, z_range=[0.0, 0.5], fit_kwargs=None, beams_kwargs=None, multibeam_kwargs=None, spectrum_1d=None, is_cgs=True, trim_sensitivity=False, fit_trace_shift=False)#

Perform a full extraction of the specified objects.

Parameters:
obj_id_listnumpy:array_like

The object ids in the segmentation map which will be extracted.

z_rangenumpy:array_like, optional

The redshift range to consider for the extraction, by default 0 < z < 0.5.

fit_kwargsdict, optional

Keyword arguments to pass to generate_fit_params.

beams_kwargsdict, optional

Keyword arguments to pass to get_beams.

multibeam_kwargsdict, optional

Keyword arguments to pass to MultiBeam.

spectrum_1d[wavelengths, flux], optional

The flux spectrum and corresponding wavelengths of the object in the model. By default, this is calculated automatically from the stored object_dispersers.

is_cgsbool, optional

The flux units of spectrum_1d[1] are cgs f_lambda flux densities, rather than normalised in the detection band, by default True.

trim_sensitivitybool, optional
refine_contam_model_with_fits(spectrum='full', max_chinu=5, fit_files=None, mag_limit=25, get_beams=None)#

Refine existing contamination models.

Refine the full-field grism models with the best fit spectra from individual extractions. [Modified version of a grizli function]

Parameters:
spectrumstr, optional

The component of the best-fit spectrum to use, either "full" or "continuum".

max_chinuint or float, optional

The maximum reduced chi-squared value of the fit to accept, in order to refine the contamination model with the resulting spectrum, by default 5.

fit_fileslist[str] or list[os.PathLike] or None, optional

An explicit list of the best-fit files to use. By default, all *full.fits files in the current directory will be used.

Returns:
bool

Returns False if the contamination maps are not modified.

add_circ_obj(radius=<Quantity 3. arcsec>, inner_radius=0, centre=None, init_id=None, **kwargs)#

Add one or more circular objects to the segmentation map.

Parameters:
radiusastropy.units.Quantity or numpy:array_like, optional

The outer radius of the aperture, by default 3 arcseconds.

inner_radiusastropy.units.Quantity or numpy:array_like, optional

If specified, an annulus will be extracted instead of an aperture.

centreastropy.coordinates.SkyCoord, optional

The centre of the aperture.

init_idint, optional

The ID to assign to the object. If multiple coordinates are given, the ID will increase in integer steps from init_id. By default, init_id will start at max(seg_map)+1.

**kwargsdict, optional

Any inputs accepted by SkyCoord, if centre is None.

Returns:
ndarray

The ID(s) corresponding to the new object(s) in the segmentation map.

add_sector_obj(radius=<Quantity 3. arcsec>, inner_radius=0, centre=None, segments=4, angle=0, init_id=None, **kwargs)#

Add one or more sector objects to the segmentation map.

The new objects are obtained by dividing the requested circular objects in a specified number of sectors.

Parameters:
radiusastropy.units.Quantity or numpy:array_like, optional

The outer radius of the aperture, by default 3 arcseconds.

inner_radiusQuantity or numpy:array_like, optional

If specified, an annulus will be extracted instead of an aperture.

centreastropy.coordinates.SkyCoord, optional

The centre of the aperture.

segmentsint, optional

The number of sectors into which each object will be divided, by default 4.

angleastropy.units.Quantity or float, optional

The position angle offset, by default 0.

init_idint, optional

The ID to assign to the object. If multiple coordinates are given, the ID will increase in integer steps from init_id. By default, init_id will start at max(seg_map)+1.

**kwargsdict, optional

Any inputs accepted by SkyCoord, if centre is None.

Returns:
ndarray

The ID(s) corresponding to the new object(s) in the segmentation map.

add_reg_obj(reg_path, format=None, reg_wcs=None, init_id=None)#

Add a new object to the segmentation map from a regions file.

Parameters:
reg_pathstr or os.PathLike

The path pointing to the region file.

formatstr or None, optional

The file format specifier. If None, the format is automatically inferred from the file extension.

reg_wcsastropy.wcs.WCS or None, optional

The WCS to use to convert pixels to world coordinates. By default, the segmentation map WCS will be used.

init_idint, optional

The ID to assign to the object. If multiple coordinates are given, the ID will increase in integer steps from init_id. By default, init_id will start at max(seg_map)+1.

Returns:
int

The ID corresponding to the new object in the segmentation map.

Raises:
AttributeError

If the segmentation map has not been loaded.

ImportError

If the regions package is not available.

Exception

If the supplied regions file cannot be read.