我想保存一個氣候文件,以便每次需要計算異常時,我都不必再運行氣候學腳本(並且需要大量時間!)。 的文件顯然以這種方式「CODEYYYYMMTTTT」:將氣候學結果保存到netcdf文件
hgap1981040000.nc
hgap1981040600.nc
hgap1981041200.nc
hgap1981041800.nc
我試着用下面的腳本到氣候平均值(從計算的NetCDF)保存到netCDF文件,和我的錯誤。
from pylab import *
import netCDF4 as nc
import numpy as np
u_2a=[]
v_2a=[]
w_2a=[]
u_2m=[]
v_2m=[]
w_2m=[]
#function to calculate mean values (this case, Apr-May only)
def mon(mo):
for yr in range (1981,1984,1):
dir_erai = '~/era-in/netc/monthly_means/{}/hgap{}{}????.nc'.format(yr,yr,mo)
print yr
f = nc.MFDataset(dir_erai)
uwnd = f.variables['U']
vwnd = f.variables['V']
wwnd = f.variables['W']
u_2 = np.mean(uwnd[0:4,:,:,:],axis=0)
v_2 = np.mean(vwnd[0:4,:,:,:],axis=0)
w_2 = np.mean(vwnd[0:4,:,:,:],axis=0)
f.close()
u_2a.append(u_2)
v_2a.append(v_2)
w_2a.append(w_2)
u_2m=np.mean(u_2a,axis=0)
v_2m=np.mean(v_2a,axis=0)
w_2m=np.mean(w_2a,axis=0)
return u_2m,v_2m,w_2m
uapr,vapr,wapr = mon('04')
umay,vmay,wmay = mon('05')
uAM = np.mean([uapr,umay],axis=0)
vAM = np.mean([vapr,vmay],axis=0)
wAM = np.mean([wapr,wmay],axis=0)
root_grp = Dataset('climatology_test.nc', 'w', format='NETCDF4')
root_grp.description = 'Example climatology winds UVW'
# dimensions
root_grp.createDimension('time', None)
root_grp.createDimension('lev', 37)
root_grp.createDimension('lat', 256)
root_grp.createDimension('lon', 512)
# variables
times = root_grp.createVariable('time', 'f8', ('time',))
levels = root_grp.createVariable('level', 'f4', ('lev',))
latitudes = root_grp.createVariable('latitude', 'f4', ('lat',))
longitudes = root_grp.createVariable('longitude', 'f4', ('lon',))
U1 = root_grp.createVariable('U1', 'f4', ('time', 'lev', 'lat', 'lon',))
V1 = root_grp.createVariable('V1', 'f4', ('time', 'lev', 'lat', 'lon',))
W1 = root_grp.createVariable('W1', 'f4', ('time', 'lev', 'lat', 'lon',))
# data
levs = [1000.,975.,950.,925.,900.,875.,850.,825.,800.,775.,750.,700.,650.,600.,550.,500.,450.,400.,350.,300.,250.,200.,175.,150.,125.,100.,70.,50.,30.,20.,10.,7.,5.,3.,2.,1.,0]
lats = np.arange(-89.5, 89.5, 0.70)
lons = np.arange(0., 358.4, 0.70)
levels[:] = levs
latitudes[:] = lats
longitudes[:] = lons
uAM[:,:,:,:] = np.random.uniform(size=(len(levs), len(lats), len(lons)))
vAM[:,:,:,:] = np.random.uniform(size=(len(levs), len(lats), len(lons)))
wAM[:,:,:,:] = np.random.uniform(size=(len(levs), len(lats), len(lons)))
root_grp.close()
和錯誤:
Traceback (most recent call last):
File "era_uv_climatology.py", line 99, in <module>
uAM[:,:,:,:] = np.random.uniform(size=(len(levs), len(lats), len(lons)))
IndexError: too many indices
我沒有改變
uAM[:,:,:,:] = np.random.uniform(size=(len(levs), len(lats), len(lons)))
vAM[:,:,:,:] = np.random.uniform(size=(len(levs), len(lats), len(lons)))
wAM[:,:,:,:] = np.random.uniform(size=(len(levs), len(lats), len(lons)))
到
U1 = uAM
V1 = vAM
W1 = wAM
,我得到了所有風值等於錯了,空netCDF文件歸零,錯誤的lon-lat範圍(1,2,3,...,256 )和(1,2,3 ....,512)。
平均法或賦值錯誤?或兩者?
是尺寸'uwnd'級x x時間緯度X經度?如果是這樣,「時間」的大小是多少?看起來你正在閱讀每月的意思......它只是一個大小? – N1B4 2015-02-12 18:02:29
文件的尺寸是[時間,等級,緯度,經度]。上面添加了文件示例(此問題的編輯版本)。月平均值以6小時的平均值計算。類似於:1998年4月0000,0600,1200和1800年的平均值。 – 2015-02-12 23:26:46
我建議查看[xarray](http://xarray.pydata.org)進行這種操作。 – gerrit 2017-01-31 12:51:37