Python API¶
- nirc2_reduce.observation.crop_center(frame, subc)¶
Crop an array around its center.
- Parameters:
frame (np.array, required.)
subc (int, required.)
- Return type:
np.array
- class nirc2_reduce.observation.Observation(fnames, instrument)¶
Bases:
objectGeneric class containing dithers and nods.
To define a custom dither pattern, inherit from this class See Bxy3() and Nod() objects for usage
- apply_flat(fname)¶
Apply the flatfield correction.
- Parameters:
fname (str, required.) – filename of flat
- apply_badpx_map(fname, kernel_size=7)¶
Replace all bad pixels in input map with the median of the pixels around it.
- Parameters:
fname (str, required.) – filename of bad pixel fits file to apply
kernel_size (int, optional.) – how large a region around which to take median
- dewarp()¶
Apply nirc2 distortion correction.
Uses updated maps from Ghez, Lu galactic center group (Service et al. 2016) doi:10.1088/1538-3873/128/967/095004 Spline interpolation used to interpolate original image. The spline is then evaluated at new pixel locations computed from the map. Custom distortion solutions should be placed in the nirc2_reduce/nirc2_reduce/data/ folder
- Parameters:
warpx_file (str, optional.) – distortion map to apply in x
warpy_file (str, optional.) – distortion map to apply in y
Notes
From Service+16 “These are lookup tables generated by evaluating the fits from the previous section at the center of every pixel on the NIRC2 detector and are the values that should be added to raw NIRC2 positions to shift them to a distortion-free frame” So the position offsets in the fits files should be added. That this works right was checked by looking at difference maps in a subdirectory of tests/ and comparing with the expected vectors in Service+16
- rotate(custom_angle=0.0, beta=0.252)¶
Rotate frame to North up plus an additional custom angle.
if header contains ‘ROTPOSN’ and ‘INSTANGL’ keywords (i.e., nirc2-like), then rotation is custom_angle + (ROTPOSN-INSTANGL) - beta, counterclockwise. otherwise, rotation is simply custom_angle - beta, counterclockwise. see https://github.com/jluastro/nirc2_distortion/wiki
- Parameters:
custom_angle (float, optional. default 0.0. units degrees.) – user-defined additional angle to rotate frames counterclockwise is positive to agree with scipy.ndimage.rotate
beta (float, optional. default 0.252 (nirc2 post 2015). units degrees.) – rotation required to get North up according to astrometry solution clockwise is positive to agree with definition of beta given by the nirc2 distortion wiki
Notes
This should be generalized to include scopes other than nirc2 more easily however, for typical observations ROTPOSN - INSTANGL is zero anyway
- remove_cosmic_rays(**kwargs)¶
Detect cosmic rays using the astroscrappy package.
- Parameters:
**kwargs (dict, optional) – keyword arguments to astroscrappy.detect_cosmics()
- per_second()¶
Change units to counts/second by dividing by ITIME x COADDS.
- Parameters:
itime_kw (str, optional.) – keyword to scrub fits header for integration time
coadd_kw (str, optional.) – keyword to scrub fits header for coadds
- apply_photometry_frames(flux_per)¶
Multiply each frame by flux density / (cts/s).
- Parameters:
flux_per (float, required. units [flux density / (cts/s)]) – conversion factor
- apply_photometry_final(flux_per)¶
Multiply final combined product by flux density / (cts/s).
- Parameters:
flux_per (float, required. units [flux density / (cts/s)]) – conversion factor
- stack()¶
Cross-correlate the images applying sub-pixel shift.
Shift found using DFT upsampling method from image_registration. Stack them on top of each other to increase SNR.
- crop_final(bw)¶
Apply crop to the final image.
- Parameters:
bw (int, required.) – border width
- com_crop_final(wx, wy=None, cutoff=None)¶
Apply a crop to the final image.
Attempts to find bright source in frame using a center-of-mass approach.
- Parameters:
wx (int, required.) – distance from com to keep in +x and -x direction i.e., half-width of output image
wy (int, optional. Default wx) – distance from com to keep in +y and -y direction
cutoff (float, optional. Default None) – the cutoff below which the data are considered noise if None, code attempts to figure out rms noise based on mean and stddev in the corners, and then takes 5x that value as the cutoff
- plot_frames(png_file=None, figsz=3)¶
Plot the individual frames any step in the process.
- Parameters:
png_file (str or None, optional.) – filename to which image is saved if None, image not saved
- plot_final(show=True, png_file=None)¶
Plot the final product.
- Parameters:
show (bool, optional.) – show plot?
png_file (str or None, optional.) – filename to which image is saved if None, image not saved
- write_frames(outfiles)¶
Write each individual frame to .fits at any step in the process.
- Parameters:
outfiles (list, required.) – filenames to write
- write_final(outfile)¶
Write the final stacked frame to .fits.
- Parameters:
outfile (str, required.) – filename to write
png_file (str or None, optional.) – filename to which image is saved if None, image not saved
- class nirc2_reduce.observation.Nod(data_fname, sky_fname, instrument)¶
Bases:
ObservationSimple on-off nod dither pattern.
- apply_sky()¶
Simply subtract the sky frame from the data.
- uranus_crop(bw)¶
Apply a custom crop for Uranus data.
We use the right side of the NIRC2 detector to avoid the bad pixels in the lower left corner so just cutting off the left bit here
- Parameters:
bw (int, required.) – width of the crop
- class nirc2_reduce.observation.Bxy3(fnames, instrument)¶
Bases:
ObservationThe famous bxy3 dither at Keck.
bxy3 avoids the noisier lower left quadrant of the detector input fnames MUST be in the conventional order where target is in positions [top left, bottom right, top right]
- make_sky(outfile)¶
Make a sky frame via simple median-average of the frames.
Writes fits file containing the sky frame with header info same as zeroth input bxy3 file
- Parameters:
outfile (str, required.) – fname to write.
- apply_sky(fname)¶
Identify patches of “normal” sky in each of the three bxy3 frames.
Comes up with a median average value of background for each frame. Then you normalize the “full” sky frame we got from median of bxy3 to the value of the sky brightness in the single frame. Also normalizes everything to be around 1, so can directly multiply by data This ONLY works if bxy3 frames are input in the correct (usual) order, top left | bottom right | top right
- Parameters:
fname (str, required.) – filename pointing to sky frame
- trim()¶
Clip each image in bxy3 to its own quadrant.
Relies on frames input being in correct order. Should be applied just before stacking
- class nirc2_reduce.observation.DitherN(fnames, instrument)¶
Bases:
ObservationGeneric N-position dither.
fnames can have arbitrary length; will make sky by simple median average
- make_sky(outfile)¶
Make a sky frame via simple median-average of the frames.
Identical to Bxy3.make_sky()
- Parameters:
outfile (str, required.) – fname to write.
- apply_sky(fname)¶
Simply subtract the sky frame from the data.
- Parameters:
fname (str, required.) – filename representing sky frame fits
- class nirc2_reduce.multi_reduce.MultiReduce(rawdir, instrument)¶
Bases:
objectSuperclass to multiBxy3 and multiNod.
- process_flats(flatdir, **badpx_kwargs)¶
Process the flatfield files.
Check for new dome flats in rawdir, make new flats for each filter present and put into flatdir
- Parameters:
flatdir (str, required.) – directory to put into and/or retrieve flats if retrieving flats without making them, ensure filenames match defaults; {date}_badpx_map_{filt}.fits for bad pixel maps in given filter {date}_flat_master_{filt}.fits for flats in given filter
**badpx_kwargs (dict, optional.) – keyword arguments to flats.Flats.make_badpx_map()
- class nirc2_reduce.multi_reduce.MultiBxy3(rawdir, instrument)¶
Bases:
MultiReduceRun multiple filters and objects in a single date of observing.
Example
obs = MultiBxy3(rawdir) obs.process_flats(flatdir) # if new flats were taken obs.run(outdir, flatdir)
- run(outdir, flatdir, filts_want=None, show=False)¶
Run the multiple reductions.
- Parameters:
outdir (str, required.) – directory to save outputs to
flatdir (str, required.) – directory in which to look for flats
filts_want (list-like or None, optional. default None.) – list of filter names to run if None, runs all filters found in rawdir
show (bool, optional. default False) – show final quicklook images while running?
Writes
------
- class nirc2_reduce.flats.Flats(fnames_off, fnames_on)¶
Bases:
objectClass for dome flats.
- write(outfile)¶
Write the flat to fits.
- Parameters:
outfile (str, required. output fits filename)
- plot()¶
Make a simple plot of the flat.
- make_badpx_map(outfile, tol=0.07, blocksize=6)¶
Find pixels whose values are very far from the average of their neighbors.
Bad pixel is defined as abs(pixel value / median of nearby pixels - 1) > tol
- Parameters:
outfile (str, required.) – fits filename to write to
tol (float, optional. default 0.07) – fractional tolerance.
blocksize (int, optional. default 6) – number of pixels over which to average in each direction
- nirc2_reduce.sort_rawfiles.dfits_fitsort(input_wildcard, fits_kws=None)¶
Python implementation of the dfits | fitsort bash script workflow.
Searches headers of all fits in input_dir to find the requested keywords makes a table of filename, kw0, kw1, …
- Parameters:
input_wildcard (str, required. path to filenames) – wildcards are allowed, as long as glob.glob() can read it. example ‘raw/2023jul14/*.fits’
fits_kws (list, optional. default ['OBJECT', 'DATE-OBS', 'FILTER']) – which header keywords to include in the output table
- Returns:
filenames and values corresponding to fits_kws
- Return type:
astropy.table.Table
- nirc2_reduce.sort_rawfiles.split_by_kw(tab, kw)¶
Split a single table into multiple tables according to unique values of kwd.
- Parameters:
tab (astropy Table, required.) – table of fits header params output by dfits_fitsort()
kw (str, required.) – table header column to split fnames by
- Returns:
list – unique values of tab[kw]
list – Astropy tables corresponding to those values
- nirc2_reduce.sort_rawfiles.get_flats(tab, instrument, ignore_objects=None)¶
Scrub table from dfits_fitsort to find domeflaton, domeflatoff filenames.
- Parameters:
tab (astropy Table, required.) – table of fits header params output by dfits_fitsort() must have FILENAME kw
instrument (str, required.) – name of instrument used, e.g. nirc2. will look for file data/{instrument}.yaml in order to scrub header keywords
ignore_objects (list, optional, default empty list.) – special values in header[object] to ignore.
- Returns:
list – filenames for dome flat off
list – filenames for dome flat on
- class nirc2_reduce.image.Image(fname)¶
Bases:
objectThin wrapper to astropy.io.fits.
To do¶
make agnostic to specific .fits header keywords expand plot, write functionality
- plot()¶
Make a simple plot of the data.
- write(fname)¶
Write the image to fits.
- Parameters:
fname (str) – Filename to write to.
- nirc2_reduce.prettycolors.make_colormap(seq)¶
Make a custom colormap.
- Parameters:
seq – a sequence of floats and RGB-tuples. The floats should be increasing and in the interval (0,1).
- Return type:
LinearSegmentedColormap
- nirc2_reduce.prettycolors.get_colormap(objname)¶
Create custom colormaps for pretty pictures for Twilight Zone website.
- Parameters:
objname (str, required. name of target. currently supported:) – io, neptune, titan, uranus