def windrose(): from windrose import plot_windrose, WindroseAxes import matplotlib.cm as cm set_windrose_style() ws = df['Wspd.m/s'].ffill() wd = df['Wdir.deg'].ffill() conc = df['GEM_avg_conc'].ffill() dfW = pd.DataFrame({'speed': ws, 'direction': wd, 'conc': conc}) plot_windrose(dfW, kind='contourf', bins=np.arange(0.01, 8, 1), cmap=cm.hot, lw=3) ax = WindroseAxes.from_ax() ax.bar(dfW.direction, dfW.conc, normed=True, opening=0.8, edgecolor='white') ax.set_legend() plot_windrose(dfW, kind='contourf', var_name='conc', direction_name='direction', bins=np.arange(0.01, dfW.conc.max(), 0.2))
def main(): df = pd.read_csv("samples/sample_wind_poitiers.csv", parse_dates=["Timestamp"]) # df['Timestamp'] = pd.to_timestamp() df = df.set_index("Timestamp") # N = 500 # ws = np.random.random(N) * 6 # wd = np.random.random(N) * 360 # df = pd.DataFrame({'speed': ws, 'direction': wd}) print(df) print(df.dtypes) bins = np.arange(0.01, 8, 1) # bins = np.arange(0, 8, 1)[1:] plot_windrose(df, kind="contour", bins=bins, cmap=cm.hot, lw=3, rmax=20000) plt.show() bins = np.arange(0, 30 + 1, 1) bins = bins[1:] ax, params = plot_windrose(df, kind="pdf", bins=bins) print("Weibull params:") print(params) # plt.savefig("screenshots/pdf.png") plt.show()
def main(): df = pd.read_csv("samples/sample_wind_poitiers.csv", parse_dates=['Timestamp']) # df['Timestamp'] = pd.to_timestamp() df = df.set_index('Timestamp') # N = 500 # ws = np.random.random(N) * 6 # wd = np.random.random(N) * 360 # df = pd.DataFrame({'speed': ws, 'direction': wd}) print(df) print(df.dtypes) bins = np.arange(0.01, 8, 1) # bins = np.arange(0, 8, 1)[1:] plot_windrose(df, kind='contour', bins=bins, cmap=cm.hot, lw=3, rmax=20000) plt.show() bins = np.arange(0, 30 + 1, 1) bins = bins[1:] ax, params = plot_windrose(df, kind='pdf', bins=bins) print("Weibull params:") print(params) # plt.savefig("screenshots/pdf.png") plt.show()
def test_windrose_pd_not_default_names(): bins = np.arange(0.01, 8, 1) kind = 'scatter' df_not_default_names = pd.DataFrame({'wind_speed': ws, 'wind_direction': wd}) plot_windrose(df_not_default_names, kind=kind, alpha=0.2, var_name='wind_speed', direction_name='wind_direction')
def test_windrose_np_plot_and_pd_plot(): # bins = np.arange(0.01, 8, 1) kind = 'scatter' plot_windrose(df, kind=kind, alpha=0.2) plt.savefig('tests/output/df/%s.png' % kind) plot_windrose(wd, ws, kind=kind, alpha=0.2) plt.savefig('tests/output/func/%s.png' % kind)
def test_windrose_pd_not_default_names(): # bins = np.arange(0.01, 8, 1) kind = "scatter" df_not_default_names = pd.DataFrame({"wind_speed": ws, "wind_direction": wd}) plot_windrose( df_not_default_names, kind=kind, alpha=0.2, var_name="wind_speed", direction_name="wind_direction", )
def test_windrose_pd_not_default_names(): bins = np.arange(0.01, 8, 1) kind = 'scatter' df_not_default_names = pd.DataFrame({ 'wind_speed': ws, 'wind_direction': wd }) plot_windrose(df_not_default_names, kind=kind, alpha=0.2, var_name='wind_speed', direction_name='wind_direction')
def RosePlot(beachnb, bluebnb, date_obs, direction_obs, speed_obs): """ returns a rose plot of the wind for the past day for the beach beachnb and for the bluebottle scenario bluenb (0:none, 1:likely, 2:some) """ location = ['Clovelly', 'Coogee', 'Maroubra'] blueb = ['none', 'likely', 'some'] wind_speed = [] wind_direction = [] one_day = datetime.timedelta(days=1) for i in range(len(date_obs)): for j in range(len(date_box[beachnb][bluebnb])): if (date_obs[i] + one_day) == date_box[beachnb][bluebnb][j]: wind_speed.append(speed_obs[i]) wind_direction.append(direction_obs[i]) df = pd.DataFrame({"speed": wind_speed, "direction": wind_direction}) bins = np.arange(0.01, 24, 4) kind = "bar" # fig=plt.figure() plot_windrose(df, kind=kind, normed=True, opening=0.8, edgecolor="white", bins=bins) plt.title("Daily averaged wind direction 1 day before at " + str(location[beachnb]) + " " + str(blueb[bluebnb])) plt.legend('wind speed (m/s)') # fig2=plt.figure() # plt.hist(wind_direction,bins_new) plt.savefig("../outputs_observation_data/sydney_obs/daily_averaged/rose" + str(location[beachnb]) + "_" + str(blueb[bluebnb]) + "_pastday.png", dpi=300)
binss = [0, 2, 4, 6, 8, 10, 12] speed = ['0-2', '2-4', '4-6', '6-8', '8-10', '10+'] df['Speed'] = pd.cut(df['Wg'], binss, labels=speed) DirSpeed = df.groupby( ['Direction', 'Speed']).size() #Number of events per direction and speed category #DirSpeed.to_csv(r'C:\Users\max_a\Dropbox\MAX\Earth Sciences\4th\Hydromet\Wind rose\Data\January_96.csv', index = False) print(DirSpeed) D = df.groupby(['Direction'])['Wg'].mean().reindex([ 'N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW' ]) print(D) ws = df["Wg"].values wd = df["Wgd"].values df = pd.DataFrame({'speed': ws, 'direction': wd}) ax = plot_windrose(df, kind='contourf', bins=np.arange(0.01, 10, 1), cmap=cm.YlGn) plt.title('December 2018') #Name of the Wind Rose! plt.legend(title='Wind speed (m/s)', loc='best', fancybox=True) plt.show()
def test_windrose_pandas(): bins = np.arange(0.01, 8, 1) kind = 'scatter' plot_windrose(df, kind=kind, alpha=0.2) plt.savefig('tests/output/df/%s.png' % kind) kind = 'bar' plot_windrose(df, kind=kind, normed=True, opening=0.8, edgecolor='white') plt.savefig('tests/output/df/%s.png' % kind) kind = 'box' plot_windrose(df, kind=kind, bins=bins) plt.savefig('tests/output/df/%s.png' % kind) kind = 'contourf' plot_windrose(df, kind=kind, bins=bins, cmap=cm.hot) plt.savefig('tests/output/df/%s.png' % kind) kind = 'contour' plot_windrose(df, kind=kind, bins=bins, cmap=cm.hot, lw=3) plt.savefig('tests/output/df/%s.png' % kind) kind = 'pdf' plot_windrose(df, kind=kind, bins=bins) plt.savefig('tests/output/df/%s.png' % kind)
con = sqlite3.connect(f) sonic_in = pd.read_sql_query( 'SELECT DATETIME(dateTime,"unixepoch") as DateTime, windDir AS direction, windSpeed * 0.45 AS speed FROM archive', con, parse_dates=['DateTime'], index_col='DateTime') #append to other data if isinstance(sonic_in, pd.DataFrame): sonic = pd.concat([sonic, sonic_in]) bins = np.arange(0.01, 10, 1) ax = windrose.plot_windrose(sonic, kind='bar', bins=bins, lw=1, cmap=cm.Greens, opening=0.8, normed=True) ax.set_rlabel_position(0) #ax.set_rgrids([20,40,60,80,100],['20','40','60','80','%'], horizontalalignment='center') maxr = ax.get_rmax() #make next-highest multiple of ten maxr = (np.ceil(maxr / 10)) * 10 radii = np.linspace(0.1, maxr, int(maxr / 10) + 1) radii_labels = ["%.0f" % r for r in radii] radii_labels[0] = "" #Removing label 0 ax.set_rgrids(radii, radii_labels, horizontalalignment='center') ax.set_thetagrids(ax.theta_angles, labels=['E', 'NE', 'N', 'NW', 'W', 'SW', 'S', 'SE'], frac=1.05)
def plotspline_by_item(item, dstart, dend, yieldpath, engine, **kwargs): logger = logging.getLogger(__name__) errors = 0 plt.style.use('seaborn-white') plt.rcParams['font.family'] = 'sans-serif' plt.rcParams['font.sans-serif'] = 'SimHei' plt.rcParams['axes.unicode_minus'] = False params = {'legend.fontsize': 'x-large', 'axes.labelsize': 'x-large', 'xtick.labelsize': 'x-large', 'ytick.labelsize': 'x-large'} plt.rcParams.update(params) itemname = item['itemname'] tblname = item['tblname'] freq = item['freq'] try: item_buoys = kwargs.get(itemname + '_buoys') or '' valid_buoys = item_buoys if item_buoys else (kwargs.get('args_buoys') or kwargs.get('valid_buoys') or '') sql_snippet = 'and plfbqzh in (' + valid_buoys + ')' if valid_buoys else '' sql_splines = 'select plfbqzh,gcrqsj,{:} from {:} where gcrqsj >= "{:}" and gcrqsj < "{:}" {:}' \ 'order by gcrqsj;'.format(itemname, tblname, dstart.strftime("%Y-%m-%d %H:%M"), dend.strftime("%Y-%m-%d %H:%M"), sql_snippet) # print(sqls) # df = pd.read_sql_query('select plfbqzh,gcrqsj,qw from nt_plfbqwsjb where ' # 'gcrqsj >= "2017-05-30 20:00:00" order by gcrqsj;', engine) df = pd.read_sql_query(sql_splines, engine) except: errors += 1 logger.exception("从%s中读取%s值时出错,绘制折线图失败", tblname, itemname) return errors if df.empty: logger.debug('空数据集!') return errors try: dfpivot = df.pivot(index='gcrqsj', columns='plfbqzh', values=itemname) # print(dfpivot[dfpivot.columns[0:1]]) # dfint = dfpivot.resample('T').interpolate('cubic') # print(dfpivot.index) xticks = pd.date_range(start=dstart, end=dend, freq=freq) dfpivot.reindex(xticks) except: errors += 1 logger.exception("处理%s要素数据时出错,绘制折线图失败", itemname) return errors splines_path = os.path.join(yieldpath, 'splines') rosecharts_path = os.path.join(yieldpath, 'rosecharts') # html_path = os.path.join(yieldpath, 'html') if not os.path.exists(splines_path): os.mkdir(splines_path) if not os.path.exists(rosecharts_path): os.mkdir(rosecharts_path) # if not os.path.exists(html_path): # os.mkdir(html_path) try: fig, ax = plt.subplots() dfpivot.index.name = '观测日期和时间' xticksperhour = pd.date_range(start=dstart, end=dend, freq='H') styles = ['v-', 'D-', 'x-', 'p-', '*-', '|-', 's-', 'c-', 'd-', ',-', 'H+'] dfpivot.plot(grid='on', ax=ax, figsize=(16, 7), rot=0, style=styles, xticks=xticksperhour.to_pydatetime(), markersize=3, markerfacecolor='green') ax.legend([x for x in dfpivot.columns], loc='center left', bbox_to_anchor=(1, 0.5), frameon=True) ax.xaxis.grid(True, which='minor') ax.xaxis.grid(True, which='major') ax.yaxis.grid(True, which='minor') if kwargs.get('major_xlabels'): ax.set_xticklabels(kwargs.get('major_xlabels'), ha='center') plt.ylabel(item['ylabel']) plt.title('{:} 至 {:} {:}监测'.format(winstrftime(dstart, '%m月%d日%H时'), winstrftime(dend, '%m月%d日%H时'), item['title']), fontsize=20, fontweight='bold') figname = '_'.join([x for x in (item['title'], dstart.strftime("%m-%d-%H_"), dend.strftime("%m-%d-%H"))]) plt.savefig(os.path.join(splines_path, figname), dpi=300) except KeyError: errors += 1 logger.warning("绘制 %s 时出错!跳过该文件继续执行...", '_'.join([x for x in (itemname, dstart.strftime("%m-%d-%H_"), dend.strftime("%m-%d-%H"))])) fig.clf() plt.close() gc.collect() if item['itemname'] == 'fs': for column in dfpivot.columns: try: sql_wrm = 'select s.gcrqsj,s.fs,d.fx from nt_plfbfssjb s, nt_plfbfxsjb d where s.plfbqzh ="{:}" and ' \ 's.gcrqsj >= "{:}" and s.gcrqsj < "{:}" and ' \ 's.plfbqzh = d.plfbqzh and s.gcrqsj = d.gcrqsj'.format(column, dstart.strftime("%Y-%m-%d %H:%M"), dend.strftime("%Y-%m-%d %H:%M")) wind_df_by_buoy = pd.read_sql_query(sql_wrm, engine) wind_df_by_buoy.index = wind_df_by_buoy.gcrqsj if not wind_df_by_buoy[wind_df_by_buoy.fs > 0].empty: ax = plot_windrose(wind_df_by_buoy, kind='bar', var_name='fs', direction_name='fx', bins=6, clean_flag=False) ax.legend(loc='best', frameon=True, prop={'size':9}) for text in ax.get_legend().get_texts(): text.set_text(text.get_text()[:-1] + ']') figname = ''.join([x for x in (column, dstart.strftime(" %m-%d %H"), '时 至 ', dend.strftime(" %m-%d %H"), '时 风玫瑰图')]) plt.title(figname, fontsize=20, fontweight='bold') plt.savefig(os.path.join(rosecharts_path, figname), dpi=300) plt.close() except: errors += 1 logger.warning("绘制 %s 时出错!跳过该文件继续执行...", ''.join([x for x in (column, dstart.strftime("%m-%d %H:%M"), '时 至 ', dend.strftime("%m-%d %H:%M"), '时 风玫瑰图')])) fig.clf() plt.close() gc.collect() return errors
def main(): #At least for testing and stand alone get the required information from the config file #Later just run from DINGO and pass info as required #cf = ConfigObj('E:/My Dropbox/Dropbox/Data_flux_data/controlfiles/Whroo/Advanced/AdvancedProcessing_config_Whroo_v13.txt') cf = ConfigObj('E:/My Dropbox/Dropbox/Data_flux_data/controlfiles/HowardSprings/Advanced/AdvancedProcessing_config_HowardSprings_v13.txt') #Input site details versionID="v12a" Site_ID=cf['Site']['Site_ID'] Canopy_height=float(cf['Site']['Canopy_height']) Instrument_height =float(cf['Site']['Instrument_height']) mypathforFluxdata=cf['Files']['mypathforFluxdata']+Site_ID+ "/Advanced_"+versionID myBaseforResults_FFP=cf['Files']['mypathforFluxdata']+Site_ID+ "/Advanced_"+versionID+"/Diagnostics/Footprint" myBaseforResults_windrose=cf['Files']['mypathforFluxdata']+Site_ID+ "/Advanced_"+versionID+"/Diagnostics/Windrose" Ws_variable_name = cf['Options']['Ws_variable_name']+'_Con' global latitude latitude= float(cf['Site']['Tower_Lat']) #Toggle what you want to do do_ffp_distances = True do_ffp_climatology_plots = True do_windrose_climatology_plots = True #Read dataframe from site FLUXDataframe_orig= pd.read_pickle(mypathforFluxdata+'/Advanced_processed_data_'+Site_ID+'_'+versionID+'.df') print "Starting Footprint analysis" #Check for place to put results - does it exist? If not create #Then subdirectories if not os.path.isdir(myBaseforResults_FFP): os.mkdir(myBaseforResults_FFP) if not os.path.isdir(myBaseforResults_windrose): os.mkdir(myBaseforResults_windrose) if not os.path.isdir(myBaseforResults_windrose+'/Annual'): os.mkdir(myBaseforResults_windrose+'/Annual') if not os.path.isdir(myBaseforResults_windrose+'/Monthly'): os.mkdir(myBaseforResults_windrose+'/Monthly') #Prepare data for footprint analysis # Apply 2D coordinate rotation from Isaac OzFluxQC v2.9.5 # We use this to get the sigma v required for input to FFP FLUXDataframe_orig = CoordRotation2D(FLUXDataframe_orig) # To calculate a single FFP flux footprint with calc_footprint_FFP. # Call calc_footprint_FFP(zm,z0,umean,h,ol,sigmav,ustar) using inputs # zm = Measurement height above displacement height z minus d [m] FLUXDataframe_orig['zm']=Instrument_height-(Canopy_height*0.666) # z0 = Roughness length [m] enter [NaN] if not known FLUXDataframe_orig['z0'] = Canopy_height * 0.1 # umean = Mean wind speed at zm [ms-1] enter [NaN] if not known FLUXDataframe_orig['umean'] = FLUXDataframe_orig[Ws_variable_name] #Subset data if required for testing FFP_Dataframe=FLUXDataframe_orig["2015-01-30 00:00":"2015-02-01 23:30"] #FFP_Dataframe=FLUXDataframe_orig #Check for missing values and drop them FFP_Dataframe= FFP_Dataframe[['Ta_Con','Ah_Con','ps_Con','ustar','Fh_Con', 'wT','vv', Ws_variable_name, 'Wd','zm','z0','umean','day_night']].dropna(how='any') #Calculate Monin Obukov Length calling Isaac OxFluxQC Metfunction print "Applying ML function" FFP_Dataframe['ol'] = FFP_Dataframe.apply(lambda x: metfunc.molen(x['Ta_Con'],x['Ah_Con'],x['ps_Con'],x['ustar'],x['Fh_Con'],fluxtype='sensible'),axis=1) # h = Boundary layer height [m] set differently for time of day/night FFP_Dataframe['h'] = FFP_Dataframe.apply(lambda x: calculate_h(x), axis=1) print 'test' #FFP_Dataframe['h'][FFP_Dataframe['day_night']==2] = 1500.0 #FFP_Dataframe['h'][FFP_Dataframe['day_night']==3] = 750.0 #In the FFP code there is are exception for numerous items so catch them here # if sigmav <= 0: raise_ffp_exception(8) # zm/ol (measurement height to Obukhov length ratio) must be equal or larger than -15.5'} # So lets catch that here print "Number rows total " + str(len(FFP_Dataframe)) print "Number rows with zmol exceptions " + str(len(FFP_Dataframe[FFP_Dataframe['ol']/FLUXDataframe_orig['zm'] < -15.5])) print "Number rows with sigma v exceptions " + str(len(FFP_Dataframe[FFP_Dataframe['vv']==0])) FFP_Dataframe=FFP_Dataframe[FFP_Dataframe['ol']/FLUXDataframe_orig['zm'] >= -15.5] FFP_Dataframe=FFP_Dataframe[FFP_Dataframe['vv']!=0] #First run FFP for each time step then later do footprint climatology if do_ffp_distances == True: #Apply FFP to dataframe as function #The following Function returns the max distance away from the tower for any given r% FFP_Dataframe['FFP_x_10%'] = FFP_Dataframe.apply(lambda x: distance_for_r(x,10), axis=1) FFP_Dataframe['FFP_x_30%'] = FFP_Dataframe.apply(lambda x: distance_for_r(x,30), axis=1) FFP_Dataframe['FFP_x_50%'] = FFP_Dataframe.apply(lambda x: distance_for_r(x,50), axis=1) FFP_Dataframe['FFP_x_70%'] = FFP_Dataframe.apply(lambda x: distance_for_r(x,70), axis=1) FFP_Dataframe['FFP_x_90%'] = FFP_Dataframe.apply(lambda x: distance_for_r(x,90), axis=1) FFP_Dataframe['FFP_x_peak'] = FFP_Dataframe.apply(lambda x: Footprint.FFP(x['zm'], x['z0'], x['umean'], x['h'], x['ol'], x['vv'], x['ustar'], x['Wd'], None)['x_ci_max'],axis=1) #FFP_Dataframe['FFP_flag'] = FFP_Dataframe.apply(lambda x: Footprint.FFP(x['zm'], x['z0'], x['umean'], x['h'], x['ol'], x['vv'], x['ustar'], x['Wd'], None)['flag_error'],axis=1) #Do a check on the dataframe including resampling to 30 minutes before merging FFP_Dataframe=dataframe_check(FFP_Dataframe, '30T') #Output the FFP input and output FFP_Dataframe[['zm', 'z0', 'umean', 'h', 'ol', 'vv', 'ustar', 'Wd','FFP_x_10%','FFP_x_30%','FFP_x_50%','FFP_x_70%','FFP_x_90%','FFP_x_peak']].to_csv(myBaseforResults_FFP+'/FFP_output_'+Site_ID+'.csv') #Next take the results and merge with the original dataframe FLUXDataframe_orig_plus_FFP = pd.merge(FLUXDataframe_orig, FFP_Dataframe[['ol', 'vv', 'FFP_x_10%','FFP_x_30%','FFP_x_50%','FFP_x_70%','FFP_x_90%','FFP_x_peak']],left_index=True,right_index=True,how="left") #Now join if do_ffp_climatology_plots == True: # Next Do the climatology footprints. First groupby year and month by = lambda x: lambda y: getattr(y, x) FFP_Dataframe_grouped=FFP_Dataframe.groupby([by('year'),by('month')]) for group_index, group_x in FFP_Dataframe_grouped: year_label=str (group_index[0]) month_label=str(group_index[1]) print "Doing footrint for year " + year_label + "and " + month_label + " month" #Do for Night and then day seperately nightdata=group_x[group_x['day_night']==3] night_day_label = 'Night' zm_list = nightdata['zm'].values z0_list = nightdata['z0'].values umean_list = nightdata['umean'].values h_list = nightdata['h'].values ol_list = nightdata['ol'].values sigmav_list = nightdata['vv'].values ustar_list = nightdata['ustar'].values wind_dir_list = nightdata['Wd'].values # Call Kljun footprint module passing lists of required parameters and then call the plotting routine FFP_output1=Footprint_climatology.FFP_climatology(zm_list,z0_list,umean_list,h_list,ol_list,sigmav_list,ustar_list,[-250 ,250, -250 ,250],wind_dir_list,None,None) Footprint_climatology.plot_footprint(FFP_output1['x_2d'], FFP_output1['y_2d'], FFP_output1['fclim_2d'], None , True, None, None, 0.3, nightdata,night_day_label,year_label,month_label,myBaseforResults_FFP,Site_ID) #Do for Night and then day seperately daydata=group_x[group_x['day_night']==1] night_day_label = 'Day' zm_list = daydata['zm'].values z0_list = daydata['z0'].values umean_list = daydata['umean'].values h_list = daydata['h'].values ol_list = daydata['ol'].values sigmav_list = daydata['vv'].values ustar_list = daydata['ustar'].values wind_dir_list = daydata['Wd'].values # Call Kljun footprint module passing lists of required parameters and then call the plotting routine FFP_output1=Footprint_climatology.FFP_climatology(zm_list,z0_list,umean_list,h_list,ol_list,sigmav_list,ustar_list,[-250 ,250, -250 ,250],wind_dir_list) Footprint_climatology.plot_footprint(FFP_output1['x_2d'], FFP_output1['y_2d'], FFP_output1['fclim_2d'], None , True, None, None, 0.3, daydata,night_day_label,year_label,month_label,myBaseforResults_FFP,Site_ID) #Now do Wind rose plots #Create a small DF for Wind rose plots DF_windrose=FFP_Dataframe[['umean','Wd','day_night']] DF_windrose.columns = ['speed', 'direction','day_night'] by = lambda x: lambda y: getattr(y, x) if do_windrose_climatology_plots == True: #Define which type of plot(s) to do. Create a list and plot different types as you want #kind : kind of plot (might be either, 'contour', 'contourf', 'bar', 'box', 'pdf') plotkinds=['box','contourf','pdf'] #Colourmap type (i.e. hot, summer, winter, plasma, etc.) cmtype = cm.plasma for kindrose in plotkinds: #Do ALL data plot first DF_windrose_temp=DF_windrose #Do Windrose for all startyear=str(DF_windrose_temp.index[0].year) endyear=str(DF_windrose_temp.index[-1].year) print "Doing Windrose for year " + startyear + '_to_' + endyear fig = plt.figure(1, figsize=(6, 6), dpi=300, facecolor='w', edgecolor='k') plot_windrose(DF_windrose_temp, kind=kindrose, bins=np.arange(0.01,8,1), cmap=cmtype, lw=2) plt.suptitle('Windrose climatology'+Site_ID + ' '+ startyear + '_to_' + endyear,size=16) #plt.show() plt.savefig(myBaseforResults_windrose+'/Annual'+'/Windrose climatology '+Site_ID + '_for_' + startyear + '_to_' + endyear +'_' + kindrose+'_' + versionID) print 'Saving Windrose_climatology_'+Site_ID + '_' + startyear + '_to_' + endyear +'_' + kindrose #Do annual plots first DF_windrose_temp=DF_windrose.groupby([by('year')]) for group_index, group_x in DF_windrose_temp: #Do Windrose for all year_label=str (group_index) print "Doing Windrose for year " + year_label fig = plt.figure(1, figsize=(6, 6), dpi=300, facecolor='w', edgecolor='k') plot_windrose(group_x, kind=kindrose, bins=np.arange(0.01,8,1), cmap=cmtype, lw=2) plt.suptitle('Windrose climatology'+Site_ID + ' '+year_label,size=16) #plt.show() plt.savefig(myBaseforResults_windrose+'/Annual'+'/Windrose climatology '+Site_ID + ' '+year_label+'_' + kindrose+'_'+versionID) print 'Saving Windrose_climatology_'+Site_ID + '_'+year_label +'_' + kindrose +'_'+versionID #Then do monthly plots DF_windrose_temp=DF_windrose.groupby([by('year'),by('month')]) for group_index, group_x in DF_windrose_temp: #Do Windrose for night year_label=str (group_index[0]) month_label=str(group_index[1]) print "Doing Windrose for year " + year_label + "and " + month_label + " month" #Do for Night and then day seperately nightdata=group_x[group_x['day_night']==3] night_day_label = 'Night' fig = plt.figure(1, figsize=(6, 6), dpi=300, facecolor='w', edgecolor='k') plot_windrose(nightdata, kind=kindrose, bins=np.arange(0.01,8,1), cmap=cmtype, lw=2) plt.suptitle('Windrose climatology'+Site_ID + ' '+year_label+' Month '+month_label + ' and '+night_day_label+'_'+versionID,size=16) #plt.show() plt.savefig(myBaseforResults_windrose+'/Monthly'+'/Windrose climatology '+Site_ID + ' '+year_label+' Month '+month_label + ' and '+night_day_label+'_'+kindrose+'_'+versionID) print 'Saving Windrose_climatology_'+Site_ID + '_'+year_label+'_month_'+month_label + '_'+night_day_label+'_'+kindrose+'_'+versionID #Do WIndrose for Day year_label=str (group_index[0]) month_label=str(group_index[1]) print "Doing Windrose for year " + year_label + "and " + month_label + " month" #Do for Night and then day seperately daydata=group_x[group_x['day_night']==1] night_day_label = 'Day' fig = plt.figure(1, figsize=(6, 6), dpi=300, facecolor='w', edgecolor='k') plot_windrose(daydata, kind=kindrose, bins=np.arange(0.01,8,1), cmap=cmtype, lw=2) plt.suptitle('Windrose climatology'+Site_ID + ' '+year_label+' Month '+month_label + ' and '+night_day_label+'_'+versionID,size=16) #plt.show() plt.savefig(myBaseforResults_windrose+'/Monthly'+'/Windrose climatology '+Site_ID + ' '+year_label+' Month '+month_label + ' and '+night_day_label+'_'+kindrose+'_'+versionID) print 'Saving Windrose_climatology_'+Site_ID + '_'+year_label+'_month_'+month_label + '_'+night_day_label+'_'+kindrose+'_'+versionID print "Finished"
def test_windrose_pandas(): bins = np.arange(0.01, 8, 1) kind = "scatter" plot_windrose(df, kind=kind, alpha=0.2) plt.savefig("tests/output/df/%s.png" % kind) plt.close() kind = "bar" plot_windrose(df, kind=kind, normed=True, opening=0.8, edgecolor="white") plt.savefig("tests/output/df/%s.png" % kind) plt.close() kind = "box" plot_windrose(df, kind=kind, bins=bins) plt.savefig("tests/output/df/%s.png" % kind) plt.close() kind = "contourf" plot_windrose(df, kind=kind, bins=bins, cmap=cm.hot) plt.savefig("tests/output/df/%s.png" % kind) plt.close() kind = "contour" plot_windrose(df, kind=kind, bins=bins, cmap=cm.hot, lw=3) plt.savefig("tests/output/df/%s.png" % kind) plt.close() kind = "pdf" plot_windrose(df, kind=kind, bins=bins) plt.savefig("tests/output/df/%s.png" % kind) plt.close()
#%% ax = WindroseAxes.from_ax() #ax.contourf(total_data_filtered["9"], total_data_filtered["8"], bins=np.arange(0, 6, 0.5), cmap=cm.BuPu) ax.contourf(total_data_filtered["9"], total_data_filtered["8"], normed=True, bins=np.arange(0, 6, 0.5), cmap=cm.BuPu) #ax.contourf(direzioni, velocità, bins=np.arange(0, 6, 0.5), cmap=cm.BuPu) # per farlo con quelli dell'arpae #ax.contour(direzioni, velocità, bins=np.arange(0, 6, 0.5), colors='black') per avere anche i contorni neri delle varie zone ax.set_legend() ax.set_title("spring 2018, 2c") #%% # this is to produce the PDF from windrose import plot_windrose df = pd.DataFrame({ 'speed': total_data_filtered["8"], 'direction': total_data_filtered["9"] }) plot_windrose(df, kind='pdf', bins=np.arange(0.01, 8, 1), cmap=cm.hot, lw=3, color="crimson") #%%
from windrose import plot_windrose from matplotlib import pyplot as plt import matplotlib.cm as cm import numpy as np df = pd.read_csv("samples/sample_wind_poitiers.csv", parse_dates=['Timestamp']) #df['Timestamp'] = pd.to_timestamp() df = df.set_index('Timestamp') #N = 500 #ws = np.random.random(N) * 6 #wd = np.random.random(N) * 360 #df = pd.DataFrame({'speed': ws, 'direction': wd}) print(df) print(df.dtypes) bins = np.arange(0.01, 8, 1) plot_windrose(df, kind='contour', bins=bins, cmap=cm.hot, lw=3) plt.show() bins = np.arange(0, 30 + 1, 1) bins = bins[1:] ax, params = plot_windrose(df, kind='pdf', bins=bins) print("Weibull params:") print(params) #plt.savefig("screenshots/pdf.png") plt.show()
def point_cloud_generator(number_of_runs): # 3 def getEndpoint(lat1, lon1, bearings, float_distance): R = 6371 # Radius of Earth, roughly distance = float_distance brng = np.deg2rad(bearings) lat1 = np.deg2rad(lat1) lon1 = np.deg2rad(lon1) lat2 = np.arcsin( np.sin(lat1) * np.cos(distance / R) + np.cos(lat1) * np.sin(distance / R) * np.cos(brng)) lon2 = lon1 + np.arctan2( np.sin(brng) * np.sin(distance / R) * np.cos(lat1), np.cos(distance / R) - np.sin(lat1) * np.sin(lat2)) lat2 = np.rad2deg(lat2) lon2 = np.rad2deg(lon2) return lat2, lon2 #%% # Generate Wind and current vectors (directions and speeds), according to weighted probabilities # Meteorological convention for wind, oceanographic convention for current n1 = range(338, 361, 1) n2 = range(1, 23, 1) ne = range(23, 68, 1) e = range(68, 113, 1) se = range(113, 158, 1) s = range(158, 203, 1) sw = range(203, 248, 1) w = range(248, 293, 1) nw = range(293, 338, 1) # Find the probabilities with non_random.py n1_prob1 = 2 n2_prob1 = 2 ne_prob1 = 2 e_prob1 = 4 se_prob1 = 80 s_prob1 = 4 sw_prob1 = 2 w_prob1 = 2 nw_prob1 = 2 n1_prob2 = 2 n2_prob2 = 2 ne_prob2 = 2 e_prob2 = 40 se_prob2 = 40 s_prob2 = 4 sw_prob2 = 2 w_prob2 = 2 nw_prob2 = 2 # 4 # Each range of integers appears in the list a weighted number of times. weighted_wd1 = (n1 * n1_prob1) + (n2 * n2_prob1) + (ne * ne_prob1) + ( e * e_prob1) + (se * se_prob1) + (s * s_prob1) + (sw * sw_prob1) + ( w * w_prob1) + (nw * nw_prob1) weighted_wd2 = (n1 * n1_prob2) + (n2 * n2_prob2) + (ne * ne_prob2) + ( e * e_prob2) + (se * se_prob2) + (s * s_prob2) + (sw * sw_prob2) + ( w * w_prob2) + (nw * nw_prob2) #weighted_wd = range(1, 82, 1) * 50 + range(82, 119, 1) * 25 + range(119, 193, 1) * 15 + range(193, 361, 1) * 10 # Meteorological convention weighted_ws1 = range(0, 801, 1) * 65 + range(801, 1501, 1) * 20 + range( 1501, 2001, 1) * 12 + range(2001, 10000, 1) * 3 # cm/second weighted_ws2 = range(0, 801, 1) * 65 + range(801, 1501, 1) * 20 + range( 1501, 2001, 1) * 12 + range(2001, 10000, 1) * 3 # cm/second weighted_cd = range(0, 46, 1) * 35 + range(46, 91, 1) * 35 + range( 340, 360, 1) * 30 # Oceanographic convention weighted_cs = range(60, 101, 1) # cm/second # 5 # Randomly select from weighted variables according to number of total_steps total_steps = 432 # Each step is 10 minutes (600 seconds) # Wind data (two wind vectors, one each from the SMKF1 and MLRF1 buoys) random_wd1 = np.random.choice(weighted_wd1, total_steps) random_ws1 = np.random.choice(weighted_ws1, total_steps) final_ws1 = (random_ws1 + 0.) / 100 # Speeds now in meters/sec plot_df1 = pd.DataFrame({'speed': final_ws1, 'direction': random_wd1}) plot_windrose(plot_df1, kind='box', bins=np.arange(0.01, 20, 1), cmap=cm.GnBu_r, lw=5) # can use kind='pdf' for probability density function random_wd2 = np.random.choice(weighted_wd2, total_steps) random_ws2 = np.random.choice(weighted_ws2, total_steps) final_ws2 = (random_ws2 + 0.) / 100 # Speeds now in meters/sec plot_df2 = pd.DataFrame({'speed': final_ws2, 'direction': random_wd2}) plot_windrose(plot_df2, kind='box', bins=np.arange(0.01, 20, 1), cmap=cm.GnBu_r, lw=5) # can use kind='pdf' for probability density function # Ocean current data random_cd = np.random.choice(weighted_cd, total_steps) random_cs = np.random.choice(weighted_cs, total_steps) final_cs = (random_cs + 0.) / 100 # Speeds now in meters/sec # Build data array of all directions and speeds my_data = np.stack( (random_wd1, final_ws1, random_wd2, final_ws2, random_cd, final_cs), axis=1) #print("Random selection of 10 weighted variables for wind and current direction and speed, in array (my_data): ") #pprint.pprint(my_data) # Backup data data_copy = my_data.copy() phi1 = -data_copy[:, 0] - 90 # DO I SUBTRACT 90? u_wd1 = data_copy[:, 1] * np.cos(np.deg2rad(phi1)) v_wd1 = data_copy[:, 1] * np.sin(np.deg2rad(phi1)) phi2 = -data_copy[:, 2] - 90 # DO I SUBTRACT 90? u_wd2 = data_copy[:, 3] * np.cos(np.deg2rad(phi2)) v_wd2 = data_copy[:, 3] * np.sin(np.deg2rad(phi2)) # Ocean u/v vector components, with bearing degrees converted to radians theta = 90 - data_copy[:, 4] u_cur = data_copy[:, 5] * np.cos(np.deg2rad(theta)) v_cur = data_copy[:, 5] * np.sin(np.deg2rad(theta)) # Create array of wind and ocean U and V vectors uv_stack = np.stack((u_wd1, v_wd1, u_wd2, v_wd2, u_cur, v_cur), axis=1) #print("U/V wind and current vector array (uv_stack): ") #pprint.pprint(uv_stack) # 7 # Calculate the final vectors U_vector = uv_stack[:, 0] + uv_stack[:, 2] + uv_stack[:, 4] V_vector = uv_stack[:, 1] + uv_stack[:, 3] + uv_stack[:, 5] vector_stack = np.stack((U_vector, V_vector), axis=1) #print("Summed U/V vector array (vector_stack): ") #pprint.pprint(vector_stack) bearings = ( 90 - np.arctan2(vector_stack[:, 1], vector_stack[:, 0]) * 180 / np.pi ) % 360 # Radians converted to degrees with 180/pi, returns bearing on unit circle speeds = np.sqrt(vector_stack[:, 0]**2, vector_stack[:, 1]**2) #route = np.stack((bearings, speeds), axis=1) # ADD 180 to the bearing, without exceeding 360..? #print("This is the route and the speed for each step in the series (route):") #pprint.pprint(route) # mean_bearing = np.mean(route, axis=0) # print "The mean direction and speed of the vector are (unit circle degrees, meters/sec): ", mean_bearing #%% # 8 Almiranta,24.81055,-80.76553333 (start_lat, start_lon) = 24.81055, -80.76553333 lat1 = float(start_lat) lon1 = float(start_lon) # print "Starting latitude and longitude: ", lat1, lon1 # 9 Calculate the distance the object floats each step using the speed,time, and drag. density = 550 # kg/m**3, hypothetical object density. Use Archimedes principle: http://www.dummies.com/education/science/physics/understanding-buoyancy-using-archimedess-principle/ refarea = 3.6 # approximate area of object (meters?) drag_coeff = 0.09 # drag coefficient drag = (((density * (speeds**2)) / 2) * drag_coeff * refarea) travel_time = 600 # seconds (10 minutes), or 0.166 hours float_distance = (speeds * travel_time) / (drag * travel_time) (newlat, newlon) = getEndpoint(lat1, lon1, bearings[0], float_distance) course = np.stack((newlat, newlon), axis=1) date_rng = pd.date_range('1/1/1733', periods=total_steps, freq='10Min') course = np.insert(course, 2, date_rng, axis=1) # print "The object follows this course: " # print(course) # Export results, then plot... # plt.scatter(newlat, newlon) return course