简体   繁体   English

选择数据变量会导致 xarray 中的坐标信息丢失

[英]Selecting data variables results in loss of coordinate information in xarray

I wish to subset my xarray Dataset via a list of variable names .我希望通过变量名称列表对我的 xarray Dataset进行子集化 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.但是,当我这样做时,生成的数据集不再具有坐标参考信息,正如在 QGIS 中将子集添加为图层所证明的那样。

How can I keep the coordinate reference information after subsetting the original Dataset ?如何在对原始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:很明显,坐标参考信息没有存储在.coords属性中,原因如下:

# 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? xarray 数据集的 CRS 存储在哪里?


For background, I am using GOES imagery from the public AWS s3 bucket.作为背景,我使用 来自公共 AWS s3 存储桶的 GOES 图像。

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. xarray 中的坐标指的是维度标签,与空间坐标参考系统元数据无关。

You're looking for xarray Attributes.您正在寻找 xarray 属性。 These can be accessed with .attrs , and you can carry over attributes from one dataset to another with:这些可以通过.attrs访问,您可以通过以下方式将属性从一个数据集转移到另一个数据集:

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.请注意,xarray不会显式处理 CRS 信息,并且默认情况下不会在计算中保留属性。 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.另请参阅有关数据结构的文档以获取有关各种 xarray 对象的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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