def field_map_ndar(ndar_field,t,ar_coorx,ar_coory,X,image_out,variable): ar_field=ndar_field[t,:] max_val=int(np.max(ndar_field)) if variable==4: max_val=100. xmin=min(ar_coorx);xmax=max(ar_coorx) ymin=min(ar_coory);ymax=max(ar_coory) step=X nx=(xmax-xmin)/step+1 ny=(ymax-ymin)/step+1 ar_indx=np.array((ar_coorx-xmin)/step,int) ar_indy=np.array((ar_coory-ymin)/step,int) ar_map=np.ones((ny,nx))*-99.9 ar_map[ar_indy,ar_indx]=ar_field ar_map2 = M.masked_where(ar_map <0, ar_map) ut.check_file_exist(image_out) pl.clf() pl.axes(axisbg='gray') pl.imshow(ar_map2, cmap=pl.cm.RdBu, interpolation='Nearest', origin='lower', vmax=max_val, vmin=0) pl.title('time step= '+ut.string(t,len(str(t)))) pl.colorbar() pl.savefig(image_out)
def field_map(ar_field, ar_coorx, ar_coory, X, picture_out, title, flip=0): """ Plot an overview depicting the location of the subcatchment. """ max_val=max(ar_field) xmin=min(ar_coorx);xmax=max(ar_coorx) ymin=min(ar_coory);ymax=max(ar_coory) step=X nx=(xmax-xmin)/step+1 ny=(ymax-ymin)/step+1 ar_indx=np.array((ar_coorx-xmin)/step,int) ar_indy=np.array((ar_coory-ymin)/step,int) ar_map=np.ones((ny,nx))*-99.9 ar_map[ar_indy,ar_indx]=ar_field if flip==1: ar_map=np.flipud(ar_map) ar_map2 = ma.masked_where(ar_map <0, ar_map) ut.check_file_exist(picture_out) pl.clf() pl.imshow(ar_map2, interpolation='Nearest', origin='lower', vmax=max_val,vmin=0) pl.title(title) pl.colorbar() pl.savefig(picture_out)
def field_map(ar_field,ar_coorx,ar_coory,X,image_out,title,flip=0,min_val=0.,max_val=0.): import pylab as pl import numpy.ma as M #max_val=max(ar_field) xmin=min(ar_coorx);xmax=max(ar_coorx) ymin=min(ar_coory);ymax=max(ar_coory) step=X nx=(xmax-xmin)/step+1 ny=(ymax-ymin)/step+1 ar_indx=np.array((ar_coorx-xmin)/step,int) ar_indy=np.array((ar_coory-ymin)/step,int) ar_map=np.ones((ny,nx))*-99.9 ar_map[ar_indy,ar_indx]=ar_field if flip==1: ar_map=np.flipud(ar_map) ar_map2 = M.masked_where(ar_map <0, ar_map) ut.check_file_exist(image_out) pl.clf() pl.imshow(ar_map2,interpolation='Nearest',origin='lower',vmax=max_val,vmin=min_val) pl.title(title) pl.colorbar() pl.savefig(image_out)
def field_map2(ar_field, ar_coorx, ar_coory, X, image_out, title, flip=0, min_val=0., max_val=0.): import pylab as pl import numpy.ma as M #max_val=max(ar_field) xmin = min(ar_coorx) xmax = max(ar_coorx) ymin = min(ar_coory) ymax = max(ar_coory) step = X nx = (xmax - xmin) / step + 1 ny = (ymax - ymin) / step + 1 ar_indx = np.array((ar_coorx - xmin) / step, int) ar_indy = np.array((ar_coory - ymin) / step, int) ar_map = np.ones((ny, nx)) * -99.9 ar_map[ar_indy, ar_indx] = ar_field if flip == 1: ar_map = np.flipud(ar_map) ar_map2 = ar_map ut.check_file_exist(image_out) pl.clf() pl.imshow(ar_map2, interpolation='Nearest', origin='lower', vmax=max_val, vmin=min_val) pl.title(title) pl.colorbar() pl.savefig(image_out)
def field_map(ar_field, ar_coorx, ar_coory, X, picture_out, title, flip=0): """ Plot an overview depicting the location of the subcatchment. """ max_val = max(ar_field) xmin = min(ar_coorx) xmax = max(ar_coorx) ymin = min(ar_coory) ymax = max(ar_coory) step = X nx = (xmax - xmin) / step + 1 ny = (ymax - ymin) / step + 1 ar_indx = np.array((ar_coorx - xmin) / step, int) ar_indy = np.array((ar_coory - ymin) / step, int) ar_map = np.ones((ny, nx)) * -99.9 ar_map[ar_indy, ar_indx] = ar_field if flip == 1: ar_map = np.flipud(ar_map) ar_map2 = ma.masked_where(ar_map < 0, ar_map) ut.check_file_exist(picture_out) pl.clf() pl.imshow(ar_map2, interpolation='Nearest', origin='lower', vmax=max_val, vmin=0) pl.title(title) pl.colorbar() pl.savefig(picture_out)
def plot_sim_observed(hydrograph_fname, simulation_folder, file_Qobs, outlet_ID): ''' Parameters ---------- image_out: fname for image of hydrographs file_Qobs: path to text file containing conserved value series outlet_ID: outlet_ID Returns ------- list1 -> list, error checking parameters such as nash, rsme etc. list2 -> list, Q_simulated ''' import pytopkapi.utils as ut from pytopkapi.results_analysis import plot_Qsim_Qobs_Rain as pt import matplotlib.pyplot as plt from matplotlib.dates import date2num from datetime import datetime file_Qsim = simulation_folder + "/results/results.h5" group_name = 'sample_event' Qobs = True Pobs = False nash = True tab_col = ['k', 'r'] tab_style = ['-', '-'] tab_width = ['1', '1'] color_P = 'b' transparency_P = 0.5 #(0 for invisible) image_out = simulation_folder + '/results/calibration/' + hydrograph_fname + ".png" if hydrograph_fname == '': image_out = simulation_folder + '/results/calibration/Result_' + str( datetime.now()).replace(':', '-')[:-7] + '.png' #create path_out if it does'nt exist ut.check_file_exist(image_out) #Read the obs #Qobs ar_date, ar_Qobs = pt.read_observed_flow(file_Qobs) delta = date2num(ar_date[1]) - date2num(ar_date[0]) # #Rain # if Pobs: # h5file = h5py.File(file_rain) # # dset_string = '/%s/rainfall' % group_name # ndar_rain = h5file[dset_string][...] # # h5file.close() # #Compute the mean catchment rainfall # ar_rain=np.average(ndar_rain,axis=1) #Read the simulated data Q file_h5 = file_Qsim ndar_Qc_out = ut.read_one_array_hdf(file_h5, 'Channel', 'Qc_out') ar_Qsim = ndar_Qc_out[1:, outlet_ID] ##Graph fig, ax = plt.subplots() lines = [] tab_leg = [] if Qobs: lines += ax.plot(ar_date, ar_Qobs, color=tab_col[-1], linestyle=tab_style[-1], linewidth=tab_width[-1]) tab_leg.append(('Observation')) tab_leg = tab_leg[::-1] # extended slicing. This Reverses the order lines += ax.plot(ar_date, ar_Qsim, color=tab_col[0], linestyle=tab_style[0], linewidth=tab_width[0]) tab_leg.append('Model') if nash: nash_value = ut.Nash(ar_Qsim, ar_Qobs) lines += ax.plot(ar_date[0:1], ar_Qsim[0:1], 'w:') tab_leg.append(('Eff = ' + str(nash_value)[0:5])) RMSE = ut.RMSE(ar_Qsim, ar_Qobs) lines += ax.plot(ar_date[0:1], ar_Qsim[0:1], 'w:') tab_leg.append(('RMSE = ' + str(RMSE)[0:5])) RMSE_norm = ut.RMSE_norm(ar_Qsim, ar_Qobs) lines += ax.plot(ar_date[0:1], ar_Qsim[0:1], 'w:') tab_leg.append(('RMSE_norm = ' + str(RMSE_norm)[0:5])) Bias_cumul = ut.Bias_cumul(ar_Qsim, ar_Qobs) lines += ax.plot(ar_date[0:1], ar_Qsim[0:1], 'w:') tab_leg.append(('Bias_cumul = ' + str(Bias_cumul)[0:5])) Diff_cumul = ut.Diff_cumul(ar_Qsim, ar_Qobs) lines += ax.plot(ar_date[0:1], ar_Qsim[0:1], 'w:') tab_leg.append(('Diff_cumul = ' + str(Diff_cumul)[0:5])) Abs_cumul = ut.Abs_cumul(ar_Qsim, ar_Qobs) lines += ax.plot(ar_date[0:1], ar_Qsim[0:1], 'w:') tab_leg.append(('Abs_cumul = ' + str(Abs_cumul)[0:5])) Err_cumul = ut.Err_cumul(ar_Qsim, ar_Qobs) lines += ax.plot(ar_date[0:1], ar_Qsim[0:1], 'w:') tab_leg.append(('Err_cumul = ' + str(Err_cumul)[0:5])) # added to include all in the label ax.set_xlim(ar_date[0], ar_date[-1]) ytitle = r'$Q \ (m^3/s)$' ax.set_ylabel(ytitle, fontsize=18) ax.set_title("Calib:Param_" + hydrograph_fname) ax2 = ax.twinx() # ax2.set_ylabel(r'$Rainfall \ (mm)$', fontsize=18, color=color_P) # ax2.bar(ar_date, ar_rain, width=delta, # facecolor='blue', edgecolor='blue', alpha=transparency_P) # ax2.set_ylim(max(ar_rain)*2, min(ar_rain)) ax2.legend(lines, tab_leg, loc='upper right', fancybox=True) leg = ax2.get_legend() leg.get_frame().set_alpha(0.75) for label in leg.get_texts(): label.set_fontsize('8') # rotate and align the tick labels so they look better, # unfortunately autofmt_xdate doesn't work with twinx due to a bug # in matplotlib <= 1.0.0 so we do it manually ## fig.autofmt_xdate() bottom = 0.2 rotation = 30 ha = 'right' for ax in fig.get_axes(): if hasattr(ax, 'is_last_row') and ax.is_last_row(): for label in ax.get_xticklabels(): label.set_ha(ha) label.set_rotation(rotation) else: for label in ax.get_xticklabels(): label.set_visible(False) ax.set_xlabel('') fig.subplots_adjust(bottom=bottom) fig.savefig(image_out) #plt.show() # RMSE= ut.RMSE(ar_Qsim,ar_Qobs) # RMSE_norm = ut.RMSE_norm(ar_Qsim,ar_Qobs) # Bias_cumul= ut.Bias_cumul(ar_Qsim,ar_Qobs) # Diff_cumul= ut.Diff_cumul(ar_Qsim,ar_Qobs) # Abs_cumul = ut.Abs_cumul(ar_Qsim,ar_Qobs) # Err_cumul = ut.Err_cumul(ar_Qsim,ar_Qobs) error_checking_param = [ str(nash_value)[0:5], str(RMSE)[0:5], str(RMSE_norm)[0:5], str(Bias_cumul)[0:5], str(Diff_cumul)[0:5], str(Abs_cumul)[0:5], str(Err_cumul)[0:5] ] return error_checking_param, ar_Qsim
def run(ini_file='plot_Qsim_Qobs_Rain.ini'): config = SafeConfigParser() config.read(ini_file) print('Read the file ', ini_file) file_Qsim = config.get('files', 'file_Qsim') file_Qobs = config.get('files', 'file_Qobs') file_rain = config.get('files', 'file_rain') image_out = config.get('files', 'image_out') group_name = config.get('groups', 'group_name') Qobs = config.getboolean('flags', 'Qobs') Pobs = config.getboolean('flags', 'Pobs') nash = config.getboolean('flags', 'nash') tab_col = ['k', 'r'] tab_style = ['-', '-'] tab_width = ['1', '1'] color_P = 'b' transparency_P = 0.5 #(0 for invisible) #create path_out if it does'nt exist ut.check_file_exist(image_out) #Read the obs #Qobs ar_date, ar_Qobs = read_observed_flow(file_Qobs) delta = date2num(ar_date[1]) - date2num(ar_date[0]) #Rain if Pobs: h5file = h5py.File(file_rain) dset_string = '/%s/rainfall' % group_name ndar_rain = h5file[dset_string][...] h5file.close() #Compute the mean catchment rainfall ar_rain = np.average(ndar_rain, axis=1) #Read the simulated data Q file_h5 = file_Qsim ndar_Qc_out = ut.read_one_array_hdf(file_h5, 'Channel', 'Qc_out') ar_Qsim = ndar_Qc_out[1:, 0] ##Graph fig, ax = plt.subplots() lines = [] tab_leg = [] if Qobs: lines += ax.plot(ar_date, ar_Qobs, color=tab_col[-1], linestyle=tab_style[-1], linewidth=tab_width[-1]) tab_leg.append(('Observation')) tab_leg = tab_leg[::-1] lines += ax.plot(ar_date, ar_Qsim, color=tab_col[0], linestyle=tab_style[0], linewidth=tab_width[0]) tab_leg.append('Model') if nash: nash_value = ut.Nash(ar_Qsim, ar_Qobs) lines += ax.plot(ar_date[0:1], ar_Qsim[0:1], 'w:') tab_leg.append(('Eff = ' + str(nash_value)[0:5])) ax.set_xlim(ar_date[0], ar_date[-1]) ytitle = r'$Q \ (m^3/s)$' ax.set_ylabel(ytitle, fontsize=18) ax.set_title(group_name) ax2 = ax.twinx() ax2.set_ylabel(r'$Rainfall \ (mm)$', fontsize=18, color=color_P) ax2.bar(ar_date, ar_rain, width=delta, facecolor='blue', edgecolor='blue', alpha=transparency_P) ax2.set_ylim(max(ar_rain) * 2, min(ar_rain)) ax2.legend(lines, tab_leg, loc='upper right', fancybox=True) leg = ax2.get_legend() leg.get_frame().set_alpha(0.75) # rotate and align the tick labels so they look better, # unfortunately autofmt_xdate doesn't work with twinx due to a bug # in matplotlib <= 1.0.0 so we do it manually ## fig.autofmt_xdate() bottom = 0.2 rotation = 30 ha = 'right' for ax in fig.get_axes(): if hasattr(ax, 'is_last_row') and ax.is_last_row(): for label in ax.get_xticklabels(): label.set_ha(ha) label.set_rotation(rotation) else: for label in ax.get_xticklabels(): label.set_visible(False) ax.set_xlabel('') fig.subplots_adjust(bottom=bottom) fig.savefig(image_out) plt.show()
def run(ini_file='plot_Qsim_Qobs_Rain.ini'): config = SafeConfigParser() config.read(ini_file) print 'Read the file ',ini_file file_Qsim=config.get('files','file_Qsim') file_Qobs=config.get('files','file_Qobs') file_rain=config.get('files','file_rain') image_out=config.get('files','image_out') group_name=config.get('groups','group_name') Qobs=config.getboolean('flags','Qobs') Pobs=config.getboolean('flags','Pobs') nash=config.getboolean('flags','nash') tab_col=['k','r'] tab_style=['-','-'] tab_width=['1','1'] color_P='b' transparency_P=0.5#(0 for invisible) #create path_out if it does'nt exist ut.check_file_exist(image_out) #Read the obs #Qobs ar_date, ar_Qobs = read_observed_flow(file_Qobs) delta = date2num(ar_date[1]) - date2num(ar_date[0]) #Rain if Pobs: h5file_in=h5.openFile(file_rain,mode='r') group='/'+group_name+'/' node = h5file_in.getNode(group+'rainfall') ndar_rain=node.read() h5file_in.close() #Compute the mean catchment rainfall ar_rain=np.average(ndar_rain,axis=1) #Read the simulated data Q file_h5=file_Qsim ndar_Qc_out=ut.read_one_array_hdf(file_h5,'/Channel/','Qc_out') ar_Qsim=ndar_Qc_out[1:,0] ##Graph fig, ax = plt.subplots() lines = [] tab_leg = [] if Qobs: lines += ax.plot(ar_date, ar_Qobs, color=tab_col[-1], linestyle=tab_style[-1], linewidth=tab_width[-1]) tab_leg.append(('Observation')) tab_leg = tab_leg[::-1] lines += ax.plot(ar_date, ar_Qsim, color=tab_col[0], linestyle=tab_style[0], linewidth=tab_width[0]) tab_leg.append('Model') if nash: nash_value = ut.Nash(ar_Qsim,ar_Qobs) lines += ax.plot(ar_date[0:1], ar_Qsim[0:1], 'w:') tab_leg.append(('Eff = '+str(nash_value)[0:5])) ax.set_xlim(ar_date[0], ar_date[-1]) ytitle=r'$Q \ (m^3/s)$' ax.set_ylabel(ytitle, fontsize=18) ax.set_title(group_name) ax2 = ax.twinx() ax2.set_ylabel(r'$Rainfall \ (mm)$', fontsize=18, color=color_P) ax2.bar(ar_date, ar_rain, width=delta, facecolor='blue', edgecolor='blue', alpha=transparency_P) ax2.set_ylim(max(ar_rain)*2, min(ar_rain)) ax2.legend(lines, tab_leg, loc='upper right', fancybox=True) leg = ax2.get_legend() leg.get_frame().set_alpha(0.75) # rotate and align the tick labels so they look better, # unfortunately autofmt_xdate doesn't work with twinx due to a bug # in matplotlib <= 1.0.0 so we do it manually ## fig.autofmt_xdate() bottom=0.2 rotation=30 ha='right' for ax in fig.get_axes(): if hasattr(ax, 'is_last_row') and ax.is_last_row(): for label in ax.get_xticklabels(): label.set_ha(ha) label.set_rotation(rotation) else: for label in ax.get_xticklabels(): label.set_visible(False) ax.set_xlabel('') fig.subplots_adjust(bottom=bottom) fig.savefig(image_out) plt.show()
def plot_sim_observed(hydrograph_fname, simulation_folder, file_Qobs, outlet_ID): ''' Parameters ---------- image_out: fname for image of hydrographs file_Qobs: path to text file containing conserved value series outlet_ID: outlet_ID Returns ------- list1 -> list, error checking parameters such as nash, rsme etc. list2 -> list, Q_simulated ''' import pytopkapi.utils as ut from pytopkapi.results_analysis import plot_Qsim_Qobs_Rain as pt import matplotlib.pyplot as plt from matplotlib.dates import date2num from datetime import datetime file_Qsim= simulation_folder + "/results/results.h5" group_name= 'sample_event' Qobs= True Pobs= False nash= True tab_col=['k','r'] tab_style=['-','-'] tab_width=['1','1'] color_P='b' transparency_P=0.5#(0 for invisible) image_out = simulation_folder +'/results/calibration/'+ hydrograph_fname+".png" if hydrograph_fname == '': image_out = simulation_folder+'/results/calibration/Result_'+ str(datetime.now()).replace(':','-')[:-7] + '.png' #create path_out if it does'nt exist ut.check_file_exist(image_out) #Read the obs #Qobs ar_date, ar_Qobs = pt.read_observed_flow(file_Qobs) delta = date2num(ar_date[1]) - date2num(ar_date[0]) # #Rain # if Pobs: # h5file = h5py.File(file_rain) # # dset_string = '/%s/rainfall' % group_name # ndar_rain = h5file[dset_string][...] # # h5file.close() # #Compute the mean catchment rainfall # ar_rain=np.average(ndar_rain,axis=1) #Read the simulated data Q file_h5=file_Qsim ndar_Qc_out=ut.read_one_array_hdf(file_h5,'Channel','Qc_out') ar_Qsim=ndar_Qc_out[1:,outlet_ID] ##Graph fig, ax = plt.subplots() lines = [] tab_leg = [] if Qobs: lines += ax.plot(ar_date, ar_Qobs, color=tab_col[-1], linestyle=tab_style[-1], linewidth=tab_width[-1]) tab_leg.append(('Observation')) tab_leg = tab_leg[::-1] # extended slicing. This Reverses the order lines += ax.plot(ar_date, ar_Qsim, color=tab_col[0], linestyle=tab_style[0], linewidth=tab_width[0]) tab_leg.append('Model') if nash: nash_value = ut.Nash(ar_Qsim,ar_Qobs) lines += ax.plot(ar_date[0:1], ar_Qsim[0:1], 'w:') tab_leg.append(('Eff = '+str(nash_value)[0:5])) RMSE = ut.RMSE(ar_Qsim, ar_Qobs) ;lines += ax.plot(ar_date[0:1], ar_Qsim[0:1], 'w:') ; tab_leg.append(('RMSE = '+str(RMSE)[0:5])) RMSE_norm = ut.RMSE_norm(ar_Qsim, ar_Qobs) ;lines += ax.plot(ar_date[0:1], ar_Qsim[0:1], 'w:') ; tab_leg.append(('RMSE_norm = '+str(RMSE_norm)[0:5])) Bias_cumul = ut.Bias_cumul(ar_Qsim, ar_Qobs) ; lines += ax.plot(ar_date[0:1], ar_Qsim[0:1], 'w:') ; tab_leg.append(('Bias_cumul = '+str(Bias_cumul)[0:5])) Diff_cumul = ut.Diff_cumul(ar_Qsim, ar_Qobs) ; lines += ax.plot(ar_date[0:1], ar_Qsim[0:1], 'w:'); tab_leg.append(('Diff_cumul = '+str(Diff_cumul)[0:5])) Abs_cumul = ut.Abs_cumul(ar_Qsim, ar_Qobs) ; lines += ax.plot(ar_date[0:1], ar_Qsim[0:1], 'w:'); tab_leg.append(('Abs_cumul = '+str(Abs_cumul)[0:5])) Err_cumul = ut.Err_cumul(ar_Qsim, ar_Qobs) ; lines += ax.plot(ar_date[0:1], ar_Qsim[0:1], 'w:'); tab_leg.append(('Err_cumul = '+str(Err_cumul)[0:5])) # added to include all in the label ax.set_xlim(ar_date[0], ar_date[-1]) ytitle=r'$Q \ (m^3/s)$' ax.set_ylabel(ytitle, fontsize=18) ax.set_title("Calib:Param_"+hydrograph_fname) ax2 = ax.twinx() # ax2.set_ylabel(r'$Rainfall \ (mm)$', fontsize=18, color=color_P) # ax2.bar(ar_date, ar_rain, width=delta, # facecolor='blue', edgecolor='blue', alpha=transparency_P) # ax2.set_ylim(max(ar_rain)*2, min(ar_rain)) ax2.legend(lines, tab_leg, loc='upper right', fancybox=True) leg = ax2.get_legend() leg.get_frame().set_alpha(0.75) for label in leg.get_texts(): label.set_fontsize('8') # rotate and align the tick labels so they look better, # unfortunately autofmt_xdate doesn't work with twinx due to a bug # in matplotlib <= 1.0.0 so we do it manually ## fig.autofmt_xdate() bottom=0.2 rotation=30 ha='right' for ax in fig.get_axes(): if hasattr(ax, 'is_last_row') and ax.is_last_row(): for label in ax.get_xticklabels(): label.set_ha(ha) label.set_rotation(rotation) else: for label in ax.get_xticklabels(): label.set_visible(False) ax.set_xlabel('') fig.subplots_adjust(bottom=bottom) fig.savefig(image_out) #plt.show() # RMSE= ut.RMSE(ar_Qsim,ar_Qobs) # RMSE_norm = ut.RMSE_norm(ar_Qsim,ar_Qobs) # Bias_cumul= ut.Bias_cumul(ar_Qsim,ar_Qobs) # Diff_cumul= ut.Diff_cumul(ar_Qsim,ar_Qobs) # Abs_cumul = ut.Abs_cumul(ar_Qsim,ar_Qobs) # Err_cumul = ut.Err_cumul(ar_Qsim,ar_Qobs) error_checking_param = [str(nash_value)[0:5], str(RMSE)[0:5],str(RMSE_norm)[0:5],str(Bias_cumul)[0:5],str(Diff_cumul)[0:5],str(Abs_cumul)[0:5],str(Err_cumul)[0:5]] return error_checking_param, ar_Qsim