source: anuga_work/development/Hinwood_2008/plot.py @ 5670

Last change on this file since 5670 was 5670, checked in by duncan, 15 years ago

Current Hinwood - working on fig's for paper

File size: 11.8 KB
Line 
1
2"""
3Plot up files from the Hinwood project.
4"""
5from os import sep
6import project
7from time import localtime, strftime
8
9def plot_compare_csv(location_sim, file_sim, location_exp, file_exp,
10                     y_label,
11                     save_as=None,
12                     plot_title="",
13                     x_label='Time (s)',
14                     legend_sim='ANUGA simulation',
15                     legend_exp='Measured flume result',
16                     is_interactive=False,
17                     x_axis=None,
18                     y_axis=None):
19    """
20    """
21    from pylab import ion, plot, xlabel, ylabel, close, legend, \
22         savefig, title, axis, setp
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)
30    time_sim = [float(x) for x in simulation['time']]
31    quantity_sim = [float(x) for x in simulation[location_sim]]
32    #print "quantity_sim", quantity_sim
33    time_exp = [float(x) for x in experiment['Time']]
34    quantity_exp = [float(x) for x in experiment[location_exp]]
35
36    if is_interactive:
37        ion()
38   
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')
42
43    # Add axis stuff and legend
44    xlabel(x_label)
45    ylabel(y_label)
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)
50    if x_axis is not None:
51        axis(xmin=x_axis[0], xmax=x_axis[1])
52   
53    if y_axis is not None:
54        axis(ymin=y_axis[0], ymax=y_axis[1])
55       
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   
66def Hinwood_files_locations(run_data, outputdir_tag, plot_type,
67                            quantity = "depth",
68                            y_location_tag=':0.0'):
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    """
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   
79   
80    file_sim = pro_instance.outputdir + quantity + "_" + id + ".csv"
81    #print "file_exp",file_exp
82    file_exp = pro_instance.raw_data_dir + sep + id + 'pressfilt_exp_' \
83               + quantity + '.csv'
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)
90        location_sims.append(gauge_x + y_location_tag)
91        location_exps.append(gauge_x)
92        save_as_list.append(pro_instance.plots_dir + sep + \
93                            outputdir_name + "_" + quantity + "_" + \
94                            gauge_x + plot_type)
95    return file_exp, file_sim, location_sims, location_exps, outputdir_name, \
96           save_as_list
97
98def plot_exp_sim_comparision(scenarios, outputdir_tag,
99                             quantity = "stage",is_interactive=False,
100         y_location_tag=':0.0'):
101    plot_type = ".pdf"
102    plot_files = {} # Index scenario (T1R3) value, list of files
103    for run_data in scenarios:
104       
105        temp = Hinwood_files_locations(run_data, outputdir_tag,
106                                       plot_type, quantity,
107                                       y_location_tag=y_location_tag)
108                                   
109        file_exp, file_sim, location_sims, location_exps, outputdir_name, \
110                  save_as_list = temp
111        plot_files[run_data['scenario_id']] = save_as_list
112        print "file_exp",file_exp
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]]
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']):
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
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']
131            if gauge < run_data['axis_maximum_x']:
132                y_axis = [run_data['axis'][2],run_data['axis'][3]]
133       
134            else:
135                y_axis = [run_data['axis'][2] + gauge_elev,
136                          run_data['axis'][3] + gauge_elev]
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,
143                             y_label='Water '+ quantity +' (m)',
144                             is_interactive=is_interactive,
145                             save_as=save_as,
146                             x_axis=x_axis,
147                             y_axis=y_axis)
148            if is_interactive is True:
149                break
150    return plot_files
151
152
153def 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)
169   
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
198def 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   
298#-------------------------------------------------------------
299if __name__ == "__main__":
300    """ Plot the stage graph for the ANUGA validation papar
301    """
302    from scenarios import scenarios
303    outputdir_tag = "_good_tri_area_0.01_limiterE"
304    outputdir_tag = "_nolmts_wdth_0.1_z_0.012_ys_0.01_mta_0.01_H"
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"
307    #outputdir_tag = "_nolmts_wdth_0.01_z_0.012_ys_0.01_mta_1e-05_G"
308    #outputdir_tag = "_test_C"
309    #scenarios = scenarios[1:]
310    #scenarios = [scenarios[7]]
311    is_interactive = False
312    #is_interactive = True
313    plot_exp_sim_comparision(scenarios, outputdir_tag,
314                             is_interactive=is_interactive,
315                             y_location_tag=':0.0')
316
Note: See TracBrowser for help on using the repository browser.