diff --git a/pyaerocom/_lowlevel_helpers.py b/pyaerocom/_lowlevel_helpers.py index 4e25f5438..00798847a 100644 --- a/pyaerocom/_lowlevel_helpers.py +++ b/pyaerocom/_lowlevel_helpers.py @@ -9,6 +9,7 @@ from pathlib import Path import numpy as np +from typing_extensions import TypedDict from pyaerocom._warnings import ignore_warnings @@ -704,3 +705,10 @@ def str_underline(title: str, indent: int = 0): length = indent + len(title) underline = "-" * len(title) return f"{title:>{length}}\n{underline:>{length}}" + + +class RegridResDeg(TypedDict): + """Typed dict for regridding resolution degrees""" + + lat_res_deg: float + lon_res_deg: float diff --git a/pyaerocom/colocation/colocation_3d.py b/pyaerocom/colocation/colocation_3d.py index 9af4a85fc..853e35d4d 100644 --- a/pyaerocom/colocation/colocation_3d.py +++ b/pyaerocom/colocation/colocation_3d.py @@ -14,6 +14,7 @@ from pyaerocom import __version__ as pya_ver from pyaerocom import const +from pyaerocom._lowlevel_helpers import RegridResDeg from pyaerocom.exceptions import ( DataUnitError, DimensionOrderError, @@ -303,7 +304,7 @@ def colocate_vertical_profile_gridded( start: str | None = None, stop: str | None = None, filter_name: str = None, - regrid_res_deg: int | dict | None = None, + regrid_res_deg: float | RegridResDeg | None = None, harmonise_units: bool = True, regrid_scheme: str = "areaweighted", var_ref: str = None, diff --git a/pyaerocom/colocation/colocation_setup.py b/pyaerocom/colocation/colocation_setup.py index b22682db6..538f700c8 100644 --- a/pyaerocom/colocation/colocation_setup.py +++ b/pyaerocom/colocation/colocation_setup.py @@ -16,6 +16,7 @@ ) from pyaerocom import const +from pyaerocom._lowlevel_helpers import RegridResDeg from pyaerocom.config import ALL_REGION_NAME from pyaerocom.helpers import start_stop from pyaerocom.io.pyaro.pyaro_config import PyaroConfig @@ -257,9 +258,12 @@ class ColocationSetup(BaseModel): harmonise_units : bool if True, units are attempted to be harmonised during co-location (note: raises Exception if True and in case units cannot be harmonised). - regrid_res_deg : int, optional - resolution in degrees for regridding of model grid (done before - co-location). Default is None. + regrid_res_deg : float or dict, optional + regrid resolution in degrees. If specified, the input gridded data + objects will be regridded in lon / lat dimension to the input + resolution (if input is float, both lat and lon are regridded to that + resolution, if input is dict, use keys `lat_res_deg` and `lon_res_deg` + to specify regrid resolutions, respectively). Default is None. colocate_time : bool if True and if obs and model sampling frequency (e.g. daily) are higher than output colocation frequency (e.g. monthly), then the datasets are @@ -411,7 +415,7 @@ def validate_basedirs(cls, v): model_outlier_ranges: dict[str, tuple[float, float]] | None = {} zeros_to_nan: bool = False harmonise_units: bool = False - regrid_res_deg: float | None = None + regrid_res_deg: float | RegridResDeg | None = None colocate_time: bool = False reanalyse_existing: bool = True raise_exceptions: bool = False diff --git a/pyaerocom/colocation/colocation_utils.py b/pyaerocom/colocation/colocation_utils.py index bd35fbb0d..7ef83f3ed 100644 --- a/pyaerocom/colocation/colocation_utils.py +++ b/pyaerocom/colocation/colocation_utils.py @@ -12,6 +12,7 @@ from pyaerocom import __version__ as pya_ver from pyaerocom import const +from pyaerocom._lowlevel_helpers import RegridResDeg from pyaerocom.exceptions import ( DataUnitError, DimensionOrderError, @@ -69,7 +70,7 @@ def resolve_var_name(data): return (var, vardef.var_name_aerocom) -def _regrid_gridded(gridded, regrid_scheme, regrid_res_deg): +def _regrid_gridded(gridded, regrid_scheme: str, regrid_res_deg: RegridResDeg): """ Regrid instance of `GriddedData` @@ -99,7 +100,7 @@ def _regrid_gridded(gridded, regrid_scheme, regrid_res_deg): regridded data object """ - if not isinstance(regrid_res_deg, dict): + if not isinstance(regrid_res_deg, dict): # at runtime RegridResDeg is a dict if not isnumeric(regrid_res_deg): raise ValueError( "Invalid input for regrid_res_deg. Need integer " @@ -158,9 +159,9 @@ def colocate_gridded_gridded( start=None, stop=None, filter_name=None, - regrid_res_deg=None, + regrid_res_deg: float | RegridResDeg | None = None, harmonise_units=True, - regrid_scheme="areaweighted", + regrid_scheme: str = "areaweighted", update_baseyear_gridded=None, min_num_obs=None, colocate_time=False, @@ -580,9 +581,9 @@ def colocate_gridded_ungridded( start=None, stop=None, filter_name=None, - regrid_res_deg=None, + regrid_res_deg: float | RegridResDeg | None = None, harmonise_units=True, - regrid_scheme="areaweighted", + regrid_scheme: str = "areaweighted", var_ref=None, update_baseyear_gridded=None, min_num_obs=None,