Example #1
0
import pynacolada as pcd
from sciproc import steinalp, dtrange
from Scientific.IO import NetCDF
import datetime as dt
import numpy as np

# purpose: 'advanced' example of making a Stein-Alpert decomposition from an monthly-mean averaged cycle (only on the 2 lowest model layers)

def avgcycle(x,n):
   xout = np.zeros([n+1]+list(np.shape(x)[1:]))
   for i in range(n):
      xout[i] = np.mean(x[i::n],axis=0)
   xout[n] = xout[0]
   return xout

dts = dtrange(dt.datetime.strptime('2009040100','%Y%m%d%H'),dt.datetime.strptime('2009050100','%Y%m%d%H'),dt.timedelta(hours=1))

for RUN in ['aq09ec_td','aq09_td','aq09ec','aq09']:
    path = '/media/URB_AQ_1/data/belgium_aq/rcm/'+RUN+'/stage1/aurora/au3d/'
    fins = [ path+'/au3d'+time.strftime('%Y%m%d%H')+'.nc' for time in dts]
    fout = NetCDF.NetCDFFile('/media/URB_AQ_1/data/belgium_aq/rcm/'+RUN+'_au3d200904_diucycle.nc','w')
    
    pcd.pcd(lambda x: avgcycle(x,24),('time',),\
            [{'file': fins, 'varname': 'O3', 'predim': 'time'}],\
            [{'file': fout, 'varname': 'O3'}],appenddim=True)
    
    fout.close(); print('data written to: ',fout)


    fin = NetCDF.NetCDFFile('/media/URB_AQ_1/data/belgium_aq/rcm/'+RUN+'_au3d200904_diucycle.nc','r')
    fout = NetCDF.NetCDFFile('/media/URB_AQ_1/data/belgium_aq/rcm/'+RUN+'_au3d200904_diucycle_spatvar.nc','w')
Example #2
0
fout = NetCDF.NetCDFFile(fnout,'w')
datout = [{'file': fout, \
           'varname': 'u'}]
func = lambda U,V: np.array([np.mean(np.sqrt(U**2+V**2),axis=0 )])
dnamsel = ['level',]
pcd.pcd(func,dnamsel,datin,datout,appenddim=True)
# Note also that the calculation is not entirely correct because the U and V loacations are shifted by 1/2-gridcell!

fout.close()
fin.close()


print("example 4: now we come to it's real power. Do the same as before, but for an hourly time series. ")

# the timeseries: just a list of all the files in the right order
dts = sp.dtrange(dt.datetime(2002,5,1),dt.datetime(2002,7,1),dt.timedelta(hours=1))
path = '/media/URB_UIP_2/data/rcm/0.009_20020101_berlin/cclm/out01/'
dtfiles = [path+'lffd'+dt.datetime.strftime(e,"%Y%m%d%H")+'.nc' for e in dts]

datin =  [{'file': dtfiles, \
           'varname': 'U', \
           'dsel': {'level' : range(30,40,1)}, \
           'daliases': { 'srlat':'rlat', 'srlon':'rlon' },\
          },\
          {'file': dtfiles, \
           'varname':'V', \
           'dsel': {'level' : range(30,40,1)},
           'daliases': { 'srlat':'rlat', 'srlon':'rlon' },\
           }\
         ]