2015-02-11 51 views
1

我想保存一個氣候文件,以便每次需要計算異常時,我都不必再運行氣候學腳本(並且需要大量時間!)。 的文件顯然以這種方式「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)。

平均法或賦值錯誤?或兩者?

+0

是尺寸'uwnd'級x x時間緯度X經度?如果是這樣,「時間」的大小是多少?看起來你正在閱讀每月的意思......它只是一個大小? – N1B4 2015-02-12 18:02:29

+0

文件的尺寸是[時間,等級,緯度,經度]。上面添加了文件示例(此問題的編輯版本)。月平均值以6小時的平均值計算。類似於:1998年4月0000,0600,1200和1800年的平均值。 – 2015-02-12 23:26:46

+0

我建議查看[xarray](http://xarray.pydata.org)進行這種操作。 – gerrit 2017-01-31 12:51:37

回答

2

這第一段代碼,

 

    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方法關閉氣候文件?

+0

Hi @Favo。它給出了同樣的錯誤。 「指數太多」。 – 2015-02-11 22:51:46

+0

我忘了關閉方法。但是,即使我這樣做,它仍然無法正常工作。 – 2015-02-11 23:22:26

2

除了缺少close()調用,您還沒有向變量寫入值。要做到這一點:

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

當您設置一個變量等於另一個在Python,如:

U1 = uAM 

你只是改變什麼名字U1指(分) - you're實際上並沒有改變U1指向的對象。因此,在腳本的情況下,您將U1重置爲指向數據數組,並且您將失去對所創建的NetCDF變量的引用。

+0

我只是編輯(和測試)與「關閉()」添加。沒有運氣。 – 2015-02-12 23:03:17

+0

以及建議的方法。沒有運氣。我還做了其他一些選擇,無論是空文件還是錯誤(太多索引)都被返回。 – 2015-02-12 23:09:14

+1

'uAM'是多維的,您仍然需要在作業中包含正確數目的括號。 'U1 [:,:,:] = uAM' – N1B4 2015-02-13 01:16:29

1

經過一些建議並重新開始編碼之後,我設法通過使用下面的腳本來獲得期望的結果。雖然,如何讓LON緯度值堅守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() 
+0

geez!結果與我手動完成的結果不同(從上面生成的一個氣候文件與我在Python中手動創建的文件進行比較)。 – 2015-02-14 16:15:54

相關問題