io
Module for file read / write tasks.
- lsmtool.io.check_file_exists(path: str | Path)
Check if a file exists at the given path.
This function checks if a file exists at the specified path. It raises exceptions if the path is not a string or pathlib.Path object, or if the file does not exist.
- Parameters:
path (
strorpathlib.Path) – The path to the file.- Returns:
path (
pathlib.Path) – The path to the file.- Raises:
TypeError – If the input path is not a string or pathlib.Path object.
FileNotFoundError – If the file does not exist at the given path.
- lsmtool.io.convert_coordinates_to_pixels(coordinates, wcs)
Convert celestial coordinates (RA, Dec) to image pixel coordinates.
This function transforms an array of shape (N ,2), with RA and Dec coordinates as columns, into pixel coordinates using the provided WCS object, handling extra axes as needed.
- Parameters:
coordinates (
numpy.ndarray) – Array of shape (N, 2) containing RA and Dec values.wcs (
astropy.wcs.WCS) – WCS object used for the coordinate transformation.
- Returns:
vertices (
listoftuple) – List of (x, y) pixel coordinate tuples.
- lsmtool.io.load(
- fileName: str | Path,
- beamMS: str | Path | None = None,
- VOPosition: Sequence[Real] = None,
- VORadius: Real | str = None,
Load a sky model from a file or VO service and return a SkyModel object.
- Parameters:
fileName (
strorpathlib.Path) – Input ASCII file from which the sky model is read (must respect the makesourcedb format), name of VO service to query (must be one of ‘GSM’, ‘LOTSS’, ‘NVSS’, ‘TGSS’, ‘VLSSR’, or ‘WENSS’), or dict (single source only).beamMS (
strorpathlib.Path, optional) – Measurement set from which the primary beam will be estimated. A column of attenuated Stokes I fluxes will be added to the table.VOPosition (
listoffloats) – A list specifying a new position as [RA, Dec] in either makesourcedb format (e.g., [‘12:23:43.21’, ‘+22.34.21.2’]) or in degrees (e.g., [123.2312, 23.3422]) for a cone search.VORadius (
floatorstr, optional) – Radius in degrees (if float) or ‘value unit’ (if str; e.g., ‘30 arcsec’) for cone search region in degrees.
- Returns:
skymodel (
lsmtool.skymodel.SkyModel) – A SkyModel object that stores the sky model and provides methods for accessing it.
Examples
Load a sky model from a makesourcedb-formated file:
>>> import lsmtool >>> s = lsmtool.load('sky.model')
Load a sky model with a beam MS so that apparent fluxes will be available (in addition to intrinsic fluxes):
>>> s = lsmtool.load('sky.model', 'SB100.MS')
Load a sky model from the WENSS using all sources within 5 degrees of the position RA = 212.8352792, Dec = 52.202644:
>>> s = lsmtool.load('WENSS', VOPosition=[212.8352792, 52.202644], VORadius=5.0)
Load a sky model from a dictionary defining the source:
>>> source = {'Name':'src1', 'Type':'POINT', 'Ra':'12:32:10.1', 'Dec':'23.43.21.21', 'I':2.134} >>> s = lsmtool.load(source)
- lsmtool.io.read_vertices_ra_dec(filename: str | Path)
Read facet vertices from a stored numpy array in .npy format.
- Parameters:
filename (
strorpathlib.Path) – The path to the file where facet vertices are stored as an array with RA and Dec values along columns.- Returns:
vertices (
numpy.ndarray) – Array of shape (N, 2) containing RA and Dec vertices as columns.
- lsmtool.io.read_vertices_x_y(filename, wcs)
Read facet vertices from a file and convert them to pixel coordinates.
- Parameters:
filename (
strorpathlib.Path) – Path to file containing the vertices to read.wcs (
astropy.wcs.WCS) – WCS object for converting the vertices to pixel coordinates.
- Returns:
vertices (
listoftupleoffloat) – The converted coordinates.
- lsmtool.io.temp_storage( )
Context manager for setting a temporary storage path.
This context manager attempts to set the TMPDIR environment variable to a short path to avoid issues with path length limitations, particularly for socket paths used by the multiprocessing module in PyBDSF calls.
- lsmtool.io.validate_paths(required: bool = True, **filenames)
Checks if provided filenames exist, raising an exception if a file is non-existant and required.
- Parameters:
required (
bool, optional) – If True, all filenames must correspond to existing files. If False, non truthy objects (None or “” etc) will not raise an exception.**filenames – Keyword arguments where keys are parameter names and values are filenames to validate.
- Raises:
TypeError – If a filename is not a string or pathlib.Path object. The corresponding parameter name will be shown in the exception message.
FileNotFoundError – If a required file does not exist. The corresponding parameter name will be shown in the exception message.