关于使用Python xarray将经度数组从0-360更改为-180到180 [英] About changing longitude array from 0 - 360 to -180 to 180 with Python xarray
本文介绍了关于使用Python xarray将经度数组从0-360更改为-180到180的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一位matlab用户,最近尝试在我的计算中更多地使用Python.我正在使用xarray,并希望将地球物理字段的经度数组从0-360更改为-180到180.但是当我这样做时:
I am a matlab user trying to use Python more for my computations recently. I am using xarray and would like to change my longitude array from 0 - 360 to -180 to 180 of a geophysical field. But when I do that:
df=xr.open_dataset(ecmwf_winds.nc)
u10=df['u10']
lon=df['longitude']
lon = np.where(lon > 180, lon-360, lon)
[X,Y]=np.meshgrid(lon,df.latitude)
plt.contourf(X,Y,u10)
等高线图显示出带有缝隙的混乱,这是没有意义的.任何人都可以帮我.我不确定我在哪里做错了.
the contourplot turns out to messy with gaps, which does not make sense. Can anyone please help me with it. I am not sure where I am doing wrong.
推荐答案
您需要先分配值,然后再沿着新的坐标值对所得的DataArray进行排序:
You need to assign the values as you've done and then also sort the resulting DataArray along the new coordinate values:
lon_name = 'longitude' # whatever name is in the data
# Adjust lon values to make sure they are within (-180, 180)
ds['_longitude_adjusted'] = xr.where(
ds[lon_name] > 180,
ds[lon_name] - 360,
ds[lon_name])
# reassign the new coords to as the main lon coords
# and sort DataArray using new coordinate values
ds = (
ds
.swap_dims({lon_name: '_longitude_adjusted'})
.sel(**{'_longitude_adjusted': sorted(ds._longitude_adjusted)})
.drop(lon_name))
ds = ds.rename({'_longitude_adjusted': lon_name})
这篇关于关于使用Python xarray将经度数组从0-360更改为-180到180的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文