python - Assign new coordinates to a variable using xarray
问题描述
I have a .nc file opened as a dataset in xarray with the following structure:
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 ...
There is an issue with this file, the variables (n2o, co2 etc.) have incorrect coordinates, the lat and lon associated with them are wrong. I would like to assign the 'latitude' and 'longitude' data variables (which contain the correct values) as the coordinates of the other variables (n2o, co2 etc.), to replace 'lat' and 'lon'. I am not sure if this is possible.
The 'latitude' variable looks like this:
<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
The ds.co2.lat
dimension looks like this:
<xarray.DataArray 'lat' (lat: 733)>
array([ 0, 1, 2, ..., 730, 731, 732], dtype=int64)
Dimensions without coordinates: lat
I have tried:
newlat = ds.variables['latitude'][:]
ds.co2['latitude'] = newlat
This didn't seem to do anything - I am not sure what to do next.
解决方案
您可能正在寻找rename
andset_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 ...
推荐阅读
- python - 如何在 SQLAlchemy 中确定自引用一对多关系的方向
- c - 当未一次性发送完整消息时,在套接字上使用 send() 的正确方法是什么?
- jquery - 如何在 Rails 中使用 Jquery Raty 存储来自星级评分的多个值
- android - API 在 POSTMAN 中工作而不是在 Retrofit android 中工作
- firebase - Google Firestore 查找已删除文档的已删除子集合
- python - Qt WebEngine 密码管理器 - 如何检测网站上何时使用用户名/密码
- python - 类型错误:+= 不支持的操作数类型:“标签”和“整数”
- java - 谁能用简单的英语解释对原子性和易失性变量的需求?
- c - 在尝试将元素分配给它时使用带有数组指针的函数有什么问题?
- serverless-framework - 一个资源可以引用另一个资源吗?