Пример #1
0
    def test_latlng_field_grid_points_to_edges(self):
        """Ensures correct output from latlng_field_grid_points_to_edges."""

        (this_field_matrix_at_edges, these_edge_latitudes_deg,
         these_edge_longitudes_deg) = grids.latlng_field_grid_points_to_edges(
             field_matrix=FIELD_MATRIX_AT_GRID_POINTS,
             min_latitude_deg=MIN_LATITUDE_DEG,
             min_longitude_deg=MIN_LONGITUDE_DEG,
             lat_spacing_deg=LAT_SPACING_DEG,
             lng_spacing_deg=LNG_SPACING_DEG)

        self.assertTrue(
            numpy.allclose(these_edge_latitudes_deg,
                           EXPECTED_GRID_CELL_EDGE_LATITUDES_DEG,
                           atol=TOLERANCE))
        self.assertTrue(
            numpy.allclose(these_edge_longitudes_deg,
                           EXPECTED_GRID_CELL_EDGE_LONGITUDES_DEG,
                           atol=TOLERANCE))
        self.assertTrue(
            numpy.allclose(this_field_matrix_at_edges,
                           EXPECTED_FIELD_MATRIX_AT_EDGES,
                           equal_nan=True,
                           atol=TOLERANCE))
Пример #2
0
def plot_latlng_grid(field_matrix,
                     field_name,
                     axes_object,
                     min_grid_point_latitude_deg,
                     min_grid_point_longitude_deg,
                     latitude_spacing_deg,
                     longitude_spacing_deg,
                     colour_map_object=None,
                     colour_norm_object=None,
                     refl_opacity=DEFAULT_OPACITY):
    """Plots lat-long grid as colour map.

    M = number of rows (unique grid-point latitudes)
    N = number of columns (unique grid-point longitudes)

    Because this method plots a lat-long grid (rather than an x-y grid), if you
    have used Basemap to plot borders or anything else, the only acceptable
    projection is cylindrical equidistant (in which x = longitude and
    y = latitude, so no coordinate conversion is necessary).

    To use the default colour scheme for the given radar field, leave
    `colour_map_object` and `colour_norm_object` empty.

    :param field_matrix: M-by-N numpy array with values of radar field.
    :param field_name: Name of radar field (must be accepted by
        `radar_utils.check_field_name`).
    :param axes_object: Instance of `matplotlib.axes._subplots.AxesSubplot`.
    :param min_grid_point_latitude_deg: Minimum latitude (deg N) over all grid
        points.  This should be the latitude in the first row of `field_matrix`
        -- i.e., at `field_matrix[0, :]`.
    :param min_grid_point_longitude_deg: Minimum longitude (deg E) over all grid
        points.  This should be the longitude in the first column of
        `field_matrix` -- i.e., at `field_matrix[:, 0]`.
    :param latitude_spacing_deg: Spacing (deg N) between grid points in adjacent
        rows.
    :param longitude_spacing_deg: Spacing (deg E) between grid points in
        adjacent columns.
    :param colour_map_object: Instance of `matplotlib.pyplot.cm`.  If this is
        None, the default colour scheme for `field_name` will be used.
    :param colour_norm_object: Instance of `matplotlib.colors.BoundaryNorm`.  If
        this is None, the default colour scheme for `field_name` will be used.
    :param refl_opacity: Opacity for reflectivity colour scheme.  Used only if
        `colour_map_object is None and colour_norm_object is None`.
    """

    field_matrix = _field_to_plotting_units(field_matrix=field_matrix,
                                            field_name=field_name)

    (field_matrix_at_edges, grid_cell_edge_latitudes_deg,
     grid_cell_edge_longitudes_deg) = grids.latlng_field_grid_points_to_edges(
         field_matrix=field_matrix,
         min_latitude_deg=min_grid_point_latitude_deg,
         min_longitude_deg=min_grid_point_longitude_deg,
         lat_spacing_deg=latitude_spacing_deg,
         lng_spacing_deg=longitude_spacing_deg)

    field_matrix_at_edges = numpy.ma.masked_where(
        numpy.isnan(field_matrix_at_edges), field_matrix_at_edges)

    use_default_colour_scheme = (colour_map_object is None
                                 or colour_norm_object is None)

    if use_default_colour_scheme:
        opacity = (refl_opacity if field_name in radar_utils.REFLECTIVITY_NAMES
                   else DEFAULT_OPACITY)

        colour_map_object, colour_norm_object = get_default_colour_scheme(
            field_name=field_name, opacity=opacity)
    else:
        if hasattr(colour_norm_object, 'boundaries'):
            colour_norm_object.boundaries = _field_to_plotting_units(
                field_matrix=colour_norm_object.boundaries,
                field_name=field_name)
        else:
            colour_norm_object.vmin = _field_to_plotting_units(
                field_matrix=colour_norm_object.vmin, field_name=field_name)
            colour_norm_object.vmax = _field_to_plotting_units(
                field_matrix=colour_norm_object.vmax, field_name=field_name)

    if hasattr(colour_norm_object, 'boundaries'):
        min_colour_value = colour_norm_object.boundaries[0]
        max_colour_value = colour_norm_object.boundaries[-1]
    else:
        min_colour_value = colour_norm_object.vmin
        max_colour_value = colour_norm_object.vmax

    pyplot.pcolormesh(grid_cell_edge_longitudes_deg,
                      grid_cell_edge_latitudes_deg,
                      field_matrix_at_edges,
                      cmap=colour_map_object,
                      norm=colour_norm_object,
                      vmin=min_colour_value,
                      vmax=max_colour_value,
                      shading='flat',
                      edgecolors='None',
                      axes=axes_object,
                      zorder=-1e11)
Пример #3
0
def plot_latlng_grid(axes_object=None,
                     field_name=None,
                     field_matrix=None,
                     min_latitude_deg=None,
                     min_longitude_deg=None,
                     lat_spacing_deg=None,
                     lng_spacing_deg=None,
                     colour_map=None,
                     colour_minimum=None,
                     colour_maximum=None):
    """Plots lat-long grid of a single radar field.

    Because this method plots a lat-long grid, rather than an x-y grid, the
    projection used for the basemap must be cylindrical equidistant (which is
    the same as a lat-long projection).

    This is the most convenient way to plot radar data, since both MYRORSS and
    MRMS data are on a lat-long grid.

    M = number of rows (unique grid-point latitudes)
    N = number of columns (unique grid-point longitudes)

    :param axes_object: Instance of `matplotlib.axes._subplots.AxesSubplot`.
    :param field_name: Name of radar field.
    :param field_matrix: M-by-N numpy array with values of radar field.  This
        should be in standard GewitterGefahr units (will be converted to
        plotting units by convert_to_plotting_units).  Latitude should increase
        while traveling down a column, and longitude should increase while
        traveling right across a row.
    :param min_latitude_deg: Minimum latitude over all grid points (deg N).
    :param min_longitude_deg: Minimum longitude over all grid points (deg E).
    :param lat_spacing_deg: Meridional spacing between adjacent grid points.
    :param lng_spacing_deg: Zonal spacing between adjacent grid points.
    :param colour_map: Instance of `matplotlib.pyplot.cm`.
    :param colour_minimum: Minimum value for colour map.
    :param colour_maximum: Maximum value for colour map.
    """

    radar_io.check_field_name(field_name)

    (field_matrix_at_edges, grid_cell_edge_latitudes_deg,
     grid_cell_edge_longitudes_deg) = grids.latlng_field_grid_points_to_edges(
         field_matrix=field_matrix,
         min_latitude_deg=min_latitude_deg,
         min_longitude_deg=min_longitude_deg,
         lat_spacing_deg=lat_spacing_deg,
         lng_spacing_deg=lng_spacing_deg)

    field_matrix_at_edges = _convert_to_plotting_units(field_matrix_at_edges,
                                                       field_name)
    field_matrix_at_edges = numpy.ma.masked_where(
        numpy.isnan(field_matrix_at_edges), field_matrix_at_edges)

    if colour_map is None:
        colour_map, colour_norm_object, _ = _get_default_colour_map(field_name)
        colour_minimum = colour_norm_object.boundaries[0]
        colour_maximum = colour_norm_object.boundaries[-1]
    else:
        error_checking.assert_is_greater(colour_maximum, colour_minimum)
        colour_norm_object = None

    pyplot.pcolormesh(grid_cell_edge_longitudes_deg,
                      grid_cell_edge_latitudes_deg,
                      field_matrix_at_edges,
                      cmap=colour_map,
                      norm=colour_norm_object,
                      vmin=colour_minimum,
                      vmax=colour_maximum,
                      shading='flat',
                      edgecolors='None',
                      axes=axes_object)
def plot_latlng_grid(axes_object,
                     probability_matrix,
                     min_latitude_deg,
                     min_longitude_deg,
                     latitude_spacing_deg,
                     longitude_spacing_deg,
                     colour_map=None,
                     colour_minimum=None,
                     colour_maximum=None):
    """Plots lat-long grid of probabilities.

    Because this method plots a lat-long grid, rather than an x-y grid, the
    projection used for the basemap must be cylindrical equidistant (which is
    the same as a lat-long projection).

    M = number of rows (unique grid-point latitudes)
    N = number of columns (unique grid-point longitudes)

    All probabilities (in `probability_matrix`, `colour_minimum`, and
    `colour_maximum`) should be dimensionless, ranging from 0...1.

    :param axes_object: Instance of `matplotlib.axes._subplots.AxesSubplot`.
    :param probability_matrix: M-by-N numpy array of probabilities.  Latitude
        should increase while traveling down each column, and longitude should
        increase while traveling to the right along each row.
    :param min_latitude_deg: Minimum latitude over all grid points (deg N).
    :param min_longitude_deg: Minimum longitude over all grid points (deg E).
    :param latitude_spacing_deg: Spacing between meridionally adjacent grid
        points (i.e., between adjacent rows).
    :param longitude_spacing_deg: Spacing between zonally adjacent grid points
        (i.e., between adjacent columns).
    :param colour_map: Instance of `matplotlib.pyplot.cm`.  If None, this method
        will use _get_default_colour_map.
    :param colour_minimum: Minimum value for colour map.
    :param colour_maximum: Maximum value for colour map.
    """

    (probability_matrix_at_edges, grid_cell_edge_latitudes_deg,
     grid_cell_edge_longitudes_deg) = grids.latlng_field_grid_points_to_edges(
         field_matrix=probability_matrix,
         min_latitude_deg=min_latitude_deg,
         min_longitude_deg=min_longitude_deg,
         lat_spacing_deg=latitude_spacing_deg,
         lng_spacing_deg=longitude_spacing_deg)

    probability_matrix_at_edges = numpy.ma.masked_where(
        numpy.isnan(probability_matrix_at_edges), probability_matrix_at_edges)

    if colour_map is None:
        colour_map, colour_norm_object, _ = _get_default_colour_map()
        colour_minimum = colour_norm_object.boundaries[0]
        colour_maximum = colour_norm_object.boundaries[-1]
    else:
        error_checking.assert_is_greater(colour_maximum, colour_minimum)
        colour_norm_object = None

    pyplot.pcolormesh(grid_cell_edge_longitudes_deg,
                      grid_cell_edge_latitudes_deg,
                      probability_matrix_at_edges,
                      cmap=colour_map,
                      norm=colour_norm_object,
                      vmin=colour_minimum,
                      vmax=colour_maximum,
                      shading='flat',
                      edgecolors='None',
                      axes=axes_object)
Пример #5
0
def _plot_score_one_field(latitude_matrix_deg,
                          longitude_matrix_deg,
                          score_matrix,
                          colour_map_object,
                          min_colour_value,
                          max_colour_value,
                          taper_cbar_top,
                          taper_cbar_bottom,
                          log_scale=False):
    """Plots one score for one field.

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

    :param latitude_matrix_deg: M-by-N numpy array of latitudes (deg N).
    :param longitude_matrix_deg: M-by-N numpy array of longitudes (deg E).
    :param score_matrix: M-by-N numpy array of score values.
    :param colour_map_object: Colour scheme (instance of `matplotlib.pyplot.cm`).
    :param min_colour_value: Minimum value in colour bar.
    :param max_colour_value: Max value in colour bar.
    :param taper_cbar_top: Boolean flag.  If True, will taper bottom of colour
        bar, implying that lower values are possible.
    :param taper_cbar_bottom: Same but for top of colour bar.
    :param log_scale: Boolean flag.  If True, will make colour bar logarithmic.
    :return: figure_object: Figure handle (instance of
        `matplotlib.figure.Figure`).
    :return: axes_object: Axes handle (instance of
        `matplotlib.axes._subplots.AxesSubplot`).
    """

    (figure_object, axes_object,
     basemap_object) = plotting_utils.create_equidist_cylindrical_map(
         min_latitude_deg=latitude_matrix_deg[0, 0],
         max_latitude_deg=latitude_matrix_deg[-1, -1],
         min_longitude_deg=longitude_matrix_deg[0, 0],
         max_longitude_deg=longitude_matrix_deg[-1, -1],
         resolution_string=RESOLUTION_STRING)

    latitude_spacing_deg = latitude_matrix_deg[1, 0] - latitude_matrix_deg[0,
                                                                           0]
    longitude_spacing_deg = (longitude_matrix_deg[0, 1] -
                             longitude_matrix_deg[0, 0])

    print(numpy.sum(numpy.invert(numpy.isnan(score_matrix))))

    (score_matrix_at_edges, grid_edge_latitudes_deg,
     grid_edge_longitudes_deg) = grids.latlng_field_grid_points_to_edges(
         field_matrix=score_matrix,
         min_latitude_deg=latitude_matrix_deg[0, 0],
         min_longitude_deg=longitude_matrix_deg[0, 0],
         lat_spacing_deg=latitude_spacing_deg,
         lng_spacing_deg=longitude_spacing_deg)

    score_matrix_at_edges = numpy.ma.masked_where(
        numpy.isnan(score_matrix_at_edges), score_matrix_at_edges)

    plotting_utils.plot_coastlines(basemap_object=basemap_object,
                                   axes_object=axes_object,
                                   line_colour=BORDER_COLOUR,
                                   line_width=BORDER_WIDTH)
    plotting_utils.plot_countries(basemap_object=basemap_object,
                                  axes_object=axes_object,
                                  line_colour=BORDER_COLOUR,
                                  line_width=BORDER_WIDTH)
    # 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,
                                  line_width=0)
    plotting_utils.plot_meridians(basemap_object=basemap_object,
                                  axes_object=axes_object,
                                  num_meridians=NUM_MERIDIANS,
                                  line_width=0)

    pyplot.pcolormesh(grid_edge_longitudes_deg,
                      grid_edge_latitudes_deg,
                      score_matrix_at_edges,
                      cmap=colour_map_object,
                      vmin=min_colour_value,
                      vmax=max_colour_value,
                      shading='flat',
                      edgecolors='None',
                      axes=axes_object,
                      zorder=-1e12)

    colour_bar_object = plotting_utils.plot_linear_colour_bar(
        axes_object_or_matrix=axes_object,
        data_matrix=score_matrix,
        colour_map_object=colour_map_object,
        min_value=min_colour_value,
        max_value=max_colour_value,
        orientation_string='horizontal',
        extend_min=taper_cbar_bottom,
        extend_max=taper_cbar_top,
        padding=0.05,
        font_size=COLOUR_BAR_FONT_SIZE)

    tick_values = colour_bar_object.get_ticks()

    if log_scale:
        tick_strings = [
            '{0:d}'.format(int(numpy.round(10**v))) for v in tick_values
        ]
    elif numpy.nanmax(numpy.absolute(score_matrix)) >= 6:
        tick_strings = [
            '{0:d}'.format(int(numpy.round(v))) for v in tick_values
        ]
    else:
        tick_strings = ['{0:.2f}'.format(v) for v in tick_values]

    colour_bar_object.set_ticks(tick_values)
    colour_bar_object.set_ticklabels(tick_strings)

    return figure_object, axes_object