API

Only a small subset of classes and functions are currently listed on these pages; this will improve as I document them more thoroughly.

Classes

class craftutils.wrap.psfex.PSFExModel(craftutils.wrap.psfex.Fittable2DModel)

Warning: does not work. I tried!

class craftutils.wrap.dragons.Table

A class to represent tables of heterogeneous data.

class craftutils.wrap.psfex.Fittable2DModel(astropy.modeling.core.FittableModel)

Base class for two-dimensional fittable models.

class craftutils.wrap.psfex.Parameter

Wraps individual parameters.

Functions

craftutils.wrap.dragons.build_data_select_str(...)
craftutils.wrap.dragons.data_select(redux_dir: str, directory, ...)
craftutils.wrap.dragons.showd(input_filenames: str | list, ...)
craftutils.wrap.dragons.caldb_init(redux_dir: str)
craftutils.wrap.dragons.caldb_add()
craftutils.wrap.dragons.reduce(data_list_path: str, redux_dir: str)
craftutils.wrap.dragons.disco(redux_dir: str, ...)
craftutils.wrap.galfit.galfit(config: str, output_dir: str)
craftutils.wrap.galfit.feedme_sky_model(...)

Generate input file lines in the GALFIT feedme format, for sky model. Parameters provided in valid astropy units will be converted to the units used by GALFIT. :param background_center: Background level at centre of fitting region.

Corresponds to item 1) in the GALFIT feedme definition.

Parameters
fit_background_center

If True, GALFIT will fit for background_center; if False, it will remain fixed.

gradient_x

dsky/dx in ADU / pixel Corresponds to item 2) in the GALFIT feedme definition.

fit_gradient_x

If True, GALFIT will fit for gradient_x; if False, it will remain fixed.

gradient_y

dsky/dy in ADU / pixel Corresponds to item 3) in the GALFIT feedme definition.

fit_gradient_y

If True, GALFIT will fit for gradient_y; if False, it will remain fixed.

skip_in_output

Skip in output image block? Corresponds to item Z) in the GALFIT feedme definition.

pix_scale

pixel scale for conversion from angular sizes (eg arcsec) to pixels.

Returns

List of strings corresponding to lines in a .feedme input file, suitable for use with open().writelines().

craftutils.wrap.galfit.feedme_sersic_model(...)

Generate input file lines in the GALFIT feedme format, for sersic model. Parameters provided in valid astropy units will be converted to the units used by GALFIT. :param x: x-position of model centre, in pixels.

Corresponds to first component of item 1) in the GALFIT feedme definition.

Parameters
y

y-position of model centre, in pixels. Corresponds to second component of item 1) in the GALFIT feedme definition.

int_mag

integrated magnitude of model. Corresponds to item 3) in the GALFIT feedme definition.

fit_x

If True, GALFIT will fit for x; if False, it will remain fixed.

fit_y

If True, GALFIT will fit for y; if False, it will remain fixed.

fit_int_mag

If True, GALFIT will fit for int_mag; if False, it will remain fixed.

r_e

Effective radius of model, in pixels. Corresponds to item 4) in the GALFIT feedme definition.

fit_r_e

If True, GALFIT will fit for r_e; if False, it will remain fixed.

n

Sersic index n of model. Corresponds to item 5) in the GALFIT feedme definition.

fit_n

If True, GALFIT will fit for n; if False, it will remain fixed.

axis_ratio

axis ratio b/a of model. Corresponds to item 9) in the GALFIT feedme definition.

fit_axis_ratio

If True, GALFIT will fit for axis_ratio; if False, it will remain fixed.

position_angle

position angle (PA) of model, in degrees, with up=0 and left=90 Corresponds to item 10) in the GALFIT feedme definition.

fit_position_angle

If True, GALFIT will fit for position_angle; if False, it will remain fixed.

skip_in_output

Skip in output image block? Corresponds to item Z) in the GALFIT feedme definition.

rotation_params

Coordinate rotation parameters (eg for fitting spiral arms)

pix_scale

pixel scale for conversion from angular sizes (eg arcsec) to pixels.

Returns

List of strings corresponding to lines in a .feedme input file, suitable for use with open().writelines().

craftutils.wrap.galfit.feedme_rot(rot_type: str = 'log', ...)

Generate input file lines in the GALFIT feedme format, for coordinate rotation (eg for fitting spiral arms). Parameters provided in valid astropy units will be converted to the units used by GALFIT. :param rot_type: “log” or “power”, determining which rotation function to use.

Corresponds to item R0) in the GALFIT feedme definition.

Parameters
r_in

Spiral inner radius, in pixels. If this goes extremely negative in fitting, fix to 0. Corresponds to item R1) in the GALFIT feedme definition.

fit_r_in

If True, GALFIT will fit for r_in; if False, it will remain fixed.

r_out

Spiral outer radius, in pixels. Corresponds to item R2) in the GALFIT feedme definition.

fit_r_out

If True, GALFIT will fit for r_out; if False, it will remain fixed.

theta_out

Cumulative rotation to outer radius, in degrees. Corresponds to item R3) in the GALFIT feedme definition.

fit_theta_out

If True, GALFIT will fit for theta_out; if False, it will remain fixed.

r_ws

winding scale radius, in pixels. Corresponds to item R4) in the GALFIT feedme definition.

fit_r_ws

If True, GALFIT will fit for r_ws; if False, it will remain fixed.

theta_inc

inclination to line-of-sight, in degrees. Corresponds to item R9) in the GALFIT feedme definition.

fit_theta_inc

If True, GALFIT will fit for theta_inc; if False, it will remain fixed.

theta_pa

Position angle of the axis about which the galaxy is rotated for inclination. Corresponds to item R10) in the GALFIT feedme definition.

fit_theta_pa

If True, GALFIT will fit for theta_pa; if False, it will remain fixed.

pix_scale

pixel scale for conversion from angular sizes (eg arcsec) to pixels.

Returns

List of strings corresponding to lines in a .feedme input file, suitable for use with open().writelines().

craftutils.wrap.galfit.feedme_model(object_type: str, ...)

Generate the input file lines for an arbitrary model for use with GALFIT. :param object_type: string indicating the type of model, to be parsed by GALFIT. :param skip_in_output: Skip in output image block?

Corresponds to item Z) in the GALFIT feedme definition.

Parameters
position

Tuple containing (x, y).

fit_position

Tuple containing (fit_x, fit_y), each specifying whether to fit x and y position respectively.

args

(value, fit), with value being the initial guess for a parameter and fit being a boolean indicating whether this parameter should be fit for (True) or held fixed (False). If value is provided without fit, fit defaults to True.

Returns

List of strings corresponding to lines in a .feedme input file, suitable for use with open().writelines().

craftutils.wrap.galfit.galfit_feedme(feedme_path: str, ...)

Generates and writes full GALFIT input .feedme parameter file. Any unset values will be left as the GALFIT defaults (see param/galfit/galfit.feedme) :param feedme_path: Path to write parameter file to. :param input_file: Path to input image FITS file.

Corresponds to item A) in the GALFIT feedme definition.

Parameters
output_file

Path to write output image block FITS file. Corresponds to item B) in the GALFIT feedme definition.

zeropoint

Path to write output image block FITS file. Corresponds to item J) in the GALFIT feedme definition.

plate_scale

The pixel scale of the image, in arcsec / pixel. If provided as a tuple: (plate_scale_x, plate_scale_y) Corresponds to item I) in the GALFIT feedme definition.

sigma_file

Path to sigma image, if used. GALFIT is quite good at generating these internally. Corresponds to item C) in the GALFIT feedme definition.

psf_file

Path to input PSF image. Corresponds to item D) in the GALFIT feedme definition.

psf_fine_sampling

PSF fine sampling factor relative to data; ie, if the PSF image has double the resolution, psf_fine_sampling is 2. GALFIT only understands integers for this value. Corresponds to item E) in the GALFIT feedme definition.

mask_file

Path to pixel mask file, a FITS image or ASCII coord list. Corresponds to item F) in the GALFIT feedme definition.

constraint_file

Path to file with parameter constraints (ASCII file). Corresponds to item G) in the GALFIT feedme definition.

fitting_region_margins

Image region to fit (xmin xmax ymin ymax) in pixels. Corresponds to item H) in the GALFIT feedme definition.

convolution_size

Size of the convolution box (x y) in pixels. Corresponds to item I) in the GALFIT feedme definition.

display_type

regular, curses or both. I don’t actually know what this does. Corresponds to item O) in the GALFIT feedme definition.

run_type

optimize, model, imgblock or subcomps, in that order.

models

List of dicts containing model initial guesses, excluding sky.

sky_model_init

A model for the sky. Not required as GALFIT finds this pretty easily.

Returns

List of strings corresponding to lines in a .feedme input file, suitable for use with open().writelines().

craftutils.wrap.galfit.extract_fit_params(header: Header)

Extract the fitted parameters for all components from a GALFIT imgblock header.

craftutils.wrap.galfit.extract_sersic_params(component_n: int, ...)

Pull the fitted parameter values for a Sersic profile from a GALFIT imgblock header. :param component_n: Component number (In header: COMP_N, where N is component_n) :param header: the GALFIT-generated image header, as read by astropy.io.fits. :return: dict with keys being parameter names and values fitted parameter values

craftutils.wrap.galfit.extract_rotation_params(component_n, ...)

Pull the fitted parameter values for a coordinate-rotated model from a GALFIT imgblock header. :param component_n: Component number (In header: COMP_N, where N is component_n) :param header: the GALFIT-generated image header, as read by astropy.io.fits. :return: dict with keys being parameter names and values fitted parameter values

craftutils.wrap.galfit.extract_sky_params(component_n: int, header)

Pull the fitted parameter values for a sky model from a GALFIT imgblock header. :param component_n: Component number (In header: COMP_N, where N is component_n) :param header: the GALFIT-generated image header, as read by astropy.io.fits. :return: dict with keys being parameter names and values fitted parameter values

craftutils.wrap.galfit.sersic_best_row(tbl: Table)

Find the best model from a table of Sersic models. :param tbl: :return:

craftutils.wrap.galfit.a_func(theta_out)

Calculates the value A as defined in Galfit User’s Manual, Appendix A (Peng 2010) :param theta_out: Cumulative rotation to outer radius, in degrees.

Corresponds to item R3) in the GALFIT feedme definition.

Returns

A (dimensionless)

craftutils.wrap.galfit.b_func(r_in: Quantity, r_out: Quantity, ...)

Calculates the value B as defined in Galfit User’s Manual, Appendix A (Peng 2010) :param r_in: Spiral inner radius, in pixels. If this goes extremely negative in fitting, fix to 0.

Corresponds to item R1) in the GALFIT feedme definition.

Parameters
r_out: Quantity

Spiral outer radius, in pixels. Corresponds to item R2) in the GALFIT feedme definition.

theta_out

Cumulative rotation to outer radius, in degrees. Corresponds to item R3) in the GALFIT feedme definition.

Returns

B, in radians

craftutils.wrap.galfit.tanh_func(r_in: Quantity, r_out, ...)

Calculates the tanh function (not the hyperbolic tangent, but a function based around it) as defined in Galfit User’s Manual, Appendix A (Peng 2010) :param r_in: Spiral inner radius, in pixels. If this goes extremely negative in fitting, fix to 0.

Corresponds to item R1) in the GALFIT feedme definition.

Parameters
r_out

Spiral outer radius, in pixels. Corresponds to item R2) in the GALFIT feedme definition.

theta_out

Cumulative rotation to outer radius, in degrees. Corresponds to item R3) in the GALFIT feedme definition.

r

r coordinate of point(s); distance from the centre of the spiral model, in pixels.

Returns

output of tanh function

craftutils.wrap.galfit.tilt_spiral(theta_inc: Quantity, ...)

Apply transformations to r coordinate to account for model inclination. :param theta_inc: inclination to line-of-sight, in degrees.

Corresponds to item R9) in the GALFIT feedme definition.

Parameters
theta_pa

Position angle of the axis about which the galaxy is rotated for inclination. Corresponds to item R10) in the GALFIT feedme definition.

r

r coordinate; distance from the centre of the spiral model, in pixels.

r

theta coordinate

Returns

r_prime, appropriately transformed r coordinate

craftutils.wrap.galfit.spiral_log_tanh(r_in: Quantity, r_out, ...)

Calculates theta, x and y (image coordinates) for a GALFIT tanh log spiral, as defined in Galfit User’s Manual (Peng 2010), including tilt. :param r_in: Spiral inner radius, in pixels. If this goes extremely negative in fitting, fix to 0.

Corresponds to item R1) in the GALFIT feedme definition.

Parameters
r_out

Spiral outer radius, in pixels. Corresponds to item R2) in the GALFIT feedme definition.

r_ws

winding scale radius, in pixels. Corresponds to item R4) in the GALFIT feedme definition.

theta_inc

inclination to line-of-sight, in degrees. Corresponds to item R9) in the GALFIT feedme definition.

theta_pa

Position angle of the axis about which the galaxy is rotated for inclination. Corresponds to item R10) in the GALFIT feedme definition.

theta_out

Cumulative rotation to outer radius, in degrees. Corresponds to item R3) in the GALFIT feedme definition.

position_angle

position angle (PA) of model, in degrees, with up=0 and left=90 Corresponds to item 10) in the GALFIT feedme definition.

x

x-position of model centre, in pixels. Corresponds to first component of item 1) in the GALFIT feedme definition.

y

y-position of model centre, in pixels. Corresponds to second component of item 1) in the GALFIT feedme definition.

r

r coordinate; distance from the centre of the spiral model, in pixels.

tilt

If True, the spiral is transformed to account for its inclination angle. If False, the output spiral is equivalent to theta_inc == 0 deg.

Returns

theta_1, x_1, y_1, theta_2, x_2, y_2, where 1 and 2 refer to each spiral arm.

craftutils.wrap.galfit.spiral_log(r_out: Quantity, r_ws, ...)

Calculates theta, x and y (image coordinates) for a pure log spiral, based on equations in Galfit User’s Manual (Peng 2010), including tilt. :param r_out: Spiral outer radius, in pixels.

Corresponds to item R2) in the GALFIT feedme definition.

Parameters
r_ws

winding scale radius, in pixels. Corresponds to item R4) in the GALFIT feedme definition.

theta_inc

inclination to line-of-sight, in degrees. Corresponds to item R9) in the GALFIT feedme definition.

theta_pa

Position angle of the axis about which the galaxy is rotated for inclination. Corresponds to item R10) in the GALFIT feedme definition.

theta_out

Cumulative rotation to outer radius, in degrees. Corresponds to item R3) in the GALFIT feedme definition.

position_angle

position angle (PA) of model, in degrees, with up=0 and left=90 Corresponds to item 10) in the GALFIT feedme definition.

x

x-position of model centre, in pixels. Corresponds to first component of item 1) in the GALFIT feedme definition.

y

y-position of model centre, in pixels. Corresponds to second component of item 1) in the GALFIT feedme definition.

r

r coordinate; distance from the centre of the spiral model, in pixels.

tilt

If True, the spiral is transformed to account for its inclination angle. If False, the output spiral is equivalent to theta_inc == 0 deg.

Returns

theta_1, x_1, y_1, theta_2, x_2, y_2, where 1 and 2 refer to each spiral arm.

craftutils.wrap.galfit.spiral_from_model_dict(model_dict: dict, ...)

A wrapper for spiral_log_tanh() that does the work of unpacking the model dict returned by extract_fit_params. :param model_dict: dict containing output model param names as keys; the dictionaries generated by

extract_fit_params corresponding to “COMP_N” will work as they are, so long as they have rotation parameters.

Parameters
r

r coordinate of point(s); distance from the centre of the spiral model, in pixels.

kwargs

Any other keywords you wish to pass to the spiral_log_tanh function. Warning: if keys overlap with model_dict, the model_dict entries will be overwritten by the kwargs entries.

Returns

craftutils.wrap.galfit.imgblock_plot(img_block: HDUList | str, ...)
craftutils.wrap.montage.gain_median_combine(...)

Only valid if you have a median-combined image. :param old_gain: Gain of the individual images. :param n_frames: Number of stacked images. :return:

craftutils.wrap.montage.gain_mean_combine(...)
craftutils.wrap.montage.image_table(input_directory: str, ...)

Executes the Montage task mImgtbl <input_directory> <output_path> :param input_directory: :param output_path: :return:

craftutils.wrap.montage.make_header(table_path: str, output_path)

Executes Montage task mMakeHdr <table_path> <output_path> :param table_path: :param output_path: :return:

craftutils.wrap.montage.check_input_images(input_directory: str, **)
craftutils.wrap.montage.inject_header(file_path: str, ...)
craftutils.wrap.montage.dict_to_montage_header(dictionary, ...)
craftutils.wrap.montage.project_execute(input_directory: str, ...)

Executes mProjExec <table_path> <header_path> <proj_dir> <stats_table_path> -p <input_directory> :param input_directory: :param table_path: :param header_path: :param proj_dir: :param stats_table_path: :return:

craftutils.wrap.montage.overlaps(table_path: str, ...)

Executes mOverlaps <table_path> <difference_table_path> :param table_path: :param difference_table_path: :return:

craftutils.wrap.montage.difference_execute(input_directory, ...)

Executes mDiffExec <difference_table_path> <header_file> <diff_dir> -p <input_directory> :param input_directory: :return:

craftutils.wrap.montage.fit_execute(difference_table_path: str, ...)

Executes mFitExec <difference_table_path> <fit_table_path> <diff_dir> :param difference_table_path: :param fit_table_path: :param diff_dir: :return:

craftutils.wrap.montage.background_model(table_path: str, ...)

Executes mBGModel <table_path> <fit_table_path> <correction_table_path> :param table_path: :param fit_table_path: :param correction_table_path: :return:

craftutils.wrap.montage.background_execute(input_directory, ...)

Executes mBgExec <table_path> <corrections_table_path> <corr_dir> -p <input_directory> :param input_directory: :param table_path: :param correction_table_path: :param corr_dir: :return:

craftutils.wrap.montage.add(input_directory: str, table_path, ...)

Executes mAdd <table_path> <template_path> <output_path> -p <input_directory> -a <coadd_type> :param input_directory: :param table_path: :param header_path: :param output_path: :param coadd_type: :return:

craftutils.wrap.montage.standard_script(input_directory: str, ...)

Does a standard median coaddition of fits files in input_directory. Adapted from an example bash script found at http://montage.ipac.caltech.edu/docs/first_mosaic_tutorial.html :param input_directory: path to directory containing input images. :param output_directory: path to directory to write data products to; will be created if it doesn’t exist. :param output_file_name: Name of final coadded image file. :param ignore_differences: If False, checks if input images have compatible exposure times, filter, etc. and raises ValueError if not. :return:

craftutils.wrap.psfex.shift(input, shift, output=None, order=3, ...)

Shift an array.

craftutils.wrap.psfex.system_command(command: str, ...)
craftutils.wrap.psfex.check_iterable(obj)
craftutils.wrap.psfex.check_successful(hdu)
craftutils.wrap.psfex.psfex(catalog: str, ...)
craftutils.wrap.psfex.load_psfex_oversampled(model, ...)

Since PSFEx generates a model using basis vectors, with the linear combination dependent on image position, this is used to collapse that into a useable kernel for convolution and insertion purposes. See https://psfex.readthedocs.io/en/latest/Appendices.html This function will return the PSFEx output with the pixel scale of the PSFEx output. To retrieve an image with the same pixel scale as the original science image, use load_psfex() :param model: Path to PSFEx-generated model, as a FITS file (usually ends in .psf); OR HDUList representing the

file.

Parameters
x

pixel x-coordinate to use for model input

y

pixel y-coordinate to use for model input

Returns

numpy.ndarray representing the PSF model as an image.

craftutils.wrap.psfex.load_psfex(model_path: str, x: float, y)

Since PSFEx generates a model using basis vectors, with the linear combination dependent on image position, this is used to collapse that into a useable kernel for convolution and insertion purposes. See https://psfex.readthedocs.io/en/latest/Appendices.html This function will return the PSFEx output to the pixel scale of the original image. To keep an oversampled PSF model image, use load_psfex_oversampled() :param model_path: Path to PSFEx-generated model, as a FITS file (usually ends in .psf) :param x: pixel x-coordinate to use for model input :param y: pixel y-coordinate to use for model input :return: numpy.ndarray representing the PSF model as an image.

craftutils.wrap.source_extractor.source_extractor(image_path, ...)
Parameters
configs

Any source-extractor (sextractor) parameter, normally read via the config file but that can be

overridden by passing to the shell command, can be given here.