问题描述
我想提取一个相当大的netcdf文件的空间子集.来自遍历netcdf文件并运行计算-Python或R
I would like to extract a spatial subset of a rather large netcdf file. From 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文件的子集(例如,爱荷华州).爱荷华州有以下边界纬度:
How do I extract just the subset of netcdf file corresponding to a state (say Iowa). Iowa has following boundary lat lon:
经度:89°5'W到96°31'W
Longitude: 89° 5' W to 96° 31' W
纬度:40°36'N到43°30'N
Latitude: 40° 36' N to 43° 30' N
推荐答案
这很容易,您必须找到纬度和经度上下限的索引.您可以通过查找与您要查找的值最接近的值来做到这一点.
Well this is pretty easy, you have to find the index for the upper and lower bound in latitude and longitude. You can do it by finding the value that is closest to the ones you're looking for.
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] ) )
然后仅将变量数组作为子集.
Then just subset the variable array.
# Air (time, latitude, longitude)
airSubset = f.variables['air'][ : , latli:latui , lonli:lonui ]
- 请注意,我假设经度尺寸变量位于东经度,而空气变量具有时间,纬度和经度尺寸.
这篇关于netcdf4提取子集的纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!