utils

Module that holds miscellaneous utility functions and classes.

This file was adapted from the original in the Rapthor repository:

Additional functions for supporting skymodel filtering based on SoFiA was added for the SKA self calibration pipeline in this merge request.

Some functions were removed or combined when migrating the module to LSMTools.

lsmtool.utils.format_coordinates(ra, dec, precision=6)

Format RA and Dec coordinates to strings in the makesourcedb format using astropy.

Converts RA and Dec values (in degrees) to string representations according to the BBS makesourcedb sky model format. The format specification can be found in the lofar documentation.

Parameters:
  • ra (float or list of float) – Right ascension values in degrees.

  • dec (float or list of float) – Declination values in degrees.

  • precision (int, optional) – The number of decimal places for seconds in RA and Dec strings. Default is 6.

Returns:

coords (tuple of numpy.ndarray) – A tuple containing two arrays: formatted RA strings and formatted Dec strings.

lsmtool.utils.rasterize(vertices, data, blank_value=0)

Rasterize a polygon into a data array, setting any data outside the polygon to blank_value.

Note

This function modifies data in-place.

Parameters:
  • vertices (list of tuple) – List of input vertices of polygon to rasterize. Each item in the list should be a (x, y) coordinate point, where x and y are float or int.

  • data (numpy.ndarray) – A 2-D numpy array into which to rasterize the polygon. Note that the data are updated in-place.

  • blank_value (numbers.Real, optional) – Value to use for filling regions outside the polygon. The data type of the fill value should be compatible with the dtype of the data array.

Returns:

data (numpy.ndarray) – 2-D array containing the rasterized polygon.

lsmtool.utils.rasterize_polygon_mask_exterior(
fits_file,
vertices_file,
output_file=None,
precision=None,
)

Rasterize the image data in fits_file using the polygon defined by the vertices_file, mask any data outside the polygon, and write the result to an output_fits file.

Parameters:
  • fits_file (str or pathlib.Path) – Path to the input FITS file.

  • vertices_file (str or pathlib.Path) – Path to the file containing polygon vertices in RA, DEC coordinates.

  • output_file (str or pathlib.Path, optional) – Path to the output FITS file. If None, the default, the input file will be overwritten.

  • precision (int, optional) – A integer specifying the numeric precision for the converted vertices. If provided, the vertex points will be rounded to this number of significant figures. This is useful on occasion since there may be some imprecision in the result due to rounding errors.

lsmtool.utils.rotation_matrix_2d(theta)

Computes the transformation matrix for rotating vectors with two spatial dimensions by angle theta in radians.

Parameters:

theta (numbers.Real or numpy.ndarray) – The angle of rotation in radians. If theta is a number (float or int), the resulting array will have shape (2, 2). If theta is a numpy array of shape (n, m, …), the resulting array will have dimensions (2, 2, n, m, …).

Returns:

matrix (numpy.ndarray) – A NumPy array containing the rotation matrix (or matrices) along the first two array dimensions.

lsmtool.utils.table_to_array(table, dtype=<class 'float'>)

Convert an astropy Table into a NumPy array.

Converts a two-column astropy Table to a NumPy array of the specified dtype. This function assumes each row in the table has a uniform data type.

Parameters:
  • table (astropy.table.Table) – The input astropy Table with two columns.

  • dtype (type, optional) – The desired data type for the NumPy array. Defaults to float.

Returns:

array (numpy.ndarray) – A NumPy array of shape (n, 2) where n is the number of rows in the input table, and the dtype is as specified.

lsmtool.utils.transfer_patches(from_skymodel, to_skymodel, patch_dict=None)

Transfers the patches defined in from_skymodel to to_skymodel.

Parameters: