|
4 | 4 |
|
5 | 5 | from imaspy.backends.netcdf.ids2nc import IDS2NC |
6 | 6 | from imaspy.backends.netcdf.nc2ids import NC2IDS |
7 | | -from imaspy.exception import InvalidNetCDFEntry |
| 7 | +from imaspy.backends.netcdf.nc_validate import validate_netcdf_file |
| 8 | +from imaspy.exception import InvalidNetCDFEntry, UnknownDDVersion |
8 | 9 | from imaspy.ids_defs import IDS_TIME_MODE_HOMOGENEOUS |
9 | 10 | from imaspy.ids_factory import IDSFactory |
10 | 11 |
|
@@ -144,3 +145,53 @@ def test_shape_array_with_invalid_dtype(memfile_with_ids, factory): |
144 | 145 | ) |
145 | 146 | with pytest.raises(InvalidNetCDFEntry): |
146 | 147 | NC2IDS(memfile_with_ids, cp).run() |
| 148 | + |
| 149 | + |
| 150 | +def test_validate_nc(tmpdir): |
| 151 | + fname = str(tmpdir / "test.nc") |
| 152 | + |
| 153 | + # Wrong extension |
| 154 | + with pytest.raises(InvalidNetCDFEntry): |
| 155 | + validate_netcdf_file("test.h5") # invalid extension |
| 156 | + |
| 157 | + # Empty file |
| 158 | + netCDF4.Dataset(fname, "w").close() |
| 159 | + with pytest.raises(InvalidNetCDFEntry): |
| 160 | + validate_netcdf_file(fname) |
| 161 | + |
| 162 | + # Invalid DD version |
| 163 | + with netCDF4.Dataset(fname, "w") as dataset: |
| 164 | + dataset.data_dictionary_version = "invalid" |
| 165 | + dataset.createGroup("core_profiles") |
| 166 | + with pytest.raises(UnknownDDVersion): |
| 167 | + validate_netcdf_file(fname) |
| 168 | + |
| 169 | + # Invalid group |
| 170 | + with netCDF4.Dataset(fname, "w") as dataset: |
| 171 | + dataset.data_dictionary_version = "4.0.0" |
| 172 | + dataset.createGroup("X") |
| 173 | + with pytest.raises(InvalidNetCDFEntry): |
| 174 | + validate_netcdf_file(fname) |
| 175 | + |
| 176 | + # Invalid occurrence |
| 177 | + with netCDF4.Dataset(fname, "w") as dataset: |
| 178 | + dataset.data_dictionary_version = "4.0.0" |
| 179 | + dataset.createGroup("core_profiles/a") |
| 180 | + with pytest.raises(InvalidNetCDFEntry): |
| 181 | + validate_netcdf_file(fname) |
| 182 | + |
| 183 | + # Invalid variable in root group |
| 184 | + with netCDF4.Dataset(fname, "w") as dataset: |
| 185 | + dataset.data_dictionary_version = "4.0.0" |
| 186 | + dataset.createVariable("core_profiles", int, ()) |
| 187 | + with pytest.raises(InvalidNetCDFEntry): |
| 188 | + validate_netcdf_file(fname) |
| 189 | + |
| 190 | + # Missing ids_properties.homogeneous_time |
| 191 | + with netCDF4.Dataset(fname, "w") as dataset: |
| 192 | + dataset.data_dictionary_version = "4.0.0" |
| 193 | + dataset.createGroup("core_profiles/1") |
| 194 | + with pytest.raises(InvalidNetCDFEntry): |
| 195 | + validate_netcdf_file(fname) |
| 196 | + |
| 197 | + # All other validations are handled by NC2IDS and tested above |
0 commit comments