def grideventmask(allpolys, s, lon, lat): '''Returns a mask for gridded dataset for each day of each event format: dictionary for each entry''' keylist = allpolys.keys() keylist.sort() gridmasks = {} masklist = [] maskdtimes = [] maskekeys = [] for k in keylist: polylist = [] hrsince = s.events[k].trkarrs[s.events[k].refkey][:, 0] dtimes = my.hrnum2dates(hrsince) chulltrue = s.events[k].trkarrs[s.events[k].refkey][:, 1] != -1 print chulltrue, dtimes cnt = 0 for poly in allpolys[k]: if chulltrue[cnt]: chmask = my.poly2mask(lon, lat, poly) polylist.append(chmask) masklist.append(chmask) maskdtimes.append(dtimes[cnt]) maskekeys.append(k) cnt += 1 gridmasks[k] = polylist return gridmasks, masklist, maskdtimes, maskekeys
def clim_wetdayspercent(erainmasked, rtime, wetness=1): '''clim_wetdayspercent(erainmasked,rtime) Usage: erainm is outputfrom projectrainmask_in2_rawdata rtime is associated time Returns: wetdayscount (stations x 13) (ndarray), 12 months and annual mean freq_wet frequency of wewet scycle_wet & scycle_freqwet (stations x nseasons x nmonths), which are same as above stats but per season''' season = [8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7] dates = my.hrnum2dates(rtime) years = np.unique(dates[:, 0]) wetdayscnt = np.zeros((erainmasked.shape[0], len(season))) freqwet = wetdayscnt.copy() eventmask = ~erainmasked.mask wetmask = erainmasked.data > wetness # LOOK AT THE TOTAL PERCENTAGE IN ALL DATA cnt = 0 for mn in season: ix = np.where(dates[:, 1] == mn)[0] wetmsk = np.int32(wetmask[:, ix]) ewetmsk = np.int32(wetmask[:, ix] & eventmask[:, ix]) wetdayscnt[:, cnt] = wetmsk.sum(1) / np.float32(len(years)) freqwet[:, cnt] = ewetmsk.sum(1) / np.float32(len(years)) / np.float32( wetdayscnt[:, cnt]) cnt += 1 # SPLIT UP BY ACTUAL SEASONS scycle_wet = np.zeros((erainmasked.shape[0], len(years[:-1]), len(season))) scycle_freqwet = scycle_wet.copy() for iyr in xrange(len(years[:-1])): yr = years[iyr] for imn in xrange(len(season)): mn = season[imn] if imn < 5: ix = np.where((dates[:, 0] == yr) & (dates[:, 1] == mn))[0] wetmsk = np.int32(wetmask[:, ix]) ewetmsk = np.int32(wetmask[:, ix] & eventmask[:, ix]) else: ix = np.where((dates[:, 0] == yr + 1) & (dates[:, 1] == mn))[0] wetmsk = np.int32(wetmask[:, ix]) ewetmsk = np.int32(wetmask[:, ix] & eventmask[:, ix]) scycle_wet[:, iyr, imn] = wetmsk.sum(1) scycle_freqwet[:, iyr, imn] = ewetmsk.sum(1) / np.float32(wetmsk.sum(1)) return wetdayscnt, freqwet, scycle_wet, scycle_freqwet
def eventanddaycount(s, rainmasks): '''Simple count of number of events and rain days per month''' erain, etime, ekeys, flagarr = rainmasks['rain'] uky, ixk = np.unique(ekeys, return_index=True) erain, etime, ekeys, flagarr = erain[ ixk, :], etime[ixk], ekeys[ixk], flagarr[ixk, :] ixwet = np.any(~erain.mask).nonzero() edates = my.hrnum2dates(etime) yrs = np.unique(edates[:, 0]) yrs = yrs[:-1] eventcnt = np.zeros((len(yrs), len(season)), dtype=np.float32) daycnt = np.zeros((len(yrs), len(season)), dtype=np.float32) iyr = -1 for yr in yrs: iyr += 1 imn = -1 for mn in season: imn += 1 ix = np.where((edates[:, 0] == yr) & (edates[:, 1] == mn)) if len(ix) > 0: ix = ix[0] else: eventcnt[iyr, imn], daycnt[iyr, imn] = 0, 0 continue mnkeys = ekeys[ix] nky = len(mnkeys) dcnt = 0 for k in mnkeys: dcnt += len(( ~np.isnan(s.events[k].rainfall['wrc'][:, 0])).nonzero()[0]) eventcnt[iyr, imn] = nky daycnt[iyr, imn] = dcnt octmarecnt = eventcnt.sum(1) octmardcnt = daycnt.sum(1) summarystats = { 'eventcnt_mean': eventcnt.mean(0), 'eventcnt_std': eventcnt.std(0), 'daycnt_mean': daycnt.mean(0), 'daycnt_std': daycnt.std(0), 'ONDJFevent_mean': octmarecnt.mean(), 'ONDJFevent_std': octmarecnt.std(), 'ONDJFday_mean': octmardcnt.mean(), 'ONDJFday_std': octmardcnt.std() } return eventcnt, daycnt, octmarecnt, octmardcnt, summarystats
def multiplot(e, m, etime, blobkeylist, trkkeylist, *args): '''A plotting multitool that can plot all variables used and produced by MetBot package USAGE: etime can either be a tuple = (hrsbefore, hrsafter) or a list = [flagday1,flagday2] *args are gridded dataset and rainfall station dataset lists [mbskey,data,lat,lon,time,alt_mbskey] IMPORTANT: if u,v vector pairs are supplied, they must be supplied in that order if u,v vector pairs for multiple levels are supplied they need to be kept paired''' ### UNPACK *ARGS LISTS OF VARIABLES INTO USABLE FORMAT hgtlist, uvlist, quvlist, olrlist = [], [], [], [] print 'Event:', e.trkkey for arg in args: v = arg[0] dset, varstr, lev, drv = v.split('-') varlev = varstr + '_' + lev #print varlev exec(varlev + 'key,' + varlev + ',' + varlev + 'lat,' + varlev + 'lon,' + varlev + 'time,' + varlev + '_altkey=arg') if varstr == 'qv': quvlist.append(['qu' + '_' + lev, varlev]) elif varstr == 'hgt': hgtlist.append(varlev) elif varstr == 'v': uvlist.append(['u' + '_' + lev, varlev]) ### PLOT TRKARRS DATA USING INDEX LIST masktimes = e.trkarrstime[e.refkey] # this because had some issues with some empty trakarrs which won't have the times6hr needed for nmbs in e.mbskeys[3:]: if ~isinstance(e.trkarrs[nmbs], dict): mbs6hrkey = nmbs times6hr = e.trkarrstime[nmbs] # Used for labeling of figure dlabels, ixlist, flarr = daylabel(e, etime) for i in ixlist: # OLR PLOT if 'olr_0' in locals(): eolr = maskeddata(e, i, olr_0, olr_0time, olr_0key) plotolr(m, olr_0lat, olr_0lon, eolr) # HGT PLOT for ph in hgtlist: exec('hgt, hgtlat, hgtlon, hgttime, hgtkey = ' + ph + ', ' + ph + 'lat, ' + ph + 'lon, ' + ph + 'time, ' + ph + 'key') ehgt = maskeddata(e, i, hgt, hgttime, hgtkey) plothgt(m, hgtlat, hgtlon, ehgt) # STATION RAINFALL PLOT if 'rain_0' in locals(): plotrainstations(m, e, i, rain_0, rain_0lat, rain_0lon, rain_0time) # BLOB CONTOURS for blbkey in blobkeylist: plotblob(m, e, i, blbkey, blobkeylist.index(blbkey)) # BLOB TRACKS for trkkey in trkkeylist: plottrack(m, e, i, trkkey, trkkeylist.index(trkkey)) # QUV PLOT cnt = 0 for pqu, pqv in quvlist: exec('qu, qv, qlat, qlon, qtime, qu_altkey, qv_altkey = ' + pqu + ', ' + pqv + ', ' + pqu + 'lat, ' + pqu + 'lon, ' + pqu + 'time, ' + pqu + '_altkey, ' + pqv + '_altkey') equ = maskeddata(e, i, qu, qtime, qu_altkey) eqv = maskeddata(e, i, qv, qtime, qv_altkey) plotquv(m, qlat, qlon, equ, eqv, cols[cnt]) cnt += 1 cnt = 0 for pu, pv in uvlist: exec('u, v, qlat, qlon, qtime, u_altkey, v_altkey = ' + pu + ', ' + pv + ', ' + pu + 'lat, ' + pu + 'lon, ' + pu + 'time, ' + pu + '_altkey, ' + pv + '_altkey') eu = maskeddata(e, i, u, qtime, u_altkey) ev = maskeddata(e, i, v, qtime, v_altkey) plotuv(m, qlat, qlon, eu, ev, gcols[cnt]) cnt += 1 # DRAW EVERYTHING redrawmap(m, lns=True) # a little thing to handle time labeling relative to event flagging tm6, tm24 = times6hr[i], masktimes[i] tdate = my.hrnum2dates([tm6])[0] eventcode = dlabels[i] humandate = 'Date: %02d-%02d-%02d %02dh00' % (tdate[2], tdate[1], tdate[0], tdate[3]) plt.title(str(e.trkkey) + ': ' + eventcode) plt.text(30, -49, humandate) plt.draw() # SAVE FIGURE dstring = str(e.trkkey) + '_%02d-%02d-%02d_%02dh00' % ( tdate[0], tdate[1], tdate[2], tdate[3]) fname = figdir + dstring + '.png' plt.savefig(fname, dpi=150) #sleep(1) #raw_input() plt.clf()
def extremerainfall(s, kchoice, rain, rtime, perc=.95, areacrit=.1, selectmonths=False): '''extremeindices, assocTTT,assocTCOL = extremerainfall(s,kchoice,rain,rtime,perc=.95,areacriteria=.1,selectmonths=False)) Big and ungainly approach to get out extreme rainfall day indices and associations to TTTs Usage: pretty obvious Returns: extremeindices, ndarray of extreme days in rain data set assocTTTs, ndarray of (extremeeventkey, ttteventkey, eventsoverlap_in_days, cutofflow_or_not) assocTCOL, same as above but where cutofflow_or_not is true''' rsort = rain.copy() rsort.sort(axis=1) ### CALCULATE EXTREME VALUE CRITERIA FOR EACH STATION wet = (rsort > 1.) extrmvals = np.zeros((rsort.shape[0])) for istn in xrange(rsort.shape[0]): iw = wet[istn, :].nonzero()[0] iex = int(len(iw) * perc) iexwet = iw[iex] extrmvals[istn] = rsort[istn, iexwet] ### PRODUCE TIME INDICES OF EXTREME DATES AND A EXTREME ARRAY MASK OF DATES extrm_ixt = [] exmsk = np.ones(rain.shape, dtype=np.bool) nstns = rain.shape[0] areacriteria = int(nstns * areacrit) for ixt in xrange(rain.shape[1]): exmsk[:, ixt] = (rain[:, ixt] >= extrmvals) if len(exmsk[:, ixt].nonzero()[0]) >= areacriteria: extrm_ixt.append(ixt) ### GET EXTREME RAINFALL DATES extrm_ixt = np.asarray(extrm_ixt) ### IF ONLY SELECTING EXTREMES FROM PARTICULAR MONTHS if selectmonths: ixselect = [] exdates = my.hrnum2dates(rtime[extrm_ixt]) for mn in selectmonths: ixselmn = np.where(exdates[:, 1] == mn) if len(ixselmn) > 0: ixselect.extend(ixselmn[0]) else: continue extrm_ixt = extrm_ixt[ixselect] exrtime = rtime[extrm_ixt] consecutive = np.array([0], dtype=np.int32) consecutive = np.append(consecutive, extrm_ixt[1:] - extrm_ixt[:-1]) extrmevents = {} ix = 0 while ix < len(extrm_ixt): ixlist = [extrm_ixt[ix]] if ix + 1 >= len(extrm_ixt): extrmevents[int(rtime[ixlist[0]])] = ixlist ix += 1 continue diff = consecutive[ix + 1] if diff == 1: ix += 1 while diff == 1: ixlist.append(extrm_ixt[ix]) ix += 1 if ix >= len(extrm_ixt): break diff = consecutive[ix] else: ix += 1 extrmevents[int(rtime[ixlist[0]])] = ixlist ttttime, tttkeys = [], [] for k in kchoice: ktimes = list(s.events[k].trktimes) ttttime.extend(list(ktimes)) for dum in xrange(len(ktimes)): tttkeys.append(k) ttttime, tttkeys = np.asarray(ttttime), np.asarray(tttkeys) extrmkeys = extrmevents.keys() extrmkeys.sort() assoc = [] for k in extrmkeys: tttk = [] exhrs = rtime[extrmevents[k]] #print exhrs for hr in exhrs: imatch = np.where(ttttime == hr)[0] #print int(hr),imatch,len(imatch) if len(imatch) == 0: continue else: imatch = list(imatch) #print imatch tttk.extend(tttkeys[imatch]) tkey = list(np.unique(np.asarray(tttk))) col = 0 if 'COL' in s.events[kchoice[0]].trkarrs.keys(): for tk in tkey: if len(s.events[tk].trkarrs['COL']) > 0: col += 1 #print tkey ### USE THIS SETUP TO CAPTURE THE SOMETIMES 2 OR MORE CLOUD BAND EVENTS ASSOCIATED TO EXTREME RAINFALL #if len(tttk)>0:assoc.append((k,tkey,len(tttk))) #else:assoc.append((k,[0],0)) if len(tttk) > 0: assoc.append((k, tkey[-1], len(tttk), col)) else: assoc.append((k, 0, 0, 0)) assoc = np.asarray(assoc, dtype=np.int32) assocTTT = assoc[assoc[:, 1].nonzero()[0], :] assocTCOL = assoc[assoc[:, 3].nonzero()[0], :] print assoc.shape, assocTTT.shape, assocTCOL.shape return extrm_ixt, extrmevents, assocTTT, assocTCOL