""" 1 - Concatenate models data ncrat cgcm2.2_tempsalt_*.nc /path_in_tupa/cgcm2.2_tempsalt.nc 2- Data from daily to monthly in some region! as a example, in NOAA DATA areas (TAO,PIRATA and RAMA - see: http://www.pmel.noaa.gov/tao/data_deliv/deliv.html) | | V """

In [1]:
%pylab inline
from numpy import *
import numpy as np
from numpy import ma
from netCDF4 import Dataset

import pylab
from pylab import *
import matplotlib.pyplot as plt
import pickle
from cStringIO import StringIO


Populating the interactive namespace from numpy and matplotlib

In [2]:
def nino34_timemean(path,var):
        from netCDF4 import Dataset
        f = Dataset(path)
        # extract the nearest point of NINO 3-4 in the model
        lon_min= np.abs(f.variables['xt_ocean'][:] -(-170)).argmin()
        lon_max= np.abs(f.variables['xt_ocean'][:] -(-120)).argmin()
        lat_min= np.abs(f.variables['yt_ocean'][:] -(-5)).argmin()
        lat_max= np.abs(f.variables['yt_ocean'][:] -(5)).argmin()
        #extract this points from the var chosen
        #v = f.variables['var'][:,0,lat_min:lat_max,lon_min:lon_max]
        v =[]
        for t in range(0,len(f.variables[var][:])):
            v.append(ma.mean(ma.masked_values(f.variables[var][t,0,lat_min:lat_max,lon_min:lon_max],f.variables[var].missing_value)))
        return v

In [3]:
def dipolo_timemean(path,var):
        from netCDF4 import Dataset
        f = Dataset(path)
        # extract the nearest point of NINO 3-4 in the model
        lon_minN= np.abs(f.variables['xt_ocean'][:] -(-55)).argmin()
        lon_maxN= np.abs(f.variables['xt_ocean'][:] -(-15)).argmin()
        lat_minN= np.abs(f.variables['yt_ocean'][:] -(5)).argmin()
        lat_maxN= np.abs(f.variables['yt_ocean'][:] -(25)).argmin()
        lon_minS= np.abs(f.variables['xt_ocean'][:] -(-30)).argmin()
        lon_maxS= np.abs(f.variables['xt_ocean'][:] -(10)).argmin()
        lat_minS= np.abs(f.variables['yt_ocean'][:] -(-20)).argmin()
        lat_maxS= np.abs(f.variables['yt_ocean'][:] -(0)).argmin()        
        #extract this points from the var chosen
        #v = f.variables['var'][:,0,lat_min:lat_max,lon_min:lon_max]
        vn,vs =[],[]
        for t in range(0,len(f.variables[var][:])):
            vn.append(ma.mean(ma.masked_values(f.variables[var][t,0,lat_minN:lat_maxN,lon_minN:lon_maxN],f.variables[var].missing_value)))
            vs.append(ma.mean(ma.masked_values(f.variables[var][t,0,lat_minS:lat_maxS,lon_minS:lon_maxS],f.variables[var].missing_value)))
        return vn,vs

In [4]:
def atlantic_timemean(path,var):
        from netCDF4 import Dataset
        f = Dataset(path)
        # extract the nearest point of NINO 3-4 in the model
        lon_minN= np.abs(f.variables['xt_ocean'][:] -(-40)).argmin()
        lon_maxN= np.abs(f.variables['xt_ocean'][:] -(8)).argmin()
        lat_minN= np.abs(f.variables['yt_ocean'][:] -(-20)).argmin()
        lat_maxN= np.abs(f.variables['yt_ocean'][:] -(20)).argmin()
 
        #extract this points from the var chosen
        #v = f.variables['var'][:,0,lat_min:lat_max,lon_min:lon_max]
        va =[]
        for t in range(0,len(f.variables[var][:])):
            va.append(ma.mean(ma.masked_values(f.variables[var][t,0,lat_minN:lat_maxN,lon_minN:lon_maxN],f.variables[var].missing_value)))
            
        return va

In [5]:
def indic_timemean(path,var):
        from netCDF4 import Dataset
        f = Dataset(path)
        # extract the nearest point of NINO 3-4 in the model
        lon_minN= np.abs(f.variables['xt_ocean'][:] -(55)).argmin()
        lon_maxN= np.abs(f.variables['xt_ocean'][:] -(100)).argmin()
        lat_minN= np.abs(f.variables['yt_ocean'][:] -(-25)).argmin()
        lat_maxN= np.abs(f.variables['yt_ocean'][:] -(15)).argmin()
 
        #extract this points from the var chosen
        #v = f.variables['var'][:,0,lat_min:lat_max,lon_min:lon_max]
        vi =[]
        for t in range(0,len(f.variables[var][:])):
            vi.append(ma.mean(ma.masked_values(f.variables[var][t,0,lat_minN:lat_maxN,lon_minN:lon_maxN],f.variables[var].missing_value)))
            
        return vi

In [6]:
def OBS_open(path,var):
        from netCDF4 import Dataset
        #data = 346         # TAO to start in 1980
        data = 82  # PIRATAto start in 1998
        #data=47       # RAMAto start 2001
        f = Dataset(path)
        v =[]
        jan,fev,mar,apr,may = [],[],[],[],[]
        jun,jul,aug,sep = [],[],[],[]
        octo,nov,dec = [],[],[]
        month = []
        for t in range(data,len(f.variables[var][:])):      
            v_mask =ma.masked_values(f.variables[var][t,:,:],1e+35)
            v.append(v_mask.mean())   # area mean
        
        for t in range(data,len(f.variables[var][:]),365): 
            month.append(np.mean(v[t:t+31]))
            if (t % 4 == 0):
                month.append(ma.mean(v[t+31:t+60]))
                month.append(ma.mean(v[t+60:t+91]))
                month.append(ma.mean(v[t+91:t+121]))
                month.append(ma.mean(v[t+121:t+152]))
                month.append(ma.mean(v[t+152:t+182]))
                month.append(ma.mean(v[t+182:t+213]))
                month.append(ma.mean(v[t+213:t+244]))
                month.append(ma.mean(v[t+244:t+274]))
                month.append(ma.mean(v[t+274:t+305]))
                month.append(ma.mean(v[t+305:t+335]))
                month.append(ma.mean(v[t+335:t+366]))
            else :
                month.append(ma.mean(v[t+31:t+59]))
                month.append(ma.mean(v[t+59:t+90]))
                month.append(ma.mean(v[t+90:t+120]))
                month.append(ma.mean(v[t+120:t+151]))
                month.append(ma.mean(v[t+151:t+181]))
                month.append(ma.mean(v[t+181:t+212]))
                month.append(ma.mean(v[t+212:t+243]))
                month.append(ma.mean(v[t+243:t+273]))
                month.append(ma.mean(v[t+273:t+304]))
                month.append(ma.mean(v[t+304:t+334]))
                month.append(ma.mean(v[t+334:t+365]))
        #for n,i in enumerate (month):
        #    if i == nan: 
        #        month[n] = mean(month)
        return month

In [7]:
#TAO = OBS_open('/path_in_tupa/sst_TAO_all.cdf',u'T_20')
#pickle.dump(TAO, open( "TAO.p", "wb" ) )

PIRATA = OBS_open('/path_in_tupa/sst_PIRATA_all.cdf',u'T_20')
pickle.dump(PIRATA, open( "PIRATA.p", "wb" ) )

#RAMA = OBS_open('/path_in_tupa/sst_RAMA_all.cdf',u'T_20')
#pickle.dump(RAMA, open( "RAMA.p", "wb" ) )

var = atlantic_timemean('/path_in_tupa/cgcm2.2_tempsalt.nc','temp') #remember step 1
pickle.dump(var, open( "var.p", "wb" ) )
var_open = pickle.load(open("var.p","rb"))


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-7-afa2509397af> in <module>()
      2 #pickle.dump(TAO, open( "TAO.p", "wb" ) )
      3 
----> 4 PIRATA = OBS_open('/path_in_tupa/sst_PIRATA_all.cdf',u'T_20')
      5 pickle.dump(PIRATA, open( "PIRATA.p", "wb" ) )
      6 

<ipython-input-6-a3c01feb1f26> in OBS_open(path, var)
      4         data = 82  # PIRATAto start in 1998
      5         #data=47       # RAMAto start 2001
----> 6         f = Dataset(path)
      7         v =[]
      8         jan,fev,mar,apr,may = [],[],[],[],[]

/scratchin/prod/mbscg/home/mbscg/software/anaconda_1.4/lib/python2.7/site-packages/netCDF4.so in netCDF4.Dataset.__init__ (netCDF4.c:19148)()

RuntimeError: No such file or directory

In [ ]: