def run(ini_file='plot_soil_moisture_maps.ini'): config.read(ini_file) print 'Read the file ',ini_file file_global_param=config.get('files','file_global_param') file_cell_param=config.get('files','file_cell_param') file_sim=config.get('files','file_sim') path_out=config.get('paths','path_out') fac_L=config.getfloat('calib_params','fac_L') fac_Ks=config.getfloat('calib_params','fac_Ks') fac_n_o=config.getfloat('calib_params','fac_n_o') fac_n_c=config.getfloat('calib_params','fac_n_c') t1=config.getfloat('flags','t1') t2=config.getfloat('flags','t2') variable=config.getfloat('flags','variable') ##~~~~~~PROGRAM~~~~~## # Create the folder if it doesn't exist ut.check_folder_exist(path_out) #~~~~Read Global parameters file X,Dt,alpha_s,alpha_o,alpha_c,A_thres,W_min,W_max\ =pm.read_global_parameters(file_global_param) #~~~~Read Cell parameters file ar_cell_label, ar_coorx, \ ar_coory, ar_lambda, \ ar_Xc, ar_dam, \ ar_tan_beta, ar_tan_beta_channel, \ ar_L0, ar_Ks0, \ ar_theta_r, ar_theta_s, \ ar_n_o0, ar_n_c0, \ ar_cell_down, ar_pVs_t0, \ ar_Vo_t0, ar_Qc_t0, \ ar_kc, psi_b, lamda = pm.read_cell_parameters(file_cell_param) #~~~~Number of cell in the catchment nb_cell=len(ar_cell_label) #~~~~Computation of cell order ar_label_sort=pm.sort_cell(ar_cell_label,ar_cell_down) #~~~~Computation of upcells li_cell_up=pm.direct_up_cell(ar_cell_label,ar_cell_down,ar_label_sort) #~~~~Computation of drained area ar_A_drained=pm.drained_area(ar_label_sort,li_cell_up,X) #~~~~Modifies the values of the parameters ar_L=ar_L0*fac_L ar_Ks=ar_Ks0*fac_Ks ar_n_o=ar_n_o0*fac_n_o ar_n_c=ar_n_c0*fac_n_c #~~~~Computation of model parameters from physical parameters ar_Vsm, ar_b_s, ar_b_o, ar_W, ar_b_c\ =pm.compute_cell_param(X,ar_Xc,Dt,alpha_s,alpha_o,alpha_c,nb_cell,\ A_thres,W_max,W_min,\ ar_lambda,ar_tan_beta,ar_tan_beta_channel,ar_L,\ ar_Ks,ar_theta_r,ar_theta_s,ar_n_o,ar_n_c,\ ar_A_drained) #Read of data from the outputs of TOPKAPI in hdf5 format ndar_Vs=np.array(ut.read_one_array_hdf(file_sim,'/Soil/','V_s')) #Assign the variables if variable==1: im_out=os.path.join(path_out, 'field_Vs_') tab=ndar_Vs elif variable==2: im_out=os.path.join(path_out, 'field_Vo_') tab=ndar_Vo elif variable==3: im_out=os.path.join(path_out, 'field_Vo_bin_') tab=ndar_Vo tab[tab>0]=1. elif variable==4: im_out=os.path.join(path_out, 'field_SSI_') tab=ndar_Vs/ar_Vsm*100. # Plot the maps for t in np.arange(int(t1),int(t2+1)): print 'Map time-step ', t image_out=im_out+ut.string(t,len(str(t2)))+'.png' field_map_ndar(tab,t,ar_coorx,ar_coory,X,image_out,variable)
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_soil_moisture_maps.ini'): config.read(ini_file) print('Read the file ',ini_file) file_global_param=config.get('files','file_global_param') file_cell_param=config.get('files','file_cell_param') file_sim=config.get('files','file_sim') path_out=config.get('paths','path_out') fac_L=config.getfloat('calib_params','fac_L') fac_Ks=config.getfloat('calib_params','fac_Ks') fac_n_o=config.getfloat('calib_params','fac_n_o') fac_n_c=config.getfloat('calib_params','fac_n_c') t1=config.getfloat('flags','t1') t2=config.getfloat('flags','t2') variable=config.getfloat('flags','variable') ##~~~~~~PROGRAM~~~~~## # Create the folder if it doesn't exist ut.check_folder_exist(path_out) #~~~~Read Global parameters file X,Dt,alpha_s,alpha_o,alpha_c,A_thres,W_min,W_max\ =pm.read_global_parameters(file_global_param) #~~~~Read Cell parameters file ar_cell_label, ar_coorx, \ ar_coory, ar_lambda, \ ar_Xc, ar_dam, \ ar_tan_beta, ar_tan_beta_channel, \ ar_L0, ar_Ks0, \ ar_theta_r, ar_theta_s, \ ar_n_o0, ar_n_c0, \ ar_cell_down, ar_pVs_t0, \ ar_Vo_t0, ar_Qc_t0, \ ar_kc, psi_b, lamda = pm.read_cell_parameters(file_cell_param) #~~~~Number of cell in the catchment nb_cell=len(ar_cell_label) #~~~~Computation of cell order ar_label_sort=pm.sort_cell(ar_cell_label,ar_cell_down) #~~~~Computation of upcells li_cell_up=pm.direct_up_cell(ar_cell_label,ar_cell_down,ar_label_sort) #~~~~Computation of drained area ar_A_drained=pm.drained_area(ar_label_sort,li_cell_up,X) #~~~~Modifies the values of the parameters ar_L=ar_L0*fac_L ar_Ks=ar_Ks0*fac_Ks ar_n_o=ar_n_o0*fac_n_o ar_n_c=ar_n_c0*fac_n_c #~~~~Computation of model parameters from physical parameters ar_Vsm, ar_b_s, ar_b_o, ar_W, ar_b_c\ =pm.compute_cell_param(X,ar_Xc,Dt,alpha_s,alpha_o,alpha_c,nb_cell,\ A_thres,W_max,W_min,\ ar_lambda,ar_tan_beta,ar_tan_beta_channel,ar_L,\ ar_Ks,ar_theta_r,ar_theta_s,ar_n_o,ar_n_c,\ ar_A_drained) #Read of data from the outputs of TOPKAPI in hdf5 format ndar_Vs=np.array(ut.read_one_array_hdf(file_sim,'Soil','V_s')) #Assign the variables if variable==1: im_out=os.path.join(path_out, 'field_Vs_') tab=ndar_Vs elif variable==2: im_out=os.path.join(path_out, 'field_Vo_') tab=ndar_Vo elif variable==3: im_out=os.path.join(path_out, 'field_Vo_bin_') tab=ndar_Vo tab[tab>0]=1. elif variable==4: im_out=os.path.join(path_out, 'field_SSI_') tab=ndar_Vs/ar_Vsm*100. # Plot the maps for t in np.arange(int(t1),int(t2+1)): print('Map time-step ', t) image_out=im_out+ut.string(t,len(str(t2)))+'.png' field_map_ndar(tab,t,ar_coorx,ar_coory,X,image_out,variable)
def mean_simuVsi(ini_file='mean_simuVsi.ini'): """ * Objective """ ### READ THE INI FILE ### config.read(ini_file) print('Read the file ',ini_file) ##~~~~~~ file_in ~~~~~~## file_in=config.get('file_in','file_in') file_in_global=config.get('file_in','file_in_global') file_h5=config.get('file_in','file_h5') ##~~~~~~ file_out ~~~~~~## file_out=config.get('file_out','file_out') ##~~~~~~ variables ~~~~~~## mean_pVs_t0=config.getfloat('variables','mean_pVs_t0') fac_L_simu=config.getfloat('variables','fac_L_simu') fac_Ks_simu=config.getfloat('variables','fac_Ks_simu') fac_n_o_simu=config.getfloat('variables','fac_n_o_simu') fac_n_c_simu=config.getfloat('variables','fac_n_c_simu') ##~~~~~~ flags ~~~~~~## nb_param=config.getfloat('flags','nb_param') #--Read and compute the parameters to have the values of parameter and ar_Vsm #~~~~Read Global parameters file print('Pretreatment of input data') #~~~~Read Global parameters file X,Dt,alpha_s,alpha_o,alpha_c,A_thres,W_min,W_max\ =pm.read_global_parameters(file_in_global) #~~~~Read Cell parameters file ar_cell_label,ar_coorx,ar_coory,ar_lambda,ar_Xc,ar_dam,ar_tan_beta,ar_tan_beta_channel,ar_L,ar_Ks,\ ar_theta_r,ar_theta_s,ar_n_o,ar_n_c,\ ar_cell_down,ar_pVs_t0,ar_Vo_t0,ar_Qc_t0,ar_kc\ =pm.read_cell_parameters(file_in) #~~~~Number of cell in the catchment nb_cell=len(ar_cell_label) #~~~~Computation of cell order ar_label_sort=pm.sort_cell(ar_cell_label,ar_cell_down) #~~~~Computation of upcells li_cell_up=pm.direct_up_cell(ar_cell_label,ar_cell_down,ar_label_sort) #~~~~Computation of drained area ar_A_drained=pm.drained_area(ar_label_sort,li_cell_up,X) #~~~~Modifies the values of the parameters ar_L1=ar_L*fac_L_simu ar_Ks1=ar_Ks*fac_Ks_simu ar_n_o1=ar_n_o*fac_n_o_simu ar_n_c1=ar_n_c*fac_n_c_simu #~~~~Computation of model parameters from physical parameters ar_Vsm, ar_b_s, ar_b_o, ar_W, ar_b_c\ =pm.compute_cell_param(X,ar_Xc,Dt,alpha_s,alpha_o,alpha_c,nb_cell,\ A_thres,W_max,W_min,\ ar_lambda,ar_tan_beta,ar_tan_beta_channel,ar_L1,\ ar_Ks1,ar_theta_r,ar_theta_s,ar_n_o1,ar_n_c1,\ ar_A_drained) #Read the soil volume file ndar_Vs=np.array(ut.read_one_array_hdf(file_h5,'Soil','V_s')) #Read the file of catchment saturation rates ndar_Vs_sat=ndar_Vs/ar_Vsm*100. tab_rate=np.average(ndar_Vs_sat,axis=1) tab_rate_sort=np.sort(tab_rate) indice_sort=np.argsort(tab_rate) #Look for the rate closest to the expected mean value if mean_pVs_t0<tab_rate_sort[0]: ind=0 print('mean_pVs_t0 expected:', mean_pVs_t0,'effective:',tab_rate_sort[0]) elif mean_pVs_t0>tab_rate_sort[-1]: ind=-1 print('mean_pVs_t0 expected:', mean_pVs_t0,' effective',tab_rate_sort[-1]) else: loop=True i=-1 while loop: i=i+1 if mean_pVs_t0>=tab_rate_sort[i] and mean_pVs_t0<tab_rate_sort[i+1]: ind=i loop=False print('mean_pVs_t0 expected:', mean_pVs_t0,' effective:',tab_rate_sort[i]) ind_end=indice_sort[ind] print(ind,ind_end) ar_Vs=ndar_Vs[ind_end,:] ar_Vsi=ar_Vs/ar_Vsm*100. print(ar_Vsi) ar_pVs_t0=ar_Vsi #~~~~~~Write parameter file~~~~~~# tab_param=np.zeros((len(ar_cell_label),nb_param)) tab_param[:,0]=ar_cell_label tab_param[:,1]=ar_coorx tab_param[:,2]=ar_coory tab_param[:,3]=ar_lambda tab_param[:,4]=ar_Xc tab_param[:,5]=ar_dam tab_param[:,6]=ar_tan_beta tab_param[:,7]=ar_tan_beta_channel tab_param[:,8]=ar_L tab_param[:,9]=ar_Ks tab_param[:,10]=ar_theta_r tab_param[:,11]=ar_theta_s tab_param[:,12]=ar_n_o tab_param[:,13]=ar_n_c tab_param[:,14]=ar_cell_down tab_param[:,15]=ar_pVs_t0 tab_param[:,16]=ar_Vo_t0 tab_param[:,17]=ar_Qc_t0 tab_param[:,18]=ar_kc np.savetxt(file_out, tab_param)
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 initial_pVs_Vo_Qc_from_simu(ini_file='initial_pVs_Vo_Qc_from_simu.ini'): """ * Objective """ ### READ THE INI FILE ### config.read(ini_file) print('Read the file ',ini_file) ##~~~~~~ file_in ~~~~~~## file_in=config.get('file_in','file_in') file_in_global=config.get('file_in','file_in_global') file_h5=config.get('file_in','file_h5') ##~~~~~~ file_out ~~~~~~## file_out=config.get('file_out','file_out') ##~~~~~~ variables ~~~~~~## time_step=config.getint('variables','time_step') fac_L_simu=config.getfloat('variables','fac_L_simu') fac_Ks_simu=config.getfloat('variables','fac_Ks_simu') fac_n_o_simu=config.getfloat('variables','fac_n_o_simu') fac_n_c_simu=config.getfloat('variables','fac_n_c_simu') ##~~~~~~ flags ~~~~~~## nb_param=config.getfloat('flags','nb_param') #--Read and compute the parameters to have the values of parameter and ar_Vsm #~~~~Read Global parameters file #~~~~Read Global parameters file X,Dt,alpha_s,alpha_o,alpha_c,A_thres,W_min,W_max\ =pm.read_global_parameters(file_in_global) #~~~~Read Cell parameters file ar_cell_label,ar_coorx,ar_coory,ar_lambda,ar_Xc,ar_dam,ar_tan_beta,ar_tan_beta_channel,ar_L,ar_Ks,\ ar_theta_r,ar_theta_s,ar_n_o,ar_n_c,\ ar_cell_down,ar_pVs_t0,ar_Vo_t0,ar_Qc_t0,ar_kc\ =pm.read_cell_parameters(file_in) #~~~~Number of cell in the catchment nb_cell=len(ar_cell_label) #~~~~Computation of cell order ar_label_sort=pm.sort_cell(ar_cell_label,ar_cell_down) #~~~~Computation of upcells li_cell_up=pm.direct_up_cell(ar_cell_label,ar_cell_down,ar_label_sort) #~~~~Computation of drained area ar_A_drained=pm.drained_area(ar_label_sort,li_cell_up,X) #~~~~Modifies the values of the parameters ar_L1=ar_L*fac_L_simu ar_Ks1=ar_Ks*fac_Ks_simu ar_n_o1=ar_n_o*fac_n_o_simu ar_n_c1=ar_n_c*fac_n_c_simu #~~~~Computation of model parameters from physical parameters ar_Vsm, ar_b_s, ar_b_o, ar_W, ar_b_c\ =pm.compute_cell_param(X,ar_Xc,Dt,alpha_s,alpha_o,alpha_c,nb_cell,\ A_thres,W_max,W_min,\ ar_lambda,ar_tan_beta,ar_tan_beta_channel,ar_L1,\ ar_Ks1,ar_theta_r,ar_theta_s,ar_n_o1,ar_n_c1,\ ar_A_drained) #Read the soil volume file ndar_Vs=np.array(ut.read_one_array_hdf(file_h5,'Soil','V_s')) #Read the overland volume file ndar_Vo=np.array(ut.read_one_array_hdf(file_h5,'Overland','V_o')) #Read the channel dischargefile ndar_Qc=np.array(ut.read_one_array_hdf(file_h5,'Channel','Qc_out')) ar_Vs=ndar_Vs[time_step,:] ar_pVs_t0=ar_Vs/ar_Vsm*100. ar_Vo_t0=ndar_Vo[time_step,:] ar_Qc_t0=ndar_Qc[time_step,:] #~~~~~~Write parameter file~~~~~~# tab_param=np.zeros((len(ar_cell_label),nb_param)) tab_param[:,0]=ar_cell_label tab_param[:,1]=ar_coorx tab_param[:,2]=ar_coory tab_param[:,3]=ar_lambda tab_param[:,4]=ar_Xc tab_param[:,5]=ar_dam tab_param[:,6]=ar_tan_beta tab_param[:,7]=ar_tan_beta_channel tab_param[:,8]=ar_L tab_param[:,9]=ar_Ks tab_param[:,10]=ar_theta_r tab_param[:,11]=ar_theta_s tab_param[:,12]=ar_n_o tab_param[:,13]=ar_n_c tab_param[:,14]=ar_cell_down tab_param[:,15]=ar_pVs_t0 tab_param[:,16]=ar_Vo_t0 tab_param[:,17]=ar_Qc_t0 tab_param[:,18]=ar_kc np.savetxt(file_out, tab_param)
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 mean_simuVsi(ini_file='mean_simuVsi.ini'): """ * Objective """ ### READ THE INI FILE ### config.read(ini_file) print 'Read the file ', ini_file ##~~~~~~ file_in ~~~~~~## file_in = config.get('file_in', 'file_in') file_in_global = config.get('file_in', 'file_in_global') file_h5 = config.get('file_in', 'file_h5') ##~~~~~~ file_out ~~~~~~## file_out = config.get('file_out', 'file_out') ##~~~~~~ variables ~~~~~~## mean_pVs_t0 = config.getfloat('variables', 'mean_pVs_t0') fac_L_simu = config.getfloat('variables', 'fac_L_simu') fac_Ks_simu = config.getfloat('variables', 'fac_Ks_simu') fac_n_o_simu = config.getfloat('variables', 'fac_n_o_simu') fac_n_c_simu = config.getfloat('variables', 'fac_n_c_simu') ##~~~~~~ flags ~~~~~~## nb_param = config.getfloat('flags', 'nb_param') #--Read and compute the parameters to have the values of parameter and ar_Vsm #~~~~Read Global parameters file print 'Pretreatment of input data' #~~~~Read Global parameters file X,Dt,alpha_s,alpha_o,alpha_c,A_thres,W_min,W_max\ =pm.read_global_parameters(file_in_global) #~~~~Read Cell parameters file ar_cell_label,ar_coorx,ar_coory,ar_lambda,ar_Xc,ar_dam,ar_tan_beta,ar_tan_beta_channel,ar_L,ar_Ks,\ ar_theta_r,ar_theta_s,ar_n_o,ar_n_c,\ ar_cell_down,ar_pVs_t0,ar_Vo_t0,ar_Qc_t0,ar_kc\ =pm.read_cell_parameters(file_in) #~~~~Number of cell in the catchment nb_cell = len(ar_cell_label) #~~~~Computation of cell order ar_label_sort = pm.sort_cell(ar_cell_label, ar_cell_down) #~~~~Computation of upcells li_cell_up = pm.direct_up_cell(ar_cell_label, ar_cell_down, ar_label_sort) #~~~~Computation of drained area ar_A_drained = pm.drained_area(ar_label_sort, li_cell_up, X) #~~~~Modifies the values of the parameters ar_L1 = ar_L * fac_L_simu ar_Ks1 = ar_Ks * fac_Ks_simu ar_n_o1 = ar_n_o * fac_n_o_simu ar_n_c1 = ar_n_c * fac_n_c_simu #~~~~Computation of model parameters from physical parameters ar_Vsm, ar_b_s, ar_b_o, ar_W, ar_b_c\ =pm.compute_cell_param(X,ar_Xc,Dt,alpha_s,alpha_o,alpha_c,nb_cell,\ A_thres,W_max,W_min,\ ar_lambda,ar_tan_beta,ar_tan_beta_channel,ar_L1,\ ar_Ks1,ar_theta_r,ar_theta_s,ar_n_o1,ar_n_c1,\ ar_A_drained) #Read the soil volume file ndar_Vs = np.array(ut.read_one_array_hdf(file_h5, '/Soil/', 'V_s')) #Read the file of catchment saturation rates ndar_Vs_sat = ndar_Vs / ar_Vsm * 100. tab_rate = np.average(ndar_Vs_sat, axis=1) tab_rate_sort = np.sort(tab_rate) indice_sort = np.argsort(tab_rate) #Look for the rate closest to the expected mean value if mean_pVs_t0 < tab_rate_sort[0]: ind = 0 print 'mean_pVs_t0 expected:', mean_pVs_t0, 'effective:', tab_rate_sort[ 0] elif mean_pVs_t0 > tab_rate_sort[-1]: ind = -1 print 'mean_pVs_t0 expected:', mean_pVs_t0, ' effective', tab_rate_sort[ -1] else: loop = True i = -1 while loop: i = i + 1 if mean_pVs_t0 >= tab_rate_sort[i] and mean_pVs_t0 < tab_rate_sort[ i + 1]: ind = i loop = False print 'mean_pVs_t0 expected:', mean_pVs_t0, ' effective:', tab_rate_sort[ i] ind_end = indice_sort[ind] print ind, ind_end ar_Vs = ndar_Vs[ind_end, :] ar_Vsi = ar_Vs / ar_Vsm * 100. print ar_Vsi ar_pVs_t0 = ar_Vsi #~~~~~~Write parameter file~~~~~~# tab_param = np.zeros((len(ar_cell_label), nb_param)) tab_param[:, 0] = ar_cell_label tab_param[:, 1] = ar_coorx tab_param[:, 2] = ar_coory tab_param[:, 3] = ar_lambda tab_param[:, 4] = ar_Xc tab_param[:, 5] = ar_dam tab_param[:, 6] = ar_tan_beta tab_param[:, 7] = ar_tan_beta_channel tab_param[:, 8] = ar_L tab_param[:, 9] = ar_Ks tab_param[:, 10] = ar_theta_r tab_param[:, 11] = ar_theta_s tab_param[:, 12] = ar_n_o tab_param[:, 13] = ar_n_c tab_param[:, 14] = ar_cell_down tab_param[:, 15] = ar_pVs_t0 tab_param[:, 16] = ar_Vo_t0 tab_param[:, 17] = ar_Qc_t0 tab_param[:, 18] = ar_kc np.savetxt(file_out, tab_param)
def initial_pVs_Vo_Qc_from_simu(ini_file='initial_pVs_Vo_Qc_from_simu.ini'): """ * Objective """ ### READ THE INI FILE ### config.read(ini_file) print 'Read the file ', ini_file ##~~~~~~ file_in ~~~~~~## file_in = config.get('file_in', 'file_in') file_in_global = config.get('file_in', 'file_in_global') file_h5 = config.get('file_in', 'file_h5') ##~~~~~~ file_out ~~~~~~## file_out = config.get('file_out', 'file_out') ##~~~~~~ variables ~~~~~~## time_step = config.getint('variables', 'time_step') fac_L_simu = config.getfloat('variables', 'fac_L_simu') fac_Ks_simu = config.getfloat('variables', 'fac_Ks_simu') fac_n_o_simu = config.getfloat('variables', 'fac_n_o_simu') fac_n_c_simu = config.getfloat('variables', 'fac_n_c_simu') ##~~~~~~ flags ~~~~~~## nb_param = config.getfloat('flags', 'nb_param') #--Read and compute the parameters to have the values of parameter and ar_Vsm #~~~~Read Global parameters file #~~~~Read Global parameters file X,Dt,alpha_s,alpha_o,alpha_c,A_thres,W_min,W_max\ =pm.read_global_parameters(file_in_global) #~~~~Read Cell parameters file ar_cell_label,ar_coorx,ar_coory,ar_lambda,ar_Xc,ar_dam,ar_tan_beta,ar_tan_beta_channel,ar_L,ar_Ks,\ ar_theta_r,ar_theta_s,ar_n_o,ar_n_c,\ ar_cell_down,ar_pVs_t0,ar_Vo_t0,ar_Qc_t0,ar_kc\ =pm.read_cell_parameters(file_in) #~~~~Number of cell in the catchment nb_cell = len(ar_cell_label) #~~~~Computation of cell order ar_label_sort = pm.sort_cell(ar_cell_label, ar_cell_down) #~~~~Computation of upcells li_cell_up = pm.direct_up_cell(ar_cell_label, ar_cell_down, ar_label_sort) #~~~~Computation of drained area ar_A_drained = pm.drained_area(ar_label_sort, li_cell_up, X) #~~~~Modifies the values of the parameters ar_L1 = ar_L * fac_L_simu ar_Ks1 = ar_Ks * fac_Ks_simu ar_n_o1 = ar_n_o * fac_n_o_simu ar_n_c1 = ar_n_c * fac_n_c_simu #~~~~Computation of model parameters from physical parameters ar_Vsm, ar_b_s, ar_b_o, ar_W, ar_b_c\ =pm.compute_cell_param(X,ar_Xc,Dt,alpha_s,alpha_o,alpha_c,nb_cell,\ A_thres,W_max,W_min,\ ar_lambda,ar_tan_beta,ar_tan_beta_channel,ar_L1,\ ar_Ks1,ar_theta_r,ar_theta_s,ar_n_o1,ar_n_c1,\ ar_A_drained) #Read the soil volume file ndar_Vs = np.array(ut.read_one_array_hdf(file_h5, '/Soil/', 'V_s')) #Read the overland volume file ndar_Vo = np.array(ut.read_one_array_hdf(file_h5, '/Overland/', 'V_o')) #Read the channel dischargefile ndar_Qc = np.array(ut.read_one_array_hdf(file_h5, '/Channel/', 'Qc_out')) ar_Vs = ndar_Vs[time_step, :] ar_pVs_t0 = ar_Vs / ar_Vsm * 100. ar_Vo_t0 = ndar_Vo[time_step, :] ar_Qc_t0 = ndar_Qc[time_step, :] #~~~~~~Write parameter file~~~~~~# tab_param = np.zeros((len(ar_cell_label), nb_param)) tab_param[:, 0] = ar_cell_label tab_param[:, 1] = ar_coorx tab_param[:, 2] = ar_coory tab_param[:, 3] = ar_lambda tab_param[:, 4] = ar_Xc tab_param[:, 5] = ar_dam tab_param[:, 6] = ar_tan_beta tab_param[:, 7] = ar_tan_beta_channel tab_param[:, 8] = ar_L tab_param[:, 9] = ar_Ks tab_param[:, 10] = ar_theta_r tab_param[:, 11] = ar_theta_s tab_param[:, 12] = ar_n_o tab_param[:, 13] = ar_n_c tab_param[:, 14] = ar_cell_down tab_param[:, 15] = ar_pVs_t0 tab_param[:, 16] = ar_Vo_t0 tab_param[:, 17] = ar_Qc_t0 tab_param[:, 18] = ar_kc np.savetxt(file_out, tab_param)
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