def test_celsius_to_kelvins(self): """Ensures correct output from celsius_to_kelvins.""" these_temperatures_kelvins = ( temperature_conversions.celsius_to_kelvins(TEMPERATURES_DEG_C)) self.assertTrue( numpy.allclose(these_temperatures_kelvins, TEMPERATURES_KELVINS, atol=TOLERANCE))
def vapour_pressure_to_dewpoint(vapour_pressures_pascals, temperatures_kelvins): """Converts each vapour pressure to dewpoint. Source: https://content.meteoblue.com/hu/specifications/weather-variables/humidity :param vapour_pressures_pascals: numpy array (any shape) of vapour pressures. :param temperatures_kelvins: numpy array (same shape) of temperatures. :return: dewpoints_kelvins: numpy array (same shape) of dewpoints. """ error_checking.assert_is_geq_numpy_array( vapour_pressures_pascals, 0., allow_nan=True ) error_checking.assert_is_geq_numpy_array( temperatures_kelvins, 0., allow_nan=True ) error_checking.assert_is_numpy_array( temperatures_kelvins, exact_dimensions=numpy.array(vapour_pressures_pascals.shape, dtype=int) ) logarithms = numpy.log( vapour_pressures_pascals / BASE_VAPOUR_PRESSURE_PASCALS ) temperatures_deg_c = temperature_conv.kelvins_to_celsius( temperatures_kelvins ) numerator_coeffs = numpy.full( temperatures_deg_c.shape, MAGNUS_DENOMINATOR_COEFF_WATER ) numerator_coeffs[temperatures_deg_c < 0] = MAGNUS_DENOMINATOR_COEFF_ICE numerators = numerator_coeffs * logarithms denominator_coeffs = numpy.full( temperatures_deg_c.shape, MAGNUS_NUMERATOR_COEFF_WATER ) denominator_coeffs[temperatures_deg_c < 0] = MAGNUS_NUMERATOR_COEFF_ICE denominators = denominator_coeffs - logarithms dewpoints_deg_c = numerators / denominators dewpoints_kelvins = temperature_conv.celsius_to_kelvins(dewpoints_deg_c) dewpoints_kelvins[numpy.invert(numpy.isfinite(dewpoints_kelvins))] = 0. dewpoints_kelvins[dewpoints_deg_c + numerator_coeffs < 0] = 0. return dewpoints_kelvins
soundings.PRESSURE_COLUMN_FOR_SHARPPY_INPUT: THESE_PRESSURES_MB, soundings.HEIGHT_COLUMN_FOR_SHARPPY_INPUT: THESE_HEIGHTS_M_ASL, soundings.TEMPERATURE_COLUMN_FOR_SHARPPY_INPUT: THESE_TEMPERATURES_DEG_C, soundings.DEWPOINT_COLUMN_FOR_SHARPPY_INPUT: THESE_DEWPOINTS_DEG_C, soundings.U_WIND_COLUMN_FOR_SHARPPY_INPUT: THESE_U_WINDS_KT, soundings.V_WIND_COLUMN_FOR_SHARPPY_INPUT: THESE_V_WINDS_KT, soundings.IS_SURFACE_COLUMN_FOR_SHARPPY_INPUT: THESE_SURFACE_FLAGS } SOUNDING_TABLE_SHARPPY_ORIG = pandas.DataFrame.from_dict(THIS_SOUNDING_DICT) # The following constants are used to test _sounding_to_sharppy_units. MB_TO_PASCALS = 100 UNITLESS_TO_PERCENT = 100 KT_TO_METRES_PER_SECOND = 1.852 / 3.6 THESE_TEMPERATURES_KELVINS = temperature_conversions.celsius_to_kelvins( THESE_TEMPERATURES_DEG_C) THESE_DEWPOINTS_KELVINS = temperature_conversions.celsius_to_kelvins( THESE_DEWPOINTS_DEG_C) THESE_SPECIFIC_HUMIDITIES = moisture_conversions.dewpoint_to_specific_humidity( THESE_DEWPOINTS_KELVINS, THESE_PRESSURES_MB * MB_TO_PASCALS) THESE_RH_PERCENT = moisture_conversions.dewpoint_to_relative_humidity( THESE_DEWPOINTS_KELVINS, THESE_TEMPERATURES_KELVINS, THESE_PRESSURES_MB * MB_TO_PASCALS) * UNITLESS_TO_PERCENT THESE_U_WINDS_M_S01 = KT_TO_METRES_PER_SECOND * THESE_U_WINDS_KT THESE_V_WINDS_M_S01 = KT_TO_METRES_PER_SECOND * THESE_V_WINDS_KT THIS_SOUNDING_DICT = { soundings.PRESSURE_COLUMN_FOR_SHARPPY_INPUT: THESE_PRESSURES_MB, nwp_model_utils.HEIGHT_COLUMN_FOR_SOUNDING_TABLES: THESE_HEIGHTS_M_ASL, nwp_model_utils.TEMPERATURE_COLUMN_FOR_SOUNDING_TABLES: THESE_TEMPERATURES_KELVINS,
def _plot_results_one_example(bwo_dict, example_index, model_metadata_dict, use_log_scale, output_dir_name): """Plots results for one example. :param bwo_dict: Dictionary read by `backwards_optimization.read_file`. :param example_index: Will plot results for example with this array index. :param model_metadata_dict: Dictionary read by `neural_net.read_metafile`. :param use_log_scale: See documentation at top of file. :param output_dir_name: Name of output directory. Figure will be saved here. """ # Housekeeping. example_id_string = bwo_dict[bwo.EXAMPLE_IDS_KEY][example_index] generator_option_dict = model_metadata_dict[ neural_net.TRAINING_OPTIONS_KEY] base_example_dict = { example_utils.SCALAR_PREDICTOR_NAMES_KEY: generator_option_dict[neural_net.SCALAR_PREDICTOR_NAMES_KEY], example_utils.VECTOR_PREDICTOR_NAMES_KEY: generator_option_dict[neural_net.VECTOR_PREDICTOR_NAMES_KEY], example_utils.HEIGHTS_KEY: generator_option_dict[neural_net.HEIGHTS_KEY] } init_example_dict = copy.deepcopy(base_example_dict) init_example_dict[example_utils.SCALAR_PREDICTOR_VALS_KEY] = ( bwo_dict[bwo.INIT_SCALAR_PREDICTORS_KEY][[example_index], ...]) init_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY] = ( bwo_dict[bwo.INIT_VECTOR_PREDICTORS_KEY][[example_index], ...]) final_example_dict = copy.deepcopy(base_example_dict) final_example_dict[example_utils.SCALAR_PREDICTOR_VALS_KEY] = ( bwo_dict[bwo.FINAL_SCALAR_PREDICTORS_KEY][[example_index], ...]) final_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY] = ( bwo_dict[bwo.FINAL_VECTOR_PREDICTORS_KEY][[example_index], ...]) diff_example_dict = copy.deepcopy(base_example_dict) diff_example_dict[example_utils.SCALAR_PREDICTOR_VALS_KEY] = ( final_example_dict[example_utils.SCALAR_PREDICTOR_VALS_KEY] - init_example_dict[example_utils.SCALAR_PREDICTOR_VALS_KEY]) diff_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY] = ( final_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY] - init_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY]) predictor_names = ( diff_example_dict[example_utils.VECTOR_PREDICTOR_NAMES_KEY]) if example_utils.TEMPERATURE_NAME in predictor_names: temperature_index = predictor_names.index( example_utils.TEMPERATURE_NAME) diff_predictor_matrix = ( diff_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY]) diff_predictor_matrix[..., temperature_index] = ( temperature_conv.celsius_to_kelvins( diff_predictor_matrix[..., temperature_index])) diff_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY] = ( diff_predictor_matrix) num_predictor_sets = len(PREDICTOR_NAMES_BY_SET) for k in range(num_predictor_sets): these_flags = numpy.array([ n in base_example_dict[example_utils.VECTOR_PREDICTOR_NAMES_KEY] for n in PREDICTOR_NAMES_BY_SET[k] ], dtype=bool) these_indices = numpy.where(these_flags)[0] if len(these_indices) == 0: continue predictor_names = [PREDICTOR_NAMES_BY_SET[k][i] for i in these_indices] predictor_colours = [ PREDICTOR_COLOURS_BY_SET[k][i] for i in these_indices ] # Plot initial and final values on the same set of axes. handle_dict = profile_plotting.plot_predictors( example_dict=init_example_dict, example_index=0, predictor_names=predictor_names, predictor_colours=predictor_colours, predictor_line_widths=numpy.full(len(these_indices), SOLID_LINE_WIDTH), predictor_line_styles=['solid'] * len(these_indices), use_log_scale=use_log_scale, handle_dict=None) profile_plotting.plot_predictors( example_dict=final_example_dict, example_index=0, predictor_names=predictor_names, predictor_colours=predictor_colours, predictor_line_widths=numpy.full(len(these_indices), DASHED_LINE_WIDTH), predictor_line_styles=['dashed'] * len(these_indices), use_log_scale=use_log_scale, handle_dict=handle_dict) output_file_name = '{0:s}/{1:s}_predictor-set-{2:d}.jpg'.format( output_dir_name, example_id_string.replace('_', '-'), k) figure_object = handle_dict[profile_plotting.FIGURE_HANDLE_KEY] 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) # Plot differences (final minus initial). handle_dict = profile_plotting.plot_predictors( example_dict=diff_example_dict, example_index=0, predictor_names=predictor_names, predictor_colours=predictor_colours, predictor_line_widths=numpy.full(len(these_indices), SOLID_LINE_WIDTH), predictor_line_styles=['solid'] * len(these_indices), use_log_scale=use_log_scale, handle_dict=None) output_file_name = '{0:s}/{1:s}_predictor-set-{2:d}_diffs.jpg'.format( output_dir_name, example_id_string.replace('_', '-'), k) figure_object = handle_dict[profile_plotting.FIGURE_HANDLE_KEY] 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)