Skip to content

Commit

Permalink
Merge pull request #683 from metno/dummy_cube_latlon_fix
Browse files Browse the repository at this point in the history
Dummy cube latlon fix
  • Loading branch information
lewisblake committed Jun 24, 2022
2 parents da26731 + 4a9da90 commit 5ce043c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ jobs:
with:
path: |
~/.cache/pip
~/.cache/srtm
.tox
key: ${{ runner.os }}-tox-${{ matrix.python-version }}-${{ hashFiles('setup.cfg') }}
key: ${{ runner.os }}-tox-${{ matrix.python-version }}-${{ hashFiles('setup.cfg', 'pyproject.toml') }}
- name: Install Tox
run: |
python -m pip install --upgrade pip
Expand Down
39 changes: 26 additions & 13 deletions pyaerocom/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
General helper methods for the pyaerocom library.
"""
from __future__ import annotations

import logging
import math as ma
Expand Down Expand Up @@ -204,7 +205,12 @@ def tuple_list_to_lists(tuple_list):
return list(map(list, zip(tuple_list)))


def make_dummy_cube_latlon(lat_res_deg=2, lon_res_deg=3, lat_range=None, lon_range=None):
def make_dummy_cube_latlon(
lat_res_deg: float = 2,
lon_res_deg: float = 3,
lat_range: list[float] | tuple[float, float] = (-90, 90),
lon_range: list[float] | tuple[float, float] = (-180, 180),
):
"""Make an empty Cube with given latitude and longitude resolution
Dimensions will be lat, lon
Expand All @@ -227,13 +233,16 @@ def make_dummy_cube_latlon(lat_res_deg=2, lon_res_deg=3, lat_range=None, lon_ran
Cube
dummy cube in input resolution
"""
if lat_range is None:
lat_range = (-90, 90)
if lon_range is None:
lon_range = (-180, 180)

lons = np.arange(lon_range[0] + lon_res_deg / 2, lon_range[1] + lon_res_deg / 2, lon_res_deg)
lats = np.arange(lat_range[0] + lat_res_deg / 2, lat_range[1] + lat_res_deg / 2, lat_res_deg)
# Accept lists for lat_range and lon_range, but make sure correct length
assert len(lat_range) == len(lon_range) == 2

lons = np.arange(
lon_range[0] + (lon_res_deg / 2), lon_range[1] + (lon_res_deg / 2), lon_res_deg
)
lats = np.arange(
lat_range[0] + (lat_res_deg / 2), lat_range[1] + (lat_res_deg / 2), lat_res_deg
)

lon_circ = check_coord_circular(lons, modulus=360)
latdim = iris.coords.DimCoord(
Expand Down Expand Up @@ -1710,14 +1719,18 @@ def make_dummy_cube(
days_since_start = np.arange(len(times))
unit = get_variable(var_name).units

lat_range = (-180, 180)
lon_range = (-90, 90)
lat_res_deg = 90
lon_res_deg = 45
lat_range = (-90, 90)
lon_range = (-180, 180)
lat_res_deg = 45
lon_res_deg = 90
time_unit = Unit(startstr, calendar="gregorian")

lons = np.arange(lon_range[0] + lon_res_deg / 2, lon_range[1] + lon_res_deg / 2, lon_res_deg)
lats = np.arange(lat_range[0] + lat_res_deg / 2, lat_range[1] + lat_res_deg / 2, lat_res_deg)
lons = np.arange(
lon_range[0] + (lon_res_deg / 2), lon_range[1] + (lon_res_deg / 2), lon_res_deg
)
lats = np.arange(
lat_range[0] + (lat_res_deg / 2), lat_range[1] + (lat_res_deg / 2), lat_res_deg
)

latdim = iris.coords.DimCoord(
lats,
Expand Down
2 changes: 1 addition & 1 deletion pyaerocom/mathutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def _nanmean_and_std(data):


@ignore_warnings(
RuntimeWarning, "An input array is constant", "invalid value encountered in true_divide"
RuntimeWarning, "An input array is constant", "invalid value encountered in .*divide"
)
def calc_statistics(data, ref_data, lowlim=None, highlim=None, min_num_valid=1, weights=None):
"""Calc statistical properties from two data arrays
Expand Down
5 changes: 5 additions & 0 deletions tests/test_geodesy.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def test_is_within_radius_km():
assert geodesy.is_within_radius_km(0, 15, 0, 16, 1000, 111.2)


@pytest.mark.xfail(
reason="fails in CI when geonum can not download 'srtm' data",
raises=FileNotFoundError,
strict=False,
)
def test_srtm_altitude():
assert geodesy.get_topo_altitude(TEST_LAT, TEST_LON) == pytest.approx(207)

Expand Down
16 changes: 15 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ def test_extract_latlon_dataarray():
lon = [1, 15, 18]
subset = helpers.extract_latlon_dataarray(data, lat, lon, check_domain=True)
assert isinstance(subset, xr.DataArray)
assert len(subset.lat) == len(lat) - 1 and len(subset.lon) == len(lon) - 1
assert len(subset.lat) == len(lat) - 1
assert len(subset.lon) == len(lon) - 1


def test_extract_latlon_dataarray_no_matches():
Expand Down Expand Up @@ -250,3 +251,16 @@ def test_extract_latlon_dataarray_no_matches_error(lat, lon):
def test_seconds_in_periods(date, ts_type, days):
seconds = timedelta(days=days) / timedelta(seconds=1)
assert helpers.seconds_in_periods(np.datetime64(date), ts_type) == seconds


def test_make_dummy_cube():
# make a dummy cube of an arbitrary variable name over one year
cube = helpers.make_dummy_cube("concpm10", start_yr=2020, stop_yr=2021)
data = xr.DataArray.from_iris(cube)
# First coordinate does not exist in the dataarray.
lat = [90, -67.5, 22.2]
lon = [180, -135.0, 45.0]
subset = helpers.extract_latlon_dataarray(data, lat, lon, check_domain=True)
assert isinstance(subset, xr.DataArray)
assert len(subset.lat) == len(lat) - 1
assert len(subset.lon) == len(lon) - 1
2 changes: 1 addition & 1 deletion tests/test_ungriddeddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def test_check_unit(data_scat_jungfraujoch):
data_scat_jungfraujoch.check_unit("sc550aer", unit="m-1")


@pytest.mark.filterwarnings("ignore:invalid value encountered in true_divide:RuntimeWarning")
@pytest.mark.filterwarnings("ignore:invalid value encountered in .*divide:RuntimeWarning")
def test_check_convert_var_units(data_scat_jungfraujoch):

out = data_scat_jungfraujoch.check_convert_var_units("sc550aer", "m-1", inplace=False)
Expand Down

0 comments on commit 5ce043c

Please sign in to comment.