|
| 1 | +""" |
| 2 | +Test suite for the validate module |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +import xarray as xr |
| 7 | +import numpy as np |
| 8 | +from pathlib import Path |
| 9 | +from regional_mom6.validate import ( |
| 10 | + _check_fill_value, |
| 11 | + _check_coordinates, |
| 12 | + _check_required_dimensions, |
| 13 | + ends_with_3_digits, |
| 14 | + validate_obc_file, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +# _check_fill_value tests |
| 19 | +def test_check_fill_value_valid(caplog): |
| 20 | + """DataArray with valid fill value logs no warnings""" |
| 21 | + da = xr.DataArray( |
| 22 | + [1, 2, 3], dims="x", name="temperature", attrs={"_FillValue": -999.0} |
| 23 | + ) |
| 24 | + _check_fill_value(da) |
| 25 | + assert "FillValue" not in caplog.text |
| 26 | + |
| 27 | + |
| 28 | +def test_check_fill_value_missing(caplog): |
| 29 | + """DataArray without _FillValue attribute logs warning""" |
| 30 | + da = xr.DataArray([1, 2, 3], dims="x", name="temperature") |
| 31 | + _check_fill_value(da) |
| 32 | + assert "FillValue" in caplog.text |
| 33 | + |
| 34 | + |
| 35 | +def test_check_fill_value_nan(caplog): |
| 36 | + """DataArray with NaN fill value logs warning""" |
| 37 | + da = xr.DataArray( |
| 38 | + [1, 2, 3], dims="x", name="temperature", attrs={"_FillValue": np.nan} |
| 39 | + ) |
| 40 | + _check_fill_value(da) |
| 41 | + assert "NaN" in caplog.text |
| 42 | + |
| 43 | + |
| 44 | +# _check_coordinates tests |
| 45 | +def test_check_coordinates_valid(caplog): |
| 46 | + """DataArray with valid coordinates attribute logs no warnings""" |
| 47 | + ds = xr.Dataset( |
| 48 | + { |
| 49 | + "temperature": (["x", "y"], np.random.rand(3, 4)), |
| 50 | + "lon": (["x", "y"], np.random.rand(3, 4)), |
| 51 | + "lat": (["x", "y"], np.random.rand(3, 4)), |
| 52 | + } |
| 53 | + ) |
| 54 | + ds["temperature"].attrs["coordinates"] = "lon lat" |
| 55 | + _check_coordinates(ds, "temperature") |
| 56 | + assert "coordinate" not in caplog.text.lower() |
| 57 | + |
| 58 | + |
| 59 | +def test_check_coordinates_missing_attribute(caplog): |
| 60 | + """DataArray without coordinates attribute logs warning""" |
| 61 | + ds = xr.Dataset({"temperature": (["x", "y"], np.random.rand(3, 4))}) |
| 62 | + _check_coordinates(ds, "temperature") |
| 63 | + assert "coordinates" in caplog.text.lower() |
| 64 | + |
| 65 | + |
| 66 | +def test_check_coordinates_missing_in_dataset(caplog): |
| 67 | + """Missing coordinate variable logs warning""" |
| 68 | + ds = xr.Dataset({"temperature": (["x", "y"], np.random.rand(3, 4))}) |
| 69 | + ds["temperature"].attrs["coordinates"] = "lon lat" |
| 70 | + _check_coordinates(ds, "temperature") |
| 71 | + assert "does not exist" in caplog.text |
| 72 | + |
| 73 | + |
| 74 | +# _check_required_dimensions tests |
| 75 | +def test_check_required_dimensions_valid_4d(caplog): |
| 76 | + """4D variable passes check when surface=False""" |
| 77 | + da = xr.DataArray( |
| 78 | + np.random.rand(2, 3, 4, 5), dims=["time", "z", "x", "y"], name="temperature" |
| 79 | + ) |
| 80 | + _check_required_dimensions(da, surface=False) |
| 81 | + assert "dimension" not in caplog.text.lower() |
| 82 | + |
| 83 | + |
| 84 | +def test_check_required_dimensions_invalid_3d_for_4d(caplog): |
| 85 | + """3D variable fails check when surface=False""" |
| 86 | + da = xr.DataArray(np.random.rand(3, 4, 5), dims=["x", "y", "z"], name="temperature") |
| 87 | + _check_required_dimensions(da, surface=False) |
| 88 | + assert "dimension" in caplog.text.lower() |
| 89 | + |
| 90 | + |
| 91 | +def test_check_required_dimensions_valid_3d_surface(caplog): |
| 92 | + """3D variable passes check when surface=True""" |
| 93 | + da = xr.DataArray(np.random.rand(2, 3, 4), dims=["time", "x", "y"], name="eta") |
| 94 | + _check_required_dimensions(da, surface=True) |
| 95 | + assert "dimension" not in caplog.text.lower() |
| 96 | + |
| 97 | + |
| 98 | +def test_check_required_dimensions_invalid_4d_for_surface(caplog): |
| 99 | + """4D variable fails check when surface=True""" |
| 100 | + da = xr.DataArray( |
| 101 | + np.random.rand(2, 3, 4, 5), dims=["time", "z", "x", "y"], name="eta" |
| 102 | + ) |
| 103 | + _check_required_dimensions(da, surface=True) |
| 104 | + assert "dimension" in caplog.text.lower() |
| 105 | + |
| 106 | + |
| 107 | +# ends_with_3_digits tests |
| 108 | +def test_ends_with_3_digits_valid_cases(): |
| 109 | + """String ending with 3 digits returns True""" |
| 110 | + assert ends_with_3_digits("temp_001") is True |
| 111 | + assert ends_with_3_digits("var_999") is True |
| 112 | + assert ends_with_3_digits("_000") is True |
| 113 | + assert ends_with_3_digits("temp_01") is False |
| 114 | + assert ends_with_3_digits("temp_0001") is False |
| 115 | + assert ends_with_3_digits("temp_abc") is False |
| 116 | + assert ends_with_3_digits("temp") is False |
| 117 | + |
| 118 | + |
| 119 | +# validate_obc_file tests |
| 120 | + |
| 121 | + |
| 122 | +def test_validate_obc_file_valid(caplog): |
| 123 | + """Valid OBC file with all required attributes passes""" |
| 124 | + ds = xr.Dataset( |
| 125 | + { |
| 126 | + "temp_segment_001": (["time", "z", "x", "y"], np.random.rand(2, 3, 4, 5)), |
| 127 | + "dz_temp_segment_001": ( |
| 128 | + ["time", "z", "x", "y"], |
| 129 | + np.random.rand(2, 3, 4, 5), |
| 130 | + ), |
| 131 | + "eta_segment_001": (["time", "x", "y"], np.random.rand(2, 4, 5)), |
| 132 | + "lon": (["x", "y"], np.random.rand(4, 5)), |
| 133 | + "lat": (["x", "y"], np.random.rand(4, 5)), |
| 134 | + } |
| 135 | + ) |
| 136 | + |
| 137 | + for var in ds.data_vars: |
| 138 | + ds[var].attrs["_FillValue"] = -999.0 |
| 139 | + ds[var].attrs["coordinates"] = "lon lat" |
| 140 | + |
| 141 | + validate_obc_file(ds, ["temp_segment_001"], surface_var="eta_segment_001") |
| 142 | + |
| 143 | + |
| 144 | +def test_validate_obc_file_issues(caplog): |
| 145 | + """OBC file with missing segment and thickness logs warnings""" |
| 146 | + ds = xr.Dataset( |
| 147 | + { |
| 148 | + "temp_001": (["time", "z", "x", "y"], np.random.rand(2, 3, 4, 5)), |
| 149 | + "lon": (["x", "y"], np.random.rand(4, 5)), |
| 150 | + "lat": (["x", "y"], np.random.rand(4, 5)), |
| 151 | + } |
| 152 | + ) |
| 153 | + ds["temp_001"].attrs["_FillValue"] = -999.0 |
| 154 | + ds["temp_001"].attrs["coordinates"] = "lon lat" |
| 155 | + |
| 156 | + validate_obc_file(ds, ["temp_001"]) |
| 157 | + assert "segment" in caplog.text |
| 158 | + assert "thickness" in caplog.text or "dz_temp_001" in caplog.text |
| 159 | + |
| 160 | + |
| 161 | +def test_validate_obc_file_encoding_dict(): |
| 162 | + """Encoding dict is applied to variables""" |
| 163 | + ds = xr.Dataset( |
| 164 | + { |
| 165 | + "temp_segment_001": (["time", "z", "x", "y"], np.random.rand(2, 3, 4, 5)), |
| 166 | + "dz_temp_segment_001": ( |
| 167 | + ["time", "z", "x", "y"], |
| 168 | + np.random.rand(2, 3, 4, 5), |
| 169 | + ), |
| 170 | + "lon": (["x", "y"], np.random.rand(4, 5)), |
| 171 | + "lat": (["x", "y"], np.random.rand(4, 5)), |
| 172 | + } |
| 173 | + ) |
| 174 | + ds["temp_segment_001"].attrs["_FillValue"] = -999.0 |
| 175 | + ds["temp_segment_001"].attrs["coordinates"] = "lon lat" |
| 176 | + ds["dz_temp_segment_001"].attrs["_FillValue"] = -999.0 |
| 177 | + |
| 178 | + encoding_dict = {"temp_segment_001": {"units": "celsius"}} |
| 179 | + validate_obc_file(ds, ["temp_segment_001"], encoding_dict=encoding_dict) |
| 180 | + |
| 181 | + assert ds["temp_segment_001"].attrs["units"] == "celsius" |
0 commit comments