Last active
March 22, 2021 06:28
-
-
Save johnbaums/95f2338b1f52e5b75245335d4579563b to your computer and use it in GitHub Desktop.
Parse HYSPLIT wind trajectory simulation results
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process_hysplit_sims <- function(file) { | |
require(dplyr) | |
require(sf) | |
dat <- readLines(file) | |
starts <- grep('PRESSURE', dat) + 1 | |
ends <- grep('New Simulation', dat) - 2 | |
ends <- c(ends[ends > starts[1]], length(dat)) | |
combined <- mapply(function(s, e, i) { | |
read.fwf(file=textConnection(dat[s:e]), | |
widths=c(6,6,6,6,6,6,6,6,8,9,9,9,9)) | |
}, starts, ends, SIMPLIFY=FALSE) %>% | |
dplyr::bind_rows(.id='sim') %>% | |
setNames(c('sim', 'traj_no', 'grid_no', 'year', 'month', 'day', 'hour', 'min', | |
'forecast_hour', 'traj_age', 'lat', 'lon', 'height', 'pressure')) | |
combined %>% | |
sf::st_as_sf(coords=c('lon', 'lat')) %>% | |
dplyr::group_by(sim) %>% | |
dplyr::summarise(do_union = FALSE) %>% | |
sf::st_cast("LINESTRING") %>% | |
sf::st_set_crs(4326) | |
} |
Author
johnbaums
commented
Mar 22, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment