-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaverage_x_years_ncdf.R
44 lines (40 loc) · 1.7 KB
/
average_x_years_ncdf.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
average_x_years_ncdf <- function(ftp_folder_input,
year_start, year_end){
# Function to make the baseline based on content of ftp folder
# for a specified year range
#Download folder
source("download_from_ftp.R")
saving_folder <- paste0("data/",ftp_folder_input)
download(ftp_folder_input,saving_folder)
begin <- as.Date(paste0(year_start,"-01-01"))
end <- as.Date(paste0(year_end,"-12-31"))
#Read all files
all_files <- list()
result <- list()
files <- dir(saving_folder, full.names=TRUE)
for(i in seq_along(files)){
r <- stack(files[i])
d <- as.Date(gsub("^X","",names(r)),
tryFormats = c("%Y-%m-%d", "%Y/%m/%d","%Y.%m.%d")) #Can we assume date to be in one of those formats?
r <- subset(r, which(d>=begin & d<=end)) #Get rid of dates outside range
r <- subset(r, which(format(d,"%m.%d")!="02.29")) #Get rid of 29th of february if any
if(nlayers(r)>0) all_files[[i]] <- r
}
all_files <- all_files[sapply(all_files,length)]
stacked <- stack(all_files)
for(j in 1:365){
d <- format(as.date(paste0("2015-",j),"%Y-%j"),"%m-%d") #2015 as it's a typical non-leap year
n <- as.Date(gsub("^X","",names(r)),
tryFormats = c("%Y-%m-%d","%Y/%m/%d","%Y.%m.%d"))
s <- subset(stacked, which(format(n,"%m-%d")==d))
result[[j]] <- calc(s, mean)
}
out <- stack(result)
if(out$xmax==360) out <- rotate(out)
#Delete the files stored locally to make room. Do we want that?
unlink(saving_folder,recursive=TRUE)
#output the raster object (not file)
out
}
#writeRaster(out, paste0("data/",output_file),"netCDF")
#upload(output_file, ftp_folder_output, output_file) #where do we want to save it?