galfitools.desi package

Submodules

galfitools.desi.central_ellipse module

galfitools.desi.central_ellipse.build_selected_mask(image: ndarray, target_value: int = 4096, use_bit: bool = False, bit: int = 12) ndarray[source]

Build a boolean mask from a maskbits image.

Parameters:
  • image (numpy.ndarray) – Input maskbits image.

  • target_value (int, optional) – Exact pixel value used when use_bit is False.

  • use_bit (bool, optional) – If True, select pixels where the given bit is active.

  • bit (int, optional) – Bit number used when use_bit is True.

Returns:

Boolean selected mask.

Return type:

numpy.ndarray

galfitools.desi.central_ellipse.central_ellipse(maskbits, region, value=4096, use_bit=False, bit=12, scale=1.0, angle_step=1.0, refine=False, refine_window=None, refine_step=0.1, radial_step=0.2, component_out=None, color='green')[source]
galfitools.desi.central_ellipse.estimate_component_center(component_mask: ndarray) tuple[float, float][source]

Estimate the centroid of a connected component.

Parameters:

component_mask (numpy.ndarray) – Boolean mask of the selected component.

Returns:

  • x0 (float) – Centroid x coordinate in NumPy coordinates.

  • y0 (float) – Centroid y coordinate in NumPy coordinates.

galfitools.desi.central_ellipse.maincentralEllipse() None[source]
galfitools.desi.central_ellipse.normalize_angle_180(theta_deg: float) float[source]

Normalize an angle to [0, 180).

galfitools.desi.central_ellipse.pixel_is_selected(component_mask: ndarray, x: float, y: float) bool[source]

Test whether the nearest pixel to (x, y) belongs to the component mask.

Parameters:
  • component_mask (numpy.ndarray) – Boolean mask of the selected component.

  • x (float) – X coordinate in NumPy pixel coordinates.

  • y (float) – Y coordinate in NumPy pixel coordinates.

Returns:

True if the nearest pixel belongs to the selected component.

Return type:

bool

galfitools.desi.central_ellipse.ray_radius(component_mask: ndarray, x0: float, y0: float, theta_deg: float, step: float = 0.2, max_radius: float | None = None) float[source]

Measure the radius along one direction until the component mask ends.

The angle is measured from the +x axis. Positive angles rotate counterclockwise on the displayed image.

Parameters:
  • component_mask (numpy.ndarray) – Boolean mask of the selected component.

  • x0 (float) – Center x coordinate in NumPy coordinates.

  • y0 (float) – Center y coordinate in NumPy coordinates.

  • theta_deg (float) – Direction angle in degrees.

  • step (float, optional) – Radial step in pixels.

  • max_radius (float, optional) – Maximum radius to test.

Returns:

Radius in pixels.

Return type:

float

galfitools.desi.central_ellipse.read_first_image_hdu(filename: str) tuple[ndarray, Header][source]

Read the first FITS HDU that contains image data.

Parameters:

filename (str) – Input FITS filename.

Returns:

  • data (numpy.ndarray) – Image data array.

  • header (astropy.io.fits.Header) – Header of the image HDU.

galfitools.desi.central_ellipse.refine_major_angle(component_mask: ndarray, x0: float, y0: float, theta_coarse: float, coarse_step: float = 1.0, refine_window: float | None = None, refine_step: float = 0.1, radial_step: float = 0.2) tuple[float, float, float, float][source]

Refine the major-axis angle around the coarse solution.

Parameters:
  • component_mask (numpy.ndarray) – Boolean mask of the selected component.

  • x0 (float) – Center x coordinate.

  • y0 (float) – Center y coordinate.

  • theta_coarse (float) – Coarse major-axis angle.

  • coarse_step (float, optional) – Coarse angular step.

  • refine_window (float, optional) – Half-width of the refinement window.

  • refine_step (float, optional) – Fine angular step.

  • radial_step (float, optional) – Radial step in pixels.

Returns:

  • best_theta (float) – Refined best angle in degrees.

  • best_r1 (float) – Forward radius.

  • best_r2 (float) – Opposite radius.

  • best_score (float) – Symmetric score.

galfitools.desi.central_ellipse.search_best_major_angle(component_mask: ndarray, x0: float, y0: float, angle_start: float = 0.0, angle_stop: float = 180.0, angle_step: float = 1.0, radial_step: float = 0.2) tuple[float, float, float, float][source]

Search for the major-axis angle using a symmetric criterion.

The selected angle maximizes min(r(theta), r(theta + 180)). Ties are broken using the larger sum r(theta) + r(theta + 180).

Parameters:
  • component_mask (numpy.ndarray) – Boolean mask of the selected component.

  • x0 (float) – Center x coordinate.

  • y0 (float) – Center y coordinate.

  • angle_start (float, optional) – Start angle in degrees.

  • angle_stop (float, optional) – Stop angle in degrees.

  • angle_step (float, optional) – Angular step in degrees.

  • radial_step (float, optional) – Radial step in pixels.

Returns:

  • best_theta (float) – Best angle in degrees.

  • best_r1 (float) – Forward radius at the best angle.

  • best_r2 (float) – Opposite radius at the best angle.

  • best_score (float) – Symmetric score at the best angle.

galfitools.desi.central_ellipse.select_central_component(selected_mask: ndarray) ndarray[source]

Select the connected component closest to the image center.

Parameters:

selected_mask (numpy.ndarray) – Boolean mask.

Returns:

Boolean mask for the selected central component.

Return type:

numpy.ndarray

galfitools.desi.central_ellipse.symmetric_radii(component_mask: ndarray, x0: float, y0: float, theta_deg: float, radial_step: float = 0.2) tuple[float, float, float][source]

Compute forward and opposite radii for one angle.

Parameters:
  • component_mask (numpy.ndarray) – Boolean mask of the selected component.

  • x0 (float) – Center x coordinate.

  • y0 (float) – Center y coordinate.

  • theta_deg (float) – Angle in degrees.

  • radial_step (float, optional) – Radial step in pixels.

Returns:

  • r_forward (float) – Radius at theta_deg.

  • r_opposite (float) – Radius at theta_deg + 180 deg.

  • score (float) – Symmetric score, defined as min(r_forward, r_opposite).

galfitools.desi.central_ellipse.write_component_mask(output_file: str, component_mask: ndarray, header: Header | None = None) None[source]

Write the selected central component as a FITS image.

Parameters:
  • output_file (str) – Output FITS filename.

  • component_mask (numpy.ndarray) – Boolean component mask.

  • header (astropy.io.fits.Header, optional) – Header copied from the input image.

galfitools.desi.central_ellipse.write_ds9_ellipse_region(region_file: str, x0_ds9: float, y0_ds9: float, semi_major: float, semi_minor: float, theta_ds9: float, color: str = 'green') None[source]

Write a DS9 ellipse region file.

Parameters:
  • region_file (str) – Output region filename.

  • x0_ds9 (float) – Center x coordinate in DS9 image coordinates.

  • y0_ds9 (float) – Center y coordinate in DS9 image coordinates.

  • semi_major (float) – Semi-major axis in pixels.

  • semi_minor (float) – Semi-minor axis in pixels.

  • theta_ds9 (float) – DS9 position angle in degrees.

  • color (str, optional) – DS9 region color.

galfitools.desi.decam2sdss module

galfitools.desi.decam2sdss.ab_mag_from_flambda(wave_A, trans, f_lambda)[source]

Compute AB mag given wavelength [Å], throughput, and f_lambda.

galfitools.desi.decam2sdss.apply_transform(direction, mags_in, coeffs, color_base=None)[source]

direction: ‘decam_to_sdss’ or ‘sdss_to_decam’ mags_in: dict with provided source mags, e.g., {‘g’:16.99, ‘z’:15.20} color_base: ‘gr’,’rz’,’gz’ or None to auto-choose from the provided pair. Returns (out_dict, base_used, rms_dict). Only bands present in mags_in are output.

galfitools.desi.decam2sdss.derive_coeffs(T_grid=(3500, 4000, 4500, 5000, 6000, 8000, 10000))[source]
galfitools.desi.decam2sdss.fit_linear(y, x)[source]

Fit y = a + b x. Return (a, b, rms).

galfitools.desi.decam2sdss.load_filters_speclite()[source]
galfitools.desi.decam2sdss.maindecam2sdss()[source]
galfitools.desi.decam2sdss.planck_flambda(wave_A, T_K)[source]

Blackbody f_lambda (shape only), erg s^-1 cm^-2 Å^-1 (up to a constant).

galfitools.desi.download_legacy_products module

galfitools.desi.download_legacy_products.configure_session() Session[source]

Create a requests session with retries for common HTTP status errors.

galfitools.desi.download_legacy_products.convert_to_sigma(image_file: Path) None[source]

Convert an inverse-variance DESI image into a sigma image for GALFIT.

DESI inverse variance is assumed to be invvar = 1 / sigma**2. Therefore, the sigma image is computed as sigma = 1 / sqrt(invvar). Invalid or non-positive inverse-variance values are assigned a large sigma.

galfitools.desi.download_legacy_products.fetch_fits(session: Session, url: str, params: dict, timeout: int = 120, retries: int = 5, retry_wait: float = 3.0, label: str = 'FITS file') HDUList[source]

Download a FITS file with retries.

This function catches incomplete reads and temporary connection failures. These errors can occur when a remote server closes a large download before all bytes have arrived.

Parameters:
  • session (requests.Session) – Active HTTP session.

  • url (str) – URL of the FITS service.

  • params (dict) – Query parameters for the request.

  • timeout (int, optional) – Request timeout in seconds.

  • retries (int, optional) – Maximum number of attempts.

  • retry_wait (float, optional) – Initial waiting time between attempts, in seconds. The wait time grows linearly with the attempt number.

  • label (str, optional) – Short label printed in the error messages.

Returns:

Downloaded FITS file.

Return type:

astropy.io.fits.HDUList

galfitools.desi.download_legacy_products.find_first_image_hdu(hdul: HDUList) ImageHDU | PrimaryHDU[source]

Return the first HDU that contains image data.

galfitools.desi.download_legacy_products.find_hdu_by_name_or_index(hdul: HDUList, names: set[str], fallback_index: int | None = None) ImageHDU | PrimaryHDU | None[source]

Return an HDU by extension name, with an optional index fallback.

galfitools.desi.download_legacy_products.main_downloadDesi() int[source]
galfitools.desi.download_legacy_products.read_radec_csv(path: str) List[Tuple[str, float, float]][source]

Read a CSV file with RA and DEC columns.

The CSV file must contain columns named ra and dec. These names are case-insensitive. If an objid column exists, it is used as the object identifier. Otherwise, sequential identifiers are assigned.

Parameters:

path (str) – Input CSV filename.

Returns:

Each tuple contains (objid, ra, dec).

Return type:

list of tuple

galfitools.desi.download_legacy_products.split_cutout_products(hdul: HDUList, require_maskbits: bool = True) tuple[PrimaryHDU, PrimaryHDU, PrimaryHDU | None][source]

Return the image, inverse-variance, and optional maskbits HDUs.

The Legacy Survey FITS cutout service may return several extensions. The usual order is image, inverse variance, and maskbits when both invvar and maskbits are requested. This function first searches by HDU name and then falls back to the expected extension order.

galfitools.desi.download_legacy_products.write_failure_log(failures: list[dict[str, str]], outdir: Path) None[source]

Write a CSV file with the downloads that failed.

galfitools.desi.download_legacy_products.write_primary(data, header, outpath: Path) None[source]

Write data and header as a primary FITS HDU.

galfitools.desi.get_seg_value module

galfitools.desi.get_seg_value.get_pixel_value(segmentation_file, x=None, y=None, ext=0)[source]

Return the value of a selected pixel in a segmentation image.

By default, the selected pixel is the central pixel of the image.

Parameters:
  • segmentation_file (str) – Path to the SExtractor segmentation FITS image.

  • x (int, optional) – Pixel x-coordinate using Python zero-based indexing. If None, the central x-coordinate is used.

  • y (int, optional) – Pixel y-coordinate using Python zero-based indexing. If None, the central y-coordinate is used.

  • ext (int, optional) – FITS extension containing the segmentation image. Default is 0.

Returns:

  • value (int or float) – Value of the selected pixel.

  • x (int) – Selected x pixel coordinate using Python indexing.

  • y (int) – Selected y pixel coordinate using Python indexing.

galfitools.desi.get_seg_value.maingetSegValue()[source]

galfitools.desi.megamask module

galfitools.desi.megamask.mainmegaMask()[source]
galfitools.desi.megamask.megaMask(segmentation_file: str, masksky_file: str, maskbits_file: str, output='megamask.fits', rem_masksky=False, rem_mask=False, ds9ellipse=None)[source]

Combines SExtractor segmentation fits file, mask fits file (created with maskSky), and DESI maskbits fits file. It removes the central galaxy mask.

Parameters:
  • segmentation_file (str) – Path to the SExtractor segmentation FITS image.

  • masksky_file (str) – Path to the mask FITS image.

  • maskbits_file (str) – Path to the DESI maskbits FITS image.

  • rem_masksky (bool) – If True, removes central galaxy from masksky using DS9 ellipse maskbits region file recommended if central galaxy has not been removed from this mask.

  • rem_mask (bool) – If True, removes completelly the central galaxy from maskbits using DS9 ellipse maskbits region file If false, removes the central galaxy for those pixels where the galaxy is labeled in maskbits

  • output (str optional) – name of the output mask fits file. Default: megamask.fits

Return type:

True

galfitools.desi.transform_ds9_ellipse module

galfitools.desi.transform_ds9_ellipse.build_ellipse(xcen, ycen, major_axis, minor_axis, angle, unit)[source]

Build a DS9 ellipse string.

galfitools.desi.transform_ds9_ellipse.format_value(value, unit='')[source]

Format a numerical value for a DS9 region file.

galfitools.desi.transform_ds9_ellipse.get_transformed_parameters(match, axis_ratio=0.4, scale=1.0)[source]

Compute the transformed ellipse parameters.

Rules

  1. The center is unchanged.

  2. The first output ellipse is rotated by 90 degrees relative to the input major axis.

  3. The new major axis is the old minor axis.

  4. The new axis ratio is fixed by axis_ratio.

  5. The output axes are multiplied by scale.

galfitools.desi.transform_ds9_ellipse.maintransformEllip()[source]
galfitools.desi.transform_ds9_ellipse.parse_angle(value)[source]

Parse a DS9 angle value in degrees.

galfitools.desi.transform_ds9_ellipse.parse_axis(value)[source]

Parse a DS9 axis value.

Parameters:

value (str) – Axis value from a DS9 ellipse region.

Returns:

  • number (float) – Numerical value.

  • unit (str) – Unit suffix, if present.

galfitools.desi.transform_ds9_ellipse.transform_ellipse(match, axis_ratio=0.4, scale=1.0)[source]

Transform one DS9 ellipse region.

galfitools.desi.transform_ds9_ellipse.transform_ellipse_rot90(match, axis_ratio=0.4, scale=1.0)[source]

Transform one DS9 ellipse region and rotate it by an additional 90 degrees.

galfitools.desi.transform_ds9_ellipse.transform_region_file(input_file, output_file, output_file_rot90, axis_ratio=0.4, scale=1.0)[source]

Read a DS9 region file and write two transformed region files.

Parameters:
  • input_file (str) – Input DS9 region file.

  • output_file (str) – First output DS9 region file.

  • output_file_rot90 (str) – Second output DS9 region file, rotated 90 degrees from the first.

  • axis_ratio (float, optional) – Axis ratio of the output ellipses. Default is 0.4.

  • scale (float, optional) – Multiplicative scale factor for both output axes. Default is 1.0.

Module contents