def load_BCs(fields, season=None, timesel='0011-01-01,0011-12-31'):

    simcbase='kemctl1'
    simpbase='kem1pert2'
    obsbase = 'kemnsidc'

    flddat={}; fldcdat={}; fldpdat={}; sicnpdat={}
    for fii,field in enumerate(fields):
        fnamesc={}; fnamesp={}
        flddiff={}; fldcavg={}; fldpavg={}

        print field
        # Loop through sim data:
        # load all 5 sim BCs
        for ii in range(0,6):

            # do obs data at ii=5
            if ii==5:
                simname=obsbase+'ctl'
                fnamec = con.getBCfilenames(field,sim=simname)
                print fnamec
                simname=obsbase+'pert'
                fnamep = con.getBCfilenames(field,sim=simname)
            else:
                simname=simcbase+'r'+str(ii+1)
                fnamec = con.getBCfilenames(field,sim=simname)
                print fnamec
                simname=simpbase+'r'+str(ii+1)
                fnamep = con.getBCfilenames(field,sim=simname)

            fldc = cnc.getNCvar(fnamec,field.upper(),timesel=timesel)
            fldp = cnc.getNCvar(fnamep,field.upper(),timesel=timesel)

            if field=='sicn':
                # save pert data to mask gt
                if season==None:
                    sicnpdat[ii] = fldp
                else:
                    sicnpdat[ii] = cutl.seasonalize_monthlyts(fldp,season=sea,climo=1)

            if season==None:
                flddiff[ii] = fldp-fldc
                fldcavg[ii] = fldc
                fldpavg[ii] = fldp
            else:
                flddiff[ii] = cutl.seasonalize_monthlyts(fldp-fldc,season=sea,climo=1) 
                fldcavg[ii] = cutl.seasonalize_monthlyts(fldc,season=sea,climo=1) 
                fldpavg[ii] = cutl.seasonalize_monthlyts(fldp,season=sea,climo=1) 

            fnamesc[ii]=fnamec
            fnamesp[ii]=fnamep

        flddat[field] = flddiff
        fldcdat[field] = fldcavg
        fldpdat[field] = fldpavg

    return flddat, fldcdat, fldpdat, sicnpdat
def load_pifield(fdict,seas,conv=1,subsampyrs=11,numsamp=50,
                 styear=None,anomyears=None,local=False,detrend=True,
                 verb=False,addcyc=False):
    """ TAKEN FROM load_canesmfield() in canesm_LE_composite.py

        loads subsampled CGCM data from specified simulation 
             (assumes a long control, e.g. piControl)

             number of total chunks will be determined by length of sim data
                      and numyrs (ntime / numyrs). First the simulation is chunked into
                      numyrs segments. Then 2 segments at a time are randomly chosen
                      (at least a decade apart) to generate anomalies. This is done 
                      numsamp times (e.g. 50)

              returns subsamp  (subsampled anomalies)
                      styears  (start index of chunking of long run)
                      anomyears (indices of anomaly differences)
    """
    casename='piControl'

    pidat = lcd.load_data(fdict,casename,local=local,conv=conv,verb=verb)
    piseadat = cutl.seasonalize_monthlyts(pidat,season=seas)

    if detrend:
        piseadat = cutl.detrend(piseadat,axis=0)

    # the data must be seasonalized before using this func.
    pisea,styear,anomyears = leg.subsamp_anom_pi(piseadat, numyrs=subsampyrs,numsamp=numsamp,
                                                 styear=styear,anomyears=anomyears)
    if addcyc:
        st=pisea[...,-1]
        pisea=np.dstack((pisea,st[...,None]))

    return pisea,styear,anomyears
def load_field(fdict,casename,timesel,seas,ftype='fullts',conv=1,local=False,verb=False):
    
    """ TAKEN FROM load_field() in canesm_le_composite.py

        returns [numens x space.flat] or [numens]

    """

    ledat = le.load_LEdata(fdict,casename,timesel=timesel, 
                           rettype='ndarray',conv=conv,ftype=ftype,local=local,verb=verb)

    # time needs to be first dimension
    try:
        if ledat.ndim==2:
            ledat = ledat.T
        elif ledat.ndim==3:
            ledat = np.transpose(ledat,(1,0,2))
        else:
            print 'Loaded data is not 2 or 3 dimensions. Do not understand.'
            raise Exception
    except:
        raise

    lesea = cutl.seasonalize_monthlyts(ledat,season=seas).mean(axis=0)  # numens x space.flat

    return lesea
def load_field(fdict,casename,timesel,seas,ftype='fullts',conv=1,local=False,verb=False):
    
    """ 
        returns [numens x space.flat] or [numens]

    """

    ledat = le.load_LEdata(fdict,casename,timesel=timesel, 
                           rettype='ndarray',conv=conv,ftype=ftype,local=local,verb=verb)

    print '@@@ ledat.shape ' + str(ledat.shape) # why does the 3d data have space flattened already...

    # time needs to be first dimension
    try:
        if ledat.ndim==2:
            ledat = ledat.T
        elif ledat.ndim==3:
            ledat = np.transpose(ledat,(1,0,2))
        else:
            print 'Loaded data is not 2 or 3 dimensions. Do not understand.'
            raise Exception
    except:
        raise

    lesea = cutl.seasonalize_monthlyts(ledat,season=seas).mean(axis=0)  # numens x space.flat

    return lesea
Example #5
0
def plotvert_allseas(fld, lev, lat,title='',units='',cmap='blue2red_w20',ptype=None,
                      cmin='',cmax='',axis=None, suppcb=0,climo=0,
                      pvals = None,sigtype='hatch',latlim=None,levlim=None,
                      addcontlines=True,screen=False):

    """ default seasons = 'SON','DJF','MAM','JJA'
    """
    import cccmautils as cutl
    
    seasons = 'SON','DJF','MAM','JJA'

    midx=0
    fig,axs = plt.subplots(1,4) 
    fig.set_size_inches(12,4)
    fig.subplots_adjust(hspace=.15,wspace=.05)

    for axii,ax in enumerate(axs.flat):
        
        #print seasons[midx]
        tmpfld=np.squeeze(cutl.seasonalize_monthlyts(fld,season=seasons[midx],climo=climo))
        #print tmpfld.shape # already 2D... only when climo=1?
        #if seasons[midx]=='DJF':
        #    print tmpfld

        if climo:
            plotfld = tmpfld
        else:
            plotfld = np.mean(tmpfld,axis=0)

        #print plotfld.shape

        if axii==0:
            vpparams = dict(cmin=cmin,cmax=cmax,cmap=cmap,ptype=ptype,
                            axis=ax,suppcb=True,units=units,
                            latlim=latlim,levlim=levlim,
                            addcontlines=addcontlines,screen=screen)
        else:
            vpparams = dict(cmin=cmin,cmax=cmax,cmap=cmap,ptype=ptype,
                            axis=ax,suppcb=True,units=units,
                            latlim=latlim,levlim=levlim,
                            addcontlines=addcontlines,screen=screen,
                            suppylab=True)
            
        cf = vert_plot(plotfld,lev,lat,**vpparams)
        
        ax.set_title(seasons[midx])
        if pvals != None:
            #addtsig(cf,pvals,lat,lon,sigtype=sigtype) # @@@ untested? no lat... 5/10/15
            addtsig(cf,pvals,lat,lev,sigtype=sigtype) 

        midx = midx+1

    cbar_ax = fig.add_axes([.91,.25, .02,.5])
    fig.colorbar(cf,cax=cbar_ax)
    plt.suptitle(title)

    return fig
Example #6
0
def map_allseas(fld, lat, lon,title='',units='',cmap='blue2red_w20',ptype='sq',
           cmin='',cmax='',axis=None, suppcb=0,lmask=0,climo=0,flipmask=0,
                  pvals = None,sigtype='hatch',conts=None,latlim=None):

    """ default seasons = 'SON','DJF','MAM','JJA'
    """
    import cccmautils as cutl
    
    seasons = 'SON','DJF','MAM','JJA'

    midx=0
    fig,axs = plt.subplots(1,4) 
    fig.set_size_inches(12,3)
    fig.subplots_adjust(hspace=.15,wspace=.05)

    for ax in axs.flat:
        
        #print seasons[midx]
        tmpfld=np.squeeze(cutl.seasonalize_monthlyts(fld,season=seasons[midx],climo=climo))
        #print tmpfld.shape # already 2D... only when climo=1?
        #if seasons[midx]=='DJF':
        #    print tmpfld

        if climo:
            plotfld = tmpfld
        else:
            plotfld = np.mean(tmpfld,axis=0)

        #print plotfld.shape
        
        bm,pc = kemmap(plotfld,lat,lon,cmin=cmin,cmax=cmax,cmap=cmap,ptype=ptype,\
                       axis=ax,suppcb=1,lmask=lmask,flipmask=flipmask,units=units,latlim=latlim)
        ax.set_title(seasons[midx])
        if pvals != None:
            addtsigm(bm,pvals,lat,lon,sigtype=sigtype)

        if conts != None:
            # add specified contour(s)
            lons, lats = np.meshgrid(lon,lat)
            bm.contour(lons,lats,plotfld,[conts],colors='k',linewidths=2,latlon=True)

        midx = midx+1

    cbar_ax = fig.add_axes([.91,.25, .02,.5])
    fig.colorbar(pc,cax=cbar_ax)
    plt.suptitle(title)

    return fig
    )  # time start year to end
else:
    fldc = ncfilec.variables[ncfield][...] * conv
    fldp1 = ncfilep1.variables[ncfield][...] * conv
    fldp2 = ncfilep2.variables[ncfield][...] * conv  # time start year to end
    fldp3 = ncfilep3.variables[ncfield][...] * conv  # time start year to end


nt, nlev, nlat = fldc.shape  # if nt == 12 then it's a climo

#  ############# set which perturbation run! ######
casenamep = casenamep2
fldp = fldp2
#  ###############################################

seasfldc = np.squeeze(cutl.seasonalize_monthlyts(fldc, timeavg, climo=climo))
seasfldp = np.squeeze(cutl.seasonalize_monthlyts(fldp, timeavg, climo=climo))

print seasfldc.shape


lats, levs = np.meshgrid(lat, lev)

tstat, pval = sp.stats.ttest_ind(seasfldp, seasfldc, axis=0)
print tstat.shape
print timeavg
pval = ma.masked_invalid(pval)  # pcolormesh needs masked_array

#   SEASONAL MEAN CLIMO (CONTROL)
plotfld = np.average(seasfldc, 0)
        
    fldczmsea = np.zeros((len(seasons),len(lat)))
    fldp1zmsea = np.zeros((len(seasons),len(lat)))
    fldp2zmsea = np.zeros((len(seasons),len(lat)))
    fldp3zmsea = np.zeros((len(seasons),len(lat)))

    tstatp1zmsea = np.zeros((len(seasons),len(lat)))
    pvalp1zmsea = np.zeros((len(seasons),len(lat)))
    tstatp2zmsea = np.zeros((len(seasons),len(lat)))
    pvalp2zmsea = np.zeros((len(seasons),len(lat)))
    tstatp3zmsea = np.zeros((len(seasons),len(lat)))
    pvalp3zmsea = np.zeros((len(seasons),len(lat)))
    
    # seasonalize first, then zonal mean
    for sidx in range(0,len(seasons)):
        fldczm  = np.mean(cutl.seasonalize_monthlyts(fldc,season=seasons[sidx]), axis=2) # zm with time dim
        fldp1zm = np.mean(cutl.seasonalize_monthlyts(fldp1,season=seasons[sidx]), axis=2)
        fldp2zm = np.mean(cutl.seasonalize_monthlyts(fldp2,season=seasons[sidx]), axis=2)
        fldp3zm = np.mean(cutl.seasonalize_monthlyts(fldp3,season=seasons[sidx]), axis=2)

        tstatp1zm,pvalp1zm = sp.stats.ttest_ind(fldp1zm,fldczm,axis=0)
        tstatp2zm,pvalp2zm = sp.stats.ttest_ind(fldp2zm,fldczm,axis=0)
        tstatp3zm,pvalp3zm = sp.stats.ttest_ind(fldp3zm,fldczm,axis=0)

        # now get the seasonal average (season x lat)
        fldczmsea[sidx,:] = np.mean(fldczm,axis=0)
        fldp1zmsea[sidx,:] = np.mean(fldp1zm,axis=0)
        fldp2zmsea[sidx,:] = np.mean(fldp2zm,axis=0)
        fldp3zmsea[sidx,:] = np.mean(fldp3zm,axis=0)

        pvalp1zmsea[sidx,:] = pvalp1zm
Example #9
0
         cutl.annualize_monthlyts(nfldc),color=dodgerblue,linewidth=2)      
plt.xlim(xlims)
plt.title('ANN NH SIA anom from 1979-89')
plt.grid()
if printtofile:
    plt.savefig('CanESMens_OBSanom' + ctimstr + '_' + cfield + '_ANN_timeseries' + pstr + '.pdf')



#   MINIMUM (SEPT)
plt.figure()
cii=0.25
#mosel=12; mostr='Dec'
mosel=9; mostr='Sep'
for ii in xrange(0,5):
    plotfld = cutl.seasonalize_monthlyts(cfldpall[ii,:],mo=mosel)
    plt.plot(years,plotfld,color=str(cii))
    if zoom:
        slope, intercept, r_value, p_value, std_err = sp.stats.linregress(hyears,plotfld[129:])
        plt.plot(years,slope*years+intercept,color=str(cii))
    cii=cii+0.15

plotfld = cutl.seasonalize_monthlyts( np.mean(cfldpall,axis=0),mo=mosel )
plt.plot(years,plotfld,color='k',linewidth=2)
if zoom:
    slope, intercept, r_value, p_value, std_err = sp.stats.linregress(hyears,plotfld[129:])
    plt.plot(years,slope*years+intercept,color='k',linewidth=2)

if afield=='sicn':
    plotfld=cutl.seasonalize_monthlyts(hfldp,mo=mosel)
    plt.plot(hyears,plotfld,color='green',linewidth=2)
    fname=fdict[fkey]
    conv=condt[fkey] 

    fld=cnc.getNCvar(fname,ncfield,timesel=timesel)*conv
    fld=clear_belowsfc(fld, sfcp, lev)

    print fname + ', fld.shape ' + str(fld.shape) # @@@

    fldint=np.zeros(len(lat))
    fldintza=np.zeros(len(lat))
    latidx=0
    for ll in lat: # do all lats
        #latrad = np.deg2rad(ll)

        fldsub = np.squeeze(fld[:,:,latidx,:-1]) # get just one lat and remove extra lon
        fldtm = np.mean(cutl.seasonalize_monthlyts(fldsub,**seasonalizedt),axis=0) # time mean
    
        #print fldtm

        #fldint[latidx] = calc_PHT(fldtm, latidx, nlon, dp, calc=calctype)
        fldint[latidx] = calc_PHT3(fldtm, ll, nlon, dp, vcalc=calctype, zcalc='zonint') # zonal integration
        fldintza[latidx] = calc_PHT3(fldtm, ll, nlon, dp, vcalc=calctype, zcalc='zonavg') # zonal average

        latidx += 1
    
    fldintdt[fkey] = fldint
    fldintzadt[fkey] = fldintza

flddf = pd.DataFrame(fldintdt)
fldtot = flddf.sum(axis=1)
fldzadf = pd.DataFrame(fldintzadt)
Example #11
0
        lefield1='zg50000.00'; lencfield1='zg'; comp1='Amon'; leconv1=1 #region1='bksmori'
        lefield2='tas'; lencfield2='tas'; comp2='Amon'; #region2='eurasiamori'
        fdict1 = {'field': lefield1+region1, 'ncfield': lencfield1, 'comp': comp1}
        fdict2 = {'field': lefield2+region2, 'ncfield': lencfield2, 'comp': comp2}


        ftype='fullts' # 'fullclimo' or 'climo' or 'fullts'
        lefdict1 = {'field': lefield1+region1, 'ncfield': lencfield1, 'comp': comp1}
        lefdict2 = {'field': lefield2+region2, 'ncfield': lencfield2, 'comp': comp2}

        # historical
        lecdat1 = le.load_LEdata(fdict1,'historical',timesel=timeselc, rettype='ndarray',conv=leconv1,ftype=ftype)
        (numens1,ntime1) = lecdat1.shape
        lepdat1=le.load_LEdata(fdict1,'historical',timesel=timeselp, rettype='ndarray',conv=leconv1,ftype=ftype)
        lecsea1 = cutl.seasonalize_monthlyts(lecdat1.T,season=sea1).T
        lepsea1 = cutl.seasonalize_monthlyts(lepdat1.T,season=sea1).T
        lefld1=lepsea1.mean(axis=1)-lecsea1.mean(axis=1)

        lecdat2 = le.load_LEdata(fdict2,'historical',timesel=timeselc, rettype='ndarray',conv=conv2,ftype=ftype)
        (numens2,ntime2) = lecdat1.shape
        lepdat2=le.load_LEdata(fdict2,'historical',timesel=timeselp, rettype='ndarray',conv=conv2,ftype=ftype)
        lecsea2 = cutl.seasonalize_monthlyts(lecdat2.T,season=sea2).T
        lepsea2 = cutl.seasonalize_monthlyts(lepdat2.T,season=sea2).T
        lefld2=lepsea2.mean(axis=1)-lecsea2.mean(axis=1)

        obs=plt.scatter(erareg.mean(),gisreg.mean(),color='blue',marker='s',s=8**2)

        ledat=plt.scatter(lefld1,lefld2,color=ccm.get_linecolor('darkolivegreen3'),marker='*',s=5**2,alpha=0.5)
        lemm, lebb, lerval, lepval, lestd_err = sp.stats.linregress(lefld1,lefld2)
        axylims = ax.get_ylim()
Example #12
0
             superslopes[monii,superii] = slope[eii]
             thismonskey = thismonskey + (mod,)
             #superkeys[monii,superii] = mod
             superii=superii+1
         
         modtrenddt[mod] = slope # trends for e/ ens member in the model group
         mntrnd = np.min(slope)
         mxtrnd = np.max(slope)
         avgtrnd = np.mean(slope)
         mndt[mod] = mntrnd
         mxdt[mod] = mxtrnd
         avgdt[mod] = avgtrnd

     # do obs e/ month
     homon = cutl.seasonalize_monthlyts(had,mo=monii+1)
     xx = np.arange(0,homon.shape[0])
     hslope, intercept, r_value, p_value, std_err = sp.stats.linregress(xx,homon)
     hmontrenddt[mo] = hslope
     
     nomon = cutl.seasonalize_monthlyts(nsidc,mo=monii+1)
     xx = np.arange(0,nomon.shape[0])
     nslope, intercept, r_value, p_value, std_err = sp.stats.linregress(xx,nomon)
     nmontrenddt[mo] = nslope

     superkeys = superkeys + (thismonskey,)
     montrenddt[mo] = modtrenddt
     monmndt[mo]= mndt
     monmxdt[mo]= mxdt
     monavgdt[mo] = avgdt
    flddiff=flddat[field]

    for ii in range(0,6): # 6 sims

        if len(fields)==1:
            ax=axs.flatten()[ii]

        else:
            if fliptohoriz:
                ax=axs[fii,ii]
            else:
                ax=axs[ii,fii]

        plotfld=flddiff[ii]*conv
        plotfld = cutl.seasonalize_monthlyts(plotfld,season=sea, climo=1)

        if field=='gt' and masksicn:
            tmp = sicnpdat[ii]
            tmp = cutl.seasonalize_monthlyts(tmp,season=sea, climo=1)
            plotfld[tmp>=0.15] = 0 # mask where model sees grid cell as sea ice (not SST)

        bm,pc = cplt.kemmap(plotfld, lat, lon, ptype='nheur',
                            latlim=latlim, axis=ax, suppcb=True, 
                            round=False,lcol=pparams['lcol'], coastres= 'c', 
                            coastwidth= 0.5, area_thresh=70000,
                            lmask=True,**metap)

        if len(fields)==1:
            ax.set_title(ylabs[ii],fontsize=fsz)
        else: