Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsla committed Jul 26, 2023
1 parent 4336778 commit f70ef28
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 58 deletions.
45 changes: 33 additions & 12 deletions R/wind_ca_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ wind_sim_hist <- function(data_u = NULL,
fhour = "18", # the forecast hour
cellsize = 25000,
full = F, # if TRUE, generate a dataframe of endpoints per time step
backwards = F, # if FALSE (default) run forwards simulation from starting point. Else backwards from end point
parallel = F, # if TRUE run in parallel
ncores = F){ # if FALSE (default) use max number of cores - 1. Else set to number of cores to use

Expand All @@ -380,15 +381,29 @@ wind_sim_hist <- function(data_u = NULL,

r <- data_u[[1]]

fhour_utc = ifelse(as.numeric(fhour) %in% c(0:5), "00",
ifelse(as.numeric(fhour) %in% c(6:11), "06",
ifelse(as.numeric(fhour) %in% c(12:17), "12",
ifelse(as.numeric(fhour) %in% c(18:23), "18",
NA))))
if(is.na(fhour_utc))
stop("fhour should be between 0-23")
# define the interval
interval <- c("00", "06", "12", "18")

if(backwards){
# update date and time based on length of hindcast
fdate = as.character(format(as.POSIXct(lubridate::ymd_h(paste(fdate, fhour)) - lubridate::hours(as.numeric(nforecast)),format='%m/%d/%Y %H:%M:%S'),format='%Y%m%d'))

fhour = as.character(lubridate::hour(lubridate::hours(as.numeric(fhour)) - lubridate::hours(nforecast)) %% 24)
}

# find closest interval
diffs <- as.numeric(fhour) - as.numeric(interval)

f_offset = as.numeric(fhour) - as.numeric(fhour_utc)
diffs[which(diffs < 0)] <- NA

# calculate the index of the closest interval
index <- which.min(diffs)

# calculate the difference
difference <- as.numeric(fhour) - as.numeric(interval[index])

# get the corresponding interval
fhour <- interval[index]

if(parallel){
require(doSNOW)
Expand Down Expand Up @@ -454,7 +469,7 @@ wind_sim_hist <- function(data_u = NULL,
x <- points[n, 1]
y <- points[n, 2]

start_layer <- which(names(data_u) == paste0(fdate, fhour_utc))
start_layer <- which(names(data_u) == paste0(fdate, fhour))

u <- data_u[[start_layer + floor(f_new/6)]]
v <- data_v[[start_layer + floor(f_new/6)]]
Expand Down Expand Up @@ -558,14 +573,17 @@ wind_sim_hist <- function(data_u = NULL,

n <- 1

for(f in seq_len(nforecast)){
forecast_hours <- seq_len(nforecast)
if(backwards) forecast_hours <- rev(forecast_hours)

f_new <- f + f_offset
for(f in forecast_hours){

f_new <- f + difference

x <- points[n, 1]
y <- points[n, 2]

start_layer <- which(names(data_u) == paste0(fdate, fhour_utc))
start_layer <- which(names(data_u) == paste0(fdate, interval[index]))

u <- data_u[[start_layer + floor(f_new/6)]]
v <- data_v[[start_layer + floor(f_new/6)]]
Expand All @@ -574,6 +592,9 @@ wind_sim_hist <- function(data_u = NULL,
speed <- wind_speed(u = u, v = v)
direction <- wind_direction(u = u, v = v)

if(backwards)
direction <- (direction + 180) %% 360

speed_ctr <- speed[y, x][1,1]

## calculate the number of steps based on wind speed and cell size
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,30 @@ Meteorological forecast cycles in 6 hour intervals (UTC 00:00, 06:00, 12:00, 18:

### Using the forecasting tool

To run a simulation, you need to select among several parameters:
The default use of the tool is to perform a **Rapid simulation** based on user-defined input. You will be prompted to select among several parameters:

![](forecast_tool_direction.png)

1. Select whether to run the simulation forwards (i.e. showing where the moths are predicted to disperse to) or backwards (i.e. predicting where the moths came from).

![](forecast_tool_time.png)

2. Forecast cycle: Select a timezone and date and time for your simulation. You can run simulations ranging up to a week prior to the current date.
2. Select a timezone and date and time for your simulation. You can run simulations ranging up to a week prior to the current date.

![](forecast_tool_duration.png)

3. Select the total flight duration of FAW, up to 24 hours.
3. Select the total flight duration of FAW, up to 24 hours for forward simulation, up to 96 hours (four days) for backward simulation.

![](forecast_tool_locality.png)

4. Locality: The location of moths present to predict their direction of travel (forwards) or their predicted origin (backwards). You can either input longitude and latitude manually (in decimal degrees), or click on the map to select a locality for the simulation.
4. Locality: The location of moths present to predict their direction of travel (forwards) or their predicted origin (backwards). You can click on the map to select up to five localities for the simulation. Delete selected localities by pressing on the adjacent button.

*Note that longer total run times and greater numbers of simulations will result in longer computation times, but simulations should still finish in under four minutes at the most. If more than five minutes have passed and no output has been created, please refresh and try again. If the error persists, contact Cesar Australia for support.*
*Note that longer total run times and greater numbers of localities will result in longer computation times, up to three minutes for forwards simulations and up to 11 minutes for backwards simulations. If that time has passed and no output has been created, please refresh and try again. If the error persists, contact Cesar Australia for support.*

After you have selected all of your starting conditions, press the **Run forecast** button. After a short wait the results of your simulation will show up on the right, with cells coloured based on the frequency of simulated trajectories passing through them.

Alternatively, you can switch to the **Multi-site Northern Prediction** tab to simulate overnight dispersal from localities where there is a year-round risk of FAW. Select your timezone of choice, and press the **Run forecast** button to generate a map of where FAW may have dispersed to overnight. This should take roughly eight minutes to produce.

<br>

For more information on surveillance and how to monitor for and manage FAW see:
Expand Down
83 changes: 42 additions & 41 deletions app.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,52 +57,14 @@ tz_choices <- c(

ui <- shinyUI(
navbarPage(
"Wind Forecast Tool v0.5.2",
selected = "Rapid Prediction",
"Wind Forecast Tool v1.0.0",
selected = "Select location for rapid simulation",
theme = shinytheme("yeti"),

# Panel 1 -----------------------------------------------------------------
tabPanel(
"Rapid Prediction",

column(
width = 4,

h5("Select timezone:"),
selectInput(
"timezone_r",
"Timezone",
choices = tz_choices,
selected = "Australia/Melbourne"
),

h5("Simulate overnight wind-assisted dispersal from the following localities:"),

leafletOutput("map_r", height = 250),

h6(
"* Simulation should take approximately eight minutes"
)

),
column(
width = 8,

HTML("<br/>"),
actionButton("run_r", "Run forecast"),
HTML("<br/>"),

plotOutput("prediction_r", height = "500px") %>%
withSpinner(color = "#428bca")

)
)
,

# Panel 2 -----------------------------------------------------------------

tabPanel(
"Custom Simulation",
"Select location for rapid simulation",

column(
width = 4,
Expand Down Expand Up @@ -178,6 +140,45 @@ ui <- shinyUI(

)
)
,

# Panel 2 -----------------------------------------------------------------

tabPanel(
"Multi-site Northern Prediction",

column(
width = 4,

h5("Select timezone:"),
selectInput(
"timezone_r",
"Timezone",
choices = tz_choices,
selected = "Australia/Melbourne"
),

h5("Simulate overnight wind-assisted dispersal from the following localities:"),

leafletOutput("map_r", height = 250),

h6(
"* Simulation should take approximately eight minutes"
)

),
column(
width = 8,

HTML("<br/>"),
actionButton("run_r", "Run forecast"),
HTML("<br/>"),

plotOutput("prediction_r", height = "500px") %>%
withSpinner(color = "#428bca")

)
)

)
)
Expand Down

0 comments on commit f70ef28

Please sign in to comment.