简体   繁体   中英

Selecting data variables results in loss of coordinate information in xarray

I wish to subset my xarray Dataset via a list of variable names . However, when I do so, the resultant Dataset no longer has the coordinate reference information, as evidenced by adding the subset as a layer in QGIS.

How can I keep the coordinate reference information after subsetting the original Dataset ?

import xarray as xr

DS = xr.open_dataset("my_data.nc")
bands = ['CMI_C01','CMI_C02','CMI_C03']

# Test does not have coordinate reference information :(
test = DS[bands]

It is apparent that the coordinate reference information is not stored in the .coords attribute, due to the following not working:

# Test still does not have coordinate reference info
test = test.assign_coords(dict(DS.coords))

# When put into QGIS, does not have the CRS
test.to_netcdf("test.nc")

Where is the CRS stored for xarray Datasets?


For background, I am using GOES imagery from the public AWS s3 bucket.

This is what the original Dataset looks like:

 Dimensions: (y: 1500, x: 2500, number_of_time_bounds: 2, number_of_image_bounds: 2, band: 1) Coordinates: (3/37) t datetime64[ns] 2017-03-04T08:38:0... * y (y) float32 0.1265... 0.04259 * x (x) float32 -0.07501... 0.06493.47 Attributes: (2/29) naming_authority: gov.nesdis.noaa Conventions: CF-1.7

coordinates in xarray refer to the dimension labels, and have nothing to do with spatial coordinate reference system metadata.

You're looking for xarray Attributes. These can be accessed with .attrs , and you can carry over attributes from one dataset to another with:

test.attrs.update(DS.attrs)

Note that xarray does not explicitly handle CRS information ever , and additionally does not preserve attributes in computations by default. You can change this behavior to keep attributes across computation steps by default with:

xr.set_options(keep_attrs=True)

See the FAQ section: What is your approach to metadata? for more information. Also see the docs on Data Structures for more detail on the various xarray objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM