Skip to content

Commit 7771632

Browse files
authored
All specific IO tests are done (#519)
* All specific IO tests are done * Fixed last IO test?
1 parent 2330b01 commit 7771632

File tree

10 files changed

+168
-168
lines changed

10 files changed

+168
-168
lines changed

environment_dev.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ dependencies:
2121
- dask
2222
- pyfftw
2323
- h5py
24-
- PyWavelets
2524
- pygrib
2625
- black
2726
- pytest-cov
@@ -32,3 +31,5 @@ dependencies:
3231
- pandas
3332
- rasterio
3433
- xarray
34+
- geotiff
35+
- cookiecutter

pysteps/io/importers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2023,7 +2023,6 @@ def _read_hdf5_cont(f, d):
20232023
}
20242024

20252025
else:
2026-
20272026
# Save h5py.Dataset by group name
20282027
d[key] = np.array(value)
20292028

pysteps/pystepsrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"path_fmt": "%Y%m%d",
5252
"fn_pattern": "%Y%m%d%H%M_FINUTM",
5353
"fn_ext": "tif",
54-
"importer": "geotiff",
54+
"importer": "fmi_geotiff",
5555
"timestep": 5,
5656
"importer_kwargs": {}
5757
},

pysteps/tests/test_io_bom_rf3.py

Lines changed: 38 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,83 +5,54 @@
55
import pytest
66

77
import pysteps
8-
from pysteps.tests.helpers import smart_assert
8+
from pysteps.tests.helpers import smart_assert, get_precipitation_fields
9+
10+
precip_dataset = get_precipitation_fields(
11+
num_prev_files=0,
12+
num_next_files=0,
13+
return_raw=True,
14+
metadata=True,
15+
source="bom",
16+
log_transform=False,
17+
)
18+
19+
precip_var = precip_dataset.attrs["precip_var"]
20+
precip_dataarray = precip_dataset[precip_var]
21+
22+
23+
def test_io_import_bom_shape():
24+
"""Test the shape of the read file."""
25+
assert precip_dataarray.shape == (1, 512, 512)
926

10-
netCDF4 = pytest.importorskip("netCDF4")
1127

1228
# Test import_bom_rf3 function
13-
expected_proj1 = (
29+
expected_proj = (
1430
"+proj=aea +lon_0=144.752 +lat_0=-37.852 " "+lat_1=-18.000 +lat_2=-36.000"
1531
)
1632

1733
test_metadata_bom = [
18-
("transform", None, None),
19-
("zerovalue", 0.0, 0.1),
20-
("projection", expected_proj1, None),
21-
("unit", "mm", None),
22-
("accutime", 6, 0.1),
23-
("x1", -128000.0, 0.1),
24-
("x2", 127500.0, 0.1),
25-
("y1", -127500.0, 0.1),
26-
("y2", 128000.0, 0.1),
27-
("xpixelsize", 500.0, 0.1),
28-
("ypixelsize", 500.0, 0.1),
29-
("cartesian_unit", "m", None),
30-
("yorigin", "upper", None),
31-
("institution", "Commonwealth of Australia, Bureau of Meteorology", None),
34+
(precip_dataset.attrs["projection"], expected_proj, None),
35+
(
36+
precip_dataset.attrs["institution"],
37+
"Commonwealth of Australia, Bureau of Meteorology",
38+
None,
39+
),
40+
(precip_dataset.x.isel(x=0).values, -127750.0, 1e-5),
41+
(precip_dataset.y.isel(y=0).values, -127250.0, 1e-5),
42+
(precip_dataset.x.isel(x=-1).values, 127250.0, 1e-5),
43+
(precip_dataset.y.isel(y=-1).values, 127750.0, 1e-5),
44+
(precip_dataset.x.attrs["stepsize"], 500.0, 1e-4),
45+
(precip_dataset.y.attrs["stepsize"], 500.0, 1e-4),
46+
(precip_dataset.x.attrs["units"], "m", None),
47+
(precip_dataset.y.attrs["units"], "m", None),
48+
(precip_dataarray.attrs["accutime"], 6, 1e-4),
49+
(precip_dataarray.attrs["transform"], None, None),
50+
(precip_dataarray.attrs["zerovalue"], 0.0, 1e-4),
51+
(precip_dataarray.attrs["units"], "mm", None),
3252
]
3353

3454

3555
@pytest.mark.parametrize("variable, expected, tolerance", test_metadata_bom)
3656
def test_io_import_bom_rf3_metadata(variable, expected, tolerance):
3757
"""Test the importer Bom RF3."""
38-
root_path = pysteps.rcparams.data_sources["bom"]["root_path"]
39-
rel_path = os.path.join("prcp-cscn", "2", "2018", "06", "16")
40-
filename = os.path.join(root_path, rel_path, "2_20180616_100000.prcp-cscn.nc")
41-
precip, _, metadata = pysteps.io.import_bom_rf3(filename)
42-
smart_assert(metadata[variable], expected, tolerance)
43-
assert precip.shape == (512, 512)
44-
45-
46-
# Test _import_bom_rf3_data function
47-
def test_io_import_bom_rf3_shape():
48-
"""Test the importer Bom RF3."""
49-
root_path = pysteps.rcparams.data_sources["bom"]["root_path"]
50-
rel_path = os.path.join("prcp-cscn", "2", "2018", "06", "16")
51-
filename = os.path.join(root_path, rel_path, "2_20180616_100000.prcp-cscn.nc")
52-
precip, _ = pysteps.io.importers._import_bom_rf3_data(filename)
53-
assert precip.shape == (512, 512)
54-
55-
56-
# Test _import_bom_rf3_geodata function
57-
expected_proj2 = (
58-
"+proj=aea +lon_0=144.752 +lat_0=-37.852 " "+lat_1=-18.000 +lat_2=-36.000"
59-
)
60-
# test_geodata: list of (variable,expected,tolerance) tuples
61-
test_geodata_bom = [
62-
("projection", expected_proj2, None),
63-
("unit", "mm", None),
64-
("accutime", 6, 0.1),
65-
("x1", -128000.0, 0.1),
66-
("x2", 127500.0, 0.1),
67-
("y1", -127500.0, 0.1),
68-
("y2", 128000.0, 0.1),
69-
("xpixelsize", 500.0, 0.1),
70-
("ypixelsize", 500.0, 0.1),
71-
("cartesian_unit", "m", None),
72-
("yorigin", "upper", None),
73-
("institution", "Commonwealth of Australia, Bureau of Meteorology", None),
74-
]
75-
76-
77-
@pytest.mark.parametrize("variable, expected, tolerance", test_geodata_bom)
78-
def test_io_import_bom_rf3_geodata(variable, expected, tolerance):
79-
"""Test the importer Bom RF3."""
80-
root_path = pysteps.rcparams.data_sources["bom"]["root_path"]
81-
rel_path = os.path.join("prcp-cscn", "2", "2018", "06", "16")
82-
filename = os.path.join(root_path, rel_path, "2_20180616_100000.prcp-cscn.nc")
83-
ds_rainfall = netCDF4.Dataset(filename)
84-
geodata = pysteps.io.importers._import_bom_rf3_geodata(ds_rainfall)
85-
smart_assert(geodata[variable], expected, tolerance)
86-
87-
ds_rainfall.close()
58+
smart_assert(variable, expected, tolerance)

pysteps/tests/test_io_dwd_hdf5.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
metadata=True,
1313
source="dwd",
1414
log_transform=False,
15-
importer_kwargs=dict(qty="RATE"),
1615
)
1716

1817
precip_var = precip_dataset.attrs["precip_var"]
@@ -54,7 +53,7 @@ def test_io_import_dwd_hdf5_ry_shape():
5453
None,
5554
),
5655
(precip_dataarray.attrs["accutime"], 5.0, 1e-10),
57-
(precip_dataset.time.attrs["stepsize"], 5.0, 1e-10),
56+
(precip_dataset.time.attrs["stepsize"], 300, 1e-10),
5857
(precip_dataarray.attrs["units"], "mm/h", None),
5958
(precip_dataarray.attrs["transform"], None, None),
6059
(precip_dataarray.attrs["zerovalue"], 0.0, 1e-6),

pysteps/tests/test_io_knmi_hdf5.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def test_io_import_knmi_hdf5_shape():
3939
(precip_dataset.x.attrs["stepsize"], 1000.0, 1e-10),
4040
(precip_dataset.y.attrs["stepsize"], 1000.0, 1e-10),
4141
(precip_dataarray.attrs["accutime"], 5.0, 1e-10),
42-
(precip_dataset.time.attrs["stepsize"], 5.0, 1e-10),
43-
(precip_dataarray.attrs["units"], "mm/h", None),
42+
(precip_dataset.time.attrs["stepsize"], 300, 1e-10),
43+
(precip_dataarray.attrs["units"], "mm", None),
4444
(precip_dataset.x.attrs["units"], "m", None),
4545
(precip_dataset.y.attrs["units"], "m", None),
4646
(

pysteps/tests/test_io_mch_gif.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_io_import_mch_gif_shape():
3636
(precip_dataset.attrs["projection"], expected_proj, None),
3737
(precip_dataset.attrs["institution"], "MeteoSwiss", None),
3838
(precip_dataarray.attrs["accutime"], 5.0, 1e-10),
39-
(precip_dataset.time.attrs["stepsize"], 5.0, 1e-10),
39+
(precip_dataset.time.attrs["stepsize"], 300, 1e-10),
4040
(precip_dataarray.attrs["units"], "mm", None),
4141
(precip_dataarray.attrs["transform"], None, None),
4242
(precip_dataarray.attrs["zerovalue"], 0.0, 1e-6),

pysteps/tests/test_io_mrms_grib.py

Lines changed: 112 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,129 @@
11
# -*- coding: utf-8 -*-
22

3-
import os
4-
5-
import numpy as np
63
import pytest
74
from numpy.testing import assert_array_almost_equal
5+
from pysteps.tests.helpers import smart_assert, get_precipitation_fields
86

9-
import pysteps
7+
precip_dataset = get_precipitation_fields(
8+
num_prev_files=0,
9+
num_next_files=0,
10+
return_raw=True,
11+
metadata=True,
12+
source="mrms",
13+
log_transform=False,
14+
window_size=1,
15+
)
1016

11-
pytest.importorskip("pygrib")
17+
print(precip_dataset)
18+
precip_var = precip_dataset.attrs["precip_var"]
19+
precip_dataarray = precip_dataset[precip_var]
1220

1321

1422
def test_io_import_mrms_grib():
1523
"""Test the importer for NSSL data."""
16-
17-
root_path = pysteps.rcparams.data_sources["mrms"]["root_path"]
18-
filename = os.path.join(
19-
root_path, "2019/06/10/", "PrecipRate_00.00_20190610-000000.grib2"
24+
assert precip_dataarray.shape == (1, 3500, 7000)
25+
assert precip_dataarray.dtype == "single"
26+
27+
28+
expected_proj = "+proj=longlat +ellps=IAU76"
29+
30+
# list of (variable,expected,tolerance) tuples
31+
test_attrs = [
32+
(precip_dataset.attrs["projection"], expected_proj, None),
33+
(
34+
precip_dataset.attrs["institution"],
35+
"NOAA National Severe Storms Laboratory",
36+
None,
37+
),
38+
(precip_dataarray.attrs["units"], "mm/h", None),
39+
(precip_dataarray.attrs["transform"], None, None),
40+
(precip_dataarray.attrs["zerovalue"], 0.0, 1e-6),
41+
(precip_dataarray.attrs["threshold"], 0.1, 1e-10),
42+
(precip_dataset.x.isel(x=0).values, -129.995, 1e-10),
43+
(precip_dataset.y.isel(y=0).values, 20.005001, 1e-10),
44+
(precip_dataset.x.isel(x=-1).values, -60.005002, 1e-10),
45+
(precip_dataset.y.isel(y=-1).values, 54.995, 1e-10),
46+
(precip_dataset.x.attrs["stepsize"], 0.01, 1e-4),
47+
(precip_dataset.y.attrs["stepsize"], 0.01, 1e-4),
48+
(precip_dataset.x.attrs["units"], "degrees", None),
49+
(precip_dataset.y.attrs["units"], "degrees", None),
50+
]
51+
52+
53+
@pytest.mark.parametrize("variable, expected, tolerance", test_attrs)
54+
def test_io_import_mrms_grib_dataset_attrs(variable, expected, tolerance):
55+
"""Test the importer MRMS_GRIB."""
56+
smart_assert(variable, expected, tolerance)
57+
58+
59+
def test_io_import_mrms_grib_dataset_extent():
60+
"""Test the importer MRMS_GRIB."""
61+
62+
precip_dataset_smaller = get_precipitation_fields(
63+
num_prev_files=0,
64+
num_next_files=0,
65+
return_raw=True,
66+
metadata=True,
67+
source="mrms",
68+
log_transform=False,
69+
extent=(230, 300, 20, 55),
70+
window_size=1,
2071
)
21-
precip, _, metadata = pysteps.io.import_mrms_grib(filename, fillna=0, window_size=1)
22-
23-
assert precip.shape == (3500, 7000)
24-
assert precip.dtype == "single"
25-
26-
expected_metadata = {
27-
"institution": "NOAA National Severe Storms Laboratory",
28-
"xpixelsize": 0.01,
29-
"ypixelsize": 0.01,
30-
"unit": "mm/h",
31-
"transform": None,
32-
"zerovalue": 0,
33-
"projection": "+proj=longlat +ellps=IAU76",
34-
"yorigin": "upper",
35-
"threshold": 0.1,
36-
"x1": -129.99999999999997,
37-
"x2": -60.00000199999991,
38-
"y1": 20.000001,
39-
"y2": 55.00000000000001,
40-
"cartesian_unit": "degrees",
41-
}
42-
43-
for key, value in expected_metadata.items():
44-
if isinstance(value, float):
45-
assert_array_almost_equal(metadata[key], expected_metadata[key])
46-
else:
47-
assert metadata[key] == expected_metadata[key]
48-
49-
x = np.arange(metadata["x1"], metadata["x2"], metadata["xpixelsize"])
50-
y = np.arange(metadata["y1"], metadata["y2"], metadata["ypixelsize"])
51-
52-
assert y.size == precip.shape[0]
53-
assert x.size == precip.shape[1]
54-
55-
# The full latitude range is (20.005, 54.995)
56-
# The full longitude range is (230.005, 299.995)
57-
58-
# Test that if the bounding box is larger than the domain, all the points are returned.
59-
precip2, _, _ = pysteps.io.import_mrms_grib(
60-
filename, fillna=0, extent=(220, 300, 20, 55), window_size=1
61-
)
62-
assert precip2.shape == (3500, 7000)
63-
64-
assert_array_almost_equal(precip, precip2)
6572

66-
del precip2
73+
precip_var_smaller = precip_dataset_smaller.attrs["precip_var"]
74+
precip_dataarray_smaller = precip_dataset_smaller[precip_var_smaller]
75+
smart_assert(precip_dataarray_smaller.shape, (1, 3500, 7000), None)
76+
assert_array_almost_equal(precip_dataarray.values, precip_dataarray_smaller.values)
77+
78+
precip_dataset_even_smaller = get_precipitation_fields(
79+
num_prev_files=0,
80+
num_next_files=0,
81+
return_raw=True,
82+
metadata=True,
83+
source="mrms",
84+
log_transform=False,
85+
extent=(250, 260, 30, 35),
86+
window_size=1,
87+
)
6788

68-
# Test that a portion of the domain is returned correctly
69-
precip3, _, _ = pysteps.io.import_mrms_grib(
70-
filename, fillna=0, extent=(250, 260, 30, 35), window_size=1
89+
precip_var_even_smaller = precip_dataset_even_smaller.attrs["precip_var"]
90+
precip_dataarray_even_smaller = precip_dataset_even_smaller[precip_var_even_smaller]
91+
smart_assert(precip_dataarray_even_smaller.shape, (1, 500, 1000), None)
92+
# XR: we had to change the selection of the original field since these is a flip happening in the way the data is read in.
93+
# XR: We had two ways to solve this: precip_dataarray[:,::-1, :][:, 2000:2500, 2000:3000][:,::-1, :] or switch the 2000:2500 to
94+
assert_array_almost_equal(
95+
precip_dataarray.values[:, 1000:1500, 2000:3000],
96+
precip_dataarray_even_smaller.values,
7197
)
7298

73-
assert precip3.shape == (500, 1000)
74-
assert_array_almost_equal(precip3, precip[2000:2500, 2000:3000])
75-
del precip3
99+
precip_dataset_double = get_precipitation_fields(
100+
num_prev_files=0,
101+
num_next_files=0,
102+
return_raw=True,
103+
metadata=True,
104+
source="mrms",
105+
log_transform=False,
106+
extent=(250, 260, 30, 35),
107+
window_size=1,
108+
dtype="double",
109+
)
76110

77-
precip4, _, _ = pysteps.io.import_mrms_grib(filename, dtype="double", fillna=0)
78-
assert precip4.dtype == "double"
79-
del precip4
111+
precip_var_double = precip_dataset_double.attrs["precip_var"]
112+
precip_dataarray_double = precip_dataset_double[precip_var_double]
113+
smart_assert(precip_dataarray_double.dtype, "double", None)
114+
115+
precip_dataset_single = get_precipitation_fields(
116+
num_prev_files=0,
117+
num_next_files=0,
118+
return_raw=True,
119+
metadata=True,
120+
source="mrms",
121+
log_transform=False,
122+
extent=(250, 260, 30, 35),
123+
window_size=1,
124+
dtype="single",
125+
)
80126

81-
precip5, _, _ = pysteps.io.import_mrms_grib(filename, dtype="single", fillna=0)
82-
assert precip5.dtype == "single"
83-
del precip5
127+
precip_var_single = precip_dataset_single.attrs["precip_var"]
128+
precip_dataarray_single = precip_dataset_single[precip_var_single]
129+
smart_assert(precip_dataarray_single.dtype, "single", None)

0 commit comments

Comments
 (0)