-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapply_extreme_function.R
More file actions
24 lines (22 loc) · 994 Bytes
/
apply_extreme_function.R
File metadata and controls
24 lines (22 loc) · 994 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
applyExtreme <- function(variable_file, function_file, output_name, ftp_directory_output){
# Function to apply the extreme functions to the variable (last step)
# Name of the variable file
# Name of the file to the function to apply
# Name of the output file
require(raster)
source("upload_to_ftp.R")
download(variable_file,"/data/variables/",folder=FALSE)
r <- stack(paste0("/data/variables/",variable_file))
variable <- stack2df(r)
#Apply function
e <- new.env()
source(function_file, local=e) #We can t assume the name of the function inside,
func <- base::get(ls(env=e),env=e) #So we do witchcraft to get it
output <- data.frame(x=variable$x,y=variable$y)
output$fd <- apply(variable[,-(1:2)],1,func)
output_rl <- rasterfromXYZ(output)
if(!dir.exists("output")) dir.create("output")
writeRaster(output_rl, paste0("output/",output_name),"netCDF")
#Load output on ftp server
upload(paste0("output/",output_name), ftp_directory_output, output_name)
}