fldpseazm = np.mean(fldpsea[..., :-1], axis=2) fldcseazmtm = np.mean(fldcseazm, axis=0) # time mean now fldpseazmtm = np.mean(fldpseazm, axis=0) plotd = np.zeros((nyr, nlat)) tstat = np.zeros((nyr, nlat)) pval = np.zeros((nyr, nlat)) for yr in years: if pattcorr == 1: # do a pattern correlation with time corrlim = 45 # @@ areas = cutl.calc_cellareas(lat, lon) areas = areas[lat > corrlim, :] # areas = ma.masked_where(lmask[lat>corrlim,:]==-1,areas) weights = areas / np.sum(np.sum(areas, axis=1), axis=0) # Could also do each year's pattern corr rather than cumulative mean DONE # OR, use the ens mean pattern as the pattern to compare against -- not useful I think, # for what we want to know: is the pattern of response for each ensemble member random # or dependent on the boundary condition. # @@ Also, would be nice to have multiple variables in one plot and/or multiple simulations if pattcorryr: # yearly anomaly pattern corr w/ the time mean pattern tmp = fldpseazm[yr, lat > corrlim, ...] - fldcseazmtm[lat > corrlim, ...] else: # time-integrated anomaly pattern corr w/ the end anomaly pattern
else: ncparams['levsel'] = level fldc = np.append(cnc.getNCvar(fnamec,ncfield,timesel='0002-01-01,061-12-31',**ncparams)*conv, cnc.getNCvar(fnamec2,ncfield,**ncparams)*conv, axis=0) fldp = np.append(cnc.getNCvar(fnamep,ncfield,timesel='0002-01-01,061-12-31',**ncparams)*conv, cnc.getNCvar(fnamep2,ncfield,**ncparams)*conv, axis=0) if sia==1: fldc = cutl.calc_seaicearea(fldc,lat,lon) fldp = cutl.calc_seaicearea(fldp,lat,lon) # Prepare the data here: @@ # @@ from hists script: areas = cutl.calc_cellareas(lat,lon,repeat=fldc.shape) areas = areas[:,lat>latlim,:] fldcorig=copy.copy(fldc) fldporig=copy.copy(fldp) fldc = fldc[:,lat>latlim,:] fldp = fldp[:,lat>latlim,:] if maskland: lmask = con.get_t63landmask(repeat=fldc.shape) areas = ma.masked_where(lmask[:,lat>latlim,:]==-1,areas) fldc = ma.masked_where(lmask[:,lat>latlim,:]==-1,fldc) fldp = ma.masked_where(lmask[:,lat>latlim,:]==-1,fldp) mtype='ocn' elif maskocean: lmask = con.get_t63landmask(repeat=fldc.shape) areas = ma.masked_where(lmask[:,lat>latlim,:]!=-1,areas) fldc = ma.masked_where(lmask[:,lat>latlim,:]!=-1,fldc)
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
# prepare the flux data by averaging, etc for field in fluxes: fnamec = basepath + casenamec + subdir + casenamec + '_' + field + '_' + timstr + '_ts.nc' fnamep = basepath + casenamep + subdir + casenamep + '_' + field + '_' + timstrp + '_ts.nc' fldc,cstd = cutl.climatologize(cnc.getNCvar(fnamec,field.upper(),timesel=timesel)) fldp,pstd = cutl.climatologize(cnc.getNCvar(fnamep,field.upper(),timesel=timesel)) fldc = ma.masked_where(sicnc<.10,fldc) # masking out non-ice region (as P&M 2014 JClim) fldp = ma.masked_where(sicnc<.10,fldp) # now calc area-weighted mean cellareas = cutl.calc_cellareas(lat,lon,repeat=fldc.shape) cellareas = ma.masked_where(sicnc<.10,cellareas) totarea = np.sum(np.sum(cellareas[:,lat>latlim,:],axis=2),axis=1) totarea = np.tile(totarea,(cellareas.shape[1],cellareas.shape[2],1)) totarea = np.transpose(totarea,(2,0,1)) #cellwgts = cellareas/totarea fldcam = np.sum(np.sum(fldc[:,lat>latlim,:]*(cellareas[:,lat>latlim,:]/totarea[:,lat>latlim,:]),axis=2),axis=1) fldpam = np.sum(np.sum(fldp[:,lat>latlim,:]*(cellareas[:,lat>latlim,:]/totarea[:,lat>latlim,:]),axis=2),axis=1) fluxdict[field] = fldpam - fldcam fnamecb = basepath + casenamecb + subdir + casenamecb + '_' + field + '_' + timstrb + '_ts.nc' fnamepb = basepath + casenamepb + subdir + casenamepb + '_' + field + '_' + timstrpb + '_ts.nc'
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