Skip to main content
 首页 » 编程设计

python之将气候学结果保存到 netcdf 文件

2025年05月04日17傻小

我想保存一个气候文件,这样每次我需要计算异常时,我就不必再次运行气候脚本(这需要很多时间!)。 这些文件显然是以这种方式“CODEYYYYMMTTTT”:

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 文件,其中所有风值都为零,并且经纬度范围 (1,2,3,...,256) 和 (1,2,3... .,512) .

平均方法或赋值错误吗?还是两者兼而有之?

请您参考如下方法:

这第一段代码,

 
 
    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))) 
 

给你这个错误是因为你的随机制服的形状与 netcdf 变量不一样。试试吧。

 
 
    uAM[0,:,:,:] = np.random.uniform(size=(len(levs), len(lats), len(lons))) 
    vAM[0,:,:,:] = np.random.uniform(size=(len(levs), len(lats), len(lons))) 
    wAM[0,:,:,:] = np.random.uniform(size=(len(levs), len(lats), len(lons))) 
 

你最后是用close方法关闭气候文件吗?