Skip to content

Instantly share code, notes, and snippets.

@johnbaums
Last active March 22, 2021 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnbaums/95f2338b1f52e5b75245335d4579563b to your computer and use it in GitHub Desktop.
Save johnbaums/95f2338b1f52e5b75245335d4579563b to your computer and use it in GitHub Desktop.
Parse HYSPLIT wind trajectory simulation results
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)
}
@johnbaums
Copy link
Author

johnbaums commented Mar 22, 2021

u <- 'https://www.ready.noaa.gov/hypubout/user.114496/trajdump_114496.txt'
readLines(u, n=20)

## > readLines(u, n=20)
##  [1] "---------------------------------"                                                           
##  [2] "       New Simulation 1     "                                                                
##  [3] "---------------------------------"                                                           
##  [4] "---------------------------------"                                                           
##  [5] "       New Simulation 3     "                                                                
##  [6] "---------------------------------"                                                           
##  [7] "     4     1"                                                                                
##  [8] "    GDAS    21     2    15     0     0"                                                      
##  [9] "    GDAS    21     2    22     0     0"                                                      
## [10] "    GDAS    21     3     1     0     0"                                                      
## [11] "    GDAS    21     3     8     0     0"                                                      
## [12] "     1 BACKWARD OMEGA   "                                                                    
## [13] "    21     3    14    17  -12.418  133.760   500.0"                                          
## [14] "     1 PRESSURE"                                                                             
## [15] "     1     1    21     3    14    17     0     1     0.0  -12.418  133.760    500.0    940.9"
## [16] "     1     1    21     3    14    16     0     2    -1.0  -12.335  133.808    458.1    946.9"
## [17] "     1     1    21     3    14    15     0     3    -2.0  -12.233  133.859    394.6    957.2"
## [18] "     1     1    21     3    14    14     0     2    -3.0  -12.117  133.910    326.5    969.6"
## [19] "     1     1    21     3    14    13     0     1    -4.0  -12.001  133.964    262.3    980.2"
## [20] "     1     1    21     3    14    12     0     0    -5.0  -11.894  134.021    208.9    987.3"
dat <- process_hysplit_sims(u)
mapview::mapview(dat, color='black', alpha=0.3, legend=FALSE)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment