def map_allmonths(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): months = con.get_mon() midx=0 fig, spax = plt.subplots(2,6) fig.set_size_inches(12,4.5) # changed from 12,6 on 4/24/14. see how it saves if latlim!=None: fig.subplots_adjust(hspace=0.05,wspace=0.05) else: fig.subplots_adjust(hspace=0,wspace=0) for ax in spax.flat: if climo == 0: monfld = fld[midx::12,...] plotfld = np.mean(monfld,axis=0) else: plotfld = fld[midx,...] #tstat,pval = sp.stats.ttest_ind(monfldp,monfldc,axis=0) #sigs[midx,:,:] = ma.masked_where(pval>0.05,pval) 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(months[midx]) if pvals != None: addtsigm(bm,pvals[midx,...],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) # or do bm.colorbar.... plt.suptitle(title) return fig
fig.savefig( prfield + "diffsig" + sigtype + "_" + casenamep + "_v_" + casename + "_timexlat_seas_nh_v2." + suff ) else: fig.savefig( prfield + "diffsig" + sigtype + "_" + casenamep + "_v_" + casename + "_timexlat_seas_nh." + suff ) if seacycle: # want month x lat (or height) cmlen = float(plt.cm.get_cmap(cmap).N) # or: from __future__ import division incr = (cmaxm - cminm) / (cmlen) conts = np.arange(cminm, cmaxm + incr, incr) months = con.get_mon() if threed: print "fix 3D vars to use premade files @@" fldc = np.append( cnc.getNCvar(fnamec, ncfield, timesel=timesel, levsel=level) * conv, cnc.getNCvar(fnamec2, ncfield, levsel=level) * conv, axis=0, ) fldp = np.append( cnc.getNCvar(fnamep, ncfield, timesel=timesel, levsel=level) * conv, cnc.getNCvar(fnamep2, ncfield, levsel=level) * conv, axis=0, ) else: if field == "turb":
def plot_pattcorrs(pcdf, pcdf2=None, rmin=None, axis=None, tftype='seasonal'): """ plot_pattcorrs(pcdf, axis): this figure plots range of pattern correlations as bars, with mean patt corr marker, plus % common variation annotated. pcdf: DataFrame of pattern correlations pcdf2: second set of data to plot rmin: minimum r value (pattern correlation) that is significant for the data axis: plot axis tftype: time frequency type: 'seasonal' or 'monthly' """ import math as math if tftype=='seasonal': numt=4 seasons=('SON','DJF','MAM','JJA') else: print 'ONLY SEASONAL IMPLEMENTED @@ 11/25/14' numt=12 # monthly seasons=con.get_mon() # within ensemble pattern correlation combinations # number of combinations: n choose k (here k=2) # n! / (k! *(n-k)!) ncomb = math.factorial(len(pcdf)) / (2 * math.factorial(len(pcdf)-2) ) # first, create matrix of data to take mean, max, min, etc pcstack = np.zeros((ncomb,numt)) # ncomb combos x 4 seasons (or 12 mon) stackii=0 keys=pcdf.keys() for eii,skey1 in enumerate(keys): for ineii,skey2 in enumerate(keys[eii+1:len(keys)]): # stack the pc's so I can take max/min, etc pcstack[stackii] = pcdf[skey1][skey2] stackii+=1 mxsea = np.max(pcstack,axis=0) mnsea = np.min(pcstack,axis=0) avgsea = np.mean(pcstack,axis=0) tmpstack=copy.copy(pcstack) tmpstack[pcstack<0] = 0 # get rid of negative correlations. they are zero for all intents & purposes in this case pcstacksq = np.power(tmpstack,2) # square the patt corr (the ones that are positive) avgseasq = np.mean(pcstacksq,axis=0) # average squared patt corr (coef of determination) if not pcdf2.empty: ncomb2 = math.factorial(len(pcdf2)) / (2 * math.factorial(len(pcdf2)-2) ) pcstack2 = np.zeros((ncomb2,numt)) # ncomb combos x 4 seasons stackii=0 keys=pcdf2.keys() for eii,skey1 in enumerate(keys): for ineii,skey2 in enumerate(keys[eii+1:len(keys)]): # stack the pc's so I can take max/min, etc pcstack2[stackii] = pcdf2[skey1][skey2] stackii+=1 mxsea2 = np.max(pcstack2,axis=0) mnsea2 = np.min(pcstack2,axis=0) avgsea2 = np.mean(pcstack2,axis=0) tmpstack2=copy.copy(pcstack2) tmpstack2[pcstack2<0] = 0 # get rid of negative correlations. they are zero for all intents & purposes in this case pcstacksq2 = np.power(tmpstack2,2) # square the patt corr (the ones that are positive) avgseasq2 = np.mean(pcstacksq2,axis=0) # average squared patt corr (coef of determination) # add annual mean annwgts = np.array((91,90,92,92))/365. print annwgts #fig,axs = plt.subplots(1,1) #fig.set_size_inches(6,4) # more squat ax = axis #[0] legtpl=() wi=0.1 # width of bar incr=0.3 # how much to shift in b/w the 2 sets of data xxsea2=np.arange(1,6) xboxmin=xxsea2-.2 if rmin!=None: ax.axhspan(-1*rmin,rmin,color='orange',alpha=.3) # shade where corr is NOT significant fillcol='0.7' fillcol2=ccm.get_linecolor('dodgerblue') for boxii in range(0,4): # loop through seasons # shaded bars/boxes ax.fill_between((xboxmin[boxii],xboxmin[boxii]+wi),mnsea[boxii],mxsea[boxii],color=fillcol, alpha=.5) if not pcdf2.empty: ax.fill_between((xboxmin[boxii]+incr,xboxmin[boxii]+incr+wi),mnsea2[boxii], mxsea2[boxii],color=fillcol2, alpha=.5) # markers ax.plot(xboxmin[boxii]+wi/2.,avgsea[boxii],color='k',marker='_',linestyle='none',markersize=15)#,alpha=.7) if not pcdf2.empty: ax.plot(xboxmin[boxii]+wi/2.+incr,avgsea2[boxii],color='b',marker='_',linestyle='none',markersize=15)#,alpha=.7) # mean values (text) val = '$%.0f$'%(avgseasq[boxii]*100) # @@ square the corrs before taking mean ax.annotate(val+'%', xy=(xboxmin[boxii]+wi/2. -.09, .95), xycoords='data') if not pcdf2.empty: val = '$%.0f$'%(avgseasq2[boxii]*100) # @@ square the corrs before taking mean ax.annotate(val+'%', xy=(xboxmin[boxii]+wi/2.+incr -.06, .95), xycoords='data') boxii=boxii+1 # add annual mean ----------- annwgtst = np.tile(annwgts,(len(pcdf),1)) # tile # each ens w/ each other # pcstacksq is ncomb x numt annwgtst = np.tile(annwgts,(ncomb,1)) # tile ann = np.average(pcstack,weights=annwgts,axis=1) # ann mean per patt corr annmax = np.max(ann) annmin = np.min(ann) avgann = np.mean(ann) annsq = np.average(pcstacksq,weights=annwgts,axis=1) # ann mean per patt corr avgannsq = np.mean(annsq) if not pcdf2.empty: # add annual mean annwgtst = np.tile(annwgts,(len(pcdf2),1)) # tile # each ens w/ each other # pcstacksq is ncomb x numt annwgtst = np.tile(annwgts,(ncomb2,1)) # tile ann = np.average(pcstack2,weights=annwgts,axis=1) # ann mean per patt corr annmax2 = np.max(ann) annmin2 = np.min(ann) avgann2 = np.mean(ann) annsq2 = np.average(pcstacksq2,weights=annwgts,axis=1) # ann mean per patt corr avgannsq2 = np.mean(annsq2) # plot annual mean markers ax.fill_between((xboxmin[boxii],xboxmin[boxii]+wi),annmin,annmax,color=fillcol,alpha=.5) if not pcdf2.empty: ax.fill_between((xboxmin[boxii]+incr,xboxmin[boxii]+incr+wi),annmin2,annmax2,color=fillcol2,alpha=.5) ax.plot(xboxmin[boxii]+wi/2.,avgann,color='k',marker='_',linestyle='none',markersize=15)#,alpha=.9) if not pcdf2.empty: ax.plot(xboxmin[boxii]+wi/2.+incr,avgann2,color='b',marker='_',linestyle='none',markersize=15)#,alpha=.9) val = '$%.0f$'%(avgannsq*100) # @@ square the corrs before taking mean ax.annotate(val+'%', xy=(xboxmin[boxii]+wi/2. -.09, .95), xycoords='data') if not pcdf2.empty: val = '$%.0f$'%(avgannsq2*100) # @@ square the corrs before taking mean ax.annotate(val+'%', xy=(xboxmin[boxii]+wi/2.+incr -.06, .95), xycoords='data') ax.set_ylabel('Pattern Correlation') ax.set_xlim((.5,5.5)) ax.set_xticks(xxsea2) ax.set_xticklabels((seasons)+('ANN',)) ax.set_ylim((0,1)) ax.grid(True)
fnamep = basepath + casenamep+ subdir + casenamep + '_' + field + '_001-121_ts.nc' # monthly calc fldc = cnc.getNCvar(fnamec,ncfield,timesel=timesel)*conv fldp = cnc.getNCvar(fnamep,ncfield,timesel=timesel)*conv fldd = fldp-fldc # take the pattern correlation flddclimo,flddstd = cutl.climatologize(fldd) # climo first (don't need to do for BCs technically) flddcclimo,flddcstd = cutl.climatologize(flddc) # climo first. baseline diff data diffdict[skey] = flddclimo # for each month, compute pattern corr pc = np.zeros((12)) for mii,mon in enumerate(con.get_mon()): tmp = np.squeeze(flddclimo[mii,lat>latlim,...]) tmpcmp = np.squeeze(flddcclimo[mii,lat>latlim,...]) pc[mii] = cutl.pattcorr(tmp.flatten()*weights.flatten(),tmpcmp.flatten()*weights.flatten()) pcmeandict[skey] = pc # monthly # seasonal calc fldcsea = np.zeros((4,len(lat),len(lon))) fldpsea = np.zeros((4,len(lat),len(lon))) flddsea = np.zeros((4,len(lat),len(lon))) pcsea = np.zeros((4)) pcsea2 = np.zeros((4)) # test pattcorr_pearson() @@ pcsea2pval = np.zeros((4)) # test pattcorr_pearson() for seaii,sea in enumerate(seasons):
plt.figure() bm,cf = cplt.kemmap(slpd,lat,lon,cmin=-2,cmax=2,type='nh',cmap='blue2red_20') # transform vectors to projection grid. uproj,vproj,xx,yy = bm.transform_vector(ugrid,vgrid,newlons,lat,46,54,returnxy=True,masked=True) qu=bm.quiver(xx,yy,uproj,vproj)#,scale=700) #qu=bm.quiver(splons,splats,spud,spvd,latlon=True) plt.quiverkey(qu,0.1,0.1,1,'1 m/s',labelpos='W') if printtofile: plt.savefig('pmsl_windvecs' + level + '_' + str(sea) + '_' + sim + '.png') fuc,fup=con.build_filepathpair(sim,ufield) fvc,fvp=con.build_filepathpair(sim,vfield) monstr=con.get_mon() if sea=='DJF': months=[12,1,2,3] prstr='DJFM' elif sea=='SON': months=[9,10,11,12] prstr='SOND' fig,axs=plt.subplots(2,2) # D, J, F, M fig.set_size_inches(10,8) for aii,ax in enumerate(axs.flat): sea=months[aii] #print sea
def pattcorr_ensemble(ename, field, latlim=60): # @@@@@@@@@@@@ is this fully implemented? Don't think so. 12/2/14 if ename=='ANT': ename='HistIC' elif ename=='TOT': ename='HistBC' enssims = con.build_ensemblesims(ename) ensnum=len(enssims) # ======= copied from Canam4_BCpatterncorr-Copy0.py =========== #ensnum=5 diffdict = {} pcmeandict = {} # fldp-fldc pattern corr compared to mean BC pchaddict = {} # fldp-fldc pattern corr compared to hadisst seadiffdict = {} # seasonal mean pcseameandict = {} pcsea2meandict = {} # to test the other pattern corr calc pcsea2pvalmeandict = {} # to test the other pattern corr calc # generate weights for the pattern corr lat = con.get_t63lat() lon = con.get_t63lon() areas = cutl.calc_cellareas(lat,lon) areas = areas[lat>latlim,:] weights = areas / np.sum(np.sum(areas,axis=1),axis=0) #for eii in range(1,ensnum+1): for skey in enssims: #skey = etype + str(eii) #casenamec = bcasenamec + skey #casenamep = bcasenamep + skey #fnamec = basepath + casenamec+ subdir + casenamec + '_' + field + '_001-121_ts.nc' #fnamep = basepath + casenamep+ subdir + casenamep + '_' + field + '_001-121_ts.nc' fnamec,fnamep = con.build_filepathpair(skey,field) # monthly calc fldc = cnc.getNCvar(fnamec,ncfield,timesel=timesel)*conv fldp = cnc.getNCvar(fnamep,ncfield,timesel=timesel)*conv fldd = fldp-fldc # take the pattern correlation flddclimo,flddstd = cutl.climatologize(fldd) # climo first (don't need to do for BCs technically) flddcclimo,flddcstd = cutl.climatologize(flddc) # climo first. baseline diff data diffdict[skey] = flddclimo # for each month, compute pattern corr pc = np.zeros((12)) for mii,mon in enumerate(con.get_mon()): tmp = np.squeeze(flddclimo[mii,lat>latlim,...]) tmpcmp = np.squeeze(flddcclimo[mii,lat>latlim,...]) pc[mii] = cutl.pattcorr(tmp.flatten()*weights.flatten(),tmpcmp.flatten()*weights.flatten()) pcmeandict[skey] = pc # monthly # seasonal calc fldcsea = np.zeros((4,len(lat),len(lon))) fldpsea = np.zeros((4,len(lat),len(lon))) flddsea = np.zeros((4,len(lat),len(lon))) pcsea = np.zeros((4)) pcsea2 = np.zeros((4)) # test pattcorr_pearson() @@ pcsea2pval = np.zeros((4)) # test pattcorr_pearson() for seaii,sea in enumerate(seasons): fldcsea[seaii,...] = np.mean(cnc.getNCvar(fnamec,ncfield,timesel=timesel,seas=sea)*conv,axis=0) fldpsea[seaii,...] = np.mean(cnc.getNCvar(fnamep,ncfield,timesel=timesel,seas=sea)*conv,axis=0) flddsea[seaii,...] = fldpsea[seaii,...]-fldcsea[seaii,...] tmp = np.squeeze(flddsea[seaii,lat>latlim,...]) tmpcmp = np.squeeze(flddcsea[seaii,lat>latlim,...]) pcsea[seaii] = cutl.pattcorr(tmp.flatten()*weights.flatten(), tmpcmp.flatten()*weights.flatten()) pcsea2[seaii],pcsea2pval[seaii] = cutl.pattcorr_pearson(tmp.flatten()*weights.flatten(), tmpcmp.flatten()*weights.flatten()) seadiffdict[skey] = flddsea pcseameandict[skey] = pcsea pcsea2meandict[skey] = pcsea2 pcsea2pvalmeandict[skey] = pcsea2pval
def pattcorr_withinensemble(ename,fdict,latlim=60,timesel='0002-01-01,0121-12-31'): """ pattcorr_withinensemble(ename,field,latlim=60) pattern corr each member of ensemble with each other one return pctable, pctablesea (DataFrames) """ # @@ need diffdict field=fdict['field'] ncfield=fdict['ncfield'] conv=fdict['conv'] seasons=('SON','DJF','MAM','JJA') if ename=='ANT': ename='histIC' elif ename=='TOT': ename='histBC' enssims = con.build_ensemblesims(ename) ensnum=len(enssims) print 'ENSSIMS: ' # @@@ print enssims # @@ # generate weights for the pattern corr lat = con.get_t63lat() lon = con.get_t63lon() areas = cutl.calc_cellareas(lat,lon) areas = areas[lat>latlim,:] weights = areas / np.sum(np.sum(areas,axis=1),axis=0) # ========= create diffdict first ===== diffdict={} seadiffdict={} for skey in enssims: fnamec,fnamep = con.build_filepathpair(skey,field) # monthly calc fldc = cnc.getNCvar(fnamec,ncfield,timesel=timesel)*conv fldp = cnc.getNCvar(fnamep,ncfield,timesel=timesel)*conv fldd = fldp-fldc # Monthly flddclimo,flddstd = cutl.climatologize(fldd) # climo first (don't need to do for BCs technically) #flddcclimo,flddcstd = cutl.climatologize(flddc) # climo first. baseline diff data diffdict[skey] = flddclimo print skey + ' ' + str(flddclimo.shape) # @@@ # Seasonal flddsea = np.zeros((4,len(lat),len(lon))) for seaii,sea in enumerate(seasons): fldcsea = np.mean(cnc.getNCvar(fnamec,ncfield,timesel=timesel,seas=sea)*conv,axis=0) fldpsea = np.mean(cnc.getNCvar(fnamep,ncfield,timesel=timesel,seas=sea)*conv,axis=0) flddsea[seaii,...] = fldpsea-fldcsea seadiffdict[skey] = flddsea # ======= Now do pattern corrs within ensemble ==== # ======= copied from Canam4_BCpatterncorr-Copy0.py =========== outterdict= dict.fromkeys(enssims) for skey1 in enssims: outfld = diffdict[skey1] innerdict = dict.fromkeys(enssims) for skey2 in enssims: #print skey1 + ' compared to ' + skey2 infld = diffdict[skey2] # for each month, compute pattern corr pc = np.zeros((12)) for mii,mon in enumerate(con.get_mon()): tmp = np.squeeze(infld[mii,lat>latlim,...]) tmpcmp = np.squeeze(outfld[mii,lat>latlim,...]) pc[mii] = cutl.pattcorr(tmp.flatten()*weights.flatten(), tmpcmp.flatten()*weights.flatten()) innerdict[skey2] = pc outterdict[skey1] = innerdict pctable = pd.DataFrame(outterdict) # 5x5 # seasonal outterdictsea= dict.fromkeys(enssims) for skey1 in enssims: outfld = seadiffdict[skey1] innerdictsea = dict.fromkeys(enssims) for skey2 in enssims: #print skey1 + ' compared to ' + skey2 infld = seadiffdict[skey2] # for each season, compute pattern corr pcsea = np.zeros((4)) for seaii,sea in enumerate(seasons): tmp = np.squeeze(infld[seaii,lat>latlim,...]) tmpcmp = np.squeeze(outfld[seaii,lat>latlim,...]) pcsea[seaii] = cutl.pattcorr(tmp.flatten()*weights.flatten(), tmpcmp.flatten()*weights.flatten()) innerdictsea[skey2] = pcsea outterdictsea[skey1] = innerdictsea pctablesea = pd.DataFrame(outterdictsea) # 5x5 return pctable, pctablesea
def loaddata(fields, simulations, ncfields=None,model='CanAM4',timeper='001-121',timefreq=None, levsel=None,meantype=None,filetype='diff',region=None,alsomask=None,rettype='dict'): """ loaddata(fields, simulations,ncfields=None,model='CanAM4',timeper='001-121',timefreq=None, levsel=None, meantype=None,filetype='diff',region=None) fields: tuple of field names [@@update. only one field now but still need tuple] simulations: tuple of simulation names (diff names='E1'...'ENS','NSIDC' etc) ncfields: tuple of ncfield names (var name in file itself). Default to upper case of field model: for now only 'CanAM4' is implemented timeper: time period of the data (used for filename). default '001-121' timefreq: time frequency TO RETURN. default all data 'monthly'|'seasonal'|'climo'|'ANN'|'DJF'|'JJA'|'NDJ'|'MAM'|'SON'| 1,2,3,4,5,6,7,8,9,10,11,12 levsel: select level in Pa (e.g. 50000 for 500hPa). default all levels meantype: 'time','zonal' @@for now. default None, but recommended to choose one if loading multiple variables and multiple simulations at once. It is assumed that time is the first dimension. filetype: 'diff','ctl','pert','pval' Default is diff where both ctl and pert are read in and differenced. region: any of the regions in constants -> region dict. default None. alsomask: if specified as 'land' or 'ocean', then calc_regmean() will mask before computing regional avg (ie. will NOT include it in average). Only used if region!=None. Default None. returns: nested dictionary@@ FIELDS->SIMULATIONS->TIMEFREQ Load requested fields from requested CanAM4 simulations into dictionaries (dataframes?). Function automatically skips first year of simulation and gets a timeseries (or climo if requested) of the rest (assumed through year 121). 3D data and 'turb' not yet implemented! @@ """ if model!='CanAM4': print 'model not supported!' return -1 print '@@ probably should invert the order such that it is field, season, sim?' #bp=con.get_basepath() #basepath=bp['basepath'] + model + '/'; subdir=bp['subdir'] timesel='0002-01-01,0121-12-31' seabool=False # set to True if requested time freq is one season or climo monbool=False # set to True if requested time freq is one month if timefreq=='monthly': # get all months tf = con.get_mon() elif timefreq=='seasonal': # get all 4 seasons tf = 'DJF','MAM','JJA','SON' elif timefreq in range(1,13): # choose an individual month tf=timefreq monbool=True elif timefreq in ('climo','ANN','DJF','JJA','NDJ','MAM','SON','ND','JF','SO'): tf=timefreq seabool=True print tf # @@ # @@@@@ add handling of sia! #datadict = dict.fromkeys(fields,{}) #for fii,field in enumerate(fields): if 1: # GET RID OF FIELD dim too 5/8/2015 fii=0; field=fields[0] if ncfields==None: ncfield=field.upper() else: ncfield=ncfields[fii] print field,ncfield #@@ # assume simulations entered are of the form: E1, R3, ENSE etc. Then # filetype input arg tells which simulation to get (or both) simdict = dict.fromkeys(simulations,{}) for sim in simulations: print sim # @@ #timdict = dict.fromkeys(tf) # construct filename here @@ fnamec,fnamep = con.build_filepathpair(sim,field) #fname = basepath+sim+subdir+sim+'_'+field+'_'+timeper+'_ts.nc' print fnamec tfkey = tf #for tfkey in tf: # print tfkey # @@ #print 'too many levels in the dict...get rid of seasonal keys and just do one@@@ 5/1/2015' # @@ get the data with cnc.getNCvar() here ncparams = {} if monbool: ncparams = {'monsel': tfkey} elif seabool: ncparams = {'seas': tfkey} if levsel!=None: ncparams['levsel'] = levsel if meantype=='zonal': ncparams['calc'] = 'zm' if filetype=='diff' or filetype=='pval': if field in ('turb','net'): fnamec,fnamep = con.build_filepathpair(sim,'hfl') fnamecb,fnamepb = con.build_filepathpair(sim,'hfs') ctl = cnc.getNCvar(fnamec,'HFL',timesel=timesel,**ncparams) +\ cnc.getNCvar(fnamecb,'HFS',timesel=timesel, **ncparams) pert = cnc.getNCvar(fnamep,'HFL',timesel=timesel,**ncparams) +\ cnc.getNCvar(fnamepb,'HFS',timesel=timesel,**ncparams) if field=='net': fnamecc,fnamepc = con.build_filepathpair(sim,'flg') ctl = ctl - cnc.getNCvar(fnamecc,'FLG',timesel=timesel,**ncparams) pert = pert - cnc.getNCvar(fnamepc,'FLG',timesel=timesel,**ncparams) else: pert = cnc.getNCvar(fnamep,ncfield,timesel=timesel,**ncparams) ctl = cnc.getNCvar(fnamec,ncfield,timesel=timesel,**ncparams) fld = pert - ctl elif filetype=='ctl': if field in ('turb','net'): fnamec,fnamep = con.build_filepathpair(sim,'hfl') fnamecb,fnamepb = con.build_filepathpair(sim,'hfs') fld = cnc.getNCvar(fnamec,'HFL',timesel=timesel,**ncparams) +\ cnc.getNCvar(fnamecb,'HFS',timesel=timesel, **ncparams) if field=='net': fnamecc,fnamepc = con.build_filepathpair(sim,'flg') fld = fld - cnc.getNCvar(fnamecc,'FLG',timesel=timesel,**ncparams) else: fld = cnc.getNCvar(fnamec,ncfield,timesel=timesel,**ncparams) elif filetype=='pert': if field in ('turb','net'): fnamec,fnamep = con.build_filepathpair(sim,'hfl') fnamecb,fnamepb = con.build_filepathpair(sim,'hfs') fld = cnc.getNCvar(fnamep,'HFL',timesel=timesel,**ncparams) +\ cnc.getNCvar(fnamepb,'HFS',timesel=timesel,**ncparams) if field=='net': fnamecc,fnamepc = con.build_filepathpair(sim,'flg') fld = fld - cnc.getNCvar(fnamepc,'FLG',timesel=timesel,**ncparams) else: fld = cnc.getNCvar(fnamep,ncfield,timesel=timesel,**ncparams) else: print "filetype not supported! ['diff'|'ctl'|'pert'|'pval']" return -1 if region != None: lat=cnc.getNCvar(fnamec,'lat'); lon=cnc.getNCvar(fnamec,'lon') if filetype=='pval': pert = cutl.calc_regmean(pert,lat,lon,region,alsomask=alsomask) ctl = cutl.calc_regmean(ctl,lat,lon,region,alsomask=alsomask) (tstat,pval) = cutl.ttest_ind(pert,ctl) fld=pval else: fld = cutl.calc_regmean(fld,lat,lon,region,alsomask=alsomask) if meantype=='time': if filetype=='pval': # this is an error. filetype supercedes meantype so time avg won't be done print 'filetype=pval and meantype-time. Ignore meantype and return pvals @@' timdict=fld else: #fldstd = np.std(fld,axis=0) fld = np.mean(fld,axis=0) #timdict[tfkey] = fld #,fldstd timdict=fld else: #timdict[tfkey] = fld timdict=fld simdict[sim] = timdict #datadict[field]=simdict # can I set attributes to a dictionary? @@ like for nfields, nsims, ntimes? #return datadict if rettype=='ndarray': # convert the dict to an array: # get the last sim data to initialize ndarray initshape=simdict[sim].shape initshape=(len(simulations),) + initshape tmp=np.zeros(initshape) for sii,skey in enumerate(simulations): tmp[sii,:] = simdict[skey] return tmp else: return simdict
printtofile=False df = pd.DataFrame.from_csv('/raid/ra40/data/ncs/for_edhawkins_sic/rcp45/cmip5_rcp45_sie.csv') df_CanESM2 = df.filter(regex="CanESM2") models = ('ACCESS1-0','ACCESS1-3','BNU-ESM','CCSM4','CESM1-BGC','CESM1-CAM5','CMCC-CMS', 'CMCC-CM','CNRM-CM5','CSIRO-Mk3-6-0','CanESM2','EC-EARTH','FGOALS-g2','GFDL-CM3', 'GFDL-ESM2M','GISS-E2-H','GISS-E2-R','HadGEM2-CC','HadGEM2-ES','IPSL-CM5A-LR', 'IPSL-CM5A-MR','IPSL-CM5B-LR','MIROC-ESM-CHEM','MIROC-ESM','MIROC5','MPI-ESM-LR', 'MPI-ESM-MR','MRI-CGCM3','NorESM1-ME') styr='1979' endyr='2012' # inclusive mons = con.get_mon() montrenddt = coll.OrderedDict() #dict.fromkeys(mons) monmndt = coll.OrderedDict() monmxdt = coll.OrderedDict() monavgdt = coll.OrderedDict() hmontrenddt = coll.OrderedDict() nmontrenddt = coll.OrderedDict() #mondf = pd.DataFrame() # index will be np.arange totens=len(df.keys()) # total # of ens members ## # trend calc ## mm, bb = np.polyfit(xx, dat, 1) ## ax.plot(onex,mm*onex + bb, color='k')#,linestyle='--')