1 | """ |
---|
2 | This script plots the mslp from a single wrfout file. It can be run from the |
---|
3 | command line as a standalone script or imported into another script and |
---|
4 | executed from there. |
---|
5 | """ |
---|
6 | |
---|
7 | import sys |
---|
8 | import os |
---|
9 | #import gc |
---|
10 | import pywrf.viz.utils as vu |
---|
11 | import pywrf.wrf.utils as wu |
---|
12 | |
---|
13 | def generate_frames(input_file_name, output_dir=None, time_window=None): |
---|
14 | """ |
---|
15 | Generate the the mslp frames from a single wrfout file and save them in |
---|
16 | the chosen directory. When the script is called as a standalone program |
---|
17 | this function is called under the hood. |
---|
18 | NB if no output directory is supplied the default will be the directory |
---|
19 | from which the script is invoked. |
---|
20 | |
---|
21 | Usage |
---|
22 | >>> import plot_mslp |
---|
23 | >>> plot_mslp(input_file_name, output_dir, time_window) |
---|
24 | """ |
---|
25 | # if the output directory is not specified or invalid |
---|
26 | if output_dir is None \ |
---|
27 | or not os.path.isdir(output_dir): |
---|
28 | output_dir = os.getcwd() |
---|
29 | |
---|
30 | log_file = os.path.join(output_dir, 'plot_mslp.log') |
---|
31 | if os.path.isfile(log_file): |
---|
32 | os.remove(log_file) |
---|
33 | vu.write_to_log_file(log_file, 'Started execution of plot_mslp.py') |
---|
34 | |
---|
35 | # let's check for a local plot_wrfout_config or use default |
---|
36 | if os.path.isfile(os.path.join(os.getcwd(),'plot_wrfout_config.py')): |
---|
37 | sys.path.insert(0, os.getcwd()) |
---|
38 | vu.write_to_log_file(log_file, |
---|
39 | 'Using plot_wrfout_config from present working directory') |
---|
40 | import plot_wrfout_config as pwc |
---|
41 | else: |
---|
42 | vu.write_to_log_file(log_file, |
---|
43 | 'Using default plot_wrfout_config') |
---|
44 | import plot_wrfout_config as pwc |
---|
45 | |
---|
46 | # We are assuming the standard wrfout names are used! |
---|
47 | # lets first work out the domain flag so that we can then use it to select |
---|
48 | # the right set of countours from the dictionary contained in |
---|
49 | # plot_wrfout_config.py |
---|
50 | # we index from the end to cope with absolute paths in the input_file_name |
---|
51 | domain = input_file_name[-23:-20] |
---|
52 | # we will assume the normal wrfout file name structure without a file |
---|
53 | # extension. Furthemore, we assume that the file is a netcdf format. The |
---|
54 | # above is reasonable but potentially fragile. |
---|
55 | input_file, vars_dict = vu.peek(input_file_name + '.nc', |
---|
56 | return_pointers=True, show_vars=False) |
---|
57 | times = wu.time_string_to_datetime(vars_dict['Times'].get_value()) |
---|
58 | # calculate the indeces for the minimum and maximum times to be plotted |
---|
59 | if time_window is not None: |
---|
60 | start_time = time_window[0] |
---|
61 | end_time = time_window[1] |
---|
62 | time_min_idx = 0 |
---|
63 | time_max_idx = len(times) |
---|
64 | for time_idx in range(len(times)): |
---|
65 | if times[time_min_idx] < start_time: |
---|
66 | time_min_idx = time_idx |
---|
67 | for time_idx in range(len(times) - 1, -1, -1): |
---|
68 | if times[time_idx] >= end_time: |
---|
69 | time_max_idx = time_idx |
---|
70 | else: |
---|
71 | time_min_idx = 0 |
---|
72 | time_max_idx = len(times) |
---|
73 | # assuming the grid does not move then we are legitimated to take |
---|
74 | # the values of latitude and longitude at the first time step |
---|
75 | lon = vars_dict['XLONG'].get_value()[0] |
---|
76 | lat = vars_dict['XLAT'].get_value()[0] |
---|
77 | |
---|
78 | for time_idx in range(time_min_idx, time_max_idx): |
---|
79 | vu.write_to_log_file(log_file, |
---|
80 | '\tprocessing time ' + times[time_idx].ctime()) |
---|
81 | time_string = vu.set_time_string('manual', times[time_idx].timetuple()) |
---|
82 | title_string = vu.set_title_string('pressure', 'hPa', |
---|
83 | time_string, 'Sea-level') |
---|
84 | mslp = wu.calculate_mslp_wrapper(vars_dict, time_idx) |
---|
85 | wind_vector = None |
---|
86 | if pwc.plot_wind_vectors: |
---|
87 | zonal_wind = vars_dict['U10'].get_value()[time_idx].copy() |
---|
88 | meridional_wind = vars_dict['V10'].get_value()[time_idx].copy() |
---|
89 | wind_vector = (zonal_wind, meridional_wind) |
---|
90 | output_file_name = vu.generate_output_file_name(output_dir, 'mslp_', |
---|
91 | times[time_idx].timetuple()) |
---|
92 | vu.plot_slab(lon, lat, mslp, |
---|
93 | cntr_lvl=pwc.mslp_cntr_lvl[domain], |
---|
94 | file_name=output_file_name, |
---|
95 | colorbar=pwc.plot_colorbar, |
---|
96 | contour_labels=pwc.plot_contour_labels, |
---|
97 | meridians_delta=pwc.meridians_delta[domain], |
---|
98 | parallels_delta=pwc.parallels_delta[domain], |
---|
99 | quiv_skip=pwc.quiv_skip[domain], |
---|
100 | frame_width=pwc.frame_width[domain], |
---|
101 | wind_vector=wind_vector, |
---|
102 | monochrome=pwc.monochrome, |
---|
103 | quiverkey_length=pwc.quiverkey_length[domain], |
---|
104 | title_string=title_string |
---|
105 | ) |
---|
106 | #del mslp |
---|
107 | |
---|
108 | input_file.close() |
---|
109 | #del lon, lat, input_file, vars_dict |
---|
110 | #gc.collect() |
---|
111 | |
---|
112 | vu.write_to_log_file(log_file, 'Completed execution of plot_mslp.py') |
---|
113 | |
---|
114 | if __name__ == '__main__': |
---|
115 | input_file, output_dir, time_window = \ |
---|
116 | vu.process_command_line_arguments_enhanced(sys.argv) |
---|
117 | generate_frames(input_file, output_dir, time_window) |
---|