API

This page details the methods and classes provided by the kmos_tools module.

Exposure Class

Class to hold individual KMOS exposures for post-processing

Takes *SCI_RECONSTRUCTED.fits filenames as input

Usage:

Create a instance of the exposure class for each exposure file:

from kmos_tools import exposure

fname = 'KMOS_SCI_RECONSTRUCTED.fits'
exp = exposure.Exposure(fname)
class kmos_tools.exposure.Exposure(reconstructed_fits_path, vb=False)[source]

A single KMOS exposure object

Takes SCI_RECONSTRUCTED filename as input, to be used for other operations.

filename

filepath to SCI_RECONSTRUCTED fits file

Type

str

hdulist

HDUlist of IFUs in the exposure

Type

astropy sequence of HDU objects or single HDU

hdr

FITS header

Type

astropy header

filter

KMOS filter used

Type

str

frame_time

date of observations

Type

str

Sky residual cleaning

Module to clean up sky subtraction residuals in KMOS data

Works on individual exposures which must then be combined via kmos_combine.

kmos_tools.sky_clean.make_sky_residual_spectra(exposure, use_named_targets=[''], clobber=True, plot=True)[source]

Calculate all the sky residual corrections for one DIT ~ a la Trevor Mendel

Generate median 1D sky spectra for each detector, only from certain cubes, and save them to use for subtraction. E.g. for KLASS we set use_named_targets=[‘S1’, ‘S3’] to only use S1 and S3 targets (high z ~ empty).

Parameters
  • exposure (object) – exposure object

  • use_named_targets (list) – list of string to match with target name, uses only IFUs containing those targets to make sky residual spectra, default uses all IFUs

  • clobber (bool) – Make a new sky spectrum if it already exists

  • plot (bool) – make and save a plot of the median sky spectra

Returns

Saves FITS file with 1D sky spectra for each detector

kmos_tools.sky_clean.subtract_sky_residual_spectra(exposure, clobber=True, plot=True)[source]

Subtract residual sky spectrum from each IFU using relevant detector sky spectrum

Rescales 1D sky spectrum \(s_\lambda\) in each spatial pixel by \(A_{x, y}\) for each IFU such that sky subtracted spectrum:

\[f_{x, y, \lambda}^\mathrm{skysub} = f_{x, y, \lambda} - A_{x, y}s_\lambda\]

Where \(A_{x, y}\) is obtained by minimising:

\[\left( \frac{f_{x, y, \lambda}^\mathrm{skysub}}{\sigma_\lambda} \right)^2\]
Parameters
  • exposure (object) – exposure object

  • clobber (bool) – Make a new sky spectrum if it already exists

  • plot (bool) – make and save a plot of the median sky spectra

Returns

Saves FITS file with 1D sky spectra for each detector with filename *_SKYSUB.fits

kmos_tools.sky_clean.sky_residual(params, sky, data, err)[source]

Residual for 1D sky-scaling

Used to find optimal rescaling of 1D spectrum, A, for each spatial pixel

\[\begin{split}R_i = \frac{(f_i - A s_i)}{\sigma_i} \\ \chi^2 = \sum_i R_i^2\end{split}\]
Parameters
  • params (params) – parameters

  • sky (ndarray) – 1D sky spectrum

  • data (ndarray) – 3D data cube

  • err (ndarray) – 1D error spectrum

Returns

residual

Finding star offsets

Module to find stars in an exposure, and fit their spatial positions to use in kmos_combine --user_shifts

Usage:

# List of frames to check (should be all the frames you want to combine)
frame_list = ['frame1.fits', 'frame2.fits']

fname_stars   = 'stars.txt'   # File to save star parameters to
fname_combine = 'combine.sof' # .sof file to create

kt.star_positions_batch(frame_list, psf_cut=0.8, starparams_filename=fname_stars, combinefiles_filename=fname_combine)

fname_usershifts = 'usershifts.txt' # file to save usershifts to

kt.make_user_shifts_file(starparams_filename=fname_stars, usershifts_filename=fname_usershifts)
kmos_tools.star_offsets.make_user_shifts_file(starparams_filename, usershifts_filename=None)[source]

Create file of user shifts to use with kmos_combine --method="user"

Note

To run kmos_combine you must use the frames in the order they are used here i.e. so the offset matches the right frame

Parameters
  • starparams_filename (str) – filepath of table with the star positions

  • usershifts_filename (str, optional) – filename to save usershift table to

kmos_tools.star_offsets.star_positions_batch(frame_list, psf_cut=0.8, edge_x=2.0, edge_y=2.0, star_ifu=None, starparams_filename=None, combinefiles_filename=None)[source]

Given a list of exposure file names, will find stars and measure PSFs.

Given a list of exposure file names, will find stars and measure PSFs. Then creates a list of frames with PSF FWHM below a given value, saves the list of star positions (to calculate user shifts) and creates a .sof file of the ‘good’ frames.

Parameters
  • frame_list (list) – List of file names of individual SCI_RECONSTRUCTED.fits files

  • psf_cut (float) – max PSF to use for combining (in arcsec) [default = 0.8 arcsec]

  • edge_x (float) – x_star > edge_x to include (i.e. not on the edge) [default = 2 pixels]

  • edge_y (float) – y_star > edge_y to include (i.e. not on the edge) [default = 2 pixels]

  • star_if (int) – IFU star is on

Yields

starparams_filename (str) – table with parameters of good stars combinefiles_filename (str): .sof file with list of good frames

kmos_tools.star_offsets.find_star_ifu(exposure)[source]

Find the IFUs in the SCI_RECONSTRUCTED cubes containing a star

Parameters

exposure (object) – exposure object

Returns

IFU containing star

Return type

exposure.star_ifu (int)

kmos_tools.star_offsets.star_fit_profile(exposure)[source]

Fit gaussian profiles to star to find PSFs

Using ESO command line pipeline tools (esorex) extract the star from the exposure, fit a Gaussian profile to it, and save the fitted star to a new fits file star_file_name.

Parameters

exposure (object) – exposure object

Returns

filepath of FITS file with star with PSF fitted to it invert (bool): was the star flux weird and inverted?

Return type

star_file_name (str)

kmos_tools.star_offsets.star_psf(exposure, clobber=True, vb=False)[source]

Get the PSF profiles of the star for calculating user shifts

Read the file star_file_name and return the main parameters

Parameters

exposure (object) – exposure object

Returns

PSF centroid x in pixels psf_center_y (float): PSF centroid y in pixels psf_fwhm (float): PSF FWHM in arcsec psf_ba (float): PSF BA # TODO I don’t remember what this is!! psf_pa (float): PSF position angle in degrees exposure.invert (bool): is the flux weird?

Return type

psf_center_x (float)

Pipeline fixes

Quick fixes for KMOS pipeline problems. Methods are:

kmos_tools.pipeline_fixes.rotation_fix()[source]

Rotates frames so that they are all orientated in the same direction for combining

kmos_tools.pipeline_fixes.calib_fix()

Fix flux calibration if it wasn’t done by the pipeline (i.e. no standard observed on on detector)

kmos_tools.pipeline_fixes.flux_fix()[source]

Fixes an old (now fixed??) pipeline problem where flux needs to be divided by DIT length in seconds

kmos_tools.pipeline_fixes.rotation_fix(exposure, updateheader=False, keepsize=False)[source]

Sometimes the instrument was rotated, this rotates all the frames to the same angle for combining

For some OBs the field is rotated so North is no longer up. This is when ocs.rot.offangle != 0

Need to rotate back to North = up so we can combine nicely

  1. Check if frame is rotated

  2. Use kmo_rotate to rotate the frame

  3. Update the headers

  4. Rename old file to _rotoffangle###

  5. Save file

Note

Best to run this on sky sub files (_noS2.fits)

Parameters
  • exposure (object) – exposure object

  • updateheader (bool) – Update info in the header for the rotated frame

  • keepsize (bool) – extrapolate to keep the IFU the same size (for combining a mixture of cubes)

kmos_tools.pipeline_fixes.flux_fix(exposure, clobber=True)[source]

Fix Reflex exposure time bug

Fix the bug in Reflex by dividing the calibrated flux from the standard stars by the DIT for science exposures

Note

This is now obsolete as fixed in kmos pipeline v. > 1.4

Parameters
  • exposure (object) –

  • clobber (bool) – Overwrite exposure file

I/O

I/O functions for KMOS data

kmos_tools.io.find_exposures(dir='*', prefix='*')[source]

Find all the SCI_RECONSTRUCTED fits files in a directory

Parameters
  • dir (str) – Directory to look for exposures in

  • prefix (str) – prefix for filenames

kmos_tools.io.insensitive_glob(pattern)[source]

Case insensitive find file names

Parameters

pattern (str) – pattern to search for

Returns

sorted list of filenames which match pattern

Return type

file_list (list)