cf.from_xarray¶
- cf.from_xarray(ds, domain=False)[source]¶
Convert an
xarraydataset to CF field or domain constructs.In general (see the note below)
cf.from_xarrayis an in-memory operation which does two things: it maps thexarraydataset variables and attributes onto CF constructs; and it copies pointers to the arrays describing the actual data, from thexarraydataset to the fields or domains. No actual data is copied.cf.from_xarraytreats thexarraydataset as if it were an in-memory view of a netCDF dataset (even if it wasn’t created as such), and creates the same CF field or domain constructs thatcf.readwould if it were to read that netCDF dataset.Warning
For optimal performance and minimal memory usage, open
xarraydatasets from disk withdecode_cf=False. This is not the default forxarray.open_datasetorxarray.open_datatree.If the dataset is opened with the default
decode_cf=True, the automatic reversal of xarray’s CF decoding for selected variables will eagerly load their data arrays into memory. This applies to variables with explicit_FillValueormissing_valueattributes, and to variables decoded into non-numeric time or timedelta objects.This is never an issue for datasets created with
cf.to_xarray:cf.from_xarray(cf.to_xarray(f))will never load those data arrays into memory.Added in version 1.13.4.0.
See also
- Parameters:
- ds:
xarray.Datasetorxarray.DataTree The
xarraydataset to be converted. The group structure of anxarray.DataTreeinstance is preserved in the returned field or domains.- domain:
bool, optional If False (the default) then return only field constructs that are defined by CF data variables in the
xarraydataset, ignoring all CF domain variables. If True then only the domain constructs that are explicitly defined by CF domain variables in thexarraydataset are returned.
- ds:
- Returns:
- Examples:
Create some field constructs, convert them to an
xarraydataset, convert thexarraydataset back to field constructs, and show that (in this case) no information has been lost.>>> f = cf.example_fields(0, 7) >>> f [<CF Field: specific_humidity(latitude(5), longitude(8)) 1>, <CF Field: eastward_wind(time(3), air_pressure(1), grid_latitude(4), grid_longitude(5)) m s-1>] >>> ds = cf.to_xarray(f) >>> ds <xarray.Dataset> Size: 3kB Dimensions: (lat: 5, lon: 8, time_1: 3, air_pressure: 1, grid_latitude: 4, grid_longitude: 5, bounds2: 2, bounds4: 4) Coordinates: * lat (lat) float64 40B -75.0 -45.0 0.0 45.0 75.0 * lon (lon) float64 64B 22.5 67.5 ... 292.5 337.5 * time_1 (time_1) float64 24B 120.5 121.5 122.5 * air_pressure (air_pressure) float64 8B 850.0 * grid_latitude (grid_latitude) float64 32B 0.44 0.0 -0.44 -0.88 * grid_longitude (grid_longitude) float64 40B -1.18 ... 0.58 latitude (grid_latitude, grid_longitude) float64 160B dask.array<chunksize=(4, 5), meta=np.ndarray> longitude (grid_latitude, grid_longitude) float64 160B dask.array<chunksize=(4, 5), meta=np.ndarray> time float64 8B dask.array<chunksize=(), meta=np.ndarray> Dimensions without coordinates: bounds2, bounds4 Data variables: lat_bnds (lat, bounds2) float64 80B dask.array<chunksize=(5, 2), meta=np.ndarray> lon_bnds (lon, bounds2) float64 128B dask.array<chunksize=(8, 2), meta=np.ndarray> q (lat, lon) float64 320B dask.array<chunksize=(5, 8), meta=np.ndarray> bounds (time_1, bounds2) float64 48B dask.array<chunksize=(3, 2), meta=np.ndarray> bounds_1 (grid_latitude, bounds2) float64 64B dask.array<chunksize=(4, 2), meta=np.ndarray> bounds_2 (grid_longitude, bounds2) float64 80B dask.array<chunksize=(5, 2), meta=np.ndarray> latitude_bounds (grid_latitude, grid_longitude, bounds4) float64 640B dask.array<chunksize=(4, 5, 4), meta=np.ndarray> bounds_3 (grid_latitude, grid_longitude, bounds4) float64 640B dask.array<chunksize=(4, 5, 4), meta=np.ndarray> rotated_latitude_longitude object 8B nan ua (time_1, air_pressure, grid_latitude, grid_longitude) float32 240B dask.array<chunksize=(3, 1, 4, 5), meta=np.ndarray> Attributes: Conventions: CF-1.13 >>> g = cf.from_xarray(ds) >>> g [<CF Field: specific_humidity(latitude(5), longitude(8)) 1>, <CF Field: eastward_wind(time(3), air_pressure(1), grid_latitude(4), grid_longitude(5)) m s-1>] >>> [field1.equals(field0) for field0, field1 in zip(f, g)] [True, True]