|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
3 | | -import os |
4 | | - |
5 | | -import numpy as np |
6 | 3 | import pytest |
7 | 4 | from numpy.testing import assert_array_almost_equal |
| 5 | +from pysteps.tests.helpers import smart_assert, get_precipitation_fields |
8 | 6 |
|
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 | +) |
10 | 16 |
|
11 | | -pytest.importorskip("pygrib") |
| 17 | +print(precip_dataset) |
| 18 | +precip_var = precip_dataset.attrs["precip_var"] |
| 19 | +precip_dataarray = precip_dataset[precip_var] |
12 | 20 |
|
13 | 21 |
|
14 | 22 | def test_io_import_mrms_grib(): |
15 | 23 | """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, |
20 | 71 | ) |
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) |
65 | 72 |
|
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 | + ) |
67 | 88 |
|
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, |
71 | 97 | ) |
72 | 98 |
|
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 | + ) |
76 | 110 |
|
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 | + ) |
80 | 126 |
|
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