Beispiel #1
0
    def test_find_invalid_latitudes(self):
        """Ensures correct output from find_invalid_latitudes."""

        these_invalid_indices = geodetic_utils.find_invalid_latitudes(
            MESSY_LATITUDES_DEG)
        self.assertTrue(
            numpy.array_equal(these_invalid_indices, INVALID_LAT_INDICES))
def remove_invalid_rows(input_table,
                        check_speed_flag=False,
                        check_direction_flag=False,
                        check_u_wind_flag=False,
                        check_v_wind_flag=False,
                        check_lat_flag=False,
                        check_lng_flag=False,
                        check_elevation_flag=False,
                        check_time_flag=False):
    """Removes any row with invalid data from pandas DataFrame.

    However, this method does not remove rows with invalid wind direction or
    elevation.  It simply sets the wind direction or elevation to NaN, so that
    it will not be mistaken for valid data.  Also, this method converts
    longitudes to positive (180...360 deg E) in western hemisphere.

    :param input_table: pandas DataFrame.
    :param check_speed_flag: Boolean flag.  If True, will check wind speed.
    :param check_direction_flag: Boolean flag.  If True, will check wind
        direction.
    :param check_u_wind_flag: Boolean flag.  If True, will check u-wind.
    :param check_v_wind_flag: Boolean flag.  If True, will check v-wind.
    :param check_lat_flag: Boolean flag.  If True, will check latitude.
    :param check_lng_flag: Boolean flag.  If True, will check longitude.
    :param check_elevation_flag: Boolean flag.  If True, will check elevation.
    :param check_time_flag: Boolean flag.  If True, will check time.
    :return: output_table: Same as input_table, except that some rows may be
        gone.
    """

    error_checking.assert_is_boolean(check_speed_flag)
    error_checking.assert_is_boolean(check_direction_flag)
    error_checking.assert_is_boolean(check_u_wind_flag)
    error_checking.assert_is_boolean(check_v_wind_flag)
    error_checking.assert_is_boolean(check_lat_flag)
    error_checking.assert_is_boolean(check_lng_flag)
    error_checking.assert_is_boolean(check_elevation_flag)
    error_checking.assert_is_boolean(check_time_flag)

    if check_speed_flag:
        invalid_sustained_indices = check_wind_speeds(
            input_table[WIND_SPEED_COLUMN].values, one_component=False)
        input_table[WIND_SPEED_COLUMN].values[
            invalid_sustained_indices] = numpy.nan

        invalid_gust_indices = check_wind_speeds(
            input_table[WIND_GUST_SPEED_COLUMN].values, one_component=False)
        input_table[WIND_GUST_SPEED_COLUMN].values[
            invalid_gust_indices] = numpy.nan

        invalid_indices = list(
            set(invalid_gust_indices).intersection(invalid_sustained_indices))
        input_table.drop(input_table.index[invalid_indices],
                         axis=0,
                         inplace=True)

    if check_direction_flag:
        invalid_indices = _check_wind_directions(
            input_table[WIND_DIR_COLUMN].values)
        input_table[WIND_DIR_COLUMN].values[invalid_indices] = numpy.nan

        invalid_indices = _check_wind_directions(
            input_table[WIND_GUST_DIR_COLUMN].values)
        input_table[WIND_GUST_DIR_COLUMN].values[invalid_indices] = numpy.nan

    if check_u_wind_flag:
        invalid_indices = check_wind_speeds(input_table[U_WIND_COLUMN].values,
                                            one_component=True)
        input_table.drop(input_table.index[invalid_indices],
                         axis=0,
                         inplace=True)

    if check_v_wind_flag:
        invalid_indices = check_wind_speeds(input_table[V_WIND_COLUMN].values,
                                            one_component=True)
        input_table.drop(input_table.index[invalid_indices],
                         axis=0,
                         inplace=True)

    if check_lat_flag:
        invalid_indices = geodetic_utils.find_invalid_latitudes(
            input_table[LATITUDE_COLUMN].values)
        input_table.drop(input_table.index[invalid_indices],
                         axis=0,
                         inplace=True)

    if check_lng_flag:
        invalid_indices = geodetic_utils.find_invalid_longitudes(
            input_table[LONGITUDE_COLUMN].values,
            sign_in_western_hemisphere=geodetic_utils.EITHER_SIGN_LONGITUDE_ARG
        )
        input_table.drop(input_table.index[invalid_indices],
                         axis=0,
                         inplace=True)

        input_table[LONGITUDE_COLUMN] = (
            lng_conversion.convert_lng_positive_in_west(
                input_table[LONGITUDE_COLUMN].values))

    if check_elevation_flag:
        invalid_indices = _check_elevations(
            input_table[ELEVATION_COLUMN].values)
        input_table[ELEVATION_COLUMN].values[invalid_indices] = numpy.nan

    if check_time_flag:
        invalid_flags = numpy.invert(input_table[TIME_COLUMN].values > 0)
        invalid_indices = numpy.where(invalid_flags)[0]
        input_table.drop(input_table.index[invalid_indices],
                         axis=0,
                         inplace=True)

    return input_table
Beispiel #3
0
def remove_invalid_reports(tornado_table,
                           check_times_flag=True,
                           check_latitudes_flag=True,
                           check_longitudes_flag=True,
                           check_fujita_rating_flag=True,
                           check_width_flag=True):
    """Removes invalid tornado reports.

    A report is considered invalid if any of its properties are invalid.

    :param tornado_table: pandas DataFrame created by
        `storm_events_io.read_tornado_reports`.
    :param check_times_flag: Boolean flag.  If True, will check start/end time
        for each report.
    :param check_latitudes_flag: Boolean flag.  If True, will check start/end
        latitude for each report.
    :param check_longitudes_flag: Boolean flag.  If True, will check start/end
        longitude for each report.
    :param check_fujita_rating_flag: Boolean flag.  If True, will check Fujita
        rating for each report.  If False, will ignore Fujita rating.
    :param check_width_flag: Boolean flag.  If True, will check tornado width
        for each report.
    :return: tornado_table: Same as input, but maybe with fewer rows.
    """

    error_checking.assert_is_boolean(check_fujita_rating_flag)
    error_checking.assert_is_boolean(check_width_flag)
    error_checking.assert_is_boolean(check_latitudes_flag)
    error_checking.assert_is_boolean(check_longitudes_flag)
    error_checking.assert_is_boolean(check_times_flag)

    if check_times_flag:
        invalid_flags = numpy.invert(
            numpy.logical_and(tornado_table[START_TIME_COLUMN].values > 0,
                              tornado_table[END_TIME_COLUMN].values > 0))
        tornado_table.drop(tornado_table.index[numpy.where(invalid_flags)[0]],
                           axis=0,
                           inplace=True)

    if check_latitudes_flag:
        invalid_indices = geodetic_utils.find_invalid_latitudes(
            tornado_table[START_LAT_COLUMN].values)
        tornado_table.drop(tornado_table.index[invalid_indices],
                           axis=0,
                           inplace=True)

        invalid_indices = geodetic_utils.find_invalid_latitudes(
            tornado_table[END_LAT_COLUMN].values)
        tornado_table.drop(tornado_table.index[invalid_indices],
                           axis=0,
                           inplace=True)

    if check_longitudes_flag:
        invalid_indices = geodetic_utils.find_invalid_longitudes(
            tornado_table[START_LNG_COLUMN].values,
            sign_in_western_hemisphere=geodetic_utils.EITHER_SIGN_LONGITUDE_ARG
        )
        tornado_table.drop(tornado_table.index[invalid_indices],
                           axis=0,
                           inplace=True)

        invalid_indices = geodetic_utils.find_invalid_longitudes(
            tornado_table[END_LNG_COLUMN].values,
            sign_in_western_hemisphere=geodetic_utils.EITHER_SIGN_LONGITUDE_ARG
        )
        tornado_table.drop(tornado_table.index[invalid_indices],
                           axis=0,
                           inplace=True)

        tornado_table[START_LNG_COLUMN] = (
            lng_conversion.convert_lng_positive_in_west(
                tornado_table[START_LNG_COLUMN].values))
        tornado_table[END_LNG_COLUMN] = (
            lng_conversion.convert_lng_positive_in_west(
                tornado_table[END_LNG_COLUMN].values))

    if check_fujita_rating_flag:
        invalid_flags = numpy.invert(
            numpy.array([
                _is_valid_fujita_rating(s)
                for s in tornado_table[FUJITA_RATING_COLUMN].values
            ]))
        tornado_table.drop(tornado_table.index[numpy.where(invalid_flags)[0]],
                           axis=0,
                           inplace=True)

    if check_width_flag:
        invalid_flags = numpy.invert(tornado_table[WIDTH_COLUMN].values > 0.)
        tornado_table.drop(tornado_table.index[numpy.where(invalid_flags)[0]],
                           axis=0,
                           inplace=True)

    return tornado_table