我想提取一个相当大的netcdf文件的空间子集.从Loop through netcdf files and run calculations – Python or R起
from pylab import *
import netCDF4
f = netCDF4.MFDataset('/usgs/data2/rsignell/models/ncep/narr/air.2m.1989.nc')
# print variables
f.variables.keys()
atemp = f.variables['air'] # TODO: extract spatial subset
如何仅提取对应于状态(例如爱荷华州)的netcdf文件的子集.爱荷华州有以下边界拉特隆:
经度:89°5’W至96°31’W
纬度:40°36’N至43°30’N
解决方法:
这很容易,您必须找到纬度和经度上下限的索引.
你可以通过找到最接近你正在寻找的值来做到这一点.
latbounds = [ 40 , 43 ]
lonbounds = [ -96 , -89 ] # degrees east ?
lats = f.variables['latitude'][:]
lons = f.variables['longitude'][:]
# latitude lower and upper index
latli = np.argmin( np.abs( lats - latbounds[0] ) )
latui = np.argmin( np.abs( lats - latbounds[1] ) )
# longitude lower and upper index
lonli = np.argmin( np.abs( lons - lonbounds[0] ) )
lonui = np.argmin( np.abs( lons - lonbounds[1] ) )
然后只是变量数组的子集.
# Air (time, latitude, longitude)
airSubset = f.variables['air'][ : , latli:latui , lonli:lonui ]
>注意,我假设经度维度变量是东经度,空气变量有时间,纬度,经度维度.