def test_create_tornado_targets(self): """Ensures correct output from create_tornado_targets.""" this_storm_to_tornadoes_table = target_val_utils.create_tornado_targets( storm_to_tornadoes_table=copy.deepcopy(STORM_TO_TORNADOES_TABLE), min_lead_time_sec=MIN_LEAD_TIME_SEC, max_lead_time_sec=MAX_LEAD_TIME_SEC, min_link_distance_metres=MIN_LINK_DISTANCE_METRES, max_link_distance_metres=MAX_LINK_DISTANCE_METRES) these_target_values = this_storm_to_tornadoes_table[TARGET_NAME].values self.assertTrue(numpy.array_equal(these_target_values, TARGET_VALUES))
def test_create_tornado_targets_genesis(self): """Ensures correct output from create_tornado_targets. In this case, target value is based on tornadogenesis only, not occurrence. """ this_storm_to_tornadoes_table = target_val_utils.create_tornado_targets( storm_to_tornadoes_table=copy.deepcopy(STORM_TO_TORNADOES_TABLE), min_lead_time_sec=MIN_LEAD_TIME_SEC, max_lead_time_sec=MAX_LEAD_TIME_SEC, min_link_distance_metres=MIN_LINK_DISTANCE_METRES, max_link_distance_metres=MAX_LINK_DISTANCE_METRES, genesis_only=True) self.assertTrue( numpy.array_equal( this_storm_to_tornadoes_table[MAIN_TORNADOGENESIS_TARGET_NAME]. values, MAIN_TORNADOGENESIS_TARGET_VALUES))
def _run(top_linkage_dir_name, spc_date_string, min_lead_times_sec, max_lead_times_sec, min_link_distances_metres, max_link_distances_metres, event_type_string, wind_speed_percentile_level, wind_speed_cutoffs_kt, top_output_dir_name): """Computes target value for ea storm object, lead-time window, and buffer. This is effectively the main method. :param top_linkage_dir_name: See documentation at top of file. :param spc_date_string: Same. :param min_lead_times_sec: Same. :param max_lead_times_sec: Same. :param min_link_distances_metres: Same. :param max_link_distances_metres: Same. :param event_type_string: Same. :param wind_speed_percentile_level: Same. :param wind_speed_cutoffs_kt: Same. :param top_output_dir_name: Same. """ num_lead_time_windows = len(min_lead_times_sec) error_checking.assert_is_numpy_array( max_lead_times_sec, exact_dimensions=numpy.array([num_lead_time_windows]) ) num_distance_buffers = len(min_link_distances_metres) error_checking.assert_is_numpy_array( max_link_distances_metres, exact_dimensions=numpy.array([num_distance_buffers]) ) linkage_file_name = linkage.find_linkage_file( top_directory_name=top_linkage_dir_name, event_type_string=event_type_string, spc_date_string=spc_date_string) print 'Reading data from: "{0:s}"...'.format(linkage_file_name) storm_to_events_table = linkage.read_linkage_file(linkage_file_name) if event_type_string == linkage.WIND_EVENT_STRING: list_of_cutoff_arrays_kt = general_utils.split_array_by_nan( wind_speed_cutoffs_kt) num_cutoff_sets = len(wind_speed_cutoffs_kt) else: list_of_cutoff_arrays_kt = None num_cutoff_sets = 1 target_names = [] for i in range(num_lead_time_windows): for j in range(num_distance_buffers): for k in range(num_cutoff_sets): if event_type_string == linkage.WIND_EVENT_STRING: this_target_name = target_val_utils.target_params_to_name( min_lead_time_sec=min_lead_times_sec[i], max_lead_time_sec=max_lead_times_sec[i], min_link_distance_metres=min_link_distances_metres[j], max_link_distance_metres=max_link_distances_metres[j], wind_speed_percentile_level=wind_speed_percentile_level, wind_speed_cutoffs_kt=list_of_cutoff_arrays_kt[k]) target_names.append(this_target_name) print 'Computing values for "{0:s}"...'.format( target_names[-1]) storm_to_events_table = ( target_val_utils.create_wind_classification_targets( storm_to_winds_table=storm_to_events_table, min_lead_time_sec=min_lead_times_sec[i], max_lead_time_sec=max_lead_times_sec[i], min_link_distance_metres=min_link_distances_metres[ j], max_link_distance_metres=max_link_distances_metres[ j], percentile_level=wind_speed_percentile_level, class_cutoffs_kt=list_of_cutoff_arrays_kt[k]) ) else: this_target_name = target_val_utils.target_params_to_name( min_lead_time_sec=min_lead_times_sec[i], max_lead_time_sec=max_lead_times_sec[i], min_link_distance_metres=min_link_distances_metres[j], max_link_distance_metres=max_link_distances_metres[j]) target_names.append(this_target_name) print 'Computing values for "{0:s}"...'.format( target_names[-1]) storm_to_events_table = ( target_val_utils.create_tornado_targets( storm_to_tornadoes_table=storm_to_events_table, min_lead_time_sec=min_lead_times_sec[i], max_lead_time_sec=max_lead_times_sec[i], min_link_distance_metres=min_link_distances_metres[ j], max_link_distance_metres=max_link_distances_metres[ j] ) ) target_file_name = target_val_utils.find_target_file( top_directory_name=top_output_dir_name, event_type_string=event_type_string, spc_date_string=spc_date_string, raise_error_if_missing=False) print 'Writing target values to: "{0:s}"...'.format(target_file_name) target_val_utils.write_target_values( storm_to_events_table=storm_to_events_table, target_names=target_names, netcdf_file_name=target_file_name)
def _compute_targets_one_day(storm_to_events_table, spc_date_string, min_lead_times_sec, max_lead_times_sec, min_link_distances_metres, max_link_distances_metres, event_type_string, wind_speed_percentile_level, wind_speed_cutoffs_kt, top_output_dir_name): """Computes target values for one SPC date. :param storm_to_events_table: pandas DataFrame returned by `linkage.read_linkage_file`. :param spc_date_string: SPC date (format "yyyymmdd"). :param min_lead_times_sec: See documentation at top of file. :param max_lead_times_sec: Same. :param min_link_distances_metres: Same. :param max_link_distances_metres: Same. :param event_type_string: Same. :param wind_speed_percentile_level: Same. :param wind_speed_cutoffs_kt: Same. :param top_output_dir_name: Same. """ num_lead_time_windows = len(min_lead_times_sec) num_distance_buffers = len(min_link_distances_metres) if event_type_string == linkage.WIND_EVENT_STRING: list_of_cutoff_arrays_kt = general_utils.split_array_by_nan( wind_speed_cutoffs_kt) num_cutoff_sets = len(wind_speed_cutoffs_kt) else: list_of_cutoff_arrays_kt = None num_cutoff_sets = 1 target_names = [] for i in range(num_lead_time_windows): for j in range(num_distance_buffers): for k in range(num_cutoff_sets): if event_type_string == linkage.WIND_EVENT_STRING: this_target_name = target_val_utils.target_params_to_name( min_lead_time_sec=min_lead_times_sec[i], max_lead_time_sec=max_lead_times_sec[i], min_link_distance_metres=min_link_distances_metres[j], max_link_distance_metres=max_link_distances_metres[j], wind_speed_percentile_level=wind_speed_percentile_level, wind_speed_cutoffs_kt=list_of_cutoff_arrays_kt[k]) target_names.append(this_target_name) print(('Computing labels for "{0:s}" on SPC date {1:s}...' ).format(this_target_name, spc_date_string)) storm_to_events_table = ( target_val_utils.create_wind_classification_targets( storm_to_winds_table=storm_to_events_table, min_lead_time_sec=min_lead_times_sec[i], max_lead_time_sec=max_lead_times_sec[i], min_link_distance_metres=min_link_distances_metres[ j], max_link_distance_metres=max_link_distances_metres[ j], percentile_level=wind_speed_percentile_level, class_cutoffs_kt=list_of_cutoff_arrays_kt[k])) else: genesis_only = (event_type_string == linkage.TORNADOGENESIS_EVENT_STRING) this_target_name = target_val_utils.target_params_to_name( min_lead_time_sec=min_lead_times_sec[i], max_lead_time_sec=max_lead_times_sec[i], min_link_distance_metres=min_link_distances_metres[j], max_link_distance_metres=max_link_distances_metres[j], genesis_only=genesis_only) target_names.append(this_target_name) print(('Computing labels for "{0:s}" on SPC date {1:s}...' ).format(this_target_name, spc_date_string)) storm_to_events_table = ( target_val_utils.create_tornado_targets( storm_to_tornadoes_table=storm_to_events_table, min_lead_time_sec=min_lead_times_sec[i], max_lead_time_sec=max_lead_times_sec[i], min_link_distance_metres=min_link_distances_metres[ j], max_link_distance_metres=max_link_distances_metres[ j], genesis_only=genesis_only)) target_file_name = target_val_utils.find_target_file( top_directory_name=top_output_dir_name, event_type_string=event_type_string, spc_date_string=spc_date_string, raise_error_if_missing=False) print('Writing target values to: "{0:s}"...'.format(target_file_name)) target_val_utils.write_target_values( storm_to_events_table=storm_to_events_table, target_names=target_names, netcdf_file_name=target_file_name)
def _compute_tornado_targets_one_day(storm_to_tornadoes_table, spc_date_string, min_lead_times_sec, max_lead_times_sec, min_link_distances_metres, max_link_distances_metres, genesis_only, min_fujita_ratings, top_output_dir_name): """Computes tornado-related target values for one SPC date. :param storm_to_tornadoes_table: pandas DataFrame returned by `linkage.read_linkage_file`. :param spc_date_string: SPC date (format "yyyymmdd"). :param min_lead_times_sec: See documentation at top of file. :param max_lead_times_sec: Same. :param min_link_distances_metres: Same. :param max_link_distances_metres: Same. :param genesis_only: Boolean flag. If True, will create labels for tornadogenesis only. If False, will create labels for occurrence, which includes pre-existing tornadoes (at forecast time). :param min_fujita_ratings: Same. :param top_output_dir_name: Same. """ num_time_windows = len(min_lead_times_sec) num_distance_buffers = len(min_link_distances_metres) num_strength_thresholds = len(min_fujita_ratings) target_names = [] for i in range(num_time_windows): for j in range(num_distance_buffers): for k in range(num_strength_thresholds): this_target_name = target_val_utils.target_params_to_name( min_lead_time_sec=min_lead_times_sec[i], max_lead_time_sec=max_lead_times_sec[i], min_link_distance_metres=min_link_distances_metres[j], max_link_distance_metres=max_link_distances_metres[j], tornadogenesis_only=genesis_only, min_fujita_rating=min_fujita_ratings[k]) target_names.append(this_target_name) print(('Creating labels for "{0:s}" on SPC date "{1:s}"...' ).format(this_target_name, spc_date_string)) storm_to_tornadoes_table = ( target_val_utils.create_tornado_targets( storm_to_tornadoes_table=storm_to_tornadoes_table, min_lead_time_sec=min_lead_times_sec[i], max_lead_time_sec=max_lead_times_sec[i], min_link_distance_metres=min_link_distances_metres[j], max_link_distance_metres=max_link_distances_metres[j], genesis_only=genesis_only, min_fujita_rating=min_fujita_ratings[k])) print(SEPARATOR_STRING) event_type_string = (linkage.TORNADOGENESIS_EVENT_STRING if genesis_only else linkage.TORNADO_EVENT_STRING) target_file_name = target_val_utils.find_target_file( top_directory_name=top_output_dir_name, event_type_string=event_type_string, spc_date_string=spc_date_string, raise_error_if_missing=False) print('Writing target values to: "{0:s}"...'.format(target_file_name)) target_val_utils.write_target_values( storm_to_events_table=storm_to_tornadoes_table, target_names=target_names, netcdf_file_name=target_file_name)