n_ttas except NameError: n_ttas = { 'nhours1':{'w_events':[],'w_hours':[], 'w_czd':[],'w_bby':[], 'n_events':[],'n_hours':[], 'n_czd':[],'n_bby':[]}, } for p in params: print p n_events = list() n_hours = list() r_czd = list() r_bby = list() for y in years: tta = tta_analysis(y) tta.start_df_layer(**p) s = pd.Series(tta.tta_dates) if s.size == 0: ev = 0 # events ho = 0 # hours cz = 0 bb = 0 else: sdiff = s-s.shift() ev = np.sum( sdiff>h ) + 1 ho = tta.tta_hours cz = tta.tta_rainfall_czd bb = tta.tta_rainfall_bby av_time = ho/float(ev) print template.format(y,ev,ho,av_time,cz,bb)
def process(year=[],wdsurf=None, wdwpro=None,rainbb=None, raincz=None, nhours=None): binss={'wdir':np.arange(0,370,10), 'wspd':np.arange(0,36,1)} target = ['wdir','wspd'] arrays = {} for t in target: first = True for y in year: print('Processing year {}'.format(y)) ' tta analysis ' tta = tta_analysis(y) tta.start_df(wdir_surf=wdsurf, wdir_wprof=wdwpro, rain_bby=rainbb, rain_czd=raincz, nhours=nhours) ' retrieve dates ' include_dates = tta.include_dates tta_dates = tta.tta_dates notta_dates = tta.notta_dates ' read wprof ' wprof_df = parse_data.windprof(y) wprof = wprof_df.dframe[t] ' wprof partition ' wprof = wprof.loc[include_dates] # all included wprof_tta = wprof.loc[tta_dates] # only tta wprof_notta = wprof.loc[notta_dates]# only notta s1 = np.squeeze(pandas2stack(wprof)) s2 = np.squeeze(pandas2stack(wprof_tta)) s3 = np.squeeze(pandas2stack(wprof_notta)) if first: wp = s1 wp_tta = s2 wp_notta = s3 first = False else: wp = np.hstack((wp,s1)) wp_tta = np.hstack((wp_tta,s2)) wp_notta = np.hstack((wp_notta, s3)) _,wp_hours = wp.shape _,tta_hours = wp_tta.shape _,notta_hours = wp_notta.shape arrays[t]=[wp,wp_tta,wp_notta] ' makes CFAD ' hist_array_spd = np.empty((40,len(binss['wspd'])-1,3)) hist_array_dir = np.empty((40,len(binss['wdir'])-1,3)) cfad_array_spd = np.empty((40,len(binss['wspd'])-1,3)) cfad_array_dir = np.empty((40,len(binss['wdir'])-1,3)) average_spd = np.empty((40,3)) average_dir = np.empty((40,3)) median_spd = np.empty((40,3)) median_dir = np.empty((40,3)) for k,v in arrays.iteritems(): hist_array = np.empty((40,len(binss[k])-1,3)) cfad_array = np.empty((40,len(binss[k])-1,3)) average = np.empty((40,3)) median = np.empty((40,3)) wp = v[0] wp_tta = v[1] wp_notta = v[2] for hgt in range(wp.shape[0]): row1 = wp[hgt,:] row2 = wp_tta[hgt,:] row3 = wp_notta[hgt,:] for n,r in enumerate([row1,row2,row3]): ' following CFAD Yuter et al (1995) ' freq,bins=np.histogram(r[~np.isnan(r)], bins=binss[k]) hist_array[hgt,:,n] = freq cfad_array[hgt,:,n] = 100.*(freq/float(freq.sum())) bin_middle = (bins[1:]+bins[:-1])/2. average[hgt,n] = np.sum(freq*bin_middle)/freq.sum() median[hgt,n] = np.percentile(r[~np.isnan(r)],50) if k == 'wspd': hist_array_spd = hist_array cfad_array_spd = cfad_array average_spd = average median_spd = median else: hist_array_dir = hist_array cfad_array_dir = cfad_array average_dir = average median_dir = median return [hist_array_spd, hist_array_dir, cfad_array_spd, cfad_array_dir, binss['wspd'], binss['wdir'], wprof_df.hgt, wp_hours, tta_hours, notta_hours, average_spd, average_dir, median_spd, median_dir]
def processv2(year=[],wdsurf=None, wdwpro=None,rainbb=None, raincz=None, nhours=None): ''' v2: target loop moved into year loop ''' binss={'wdir': np.arange(0,370,10), 'wspd': np.arange(0,36,1), 'u': np.arange(-15,21,1), 'v': np.arange(-14,21,1), } target = ['wdir','wspd'] arrays = {} wsp = np.empty((40,1)) wsp_tta = np.empty((40,1)) wsp_notta = np.empty((40,1)) wdr = np.empty((40,1)) wdr_tta = np.empty((40,1)) wdr_notta = np.empty((40,1)) for y in year: print('Processing year {}'.format(y)) ' tta analysis ' tta = tta_analysis(y) tta.start_df(wdir_surf = wdsurf, wdir_wprof = wdwpro, rain_bby = rainbb, rain_czd = raincz, nhours = nhours) ' retrieve dates ' include_dates = tta.include_dates tta_dates = tta.tta_dates notta_dates = tta.notta_dates ' read wprof ' wprof_df = parse_data.windprof(y) for n,t in enumerate(target): wprof = wprof_df.dframe[t] ' wprof partition ' wprof = wprof.loc[include_dates] # all included wprof_tta = wprof.loc[tta_dates] # only tta wprof_notta = wprof.loc[notta_dates]# only notta s1 = np.squeeze(pandas2stack(wprof)) if wprof_tta.size > 0: s2 = np.squeeze(pandas2stack(wprof_tta)) ttaok = True else: ttaok =False s3 = np.squeeze(pandas2stack(wprof_notta)) if t == 'wdir': wdr = np.hstack((wdr,s1)) if ttaok is True: if s2.ndim == 1: s2=np.expand_dims(s2,axis=1) wdr_tta = np.hstack((wdr_tta,s2)) wdr_notta = np.hstack((wdr_notta, s3)) else: wsp = np.hstack((wsp,s1)) if ttaok is True: if s2.ndim == 1: s2=np.expand_dims(s2,axis=1) wsp_tta = np.hstack((wsp_tta,s2)) wsp_notta = np.hstack((wsp_notta, s3)) arrays['wdir']=[wdr,wdr_tta,wdr_notta] arrays['wspd']=[wsp,wsp_tta,wsp_notta] uw = -wsp*np.sin(np.radians(wdr)) uw_tta = -wsp_tta*np.sin(np.radians(wdr_tta)) uw_notta = -wsp_notta*np.sin(np.radians(wdr_notta)) vw = -wsp*np.cos(np.radians(wdr)) vw_tta = -wsp_tta*np.cos(np.radians(wdr_tta)) vw_notta = -wsp_notta*np.cos(np.radians(wdr_notta)) arrays['u']=[uw,uw_tta,uw_notta] arrays['v']=[vw,vw_tta,vw_notta] ''' total hours, first rows are empty ''' _,wp_hours = wsp.shape _,tta_hours = wsp_tta.shape _,notta_hours = wsp_notta.shape wp_hours -= 1 tta_hours-= 1 notta_hours -= 1 ' initialize arrays ' hist_array_spd = np.empty((40,len(binss['wspd'])-1,3)) hist_array_dir = np.empty((40,len(binss['wdir'])-1,3)) cfad_array_spd = np.empty((40,len(binss['wspd'])-1,3)) cfad_array_dir = np.empty((40,len(binss['wdir'])-1,3)) average_spd = np.empty((40,3)) average_dir = np.empty((40,3)) median_spd = np.empty((40,3)) median_dir = np.empty((40,3)) ' loop for variable (wdir,wspd) ' for k,v in arrays.iteritems(): hist_array = np.empty((40,len(binss[k])-1,3)) cfad_array = np.empty((40,len(binss[k])-1,3)) average = np.empty((40,3)) median = np.empty((40,3)) ' extract value' wp = v[0] wp_tta = v[1] wp_notta = v[2] ' makes CFAD ' for hgt in range(wp.shape[0]): row1 = wp[hgt,:] row2 = wp_tta[hgt,:] row3 = wp_notta[hgt,:] for n,r in enumerate([row1,row2,row3]): ' following CFAD Yuter et al (1995) ' freq,bins=np.histogram(r[~np.isnan(r)], bins=binss[k]) hist_array[hgt,:,n] = freq cfad_array[hgt,:,n] = 100.*(freq/float(freq.sum())) bin_middle = (bins[1:]+bins[:-1])/2. average[hgt,n] = np.sum(freq*bin_middle)/freq.sum() median[hgt,n] = np.percentile(r[~np.isnan(r)],50) if k == 'wspd': hist_array_spd = hist_array cfad_array_spd = cfad_array average_spd = average median_spd = median elif k == 'wdir': hist_array_dir = hist_array cfad_array_dir = cfad_array average_dir = average median_dir = median elif k == 'u': hist_array_u = hist_array cfad_array_u = cfad_array average_u = average median_u = median elif k == 'v': hist_array_v = hist_array cfad_array_v = cfad_array average_v = average median_v = median return [hist_array_spd, hist_array_dir, hist_array_u, hist_array_v, cfad_array_spd, cfad_array_dir, cfad_array_u, cfad_array_v, binss['wspd'], binss['wdir'], binss['u'], binss['v'], wprof_df.hgt, wp_hours, tta_hours, notta_hours, average_spd, average_dir, average_u, average_v, median_spd, median_dir, median_u, median_v, ]
# 'wdir_layer': [0,500], # 'rain_czd': 0.25, # 'nhours': 2 # } for a in [140,160]] # # params = params_nh + params_wth try: results except NameError: # results = {nparam:list() for nparam in range(len(params))} results = {nparam: pd.DataFrame() for nparam in range(len(params))} first = True for y in years: print y tta = tta_analysis(y) if first: for n, p in enumerate(params): tta.start_df_layer(**p) # results[n] = tta.print_stats(return_results=True, # skip_print=True) results[n] = results[n].append(tta.df[tta.df.tta]) first = False else: for n, p in enumerate(params): tta.start_df_layer(**p) r = tta.print_stats(return_results=True, skip_print=True) results[n] = np.vstack((results[n], r)) first = True
def params(wdir_surf=None,wdir_wprof=None, wdir_thres=None,wdir_layer=None, rain_bby=None, rain_czd=None, nhours=None): years = [1998]+range(2001,2013) if wdir_surf is not None: params = dict(wdir_surf = wdir_surf, wdir_wprof = wdir_wprof, rain_bby = rain_bby, rain_czd = rain_czd, nhours = nhours) elif wdir_thres is not None: params = dict(wdir_thres = wdir_thres, wdir_layer = wdir_layer, rain_bby = rain_bby, rain_czd = rain_czd, nhours = nhours) print(params) for y in years: tta=tta_analysis(y) if wdir_surf is not None: tta.start_df(**params) elif wdir_thres is not None: tta.start_df_layer(**params) if y == 1998: results = tta.print_stats(header=True,return_results=True) else: r = tta.print_stats(return_results=True) results = np.vstack((results,r)) ''' print totals ''' print('-'*((6*13)+12)) yer_col = '{:6}' bby_col = '{:7.0f}{:7.0f}{:7.0f}' czd_col = '{:7.0f}{:7.0f}{:7.0f}' rto_col = '{:6.1f}{:6.1f}' hrs_col = '{:6.0f}{:6.0f}' prc_col = '{:6.0f}{:6.0f}' bby_total = results[:,0].sum() bby_tta = results[:,1].sum() bby_notta = results[:,2].sum() czd_total = results[:,3].sum() czd_tta = results[:,4].sum() czd_notta = results[:,5].sum() tta_ratio = czd_tta/bby_tta notta_ratio = czd_notta/bby_notta tta_hours = results[:,8].sum() notta_hours = results[:,9].sum() rain_perc_bby = np.round(100.*(bby_tta/bby_total),0).astype(int) rain_perc_czd = np.round(100.*(czd_tta/czd_total),0).astype(int) col0 = yer_col.format('') col1 = bby_col.format(bby_total, bby_tta, bby_notta) col2 = czd_col.format(czd_total, czd_tta, czd_notta) col3 = rto_col.format(tta_ratio, notta_ratio) col4 = hrs_col.format(tta_hours, notta_hours) col5 = prc_col.format(rain_perc_bby, rain_perc_czd) print(col0+col1+col2+col3+col4+col5)
U = pd.Series(name='U-comp') V = pd.Series(name='V-comp') try: results except NameError: results = collections.OrderedDict() for tr in thres: results[tr] = {'U': U, 'V': V} for year in years: for p in params: tta = tta_analysis(year=year) tta.start_df_layer(**p) tta_dates = tta.tta_dates " parse surface and profile obs " bby = parse_data.surface('bby', year=year) wpr = parse_data.windprof(year=year) wpr_tta = wpr.dframe.loc[tta_dates] wdr_tta = wpr_tta['wdir'] wsp_tta = wpr_tta['wspd'] bby_tta = bby.dframe.loc[tta_dates] " append surface values to windprof "