@@ -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+
145174class TestLoadCFAreaPublic :
146175 """Test public API load_cf_area() for loading an AreaDefinition from netCDF/CF files."""
147176
@@ -244,6 +273,7 @@ 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+ < << << << pr / cf - axis - units
247277 def test_load_cf_axis_without_units (self ):
248278 cf_file = _prepare_cf_nh10km ()
249279 del cf_file ['xc' ].attrs ['units' ]
@@ -264,6 +294,25 @@ def test_load_cf_axis_with_non_string_units(self):
264294 assert cf_info ['x' ]['unit' ] is None
265295 assert cf_info ['y' ]['unit' ] is None
266296
297+ == == == =
298+ def test_load_cf_dataset_input_decodes_cf_coordinates (self , tmp_path ):
299+ import xarray as xr
300+
301+ cf_file = _prepare_cf_projected_packed_xy ()
302+ file_path = tmp_path / "packed_xy.nc"
303+ cf_file .to_netcdf (file_path )
304+
305+ expected_area , expected_info = load_cf_area (file_path , variable = "temp" )
306+
307+ with xr .open_dataset (file_path , decode_cf = False ) as raw_ds :
308+ area_from_dataset , info_from_dataset = load_cf_area (raw_ds , variable = "temp" )
309+
310+ np .testing .assert_allclose (area_from_dataset .area_extent , expected_area .area_extent )
311+ assert info_from_dataset ['x' ]['first' ] == expected_info ['x' ]['first' ]
312+ assert info_from_dataset ['x' ]['last' ] == expected_info ['x' ]['last' ]
313+ assert info_from_dataset ['y' ]['first' ] == expected_info ['y' ]['first' ]
314+ assert info_from_dataset ['y' ]['last' ] == expected_info ['y' ]['last' ]
315+ >> >> >> > main
267316
268317def _validate_lonlat_cf_area (adef , cf_info , exp_lon , exp_lat ):
269318 assert adef .shape == (19 , 37 )
0 commit comments