""" Plot up files from the Hinwood project. """ from os import sep import project from time import localtime, strftime from Numeric import compress from calc_rmsd import get_max_min_condition_array def plot_compare_csv(location_sim, file_sim, location_exp, file_exp, y_label, save_as=None, plot_title="", x_label='Time (s)', legend_sim='ANUGA simulation', legend_exp='Measured flume result', is_interactive=False, x_axis=None, y_axis=None): """ """ from pylab import ion, plot, xlabel, ylabel, close, legend, \ savefig, title, axis, setp from anuga.shallow_water.data_manager import csv2dict # Load in the csv files and convert info from strings to floats simulation, _ = csv2dict(file_sim) experiment, _ = csv2dict(file_exp) time_sim = [float(x) for x in simulation['time']] quantity_sim = [float(x) for x in simulation[location_sim]] #print "quantity_sim", quantity_sim time_exp = [float(x) for x in experiment['Time']] quantity_exp = [float(x) for x in experiment[location_exp]] if is_interactive: ion() l_sim, l_exp = plot(time_sim, quantity_sim, time_exp, quantity_exp) setp(l_sim, color='r') setp(l_exp, color='b') # Add axis stuff and legend xlabel(x_label) ylabel(y_label) # The order defines the label #legend((legend_exp, legend_sim),'upper left') legend((legend_sim, legend_exp),'upper left') title(plot_title) if x_axis is not None: axis(xmin=x_axis[0], xmax=x_axis[1]) if y_axis is not None: axis(ymin=y_axis[0], ymax=y_axis[1]) if is_interactive: # Wait for enter pressed raw_input() if save_as is not None: savefig(save_as) #Need to close this plot close() def Hinwood_files_locations(run_data, outputdir_tag, plot_type, quantity = "depth", y_location_tag=':0.0'): """ run_data is a dictionary of data describing a Hinwood experiment outputdir_tag a string at the end of an output dir; '_good_tri_area_0.01_A' plot_type the file extension of the plot, eg '.pdf' """ id = run_data['scenario_id'] outputdir_name = id + outputdir_tag pro_instance = project.Project(['data','flumes','Hinwood_2008'], outputdir_name=outputdir_name) file_sim = pro_instance.outputdir + quantity + "_" + id + ".csv" #print "file_exp",file_exp file_exp = pro_instance.raw_data_dir + sep + id + 'pressfilt_exp_' \ + quantity + '.csv' #print "file_sim", file_sim location_sims = [] location_exps = [] save_as_list = [] for gauge_x in run_data['gauge_x']: gauge_x = str(gauge_x) location_sims.append(gauge_x + y_location_tag) location_exps.append(gauge_x) save_as_list.append(pro_instance.plots_dir + sep + \ outputdir_name + "_" + quantity + "_" + \ gauge_x + plot_type) return file_exp, file_sim, location_sims, location_exps, outputdir_name, \ save_as_list def plot_exp_sim_comparision(scenarios, outputdir_tag, quantity = "stage",is_interactive=False, y_location_tag=':0.0'): plot_type = ".pdf" plot_files = {} # Index scenario (T1R3) value, list of files for run_data in scenarios: temp = Hinwood_files_locations(run_data, outputdir_tag, plot_type, quantity, y_location_tag=y_location_tag) file_exp, file_sim, location_sims, location_exps, outputdir_name, \ save_as_list = temp plot_files[run_data['scenario_id']] = save_as_list print "file_exp",file_exp print "run_data['scenario_id']", run_data['scenario_id'] #location_sims = [location_sims[0]] #location_exps = [location_exps[0]] #save_as_list = [save_as_list[0]] for loc_sim, loc_exp, save_as, gauge, gauge_elev in \ map(None, location_sims, location_exps, save_as_list, run_data['gauge_x'], run_data['gauge_bed_elevation']): time_date = strftime('plot date: %d/%m/%Y Time: %H:%M:%S', localtime()) plot_title = "Scenario: " + outputdir_name + "\n" + \ "X Gauge (m):" + str(gauge) + " " + time_date # When the shore gauges out of the are used, don't # use the standard y axis scale x_axis = run_data['wave_times'] if gauge < run_data['axis_maximum_x']: y_axis = [run_data['axis'][2],run_data['axis'][3]] else: y_axis = [run_data['axis'][2] + gauge_elev, run_data['axis'][3] + gauge_elev] print "Doing ", plot_title plot_compare_csv(location_sim=loc_sim, file_sim=file_sim, location_exp=loc_exp, file_exp=file_exp, plot_title=plot_title, y_label='Water '+ quantity +' (m)', is_interactive=is_interactive, save_as=save_as, x_axis=x_axis, y_axis=y_axis) if is_interactive is True: break return plot_files def auto_plot_compare_csv_subfigures(scenarios, outputdir_tag, to_publish_indexes, quantity = "stage",is_interactive=False, y_location_tag=':0.0', add_run_info=False): """ Used by validation graphs to produce the final figures """ plot_type = ".pdf" save_as_list = [] for run_data in scenarios: id = run_data['scenario_id'] if not to_publish_indexes.has_key(id): continue outputdir_name = id + outputdir_tag pro_instance = project.Project(['data','flumes','Hinwood_2008'], outputdir_name=outputdir_name) file_sim = pro_instance.outputdir + quantity + "_" + id + ".csv" file_exp = pro_instance.raw_data_dir + sep + id + 'pressfilt_exp_' \ + quantity + '.csv' if add_run_info is True: plot_title = "Title will not be in final draft \n" + \ id + outputdir_tag else: plot_title = "" #save_as = pro_instance.plots_dir + sep + \ # outputdir_name + "_" + quantity + "_" + plot_type save_as = pro_instance.plots_dir + sep + 'S' + id[1:2] + \ '-' + quantity + '-compare' + plot_type save_as_list.append(save_as) plot_compare_csv_subfigures(file_sim, file_exp, to_publish_indexes[id], run_data, plot_title=plot_title, save_as=save_as, y_label='Water '+ quantity +' (m)', is_interactive=is_interactive, y_location_tag=y_location_tag) return save_as_list def plot_compare_csv_subfigures(file_sim, file_exp, gauge_indexs, run_data, save_as=None, plot_title="", x_label='Time (s)', y_label=None, legend_sim='ANUGA simulation', legend_exp='Measured flume result', is_interactive=False, x_axis=None, y_axis=None, y_location_tag=':0.0'): """ Used by validation graphs to produce the final figures """ from pylab import ion, plot, xlabel, ylabel, close, legend, \ savefig, title, axis, setp, subplot, grid, figlegend, gca, \ text, sqrt import pylab from anuga.shallow_water.data_manager import csv2dict print "file_sim", file_sim if False: fig_width_pt = 246.0 # Get this from LaTeX using \showthe\columnwidth inches_per_pt = 1.0/72.27 # Convert pt to inch golden_mean = (sqrt(5)-1.0)/2.0 # Aesthetic ratio fig_width = fig_width_pt*inches_per_pt # width in inches fig_height = fig_width*golden_mean # height in inches fig_size = [fig_width,fig_height] print "fig_size", fig_size params = {'backend': 'ps', 'axes.labelsize': 10, 'text.fontsize': 10, 'legend.fontsize': 10, 'xtick.labelsize': 8, 'ytick.labelsize': 8, 'text.usetex': True, 'figure.figsize': [3.4, 2.1]} if False: params = {'backend': 'ps', 'axes.labelsize': 10, 'text.fontsize': 10, 'legend.fontsize': 10, 'xtick.labelsize': 8, 'ytick.labelsize': 8, 'figure.figsize': [3.4, 3.1]} pylab.rcParams.update(params) # Load in the csv files and convert info from strings to floats simulation, _ = csv2dict(file_sim) experiment, _ = csv2dict(file_exp) time_sim = [float(x) for x in simulation['time']] time_exp = [float(x) for x in experiment['Time']] # Trim the simulation data, due to strange anuga paper bug condition_sim = get_max_min_condition_array(run_data['wave_times'][0], run_data['wave_times'][1], time_sim) time_sim = compress(condition_sim, time_sim) condition_exp = get_max_min_condition_array(run_data['wave_times'][0], run_data['wave_times'][1], time_exp) time_exp = compress(condition_exp, time_exp) if is_interactive: ion() for position, i in enumerate(gauge_indexs): gauge_x = run_data['gauge_x'][i] grid_position = (len(gauge_indexs))*100 + 10 + position +1 subplot(grid_position) location_sim = str(gauge_x) + y_location_tag location_exp = str(gauge_x) quantity_sim = [float(x) for x in simulation[location_sim]] quantity_exp = [float(x) for x in experiment[location_exp]] # Trim the simulation data, due to strange anuga paper bug quantity_sim = compress(condition_sim, quantity_sim) quantity_exp = compress(condition_exp, quantity_exp) l_sim, l_exp = plot(time_sim, quantity_sim, time_exp, quantity_exp) setp(l_sim, color='k', linestyle='--') setp(l_exp, color='k') grid(True) # When the shore gauges are used, don't # use the standard y axis scale gauge_elev = run_data['gauge_bed_elevation'][i] x_axis = run_data['wave_times'] if gauge_x < run_data['axis_maximum_x']: y_axis = [run_data['axis'][2],run_data['axis'][3]] setp(gca(), yticks=[-0.04,-0.02, 0.0, 0.02, 0.04]) y_text = -0.035 else: y_axis = [run_data['axis'][2] + gauge_elev, run_data['axis'][3] + gauge_elev] setp(gca(), yticks=[0.02, 0.04, 0.06, 0.08, 0.1]) y_text = 0.025 text(x_axis[0]+5,y_text, str(round(gauge_x,1)) + " m gauge") if position == 0: title(plot_title) if position == 1: ylabel(y_label) #legend((legend_sim, legend_exp),'lower center') if y_axis is not None: axis(ymin=y_axis[0]-0.001, ymax=y_axis[1]-0.001) # the fudge -0.001 is to reduce the ytick's axis(xmin=x_axis[0], xmax=x_axis[1]) # Only do tick marks on the final graph if not position == 2: setp(gca(), xticklabels=[]) # Add axis stuff and legend xlabel(x_label) #ylabel(y_label) # The order defines the label #legend((legend_exp, legend_sim),'upper left') # I couldn't get this working #figlegend((l_sim, l_exp), # (legend_sim, legend_exp), # 'lower center', axespad = -0.05) if is_interactive: # Wait for enter pressed raw_input() if save_as is not None: savefig(save_as) #Need to close this plot close() #------------------------------------------------------------- if __name__ == "__main__": """ Plot the stage graph for the ANUGA validation papar """ from scenarios import scenarios outputdir_tag = "_good_tri_area_0.01_limiterE" outputdir_tag = "_nolmts_wdth_0.1_z_0.012_ys_0.01_mta_0.01_H" outputdir_tag = "_good_lmts_wdth_0.1_z_0.0_ys_0.01_mta_0.01_F" outputdir_tag = "_nolmts_wdth_0.1_z_0.0_ys_0.01_mta_0.01_G" #outputdir_tag = "_nolmts_wdth_0.01_z_0.012_ys_0.01_mta_1e-05_G" #outputdir_tag = "_test_C" #scenarios = scenarios[1:] #scenarios = [scenarios[7]] is_interactive = False #is_interactive = True plot_exp_sim_comparision(scenarios, outputdir_tag, is_interactive=is_interactive, y_location_tag=':0.0')