Skip to content

utils module

The utils module provides functions used by the other modules.

check_valid_file(file_path, type='PRS_L2D')

Checks if the given file path points to a valid file.

Parameters:

Name Type Description Default
file_path str

Path to the file.

required
type str

Expected file type ('PRS_L2B', 'PRS_L2C', 'PRS_L2D'). Defaults to 'PRS_L2D'.

'PRS_L2D'

Returns:

Type Description
bool

True if file_path points to the correct file, False otherwise.

Source code in prismatools/utils.py
def check_valid_file(file_path: str, type: str = "PRS_L2D") -> bool:
    """
    Checks if the given file path points to a valid file.

    Args:
        file_path (str): Path to the file.
        type (str, optional): Expected file type ('PRS_L2B', 'PRS_L2C', 'PRS_L2D'). Defaults to 'PRS_L2D'.

    Returns:
        bool: True if file_path points to the correct file, False otherwise.
    """
    if not os.path.isfile(file_path):
        raise FileNotFoundError(f"{file_path} does not exist.")

    valid_types = {"PRS_L2B", "PRS_L2C", "PRS_L2D"}
    if type not in valid_types:
        raise ValueError(
            f"Unsupported file type: {type}. Supported types are {valid_types}."
        )

    basename = os.path.basename(file_path)
    return basename.startswith(type) and basename.endswith(".he5")

convert_coords(coords, from_epsg, to_epsg)

Convert a list of coordinates from one EPSG to another. Coordinates are expected in the format (lat, lon).

Parameters:

Name Type Description Default
coords List[Tuple[float, float]]

List of tuples containing coordinates in the format (latitude, longitude).

required
from_epsg str

Source EPSG code (e.g. "epsg:4326").

required
to_epsg str

Target EPSG code (e.g. "epsg:32632").

required

Returns:

Type Description
List[Tuple[float, float]]

List of tuple containing converted coordinates (x, y)

Source code in prismatools/utils.py
def convert_coords(
    coords: List[Tuple[float, float]], from_epsg: str, to_epsg: str
) -> List[Tuple[float, float]]:
    """
    Convert a list of coordinates from one EPSG to another. Coordinates are
    expected in the format (lat, lon).

    Args:
        coords (List[Tuple[float, float]]):
            List of tuples containing coordinates in the format (latitude, longitude).
        from_epsg (str): Source EPSG code (e.g. "epsg:4326").
        to_epsg (str): Target EPSG code (e.g. "epsg:32632").

    Returns:
        List of tuple containing converted coordinates (x, y)
    """
    transformer = pyproj.Transformer.from_crs(from_epsg, to_epsg, always_xy=True)
    return [transformer.transform(lon, lat) for lat, lon in coords]

get_transform(ul_easting, ul_northing, res=30)

Returns an affine transformation for a given upper-left corner and resolution.

Parameters:

Name Type Description Default
ul_easting float

Easting coordinate of the upper-left corner.

required
ul_northing float

Northing coordinate of the upper-left corner.

required
res int

Pixel resolution. Defaults to 30.

30

Returns:

Type Description
Affine

Affine transformation object representing the spatial transform.

Source code in prismatools/utils.py
def get_transform(ul_easting: float, ul_northing: float, res: int = 30) -> Affine:
    """
    Returns an affine transformation for a given upper-left corner and resolution.

    Args:
        ul_easting (float): Easting coordinate of the upper-left corner.
        ul_northing (float): Northing coordinate of the upper-left corner.
        res (int, optional): Pixel resolution. Defaults to 30.

    Returns:
        Affine: Affine transformation object representing the spatial transform.
    """
    return Affine.translation(ul_easting, ul_northing) * Affine.scale(res, -res)