stormwindmodel icon indicating copy to clipboard operation
stormwindmodel copied to clipboard

Consider separating conversions and interpolation steps in track interpolation

Open geanders opened this issue 4 years ago • 0 comments

This is in create_full_track.

Currently, it includes some parts that are to convert track data to a common standard before running the wind model:

  • Convert the max. winds from knots to meters per second (this one will depend on the units the track data start in---always knots in different basins?)
  • Add 360 to longitude if it's below -180, then to all longitudes multiple -1 (this one will depend on the conventions used for longitude in the input tracking data)
create_full_track <- function(hurr_track = stormwindmodel::floyd_tracks,
                              tint = 0.25){
  hurr_track <- dplyr::select(hurr_track, .data$date, .data$latitude,
                               .data$longitude, .data$wind) %>%
    dplyr::rename(vmax = .data$wind,
                  tclat = .data$latitude,
                  tclon = .data$longitude) %>%
    dplyr::mutate(date = lubridate::ymd_hm(.data$date),
                  tclat = abs(as.numeric(.data$tclat)),
                  tclon = as.numeric(.data$tclon),
                  tclon = ifelse(.data$tclon > -180, .data$tclon, .data$tclon + 360), # Stuff with longitude conventions here and next line
                  tclon = -1 * .data$tclon,
                  vmax = weathermetrics::convert_wind_speed(.data$vmax, "knots",  # Unit conversion here
                                                            "mps", round = 3),
                  track_time_simple = difftime(.data$date, dplyr::first(.data$date),
                                               units = "hour"),
                  track_time_simple = as.numeric(.data$track_time_simple))

  full_track <- hurr_track %>%
    tidyr::nest(data = tidyr::everything()) %>%
    # Create times to interpolate to
    dplyr::mutate(interp_time = purrr::map(.data$data,
                                           .f = ~ seq(from = dplyr::first(.x$track_time_simple),
                                                      to = dplyr::last(.x$track_time_simple),
                                                      by = tint))) %>%
    # Interpolate latitude and longitude using natural cubic splines
    dplyr::mutate(tclat = purrr::map2(.data$data, .data$interp_time,
                                      .f = ~ spline(x = .x$track_time_simple,
                                                    y = .x$tclat,
                                                    xout = .y,
                                                    method = "natural")$y)) %>%
    dplyr::mutate(tclon = purrr::map2(.data$data, .data$interp_time,
                                      .f = ~ spline(x = .x$track_time_simple,
                                                    y = .x$tclon,
                                                    xout = .y,
                                                    method = "natural")$y)) %>%
    # Interpolate max wind using linear interpolation
    dplyr::mutate(vmax = purrr::map2(.data$data, .data$interp_time,
                                     .f = ~ approx(x = .x$track_time_simple,
                                                   y = .x$vmax,
                                                   xout = .y)$y)) %>%
    dplyr::mutate(date = purrr::map2(.data$data, .data$interp_time,
                                    .f = ~ dplyr::first(.x$date) +
                                      lubridate::seconds(3600 * .y))) %>%
    dplyr::select(.data$date, .data$tclat, .data$tclon, .data$vmax) %>%
    tidyr::unnest(.data$date:.data$vmax)

  return(full_track)
}

geanders avatar Apr 12 '21 23:04 geanders