<commit_before>"""Plots CNN forecasts on the RAP grid."""

import argparse
import numpy
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as pyplot
from gewittergefahr.gg_utils import time_conversion
from gewittergefahr.gg_utils import projections
from gewittergefahr.gg_utils import nwp_model_utils
from gewittergefahr.gg_utils import file_system_utils
from gewittergefahr.deep_learning import prediction_io
from gewittergefahr.plotting import plotting_utils
from gewittergefahr.plotting import probability_plotting
from gewittergefahr.plotting import imagemagick_utils

FILE_NAME_TIME_FORMAT = '%Y-%m-%d-%H%M%S'

TEST_LATITUDES_DEG = numpy.array([25.])
TEST_LONGITUDES_DEG = numpy.array([265.])
PYPROJ_OBJECT = nwp_model_utils.init_projection(nwp_model_utils.RAP_MODEL_NAME)

TITLE_FONT_SIZE = 20
BORDER_COLOUR = numpy.full(3, 0.)
GRID_LINE_COLOUR = numpy.full(3, 1.)
PARALLEL_SPACING_DEG = 5.
MERIDIAN_SPACING_DEG = 15.
FIGURE_RESOLUTION_DPI = 300

INPUT_FILE_ARG_NAME = 'input_prediction_file_name'
OUTPUT_DIR_ARG_NAME = 'output_dir_name'

INPUT_FILE_HELP_STRING = (
    'Path to input file (will be read by '
    '`prediction_io.read_gridded_predictions`).')

OUTPUT_DIR_HELP_STRING = (
    'Name of output directory.  Figures will be saved here.')

INPUT_ARG_PARSER = argparse.ArgumentParser()
INPUT_ARG_PARSER.add_argument(
    '--' + INPUT_FILE_ARG_NAME, type=str, required=True,
    help=INPUT_FILE_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + OUTPUT_DIR_ARG_NAME, type=str, required=True,
    help=OUTPUT_DIR_HELP_STRING)


def _get_projection_offsets(
        basemap_object, pyproj_object, test_latitudes_deg, test_longitudes_deg):
    """Finds offsets between basemap and pyproj projections.

    P = number of points used to find offsets

    :param basemap_object: Instance of `mpl_toolkits.basemap.Basemap`.
    :param pyproj_object: Instance of `pyproj.Proj`.  The two objects should
        encode the same projection, just with different false easting/northing.
    :param test_latitudes_deg: length-P numpy array of latitudes (deg N).
    :param test_longitudes_deg: length-P numpy array of longitudes (deg E).
    :return: x_offset_metres: x-offset (basemap minus pyproj).
    :return: y_offset_metres: y-offset (basemap minus pyproj).
    """

    pyproj_x_coords_metres, pyproj_y_coords_metres = (
        projections.project_latlng_to_xy(
            latitudes_deg=test_latitudes_deg,
            longitudes_deg=test_longitudes_deg, projection_object=pyproj_object)
    )

    basemap_x_coords_metres, basemap_y_coords_metres = basemap_object(
        test_longitudes_deg, test_latitudes_deg)

    x_offset_metres = numpy.mean(
        basemap_x_coords_metres - pyproj_x_coords_metres
    )
    y_offset_metres = numpy.mean(
        basemap_y_coords_metres - pyproj_y_coords_metres
    )

    return x_offset_metres, y_offset_metres


def _plot_forecast_one_time(gridded_forecast_dict, time_index, output_dir_name):
    """Plots gridded forecast at one time.

    :param gridded_forecast_dict: Dictionary returned by
        `prediction_io.read_gridded_predictions`.
    :param time_index: Will plot the [i]th gridded forecast, where
        i = `time_index`.
    :param output_dir_name: Name of output directory.  Figure will be saved
        here.
    """

    axes_object, basemap_object = plotting_utils.init_map_with_nwp_projection(
        model_name=nwp_model_utils.RAP_MODEL_NAME,
        grid_name=nwp_model_utils.NAME_OF_130GRID, xy_limit_dict=None
    )[1:]

    x_offset_metres, y_offset_metres = _get_projection_offsets(<commit_msg>Playing with plotting area in plot_gridded_cnn_forecasts.py.<commit_after>"""Plots CNN forecasts on the RAP grid."""

import argparse
import numpy
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as pyplot
from gewittergefahr.gg_utils import time_conversion
from gewittergefahr.gg_utils import projections
from gewittergefahr.gg_utils import nwp_model_utils
from gewittergefahr.gg_utils import file_system_utils
from gewittergefahr.deep_learning import prediction_io
from gewittergefahr.plotting import plotting_utils
from gewittergefahr.plotting import probability_plotting
from gewittergefahr.plotting import imagemagick_utils

FILE_NAME_TIME_FORMAT = '%Y-%m-%d-%H%M%S'

TEST_LATITUDES_DEG = numpy.array([25.])
TEST_LONGITUDES_DEG = numpy.array([265.])
PYPROJ_OBJECT = nwp_model_utils.init_projection(nwp_model_utils.RAP_MODEL_NAME)

TITLE_FONT_SIZE = 20
BORDER_COLOUR = numpy.full(3, 0.)
GRID_LINE_COLOUR = numpy.full(3, 1.)
PARALLEL_SPACING_DEG = 5.
MERIDIAN_SPACING_DEG = 15.
FIGURE_RESOLUTION_DPI = 300

INPUT_FILE_ARG_NAME = 'input_prediction_file_name'
OUTPUT_DIR_ARG_NAME = 'output_dir_name'

INPUT_FILE_HELP_STRING = (
    'Path to input file (will be read by '
    '`prediction_io.read_gridded_predictions`).')

OUTPUT_DIR_HELP_STRING = (
    'Name of output directory.  Figures will be saved here.')

INPUT_ARG_PARSER = argparse.ArgumentParser()
INPUT_ARG_PARSER.add_argument(
    '--' + INPUT_FILE_ARG_NAME, type=str, required=True,
    help=INPUT_FILE_HELP_STRING)

INPUT_ARG_PARSER.add_argument(
    '--' + OUTPUT_DIR_ARG_NAME, type=str, required=True,
    help=OUTPUT_DIR_HELP_STRING)


def _get_projection_offsets(
        basemap_object, pyproj_object, test_latitudes_deg, test_longitudes_deg):
    """Finds offsets between basemap and pyproj projections.

    P = number of points used to find offsets

    :param basemap_object: Instance of `mpl_toolkits.basemap.Basemap`.
    :param pyproj_object: Instance of `pyproj.Proj`.  The two objects should
        encode the same projection, just with different false easting/northing.
    :param test_latitudes_deg: length-P numpy array of latitudes (deg N).
    :param test_longitudes_deg: length-P numpy array of longitudes (deg E).
    :return: x_offset_metres: x-offset (basemap minus pyproj).
    :return: y_offset_metres: y-offset (basemap minus pyproj).
    """

    pyproj_x_coords_metres, pyproj_y_coords_metres = (
        projections.project_latlng_to_xy(
            latitudes_deg=test_latitudes_deg,
            longitudes_deg=test_longitudes_deg, projection_object=pyproj_object)
    )

    basemap_x_coords_metres, basemap_y_coords_metres = basemap_object(
        test_longitudes_deg, test_latitudes_deg)

    x_offset_metres = numpy.mean(
        basemap_x_coords_metres - pyproj_x_coords_metres
    )
    y_offset_metres = numpy.mean(
        basemap_y_coords_metres - pyproj_y_coords_metres
    )

    return x_offset_metres, y_offset_metres


def _plot_forecast_one_time(gridded_forecast_dict, time_index, output_dir_name):
    """Plots gridded forecast at one time.

    :param gridded_forecast_dict: Dictionary returned by
        `prediction_io.read_gridded_predictions`.
    :param time_index: Will plot the [i]th gridded forecast, where
        i = `time_index`.
    :param output_dir_name: Name of output directory.  Figure will be saved
        here.
    """

    latlng_limit_dict = {
        plotting_utils.MIN_LATITUDE_KEY: 27.27,
        plotting_utils.MAX_LATITUDE_KEY: 43.34,
        plotting_utils.MIN_LONGITUDE_KEY: 259.48,
        plotting_utils.MAX_LONGITUDE_KEY: 282.30
    }

    axes_object, basemap_object = plotting_utils.init_map_with_nwp_projection(
        model_name=nwp_model_utils.RAP_MODEL_NAME,
        grid_name=nwp_model_utils.NAME_OF_130GRID, xy_limit_dict=None,
        latlng_limit_dict=latlng_limit_dict
    )[1:]

    x_offset_metres, y_offset_metres = _get_projection_offsets(
{'fraction_non_alphanumeric': 0.07323568575233022, 'fraction_numerical': 0.007989347536617843, 'mean_word_length': 4.690151515151515, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4148329', 'n_tokens_mistral': 2809, 'n_tokens_neox': 2677, 'n_words': 518}