def test_check_heights_gridrad_valid(self): """Ensures correct output from check_heights. In this case, data source is GridRad and input height is valid. """ radar_utils.check_heights(data_source=radar_utils.GRIDRAD_SOURCE_ID, heights_m_asl=numpy.array([500.]))
def test_check_heights_gridrad_invalid(self): """Ensures correct output from check_heights. In this case, data source is GridRad and input height is *invalid*. """ with self.assertRaises(ValueError): radar_utils.check_heights( data_source=radar_utils.GRIDRAD_SOURCE_ID, heights_m_asl=numpy.array([501.]))
def test_check_heights_myrorss_reflectivity_valid(self): """Ensures correct output from check_heights. In this case, data source is MYRORSS; field is reflectivity (defined at many height levels); and input height is valid. """ radar_utils.check_heights( data_source=radar_utils.MYRORSS_SOURCE_ID, heights_m_asl=numpy.array([250.]), field_name=radar_utils.REFL_NAME)
def test_check_heights_myrorss_shear_valid(self): """Ensures correct output from check_heights. In this case, data source and field are azimuthal shear in MYRORSS; input height is valid. """ radar_utils.check_heights( data_source=radar_utils.MYRORSS_SOURCE_ID, heights_m_asl=numpy.array([radar_utils.SHEAR_HEIGHT_M_ASL]), field_name=radar_utils.MID_LEVEL_SHEAR_NAME)
def test_check_heights_myrorss_non_shear_valid(self): """Ensures correct output from check_heights. In this case, data source is MYRORSS; field is *not* azimuthal shear; and input height is valid. """ radar_utils.check_heights( data_source=radar_utils.MYRORSS_SOURCE_ID, heights_m_asl=numpy.array( [radar_utils.DEFAULT_HEIGHT_MYRORSS_M_ASL]), field_name=radar_utils.REFL_M10CELSIUS_NAME)
def test_check_heights_myrorss_refl_invalid(self): """Ensures correct output from check_heights. In this case, data source is MYRORSS; field is reflectivity (defined at many height levels); and input height is *invalid*. """ with self.assertRaises(ValueError): radar_utils.check_heights( data_source=radar_utils.MYRORSS_SOURCE_ID, heights_m_asl=numpy.array([251.]), field_name=radar_utils.REFL_NAME)
def test_check_heights_myrorss_non_shear_invalid(self): """Ensures correct output from check_heights. In this case, data source is MYRORSS; field is *not* azimuthal shear; and input height is *invalid*. """ these_heights_m_asl = numpy.array( [radar_utils.DEFAULT_HEIGHT_MYRORSS_M_ASL + 1.]) with self.assertRaises(ValueError): radar_utils.check_heights( data_source=radar_utils.MYRORSS_SOURCE_ID, heights_m_asl=these_heights_m_asl, field_name=radar_utils.REFL_M10CELSIUS_NAME)
def test_check_heights_myrorss_shear_invalid(self): """Ensures correct output from check_heights. In this case, data source and field are azimuthal shear in MYRORSS; input height is *invalid*. """ these_heights_m_asl = numpy.array( [radar_utils.SHEAR_HEIGHT_M_ASL + 1.]) with self.assertRaises(ValueError): radar_utils.check_heights( data_source=radar_utils.MYRORSS_SOURCE_ID, heights_m_asl=these_heights_m_asl, field_name=radar_utils.MID_LEVEL_SHEAR_NAME)
def fields_and_refl_heights_to_pairs(field_names, data_source, refl_heights_m_asl=None): """Converts unique arrays (field names and refl heights) to non-unique ones. F = number of fields N = number of field-height pairs :param field_names: length-F list with names of radar fields in GewitterGefahr format. :param data_source: Data source (string). :param refl_heights_m_asl: 1-D numpy array of reflectivity heights (metres above sea level). :return: field_name_by_pair: length-N list of field names. :return: height_by_pair_m_asl: length-N numpy array of corresponding heights (metres above sea level). """ check_data_source(data_source) error_checking.assert_is_string_list(field_names) error_checking.assert_is_numpy_array(numpy.array(field_names), num_dimensions=1) field_name_by_pair = [] height_by_pair_m_asl = numpy.array([]) for this_field_name in field_names: if this_field_name == radar_utils.REFL_NAME: radar_utils.check_heights(data_source=data_source, heights_m_asl=refl_heights_m_asl, field_name=this_field_name) these_heights_m_asl = copy.deepcopy(refl_heights_m_asl) else: these_heights_m_asl = radar_utils.get_valid_heights( data_source=data_source, field_name=this_field_name) field_name_by_pair += [this_field_name] * len(these_heights_m_asl) height_by_pair_m_asl = numpy.concatenate( (height_by_pair_m_asl, these_heights_m_asl)) return field_name_by_pair, height_by_pair_m_asl
def get_relative_dir_for_raw_files(field_name, data_source, height_m_asl=None): """Generates relative path for raw files. :param field_name: Name of radar field in GewitterGefahr format. :param data_source: Data source (string). :param height_m_asl: Radar height (metres above sea level). :return: relative_directory_name: Relative path for raw files. """ if field_name == radar_utils.REFL_NAME: radar_utils.check_heights(data_source=data_source, heights_m_asl=numpy.array([height_m_asl]), field_name=radar_utils.REFL_NAME) else: height_m_asl = radar_utils.get_valid_heights(data_source=data_source, field_name=field_name)[0] return '{0:s}/{1:05.2f}'.format( radar_utils.field_name_new_to_orig(field_name=field_name, data_source_name=data_source), float(height_m_asl) * METRES_TO_KM)
def fields_and_refl_heights_to_dict(field_names, data_source, refl_heights_m_asl=None): """Converts two arrays (field names and reflectivity heights) to dictionary. :param field_names: 1-D list with names of radar fields in GewitterGefahr format. :param data_source: Data source (string). :param refl_heights_m_asl: 1-D numpy array of reflectivity heights (metres above sea level). :return: field_to_heights_dict_m_asl: Dictionary, where each key is a field name and each value is a 1-D numpy array of heights (metres above sea level). """ check_data_source(data_source) error_checking.assert_is_string_list(field_names) error_checking.assert_is_numpy_array(numpy.array(field_names), num_dimensions=1) field_to_heights_dict_m_asl = {} for this_field_name in field_names: if this_field_name == radar_utils.REFL_NAME: radar_utils.check_heights(data_source=data_source, heights_m_asl=refl_heights_m_asl, field_name=this_field_name) field_to_heights_dict_m_asl.update( {this_field_name: refl_heights_m_asl}) else: field_to_heights_dict_m_asl.update({ this_field_name: radar_utils.get_valid_heights(data_source=data_source, field_name=this_field_name) }) return field_to_heights_dict_m_asl
def fields_and_refl_heights_to_pairs(field_names, heights_m_asl): """Converts unique arrays (field names and heights) to non-unique ones. F = number of fields H = number of heights N = F * H = number of field/height pairs :param field_names: length-F list with names of radar fields in GewitterGefahr format. :param heights_m_asl: length-H numpy array of heights (metres above sea level). :return: field_name_by_pair: length-N list of field names. :return: height_by_pair_m_asl: length-N numpy array of corresponding heights (metres above sea level). """ error_checking.assert_is_string_list(field_names) error_checking.assert_is_numpy_array(numpy.array(field_names), num_dimensions=1) radar_utils.check_heights(data_source=radar_utils.GRIDRAD_SOURCE_ID, heights_m_asl=heights_m_asl) field_name_by_pair = [] height_by_pair_m_asl = numpy.array([], dtype=int) for this_field_name in field_names: radar_utils.field_name_new_to_orig( field_name=this_field_name, data_source_name=radar_utils.GRIDRAD_SOURCE_ID) field_name_by_pair += [this_field_name] * len(heights_m_asl) height_by_pair_m_asl = numpy.concatenate( (height_by_pair_m_asl, heights_m_asl)) return field_name_by_pair, height_by_pair_m_asl