-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_hatch.R
85 lines (70 loc) · 2.57 KB
/
map_hatch.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
library(tidyverse)
library(raster)
source("calc_hatch_function_slow.R")
# current year
cyear <- format(Sys.Date(), "%Y")
# where to download
downloadfolder <- paste0("../silo/", cyear)
dir.create(downloadfolder, recursive = TRUE, showWarnings = FALSE)
# make function to download silo data into folders sorted by year
download.silo <- function(var, date, downloadfolder = "./") {
date <- as.Date(date, origin = "1970-01-01")
cat(paste("downloading", date, var, "\n"))
dir.create(file.path(downloadfolder, var), showWarnings = F)
year <- format(date, "%Y")
month <- format(date, "%m")
day <- format(date, "%d")
filename <- paste0(year, month, day, ".", var, ".tif")
filepath <- file.path(downloadfolder, var, filename)
url <- file.path(
"https://s3-ap-southeast-2.amazonaws.com/silo-open-data/Official/daily", var, year, filename
)
print(url)
if (!file.exists(filepath)) {
tryCatch(
download.file(url, filepath, quiet = TRUE, mode = "wb"),
error = function(e) e
)
}
}
# download new climate data for current year
vars <- c("daily_rain", "min_temp", "max_temp")
warning("here!")
dates <- seq.Date(as.Date(paste0(cyear, "-01-01")), Sys.Date() - 2, by = 1)
# dates = seq.Date(as.Date(paste0(cyear,"-01-01")), as.Date("2022-05-29"), by = 1)
for (date in dates) {
for (var in vars) {
download.silo(var, date, downloadfolder)
}
}
# build stack of climate data for current year
rainfiles <- list.files(file.path(downloadfolder, "/daily_rain/"), full.names = T)
tminfiles <- list.files(file.path(downloadfolder, "/min_temp/"), full.names = T)
tmaxfiles <- list.files(file.path(downloadfolder, "/max_temp/"), full.names = T)
# set the simulation day by restricting raster stack here for historical sims
RAIN <- stack(rainfiles)
TMIN <- stack(tminfiles)
TMAX <- stack(tmaxfiles)
# load previous hatch simulation
hatchrasterfiles <- list.files("plots/hatchraster/", pattern = "tif$", full.names = T)
hatchrasterfile <- hatchrasterfiles[length(hatchrasterfiles)]
if (length(hatchrasterfiles) == 0) hatchrasterfile <- NULL
# run new simulation with new climate data
hatch <- calc_hatch(TMIN, TMAX, RAIN, hatchrasterfile = hatchrasterfile)
# save model output as raster
h <- RAIN[[1]]
h[] <- hatch
plot(h)
# find the min max date available for each climate data
findmaxdate <- function(filelocation) {
filelocation |>
stringr::str_extract("\\d{8}") |>
as.integer() |>
max()
}
maxsilodate <- min(
findmaxdate(rainfiles),
findmaxdate(tminfiles),
findmaxdate(tmaxfiles)
)
writeRaster(h, sprintf("./plots/hatchraster/hatch_day_at_%s.tif", maxsilodate))