R/reading_data.R

Defines functions filter_observation

Documented in filter_observation

#' Filter Observation
#'
#' @description Filters the hurricane data for a specific \code{storm_name} and \code{date}.
#'
#' @param storm_name The name of the storm to filter by.
#' @param dt A specific date to filter by.
#'
#' @return Returns an observation with an id, date, latitude, longitude, wind_speed, ne, nw, se, sw variables
#'
#' @examples
#' \dontrun{
#' filter_observation()
#' }
#'
#' @importFrom magrittr %>%
#' @importFrom dplyr mutate select_
#' @importFrom readr read_fwf fwf_widths
#' @importFrom tidyr gather_ separate spread
#' @importFrom lubridate ymd_h
#'
#' @export
filter_observation <- function(storm_name = "ike", date = '2008-09-13 12:00:00') {
	ext_tracks_widths <- c(7, 10, 2, 2, 3, 5, 5, 6, 4, 5, 4, 4, 5, 3, 4, 3, 3, 3,
												 4, 3, 3, 3, 4, 3, 3, 3, 2, 6, 1)
	ext_tracks_colnames <- c("storm_id", "storm_name", "month", "day",
													 "hour", "year", "latitude", "longitude",
													 "max_wind", "min_pressure", "rad_max_wind",
													 "eye_diameter", "pressure_1", "pressure_2",
													 paste("radius_34", c("ne", "se", "sw", "nw"), sep = "_"),
													 paste("radius_50", c("ne", "se", "sw", "nw"), sep = "_"),
													 paste("radius_64", c("ne", "se", "sw", "nw"), sep = "_"),
													 "storm_type", "distance_to_land", "final")

	ext_tracks <- readr::read_fwf("ebtrk_atlc_1988_2015.txt",
																readr::fwf_widths(ext_tracks_widths, ext_tracks_colnames),
																na = "-99")

	gather_cols <- names(ext_tracks)[grepl("radius", names(ext_tracks))]
	col_to_keep <- c("storm_id", "date", "latitude", "longitude", "wind_speed", "ne", "nw", "se", "sw")

	tidy_tracks <- ext_tracks %>%
		tidyr::gather_(key = "key", value = "values", gather_cols = gather_cols) %>%
		tidyr::separate(key, c("radius", "wind_speed", "type")) %>%
		tidyr::spread(key = "type", value = "values") %>%
		dplyr::mutate(
			storm_id = paste(tolower(storm_name), year, sep="-"),
			date = paste(year, month, day, hour) %>% lubridate::ymd_h(),
			longitude = -longitude,
			wind_speed = factor(wind_speed)
		) %>%
		dplyr::select_(.dots = col_to_keep)

	filter_tracks <- function(data = tidy_tracks, storm_name = "ike", dt) {
		data %>%
			dplyr::filter(grepl(paste0("^", storm_name), storm_id)) %>%
			dplyr::filter(date == as.Date(dt))
	}

	storm_observation <- filter_tracks(dt = date)
}
Davidovich4/hurricane documentation built on May 23, 2019, 7:16 a.m.