def _plot_saliency_for_2d3d_radar(
        list_of_input_matrices, list_of_saliency_matrices,
        training_option_dict, saliency_colour_map_object,
        max_colour_value_by_example, output_dir_name, storm_ids=None,
        storm_times_unix_sec=None):
    """Plots saliency for 2-D azimuthal-shear and 3-D reflectivity fields.

    E = number of examples (storm objects)

    If `storm_ids is None` and `storm_times_unix_sec is None`, will assume that
    the input matrices contain probability-matched means.

    :param list_of_input_matrices: See doc for
        `saliency_maps.read_standard_file`.
    :param list_of_saliency_matrices: Same.
    :param training_option_dict: Dictionary returned by
        `cnn.read_model_metadata`.
    :param saliency_colour_map_object: See documentation at top of file.
    :param max_colour_value_by_example: length-E numpy array with max value in
        colour scheme for each example.  Minimum value for [i]th example will be
        -1 * max_colour_value_by_example[i], since the colour scheme is
        zero-centered and divergent.
    :param output_dir_name: Name of output directory (figures will be saved
        here).
    :param storm_ids: length-E list of storm IDs (strings).
    :param storm_times_unix_sec: length-E numpy array of storm times.
    """

    pmm_flag = storm_ids is None and storm_times_unix_sec is None

    reflectivity_matrix_dbz = list_of_input_matrices[0]
    reflectivity_saliency_matrix = list_of_saliency_matrices[0]
    az_shear_matrix_s01 = list_of_input_matrices[1]
    az_shear_saliency_matrix = list_of_saliency_matrices[1]

    num_examples = reflectivity_matrix_dbz.shape[0]
    num_reflectivity_heights = len(
        training_option_dict[trainval_io.RADAR_HEIGHTS_KEY]
    )
    num_panel_rows_for_reflectivity = int(numpy.floor(
        numpy.sqrt(num_reflectivity_heights)
    ))

    az_shear_field_names = training_option_dict[trainval_io.RADAR_FIELDS_KEY]
    num_az_shear_fields = len(az_shear_field_names)
    plot_colour_bar_flags = numpy.full(num_az_shear_fields, False, dtype=bool)

    for i in range(num_examples):
        _, these_axes_objects = radar_plotting.plot_3d_grid_without_coords(
            field_matrix=numpy.flip(reflectivity_matrix_dbz[i, ..., 0], axis=0),
            field_name=radar_utils.REFL_NAME,
            grid_point_heights_metres=training_option_dict[
                trainval_io.RADAR_HEIGHTS_KEY],
            ground_relative=True,
            num_panel_rows=num_panel_rows_for_reflectivity,
            font_size=FONT_SIZE_SANS_COLOUR_BARS)

        saliency_plotting.plot_many_2d_grids_with_pm_signs(
            saliency_matrix_3d=numpy.flip(
                reflectivity_saliency_matrix[i, ..., 0], axis=0),
            axes_objects_2d_list=these_axes_objects,
            colour_map_object=saliency_colour_map_object,
            max_absolute_colour_value=max_colour_value_by_example[i])

        this_colour_map_object, this_colour_norm_object = (
            radar_plotting.get_default_colour_scheme(radar_utils.REFL_NAME)
        )

        plotting_utils.add_colour_bar(
            axes_object_or_list=these_axes_objects,
            values_to_colour=reflectivity_matrix_dbz[i, ..., 0],
            colour_map=this_colour_map_object,
            colour_norm_object=this_colour_norm_object,
            orientation='horizontal', extend_min=True, extend_max=True)

        if pmm_flag:
            this_title_string = 'Probability-matched mean'
            this_file_name = '{0:s}/saliency_pmm_reflectivity.jpg'.format(
                output_dir_name)
        else:
            this_storm_time_string = time_conversion.unix_sec_to_string(
                storm_times_unix_sec[i], TIME_FORMAT)

            this_title_string = 'Storm "{0:s}" at {1:s}'.format(
                storm_ids[i], this_storm_time_string)

            this_file_name = (
                '{0:s}/saliency_{1:s}_{2:s}_reflectivity.jpg'
            ).format(
                output_dir_name, storm_ids[i].replace('_', '-'),
                this_storm_time_string
            )

        this_title_string += ' (max absolute saliency = {0:.3f})'.format(
            max_colour_value_by_example[i])
        pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)

        print 'Saving figure to file: "{0:s}"...'.format(this_file_name)
        pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
        pyplot.close()

        _, these_axes_objects = (
            radar_plotting.plot_many_2d_grids_without_coords(
                field_matrix=numpy.flip(az_shear_matrix_s01[i, ...], axis=0),
                field_name_by_panel=az_shear_field_names,
                panel_names=az_shear_field_names, num_panel_rows=1,
                plot_colour_bar_by_panel=plot_colour_bar_flags,
                font_size=FONT_SIZE_SANS_COLOUR_BARS)
        )

        saliency_plotting.plot_many_2d_grids_with_pm_signs(
            saliency_matrix_3d=numpy.flip(
                az_shear_saliency_matrix[i, ...], axis=0),
            axes_objects_2d_list=these_axes_objects,
            colour_map_object=saliency_colour_map_object,
            max_absolute_colour_value=max_colour_value_by_example[i])

        this_colour_map_object, this_colour_norm_object = (
            radar_plotting.get_default_colour_scheme(
                radar_utils.LOW_LEVEL_SHEAR_NAME)
        )

        plotting_utils.add_colour_bar(
            axes_object_or_list=these_axes_objects,
            values_to_colour=az_shear_saliency_matrix[i, ...],
            colour_map=this_colour_map_object,
            colour_norm_object=this_colour_norm_object,
            orientation='horizontal', extend_min=True, extend_max=True)

        pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)
        this_file_name = this_file_name.replace(
            '_reflectivity.jpg', '_azimuthal-shear.jpg')

        print 'Saving figure to file: "{0:s}"...'.format(this_file_name)
        pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
        pyplot.close()
Exemplo n.º 2
0
def _plot_3d_radar(training_option_dict,
                   output_dir_name,
                   pmm_flag,
                   diff_colour_map_object=None,
                   max_colour_percentile_for_diff=None,
                   full_id_strings=None,
                   storm_time_strings=None,
                   novel_radar_matrix=None,
                   novel_radar_matrix_upconv=None,
                   novel_radar_matrix_upconv_svd=None):
    """Plots results of novelty detection for 3-D radar fields.

    E = number of examples (storm objects)
    M = number of rows in spatial grid
    N = number of columns in spatial grid
    H = number of heights in spatial grid
    F = number of fields

    If `novel_radar_matrix` is the only matrix given, this method will plot the
    original (not reconstructed) radar fields.

    If `novel_radar_matrix_upconv` is the only matrix given, will plot
    upconvnet-reconstructed fields.

    If `novel_radar_matrix_upconv_svd` is the only matrix given, will plot
    upconvnet-and-SVD-reconstructed fields.

    If both `novel_radar_matrix_upconv` and `novel_radar_matrix_upconv_svd` are
    given, will plot novelty fields (upconvnet/SVD reconstruction minus
    upconvnet reconstruction).

    :param training_option_dict: See doc for `cnn.read_model_metadata`.
    :param output_dir_name: Name of output directory (figures will be saved
        here).
    :param pmm_flag: Boolean flag.  If True, the input matrices contain
        probability-matched means.
    :param diff_colour_map_object:
        [used only if both `novel_radar_matrix_upconv` and
        `novel_radar_matrix_upconv_svd` are given]

        See documentation at top of file.

    :param max_colour_percentile_for_diff: Same.
    :param full_id_strings: [optional and used only if `pmm_flag = False`]
        length-E list of full storm IDs.
    :param storm_time_strings: [optional and used only if `pmm_flag = False`]
        length-E list of storm times.
    :param novel_radar_matrix: E-by-M-by-N-by-H-by-F numpy array of original
        (not reconstructed) radar fields.
    :param novel_radar_matrix_upconv: E-by-M-by-N-by-H-by-F numpy array of
        upconvnet-reconstructed radar fields.
    :param novel_radar_matrix_upconv_svd: E-by-M-by-N-by-H-by-F numpy array of
        upconvnet-and-SVD-reconstructed radar fields.
    """

    if pmm_flag:
        have_storm_ids = False
    else:
        have_storm_ids = not (full_id_strings is None
                              or storm_time_strings is None)

    plot_difference = False

    if novel_radar_matrix is not None:
        plot_type_abbrev = 'actual'
        plot_type_verbose = 'actual'
        radar_matrix_to_plot = novel_radar_matrix
    else:
        if (novel_radar_matrix_upconv is not None
                and novel_radar_matrix_upconv_svd is not None):

            plot_difference = True
            plot_type_abbrev = 'novelty'
            plot_type_verbose = 'novelty'
            radar_matrix_to_plot = (novel_radar_matrix_upconv -
                                    novel_radar_matrix_upconv_svd)

        else:
            if novel_radar_matrix_upconv is not None:
                plot_type_abbrev = 'upconv'
                plot_type_verbose = 'upconvnet reconstruction'
                radar_matrix_to_plot = novel_radar_matrix_upconv
            else:
                plot_type_abbrev = 'upconv-svd'
                plot_type_verbose = 'upconvnet/SVD reconstruction'
                radar_matrix_to_plot = novel_radar_matrix_upconv_svd

    radar_field_names = training_option_dict[trainval_io.RADAR_FIELDS_KEY]
    radar_heights_m_agl = training_option_dict[trainval_io.RADAR_HEIGHTS_KEY]

    num_storms = novel_radar_matrix.shape[0]
    num_heights = novel_radar_matrix.shape[-2]
    num_panel_rows = int(numpy.floor(numpy.sqrt(num_heights)))

    for i in range(num_storms):
        if pmm_flag:
            this_title_string = 'Probability-matched mean'
            this_base_file_name = 'pmm'
        else:
            if have_storm_ids:
                this_title_string = 'Storm "{0:s}" at {1:s}'.format(
                    full_id_strings[i], storm_time_strings[i])

                this_base_file_name = '{0:s}_{1:s}'.format(
                    full_id_strings[i].replace('_', '-'),
                    storm_time_strings[i])
            else:
                this_title_string = 'Example {0:d}'.format(i + 1)
                this_base_file_name = 'example{0:06d}'.format(i)

        this_title_string += ' ({0:s})'.format(plot_type_verbose)

        for j in range(len(radar_field_names)):
            this_file_name = '{0:s}/{1:s}_{2:s}_{3:s}.jpg'.format(
                output_dir_name, this_base_file_name, plot_type_abbrev,
                radar_field_names[j].replace('_', '-'))

            if plot_difference:
                this_colour_map_object = diff_colour_map_object

                this_max_value = numpy.percentile(
                    numpy.absolute(radar_matrix_to_plot[i, ..., j]),
                    max_colour_percentile_for_diff)

                this_colour_norm_object = matplotlib.colors.Normalize(
                    vmin=-1 * this_max_value, vmax=this_max_value, clip=False)
            else:
                this_colour_map_object, this_colour_norm_object = (
                    radar_plotting.get_default_colour_scheme(
                        radar_field_names[j]))

            _, this_axes_object_matrix = (
                radar_plotting.plot_3d_grid_without_coords(
                    field_matrix=numpy.flip(radar_matrix_to_plot[i, ..., j],
                                            axis=0),
                    field_name=radar_field_names[j],
                    grid_point_heights_metres=radar_heights_m_agl,
                    ground_relative=True,
                    num_panel_rows=num_panel_rows,
                    font_size=FONT_SIZE_SANS_COLOUR_BARS,
                    colour_map_object=this_colour_map_object,
                    colour_norm_object=this_colour_norm_object))

            plotting_utils.plot_colour_bar(
                axes_object_or_matrix=this_axes_object_matrix,
                data_matrix=radar_matrix_to_plot[i, ..., j],
                colour_map_object=this_colour_map_object,
                colour_norm_object=this_colour_norm_object,
                orientation_string='horizontal',
                extend_min=True,
                extend_max=True)

            pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)
            print('Saving figure to: "{0:s}"...'.format(this_file_name))
            pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
            pyplot.close()
Exemplo n.º 3
0
def _plot_storm_outlines_one_time(storm_object_table,
                                  valid_time_unix_sec,
                                  axes_object,
                                  basemap_object,
                                  storm_colour,
                                  storm_opacity,
                                  include_secondary_ids,
                                  output_dir_name,
                                  radar_matrix=None,
                                  radar_field_name=None,
                                  radar_latitudes_deg=None,
                                  radar_longitudes_deg=None):
    """Plots storm outlines (and may underlay radar data) at one time step.

    M = number of rows in radar grid
    N = number of columns in radar grid
    K = number of storm objects

    :param storm_object_table: See doc for `storm_plotting.plot_storm_outlines`.
    :param valid_time_unix_sec: Will plot storm outlines only at this time.
        Will plot tracks up to and including this time.
    :param axes_object: Same.
    :param basemap_object: Same.
    :param storm_colour: Same.
    :param storm_opacity: Same.
    :param include_secondary_ids: Same.
    :param output_dir_name: See documentation at top of file.
    :param radar_matrix: M-by-N numpy array of radar values.  If
        `radar_matrix is None`, radar data will simply not be plotted.
    :param radar_field_name: [used only if `radar_matrix is not None`]
        See documentation at top of file.
    :param radar_latitudes_deg: [used only if `radar_matrix is not None`]
        length-M numpy array of grid-point latitudes (deg N).
    :param radar_longitudes_deg: [used only if `radar_matrix is not None`]
        length-N numpy array of grid-point longitudes (deg E).
    """

    min_plot_latitude_deg = basemap_object.llcrnrlat
    max_plot_latitude_deg = basemap_object.urcrnrlat
    min_plot_longitude_deg = basemap_object.llcrnrlon
    max_plot_longitude_deg = basemap_object.urcrnrlon

    plotting_utils.plot_coastlines(basemap_object=basemap_object,
                                   axes_object=axes_object,
                                   line_colour=BORDER_COLOUR)

    plotting_utils.plot_countries(basemap_object=basemap_object,
                                  axes_object=axes_object,
                                  line_colour=BORDER_COLOUR)

    plotting_utils.plot_states_and_provinces(basemap_object=basemap_object,
                                             axes_object=axes_object,
                                             line_colour=BORDER_COLOUR)

    plotting_utils.plot_parallels(basemap_object=basemap_object,
                                  axes_object=axes_object,
                                  num_parallels=NUM_PARALLELS)

    plotting_utils.plot_meridians(basemap_object=basemap_object,
                                  axes_object=axes_object,
                                  num_meridians=NUM_MERIDIANS)

    if radar_matrix is not None:
        good_indices = numpy.where(
            numpy.logical_and(radar_latitudes_deg >= min_plot_latitude_deg,
                              radar_latitudes_deg <= max_plot_latitude_deg))[0]

        radar_latitudes_deg = radar_latitudes_deg[good_indices]
        radar_matrix = radar_matrix[good_indices, :]

        good_indices = numpy.where(
            numpy.logical_and(
                radar_longitudes_deg >= min_plot_longitude_deg,
                radar_longitudes_deg <= max_plot_longitude_deg))[0]

        radar_longitudes_deg = radar_longitudes_deg[good_indices]
        radar_matrix = radar_matrix[:, good_indices]

        latitude_spacing_deg = radar_latitudes_deg[1] - radar_latitudes_deg[0]
        longitude_spacing_deg = (radar_longitudes_deg[1] -
                                 radar_longitudes_deg[0])

        radar_plotting.plot_latlng_grid(
            field_matrix=radar_matrix,
            field_name=radar_field_name,
            axes_object=axes_object,
            min_grid_point_latitude_deg=numpy.min(radar_latitudes_deg),
            min_grid_point_longitude_deg=numpy.min(radar_longitudes_deg),
            latitude_spacing_deg=latitude_spacing_deg,
            longitude_spacing_deg=longitude_spacing_deg)

        colour_map_object, colour_norm_object = (
            radar_plotting.get_default_colour_scheme(radar_field_name))

        latitude_range_deg = max_plot_latitude_deg - min_plot_latitude_deg
        longitude_range_deg = max_plot_longitude_deg - min_plot_longitude_deg

        if latitude_range_deg > longitude_range_deg:
            orientation_string = 'vertical'
        else:
            orientation_string = 'horizontal'

        colour_bar_object = plotting_utils.plot_colour_bar(
            axes_object_or_matrix=axes_object,
            data_matrix=radar_matrix,
            colour_map_object=colour_map_object,
            colour_norm_object=colour_norm_object,
            orientation_string=orientation_string,
            extend_min=radar_field_name in radar_plotting.SHEAR_VORT_DIV_NAMES,
            extend_max=True,
            fraction_of_axis_length=0.9)

        colour_bar_object.set_label(
            radar_plotting.FIELD_NAME_TO_VERBOSE_DICT[radar_field_name])

    valid_time_rows = numpy.where(storm_object_table[
        tracking_utils.VALID_TIME_COLUMN].values == valid_time_unix_sec)[0]

    line_colour = matplotlib.colors.to_rgba(storm_colour, storm_opacity)

    storm_plotting.plot_storm_outlines(
        storm_object_table=storm_object_table.iloc[valid_time_rows],
        axes_object=axes_object,
        basemap_object=basemap_object,
        line_colour=line_colour)

    storm_plotting.plot_storm_ids(
        storm_object_table=storm_object_table.iloc[valid_time_rows],
        axes_object=axes_object,
        basemap_object=basemap_object,
        plot_near_centroids=False,
        include_secondary_ids=include_secondary_ids,
        font_colour=storm_plotting.DEFAULT_FONT_COLOUR)

    storm_plotting.plot_storm_tracks(storm_object_table=storm_object_table,
                                     axes_object=axes_object,
                                     basemap_object=basemap_object,
                                     colour_map_object=None,
                                     line_colour=TRACK_COLOUR)

    nice_time_string = time_conversion.unix_sec_to_string(
        valid_time_unix_sec, NICE_TIME_FORMAT)

    abbrev_time_string = time_conversion.unix_sec_to_string(
        valid_time_unix_sec, FILE_NAME_TIME_FORMAT)

    pyplot.title('Storm objects at {0:s}'.format(nice_time_string))
    output_file_name = '{0:s}/storm_outlines_{1:s}.jpg'.format(
        output_dir_name, abbrev_time_string)

    print('Saving figure to: "{0:s}"...'.format(output_file_name))
    pyplot.savefig(output_file_name, dpi=FIGURE_RESOLUTION_DPI)
    pyplot.close()

    imagemagick_utils.trim_whitespace(input_file_name=output_file_name,
                                      output_file_name=output_file_name)
def _plot_saliency_for_3d_radar(
        radar_matrix, radar_saliency_matrix, model_metadata_dict,
        saliency_colour_map_object, max_colour_value_by_example,
        output_dir_name, storm_ids=None, storm_times_unix_sec=None):
    """Plots saliency for 3-D radar fields.

    E = number of examples
    M = number of rows in spatial grid
    N = number of columns in spatial grid
    H = number of heights in spatial grid
    F = number of fields

    If `storm_ids is None` and `storm_times_unix_sec is None`, will assume that
    the input matrices contain probability-matched means.

    :param radar_matrix: E-by-M-by-N-by-H-by-F numpy array of radar values
        (predictors).
    :param radar_saliency_matrix: E-by-M-by-N-by-H-by-F numpy array of saliency
        values.
    :param model_metadata_dict: See doc for `cnn.read_model_metadata`.
    :param saliency_colour_map_object: See doc for
        `_plot_saliency_for_2d3d_radar`.
    :param max_colour_value_by_example: Same.
    :param output_dir_name: Same.
    :param storm_ids: Same.
    :param storm_times_unix_sec: Same.
    """

    pmm_flag = storm_ids is None and storm_times_unix_sec is None
    training_option_dict = model_metadata_dict[cnn.TRAINING_OPTION_DICT_KEY]

    num_examples = radar_matrix.shape[0]
    num_fields = radar_matrix.shape[-1]
    num_heights = len(training_option_dict[trainval_io.RADAR_HEIGHTS_KEY])
    num_panel_rows = int(numpy.floor(numpy.sqrt(num_heights)))

    for i in range(num_examples):
        for k in range(num_fields):
            this_field_name = training_option_dict[
                trainval_io.RADAR_FIELDS_KEY][k]

            _, these_axes_objects = (
                radar_plotting.plot_3d_grid_without_coords(
                    field_matrix=numpy.flip(radar_matrix[i, ..., k], axis=0),
                    field_name=this_field_name,
                    grid_point_heights_metres=training_option_dict[
                        trainval_io.RADAR_HEIGHTS_KEY],
                    ground_relative=True, num_panel_rows=num_panel_rows,
                    font_size=FONT_SIZE_SANS_COLOUR_BARS)
            )

            saliency_plotting.plot_many_2d_grids_with_pm_signs(
                saliency_matrix_3d=numpy.flip(
                    radar_saliency_matrix[i, ..., k], axis=0),
                axes_objects_2d_list=these_axes_objects,
                colour_map_object=saliency_colour_map_object,
                max_absolute_colour_value=max_colour_value_by_example[i])

            this_colour_map_object, this_colour_norm_object = (
                radar_plotting.get_default_colour_scheme(this_field_name)
            )

            plotting_utils.add_colour_bar(
                axes_object_or_list=these_axes_objects,
                values_to_colour=radar_matrix[i, ..., k],
                colour_map=this_colour_map_object,
                colour_norm_object=this_colour_norm_object,
                orientation='horizontal', extend_min=True, extend_max=True)

            if pmm_flag:
                this_title_string = 'Probability-matched mean'
                this_file_name = '{0:s}/saliency_pmm_{1:s}.jpg'.format(
                    output_dir_name, this_field_name.replace('_', '-')
                )
            else:
                this_storm_time_string = time_conversion.unix_sec_to_string(
                    storm_times_unix_sec[i], TIME_FORMAT)

                this_title_string = 'Storm "{0:s}" at {1:s}'.format(
                    storm_ids[i], this_storm_time_string)

                this_file_name = '{0:s}/saliency_{1:s}_{2:s}_{3:s}.jpg'.format(
                    output_dir_name, storm_ids[i].replace('_', '-'),
                    this_storm_time_string, this_field_name.replace('_', '-')
                )

            this_title_string += ' (max absolute saliency = {0:.3f})'.format(
                max_colour_value_by_example[i])
            pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)

            print 'Saving figure to file: "{0:s}"...'.format(this_file_name)
            pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
            pyplot.close()
def _plot_3d_radar_cams(
        radar_matrix, model_metadata_dict, cam_colour_map_object,
        max_colour_prctile_for_cam, output_dir_name,
        class_activation_matrix=None, ggradcam_output_matrix=None,
        storm_ids=None, storm_times_unix_sec=None):
    """Plots class-activation maps for 3-D radar data.

    E = number of examples
    M = number of rows in spatial grid
    N = number of columns in spatial grid
    H = number of heights in spatial grid
    F = number of radar fields

    This method will plot either `class_activation_matrix` or
    `ggradcam_output_matrix`, not both.

    If `storm_ids is None` and `storm_times_unix_sec is None`, will assume that
    the input matrices contain probability-matched means.

    :param radar_matrix: E-by-M-by-N-by-H-by-F numpy array of radar values.
    :param model_metadata_dict: Dictionary with CNN metadata (see doc for
        `cnn.read_model_metadata`).
    :param cam_colour_map_object: See documentation at top of file.
    :param max_colour_prctile_for_cam: Same.
    :param output_dir_name: Same.
    :param class_activation_matrix: E-by-M-by-N-by-H numpy array of class
        activations.
    :param ggradcam_output_matrix: E-by-M-by-N-by-H-by-F numpy array of output
        values from guided Grad-CAM.
    :param storm_ids: length-E list of storm IDs (strings).
    :param storm_times_unix_sec: length-E numpy array of storm times.
    """

    pmm_flag = storm_ids is None and storm_times_unix_sec is None

    num_examples = radar_matrix.shape[0]
    num_heights = radar_matrix.shape[-2]
    num_fields = radar_matrix.shape[-1]
    num_panel_rows = int(numpy.floor(numpy.sqrt(num_heights)))

    if class_activation_matrix is None:
        quantity_string = 'max absolute guided Grad-CAM output'
        pathless_file_name_prefix = 'guided-gradcam'
    else:
        quantity_string = 'max class activation'
        pathless_file_name_prefix = 'gradcam'

    training_option_dict = model_metadata_dict[cnn.TRAINING_OPTION_DICT_KEY]

    for i in range(num_examples):
        for k in range(num_fields):
            this_field_name = training_option_dict[
                trainval_io.RADAR_FIELDS_KEY][k]

            _, these_axes_objects = radar_plotting.plot_3d_grid_without_coords(
                field_matrix=numpy.flip(radar_matrix[i, ..., k], axis=0),
                field_name=this_field_name,
                grid_point_heights_metres=training_option_dict[
                    trainval_io.RADAR_HEIGHTS_KEY],
                ground_relative=True, num_panel_rows=num_panel_rows,
                font_size=FONT_SIZE_SANS_COLOUR_BARS)

            if class_activation_matrix is None:
                this_matrix = ggradcam_output_matrix[i, ..., k]

                this_max_contour_level = numpy.percentile(
                    numpy.absolute(this_matrix), max_colour_prctile_for_cam)
                if this_max_contour_level == 0:
                    this_max_contour_level = 10.

                saliency_plotting.plot_many_2d_grids_with_contours(
                    saliency_matrix_3d=numpy.flip(this_matrix, axis=0),
                    axes_objects_2d_list=these_axes_objects,
                    colour_map_object=cam_colour_map_object,
                    max_absolute_contour_level=this_max_contour_level,
                    contour_interval=this_max_contour_level / 10)

            else:
                this_matrix = class_activation_matrix[i, ...]

                this_max_contour_level = numpy.percentile(
                    this_matrix, max_colour_prctile_for_cam)
                if this_max_contour_level == 0:
                    this_max_contour_level = 10.

                cam_plotting.plot_many_2d_grids(
                    class_activation_matrix_3d=numpy.flip(this_matrix, axis=0),
                    axes_objects_2d_list=these_axes_objects,
                    colour_map_object=cam_colour_map_object,
                    max_contour_level=this_max_contour_level,
                    contour_interval=this_max_contour_level / NUM_CONTOURS)

            this_colour_map_object, this_colour_norm_object = (
                radar_plotting.get_default_colour_scheme(this_field_name)
            )

            plotting_utils.add_colour_bar(
                axes_object_or_list=these_axes_objects,
                values_to_colour=radar_matrix[i, ..., k],
                colour_map=this_colour_map_object,
                colour_norm_object=this_colour_norm_object,
                orientation='horizontal', extend_min=True, extend_max=True)

            if pmm_flag:
                this_title_string = 'Probability-matched mean'
                this_figure_file_name = '{0:s}/{1:s}_pmm_{2:s}.jpg'.format(
                    output_dir_name, pathless_file_name_prefix,
                    this_field_name.replace('_', '-')
                )

            else:
                this_storm_time_string = time_conversion.unix_sec_to_string(
                    storm_times_unix_sec[i], TIME_FORMAT)

                this_title_string = 'Storm "{0:s}" at {1:s}'.format(
                    storm_ids[i], this_storm_time_string)

                this_figure_file_name = (
                    '{0:s}/{1:s}_{2:s}_{3:s}_{4:s}.jpg'
                ).format(
                    output_dir_name, pathless_file_name_prefix,
                    storm_ids[i].replace('_', '-'), this_storm_time_string,
                    this_field_name.replace('_', '-')
                )

            this_title_string += ' ({0:s} = {1:.3f})'.format(
                quantity_string, this_max_contour_level)
            pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)

            print 'Saving figure to file: "{0:s}"...'.format(
                this_figure_file_name)
            pyplot.savefig(this_figure_file_name, dpi=FIGURE_RESOLUTION_DPI)
            pyplot.close()
def _plot_storm_outlines_one_time(storm_object_table,
                                  valid_time_unix_sec,
                                  warning_table,
                                  axes_object,
                                  basemap_object,
                                  storm_outline_colour,
                                  storm_outline_opacity,
                                  include_secondary_ids,
                                  output_dir_name,
                                  primary_id_to_track_colour=None,
                                  radar_matrix=None,
                                  radar_field_name=None,
                                  radar_latitudes_deg=None,
                                  radar_longitudes_deg=None,
                                  radar_colour_map_object=None):
    """Plots storm outlines (and may underlay radar data) at one time step.

    M = number of rows in radar grid
    N = number of columns in radar grid
    K = number of storm objects

    If `primary_id_to_track_colour is None`, all storm tracks will be the same
    colour.

    :param storm_object_table: See doc for `storm_plotting.plot_storm_outlines`.
    :param valid_time_unix_sec: Will plot storm outlines only at this time.
        Will plot tracks up to and including this time.
    :param warning_table: None or a pandas table with the following columns.
    warning_table.start_time_unix_sec: Start time.
    warning_table.end_time_unix_sec: End time.
    warning_table.polygon_object_latlng: Polygon (instance of
        `shapely.geometry.Polygon`) with lat-long coordinates of warning
        boundary.

    :param axes_object: See doc for `storm_plotting.plot_storm_outlines`.
    :param basemap_object: Same.
    :param storm_outline_colour: Same.
    :param storm_outline_opacity: Same.
    :param include_secondary_ids: Same.
    :param output_dir_name: See documentation at top of file.
    :param primary_id_to_track_colour: Dictionary created by
        `_assign_colours_to_storms`.  If this is None, all storm tracks will be
        the same colour.
    :param radar_matrix: M-by-N numpy array of radar values.  If
        `radar_matrix is None`, radar data will simply not be plotted.
    :param radar_field_name: [used only if `radar_matrix is not None`]
        See documentation at top of file.
    :param radar_latitudes_deg: [used only if `radar_matrix is not None`]
        length-M numpy array of grid-point latitudes (deg N).
    :param radar_longitudes_deg: [used only if `radar_matrix is not None`]
        length-N numpy array of grid-point longitudes (deg E).
    :param radar_colour_map_object: [used only if `radar_matrix is not None`]
        Colour map (instance of `matplotlib.pyplot.cm`).  If None, will use
        default for the given field.
    """

    # plot_storm_ids = radar_matrix is None or radar_colour_map_object is None
    plot_storm_ids = False

    min_plot_latitude_deg = basemap_object.llcrnrlat
    max_plot_latitude_deg = basemap_object.urcrnrlat
    min_plot_longitude_deg = basemap_object.llcrnrlon
    max_plot_longitude_deg = basemap_object.urcrnrlon

    plotting_utils.plot_coastlines(basemap_object=basemap_object,
                                   axes_object=axes_object,
                                   line_colour=BORDER_COLOUR)
    plotting_utils.plot_countries(basemap_object=basemap_object,
                                  axes_object=axes_object,
                                  line_colour=BORDER_COLOUR)
    plotting_utils.plot_states_and_provinces(basemap_object=basemap_object,
                                             axes_object=axes_object,
                                             line_colour=BORDER_COLOUR)
    plotting_utils.plot_parallels(basemap_object=basemap_object,
                                  axes_object=axes_object,
                                  num_parallels=NUM_PARALLELS)
    plotting_utils.plot_meridians(basemap_object=basemap_object,
                                  axes_object=axes_object,
                                  num_meridians=NUM_MERIDIANS)

    if radar_matrix is not None:
        custom_colour_map = radar_colour_map_object is not None

        good_indices = numpy.where(
            numpy.logical_and(radar_latitudes_deg >= min_plot_latitude_deg,
                              radar_latitudes_deg <= max_plot_latitude_deg))[0]

        radar_latitudes_deg = radar_latitudes_deg[good_indices]
        radar_matrix = radar_matrix[good_indices, :]

        good_indices = numpy.where(
            numpy.logical_and(
                radar_longitudes_deg >= min_plot_longitude_deg,
                radar_longitudes_deg <= max_plot_longitude_deg))[0]

        radar_longitudes_deg = radar_longitudes_deg[good_indices]
        radar_matrix = radar_matrix[:, good_indices]

        latitude_spacing_deg = radar_latitudes_deg[1] - radar_latitudes_deg[0]
        longitude_spacing_deg = (radar_longitudes_deg[1] -
                                 radar_longitudes_deg[0])

        if radar_colour_map_object is None:
            colour_map_object, colour_norm_object = (
                radar_plotting.get_default_colour_scheme(radar_field_name))
        else:
            colour_map_object = radar_colour_map_object
            colour_norm_object = radar_plotting.get_default_colour_scheme(
                radar_field_name)[-1]

            this_ratio = radar_plotting._field_to_plotting_units(
                field_matrix=1., field_name=radar_field_name)

            colour_norm_object = pyplot.Normalize(
                colour_norm_object.vmin / this_ratio,
                colour_norm_object.vmax / this_ratio)

        radar_plotting.plot_latlng_grid(
            field_matrix=radar_matrix,
            field_name=radar_field_name,
            axes_object=axes_object,
            min_grid_point_latitude_deg=numpy.min(radar_latitudes_deg),
            min_grid_point_longitude_deg=numpy.min(radar_longitudes_deg),
            latitude_spacing_deg=latitude_spacing_deg,
            longitude_spacing_deg=longitude_spacing_deg,
            colour_map_object=colour_map_object,
            colour_norm_object=colour_norm_object)

        latitude_range_deg = max_plot_latitude_deg - min_plot_latitude_deg
        longitude_range_deg = max_plot_longitude_deg - min_plot_longitude_deg

        if latitude_range_deg > longitude_range_deg:
            orientation_string = 'vertical'
        else:
            orientation_string = 'horizontal'

        colour_bar_object = plotting_utils.plot_colour_bar(
            axes_object_or_matrix=axes_object,
            data_matrix=radar_matrix,
            colour_map_object=colour_map_object,
            colour_norm_object=colour_norm_object,
            orientation_string=orientation_string,
            padding=0.05,
            extend_min=radar_field_name in radar_plotting.SHEAR_VORT_DIV_NAMES,
            extend_max=True,
            fraction_of_axis_length=1.)

        radar_field_name_verbose = radar_utils.field_name_to_verbose(
            field_name=radar_field_name, include_units=True)
        radar_field_name_verbose = radar_field_name_verbose.replace(
            'm ASL', 'kft ASL')
        colour_bar_object.set_label(radar_field_name_verbose)

        if custom_colour_map:
            tick_values = colour_bar_object.get_ticks()
            tick_label_strings = ['{0:.1f}'.format(v) for v in tick_values]
            colour_bar_object.set_ticks(tick_values)
            colour_bar_object.set_ticklabels(tick_label_strings)

    valid_time_rows = numpy.where(storm_object_table[
        tracking_utils.VALID_TIME_COLUMN].values == valid_time_unix_sec)[0]

    this_colour = matplotlib.colors.to_rgba(storm_outline_colour,
                                            storm_outline_opacity)

    storm_plotting.plot_storm_outlines(
        storm_object_table=storm_object_table.iloc[valid_time_rows],
        axes_object=axes_object,
        basemap_object=basemap_object,
        line_colour=this_colour)

    if plot_storm_ids:
        storm_plotting.plot_storm_ids(
            storm_object_table=storm_object_table.iloc[valid_time_rows],
            axes_object=axes_object,
            basemap_object=basemap_object,
            plot_near_centroids=False,
            include_secondary_ids=include_secondary_ids,
            font_colour=storm_plotting.DEFAULT_FONT_COLOUR)

    if warning_table is not None:
        warning_indices = numpy.where(
            numpy.logical_and(
                warning_table[WARNING_START_TIME_KEY].values <=
                valid_time_unix_sec, warning_table[WARNING_END_TIME_KEY].values
                >= valid_time_unix_sec))[0]

        for k in warning_indices:
            this_vertex_dict = polygons.polygon_object_to_vertex_arrays(
                warning_table[WARNING_LATLNG_POLYGON_KEY].values[k])
            these_latitudes_deg = this_vertex_dict[polygons.EXTERIOR_Y_COLUMN]
            these_longitudes_deg = this_vertex_dict[polygons.EXTERIOR_X_COLUMN]

            these_latitude_flags = numpy.logical_and(
                these_latitudes_deg >= min_plot_latitude_deg,
                these_latitudes_deg <= max_plot_latitude_deg)
            these_longitude_flags = numpy.logical_and(
                these_longitudes_deg >= min_plot_longitude_deg,
                these_longitudes_deg <= max_plot_longitude_deg)
            these_coord_flags = numpy.logical_and(these_latitude_flags,
                                                  these_longitude_flags)

            if not numpy.any(these_coord_flags):
                continue

            these_x_metres, these_y_metres = basemap_object(
                these_longitudes_deg, these_latitudes_deg)
            axes_object.plot(these_x_metres,
                             these_y_metres,
                             color=this_colour,
                             linestyle='dashed',
                             linewidth=storm_plotting.DEFAULT_POLYGON_WIDTH)

            axes_object.text(numpy.mean(these_x_metres),
                             numpy.mean(these_y_metres),
                             'W{0:d}'.format(k),
                             fontsize=storm_plotting.DEFAULT_FONT_SIZE,
                             fontweight='bold',
                             color=this_colour,
                             horizontalalignment='center',
                             verticalalignment='center')

            these_sec_id_strings = (
                warning_table[LINKED_SECONDARY_IDS_KEY].values[k])
            if len(these_sec_id_strings) == 0:
                continue

            these_object_indices = numpy.array([], dtype=int)

            for this_sec_id_string in these_sec_id_strings:
                these_subindices = numpy.where(
                    storm_object_table[tracking_utils.SECONDARY_ID_COLUMN].
                    values[valid_time_rows] == this_sec_id_string)[0]

                these_object_indices = numpy.concatenate(
                    (these_object_indices, valid_time_rows[these_subindices]))

            for i in these_object_indices:
                this_vertex_dict = polygons.polygon_object_to_vertex_arrays(
                    storm_object_table[
                        tracking_utils.LATLNG_POLYGON_COLUMN].values[i])

                these_x_metres, these_y_metres = basemap_object(
                    this_vertex_dict[polygons.EXTERIOR_X_COLUMN],
                    this_vertex_dict[polygons.EXTERIOR_Y_COLUMN])

                axes_object.text(numpy.mean(these_x_metres),
                                 numpy.mean(these_y_metres),
                                 'W{0:d}'.format(k),
                                 fontsize=storm_plotting.DEFAULT_FONT_SIZE,
                                 fontweight='bold',
                                 color=this_colour,
                                 horizontalalignment='center',
                                 verticalalignment='center')

    if primary_id_to_track_colour is None:
        storm_plotting.plot_storm_tracks(storm_object_table=storm_object_table,
                                         axes_object=axes_object,
                                         basemap_object=basemap_object,
                                         colour_map_object=None,
                                         constant_colour=DEFAULT_TRACK_COLOUR)
    else:
        for this_primary_id_string in primary_id_to_track_colour:
            this_storm_object_table = storm_object_table.loc[
                storm_object_table[tracking_utils.PRIMARY_ID_COLUMN] ==
                this_primary_id_string]

            if len(this_storm_object_table.index) == 0:
                continue

            storm_plotting.plot_storm_tracks(
                storm_object_table=this_storm_object_table,
                axes_object=axes_object,
                basemap_object=basemap_object,
                colour_map_object=None,
                constant_colour=primary_id_to_track_colour[
                    this_primary_id_string])

    nice_time_string = time_conversion.unix_sec_to_string(
        valid_time_unix_sec, NICE_TIME_FORMAT)

    abbrev_time_string = time_conversion.unix_sec_to_string(
        valid_time_unix_sec, FILE_NAME_TIME_FORMAT)

    pyplot.title('Storm objects at {0:s}'.format(nice_time_string))
    output_file_name = '{0:s}/storm_outlines_{1:s}.jpg'.format(
        output_dir_name, abbrev_time_string)

    print('Saving figure to: "{0:s}"...'.format(output_file_name))
    pyplot.savefig(output_file_name,
                   dpi=FIGURE_RESOLUTION_DPI,
                   pad_inches=0,
                   bbox_inches='tight')
    pyplot.close()
Exemplo n.º 7
0
def _plot_bwo_for_3d_radar(
        optimized_radar_matrix, training_option_dict, diff_colour_map_object,
        max_colour_percentile_for_diff, top_output_dir_name, pmm_flag,
        input_radar_matrix=None, storm_ids=None, storm_times_unix_sec=None):
    """Plots BWO results for 3-D radar fields.

    E = number of examples (storm objects)
    M = number of rows in spatial grid
    N = number of columns in spatial grid
    H = number of heights in spatial grid
    F = number of fields

    :param optimized_radar_matrix: E-by-M-by-N-by-H-by-F numpy array of radar
        values (predictors).
    :param training_option_dict: See doc for `_plot_bwo_for_2d3d_radar`.
    :param diff_colour_map_object: Same.
    :param max_colour_percentile_for_diff: Same.
    :param top_output_dir_name: Same.
    :param pmm_flag: Same.
    :param input_radar_matrix: Same as `optimized_radar_matrix` but with
        non-optimized input.
    :param storm_ids: See doc for `_plot_bwo_for_2d3d_radar`.
    :param storm_times_unix_sec: Same.
    """

    before_optimization_dir_name = '{0:s}/before_optimization'.format(
        top_output_dir_name)
    after_optimization_dir_name = '{0:s}/after_optimization'.format(
        top_output_dir_name)
    difference_dir_name = '{0:s}/after_minus_before_optimization'.format(
        top_output_dir_name)

    file_system_utils.mkdir_recursive_if_necessary(
        directory_name=before_optimization_dir_name)
    file_system_utils.mkdir_recursive_if_necessary(
        directory_name=after_optimization_dir_name)
    file_system_utils.mkdir_recursive_if_necessary(
        directory_name=difference_dir_name)

    if pmm_flag:
        have_storm_ids = False
    else:
        have_storm_ids = not (storm_ids is None or storm_times_unix_sec is None)

    radar_field_names = training_option_dict[trainval_io.RADAR_FIELDS_KEY]
    radar_heights_m_agl = training_option_dict[trainval_io.RADAR_HEIGHTS_KEY]

    num_storms = optimized_radar_matrix.shape[0]
    num_heights = optimized_radar_matrix.shape[-2]
    num_panel_rows = int(numpy.floor(
        numpy.sqrt(num_heights)
    ))

    for i in range(num_storms):
        print '\n'

        if pmm_flag:
            this_base_title_string = 'Probability-matched mean'
            this_base_pathless_file_name = 'pmm'
        else:
            if have_storm_ids:
                this_storm_time_string = time_conversion.unix_sec_to_string(
                    storm_times_unix_sec[i], TIME_FORMAT)

                this_base_title_string = 'Storm "{0:s}" at {1:s}'.format(
                    storm_ids[i], this_storm_time_string)

                this_base_pathless_file_name = '{0:s}_{1:s}'.format(
                    storm_ids[i].replace('_', '-'), this_storm_time_string)

            else:
                this_base_title_string = 'Example {0:d}'.format(i + 1)
                this_base_pathless_file_name = 'example{0:06d}'.format(i)

        for j in range(len(radar_field_names)):
            _, these_axes_objects = (
                radar_plotting.plot_3d_grid_without_coords(
                    field_matrix=numpy.flip(
                        optimized_radar_matrix[i, ..., j], axis=0),
                    field_name=radar_field_names[j],
                    grid_point_heights_metres=radar_heights_m_agl,
                    ground_relative=True, num_panel_rows=num_panel_rows,
                    font_size=FONT_SIZE_SANS_COLOUR_BARS)
            )

            this_colour_map_object, this_colour_norm_object = (
                radar_plotting.get_default_colour_scheme(
                    radar_field_names[j])
            )

            plotting_utils.add_colour_bar(
                axes_object_or_list=these_axes_objects,
                values_to_colour=optimized_radar_matrix[i, ..., j],
                colour_map=this_colour_map_object,
                colour_norm_object=this_colour_norm_object,
                orientation='horizontal', extend_min=True, extend_max=True)

            this_title_string = '{0:s} (after optimization)'.format(
                this_base_title_string)

            this_file_name = (
                '{0:s}/{1:s}_after-optimization_{2:s}.jpg'
            ).format(
                after_optimization_dir_name, this_base_pathless_file_name,
                radar_field_names[j].replace('_', '-')
            )

            pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)
            print 'Saving figure to: "{0:s}"...'.format(this_file_name)
            pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
            pyplot.close()

            if input_radar_matrix is None:
                continue

            _, these_axes_objects = (
                radar_plotting.plot_3d_grid_without_coords(
                    field_matrix=numpy.flip(
                        input_radar_matrix[i, ..., j], axis=0),
                    field_name=radar_field_names[j],
                    grid_point_heights_metres=radar_heights_m_agl,
                    ground_relative=True, num_panel_rows=num_panel_rows,
                    font_size=FONT_SIZE_SANS_COLOUR_BARS)
            )

            this_colour_map_object, this_colour_norm_object = (
                radar_plotting.get_default_colour_scheme(
                    radar_field_names[j])
            )

            plotting_utils.add_colour_bar(
                axes_object_or_list=these_axes_objects,
                values_to_colour=input_radar_matrix[i, ..., j],
                colour_map=this_colour_map_object,
                colour_norm_object=this_colour_norm_object,
                orientation='horizontal', extend_min=True, extend_max=True)

            this_title_string = '{0:s} (before optimization)'.format(
                this_base_title_string)

            this_file_name = (
                '{0:s}/{1:s}_before-optimization_{2:s}.jpg'
            ).format(
                before_optimization_dir_name, this_base_pathless_file_name,
                radar_field_names[j].replace('_', '-')
            )

            pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)
            print 'Saving figure to: "{0:s}"...'.format(this_file_name)
            pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
            pyplot.close()

            this_diff_matrix = (
                optimized_radar_matrix[i, ..., j] -
                input_radar_matrix[i, ..., j]
            )

            this_max_value = numpy.percentile(
                numpy.absolute(this_diff_matrix),
                max_colour_percentile_for_diff)

            this_colour_norm_object = matplotlib.colors.Normalize(
                vmin=-1 * this_max_value, vmax=this_max_value, clip=False)

            _, these_axes_objects = (
                radar_plotting.plot_3d_grid_without_coords(
                    field_matrix=numpy.flip(this_diff_matrix, axis=0),
                    field_name=radar_field_names[j],
                    grid_point_heights_metres=radar_heights_m_agl,
                    ground_relative=True, num_panel_rows=num_panel_rows,
                    font_size=FONT_SIZE_SANS_COLOUR_BARS,
                    colour_map_object=diff_colour_map_object,
                    colour_norm_object=this_colour_norm_object)
            )

            plotting_utils.add_colour_bar(
                axes_object_or_list=these_axes_objects,
                values_to_colour=this_diff_matrix,
                colour_map=diff_colour_map_object,
                colour_norm_object=this_colour_norm_object,
                orientation='horizontal', extend_min=True, extend_max=True)

            this_title_string = (
                '{0:s} (after minus before optimization)'
            ).format(this_base_title_string)

            this_file_name = (
                '{0:s}/{1:s}_optimization-diff_{2:s}.jpg'
            ).format(
                difference_dir_name, this_base_pathless_file_name,
                radar_field_names[j].replace('_', '-')
            )

            pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)
            print 'Saving figure to: "{0:s}"...'.format(this_file_name)
            pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
            pyplot.close()
Exemplo n.º 8
0
def _plot_bwo_for_2d3d_radar(
        list_of_optimized_matrices, training_option_dict,
        diff_colour_map_object, max_colour_percentile_for_diff,
        top_output_dir_name, pmm_flag, list_of_input_matrices=None,
        storm_ids=None, storm_times_unix_sec=None):
    """Plots BWO results for 2-D azimuthal-shear and 3-D reflectivity fields.

    E = number of examples (storm objects)
    T = number of input tensors to the model

    :param list_of_optimized_matrices: length-T list of numpy arrays, where the
        [i]th array is the optimized version of the [i]th input matrix to the
        model.
    :param training_option_dict: See doc for `cnn.read_model_metadata`.
    :param diff_colour_map_object: See documentation at top of file.
    :param max_colour_percentile_for_diff: Same.
    :param top_output_dir_name: Path to top-level output directory (figures will
        be saved here).
    :param pmm_flag: Boolean flag.  If True, `list_of_predictor_matrices`
        contains probability-matched means.
    :param list_of_input_matrices: Same as `list_of_optimized_matrices` but with
        non-optimized input matrices.
    :param storm_ids: [optional and used only if `pmm_flag = False`]
        length-E list of storm IDs (strings).
    :param storm_times_unix_sec: [optional and used only if `pmm_flag = False`]
        length-E numpy array of storm times.
    """

    before_optimization_dir_name = '{0:s}/before_optimization'.format(
        top_output_dir_name)
    after_optimization_dir_name = '{0:s}/after_optimization'.format(
        top_output_dir_name)
    difference_dir_name = '{0:s}/after_minus_before_optimization'.format(
        top_output_dir_name)

    file_system_utils.mkdir_recursive_if_necessary(
        directory_name=before_optimization_dir_name)
    file_system_utils.mkdir_recursive_if_necessary(
        directory_name=after_optimization_dir_name)
    file_system_utils.mkdir_recursive_if_necessary(
        directory_name=difference_dir_name)

    if pmm_flag:
        have_storm_ids = False
    else:
        have_storm_ids = not (storm_ids is None or storm_times_unix_sec is None)

    az_shear_field_names = training_option_dict[trainval_io.RADAR_FIELDS_KEY]
    num_az_shear_fields = len(az_shear_field_names)
    plot_colour_bar_flags = numpy.full(num_az_shear_fields, False, dtype=bool)

    num_storms = list_of_optimized_matrices[0].shape[0]

    for i in range(num_storms):
        print '\n'

        if pmm_flag:
            this_base_title_string = 'Probability-matched mean'
            this_base_pathless_file_name = 'pmm'
        else:
            if have_storm_ids:
                this_storm_time_string = time_conversion.unix_sec_to_string(
                    storm_times_unix_sec[i], TIME_FORMAT)

                this_base_title_string = 'Storm "{0:s}" at {1:s}'.format(
                    storm_ids[i], this_storm_time_string)

                this_base_pathless_file_name = '{0:s}_{1:s}'.format(
                    storm_ids[i].replace('_', '-'), this_storm_time_string)

            else:
                this_base_title_string = 'Example {0:d}'.format(i + 1)
                this_base_pathless_file_name = 'example{0:06d}'.format(i)

        this_reflectivity_matrix_dbz = numpy.flip(
            list_of_optimized_matrices[0][i, ..., 0], axis=0)

        this_num_heights = this_reflectivity_matrix_dbz.shape[-1]
        this_num_panel_rows = int(numpy.floor(
            numpy.sqrt(this_num_heights)
        ))

        _, these_axes_objects = radar_plotting.plot_3d_grid_without_coords(
            field_matrix=this_reflectivity_matrix_dbz,
            field_name=radar_utils.REFL_NAME,
            grid_point_heights_metres=training_option_dict[
                trainval_io.RADAR_HEIGHTS_KEY],
            ground_relative=True, num_panel_rows=this_num_panel_rows,
            font_size=FONT_SIZE_SANS_COLOUR_BARS)

        this_colour_map_object, this_colour_norm_object = (
            radar_plotting.get_default_colour_scheme(radar_utils.REFL_NAME)
        )

        plotting_utils.add_colour_bar(
            axes_object_or_list=these_axes_objects,
            values_to_colour=this_reflectivity_matrix_dbz,
            colour_map=this_colour_map_object,
            colour_norm_object=this_colour_norm_object,
            orientation='horizontal', extend_min=True, extend_max=True)

        this_title_string = '{0:s} (after optimization)'.format(
            this_base_title_string)

        this_file_name = (
            '{0:s}/{1:s}_after-optimization_reflectivity.jpg'
        ).format(after_optimization_dir_name, this_base_pathless_file_name)

        pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)
        print 'Saving figure to: "{0:s}"...'.format(this_file_name)
        pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
        pyplot.close()

        this_az_shear_matrix_s01 = numpy.flip(
            list_of_optimized_matrices[1][i, ..., 0], axis=0)

        _, these_axes_objects = (
            radar_plotting.plot_many_2d_grids_without_coords(
                field_matrix=this_az_shear_matrix_s01,
                field_name_by_panel=az_shear_field_names, num_panel_rows=1,
                panel_names=az_shear_field_names,
                plot_colour_bar_by_panel=plot_colour_bar_flags,
                font_size=FONT_SIZE_SANS_COLOUR_BARS)
        )

        this_colour_map_object, this_colour_norm_object = (
            radar_plotting.get_default_colour_scheme(
                radar_utils.LOW_LEVEL_SHEAR_NAME)
        )

        plotting_utils.add_colour_bar(
            axes_object_or_list=these_axes_objects,
            values_to_colour=this_az_shear_matrix_s01,
            colour_map=this_colour_map_object,
            colour_norm_object=this_colour_norm_object,
            orientation='horizontal', extend_min=True, extend_max=True)

        this_title_string = '{0:s} (after optimization)'.format(
            this_base_title_string)

        this_file_name = (
            '{0:s}/{1:s}_after-optimization_azimuthal-shear.jpg'
        ).format(after_optimization_dir_name, this_base_pathless_file_name)

        pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)
        print 'Saving figure to: "{0:s}"...'.format(this_file_name)
        pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
        pyplot.close()

        if list_of_input_matrices is None:
            continue

        this_reflectivity_matrix_dbz = numpy.flip(
            list_of_input_matrices[0][i, ..., 0], axis=0)

        _, these_axes_objects = radar_plotting.plot_3d_grid_without_coords(
            field_matrix=this_reflectivity_matrix_dbz,
            field_name=radar_utils.REFL_NAME,
            grid_point_heights_metres=training_option_dict[
                trainval_io.RADAR_HEIGHTS_KEY],
            ground_relative=True, num_panel_rows=this_num_panel_rows,
            font_size=FONT_SIZE_SANS_COLOUR_BARS)

        this_colour_map_object, this_colour_norm_object = (
            radar_plotting.get_default_colour_scheme(radar_utils.REFL_NAME)
        )

        plotting_utils.add_colour_bar(
            axes_object_or_list=these_axes_objects,
            values_to_colour=this_reflectivity_matrix_dbz,
            colour_map=this_colour_map_object,
            colour_norm_object=this_colour_norm_object,
            orientation='horizontal', extend_min=True, extend_max=True)

        this_title_string = '{0:s} (before optimization)'.format(
            this_base_title_string)

        this_file_name = (
            '{0:s}/{1:s}_before-optimization_reflectivity.jpg'
        ).format(before_optimization_dir_name, this_base_pathless_file_name)

        pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)
        print 'Saving figure to: "{0:s}"...'.format(this_file_name)
        pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
        pyplot.close()

        this_az_shear_matrix_s01 = numpy.flip(
            list_of_input_matrices[1][i, ..., 0], axis=0)

        _, these_axes_objects = (
            radar_plotting.plot_many_2d_grids_without_coords(
                field_matrix=this_az_shear_matrix_s01,
                field_name_by_panel=az_shear_field_names, num_panel_rows=1,
                panel_names=az_shear_field_names,
                plot_colour_bar_by_panel=plot_colour_bar_flags,
                font_size=FONT_SIZE_SANS_COLOUR_BARS)
        )

        this_colour_map_object, this_colour_norm_object = (
            radar_plotting.get_default_colour_scheme(
                radar_utils.LOW_LEVEL_SHEAR_NAME)
        )

        plotting_utils.add_colour_bar(
            axes_object_or_list=these_axes_objects,
            values_to_colour=this_az_shear_matrix_s01,
            colour_map=this_colour_map_object,
            colour_norm_object=this_colour_norm_object,
            orientation='horizontal', extend_min=True, extend_max=True)

        this_title_string = '{0:s} (before optimization)'.format(
            this_base_title_string)

        this_file_name = (
            '{0:s}/{1:s}_before-optimization_azimuthal-shear.jpg'
        ).format(before_optimization_dir_name, this_base_pathless_file_name)

        pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)
        print 'Saving figure to: "{0:s}"...'.format(this_file_name)
        pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
        pyplot.close()

        this_refl_diff_matrix_dbz = (
            list_of_optimized_matrices[0][i, ..., 0] -
            list_of_input_matrices[0][i, ..., 0]
        )
        this_refl_diff_matrix_dbz = numpy.flip(
            this_refl_diff_matrix_dbz, axis=0)

        this_max_value_dbz = numpy.percentile(
            numpy.absolute(this_refl_diff_matrix_dbz),
            max_colour_percentile_for_diff)

        this_colour_norm_object = matplotlib.colors.Normalize(
            vmin=-1 * this_max_value_dbz, vmax=this_max_value_dbz, clip=False)

        _, these_axes_objects = radar_plotting.plot_3d_grid_without_coords(
            field_matrix=this_refl_diff_matrix_dbz,
            field_name=radar_utils.REFL_NAME,
            grid_point_heights_metres=training_option_dict[
                trainval_io.RADAR_HEIGHTS_KEY],
            ground_relative=True, num_panel_rows=this_num_panel_rows,
            font_size=FONT_SIZE_SANS_COLOUR_BARS,
            colour_map_object=diff_colour_map_object,
            colour_norm_object=this_colour_norm_object)

        plotting_utils.add_colour_bar(
            axes_object_or_list=these_axes_objects,
            values_to_colour=this_refl_diff_matrix_dbz,
            colour_map=diff_colour_map_object,
            colour_norm_object=this_colour_norm_object,
            orientation='horizontal', extend_min=True, extend_max=True)

        this_title_string = '{0:s} (after minus before optimization)'.format(
            this_base_title_string)

        this_file_name = (
            '{0:s}/{1:s}_optimization-diff_reflectivity.jpg'
        ).format(difference_dir_name, this_base_pathless_file_name)

        pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)
        print 'Saving figure to: "{0:s}"...'.format(this_file_name)
        pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
        pyplot.close()

        this_shear_diff_matrix_s01 = (
            list_of_optimized_matrices[1][i, ..., 0] -
            list_of_input_matrices[1][i, ..., 0]
        )
        this_shear_diff_matrix_s01 = numpy.flip(
            this_shear_diff_matrix_s01, axis=0)

        this_max_value_s01 = numpy.percentile(
            numpy.absolute(this_shear_diff_matrix_s01),
            max_colour_percentile_for_diff)

        this_colour_norm_object = matplotlib.colors.Normalize(
            vmin=-1 * this_max_value_s01, vmax=this_max_value_s01, clip=False)

        _, these_axes_objects = (
            radar_plotting.plot_many_2d_grids_without_coords(
                field_matrix=this_shear_diff_matrix_s01,
                field_name_by_panel=az_shear_field_names, num_panel_rows=1,
                panel_names=az_shear_field_names,
                colour_map_object_by_panel=
                [diff_colour_map_object] * num_az_shear_fields,
                colour_norm_object_by_panel=
                [copy.deepcopy(this_colour_norm_object)] * num_az_shear_fields,
                plot_colour_bar_by_panel=plot_colour_bar_flags,
                font_size=FONT_SIZE_SANS_COLOUR_BARS)
        )

        plotting_utils.add_colour_bar(
            axes_object_or_list=these_axes_objects,
            values_to_colour=this_shear_diff_matrix_s01,
            colour_map=diff_colour_map_object,
            colour_norm_object=this_colour_norm_object,
            orientation='horizontal', extend_min=True, extend_max=True)

        this_title_string = '{0:s} (after minus before optimization)'.format(
            this_base_title_string)

        this_file_name = (
            '{0:s}/{1:s}_optimization-diff_azimuthal-shear.jpg'
        ).format(difference_dir_name, this_base_pathless_file_name)

        pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)
        print 'Saving figure to: "{0:s}"...'.format(this_file_name)
        pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
        pyplot.close()
Exemplo n.º 9
0
def _plot_one_field(reflectivity_matrix_dbz, latitudes_deg, longitudes_deg,
                    add_colour_bar, panel_letter, output_file_name):
    """Plots reflectivity field from one dataset.

    :param reflectivity_matrix_dbz: See doc for `_read_file`.
    :param latitudes_deg: Same.
    :param longitudes_deg: Same.
    :param add_colour_bar: Boolean flag.
    :param panel_letter: Panel letter (will be printed at top left of figure).
    :param output_file_name: Path to output file (figure will be saved here).
    """

    (figure_object, axes_object,
     basemap_object) = plotting_utils.create_equidist_cylindrical_map(
         min_latitude_deg=numpy.min(latitudes_deg),
         max_latitude_deg=numpy.max(latitudes_deg),
         min_longitude_deg=numpy.min(longitudes_deg),
         max_longitude_deg=numpy.max(longitudes_deg),
         resolution_string='i')

    plotting_utils.plot_coastlines(basemap_object=basemap_object,
                                   axes_object=axes_object,
                                   line_colour=BORDER_COLOUR)
    plotting_utils.plot_countries(basemap_object=basemap_object,
                                  axes_object=axes_object,
                                  line_colour=BORDER_COLOUR)
    plotting_utils.plot_states_and_provinces(basemap_object=basemap_object,
                                             axes_object=axes_object,
                                             line_colour=BORDER_COLOUR)
    plotting_utils.plot_parallels(basemap_object=basemap_object,
                                  axes_object=axes_object,
                                  num_parallels=NUM_PARALLELS)
    plotting_utils.plot_meridians(basemap_object=basemap_object,
                                  axes_object=axes_object,
                                  num_meridians=NUM_MERIDIANS)

    radar_plotting.plot_latlng_grid(
        field_matrix=reflectivity_matrix_dbz,
        field_name=RADAR_FIELD_NAME,
        axes_object=axes_object,
        min_grid_point_latitude_deg=numpy.min(latitudes_deg),
        min_grid_point_longitude_deg=numpy.min(longitudes_deg),
        latitude_spacing_deg=latitudes_deg[1] - latitudes_deg[0],
        longitude_spacing_deg=longitudes_deg[1] - longitudes_deg[0])

    if add_colour_bar:
        colour_map_object, colour_norm_object = (
            radar_plotting.get_default_colour_scheme(RADAR_FIELD_NAME))

        plotting_utils.plot_colour_bar(axes_object_or_matrix=axes_object,
                                       data_matrix=reflectivity_matrix_dbz,
                                       colour_map_object=colour_map_object,
                                       colour_norm_object=colour_norm_object,
                                       orientation_string='horizontal',
                                       padding=0.05,
                                       extend_min=False,
                                       extend_max=True,
                                       fraction_of_axis_length=1.)

    plotting_utils.label_axes(axes_object=axes_object,
                              label_string='({0:s})'.format(panel_letter),
                              y_coord_normalized=1.03)

    print('Saving figure to: "{0:s}"...'.format(output_file_name))
    figure_object.savefig(output_file_name,
                          dpi=FIGURE_RESOLUTION_DPI,
                          pad_inches=0,
                          bbox_inches='tight')
    pyplot.close(figure_object)
def _plot_one_example_one_time(storm_object_table, full_id_string,
                               valid_time_unix_sec, tornado_table,
                               top_myrorss_dir_name, radar_field_name,
                               radar_height_m_asl, latitude_limits_deg,
                               longitude_limits_deg):
    """Plots one example with surrounding context at one time.

    :param storm_object_table: pandas DataFrame, containing only storm objects
        at one time with the relevant primary ID.  Columns are documented in
        `storm_tracking_io.write_file`.
    :param full_id_string: Full ID of storm of interest.
    :param valid_time_unix_sec: Valid time.
    :param tornado_table: pandas DataFrame created by
        `linkage._read_input_tornado_reports`.
    :param top_myrorss_dir_name: See documentation at top of file.
    :param radar_field_name: Same.
    :param radar_height_m_asl: Same.
    :param latitude_limits_deg: See doc for `_get_plotting_limits`.
    :param longitude_limits_deg: Same.
    """

    min_plot_latitude_deg = latitude_limits_deg[0]
    max_plot_latitude_deg = latitude_limits_deg[1]
    min_plot_longitude_deg = longitude_limits_deg[0]
    max_plot_longitude_deg = longitude_limits_deg[1]

    radar_file_name = myrorss_and_mrms_io.find_raw_file(
        top_directory_name=top_myrorss_dir_name,
        spc_date_string=time_conversion.time_to_spc_date_string(
            valid_time_unix_sec),
        unix_time_sec=valid_time_unix_sec,
        data_source=radar_utils.MYRORSS_SOURCE_ID,
        field_name=radar_field_name,
        height_m_asl=radar_height_m_asl,
        raise_error_if_missing=True)

    print('Reading data from: "{0:s}"...'.format(radar_file_name))

    radar_metadata_dict = myrorss_and_mrms_io.read_metadata_from_raw_file(
        netcdf_file_name=radar_file_name,
        data_source=radar_utils.MYRORSS_SOURCE_ID)

    sparse_grid_table = (myrorss_and_mrms_io.read_data_from_sparse_grid_file(
        netcdf_file_name=radar_file_name,
        field_name_orig=radar_metadata_dict[
            myrorss_and_mrms_io.FIELD_NAME_COLUMN_ORIG],
        data_source=radar_utils.MYRORSS_SOURCE_ID,
        sentinel_values=radar_metadata_dict[radar_utils.SENTINEL_VALUE_COLUMN])
                         )

    radar_matrix, grid_point_latitudes_deg, grid_point_longitudes_deg = (
        radar_s2f.sparse_to_full_grid(sparse_grid_table=sparse_grid_table,
                                      metadata_dict=radar_metadata_dict))

    radar_matrix = numpy.flip(radar_matrix, axis=0)
    grid_point_latitudes_deg = grid_point_latitudes_deg[::-1]

    axes_object, basemap_object = (
        plotting_utils.create_equidist_cylindrical_map(
            min_latitude_deg=min_plot_latitude_deg,
            max_latitude_deg=max_plot_latitude_deg,
            min_longitude_deg=min_plot_longitude_deg,
            max_longitude_deg=max_plot_longitude_deg,
            resolution_string='i')[1:])

    plotting_utils.plot_coastlines(basemap_object=basemap_object,
                                   axes_object=axes_object,
                                   line_colour=BORDER_COLOUR)

    plotting_utils.plot_countries(basemap_object=basemap_object,
                                  axes_object=axes_object,
                                  line_colour=BORDER_COLOUR)

    plotting_utils.plot_states_and_provinces(basemap_object=basemap_object,
                                             axes_object=axes_object,
                                             line_colour=BORDER_COLOUR)

    plotting_utils.plot_parallels(basemap_object=basemap_object,
                                  axes_object=axes_object,
                                  num_parallels=NUM_PARALLELS)

    plotting_utils.plot_meridians(basemap_object=basemap_object,
                                  axes_object=axes_object,
                                  num_meridians=NUM_MERIDIANS)

    radar_plotting.plot_latlng_grid(
        field_matrix=radar_matrix,
        field_name=radar_field_name,
        axes_object=axes_object,
        min_grid_point_latitude_deg=numpy.min(grid_point_latitudes_deg),
        min_grid_point_longitude_deg=numpy.min(grid_point_longitudes_deg),
        latitude_spacing_deg=numpy.diff(grid_point_latitudes_deg[:2])[0],
        longitude_spacing_deg=numpy.diff(grid_point_longitudes_deg[:2])[0])

    colour_map_object, colour_norm_object = (
        radar_plotting.get_default_colour_scheme(radar_field_name))

    plotting_utils.plot_colour_bar(axes_object_or_matrix=axes_object,
                                   data_matrix=radar_matrix,
                                   colour_map_object=colour_map_object,
                                   colour_norm_object=colour_norm_object,
                                   orientation_string='horizontal',
                                   extend_min=False,
                                   extend_max=True,
                                   fraction_of_axis_length=0.8)

    first_list, second_list = temporal_tracking.full_to_partial_ids(
        [full_id_string])
    primary_id_string = first_list[0]
    secondary_id_string = second_list[0]

    # Plot outlines of unrelated storms (with different primary IDs).
    this_storm_object_table = storm_object_table.loc[storm_object_table[
        tracking_utils.PRIMARY_ID_COLUMN] != primary_id_string]

    storm_plotting.plot_storm_outlines(
        storm_object_table=this_storm_object_table,
        axes_object=axes_object,
        basemap_object=basemap_object,
        line_width=2,
        line_colour='k',
        line_style='dashed')

    # Plot outlines of related storms (with the same primary ID).
    this_storm_object_table = storm_object_table.loc[
        (storm_object_table[tracking_utils.PRIMARY_ID_COLUMN] ==
         primary_id_string) & (storm_object_table[
             tracking_utils.SECONDARY_ID_COLUMN] != secondary_id_string)]

    this_num_storm_objects = len(this_storm_object_table.index)

    if this_num_storm_objects > 0:
        storm_plotting.plot_storm_outlines(
            storm_object_table=this_storm_object_table,
            axes_object=axes_object,
            basemap_object=basemap_object,
            line_width=2,
            line_colour='k',
            line_style='solid')

        for j in range(len(this_storm_object_table)):
            axes_object.text(
                this_storm_object_table[
                    tracking_utils.CENTROID_LONGITUDE_COLUMN].values[j],
                this_storm_object_table[
                    tracking_utils.CENTROID_LATITUDE_COLUMN].values[j],
                'P',
                fontsize=FONT_SIZE,
                color=FONT_COLOUR,
                fontweight='bold',
                horizontalalignment='center',
                verticalalignment='center')

    # Plot outline of storm of interest (same secondary ID).
    this_storm_object_table = storm_object_table.loc[storm_object_table[
        tracking_utils.SECONDARY_ID_COLUMN] == secondary_id_string]

    storm_plotting.plot_storm_outlines(
        storm_object_table=this_storm_object_table,
        axes_object=axes_object,
        basemap_object=basemap_object,
        line_width=4,
        line_colour='k',
        line_style='solid')

    this_num_storm_objects = len(this_storm_object_table.index)

    plot_forecast = (this_num_storm_objects > 0 and FORECAST_PROBABILITY_COLUMN
                     in list(this_storm_object_table))

    if plot_forecast:
        this_polygon_object_latlng = this_storm_object_table[
            tracking_utils.LATLNG_POLYGON_COLUMN].values[0]

        this_latitude_deg = numpy.min(
            numpy.array(this_polygon_object_latlng.exterior.xy[1]))

        this_longitude_deg = this_storm_object_table[
            tracking_utils.CENTROID_LONGITUDE_COLUMN].values[0]

        label_string = 'Prob = {0:.3f}\nat {1:s}'.format(
            this_storm_object_table[FORECAST_PROBABILITY_COLUMN].values[0],
            time_conversion.unix_sec_to_string(valid_time_unix_sec,
                                               TORNADO_TIME_FORMAT))

        bounding_box_dict = {
            'facecolor':
            plotting_utils.colour_from_numpy_to_tuple(
                PROBABILITY_BACKGROUND_COLOUR),
            'alpha':
            PROBABILITY_BACKGROUND_OPACITY,
            'edgecolor':
            'k',
            'linewidth':
            1
        }

        axes_object.text(this_longitude_deg,
                         this_latitude_deg,
                         label_string,
                         fontsize=FONT_SIZE,
                         color=plotting_utils.colour_from_numpy_to_tuple(
                             PROBABILITY_FONT_COLOUR),
                         fontweight='bold',
                         bbox=bounding_box_dict,
                         horizontalalignment='center',
                         verticalalignment='top',
                         zorder=1e10)

    tornado_latitudes_deg = tornado_table[linkage.EVENT_LATITUDE_COLUMN].values
    tornado_longitudes_deg = tornado_table[
        linkage.EVENT_LONGITUDE_COLUMN].values

    tornado_times_unix_sec = tornado_table[linkage.EVENT_TIME_COLUMN].values
    tornado_time_strings = [
        time_conversion.unix_sec_to_string(t, TORNADO_TIME_FORMAT)
        for t in tornado_times_unix_sec
    ]

    axes_object.plot(tornado_longitudes_deg,
                     tornado_latitudes_deg,
                     linestyle='None',
                     marker=TORNADO_MARKER_TYPE,
                     markersize=TORNADO_MARKER_SIZE,
                     markeredgewidth=TORNADO_MARKER_EDGE_WIDTH,
                     markerfacecolor=plotting_utils.colour_from_numpy_to_tuple(
                         TORNADO_MARKER_COLOUR),
                     markeredgecolor=plotting_utils.colour_from_numpy_to_tuple(
                         TORNADO_MARKER_COLOUR))

    num_tornadoes = len(tornado_latitudes_deg)

    for j in range(num_tornadoes):
        axes_object.text(tornado_longitudes_deg[j] + 0.02,
                         tornado_latitudes_deg[j] - 0.02,
                         tornado_time_strings[j],
                         fontsize=FONT_SIZE,
                         color=FONT_COLOUR,
                         fontweight='bold',
                         horizontalalignment='left',
                         verticalalignment='top')
Exemplo n.º 11
0
def _plot_2d3d_radar_scan(list_of_predictor_matrices,
                          model_metadata_dict,
                          allow_whitespace,
                          title_string=None):
    """Plots 3-D reflectivity and 2-D azimuthal shear for one example.

    :param list_of_predictor_matrices: See doc for `_plot_3d_radar_scan`.
    :param model_metadata_dict: Same.
    :param allow_whitespace: Same.
    :param title_string: Same.
    :return: figure_objects: length-2 list of figure handles (instances of
        `matplotlib.figure.Figure`).  The first is for reflectivity; the second
        is for azimuthal shear.
    :return: axes_object_matrices: length-2 list (the first is for reflectivity;
        the second is for azimuthal shear).  Each element is a 2-D numpy
        array of axes handles (instances of
        `matplotlib.axes._subplots.AxesSubplot`).
    """

    training_option_dict = model_metadata_dict[cnn.TRAINING_OPTION_DICT_KEY]
    az_shear_field_names = training_option_dict[trainval_io.RADAR_FIELDS_KEY]
    refl_heights_m_agl = training_option_dict[trainval_io.RADAR_HEIGHTS_KEY]

    num_az_shear_fields = len(az_shear_field_names)
    num_refl_heights = len(refl_heights_m_agl)

    this_num_panel_rows = int(numpy.floor(numpy.sqrt(num_refl_heights)))
    this_num_panel_columns = int(
        numpy.ceil(float(num_refl_heights) / this_num_panel_rows))

    if allow_whitespace:
        refl_figure_object = None
        refl_axes_object_matrix = None
    else:
        refl_figure_object, refl_axes_object_matrix = (
            plotting_utils.create_paneled_figure(
                num_rows=this_num_panel_rows,
                num_columns=this_num_panel_columns,
                horizontal_spacing=0.,
                vertical_spacing=0.,
                shared_x_axis=False,
                shared_y_axis=False,
                keep_aspect_ratio=True))

    refl_figure_object, refl_axes_object_matrix = (
        radar_plotting.plot_3d_grid_without_coords(
            field_matrix=numpy.flip(list_of_predictor_matrices[0][..., 0],
                                    axis=0),
            field_name=radar_utils.REFL_NAME,
            grid_point_heights_metres=refl_heights_m_agl,
            ground_relative=True,
            num_panel_rows=this_num_panel_rows,
            figure_object=refl_figure_object,
            axes_object_matrix=refl_axes_object_matrix,
            font_size=FONT_SIZE_SANS_COLOUR_BARS))

    if allow_whitespace:
        this_colour_map_object, this_colour_norm_object = (
            radar_plotting.get_default_colour_scheme(radar_utils.REFL_NAME))

        plotting_utils.plot_colour_bar(
            axes_object_or_matrix=refl_axes_object_matrix,
            data_matrix=list_of_predictor_matrices[0],
            colour_map_object=this_colour_map_object,
            colour_norm_object=this_colour_norm_object,
            orientation_string='horizontal',
            extend_min=True,
            extend_max=True)

        if title_string is not None:
            this_title_string = '{0:s}; {1:s}'.format(title_string,
                                                      radar_utils.REFL_NAME)
            pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)

    if allow_whitespace:
        shear_figure_object = None
        shear_axes_object_matrix = None
    else:
        shear_figure_object, shear_axes_object_matrix = (
            plotting_utils.create_paneled_figure(
                num_rows=1,
                num_columns=num_az_shear_fields,
                horizontal_spacing=0.,
                vertical_spacing=0.,
                shared_x_axis=False,
                shared_y_axis=False,
                keep_aspect_ratio=True))

    shear_figure_object, shear_axes_object_matrix = (
        radar_plotting.plot_many_2d_grids_without_coords(
            field_matrix=numpy.flip(list_of_predictor_matrices[1], axis=0),
            field_name_by_panel=az_shear_field_names,
            panel_names=az_shear_field_names,
            num_panel_rows=1,
            figure_object=shear_figure_object,
            axes_object_matrix=shear_axes_object_matrix,
            plot_colour_bar_by_panel=numpy.full(num_az_shear_fields,
                                                False,
                                                dtype=bool),
            font_size=FONT_SIZE_SANS_COLOUR_BARS))

    if allow_whitespace:
        this_colour_map_object, this_colour_norm_object = (
            radar_plotting.get_default_colour_scheme(
                radar_utils.LOW_LEVEL_SHEAR_NAME))

        plotting_utils.plot_colour_bar(
            axes_object_or_matrix=shear_axes_object_matrix,
            data_matrix=list_of_predictor_matrices[1],
            colour_map_object=this_colour_map_object,
            colour_norm_object=this_colour_norm_object,
            orientation_string='horizontal',
            extend_min=True,
            extend_max=True)

        if title_string is not None:
            pyplot.suptitle(title_string, fontsize=TITLE_FONT_SIZE)

    figure_objects = [refl_figure_object, shear_figure_object]
    axes_object_matrices = [refl_axes_object_matrix, shear_axes_object_matrix]
    return figure_objects, axes_object_matrices
Exemplo n.º 12
0
def _plot_3d_radar_scan(list_of_predictor_matrices,
                        model_metadata_dict,
                        allow_whitespace,
                        title_string=None):
    """Plots 3-D radar scan for one example.

    J = number of panel rows in image
    K = number of panel columns in image
    F = number of radar fields

    :param list_of_predictor_matrices: List created by
        `testing_io.read_specific_examples`, except that the first axis (example
        dimension) is removed.
    :param model_metadata_dict: Dictionary returned by
        `cnn.read_model_metadata`.
    :param allow_whitespace: See documentation at top of file.
    :param title_string: Title (may be None).

    :return: figure_objects: length-F list of figure handles (instances of
        `matplotlib.figure.Figure`).
    :return: axes_object_matrices: length-F list.  Each element is a J-by-K
        numpy array of axes handles (instances of
        `matplotlib.axes._subplots.AxesSubplot`).
    """

    training_option_dict = model_metadata_dict[cnn.TRAINING_OPTION_DICT_KEY]
    radar_field_names = training_option_dict[trainval_io.RADAR_FIELDS_KEY]
    radar_heights_m_agl = training_option_dict[trainval_io.RADAR_HEIGHTS_KEY]

    num_radar_fields = len(radar_field_names)
    num_radar_heights = len(radar_heights_m_agl)

    num_panel_rows = int(numpy.floor(numpy.sqrt(num_radar_heights)))
    num_panel_columns = int(
        numpy.ceil(float(num_radar_heights) / num_panel_rows))

    figure_objects = [None] * num_radar_fields
    axes_object_matrices = [None] * num_radar_fields
    radar_matrix = list_of_predictor_matrices[0]

    for j in range(num_radar_fields):
        this_radar_matrix = numpy.flip(radar_matrix[..., j], axis=0)

        if not allow_whitespace:
            figure_objects[j], axes_object_matrices[j] = (
                plotting_utils.create_paneled_figure(
                    num_rows=num_panel_rows,
                    num_columns=num_panel_columns,
                    horizontal_spacing=0.,
                    vertical_spacing=0.,
                    shared_x_axis=False,
                    shared_y_axis=False,
                    keep_aspect_ratio=True))

        figure_objects[j], axes_object_matrices[j] = (
            radar_plotting.plot_3d_grid_without_coords(
                field_matrix=this_radar_matrix,
                field_name=radar_field_names[j],
                grid_point_heights_metres=radar_heights_m_agl,
                ground_relative=True,
                num_panel_rows=num_panel_rows,
                figure_object=figure_objects[j],
                axes_object_matrix=axes_object_matrices[j],
                font_size=FONT_SIZE_SANS_COLOUR_BARS))

        if allow_whitespace:
            this_colour_map_object, this_colour_norm_object = (
                radar_plotting.get_default_colour_scheme(radar_field_names[j]))

            plotting_utils.plot_colour_bar(
                axes_object_or_matrix=axes_object_matrices[j],
                data_matrix=this_radar_matrix,
                colour_map_object=this_colour_map_object,
                colour_norm_object=this_colour_norm_object,
                orientation_string='horizontal',
                extend_min=True,
                extend_max=True)

            if title_string is not None:
                this_title_string = '{0:s}; {1:s}'.format(
                    title_string, radar_field_names[j])
                pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)

    return figure_objects, axes_object_matrices
Exemplo n.º 13
0
def _plot_one_example_one_time(
        storm_object_table, full_id_string, valid_time_unix_sec,
        tornado_table, top_myrorss_dir_name, radar_field_name,
        radar_height_m_asl, latitude_limits_deg, longitude_limits_deg):
    """Plots one example with surrounding context at one time.

    :param storm_object_table: pandas DataFrame, containing only storm objects
        at one time with the relevant primary ID.  Columns are documented in
        `storm_tracking_io.write_file`.
    :param full_id_string: Full ID of storm of interest.
    :param valid_time_unix_sec: Valid time.
    :param tornado_table: pandas DataFrame created by
        `linkage._read_input_tornado_reports`.
    :param top_myrorss_dir_name: See documentation at top of file.
    :param radar_field_name: Same.
    :param radar_height_m_asl: Same.
    :param latitude_limits_deg: See doc for `_get_plotting_limits`.
    :param longitude_limits_deg: Same.
    """

    min_plot_latitude_deg = latitude_limits_deg[0]
    max_plot_latitude_deg = latitude_limits_deg[1]
    min_plot_longitude_deg = longitude_limits_deg[0]
    max_plot_longitude_deg = longitude_limits_deg[1]

    radar_file_name = myrorss_and_mrms_io.find_raw_file_inexact_time(
        top_directory_name=top_myrorss_dir_name,
        desired_time_unix_sec=valid_time_unix_sec,
        spc_date_string=time_conversion.time_to_spc_date_string(
            valid_time_unix_sec),
        data_source=radar_utils.MYRORSS_SOURCE_ID,
        field_name=radar_field_name, height_m_asl=radar_height_m_asl,
        max_time_offset_sec=
        myrorss_and_mrms_io.DEFAULT_MAX_TIME_OFFSET_FOR_NON_SHEAR_SEC,
        raise_error_if_missing=True)

    print('Reading data from: "{0:s}"...'.format(radar_file_name))

    radar_metadata_dict = myrorss_and_mrms_io.read_metadata_from_raw_file(
        netcdf_file_name=radar_file_name,
        data_source=radar_utils.MYRORSS_SOURCE_ID)

    sparse_grid_table = (
        myrorss_and_mrms_io.read_data_from_sparse_grid_file(
            netcdf_file_name=radar_file_name,
            field_name_orig=radar_metadata_dict[
                myrorss_and_mrms_io.FIELD_NAME_COLUMN_ORIG],
            data_source=radar_utils.MYRORSS_SOURCE_ID,
            sentinel_values=radar_metadata_dict[
                radar_utils.SENTINEL_VALUE_COLUMN]
        )
    )

    radar_matrix, grid_point_latitudes_deg, grid_point_longitudes_deg = (
        radar_s2f.sparse_to_full_grid(
            sparse_grid_table=sparse_grid_table,
            metadata_dict=radar_metadata_dict)
    )

    radar_matrix = numpy.flip(radar_matrix, axis=0)
    grid_point_latitudes_deg = grid_point_latitudes_deg[::-1]

    axes_object, basemap_object = (
        plotting_utils.create_equidist_cylindrical_map(
            min_latitude_deg=min_plot_latitude_deg,
            max_latitude_deg=max_plot_latitude_deg,
            min_longitude_deg=min_plot_longitude_deg,
            max_longitude_deg=max_plot_longitude_deg, resolution_string='h'
        )[1:]
    )

    plotting_utils.plot_coastlines(
        basemap_object=basemap_object, axes_object=axes_object,
        line_colour=plotting_utils.DEFAULT_COUNTRY_COLOUR)

    plotting_utils.plot_countries(
        basemap_object=basemap_object, axes_object=axes_object)

    plotting_utils.plot_states_and_provinces(
        basemap_object=basemap_object, axes_object=axes_object)

    plotting_utils.plot_parallels(
        basemap_object=basemap_object, axes_object=axes_object,
        num_parallels=NUM_PARALLELS, line_width=0)

    plotting_utils.plot_meridians(
        basemap_object=basemap_object, axes_object=axes_object,
        num_meridians=NUM_MERIDIANS, line_width=0)

    radar_plotting.plot_latlng_grid(
        field_matrix=radar_matrix, field_name=radar_field_name,
        axes_object=axes_object,
        min_grid_point_latitude_deg=numpy.min(grid_point_latitudes_deg),
        min_grid_point_longitude_deg=numpy.min(grid_point_longitudes_deg),
        latitude_spacing_deg=numpy.diff(grid_point_latitudes_deg[:2])[0],
        longitude_spacing_deg=numpy.diff(grid_point_longitudes_deg[:2])[0]
    )

    colour_map_object, colour_norm_object = (
        radar_plotting.get_default_colour_scheme(radar_field_name)
    )

    plotting_utils.plot_colour_bar(
        axes_object_or_matrix=axes_object, data_matrix=radar_matrix,
        colour_map_object=colour_map_object,
        colour_norm_object=colour_norm_object, orientation_string='horizontal',
        padding=0.05, extend_min=False, extend_max=True,
        fraction_of_axis_length=0.8)

    first_list, second_list = temporal_tracking.full_to_partial_ids(
        [full_id_string]
    )
    primary_id_string = first_list[0]
    secondary_id_string = second_list[0]

    # Plot outlines of unrelated storms (with different primary IDs).
    this_storm_object_table = storm_object_table.loc[
        storm_object_table[tracking_utils.PRIMARY_ID_COLUMN] !=
        primary_id_string
    ]

    storm_plotting.plot_storm_outlines(
        storm_object_table=this_storm_object_table, axes_object=axes_object,
        basemap_object=basemap_object, line_width=AUXILIARY_STORM_WIDTH,
        line_colour='k', line_style='dashed')

    # Plot outlines of related storms (with the same primary ID).
    this_storm_object_table = storm_object_table.loc[
        (storm_object_table[tracking_utils.PRIMARY_ID_COLUMN] ==
         primary_id_string) &
        (storm_object_table[tracking_utils.SECONDARY_ID_COLUMN] !=
         secondary_id_string)
    ]

    this_num_storm_objects = len(this_storm_object_table.index)

    if this_num_storm_objects > 0:
        storm_plotting.plot_storm_outlines(
            storm_object_table=this_storm_object_table, axes_object=axes_object,
            basemap_object=basemap_object, line_width=AUXILIARY_STORM_WIDTH,
            line_colour='k', line_style='solid'
        )

        for j in range(len(this_storm_object_table)):
            axes_object.text(
                this_storm_object_table[
                    tracking_utils.CENTROID_LONGITUDE_COLUMN
                ].values[j],
                this_storm_object_table[
                    tracking_utils.CENTROID_LATITUDE_COLUMN
                ].values[j],
                'P',
                fontsize=MAIN_FONT_SIZE, color=FONT_COLOUR, fontweight='bold',
                horizontalalignment='center', verticalalignment='center'
            )

    # Plot outline of storm of interest (same secondary ID).
    this_storm_object_table = storm_object_table.loc[
        storm_object_table[tracking_utils.SECONDARY_ID_COLUMN] ==
        secondary_id_string
    ]

    storm_plotting.plot_storm_outlines(
        storm_object_table=this_storm_object_table, axes_object=axes_object,
        basemap_object=basemap_object, line_width=MAIN_STORM_WIDTH,
        line_colour='k', line_style='solid')

    this_num_storm_objects = len(this_storm_object_table.index)

    plot_forecast = (
        this_num_storm_objects > 0 and
        FORECAST_PROBABILITY_COLUMN in list(this_storm_object_table)
    )

    if plot_forecast:
        label_string = 'Prob = {0:.3f}\nat {1:s}'.format(
            this_storm_object_table[FORECAST_PROBABILITY_COLUMN].values[0],
            time_conversion.unix_sec_to_string(
                valid_time_unix_sec, TORNADO_TIME_FORMAT)
        )

        axes_object.set_title(
            label_string.replace('\n', ' '), fontsize=TITLE_FONT_SIZE
        )

    tornado_id_strings = tornado_table[tornado_io.TORNADO_ID_COLUMN].values

    for this_tornado_id_string in numpy.unique(tornado_id_strings):
        these_rows = numpy.where(
            tornado_id_strings == this_tornado_id_string
        )[0]

        this_tornado_table = tornado_table.iloc[these_rows].sort_values(
            linkage.EVENT_TIME_COLUMN, axis=0, ascending=True, inplace=False
        )
        _plot_one_tornado(
            tornado_table=this_tornado_table, axes_object=axes_object
        )
def plot_examples(list_of_predictor_matrices,
                  storm_ids,
                  storm_times_unix_sec,
                  model_metadata_dict,
                  output_dir_name,
                  storm_activations=None):
    """Plots one or more learning examples.

    E = number of examples (storm objects)

    :param list_of_predictor_matrices: List created by
        `testing_io.read_specific_examples`.  Contains data to be plotted.
    :param storm_ids: length-E list of storm IDs.
    :param storm_times_unix_sec: length-E numpy array of storm times.
    :param model_metadata_dict: See doc for `cnn.read_model_metadata`.
    :param output_dir_name: Name of output directory (figures will be saved
        here).
    :param storm_activations: length-E numpy array of storm activations (may be
        None).  Will be included in title of each figure.
    """

    training_option_dict = model_metadata_dict[cnn.TRAINING_OPTION_DICT_KEY]
    sounding_field_names = training_option_dict[
        trainval_io.SOUNDING_FIELDS_KEY]
    plot_soundings = sounding_field_names is not None

    if plot_soundings:
        list_of_metpy_dictionaries = dl_utils.soundings_to_metpy_dictionaries(
            sounding_matrix=list_of_predictor_matrices[-1],
            field_names=sounding_field_names)
    else:
        list_of_metpy_dictionaries = None

    num_radar_dimensions = len(list_of_predictor_matrices[0].shape) - 2
    list_of_layer_operation_dicts = model_metadata_dict[
        cnn.LAYER_OPERATIONS_KEY]

    if num_radar_dimensions == 2:
        if list_of_layer_operation_dicts is None:
            field_name_by_panel = training_option_dict[
                trainval_io.RADAR_FIELDS_KEY]

            panel_names = (
                radar_plotting.radar_fields_and_heights_to_panel_names(
                    field_names=field_name_by_panel,
                    heights_m_agl=training_option_dict[
                        trainval_io.RADAR_HEIGHTS_KEY]))

            plot_colour_bar_by_panel = numpy.full(len(panel_names),
                                                  True,
                                                  dtype=bool)

        else:
            field_name_by_panel, panel_names = (
                radar_plotting.layer_ops_to_field_and_panel_names(
                    list_of_layer_operation_dicts))

            plot_colour_bar_by_panel = numpy.full(len(panel_names),
                                                  False,
                                                  dtype=bool)
            plot_colour_bar_by_panel[2::3] = True
    else:
        field_name_by_panel = None
        panel_names = None
        plot_colour_bar_by_panel = None

    az_shear_field_names = training_option_dict[trainval_io.RADAR_FIELDS_KEY]
    num_az_shear_fields = len(az_shear_field_names)

    num_storms = len(storm_ids)
    myrorss_2d3d = len(list_of_predictor_matrices) == 3

    for i in range(num_storms):
        this_time_string = time_conversion.unix_sec_to_string(
            storm_times_unix_sec[i], TIME_FORMAT)
        this_base_title_string = 'Storm "{0:s}" at {1:s}'.format(
            storm_ids[i], this_time_string)

        if storm_activations is not None:
            this_base_title_string += ' (activation = {0:.3f})'.format(
                storm_activations[i])

        this_base_file_name = '{0:s}/storm={1:s}_{2:s}'.format(
            output_dir_name, storm_ids[i].replace('_', '-'), this_time_string)

        if plot_soundings:
            sounding_plotting.plot_sounding(
                sounding_dict_for_metpy=list_of_metpy_dictionaries[i],
                title_string=this_base_title_string)

            this_file_name = '{0:s}_sounding.jpg'.format(this_base_file_name)
            print 'Saving figure to: "{0:s}"...'.format(this_file_name)
            pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
            pyplot.close()

        if myrorss_2d3d:
            this_reflectivity_matrix_dbz = numpy.flip(
                list_of_predictor_matrices[0][i, ..., 0], axis=0)

            this_num_heights = this_reflectivity_matrix_dbz.shape[-1]
            this_num_panel_rows = int(numpy.floor(
                numpy.sqrt(this_num_heights)))

            _, these_axes_objects = radar_plotting.plot_3d_grid_without_coords(
                field_matrix=this_reflectivity_matrix_dbz,
                field_name=radar_utils.REFL_NAME,
                grid_point_heights_metres=training_option_dict[
                    trainval_io.RADAR_HEIGHTS_KEY],
                ground_relative=True,
                num_panel_rows=this_num_panel_rows,
                font_size=FONT_SIZE_SANS_COLOUR_BARS)

            this_colour_map_object, this_colour_norm_object = (
                radar_plotting.get_default_colour_scheme(
                    radar_utils.REFL_NAME))

            plotting_utils.add_colour_bar(
                axes_object_or_list=these_axes_objects,
                values_to_colour=this_reflectivity_matrix_dbz,
                colour_map=this_colour_map_object,
                colour_norm_object=this_colour_norm_object,
                orientation='horizontal',
                extend_min=True,
                extend_max=True)

            this_title_string = '{0:s}; {1:s}'.format(this_base_title_string,
                                                      radar_utils.REFL_NAME)
            this_file_name = '{0:s}_reflectivity.jpg'.format(
                this_base_file_name)

            pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)
            print 'Saving figure to: "{0:s}"...'.format(this_file_name)
            pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
            pyplot.close()

            this_az_shear_matrix_s01 = numpy.flip(
                list_of_predictor_matrices[1][i, ..., 0], axis=0)

            _, these_axes_objects = (
                radar_plotting.plot_many_2d_grids_without_coords(
                    field_matrix=this_az_shear_matrix_s01,
                    field_name_by_panel=az_shear_field_names,
                    panel_names=az_shear_field_names,
                    num_panel_rows=1,
                    plot_colour_bar_by_panel=numpy.full(num_az_shear_fields,
                                                        False,
                                                        dtype=bool),
                    font_size=FONT_SIZE_SANS_COLOUR_BARS))

            this_colour_map_object, this_colour_norm_object = (
                radar_plotting.get_default_colour_scheme(
                    radar_utils.LOW_LEVEL_SHEAR_NAME))

            plotting_utils.add_colour_bar(
                axes_object_or_list=these_axes_objects,
                values_to_colour=this_az_shear_matrix_s01,
                colour_map=this_colour_map_object,
                colour_norm_object=this_colour_norm_object,
                orientation='horizontal',
                extend_min=True,
                extend_max=True)

            this_file_name = '{0:s}_shear.jpg'.format(this_base_file_name)
            pyplot.suptitle(this_base_title_string, fontsize=TITLE_FONT_SIZE)
            print 'Saving figure to: "{0:s}"...'.format(this_file_name)
            pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
            pyplot.close()

            continue

        this_radar_matrix = list_of_predictor_matrices[0]

        if num_radar_dimensions == 2:
            this_num_channels = this_radar_matrix.shape[-1]
            this_num_panel_rows = int(
                numpy.floor(numpy.sqrt(this_num_channels)))

            radar_plotting.plot_many_2d_grids_without_coords(
                field_matrix=numpy.flip(this_radar_matrix[i, ...], axis=0),
                field_name_by_panel=field_name_by_panel,
                panel_names=panel_names,
                num_panel_rows=this_num_panel_rows,
                plot_colour_bar_by_panel=plot_colour_bar_by_panel,
                font_size=FONT_SIZE_WITH_COLOUR_BARS,
                row_major=False)

            this_title_string = this_base_title_string + ''
            pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)

            this_file_name = '{0:s}.jpg'.format(this_base_file_name)
            print 'Saving figure to: "{0:s}"...'.format(this_file_name)
            pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
            pyplot.close()

            continue

        radar_field_names = training_option_dict[trainval_io.RADAR_FIELDS_KEY]
        radar_heights_m_agl = training_option_dict[
            trainval_io.RADAR_HEIGHTS_KEY]

        for j in range(len(radar_field_names)):
            this_num_heights = this_radar_matrix.shape[-2]
            this_num_panel_rows = int(numpy.floor(
                numpy.sqrt(this_num_heights)))

            _, these_axes_objects = radar_plotting.plot_3d_grid_without_coords(
                field_matrix=numpy.flip(this_radar_matrix[i, ..., j], axis=0),
                field_name=radar_field_names[j],
                grid_point_heights_metres=radar_heights_m_agl,
                ground_relative=True,
                num_panel_rows=this_num_panel_rows,
                font_size=FONT_SIZE_SANS_COLOUR_BARS)

            this_colour_map_object, this_colour_norm_object = (
                radar_plotting.get_default_colour_scheme(radar_field_names[j]))

            plotting_utils.add_colour_bar(
                axes_object_or_list=these_axes_objects,
                values_to_colour=this_radar_matrix[i, ..., j],
                colour_map=this_colour_map_object,
                colour_norm_object=this_colour_norm_object,
                orientation='horizontal',
                extend_min=True,
                extend_max=True)

            this_title_string = '{0:s}; {1:s}'.format(this_base_title_string,
                                                      radar_field_names[j])
            this_file_name = '{0:s}_{1:s}.jpg'.format(
                this_base_file_name, radar_field_names[j].replace('_', '-'))

            pyplot.suptitle(this_title_string, fontsize=TITLE_FONT_SIZE)
            print 'Saving figure to: "{0:s}"...'.format(this_file_name)
            pyplot.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI)
            pyplot.close()
def _plot_echo_tops(echo_top_matrix_km_asl, latitudes_deg, longitudes_deg,
                    plot_colour_bar, convective_flag_matrix=None):
    """Plots grid of 40-dBZ echo tops.

    M = number of rows in grid
    N = number of columns in grid

    :param echo_top_matrix_km_asl: M-by-N numpy array of echo tops (km above sea
        level).
    :param latitudes_deg: length-M numpy array of latitudes (deg N).
    :param longitudes_deg: length-N numpy array of longitudes (deg E).
    :param plot_colour_bar: Boolean flag.
    :param convective_flag_matrix: M-by-N numpy array of Boolean flags,
        indicating which grid cells are convective.  If
        `convective_flag_matrix is None`, all grid cells will be plotted.  If
        `convective_flag_matrix is not None`, only convective grid cells will be
        plotted.
    :return: figure_object: Figure handle (instance of
        `matplotlib.figure.Figure`).
    :return: axes_object: Axes handle (instance of
        `matplotlib.axes._subplots.AxesSubplot`).
    :return: basemap_object: Basemap handle (instance of
        `mpl_toolkits.basemap.Basemap`).
    """

    figure_object, axes_object, basemap_object = (
        plotting_utils.create_equidist_cylindrical_map(
            min_latitude_deg=numpy.min(latitudes_deg),
            max_latitude_deg=numpy.max(latitudes_deg),
            min_longitude_deg=numpy.min(longitudes_deg),
            max_longitude_deg=numpy.max(longitudes_deg), resolution_string='h'
        )
    )

    # plotting_utils.plot_coastlines(
    #     basemap_object=basemap_object, axes_object=axes_object,
    #     line_colour=plotting_utils.DEFAULT_COUNTRY_COLOUR
    # )
    plotting_utils.plot_countries(
        basemap_object=basemap_object, axes_object=axes_object
    )
    plotting_utils.plot_states_and_provinces(
        basemap_object=basemap_object, axes_object=axes_object
    )
    plotting_utils.plot_parallels(
        basemap_object=basemap_object, axes_object=axes_object,
        num_parallels=NUM_PARALLELS, line_width=0
    )
    plotting_utils.plot_meridians(
        basemap_object=basemap_object, axes_object=axes_object,
        num_meridians=NUM_MERIDIANS, line_width=0
    )

    matrix_to_plot = echo_top_matrix_km_asl + 0.
    if convective_flag_matrix is not None:
        matrix_to_plot[convective_flag_matrix == False] = numpy.nan

    radar_plotting.plot_latlng_grid(
        field_matrix=matrix_to_plot, field_name=radar_utils.ECHO_TOP_40DBZ_NAME,
        axes_object=axes_object,
        min_grid_point_latitude_deg=numpy.min(latitudes_deg),
        min_grid_point_longitude_deg=numpy.min(longitudes_deg),
        latitude_spacing_deg=numpy.diff(latitudes_deg[:2])[0],
        longitude_spacing_deg=numpy.diff(longitudes_deg[:2])[0]
    )

    if not plot_colour_bar:
        return figure_object, axes_object, basemap_object

    colour_map_object, colour_norm_object = (
        radar_plotting.get_default_colour_scheme(
            radar_utils.ECHO_TOP_40DBZ_NAME)
    )

    colour_bar_object = plotting_utils.plot_colour_bar(
        axes_object_or_matrix=axes_object, data_matrix=matrix_to_plot,
        colour_map_object=colour_map_object,
        colour_norm_object=colour_norm_object, orientation_string='horizontal',
        extend_min=False, extend_max=True, fraction_of_axis_length=1.
    )

    colour_bar_object.set_label('40-dBZ echo top (kft ASL)')

    return figure_object, axes_object, basemap_object