11# ============================*
2- # ** Copyright UCAR (c) 2020
3- # ** University Corporation for Atmospheric Research (UCAR)
4- # ** National Center for Atmospheric Research (NCAR)
5- # ** Research Applications Lab (RAL)
6- # ** P.O.Box 3000, Boulder, Colorado, 80307-3000, USA
7- # ============================*
8-
9-
10-
2+ # ** Copyright UCAR (c) 2025
3+ # ** University Corporation for Atmospheric Research (UCAR)
4+ # ** National Science Foundation National Center for Atmospheric Research (NSF NCAR)
5+ # ** Research Applications Lab (RAL)
6+ # ** P.O.Box 3000, Boulder, Colorado, 80307-3000, USA
7+ # ============================*
8+
9+
1110"""
1211plot_cross_section
1312"""
2625matplotlib .use ('Agg' )
2726from metplotpy .plots import util
2827
28+
2929def plot_cross_section (config , data_set , args ):
3030 """
3131 Generate the cross-section plot of the field specified in the YAML config file
@@ -57,7 +57,7 @@ def plot_cross_section(config, data_set, args):
5757 # Create the log file using the same name as the plot name with '_log' and in the
5858 # same location.
5959 try :
60- os .makedirs (plot_outdir , exist_ok = True )
60+ os .makedirs (plot_outdir , exist_ok = True )
6161 except FileExistsError :
6262 pass
6363 log_filename = os .path .join (plot_outdir , config ['plot_filename' ] + '_log' + '.txt' )
@@ -69,6 +69,7 @@ def plot_cross_section(config, data_set, args):
6969 fig , ax = plt .subplots (figsize = (plot_width , plot_height ))
7070
7171 field = data_set [config ['field' ]]
72+
7273 itime = config ['index_time_slice' ]
7374
7475 # originally, axis=1 but the order of dimensions was modified
@@ -78,34 +79,47 @@ def plot_cross_section(config, data_set, args):
7879 # originally, the transpose of the field_azi_mean was used, but this is no
7980 # longer necessary. If the transpose is used, the dimensions are incorrect
8081 # and a TypeError will be raised by the contour plot.
82+ if config ['colormap' ] != 'custom' :
83+ colormap = config ['colormap' ]
84+ else :
85+ if config ['colormap_by_rgb' ]:
86+ methodology = "by_rgb"
87+ colormap_triplet_list = config ['colormap_colors_rgb' ]
88+ else :
89+ methodology = "by_hex"
90+ colormap_triplet_list = config ['colormap_colors_hexadecimal' ]
91+
92+ colormap = util .customize_colormap (colormap_triplet_list , methodology )
93+
8194 if config ['filled_contour_on' ]:
82- scalar_contour = ax .contourf (data_set ['range' ],
83- data_set [config ['vertical_coord_name' ]],
84- field_azi_mean ,
85- levels = np .arange (config ['contour_level_start' ],
86- config ['contour_level_end' ],
95+ scalar_contour = ax .contourf (data_set ['range' ], data_set [config ['vertical_coord_name' ]], field_azi_mean ,
96+ levels = np .arange (config ['contour_level_start' ], config ['contour_level_end' ],
97+ config ['contour_level_stepsize' ]), cmap = colormap )
98+ # Add colorbar legend
99+ cbar = plt .colorbar (scalar_contour , label = config ['filled_contour_colorbar_label' ])
100+ # cbar.set_label(config['filled_contour_colorbar_label'])
101+
102+ # scalar_contour = ax.contour(data_set['range'], data_set[config['vertical_coord_name']], field_azi_mean,
103+ # levels=np.arange(config['contour_level_start'], config['contour_level_end'],
104+ # config['contour_level_stepsize']),
105+ # colors=config['contour_line_colors'], linewidths=(config['line_width'],),
106+ # linestyles=[config['contour_line_style']])
107+
108+
109+
110+ if config ['line_contour_on' ]:
111+ scalar_contour = ax .contour (data_set ['range' ], data_set [config ['vertical_coord_name' ]], field_azi_mean ,
112+ levels = np .arange (config ['contour_level_start' ], config ['contour_level_end' ],
87113 config ['contour_level_stepsize' ]),
88- cmap = config ['colormap' ]
89- )
90-
91- scalar_contour = ax .contour (data_set ['range' ],
92- data_set [config ['vertical_coord_name' ]],
93- field_azi_mean ,
94- levels = np .arange (config ['contour_level_start' ],
95- config ['contour_level_end' ],
96- config ['contour_level_stepsize' ]),
97- colors = config ['contour_line_colors' ],
98- linewidths = (config ['line_width' ])
99- )
114+ colors = config ['contour_line_colors' ], linewidths = (config ['line_width' ],),
115+ linestyles = [config ['contour_line_style' ]])
100116
101117 plt .title (config ['plot_title' ])
102118 ax .clabel (scalar_contour , colors = config ['contour_label_color' ], fmt = config ['contour_label_fmt' ])
103119 ax .set_xlabel (config ['x_label' ])
104120 ax .set_ylabel (config ['y_label' ])
105121 ax .set_xticks (np .arange (config ['x_tick_start' ], config ['x_tick_end' ]))
106- ax .set_yticks (np .arange (config ['y_tick_start' ],
107- config ['y_tick_end' ],
108- config ['y_tick_stepsize' ]))
122+ ax .set_yticks (np .arange (config ['y_tick_start' ], config ['y_tick_end' ], config ['y_tick_stepsize' ]))
109123 ax .set_yscale (config ['y_scale' ])
110124 ax .set_ylim (config ['y_lim_start' ], config ['y_lim_end' ])
111125
@@ -117,32 +131,21 @@ def plot_cross_section(config, data_set, args):
117131
118132
119133if __name__ == '__main__' :
120- parser = argparse .ArgumentParser (
121- description = 'Plot Tropical Cyclone Cross Section' )
122-
123- parser .add_argument (
124- '--datadir' , type = str , dest = 'datadir' ,
125- required = True )
126- parser .add_argument (
127- '--plotdir' , type = str , dest = 'plotdir' ,
128- required = True )
129- parser .add_argument (
130- '--filename' , type = str , dest = 'filename' ,
131- required = True )
132- parser .add_argument ('--config' , type = str ,
133- required = True ,
134- help = 'configuration file' )
135- parser .add_argument ('--loglevel' , type = str ,
136- required = False )
134+ parser = argparse .ArgumentParser (description = 'Plot Tropical Cyclone Cross Section' )
135+
136+ parser .add_argument ('--datadir' , type = str , dest = 'datadir' , required = True )
137+ parser .add_argument ('--plotdir' , type = str , dest = 'plotdir' , required = True )
138+ parser .add_argument ('--filename' , type = str , dest = 'filename' , required = True )
139+ parser .add_argument ('--config' , type = str , required = True , help = 'configuration file' )
140+ parser .add_argument ('--loglevel' , type = str , required = False )
137141
138142 input_args = parser .parse_args ()
139143
140144 """
141145 Read YAML configuration file
142146 """
143147 try :
144- plotting_config = yaml .load (
145- open (input_args .config ), Loader = yaml .FullLoader )
148+ plotting_config = yaml .load (open (input_args .config ), Loader = yaml .FullLoader )
146149 except yaml .YAMLError as exc :
147150 sys .exit (1 )
148151
@@ -154,4 +157,3 @@ def plot_cross_section(config, data_set, args):
154157 except (ValueError , FileNotFoundError , PermissionError ):
155158 sys .exit (1 )
156159 plot_cross_section (plotting_config , input_data , input_args )
157-
0 commit comments