我在.array中打开了一个.nc文件作为数据集,其结构如下:
ds
<xarray.Dataset>
Dimensions: (lat: 733, lon: 720, time: 204)
Coordinates:
* time (time) int16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
Dimensions without coordinates: lat , lon
Data variables :
latitude (lat) float32 56.0 55.9917 55.9833 55.975 55.9667 55.9583 ...
longitude (lon) float32 -11.0 -10.9917 -10.9833 -10.975 -10.9667 ...
n2o (lat, lon, time) float64 nan nan nan nan nan nan nan nan nan ...
co2 (lat, lon, time) float64 nan nan nan nan nan nan nan nan nan ...
no3 (lat, lon, time) float64 nan nan nan nan nan nan nan nan nan ...
ch4 (lat, lon, time) float64 nan nan nan nan nan nan nan nan nan ...
soc (lat, lon, time) float64 nan nan nan nan nan nan nan nan nan ...
此文件存在问题,变量(n2o,co2等)的坐标不正确,与它们关联的经纬度错误。我想将“纬度”和“经度”数据变量(包含正确的值)分配为其他变量(n2o,co2等)的坐标,以替换“纬度”和“ lon”。我不确定这是否可能。
“纬度”变量如下所示:
<xarray.Variable (lat: 733)>
array([ 56. , 55.991665, 55.98333 , ..., 49.915367, 49.907032,
49.898697], dtype=float32)
Attributes:
units: degrees of latitude North to South in 30 arc seconds steps
long_name: latitude
ds.co2.lat
维度如下:
<xarray.DataArray 'lat' (lat: 733)>
array([ 0, 1, 2, ..., 730, 731, 732], dtype=int64)
Dimensions without coordinates: lat
我尝试过:
newlat = ds.variables['latitude'][:]
ds.co2['latitude'] = newlat
这似乎什么也没做-我不确定下一步该怎么做。
答案 0 :(得分:2)
您可能正在寻找rename
和set_coords
方法。您看到的是xarray无法确定纬度/经度是坐标变量。解决方案是先重命名纬度/经度变量,然后将这些变量关联为坐标。
In [10]: ds = xr.Dataset({'a': xr.Variable(('lon', 'lat'), np.random.random((4, 5))), 'b': xr.Variable(('lon', 'lat'),np.random.random((4, 5))), 'longitude':
...: xr.Variable('lon', [1, 2, 3, 4]), 'latitude': xr.Variable('lat', [1, 2, 3, 4, 5])})
In [11]: ds
Out[11]:
<xarray.Dataset>
Dimensions: (lat: 5, lon: 4)
Dimensions without coordinates: lat, lon
Data variables:
a (lon, lat) float64 0.8694 0.5929 0.04661 0.9571 0.1814 ...
b (lon, lat) float64 0.8893 0.5772 0.8457 0.8337 0.7966 0.1619 ...
longitude (lon) int64 1 2 3 4
latitude (lat) int64 1 2 3 4 5
In [12]: ds.rename({'latitude': 'lat', 'longitude': 'lon'}).set_coords(['lon', 'lat'])
Out[12]:
<xarray.Dataset>
Dimensions: (lat: 5, lon: 4)
Coordinates:
* lon (lon) int64 1 2 3 4
* lat (lat) int64 1 2 3 4 5
Data variables:
a (lon, lat) float64 0.8694 0.5929 0.04661 0.9571 0.1814 0.01788 ...
b (lon, lat) float64 0.8893 0.5772 0.8457 0.8337 0.7966 0.1619 ...