Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix res_grid_deg bug to allow either floats or a dict #1227

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pyaerocom/_lowlevel_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pathlib import Path

import numpy as np
from typing_extensions import TypedDict

from pyaerocom._warnings import ignore_warnings

Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion pyaerocom/colocation/colocation_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 8 additions & 4 deletions pyaerocom/colocation/colocation_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions pyaerocom/colocation/colocation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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`

Expand Down Expand Up @@ -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 "
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down