R/tidy_storm.R

Defines functions tidy_storm

Documented in tidy_storm

#' @title Tidy Storm Data
#' @description Tidy up the storm data for mapping purpose.
#' @param storm The name of the storm
#' @param storm_date The date and time of when the storm occured, type in specific date up to hour, further input (min and sec) will throw an error.
#' @return A tibble of organized storm data
#' @examples tidy_storm("Katrina", "2005-08-29 12")
#' @importFrom lubridate ymd_hms make_datetime year
#' @importFrom stringr str_to_upper str_to_title str_extract
#' @importFrom dplyr mutate filter select group_by summarize
#' @importFrom tidyr gather
#' @importFrom magrittr %>%
#' @export
tidy_storm <- function(storm,storm_date) {
  dat <-  parse_dat()
  #Parsed Time, truncated = 2 becasue m&s is not specified in the data (default 00:00)
  parsed_time <- lubridate::ymd_hms(storm_date, truncated = 2)
  #Tidying Data Format
  storm <- stringr::str_to_upper(storm)

  storm_observation <- dat %>%
    dplyr::mutate(date = lubridate::make_datetime(as.integer(year),
                                                  as.integer(month),
                                                  as.integer(day),
                                                   as.integer(hour))) %>%
    dplyr::filter(storm_name == storm, date == parsed_time) %>%
    dplyr::select(-c(storm_id, max_wind, min_pressure, rad_max_wind, eye_diameter, pressure_1,
                  pressure_2, storm_type, distance_to_land, final, day, hour, year, month)) %>%
    tidyr::gather(knots_direction, values, -latitude,-longitude, -date, -storm_name) %>%
    dplyr::mutate(
           storm_name_adjusted = stringr::str_to_title(storm_name),
           longitude = -longitude,
           storm_id = paste0(storm_name_adjusted[1], "-", lubridate::year(date)),
           wind_speed = stringr::str_extract(knots_direction,"34|50|64")) %>%
    dplyr::group_by(storm_id, date, latitude, longitude, wind_speed) %>%
    dplyr::summarize(ne = values[1], nw = values[4], se = values[2], sw = values[3])
  #Hind sight... code too complicated... need imrpovements
  storm_observation
}
mykoCheng/ANewGeom documentation built on May 5, 2019, 12:31 p.m.