Skip to content

Commit

Permalink
Merge pull request #29 from wpreimes/fix_cmdline
Browse files Browse the repository at this point in the history
Add missing arg to cmd parser to fix reshuffling land points
  • Loading branch information
wpreimes authored Feb 14, 2022
2 parents 574a227 + 1db0197 commit 3cf3757
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 43 deletions.
1 change: 1 addition & 0 deletions src/ecmwf_models/era5/reshuffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ def main(args):
variables=args.variables,
bbox=args.bbox,
h_steps=args.h_steps,
land_points=args.land_points,
imgbuffer=args.imgbuffer,
)

Expand Down
35 changes: 23 additions & 12 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,49 @@
Tests for grid generation
"""

from ecmwf_models.grid import ERA_RegularImgGrid, get_grid_resolution, \
ERA_IrregularImgGrid, ERA5_RegularImgLandGrid
from ecmwf_models.grid import (
ERA_RegularImgGrid,
get_grid_resolution,
ERA_IrregularImgGrid,
ERA5_RegularImgLandGrid,
)
import numpy as np


def test_ERA_regular_grid():
reg_grid = ERA_RegularImgGrid(0.3, 0.3)
assert np.unique(reg_grid.activearrlat).size == 601
assert np.unique(reg_grid.activearrlon).size == 1200
assert get_grid_resolution(reg_grid.activearrlat,
reg_grid.activearrlon) == (0.3, 0.3)
assert get_grid_resolution(
reg_grid.activearrlat, reg_grid.activearrlon
) == (
0.3,
0.3,
)


def test_ERA_irregular_grid():
# we test this with a regular grid, because it's easier
lon = np.arange(0, 360 - 1. / 2, 1.)
lat = np.arange(90, -1 * 90 - 1. / 2, -1 * 1.)
lon = np.arange(0, 360 - 1.0 / 2, 1.0)
lat = np.arange(90, -1 * 90 - 1.0 / 2, -1 * 1.0)
lons, lats = np.meshgrid(lon, lat)

grid = ERA_IrregularImgGrid(lons, lats)

assert grid == ERA_RegularImgGrid(1.,1.)
assert grid == ERA_RegularImgGrid(1.0, 1.0)


def test_ERA5_landgrid_025():
grid = ERA5_RegularImgLandGrid(0.25, 0.25) # 0.25*0.25
grid = ERA5_RegularImgLandGrid(0.25, 0.25) # 0.25*0.25
assert grid.get_grid_points()[0].size == 244450
assert grid.find_nearest_gpi(16.375, 48.125)[0] == 240545
assert grid.find_nearest_gpi(16.25, 48.25) == (240545, 0.0)
assert grid.gpi2cell(240545) == 1431


def test_ERA5_landgrid_01():
grid = ERA5_RegularImgLandGrid(0.1, 0.1) # 0.1*0.1
grid = ERA5_RegularImgLandGrid(0.1, 0.1) # 0.1*0.1
assert grid.get_grid_points()[0].size == 1544191
assert grid.find_nearest_gpi(16.375, 48.125)[0] == 1508564
assert grid.find_nearest_gpi(16.4, 48.1) == (1508564, 0.0)
np.testing.assert_almost_equal(grid.gpi2lonlat(1508564)[0], 16.4)
np.testing.assert_almost_equal(grid.gpi2lonlat(1508564)[1], 48.1)
assert grid.gpi2cell(1508564) == 1431
assert grid.gpi2cell(1508564) == 1431
140 changes: 109 additions & 31 deletions tests/tests_era5/test_era5_reshuffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,68 +21,146 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

'''
"""
Test module for image to time series conversion.
'''
"""

import os
import glob
import tempfile
import numpy as np
import numpy.testing as nptest
from datetime import datetime

from ecmwf_models.era5.reshuffle import main
from ecmwf_models import ERATs
from ecmwf_models.era5.reshuffle import parse_args


def test_parse_args():

args = parse_args(
[
"/in",
"/out",
"2000-01-01",
"2010-12-31",
"swvl1",
"swvl2",
"--land_points",
"True",
"--imgbuffer",
"1000",
"--bbox",
"12",
"46",
"17",
"50",
]
)

assert isinstance(args.dataset_root, str) and args.dataset_root == "/in"
assert (
isinstance(args.timeseries_root, str)
and args.timeseries_root == "/out"
)
assert isinstance(args.start, datetime) and args.start == datetime(
2000, 1, 1
)
assert isinstance(args.end, datetime) and args.end == datetime(
2010, 12, 31
)
assert isinstance(args.variables, list) and len(args.variables) == 2
assert isinstance(args.land_points, bool) and args.land_points is True
assert isinstance(args.imgbuffer, int) and args.imgbuffer == 1000
assert (
isinstance(args.bbox, list)
and len(args.bbox) == 4
and all([isinstance(a, float) for a in args.bbox])
)


def test_ERA5_reshuffle_nc():
# test reshuffling era5 netcdf images to time series

inpath = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..',
"ecmwf_models-test-data", "ERA5", "netcdf")
inpath = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"..",
"ecmwf_models-test-data",
"ERA5",
"netcdf",
)

startdate = '2010-01-01'
enddate = '2010-01-01'
startdate = "2010-01-01"
enddate = "2010-01-01"
parameters = ["swvl1", "swvl2"]
h_steps = ['--h_steps', '0', '12']
landpoints = ['--land_points', 'True']
bbox = ['--bbox', "12", '46', '17', '50']
h_steps = ["--h_steps", "0", "12"]
landpoints = ["--land_points", "True"]
bbox = ["--bbox", "12", "46", "17", "50"]

with tempfile.TemporaryDirectory() as ts_path:
args = [inpath, ts_path, startdate, enddate] \
+ parameters + h_steps + landpoints + bbox
args = (
[inpath, ts_path, startdate, enddate]
+ parameters
+ h_steps
+ landpoints
+ bbox
)
main(args)
assert len(glob.glob(os.path.join(ts_path, "*.nc"))) == 5 # less files because only land points and bbox
ds = ERATs(ts_path, ioclass_kws={'read_bulk': True})
assert (
len(glob.glob(os.path.join(ts_path, "*.nc"))) == 5
) # less files because only land points and bbox
ds = ERATs(ts_path, ioclass_kws={"read_bulk": True})
ts = ds.read(15, 48)
ds.close()
swvl1_values_should = np.array([0.402825, 0.390983], dtype=np.float32)
nptest.assert_allclose(ts['swvl1'].values, swvl1_values_should, rtol=1e-5)
swvl2_values_should = np.array([0.390512, 0.390981], dtype=np.float32)
nptest.assert_allclose(ts['swvl2'].values, swvl2_values_should, rtol=1e-5)
swvl1_values_should = np.array([0.402825, 0.390983], dtype=np.float32)
nptest.assert_allclose(
ts["swvl1"].values, swvl1_values_should, rtol=1e-5
)
swvl2_values_should = np.array([0.390512, 0.390981], dtype=np.float32)
nptest.assert_allclose(
ts["swvl2"].values, swvl2_values_should, rtol=1e-5
)


def test_ERA5_reshuffle_grb():
# test reshuffling era5 netcdf images to time series

inpath = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..',
"ecmwf_models-test-data", "ERA5", "netcdf")
startdate = '2010-01-01'
enddate = '2010-01-01'
inpath = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"..",
"ecmwf_models-test-data",
"ERA5",
"netcdf",
)
startdate = "2010-01-01"
enddate = "2010-01-01"
parameters = ["swvl1", "swvl2"]
h_steps = ['--h_steps', '0', '12']
landpoints = ['--land_points', 'False']
bbox = ['--bbox', "12", '46', '17', '50']
h_steps = ["--h_steps", "0", "12"]
landpoints = ["--land_points", "False"]
bbox = ["--bbox", "12", "46", "17", "50"]

with tempfile.TemporaryDirectory() as ts_path:

args = [inpath, ts_path, startdate, enddate] + parameters + \
h_steps + landpoints + bbox
args = (
[inpath, ts_path, startdate, enddate]
+ parameters
+ h_steps
+ landpoints
+ bbox
)

main(args)

assert len(glob.glob(os.path.join(ts_path, "*.nc"))) == 5
ds = ERATs(ts_path, ioclass_kws={'read_bulk': True})
ds = ERATs(ts_path, ioclass_kws={"read_bulk": True})
ts = ds.read(15, 48)
ds.close()
swvl1_values_should = np.array([0.402824, 0.390979], dtype=np.float32)
nptest.assert_allclose(ts['swvl1'].values, swvl1_values_should, rtol=1e-5)
swvl2_values_should = np.array([0.390514, 0.390980], dtype=np.float32)
nptest.assert_allclose(ts['swvl2'].values, swvl2_values_should, rtol=1e-5)
swvl1_values_should = np.array([0.402824, 0.390979], dtype=np.float32)
nptest.assert_allclose(
ts["swvl1"].values, swvl1_values_should, rtol=1e-5
)
swvl2_values_should = np.array([0.390514, 0.390980], dtype=np.float32)
nptest.assert_allclose(
ts["swvl2"].values, swvl2_values_should, rtol=1e-5
)

0 comments on commit 3cf3757

Please sign in to comment.