Skip to content

Commit 60e6132

Browse files
Zaczerodjhoese
authored andcommitted
Decode CF metadata for pre-opened datasets
(cherry picked from commit a8fedea) (cherry picked from commit 997480a)
1 parent 762aece commit 60e6132

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

pyresample/test/test_utils/test_cf.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,35 @@ def _prepare_cf_llnocrs():
142142
return ds
143143

144144

145+
def _prepare_cf_projected_packed_xy():
146+
import xarray as xr
147+
148+
axis_values = np.array([-100, 0, 100], dtype=np.int16)
149+
ds = xr.Dataset({'temp': (('y', 'x'), np.ma.masked_all((3, 3)), {'grid_mapping': 'crs'})},
150+
coords={'x': ('x', axis_values),
151+
'y': ('y', axis_values[::-1])})
152+
ds['x'].attrs['standard_name'] = 'projection_x_coordinate'
153+
ds['x'].attrs['units'] = 'm'
154+
ds['x'].attrs['scale_factor'] = 10.0
155+
ds['x'].attrs['add_offset'] = 1000.0
156+
ds['y'].attrs['standard_name'] = 'projection_y_coordinate'
157+
ds['y'].attrs['units'] = 'm'
158+
ds['y'].attrs['scale_factor'] = 10.0
159+
ds['y'].attrs['add_offset'] = 1000.0
160+
161+
ds['crs'] = 0
162+
ds['crs'].attrs['grid_mapping_name'] = "stereographic"
163+
ds['crs'].attrs['false_easting'] = 0.
164+
ds['crs'].attrs['false_northing'] = 0.
165+
ds['crs'].attrs['semi_major_axis'] = 6378137.
166+
ds['crs'].attrs['inverse_flattening'] = 298.257223563
167+
ds['crs'].attrs['latitude_of_projection_origin'] = 90.
168+
ds['crs'].attrs['straight_vertical_longitude_from_pole'] = 0.
169+
ds['crs'].attrs['scale_factor_at_projection_origin'] = 1.
170+
171+
return ds
172+
173+
145174
class TestLoadCFAreaPublic:
146175
"""Test public API load_cf_area() for loading an AreaDefinition from netCDF/CF files."""
147176

@@ -244,6 +273,23 @@ def test_load_cf_latlon(self, file_func, kwargs, exp_lat, exp_lon, future_geomet
244273
_validate_lonlat_cf_area(adef, cf_info, exp_lon, exp_lat)
245274
assert_future_geometry(adef, future_geometries)
246275

276+
def test_load_cf_dataset_input_decodes_cf_coordinates(self, tmp_path):
277+
import xarray as xr
278+
279+
cf_file = _prepare_cf_projected_packed_xy()
280+
file_path = tmp_path / "packed_xy.nc"
281+
cf_file.to_netcdf(file_path)
282+
283+
expected_area, expected_info = load_cf_area(file_path, variable="temp")
284+
285+
with xr.open_dataset(file_path, decode_cf=False) as raw_ds:
286+
area_from_dataset, info_from_dataset = load_cf_area(raw_ds, variable="temp")
287+
288+
np.testing.assert_allclose(area_from_dataset.area_extent, expected_area.area_extent)
289+
assert info_from_dataset['x']['first'] == expected_info['x']['first']
290+
assert info_from_dataset['x']['last'] == expected_info['x']['last']
291+
assert info_from_dataset['y']['first'] == expected_info['y']['first']
292+
assert info_from_dataset['y']['last'] == expected_info['y']['last']
247293

248294
def _validate_lonlat_cf_area(adef, cf_info, exp_lon, exp_lat):
249295
assert adef.shape == (19, 37)

pyresample/utils/cf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,6 @@ def _open_nc_file(nc_file: str | Path | xr.Dataset) -> xr.Dataset:
473473
if xr is None:
474474
raise ImportError("Xarray (pip install xarray) is required to load geometries from a NetCDF file.")
475475
if isinstance(nc_file, xr.Dataset):
476-
return nc_file
476+
return xr.decode_cf(nc_file)
477477

478478
return xr.open_dataset(nc_file)

0 commit comments

Comments
 (0)