easyspec.cleaning package

class easyspec.cleaning.cleaning.cleaning[source]

Bases: object

This class contains all the functions necessary to perform the image reduction/cleaning, including cosmic ray removal.

CR_and_gain_corrections(flattened_debiased_data, gain=None, gain_header_entry='GAIN', readnoise=None, readnoise_header_entry='RDNOISE', Type='all', sigclip=5)[source]

Function to remove cosmic rays from target and standard star raw data based on the LACosmic method originally developed and implemented for IRAF by Pieter G. van Dokkum. This function also renormalizes the data according to the CCD/CMOS gain. We always need to work in electrons for cosmic ray detection.

Parameters:
  • flattened_debiased_data (dict) – A dictionary containing the data that needs correction.

  • gain (float) – This is the CCD gain. We will use it to convert the image units from ADU to electrons. If gain = None, easyspec will look for this entry automatically in the file header.

  • gain_header_entry (string) – We use this only if gain=None. This must be the keyword containing the gain value in the fits file header.

  • readnoise (float) – This is the CCD read noise, used to generate the noise model of the image. If readnoise = None, easyspec will look for this entry automatically in the file header.

  • readnoise_header_entry (string) – We use this only if readnoise=None. This must be the keyword containing the read noise value in the fits file header.

  • Type (string) – Type of data files to apply the corrections. Options are: “all”, “lamp”, “standard_star”, and “target”.

  • sigclip (float) – Laplacian-to-noise limit for cosmic ray detection. Lower values will flag more pixels as cosmic rays.

Returns:

CR_corrected_data – Dictionary containing the data corrected for gain and cosmic rays.

Return type:

dict

data_paths(bias=None, flats=None, lamp=None, standard_star=None, targets=None, darks=None)[source]

This function collects the paths for all data files (bias, flats, lamp, standard star, and targets), as long as they have the extension “.fit*”.

IMPORTANT: please note that different kinds of data files must be in different directories. E.g., put all bias files in a directory called “bias”, all flat files in a directory called “flats” and so on.

Parameters:
  • bias (strings) – Strings containing the path to the directory containing the data files.

  • flats (strings) – Strings containing the path to the directory containing the data files.

  • lamp (strings) – Strings containing the path to the directory containing the data files.

  • standard_star (strings) – Strings containing the path to the directory containing the data files.

  • darks (strings) – Strings containing the path to the directory containing the data files.

  • targets (string or list of strings) – You can pass a single directory path as a string or a list of paths. E.g. targets = “./spectra_target1” or targets = [“./spectra_target1”,”./spectra_target2”].

Returns:

all_raw_data – A dictionary containing the paths to all data files (dictionary keys) and the raw data for all images (dictionary values).

Return type:

dict

debias(trimmed_data, masterbias, Type='all', pad_with_zeros=True)[source]

Function to subtract the masterbias from the rest of the data.

Parameters:
  • trimmed_data (dict) – Dictionary containing trimmed data. It can even contain the raw bias files. At the end we select only the entry given in the variable Type.

  • masterbias (array) – Array with master bias raw data.

  • Type (string) – Type of data files to subtract the master bias. Options are: “all”, “flat”, “lamp”, “standard_star”, “target”, and “dark”.

  • pad_with_zeros (bool) – If True, easyspec will look for negative-value pixels after the bias subtraction and set them to zero.

Returns:

debias_data_out – Dictionary containing the debiased data.

Return type:

dict

flatten(debiased_data, norm_master_flat, Type='all')[source]

Function to apply the master flat on the rest of the data.

Parameters:
  • debiased_data (dict) – Dictionary containing debiased data. It can even contain the raw bias and dark files, but they will be ignored in the process.

  • norm_master_flat (array) – Array with normalized master flat raw data.

  • Type (string) – Type of data files to apply the normalized master flat. Options are: “all”, “lamp”, “standard_star”, and “target”.

Returns:

flattened_debiased_data_out – Dictionary containing the flattened data.

Return type:

dict

look_in_header(file_name, I_am_looking_for)[source]

This function looks for a specific entry over all header entries in a fits file.

Parameters:
  • file_name (string) – The path to the fits data file.

  • I_am_looking_for (string) – Which entry are you looking for? E.g. “EXPTIME” or “RDNOISE”.

Returns:

value – The value corresponding to the key you are looking for.

Return type:

don’t have a predefined type

master(Type, trimmed_data, method='median', header_hdu_entry=0, exposure_header_entry=None, airmass_header_entry=None, plot=True)[source]

Function to stack the data files into a master file.

Parameters:
  • Type (string) – Type of master to stack. Options are: “bias”, “flat”, “lamp”, “standard_star”, “target”, and “dark”.

  • trimmed_data (dict) – Dictionary containing trimmed data. At the end we select only the entries defined with the variable ‘Type’.

  • method (string) – Stacking method. Options are “median”, “mean”, or “mode”.

  • header_hdu_entry (int) – HDU extension where we can find the header. Please check your data before choosing this value.

  • exposure_header_entry (string) – If you want to save the mean/median exposure time in the master fits file, type the header keyword for the exposure here.

  • airmass_header_entry (string) – If you want to save the mean/median airmass in the master fits file, type the header keyword for the airmass here.

  • plot (bool) – Keep it as True if you want to plot the master bias/dark/flat/etc.

Returns:

  • master (numpy array) – The master bias raw data.

  • master_TYPE.fits (data file) – This function automatically saves a file named master_TYPE.fits in the working directory.

norm_master_flat(masterflat, method='polynomial', degree=5, smooth_window=101, header_hdu_entry=0, plots=True)[source]

Function to normalize the master flat.

Parameters:
  • masterflat (array) – Array with master flat raw data.

  • method (string) – Method to be adopted to fit the master flat profile (see plots for clarification). Options are “polynomial” or “median_filter”.

  • degree (int) – Polynomial degree to be fitted to the median masterflat x-profile. This parameter is useful only if method = “polynomial”.

  • smooth_window (integer) – Must be an odd number. This is the number of neighbouring pixels used to extract the flat field profile with a median filter.

  • header_hdu_entry (int) – HDU extension where we can find the header. Please check your data before choosing this value.

  • plots (bool) – If True, easyspec will plot the master flat profile, fit residuals and normalized master flat.

Returns:

  • normalized_master_flat (array) – Normalized master flat raw data.

  • norm_master_flat.fits (data file) – This function automatically saves a file named “norm_master_flat.fits” in the working directory.

pad(image_data, value, x1, x2, y1, y2, plot=True, save_fits=False, get_header_from=None, fits_name='padded_image.fits', header_hdu_entry=0)[source]

We use this function to pad a specific section of an image with a given value. It is particularly useful when handling the master files.

Parameters:
  • image_data (array) – Array containing the raw data for one image.

  • value (float) – The value that is going to be substituted in the image section defided by the limits x1,x2,y1,y2.

  • x1 (integers) – Values giving the image section limits for padding.

  • x2 (integers) – Values giving the image section limits for padding.

  • y1 (integers) – Values giving the image section limits for padding.

  • y2 (integers) – Values giving the image section limits for padding.

  • plot (bool) – If True, easyspec will print the padded image.

  • save_fits (bool) – Set it to True if you want to save a fits file for the padded image. It only works if get_header_from is not None.

  • get_header_from (string) – This is the path to the original fits file you want to import the header. E.g.: “./master_flat.fits”

  • fits_name (string) – Name of the fits file to be saved.

  • header_hdu_entry (int) – HDU extension where we can find the header. Please check your data before choosing this value.

Returns:

image_data – An array with the new padded image.

Return type:

array

plot(image_data, figure_name, save=True, format='png')[source]

This function plots and saves a single image.

Parameters:
  • image_data (array) – Array containing the raw data for one image.

  • figure_name (string) – Name of the figure. It can also be a path ending with the figure name, e.g. ./output/FIGURE_NAME, as long as this directory exists.

  • save (bool) – If True, the image will be saved.

  • format (string) – You can use png, jpeg, pdf and all other formats accepted by matplotlib.

Returns:

figure_name.png – An image saved in the current directory.

Return type:

file

plot_images(datacube, image_type='all', Ncols=2, specific_image=None, vmin=None, vmax=None, vertical_scale=None)[source]

Function to plot a matrix with all the images given in datacube

Parameters:
  • datacube (dict) – A dictionary containing the paths to all data files (dictionary keys) and the data for all images (dictionary values).

  • image_type (string) – String with the type of file to plot. Options: “all”, “bias”, “flat”, “lamp”, “standard_star”, “target”, and “dark”.

  • Ncols (int) – Number of culumns for the grid.

  • specific_image (int) – In case you want to see a single image from e.g. the bias or the target sample, use specific_image = number corresponding to your image. It goes from zero up to the total number (minus one) of files of a specific kind you have. For instance, if you want to display only the first bias file, use image_type = “bias”, Ncols = 1, and specific_image = 0.

  • vertical_scale (float) – This variable allows you to control the vertical scale between the plots.

Return type:

A plot with 3 columns and several lines containing all the images passed in the variable datacube.

rotate(raw_image_data, N_rotations=1)[source]

Function to rotate the raw data files by N_rotations*90°. In easyspec, the dispersion axis must be in the horizontal.

Parameters:
  • raw_image_data (dict) – A dictionary containing the paths to all data files (dictionary keys) and the raw data for all images (dictionary values). E.g.: the output from the data_paths() function.

  • N_rotations (int) – Number of 90° rotations to be performed.

Returns:

raw_images_rotated – A dictionary containing the paths to all data files (dictionary keys) and the rotated raw data for all images (dictionary values).

Return type:

dict

save_fits_files(datacube, output_directory, header_hdu_entry=0, Type='all')[source]

This function saves the raw data contained in the dictionary ‘datacube’ as fits files in the directory ‘output_directory’.

Parameters:
  • datacube (dict) – A dictionary containing the paths to the data files (dictionary keys) and the data for their respective images (dictionary values).

  • output_directory (string) – String with the path to the output directory.

  • header_hdu_entry (int) – HDU extension where we can find the header. Please check your data before choosing this value.

  • Type (string) – Type of data files to be saved. Options are: “all”, “bias”, “flat”, “lamp”, “standard_star”, “target”, and “dark”.

Return type:

Saves one or more fits files in the output directory.

sub_dark(debiased_data, masterdark, Type='all', pad_with_zeros=True)[source]

Function to subtract the masterdark from the rest of the data.

Parameters:
  • debiased_data (dict) – Dictionary containing debiased data. This dictionary can even contain the raw dark files, but should not contain the raw bias files. At the end we select only the entry given in the variable Type.

  • masterdark (array) – Array with master dark raw data.

  • Type (string) – Type of data files to subtract the master bias. Options are: “all”, “flat”, “lamp”, “standard_star”, and “target”.

  • pad_with_zeros (bool) – If True, easyspec will look for negative-value pixels after the dark subtraction and set them to zero.

Returns:

subdark_data_out – Dictionary containing the data corrected for dark.

Return type:

dict

trim(raw_image_data, x1, x2, y1, y2)[source]

We use this function to trim the data

Parameters:
  • raw_image_data (dict) – Dictionary containing the path (keys) and the raw data (values) for one or several images.

  • x1 (integers) – Values giving the cutting limits for the trim function.

  • x2 (integers) – Values giving the cutting limits for the trim function.

  • y1 (integers) – Values giving the cutting limits for the trim function.

  • y2 (integers) – Values giving the cutting limits for the trim function.

Returns:

raw_image_data_trimmed – A dictionary with all image data files trimmed

Return type:

dict

vertical_align(CR_corrected_data, Type='all')[source]

This function is used to align target and standard star data taht are shifted in the vertical axis. Although this function does not align lamp images, it accepts the raw lamp data in the dictionary CR_corrected_data. In the last step of this function, all data will be trimmed according to the alignment cuts, including the lamp data (if provided).

Parameters:
  • CR_corrected_data (dict) – A dictionary containing the paths to the data files (dictionary keys) and the data for their respective images (dictionary values).

  • Type (string) – Type of data files to align. Options are: “all”, “standard_star”, and “target”.

Returns:

aligned_data – Dictionary containing the aligned and trimmed data passed in the variable CR_corrected_data.

Return type:

dict