python中基于坐标边界的WRF netcdf文件子集小数组

2024-09-29 19:23:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个来自WRF运行的netcdf文件,一个包含每小时的数据,另一个较小的文件有坐标(XLAT和XLONG)。我试图根据某些坐标检索数据的子集。在

其中一个变量的例子是温度'T2',其维度(110151359)分别是(时间,南趵北,西趵东)。在

XLAT和XLONG的尺寸相同(110151359)。在

有一个相同的问题被问到(请看netcdf4 extract for subset of lat lon),因为我的横向/纵向维度有点不同,脚本对我不起作用,我也不知道为什么。我试图将坐标转换成1D数组,这样它将与上一个问题类似,但是脚本不起作用,我得到了一个索引错误。在

如果有人能帮我,那就太棒了!提前感谢:)

import numpy as np
from netCDF4 import dataset  
import matplotlib.pyplot as plt

lons = b.variables['XLONG'][:]
lats = b.variables['XLAT'][:]

lons2d =lons.reshape((1015,1359))
lons1d = lons2d.reshape((1379385))

lats2d =lats.reshape((1015,1359))
lats1d = lats2d.reshape((1379385))

lat_bnds, lon_bnds = [49,53], [-125,-115]
lat_inds = np.where((lats1d > lat_bnds[0]) & (lats1d < lat_bnds[1]))
lon_inds = np.where((lons1d > lon_bnds[0]) & (lons1d < lon_bnds[1]))

T_subset = a.variables['T2'][:,lat_inds,lon_inds]

但是我得到了以下错误:

^{pr2}$

Tags: 文件数据importnpvariableslonlatt2
2条回答

我不知道为什么它不起作用,但我认为这是你想要的,而且更干净:

import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt

# By indexing at 0 along first dimension, we eliminate the time
# dimension, which only had size 0 anyway.
lons = b.variables['XLONG'][0]
lats = b.variables['XLAT'][0]
temp = a.variables['T2'][0]

lat_bnds, lon_bnds = [49,53], [-125,-115]

# Just AND together all of them and make a big mask
subset = ((lats > lat_bnds[0]) & (lats < lat_bnds[1]) & 
          (lons > lon_bnds[0]) & (lons < lon_bnds[1]))

# Apply mask should apply to trailing dimensions...I think
T_subset = temp[subset]

我看到lat_inds有一个明显的问题,因为它有max-shape 1015*1359,但是您尝试使用它作为纬度的索引,其大小为1015。所以在我看来,你应该先找到lat_indslon_inds的相似值,这两个点同时满足lon和lat的限制,然后使用这个数组来处理平坦的数据。比如:

uni_ind=numpy.intersect1d(lat_inds,lon_inds)
T_subset=np.ravel(a.variables['T2'])[uni_ind]

将数组转换回二维可能还存在一些问题,因为我假设原始数据不是在柱坐标系中,因此结果子集可能不是矩形的。 这个代码没有测试,如果你共享原始数据文件,我也可以这样做。在

编辑: 为了正确绘图,使用遮罩比较容易,这个例子应该有足够的信息。在

^{pr2}$

相关问题 更多 >

    热门问题