def spatiofreq2_season(s,lat,lon,yrs,eventkeys,figno=1,season='coreseason',\ key='noaa-olr-0-all',flagonly=False,file_suffix='test',savefig=False,\ fontdict=False): '''spatiofreq2_season(s,lat,lon,yrs,figno=1,season='coreseason',\ flagonly=False,file_suffix='test') Produces subplots of cloud-band gridpoint count by month ''' if not fontdict: fd = {'fontsize': 14, 'fontweight': 'bold'} else: fd = fontdict mbkl = key.split('-') if mbkl[0] == 'noaa': dclim = (1, 9, 1) if mbkl[0] == 'hadam3p': dclim = (1, 11, 1) if isinstance(season, str): if season == 'coreseason': mns = [10, 11, 12, 1, 2, 3] elif season == 'fullseason': mns = [8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7] elif isinstance(season, list): mns = season m, f = blb.SAfrBasemap(lat[4:-7],lon[3:-2],drawstuff=True,prj='cyl',\ fno=figno,rsltn='l',fontdict=fd) if len(mns) == 12: plt.close() plt.figure(figsize=[12, 10]) elif len(mns) == 6: plt.close() plt.figure(figsize=[13, 8]) cnt = 1 msklist = [] for mn in mns: if len(mns) == 12: plt.subplot(4, 3, cnt) elif len(mns) == 6: plt.subplot(3, 2, cnt) if flagonly: allmask=stats.spatiofreq2(m,s,lat,lon,yrs,eventkeys,\ clim=(1.,9.,1.),month=mn,flagonly=True,fontdict=fd) else: allmask=stats.spatiofreq2(m,s,lat,lon,yrs,eventkeys,clim=dclim,\ month=mn,flagonly=False,fontdict=fd) if cnt % 2 == 0: syp.redrawmap(m, lns=True, resol='verylow', parallel=False, fontdict=fd) else: syp.redrawmap(m, lns=True, resol='verylow', fontdict=fd) cnt += 1 msklist.append(allmask) my.xtickfonts() my.ytickfonts() plt.subplots_adjust(left=0.05,right=0.92,top=0.97,bottom=0.02,\ wspace=0.02,hspace=0.1) if savefig: if flagonly: plt.savefig(stats.figdir + file_suffix + '_flagonly.png', dpi=150) else: plt.savefig(stats.figdir + file_suffix + '.png', dpi=150) return msklist
def __init__(self, mbskeylist, mbslist, sbst='SA', hrwindow=49, COL=False): fstr = "hrtime, Label, Angle, cX, cY, minX, maxX, minY, maxY, area, cb.m10, cb.m01, cb.m11, cb.m20, cb.m02, cb.u11, cb.u20, cb.u02, cb.n11, cb.n20, cb.n02, cb.p1, cb.p2, Circularity" self.fields = [code.strip() for code in fstr.split(',')] domains = { 'SA': ((-60.0, 0.0), (0.0, 80.0)), 'Fauch08': ( (-40.0, -15.0), (7.5, 70.0), ), 'SH': ((-90.0, 0.0), (0, 360)), } vlt, vln = domains[sbst][0], domains[sbst][1] self.vaxis = vln[0], vln[1], vlt[0], vlt[1] self.mbskeys = tuple(mbskeylist) self.hrwindow = hrwindow self.blobs = {} self.tracks = {} self.trackshr = {} self.trackscX = {} self.trackscY = {} self.events = {} refm = mbslist[0] if isinstance(refm, tuple): for i in xrange(len(mbskeylist)): self.blobs[mbskeylist[i]] = { 'mbs': mbslist[i][0], 'mbt': mbslist[i][1], 'ch': mbslist[i][2] } elif isinstance(refm, str): #f, extens = refm.split('.') extens = refm.split('.')[-1] if extens == 'mbs': for i in xrange(len(mbskeylist)): mbs, mbt, ch = blb.mbopen(mbslist[i]) self.blobs[mbskeylist[i]] = { 'mbs': mbs, 'mbt': mbt, 'ch': ch } elif extens == 'synop': tmo = timer() self.mbskeys, self.hrwindow, self.flagkey, self.blobs,\ self.tracks, self.trackshr, self.trackscX, self.trackscY,\ self.events = self.open(refm) print "Opening time:", (timer() - tmo), "s" if len(self.events) > 0: ks = self.events.keys() print "Computing trkarrs of", len(ks), "events..." if not COL: addtrkarrs(self) elif COL: addCOLs(self) u = self.uniqueevents() print "Completion time:", (timer() - tmo), "s" self.flagkey = self.mbskeys[0] # only here because for synop call..
def datastretched(vrb, lat, lon, v, sub='SA'): '''This function copies first few steps of Metblobs() by taking data and then stretching it according to filterdict.py However it then uses this and the threshold value to calculate grid point frequencies for values that meet the threshold criteria''' dct = blb.blobfilters[sub + 'cloudband'][v] domain = dct['ROI'] dstretch = dct['stretch'] dthresh, highlow = dct['thresh'] data, thrs = blb.Stretch(vrb, dthresh, tval=dstretch) print "Stretched variable:", v if highlow == 'low': #thrs=255-thrs datamsk = data <= thrs elif highlow == 'high': datamsk = data >= thrs return ~datamsk
def spatiofreq2(m,s,lat,lon,yrs,eventkeys,meanmask=False,figno=1,\ clim=(4,36,4),month=False,flagonly=False,fontdict=False): '''Get grid-cell frequencies for no. of times a grid-cell falls within a contour describing a feature from metblobs. USAGE: If wish to create Basemap within function, m will be "create" if wish to have only for particular month, month=yourchoice if wish to only count for flagged days, flagonly=True''' #plt.close('all') if not fontdict: fd = {'fontsize': 14, 'fontweight': 'bold'} else: fd = fontdict mbskeys = s.mbskeys refkey = s.events.values()[0].refkey basekey = refkey try: dset, varstr, levsel, deriv, expid = basekey.split('-') descr = dset + '-' + varstr except: dset, varstr, levsel, deriv = basekey.split('-') descr = dset + '-' + varstr #vkey='%s-%s-%s-%s' %(dset, varstr, levsel, deriv) #x1,x2,y1,y2=blb.blobfilters[sub+'cloudband'][vkey]['ROI'] #nx, ny = np.abs(x1-x2)/res, np.abs(y1-y2)/res #grdsz = (np.int32(nx),np.int32(ny)) if not eventkeys: eventkeys = [] for ed in s.uniques: eventkeys.append(ed[0]) x, y = np.array(()), np.array(()) #lon=np.arange(lon[0],lon[-1]+1,0.5) #lat=np.arange(lat[0],lat[-1]-1,-0.5) allmask = np.zeros((lat.shape[0], lon.shape[0]), dtype=np.float32) #Determine which variable we dealing with countkey = refkey if flagonly: countkey = s.flagkey for k in eventkeys: e = s.events[k] if month: mn = month mst = e.trkdtimes[0, 1] if mst != mn: continue if flagonly: itrk = e.ixflags else: trkarr = np.int32(e.trkarrs[countkey]) if trkarr.ndim == 2: ixt = np.where(trkarr[:, 1] > 0)[0] uni, iu = np.unique(trkarr[ixt, 0], return_index=True) itrk = trkarr[ixt, 1] #print len(itrk),":",itrk elif trkarr.ndim == 3: itrk = np.ndarray((0, ), dtype=np.int32) for d in xrange(trkarr.shape[2]): ixt = np.where(trkarr[:, 1, d] > 0)[0] uni, iu = np.unique(trkarr[ixt, 0], return_index=True) ixt = ixt[iu] itrk = np.append(itrk, trkarr[ixt, 1, d].squeeze()) # Get masks for each contour feature of a track for ixtrk in itrk: mask = my.poly2mask(lon, lat, e.blobs[countkey]['ch'][ixtrk]) allmask = allmask + np.float32(mask) #cm=plt.cm.PuBu #cm=plt.cm.gist_earth_r #cm=plt.cm.YlGnBu #cm=plt.cm.binary #cm=plt.cm.OrRd if isinstance(meanmask, np.ndarray): cm = plt.cm.RdBu #cm=plt.cm.bwr else: cm = plt.cm.gist_gray_r #cm=plt.cm.jet #cm=plt.cm.gray_r cm.set_under(color='w') if m == 'create': m, f = blb.SAfrBasemap(lat[3:-6],lon[3:-3],drawstuff=True,\ prj='cyl',fno=figno,rsltn='l') #df=m.transform_scalar(allmask[::-1,:],lon,lat[::-1],len(lon),len(lat)) #m.imshow(df,cm,interpolation='nearest') #m.pcolor(lon,lat,allmask,cmap=cm) #plt.clim(300,1200) lon, lat = np.meshgrid(lon, lat) #m.contourf(lon,lat,(allmask/len(yrs)),cmap=cm) # ### Next is dirty trick to drop out permanent mid-latitude cloudiness ### allowing colormap to enhance cloud bands better allmask[-9:, :] = 0 std_mask = allmask / len(yrs) if isinstance(m, bool): return std_mask # IF M WAS NOT A BOOLEAN (False) THIS WILL CONTINUE TO PLOTTING if isinstance(meanmask, np.ndarray): std_mask = std_mask - meanmask std_mask = np.where(np.abs(std_mask) < .5, np.nan, std_mask) lnmin,lnmx,latmn,latmx =\ blb.filters.blobfilters['SAcloudband'][countkey]['ROI'] latmask = (lat < latmn) & (lat > latmx) # this is for S. Hemisphere meanmasked = np.ma.MaskedArray(meanmask, mask=~latmask) cs = m.contour(lon, lat, meanmasked, [1, 2, 3, 4], colors='k') labels=plt.clabel(cs, [1,2,3,4],inline_spacing=2, fmt='%d',\ fontsize=14) #m.contourf(lon,lat,meanmasked,[2,4,14],hatches=['.','..'],\ # colors='none',linestyles='-',linewidths='1',alpha=.1) ## NEED TO DO THIS SINCE PCOLOR IS NOT SHADING VALUES OUTSIDE OF THE CLIMS cstd_mask = np.where(std_mask > clim[1], clim[1], std_mask) cstd_mask = np.where(cstd_mask < clim[0], clim[0], cstd_mask) # Plot pcolor pcolmap = m.pcolormesh(lon, lat, cstd_mask, cmap=cm, zorder=1) img = plt.gci() for k in eventkeys: e = s.events[k] if month: mn = month mst = e.trkdtimes[0, 1] if mst != mn: continue m.plot(e.trkcX[0], e.trkcY[0], color='w', marker='o', markersize=4) if 'COL' in e.mbskeys: if len(e.assoctrks['COL']) > 0: trar = e.trkarrs['COL'] for ntrk in xrange(trar.shape[2]): ixx = np.where(trar[:, 1, ntrk] > 0)[0] xx, yy = trar[ixx, 2, ntrk], trar[ixx, 3, ntrk] off = np.random.rand() * .5 m.scatter(xx[-1]+off,yy[-1]+off,50,color='none',\ edgecolor='k',marker='o',linewidth='2') plt.sci(img) #m.plot(e.trkcX,e.trkcY,'0.5') plt.clim(clim[0], clim[1]) bounds = np.arange(clim[0], clim[1] + clim[2], clim[2]) #vals=np.arange(0,35,2) if not month: f, ax = plt.gcf(), plt.gca() axcol = f.add_axes([0.93, 0.2, 0.02, 0.6]) plt.colorbar(mappable=img, cax=axcol, boundaries=bounds) my.ytickfonts() if isinstance(meanmask, np.ndarray): plt.ylabel('anomaly grid-point count / year', fontdict=fd) else: plt.ylabel('grid-point count / year', fontdict=fd) plt.axes(ax) plt.title('Cloudband Annual Grid-Point Count Climatology: '\ +descr.upper(),fontsize='14',fontdict=fd) fname = figdir + '/FootprintFreqencygray-' + descr + '.png' if flagonly: fname = figdir + '/FootprintFreqencygray-' + descr + '_flagonly.png' plt.savefig(fname, dpi=150) elif month: f, ax = plt.gcf(), plt.gca() axcol = f.add_axes([0.93, 0.2, 0.02, 0.6]) plt.colorbar(cax=axcol, boundaries=bounds) my.ytickfonts() if isinstance(meanmask, np.ndarray): plt.ylabel('anomaly grid-point count / year', fontdict=fd) else: plt.ylabel('grid-point count / year', fontdict=fd) plt.axes(ax) plt.title(mndict[month], fontweight='demibold') return std_mask
def rawdatamasks(varlist): noaaolr = False ncep2 = False time6, time24 = [], [] newvarlist = varlist[:] retlist = [] for v in varlist: dset, varstr, lev, drv = v.split('-') retlist.append('datam' + varstr + '_' + lev) if dset == 'noaa' and varstr == 'olr': noaaolr = True newvarlist.remove(v) if dset == 'ncep2': ncep2 = True if ncep2: if varstr == 'uvmag': newvarlist.remove(v) newvarlist.extend([ 'ncep2-uwnd-' + lev + '-del2', 'ncep2-vwnd-' + lev + '-del2' ]) if varstr == 'quvmag': newvarlist.remove(v) newvarlist.extend( ['ncep2-qu-' + lev + '-del2', 'ncep2-qv-' + lev + '-del2']) sub = "SA" picdir = "/home/neil/work/computervision/metblobs/" + sub + "/pickles/" hgtthresh = 1.3 jetthresh = 1.0 moistthresh = 0.5 # CONSTRUCT A REFTIME FROM CENTRES OF THE SEASON 01-JAN refsubset = True hrwindow = 24 * 30 * 3 refdates = [] for yr in np.arange(1979, 2012): refdates.append((yr, 01, 01, 0)) refdates = np.asarray(refdates) reftime = my.dates2hrnum(refdates) mycm = plt.cm.PuBu # OPENING FILES # Liebmann & Smith Interpolated OLR if noaaolr: v = "noaa-olr-0-0" print '\n', v dset, varstr, lev, drv = v.split('-') dsrc = "/home/neil/data/olr/" #olr, time, lat, lon, dtime = mync.openolr(dsrc+'olr.NDJF.Fauch08.nc','olr') olr, time, lat, lon, dtime = mync.openolr(dsrc + 'olr.day.mean.nc', 'olr', subs=sub) time = time - 15769752.0 olr = olr[1675:, :, :] time = time[1675:] dtime = dtime[1675:] # gets all the data since 1979-01-01 00:00 if refsubset: exec("ixt, [time, " + varstr + ", dtime] = my.ixtwindow(reftime,time,hrwindow,time," + varstr + ",dtime)") time24 = time exec('datamsk' + varstr + '_' + lev + ' = datastretched(' + varstr + ',lat,lon,v,sub=sub)') exec('datam' + varstr + '_' + lev + ' = np.ma.MaskedArray(' + varstr + ',mask=datamsk' + varstr + '_' + lev + ',dtype=np.float32)') #m, f = blb.SAfrBasemap(lat[3:-6],lon[3:-3],drawstuff=True,prj='cyl',fno=1,rsltn='l') #exec('plt.figure();syp.redrawmap(m);m.pcolor(lon,lat,nfreq'+varstr+'_'+lev+',cmap=mycm);plt.clim(0.01,0.5);\ #plt.title("'+varstr+'_'+lev+'")') # NCEP2 subset file of your choice (created in by ncep2subset in subsetfiles.py) if ncep2: print "Analysing ncep2 data set..." #hgtlist=['ncep2-hgt-850-del2']#,'ncep2-hgt-500-del2','ncep2-hgt-250-del2'] #uvlist=['ncep2-uwnd-250-del2','ncep2-vwnd-250-del2']#,'ncep2-uwnd-500-del2','ncep2-vwnd-500-del2','ncep2-uwnd-250-del2','ncep2-vwnd-250-del2'] #quvlist=['ncep2-qu-700-del2','ncep2-qv-700-del2','ncep2-qu-500-del2','ncep2-qv-500-del2','ncep2-qu-850-del2','ncep2-qv-850-del2'] #quvlist=['ncep2-qu-850-del2','ncep2-qv-850-del2'] #varlist=[] #varlist.extend(hgtlist) #varlist.extend(uvlist) #varlist.extend(quvlist) for v in newvarlist: print '\n', v # OPEN FILE sbst = sub dset, varstr, lev, deriv = v.split('-') dsrc = "/home/neil/data/ncep2/6hourly/" + varstr + "/" ncfileout = dsrc + "%s.%s_%s.nc" % (varstr, sbst, lev) exec(varstr + ", time, lat, lon, dtime = mync.open" + dset + "(ncfileout,\'" + varstr + "\')") # SUBSET IN TIME BY REFTIME if refsubset: exec("ixt, [time, " + varstr + ", dtime] = my.ixtwindow(reftime,time,hrwindow,time," + varstr + ",dtime)") time6 = time # CALCULATION OF DERIVED VARIABLES # Laplacian of pressure surface if deriv == 'del2' and varstr == 'hgt': print "Get laplacian of hgt..." del2 = my.del2(hgt, time, lat, lon) del2 = my.coriolosscale(del2, lat, lon) slims, thrs = blb.calclims(del2, scalestd=hgtthresh, disttype='normal') blb.blobfilters[sub + 'cloudband'][v]['stretch'] = slims blb.blobfilters[sub + 'cloudband'][v]['thresh'] = (thrs[1], 'high') exec('datamsk' + varstr + '_' + lev + ' = datastretched(del2,lat,lon,v,sub=sub)') exec('datam' + varstr + '_' + lev + ' = np.ma.MaskedArray(' + varstr + ',mask=datamsk' + varstr + '_' + lev + ',dtype=np.float32)') del del2, hgt #m, f = blb.SAfrBasemap(lat[3:-6],lon[3:-3],drawstuff=True,prj='cyl',fno=1,rsltn='l') #exec('plt.figure();syp.redrawmap(m);m.pcolor(lon,lat,nfreq'+varstr+'_'+lev+',cmap=mycm);plt.clim(0.01,0.9);\ #plt.title("'+varstr+'_'+lev+'")') #plt.colorbar() # Laplacian of u,v magnitude elif 'uwnd' in locals() and 'vwnd' in locals() and deriv == 'del2': print "Get laplacian of vector wind magnitude..." vmag = dset + "-uvmag-" + lev + "-" + deriv dset, varstr, lev, deriv = vmag.split('-') mag, degdir = my.magdir(uwnd, vwnd) del uwnd, vwnd del2 = my.del2(mag, time, lat, lon) del degdir del2 = my.coriolosscale(del2, lat, lon) slims, thrs = blb.calclims(del2, scalestd=jetthresh, disttype='normal') blb.blobfilters[sub + 'cloudband'][vmag]['stretch'] = slims blb.blobfilters[sub + 'cloudband'][vmag]['thresh'] = (thrs[0], 'low') exec('datamsk' + varstr + '_' + lev + ' = datastretched(del2,lat,lon,vmag,sub=sub)') exec('datam' + varstr + '_' + lev + ' = np.ma.MaskedArray(mag,mask=datamsk' + varstr + '_' + lev + ',dtype=np.float32)') #exec('datam'+varstr+'_'+lev+' = np.ma.MaskedArray(v,mask=datamsk'+varstr+'_'+lev+',dtype=np.float32)') del del2, mag #m, f = blb.SAfrBasemap(lat[3:-6],lon[3:-3],drawstuff=True,prj='cyl',fno=1,rsltn='l') #exec('plt.figure();syp.redrawmap(m);m.pcolor(lon,lat,nfreq'+varstr+'_'+lev+',cmap=mycm);plt.clim(0.01,0.5);\ #plt.title("'+varstr+'_'+lev+'")') # Laplacian of qu,qv magnitude elif 'qu' in locals() and 'qv' in locals() and deriv == 'del2': print "Get laplacian of vector moisture flux magnitude..." vmag = dset + "-quvmag-" + lev + "-" + deriv dset, varstr, lev, deriv = vmag.split('-') mag, degdir = my.magdir(qu, qv) del qu, qv del2 = my.del2(mag, time, lat, lon) del degdir #del2 = my.coriolosscale(del2,lat,lon) slims, thrs = blb.calclims(del2, scalestd=moistthresh, disttype='normal') blb.blobfilters[sub + 'cloudband'][vmag]['stretch'] = slims blb.blobfilters[sub + 'cloudband'][vmag]['thresh'] = (thrs[0], 'low') exec('datamsk' + varstr + '_' + lev + ' = datastretched(del2,lat,lon,vmag,sub=sub)') exec('datam' + varstr + '_' + lev + ' = np.ma.MaskedArray(mag,mask=datamsk' + varstr + '_' + lev + ',dtype=np.float32)') #exec('datam'+varstr+'_'+lev+' = np.ma.MaskedArray(v,mask=datamsk'+varstr+'_'+lev+',dtype=np.float32)') del del2, mag #m, f = blb.SAfrBasemap(lat[3:-6],lon[3:-3],drawstuff=True,prj='cyl',fno=1,rsltn='l') #exec('plt.figure();syp.redrawmap(m);m.pcolor(lon,lat,nfreq'+varstr+'_'+lev+',cmap=mycm);plt.clim(0.01,0.5);\ #plt.title("'+varstr+'_'+lev+'")') exec('out=(' + ", ".join(retlist) + ')') return out, time24, time6