Exemplo n.º 1
0
    def test_fluxes_to_heating_rate(self):
        """Ensures correct output from fluxes_to_heating_rate."""

        this_example_dict = example_utils.fluxes_to_heating_rate(
            copy.deepcopy(EXAMPLE_DICT_FLUXES_ONLY))

        self.assertTrue(
            _compare_example_dicts(this_example_dict,
                                   EXAMPLE_DICT_WITH_HEATING_RATE))
Exemplo n.º 2
0
def _fluxes_to_heating_rate(down_fluxes_w_m02, up_fluxes_w_m02, pressures_pa):
    """Converts upwelling and downwelling fluxes to heating rate at each level.

    This is a light wrapper for `example_utils.fluxes_to_heating_rate`.

    L = number of levels in grid

    :param down_fluxes_w_m02: length-L numpy array of downwelling fluxes (Watts
        per square metre).
    :param up_fluxes_w_m02 : length-L numpy array of upwelling fluxes (Watts
        per square metre).
    :param pressures_pa: length-L numpy array of pressures (Pascals).
    :return: heating_rates_k_day01: length-L numpy array of heating rates
        (Kelvins per day).
    """

    target_matrix = numpy.vstack((down_fluxes_w_m02, up_fluxes_w_m02))
    target_matrix = numpy.expand_dims(numpy.transpose(target_matrix), axis=0)
    predictor_matrix = numpy.expand_dims(pressures_pa, axis=-1)
    predictor_matrix = numpy.expand_dims(predictor_matrix, axis=0)

    dummy_example_dict = {
        example_utils.VECTOR_TARGET_NAMES_KEY: [
            example_utils.SHORTWAVE_DOWN_FLUX_NAME,
            example_utils.SHORTWAVE_UP_FLUX_NAME
        ],
        example_utils.VECTOR_TARGET_VALS_KEY:
        target_matrix,
        example_utils.VECTOR_PREDICTOR_NAMES_KEY:
        [example_utils.PRESSURE_NAME],
        example_utils.VECTOR_PREDICTOR_VALS_KEY:
        predictor_matrix
    }
    dummy_example_dict = example_utils.fluxes_to_heating_rate(
        dummy_example_dict)

    return example_utils.get_field_from_dict(
        example_dict=dummy_example_dict,
        field_name=example_utils.SHORTWAVE_HEATING_RATE_NAME)[0, :]
Exemplo n.º 3
0
def _get_predicted_heating_rates(prediction_example_dict,
                                 pressure_matrix_pascals, model_metadata_dict):
    """Computes predicted heating rates from predicted flux-increment profiles.

    :param prediction_example_dict: Dictionary with predictions.  For a list of
        keys, see doc for `example_io.read_file`.
    :param pressure_matrix_pascals: See doc for `_get_unnormalized_pressure`.
    :param model_metadata_dict: Same.
    :return: prediction_example_dict: Same but with heating rates.
    """

    num_examples = pressure_matrix_pascals.shape[0]
    generator_option_dict = copy.deepcopy(
        model_metadata_dict[neural_net.TRAINING_OPTIONS_KEY])

    this_dict = {
        example_utils.VECTOR_PREDICTOR_NAMES_KEY:
        [example_utils.PRESSURE_NAME],
        example_utils.VECTOR_PREDICTOR_VALS_KEY:
        numpy.expand_dims(pressure_matrix_pascals, axis=-1),
        example_utils.SCALAR_PREDICTOR_NAMES_KEY: [],
        example_utils.SCALAR_PREDICTOR_VALS_KEY:
        numpy.full((num_examples, 0), 0.),
        example_utils.VALID_TIMES_KEY:
        numpy.full(num_examples, 0, dtype=int)
    }
    prediction_example_dict.update(this_dict)

    prediction_example_dict = (
        example_utils.fluxes_increments_to_actual(prediction_example_dict))
    prediction_example_dict = example_utils.fluxes_to_heating_rate(
        prediction_example_dict)

    target_names = (generator_option_dict[neural_net.VECTOR_TARGET_NAMES_KEY] +
                    generator_option_dict[neural_net.SCALAR_TARGET_NAMES_KEY])
    return example_utils.subset_by_field(example_dict=prediction_example_dict,
                                         field_names=target_names)
Exemplo n.º 4
0
def _fluxes_to_heating_rate(vector_target_matrix, vector_prediction_matrix,
                            model_metadata_dict, prediction_file_name,
                            example_dir_name):
    """If necessary, converts fluxes to heating rates.

    This method is a wrapper for `example_utils.fluxes_to_heating_rate`.

    :param vector_target_matrix: See doc for `_fluxes_increments_to_actual`.
    :param vector_prediction_matrix: Same.
    :param model_metadata_dict: Same.
    :param prediction_file_name: See documentation at top of file.
    :param example_dir_name: Same.
    :return: vector_target_matrix: See doc for `_fluxes_increments_to_actual`.
    :return: vector_prediction_matrix: Same.
    :return: model_metadata_dict: Same.
    """

    generator_option_dict = model_metadata_dict[
        neural_net.TRAINING_OPTIONS_KEY]
    vector_target_names = (
        generator_option_dict[neural_net.VECTOR_TARGET_NAMES_KEY])
    need_heating_rate = (example_utils.SHORTWAVE_HEATING_RATE_NAME
                         not in vector_target_names)
    have_fluxes = all([t in vector_target_names for t in FLUX_NAMES])

    if not (need_heating_rate and have_fluxes
            and example_dir_name is not None):
        return (vector_target_matrix, vector_prediction_matrix,
                model_metadata_dict)

    num_examples = vector_target_matrix.shape[0]

    this_example_dict = misc_utils.get_raw_examples(
        example_file_name='',
        num_examples=0,
        example_dir_name=example_dir_name,
        example_id_file_name=prediction_file_name)
    this_example_dict = example_utils.subset_by_height(
        example_dict=this_example_dict,
        heights_m_agl=generator_option_dict[neural_net.HEIGHTS_KEY])
    pressure_matrix_pascals = example_utils.get_field_from_dict(
        example_dict=this_example_dict, field_name=example_utils.PRESSURE_NAME)
    pressure_matrix_pascals = pressure_matrix_pascals[:num_examples, ...]

    base_example_dict = {
        example_utils.SCALAR_PREDICTOR_NAMES_KEY: [],
        example_utils.SCALAR_PREDICTOR_VALS_KEY:
        numpy.full((num_examples, 0), 0.),
        example_utils.VECTOR_PREDICTOR_NAMES_KEY:
        [example_utils.PRESSURE_NAME],
        example_utils.VECTOR_PREDICTOR_VALS_KEY:
        numpy.expand_dims(pressure_matrix_pascals, axis=-1),
        example_utils.SCALAR_TARGET_NAMES_KEY: [],
        example_utils.SCALAR_TARGET_VALS_KEY:
        numpy.full((num_examples, 0), 0.),
        example_utils.VECTOR_TARGET_NAMES_KEY:
        generator_option_dict[neural_net.VECTOR_TARGET_NAMES_KEY],
        example_utils.HEIGHTS_KEY:
        generator_option_dict[neural_net.HEIGHTS_KEY],
        example_utils.VALID_TIMES_KEY:
        numpy.full(num_examples, 0, dtype=int)
    }

    target_example_dict = copy.deepcopy(base_example_dict)
    target_example_dict[example_utils.VECTOR_TARGET_VALS_KEY] = (
        vector_target_matrix)
    target_example_dict = example_utils.fluxes_to_heating_rate(
        target_example_dict)
    vector_target_matrix = (
        target_example_dict[example_utils.VECTOR_TARGET_VALS_KEY])

    prediction_example_dict = copy.deepcopy(base_example_dict)
    prediction_example_dict[example_utils.VECTOR_TARGET_VALS_KEY] = (
        vector_prediction_matrix)
    prediction_example_dict = example_utils.fluxes_to_heating_rate(
        prediction_example_dict)
    vector_prediction_matrix = (
        prediction_example_dict[example_utils.VECTOR_TARGET_VALS_KEY])

    generator_option_dict[neural_net.VECTOR_TARGET_NAMES_KEY] = (
        target_example_dict[example_utils.VECTOR_TARGET_NAMES_KEY])
    model_metadata_dict[neural_net.TRAINING_OPTIONS_KEY] = (
        generator_option_dict)

    return vector_target_matrix, vector_prediction_matrix, model_metadata_dict