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:
objectA 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_root
str The root name of the catalogue and processed images, e.g.
'{field_root}-ir.cat.fits'.- in_dir
stroros.PathLike The input directory, containing all the original (unmodified) grism files, mosaics, and the segmentation map. Often named
'Prep/'.- out_dir
stroros.PathLike The output directory, where all the modified files will be saved. Any relevant files are copied from
in_dirtoout_dir, if they do not already exist.- seg_path
stroros.PathLike, optional The path pointing to the original segmentation map. If not supplied, this will have to be loaded later.
- field_root
- Attributes:
- catalogue
astropy.table.Table The multiband catalogue used by
grizli.- field_root
str The root name of the catalogue and processed images.
- grp
grizli.multifit.GroupFLT The container for multiple grism exposures.
- in_dir
os.PathLike The input directory.
- out_dir
os.PathLike The output directory, where all the modified files are saved.
- seg_hdr
astropy.io.fits.Header The header of the current segmentation map.
- seg_mapnumpy:array_like
The current segmentation map.
- seg_name
str The name of the current segmentation map.
- seg_wcs
astropy.wcs.WCS The WCS of the current segmentation map.
- catalogue
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_path
stroros.PathLike The path pointing to the segmentation map.
- ext
intorstr, optional The number or name of the HDUList extension containing the segmentation data, by default 0.
- seg_path
- regen_multiband_catalogue(**kwargs)#
Recreate the
grizlicatalogue for the current segmentation map.- Parameters:
- **kwargs
dict, optional Keyword arguments are passed through to
regen_multiband_catalogue.
- **kwargs
- Returns:
TableThe 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_filter
str, optional The filter image used for the source detection, by default
"ir". This is used to locate the catalogue.- pad
intortuple[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_count
int, optional If < 0, load files serially. If > 0, load files in
cpu_countparallel processes. Use all available cores ifcpu_count=0. Defaults to 4 processes.- catalog_path
stroros.Pathlike, optional The path of the catalogue to use when loading the grism files, Defaults to
"{self.field_root}-{detection_filter}.cat.fits".- seg_path
stroros.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".- **kwargs
dict, optional Any other keyword arguments, passed through to GroupFLT().
- Returns:
GroupFLTThe 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:
- targets
astropy.table.Table The targets to match.
- column_names
dict|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.
- targets
- 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_kwargs
dict, optional Keyword arguments to pass to
generate_fit_params.- beams_kwargs
dict, optional Keyword arguments to pass to
get_beams.- multibeam_kwargs
dict, 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:
- spectrum
str, optional The component of the best-fit spectrum to use, either
"full"or"continuum".- max_chinu
intorfloat, 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_files
list[str] orlist[os.PathLike] orNone, optional An explicit list of the best-fit files to use. By default, all
*full.fitsfiles in the current directory will be used.
- spectrum
- 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:
- radius
astropy.units.Quantityor numpy:array_like, optional The outer radius of the aperture, by default 3 arcseconds.
- inner_radius
astropy.units.Quantityor numpy:array_like, optional If specified, an annulus will be extracted instead of an aperture.
- centre
astropy.coordinates.SkyCoord, optional The centre of the aperture.
- init_id
int, 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_idwill start atmax(seg_map)+1.- **kwargs
dict, optional Any inputs accepted by
SkyCoord, ifcentreis None.
- radius
- Returns:
ndarrayThe 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:
- radius
astropy.units.Quantityor numpy:array_like, optional The outer radius of the aperture, by default 3 arcseconds.
- inner_radius
Quantityor numpy:array_like, optional If specified, an annulus will be extracted instead of an aperture.
- centre
astropy.coordinates.SkyCoord, optional The centre of the aperture.
- segments
int, optional The number of sectors into which each object will be divided, by default 4.
- angle
astropy.units.Quantityorfloat, optional The position angle offset, by default 0.
- init_id
int, 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_idwill start atmax(seg_map)+1.- **kwargs
dict, optional Any inputs accepted by
SkyCoord, ifcentreisNone.
- radius
- Returns:
ndarrayThe 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_path
stroros.PathLike The path pointing to the region file.
- format
strorNone, optional The file format specifier. If None, the format is automatically inferred from the file extension.
- reg_wcs
astropy.wcs.WCSorNone, optional The WCS to use to convert pixels to world coordinates. By default, the segmentation map WCS will be used.
- init_id
int, 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_idwill start atmax(seg_map)+1.
- reg_path
- Returns:
intThe ID corresponding to the new object in the segmentation map.
- Raises:
AttributeErrorIf the segmentation map has not been loaded.
ImportErrorIf the regions package is not available.
ExceptionIf the supplied regions file cannot be read.