保存气候学结果为netcdf文件

2024-06-03 12:03:23 发布

您现在位置:Python中文网/ 问答频道 /正文

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

hgap1981040000.nc
hgap1981040600.nc
hgap1981041200.nc
hgap1981041800.nc

我试图使用下面的脚本将气候平均值(从netcdf计算)保存到netcdf文件中,但得到了错误。在

^{pr2}$

错误是:

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

平均法或赋值法是错误的吗?还是两者都有?在


Tags: 文件脚本sizelen错误npnetcdfrandom
3条回答

在提出一些建议并重新开始编码之后,我使用下面的脚本成功地获得了所需的结果。然而,如何获得lonlat的值分别保持在0到36090到-90仍然是一个谜。在

import netCDF4 as nc
from netCDF4 import *
import numpy as np
import time
from numpy.random import uniform

u_2a=[]
v_2a=[]
w_2a=[]
u_2m=[]
v_2m=[]
w_2m=[]

def mon(mo):
    for yr in range (1981,2011,1):
        dir_erai = '~/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')
usep,vsep,wsep = mon('09')
uoct,voct,woct = mon('10')

#creating netcdf
rootgrp = Dataset('climatology_u.nc', 'w', format='NETCDF4')
print rootgrp.data_model

level = rootgrp.createDimension('level',None)
lat = rootgrp.createDimension('lat', 256)
lon = rootgrp.createDimension('lon', 512)
print rootgrp.dimensions

levels = rootgrp.createVariable('level','i4',('level',))
latitudes = rootgrp.createVariable('latitude','f4',('lat',))
longitudes = rootgrp.createVariable('longitude','f4',('lon',))
U1 = rootgrp.createVariable('U1','f4',('level','lat','lon',))
V1 = rootgrp.createVariable('V1','f4',('level','lat','lon',))
W1 = rootgrp.createVariable('W1','f4',('level','lat','lon',))


rootgrp.description = 'Create UVW climatology'
rootgrp.source = 'netCDF4 python module tutorial'
latitudes.units = 'degrees north'
longitudes.units = 'degrees east'
levels.units = 'hPa'
U1.units = 'm/s'
V1.units = 'm/s'
W1.units = 'm/s'

lats =  np.arange(-89.5, 89.5, 0.70)
lons =  np.arange(0., 358.4, 0.70)
latitudes[:]=lats[::-1] #this probably doesn't matter
longitudes[:]=lons


nlats = len(rootgrp.dimensions['lat'])
nlons = len(rootgrp.dimensions['lon'])
print 'U1 shape before adding data = ',U1.shape
U1[0:37,:,:] = uniform(size=(37,nlats,nlons))
V1[0:37,:,:] = uniform(size=(37,nlats,nlons))
W1[0:37,:,:] = uniform(size=(37,nlats,nlons))
print 'U1 shape after adding data = ',U1.shape
levels[:] = [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] 

U1[:,:,:]=uapr[:,::-1,:] # without "::-1", the data is upside down,sth to do with source dataset
V1[:,:,:]=vapr[:,::-1,:]
W1[:,:,:]=wapr[:,::-1,:]


rootgrp.close()

第一段代码


    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变量。试试吧。在

^{pr2}$

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

除了缺少close()调用之外,在任何地方都不能将值写入变量中。为此:

U1[:] = uAM
V1[:] = vAM
W1[:] = wAM

在Python中将一个变量设置为另一个变量时,例如:

^{pr2}$

你只是改变了名称U1所指的(点),实际上并没有改变U1指向的对象的任何内容。因此,在脚本的情况下,重置U1以指向数据数组,并且将丢失对所创建的NetCDF变量的引用。在

相关问题 更多 >