在Python中读取和操作多个NetCDF文件

4

我需要帮助阅读多个netCDF文件,尽管这里有一些例子,但它们都不能正常工作。 我使用Python(x,y)版本2.7.5和其他软件包:netcdf4 1.0.7-4,matplotlib 1.3.1-4,numpy 1.8,pandas 0.12,basemap 1.0.2...

我用GrADS做了很多事情,现在需要开始用Python做。 我有一些2米温度数据(来自ECMWF的每年4小时数据),每个文件包含2米温度数据,Xsize=480,Ysize=241, Zsize(水平)=1,Tsize(时间)=1460或闰年的1464。 这些是我的文件名类似于:t2m.1981.nc,t2m.1982.nc,t2m.1983.nc ...等。

根据这个页面: (Loop through netcdf files and run calculations - Python or R) 这是我目前的进展:

from pylab import *
import netCDF4 as nc
from netCDF4 import *
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import numpy as np

f = nc.MFDataset('d:/data/ecmwf/t2m.????.nc') # as '????' being the years
t2mtr = f.variables['t2m']

ntimes, ny, nx = shape(t2mtr)
temp2m = zeros((ny,nx),dtype=float64)
print ntimes
for i in xrange(ntimes):
    temp2m += t2mtr[i,:,:] #I'm not sure how to slice this, just wanted to get the 00Z values.
      # is it possible to assign to a new array,...
      #... (for eg.) the average values of  00z for January only from 1981-2000? 

#creating a NetCDF file
nco = nc.Dataset('d:/data/ecmwf/t2m.00zJan.nc','w',clobber=True)
nco.createDimension('x',nx)
nco.createDimension('y',ny)

temp2m_v = nco.createVariable('t2m', 'i4',  ( 'y', 'x'))
temp2m_v.units='Kelvin'
temp2m_v.long_name='2 meter Temperature'
temp2m_v.grid_mapping = 'Lambert_Conformal' # can it be something else or ..
#... eliminated?).This is straight from the solution on that webpage.

lono = nco.createVariable('longitude','f8')
lato = nco.createVariable('latitude','f8')
xo = nco.createVariable('x','f4',('x')) #not sure if this is important
yo = nco.createVariable('y','f4',('y')) #not sure if this is important
lco = nco.createVariable('Lambert_Conformal','i4') #not sure

#copy all the variable attributes from original file
for var in ['longitude','latitude']:
    for att in f.variables[var].ncattrs():
        setattr(nco.variables[var],att,getattr(f.variables[var],att))

# copy variable data for lon,lat,x and y
lono=f.variables['longitude'][:]
lato=f.variables['latitude'][:]
#xo[:]=f.variables['x']
#yo[:]=f.variables['y']

#  write the temp at 2 m data
temp2m_v[:,:]=temp2m

# copy Global attributes from original file
for att in f.ncattrs():
    setattr(nco,att,getattr(f,att))

nco.Conventions='CF-1.6' #not sure what is this.
nco.close()

#attempt to plot the 00zJan mean
file=nc.Dataset('d:/data/ecmwf/t2m.00zJan.nc','r')
t2mtr=file.variables['t2m'][:]
lon=file.variables['longitude'][:]
lat=file.variables['latitude'][:]
clevs=np.arange(0,500.,10.)
map =   Basemap(projection='cyl',llcrnrlat=0.,urcrnrlat=10.,llcrnrlon=97.,urcrnrlon=110.,resolution='i')
x,y=map(*np.meshgrid(lon,lat))
cs = map.contourf(x,y,t2mtr,clevs,extend='both')
map.drawcoastlines()
map.drawcountries()
plt.plot(cs)
plt.show()

第一个问题出现在temp2m += t2mtr[1,:,:]。我不确定如何切片数据以仅获取所有文件的00z(假设仅为1月份)。

其次,在运行测试时,cs = map.contourf(x,y,t2mtr,clevs,extend='both')出现错误,提示“形状与z不匹配:找到(1,1),而不是(241,480)”。我知道输出数据上可能有一些错误,由于记录值时出现错误,但我无法弄清楚是什么/在哪里。

感谢您的时间。希望这不会让人困惑。


1
00Z数据是什么意思?我不理解你的数据集是什么格式:3D,形状为(时间,x,y),这是我理解的。 Z在哪里/是什么? - user707650
每个文件包含00z、06z、12z和18z(时间,协调世界时)。这是4次日常数据。因此,假设在一个名为t2m.2000.nc的文件中,t=1464,每天4次。由于数据位于地表(2米温度),Z值=1。这是全球网格化数据。 - Fir Nor
1个回答

3
所以t2mtr是一个三维数组。
ntimes, ny, nx = shape(t2mtr)

这将计算第一个轴上的所有值的总和:

for i in xrange(ntimes):
    temp2m += t2mtr[i,:,:]

更好的方法是:
temp2m = np.sum(tm2tr, axis=0)
temp2m = tm2tr.sum(axis=0) # alt

如果您想求平均值,请使用np.mean而不是np.sum

要对一部分时间进行平均值计算,比如jan_times,可以使用以下表达式:

jan_avg = np.mean(tm2tr[jan_times,:,:], axis=0)

如果您只想得到一个简单的范围,比如前30个时间点,则这是最简单的。为简单起见,我假设数据是日常的,年份长度恒定。您可以调整4小时频率和闰年等设置。

tm2tr[0:31,:,:]

获取多年1月份数据的一种简单方法是构建如下所示的索引:

yr_starts = np.arange(0,3)*365 # can adjust for leap years
jan_times = (yr_starts[:,None]+ np.arange(31)).flatten()
# array([  0,   1,   2, ...  29,  30, 365, ..., 756, 757, 758, 759, 760])

另一种选择是重新设计 tm2tr(不适用于闰年)。
tm2tr.reshape(nyrs, 365, nx, ny)[:,0:31,:,:].mean(axis=1)

您可以使用类似以下的内容测试时间采样:
np.arange(5*365).reshape(5,365)[:,0:31].mean(axis=1)

数据集中没有时间变量吗?你可以从那里提取所需的时间索引。我几年前使用过ECMWF数据,但记不清很多细节了。
至于您的contourf错误,请检查3个主要参数的形状:x,y,t2mtr。它们应该匹配。我没有使用过Basemap。

感谢@hpaulj的解释和解决方案。正是我所需要的。是的,数据可以设置到特定的时间索引中,但我下载的方式是为了节省时间(和一点空间)。再次感谢。 - Fir Nor
嗨@hpaulj,我想知道在yr_starts = np.arange(0,3)*365中,(0,3)是指时间步长(在这种情况下,每天4小时)还是一些随机年数。谢谢! - Fir Nor
np.arange(0,3)*365 就是 [0, 365, 2*365],我假设这些是连续的1月1日数据点的索引。对于您的4小时数据,它需要额外的 *6,以及其他起始点和闰年的偏移量。 - hpaulj
我还会检查数据中是否存在 nan(或等价物)。当我使用ECMWF时,我需要像温度这样的点数据和像降水这样的周期数据。一个数据集在请求的时间段末尾有一个 nan,另一个数据集在开头有一个。 - hpaulj
谢谢 @hpaulj .. 我刚刚意识到我也可以在Python控制台上检查 numpy.arange。 :) - Fir Nor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接