[英]Combining a large amount of netCDF files
我有一個netCDF(.nc)文件的大文件夾,每個文件都有一個相似的名字。 數據文件包含時間,經度,緯度和月降水量的變量。 目標是使每個月的平均月降水量超過X年。 因此,最后我將得到12個值,表示每個緯度和長度的X年平均月降水量。 多年來,每個文件都是同一個位置。 每個文件以相同的名稱開頭,以“date.sub.nc”結尾,例如:
'data1.somthing.somthing1.avg_2d_Ind_Nx.200109.SUB.nc'
'data1.somthing.somthing1.avg_2d_Ind_Nx.200509.SUB.nc'
'data2.somthing.somthing1.avg_2d_Ind_Nx.201104.SUB.nc'
'data2.somthing.somthing1.avg_2d_Ind_Nx.201004.SUB.nc'
'data2.somthing.somthing1.avg_2d_Ind_Nx.201003.SUB.nc'
'data2.somthing.somthing1.avg_2d_Ind_Nx.201103.SUB.nc'
'data1.somthing.somthing1.avg_2d_Ind_Nx.201203.SUB.nc'
結局是YearMonth.SUB.nc到目前為止我所擁有的是:
array=[]
f = nc.MFDataset('data*.nc')
precp = f.variables['prectot']
time = f.variables['time']
array = f.variables['time','longitude','latitude','prectot']
我得到一個KeyError :('時間','經度','緯度','prectot')。 有沒有辦法結合所有這些數據,所以我能夠操縱它?
正如@CharlieZender所提到的那樣, ncra
是ncra
的方式,我將提供有關將該函數集成到Python腳本中的更多細節。 (PS - 您可以使用Homebrew輕松安裝NCO,例如http://alejandrosoto.net/blog/2014/01/22/setting-up-my-mac-for-scientific-research/ )
import subprocess
import netCDF4
import glob
import numpy as np
for month in range(1,13):
# Gather all the files for this month
month_files = glob.glob('/path/to/files/*{0:0>2d}.SUB.nc'.format(month))
# Using NCO functions ---------------
avg_file = './precip_avg_{0:0>2d}.nc'.format(month)
# Concatenate the files using ncrcat
subprocess.call(['ncrcat'] + month_files + ['-O', avg_file])
# Take the time (record) average using ncra
subprocess.call(['ncra', avg_file, '-O', avg_file])
# Read in the monthly precip climatology file and do whatever now
ncfile = netCDF4.Dataset(avg_file, 'r')
pr = ncfile.variables['prectot'][:,:,:]
....
# Using only Python -------------
# Initialize an array to store monthly-mean precip for all years
# let's presume we know the lat and lon dimensions (nlat, nlon)
nyears = len(month_files)
pr_arr = np.zeros([nyears,nlat,nlon], dtype='f4')
# Populate pr_arr with each file's monthly-mean precip
for idx, filename in enumerate(month_files):
ncfile = netCDF4.Dataset(filename, 'r')
pr = ncfile.variable['prectot'][:,:,:]
pr_arr[idx,:,:] = np.mean(pr, axis=0)
ncfile.close()
# Take the average along all years for a monthly climatology
pr_clim = np.mean(pr_arr, axis=0) # 2D now [lat,lon]
NCO這樣做
ncra *.01.SUB.nc pcp_avg_01.nc
ncra *.02.SUB.nc pcp_avg_02.nc
...
ncra *.12.SUB.nc pcp_avg_12.nc
ncrcat pcp_avg_??.nc pcp_avg.nc
當然,前12個命令可以使用Bash循環完成,將總行數減少到小於5。 如果您更喜歡使用python編寫腳本,可以使用它來檢查您的答案。 ncra docs 在這里 。
命令ymonmean計算CDO中日歷月的平均值。 因此,任務可以分為兩行:
cdo mergetime data*.SUB.nc merged.nc # put files together into one series
cdo ymonmean merged.nc annual_cycle.nc # mean of all Jan,Feb etc.
cdo還可以計算其他統計數據的年度周期,ymonstd,ymonmax等......時間單位可以是天數或五進制數以及月數。 (例如ydaymean)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.