Tools

General tools that are used in the infrastructure of spike.

spike.tools.checkpixloc(coords, img, inst, camera=None)

Get object location on detector.

Parameters:
  • coords (astropy skycoord object) – Coordinates of object of interest or list of skycoord objects.

  • img (str) – Path to image.

  • inst (str) – Instrument of interest. HST: ‘ACS’, ‘WFC3’, ‘WFPC’, WFPC2’, ‘NICMOS’ JWST: ‘MIRI’, ‘NIRCAM’, ‘NIRISS’ Roman: ‘WFI’, ‘CGI’

  • camera (str) – Camera associated with instrument. HST/ACS: ‘WFC’, ‘HRC’ HST/WFC3: ‘UVIS’, ‘IR’ JWST/NIRISS: ‘Imaging’, ‘AMI’ #AMI has different multi-extension mode

Returns:

Pixel coordinates, chip number (HST) or detector name (JWST/Roman) if relevant, and filter name.

Only returned if object coordinates fall onto detector - returns NaNs if not.

Return type:

[X, Y, chip, filter] (list)

spike.tools.cutout(img, coords, ext=1, fov_pixel=120, save=True, clobber=False)

Get cutout of image around some coordinates.

Parameters:
  • img (str) – Path to image to crop.

  • coords (astropy skycoords object) – Coordinates of object of interest or list of skycoord objects. Easiest is to feed in the output of spike.tools.objloc.

  • ext (int) – Integer index of extension to crop.

  • fov_pixel (int) – “Diameter” of square cutout region in pixels.

  • save (bool) – If True, will save a FITS file containing cropped region. Note that the output FITS file will not have any distortion corrections stored as it is already decontextualized from the original image.

  • clobber (bool) – If True (and save = True), will overwrite existing FITS files with the same name. (Default state – clobber = False – is recommended.)

Returns:

Array containing cutout region of the image.

If save = True, will save a cropped version of the .fits file (given image name + _crop suffix), including modified WCS.

Return type:

cutoutim (arr)

spike.tools.mask_fits(img, ext=1, maskdq=True, dqthresh=0, maskerr=False, errthresh=20, usermask=None, fillval=0, clobber=False)

Generate a FITS file that fills in masked pixels with a specified value. Useful for feeding to e.g., SExtractor. Preserves truncated FITS extension structure, for the specified extension on which the the mask was applied plus ERR and DQ extensions. (Assumes that extensions are named SCI, ERR, DQ.)

Parameters:
  • img (str) – Path to image to which mask is applied.

  • ext (int) – Integer index for version of extension to which mask should be applied. This will be used to index dat[(‘SCI’, ext)] etc.

  • maskdq (bool) – If True, masks values above dqthresh.

  • dqthresh (float) – Maximum acceptable value in DQ image. Pixels above dqthresh will be masked.

  • maskerr (bool) – If True, masks values above errthresh.

  • errthresh (float) – Maximum acceptable value in ERR image. Pixels above errthresh will be masked.

  • usermask (arr) – If specified, used as mask on data array (good pixels should have value of 0). Must be the same dimensions as the data array. (Can be used in addition to DQ and ERR masking.)

  • fillval (float) – Value with which to fill the masked pixels.

  • clobber (bool) – If True , will overwrite existing FITS files with the same name. (Default state – clobber = False – is recommended.)

Returns:

Generates a new FITS file with a _mask suffix with masked pixels filled in by fillval.

spike.tools.objloc(obj, prompt=True)

Get object location.

Parameters:
  • obj (str) – Name or coordinates for object of interest. If coordinates, should be in HH:MM:SS DD:MM:SS or degree formats. Names must be resolvable in SIMBAD.

  • prompt (bool) – If prompt, will ask for coordinates in the case that obj name is not resolvable.

Returns:

coords (astropy coordinates object)

spike.tools.psfexim(psfpath, pixloc, regrid=True, save=False, clobber=False)

Generate image from PSFEx .psf file.

Parameters:
  • psfpath (str) – Path to the relevant .psf file from the working directory.

  • pixloc (tuple) – Pixel location of object of interest in (x, y).

  • regrid (bool) – If True, will (interpolate and) regrid model PSF to image pixel scale.

  • save (str) – If ‘fits’ or ‘arr’, will save in the specified format with name from psfpath. The option to save as an array results in a .npy file.

  • clobber (bool) – If True (and save = True), will overwrite existing files with the same name. (Default state – clobber = False – is recommended.)

Returns:

2D image of PSFEx model

spike.tools.pypsfex(cat_path, pos, config=None, userargs=None, makepsf=True, savepsf=False, keepconfig=False, regrid=True, clobber=False)

Wrapper to easily call PSFEx from python.

Parameters:
  • cat_path (str) – Path to the SExtractor catalog from the working directory.

  • pos (tuple) – Pixel (and spectral) location of object of interest in (x, y, chip, filter) - as from spike.tools.checkpixloc. If not specified, generates generic model assuming central pixel.

  • config (str) – If specifying custom config, path to config file. If none, uses default.psfex.

  • userargs (str) – Any additional command line arguments to feed to PSFEx. The preferred way to include user arguments is via specification in the config file as command line arguments simply override the corresponding configuration setting.

  • makepsf (bool) – If True, returns 2D PSF model.

  • savepsf (str) – If ‘fits’, ‘arr’, or ‘txt’, will save 2D model PSF in that file format with the same name as the catalog.

  • keepconfig (str) – If True, retain parameter files and convolutional kernels moved to working dir.

  • regrid (bool) – If True, will (interpolate and) regrid model PSF to image pixel scale.

  • clobber (bool) – If True, will overwrite existing files with the same name on save. (Default state – clobber = False – is recommended.)

Returns:

Generates a .psf file that stores linear bases of PSF. If makepsf = True, also returns 2D array containing PSF model.

spike.tools.pysextractor(img_path, config=None, psf=True, userargs=None, keepconfig=False)

Wrapper to easily call SExtractor from python. Using this in lieu of sep for easier compatibility with PSFEx.

Parameters:
  • img_path (str) – Path to the image from the working directory.

  • config (str) – If specifying custom config, path to config file. If none, uses default.sex.

  • psf (bool) – If True and no configuration file specified, uses config and param files that work with PSFEx.

  • userargs (str) – Any additional command line arguments to feed to SExtractor. The preferred way to include user arguments is via specification in the config file as command line arguments simply override the corresponding configuration setting.

  • keepconfig (str) – If True, retain parameter files and convolutional kernels moved to working dir.

Returns:

Generates a .cat file with the same name as img_path

spike.tools.regridarr(im, sample)

Regrid PSF model to input pixel scale.

Parameters:
  • im (arr) – PSF image array.

  • sample (float) – Npix,im/Npix,orig. If sample > 1, oversampled; if sample < 1, undersampled.

Returns:

Interpolated and regridded PSF model.

spike.tools.rewrite_fits(psfarr, coords, img, imcam, pos, method=None, clobber=False)

Write relevant image headers to the model PSFs and modify the coordinates and WCS. Creates a full _topsf_*.fits file with only one SCI extension for use with drizzle/resample.

Parameters:
  • psfarr (arr) – The 2D PSF model.

  • coords (astropy skycoord object) – Coordinates of object of interest or list of skycoord objects.

  • img (str) – Path to image for which PSF is generated.

  • imcam (str) – ‘ACS/WFC’ is the only recommended instrument/camera combination for this PSF generation method.

  • pos (list) – Location of object of interest (spatial and spectral).[X, Y, chip, filter]

  • method (list) – Method used to generate PSF.

  • clobber (bool) – If True, will overwrite existing FITS files with the same name. (Default state – clobber = False – is recommended.)

Returns:

Generates a new FITS file with a _topsf suffix, which stores the 2D PSF model in the ‘SCI’ extension and inherits the header information from the original image.

spike.tools.to_asdf(fitspath, save=True, clobber=False)

Convert .fits file to .asdf by simply wrapping data and header extensions.

Parameters:
  • fitspath (str) – Path to .fits file to convert.

  • save (bool) – If True, saves the .asdf file.

  • clobber (bool) – If True, will overwrite existing files with the same name on save. (Default state – clobber = False – is recommended.)

Returns:

ASDF file object