[5399] | 1 | |
---|
[5410] | 2 | """ |
---|
| 3 | Plot up files from the Hinwood project. |
---|
| 4 | """ |
---|
| 5 | from os import sep |
---|
| 6 | import project |
---|
[5426] | 7 | from time import localtime, strftime |
---|
[5410] | 8 | |
---|
| 9 | def plot_compare_csv(location_sim, file_sim, location_exp, file_exp, |
---|
| 10 | y_label, |
---|
[5399] | 11 | save_as=None, |
---|
[5410] | 12 | plot_title="", |
---|
[5399] | 13 | x_label='Time (s)', |
---|
| 14 | legend_sim='ANUGA simulation', |
---|
| 15 | legend_exp='Measured flume result', |
---|
[5459] | 16 | is_interactive=False, |
---|
[5664] | 17 | x_axis=None, |
---|
| 18 | y_axis=None): |
---|
[5399] | 19 | """ |
---|
| 20 | """ |
---|
| 21 | from pylab import ion, plot, xlabel, ylabel, close, legend, \ |
---|
[5494] | 22 | savefig, title, axis, setp |
---|
[5399] | 23 | from anuga.shallow_water.data_manager import csv2dict |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | # Load in the csv files and convert info from strings to floats |
---|
| 28 | simulation, _ = csv2dict(file_sim) |
---|
| 29 | experiment, _ = csv2dict(file_exp) |
---|
[5410] | 30 | time_sim = [float(x) for x in simulation['time']] |
---|
[5399] | 31 | quantity_sim = [float(x) for x in simulation[location_sim]] |
---|
| 32 | #print "quantity_sim", quantity_sim |
---|
[5410] | 33 | time_exp = [float(x) for x in experiment['Time']] |
---|
[5399] | 34 | quantity_exp = [float(x) for x in experiment[location_exp]] |
---|
| 35 | |
---|
| 36 | if is_interactive: |
---|
| 37 | ion() |
---|
| 38 | |
---|
[5494] | 39 | l_sim, l_exp = plot(time_sim, quantity_sim, time_exp, quantity_exp) |
---|
| 40 | setp(l_sim, color='r') |
---|
| 41 | setp(l_exp, color='b') |
---|
[5399] | 42 | |
---|
| 43 | # Add axis stuff and legend |
---|
| 44 | xlabel(x_label) |
---|
| 45 | ylabel(y_label) |
---|
[5410] | 46 | # The order defines the label |
---|
| 47 | #legend((legend_exp, legend_sim),'upper left') |
---|
| 48 | legend((legend_sim, legend_exp),'upper left') |
---|
| 49 | title(plot_title) |
---|
[5664] | 50 | if x_axis is not None: |
---|
| 51 | axis(xmin=x_axis[0], xmax=x_axis[1]) |
---|
[5399] | 52 | |
---|
[5664] | 53 | if y_axis is not None: |
---|
| 54 | axis(ymin=y_axis[0], ymax=y_axis[1]) |
---|
| 55 | |
---|
[5399] | 56 | if is_interactive: |
---|
| 57 | # Wait for enter pressed |
---|
| 58 | raw_input() |
---|
| 59 | |
---|
| 60 | if save_as is not None: |
---|
| 61 | savefig(save_as) |
---|
| 62 | |
---|
| 63 | #Need to close this plot |
---|
| 64 | close() |
---|
| 65 | |
---|
[5455] | 66 | def Hinwood_files_locations(run_data, outputdir_tag, plot_type, |
---|
[5616] | 67 | quantity = "depth", |
---|
| 68 | y_location_tag=':0.0'): |
---|
[5455] | 69 | """ |
---|
| 70 | run_data is a dictionary of data describing a Hinwood experiment |
---|
| 71 | outputdir_tag a string at the end of an output dir; '_good_tri_area_0.01_A' |
---|
| 72 | plot_type the file extension of the plot, eg '.pdf' |
---|
| 73 | """ |
---|
[5410] | 74 | id = run_data['scenario_id'] |
---|
| 75 | outputdir_name = id + outputdir_tag |
---|
| 76 | pro_instance = project.Project(['data','flumes','Hinwood_2008'], |
---|
| 77 | outputdir_name=outputdir_name) |
---|
| 78 | |
---|
[5494] | 79 | |
---|
[5455] | 80 | file_sim = pro_instance.outputdir + quantity + "_" + id + ".csv" |
---|
[5410] | 81 | #print "file_exp",file_exp |
---|
[5455] | 82 | file_exp = pro_instance.raw_data_dir + sep + id + 'pressfilt_exp_' \ |
---|
| 83 | + quantity + '.csv' |
---|
[5410] | 84 | #print "file_sim", file_sim |
---|
| 85 | location_sims = [] |
---|
| 86 | location_exps = [] |
---|
| 87 | save_as_list = [] |
---|
| 88 | for gauge_x in run_data['gauge_x']: |
---|
| 89 | gauge_x = str(gauge_x) |
---|
[5616] | 90 | location_sims.append(gauge_x + y_location_tag) |
---|
[5410] | 91 | location_exps.append(gauge_x) |
---|
| 92 | save_as_list.append(pro_instance.plots_dir + sep + \ |
---|
[5455] | 93 | outputdir_name + "_" + quantity + "_" + \ |
---|
| 94 | gauge_x + plot_type) |
---|
[5410] | 95 | return file_exp, file_sim, location_sims, location_exps, outputdir_name, \ |
---|
| 96 | save_as_list |
---|
[5616] | 97 | |
---|
[5670] | 98 | def plot_exp_sim_comparision(scenarios, outputdir_tag, |
---|
| 99 | quantity = "stage",is_interactive=False, |
---|
[5616] | 100 | y_location_tag=':0.0'): |
---|
[5449] | 101 | plot_type = ".pdf" |
---|
[5670] | 102 | plot_files = {} # Index scenario (T1R3) value, list of files |
---|
[5449] | 103 | for run_data in scenarios: |
---|
| 104 | |
---|
[5455] | 105 | temp = Hinwood_files_locations(run_data, outputdir_tag, |
---|
[5616] | 106 | plot_type, quantity, |
---|
| 107 | y_location_tag=y_location_tag) |
---|
[5455] | 108 | |
---|
[5449] | 109 | file_exp, file_sim, location_sims, location_exps, outputdir_name, \ |
---|
| 110 | save_as_list = temp |
---|
[5670] | 111 | plot_files[run_data['scenario_id']] = save_as_list |
---|
[5616] | 112 | print "file_exp",file_exp |
---|
[5449] | 113 | print "run_data['scenario_id']", run_data['scenario_id'] |
---|
| 114 | #location_sims = [location_sims[0]] |
---|
| 115 | #location_exps = [location_exps[0]] |
---|
| 116 | #save_as_list = [save_as_list[0]] |
---|
[5664] | 117 | for loc_sim, loc_exp, save_as, gauge, gauge_elev in \ |
---|
| 118 | map(None, location_sims, |
---|
| 119 | location_exps, |
---|
| 120 | save_as_list, |
---|
| 121 | run_data['gauge_x'], |
---|
| 122 | run_data['gauge_bed_elevation']): |
---|
[5449] | 123 | time_date = strftime('plot date: %d/%m/%Y Time: %H:%M:%S', |
---|
| 124 | localtime()) |
---|
| 125 | plot_title = "Scenario: " + outputdir_name + "\n" + \ |
---|
| 126 | "X Gauge (m):" + str(gauge) + " " + time_date |
---|
[5664] | 127 | |
---|
| 128 | # When the shore gauges out of the are used, don't |
---|
| 129 | # use the standard y axis scale |
---|
| 130 | x_axis = run_data['wave_times'] |
---|
[5459] | 131 | if gauge < run_data['axis_maximum_x']: |
---|
[5664] | 132 | y_axis = [run_data['axis'][2],run_data['axis'][3]] |
---|
| 133 | |
---|
[5459] | 134 | else: |
---|
[5664] | 135 | y_axis = [run_data['axis'][2] + gauge_elev, |
---|
| 136 | run_data['axis'][3] + gauge_elev] |
---|
[5449] | 137 | print "Doing ", plot_title |
---|
| 138 | plot_compare_csv(location_sim=loc_sim, |
---|
| 139 | file_sim=file_sim, |
---|
| 140 | location_exp=loc_exp, |
---|
| 141 | file_exp=file_exp, |
---|
| 142 | plot_title=plot_title, |
---|
[5455] | 143 | y_label='Water '+ quantity +' (m)', |
---|
[5494] | 144 | is_interactive=is_interactive, |
---|
[5459] | 145 | save_as=save_as, |
---|
[5664] | 146 | x_axis=x_axis, |
---|
| 147 | y_axis=y_axis) |
---|
[5494] | 148 | if is_interactive is True: |
---|
| 149 | break |
---|
[5670] | 150 | return plot_files |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | def auto_plot_compare_csv_subfigures(scenarios, outputdir_tag, |
---|
| 154 | to_publish_indexes, |
---|
| 155 | quantity = "stage",is_interactive=False, |
---|
| 156 | y_location_tag=':0.0', |
---|
| 157 | add_run_info=False): |
---|
| 158 | plot_type = ".pdf" |
---|
| 159 | save_as_list = [] |
---|
| 160 | for run_data in scenarios: |
---|
| 161 | |
---|
| 162 | id = run_data['scenario_id'] |
---|
| 163 | if not to_publish_indexes.has_key(id): |
---|
| 164 | continue |
---|
| 165 | |
---|
| 166 | outputdir_name = id + outputdir_tag |
---|
| 167 | pro_instance = project.Project(['data','flumes','Hinwood_2008'], |
---|
| 168 | outputdir_name=outputdir_name) |
---|
[5449] | 169 | |
---|
[5670] | 170 | |
---|
| 171 | file_sim = pro_instance.outputdir + quantity + "_" + id + ".csv" |
---|
| 172 | file_exp = pro_instance.raw_data_dir + sep + id + 'pressfilt_exp_' \ |
---|
| 173 | + quantity + '.csv' |
---|
| 174 | |
---|
| 175 | if add_run_info is True: |
---|
| 176 | plot_title = "Title will not be in final draft \n" + \ |
---|
| 177 | id + outputdir_tag |
---|
| 178 | else: |
---|
| 179 | plot_title = "" |
---|
| 180 | #save_as = pro_instance.plots_dir + sep + \ |
---|
| 181 | # outputdir_name + "_" + quantity + "_" + plot_type |
---|
| 182 | save_as = pro_instance.plots_dir + sep + id + quantity + \ |
---|
| 183 | '_compare' + plot_type |
---|
| 184 | save_as_list.append(save_as) |
---|
| 185 | |
---|
| 186 | |
---|
| 187 | plot_compare_csv_subfigures(file_sim, |
---|
| 188 | file_exp, |
---|
| 189 | to_publish_indexes[id], |
---|
| 190 | run_data, |
---|
| 191 | plot_title=plot_title, |
---|
| 192 | save_as=save_as, |
---|
| 193 | y_label='Water '+ quantity +' (m)', |
---|
| 194 | is_interactive=is_interactive, |
---|
| 195 | y_location_tag=y_location_tag) |
---|
| 196 | return save_as_list |
---|
| 197 | |
---|
| 198 | def plot_compare_csv_subfigures(file_sim, |
---|
| 199 | file_exp, |
---|
| 200 | gauge_indexs, |
---|
| 201 | run_data, |
---|
| 202 | save_as=None, |
---|
| 203 | plot_title="", |
---|
| 204 | x_label='Time (s)', |
---|
| 205 | y_label=None, |
---|
| 206 | legend_sim='ANUGA simulation', |
---|
| 207 | legend_exp='Measured flume result', |
---|
| 208 | is_interactive=False, |
---|
| 209 | x_axis=None, |
---|
| 210 | y_axis=None, |
---|
| 211 | y_location_tag=':0.0'): |
---|
| 212 | """ |
---|
| 213 | """ |
---|
| 214 | from pylab import ion, plot, xlabel, ylabel, close, legend, \ |
---|
| 215 | savefig, title, axis, setp, subplot, grid, figlegend, gca, \ |
---|
| 216 | text |
---|
| 217 | |
---|
| 218 | from anuga.shallow_water.data_manager import csv2dict |
---|
| 219 | |
---|
| 220 | print "file_sim", file_sim |
---|
| 221 | # Load in the csv files and convert info from strings to floats |
---|
| 222 | simulation, _ = csv2dict(file_sim) |
---|
| 223 | experiment, _ = csv2dict(file_exp) |
---|
| 224 | time_sim = [float(x) for x in simulation['time']] |
---|
| 225 | time_exp = [float(x) for x in experiment['Time']] |
---|
| 226 | |
---|
| 227 | |
---|
| 228 | if is_interactive: |
---|
| 229 | ion() |
---|
| 230 | |
---|
| 231 | for position, i in enumerate(gauge_indexs): |
---|
| 232 | gauge_x = run_data['gauge_x'][i] |
---|
| 233 | grid_position = (len(gauge_indexs)+1)*100 + 10 + position +1 |
---|
| 234 | subplot(grid_position) |
---|
| 235 | location_sim = str(gauge_x) + y_location_tag |
---|
| 236 | location_exp = str(gauge_x) |
---|
| 237 | |
---|
| 238 | quantity_sim = [float(x) for x in simulation[location_sim]] |
---|
| 239 | quantity_exp = [float(x) for x in experiment[location_exp]] |
---|
| 240 | |
---|
| 241 | |
---|
| 242 | l_sim, l_exp = plot(time_sim, quantity_sim, time_exp, quantity_exp) |
---|
| 243 | setp(l_sim, color='r', linestyle='--') |
---|
| 244 | setp(l_exp, color='b') |
---|
| 245 | grid(True) |
---|
| 246 | |
---|
| 247 | # When the shore gauges out of the are used, don't |
---|
| 248 | # use the standard y axis scale |
---|
| 249 | gauge_elev = run_data['gauge_bed_elevation'][i] |
---|
| 250 | x_axis = run_data['wave_times'] |
---|
| 251 | if gauge_x < run_data['axis_maximum_x']: |
---|
| 252 | y_axis = [run_data['axis'][2],run_data['axis'][3]] |
---|
| 253 | setp(gca(), yticks=[-0.04,-0.02, 0.0, 0.02, 0.04]) |
---|
| 254 | y_text = -0.035 |
---|
| 255 | else: |
---|
| 256 | y_axis = [run_data['axis'][2] + gauge_elev, |
---|
| 257 | run_data['axis'][3] + gauge_elev] |
---|
| 258 | setp(gca(), yticks=[0.02, 0.04, 0.06, 0.08, 0.1]) |
---|
| 259 | y_text = 0.025 |
---|
| 260 | text(x_axis[0]+5,y_text, str(round(gauge_x,1)) + " m gauge") |
---|
| 261 | if position == 0: |
---|
| 262 | title(plot_title) |
---|
| 263 | if position == 1: |
---|
| 264 | ylabel(y_label) |
---|
| 265 | #legend((legend_sim, legend_exp),'lower center') |
---|
| 266 | if y_axis is not None: |
---|
| 267 | axis(ymin=y_axis[0]-0.001, ymax=y_axis[1]-0.001) |
---|
| 268 | # the fudge -0.001 is to reduce the ytick's |
---|
| 269 | |
---|
| 270 | # need to drop the axis name being printed |
---|
| 271 | if x_axis is not None: |
---|
| 272 | axis(xmin=x_axis[0], xmax=x_axis[1]) |
---|
| 273 | |
---|
| 274 | # Only do tick marks on the final graph |
---|
| 275 | if not position == 2: |
---|
| 276 | setp(gca(), xticklabels=[]) |
---|
| 277 | |
---|
| 278 | # Add axis stuff and legend |
---|
| 279 | xlabel(x_label) |
---|
| 280 | |
---|
| 281 | #ylabel(y_label) |
---|
| 282 | # The order defines the label |
---|
| 283 | #legend((legend_exp, legend_sim),'upper left') |
---|
| 284 | figlegend((l_sim, l_exp), |
---|
| 285 | (legend_sim, legend_exp), |
---|
| 286 | 'lower center') |
---|
| 287 | |
---|
| 288 | if is_interactive: |
---|
| 289 | # Wait for enter pressed |
---|
| 290 | raw_input() |
---|
| 291 | |
---|
| 292 | if save_as is not None: |
---|
| 293 | savefig(save_as) |
---|
| 294 | |
---|
| 295 | #Need to close this plot |
---|
| 296 | close() |
---|
| 297 | |
---|
[5399] | 298 | #------------------------------------------------------------- |
---|
| 299 | if __name__ == "__main__": |
---|
| 300 | """ Plot the stage graph for the ANUGA validation papar |
---|
| 301 | """ |
---|
[5410] | 302 | from scenarios import scenarios |
---|
[5577] | 303 | outputdir_tag = "_good_tri_area_0.01_limiterE" |
---|
[5616] | 304 | outputdir_tag = "_nolmts_wdth_0.1_z_0.012_ys_0.01_mta_0.01_H" |
---|
[5659] | 305 | outputdir_tag = "_good_lmts_wdth_0.1_z_0.0_ys_0.01_mta_0.01_F" |
---|
| 306 | outputdir_tag = "_nolmts_wdth_0.1_z_0.0_ys_0.01_mta_0.01_G" |
---|
[5595] | 307 | #outputdir_tag = "_nolmts_wdth_0.01_z_0.012_ys_0.01_mta_1e-05_G" |
---|
[5494] | 308 | #outputdir_tag = "_test_C" |
---|
[5459] | 309 | #scenarios = scenarios[1:] |
---|
[5664] | 310 | #scenarios = [scenarios[7]] |
---|
[5494] | 311 | is_interactive = False |
---|
| 312 | #is_interactive = True |
---|
[5670] | 313 | plot_exp_sim_comparision(scenarios, outputdir_tag, |
---|
| 314 | is_interactive=is_interactive, |
---|
| 315 | y_location_tag=':0.0') |
---|
[5399] | 316 | |
---|