-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMEDGOLD_2Granoduro_v1.0.R
153 lines (113 loc) · 3.97 KB
/
MEDGOLD_2Granoduro_v1.0.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#Clean all
rm(list=ls())
library(s2dverification)
library(SpecsVerification)
library(abind)
library(multiApply)
library(easyVerification)
library(ncdf4)
library(CSTools)
library(zeallot)
library(lubridate)
#Set global parameters. These parameters will not change
start_time <- Sys.time()
dpath <- '/home/sandro/DATA/PROJECTS/MEDGOLD/GRANODURO/BC/DAILY/'
dayst <- '01'
monst <- '11'
yrst <- 1988
yren <- 2017
sdates <- paste0(as.character(seq(yrst,yren)),monst,dayst)
exp_name <- 'ecmf'
nc_domain_fname <- 'it-medgold'
grids <- list(
list(name='RAVENNA',
lat=44.48,
lon=12.17),
list(name='JESI',
lat=43.53,
lon=13.27),
list(name='FOGGIA',
lat=41.34,
lon=15.56)
)
for (igrid in seq(1:length(grids))) {
glat <- grids[[igrid]]$lat
glon <- grids[[igrid]]$lon
fi_var_names <- c('tmin2m','tmax2m','totprec')
nc_var_names <- c('mn2t24', 'mx2t24', 'tp')
nc_var_longnames <-
c('Minimum temperature at 2 metres in the last 24 hours',
'Maximum temperature at 2 metres in the last 24 hours',
'Total precipitation'
)
nc_var_units <- c('K', 'K', 'm')
nvars <- length(nc_var_units)
#Extract variables at the selcted grid first.
#The zip file for each year, with all variables, is created in a separated loop
# after the loop over vars
for (ivar in seq(1:nvars)) {
fi_var_name <- fi_var_names[ivar]
nc_var_name <- nc_var_names[ivar]
nc_var_longname <- nc_var_longnames[ivar]
nc_var_unit <- nc_var_units[ivar]
exp <- list(
list(
name = exp_name,
path = file.path('/home/sandro/DATA/SEASONAL/ECMF/BC/',
'$VAR_NAME$_$EXP_NAME$_$START_DATE$_$SUFFIX$.nc'),
nc_var_name = nc_var_name,
suffix = nc_domain_fname,
var_min = '-300'
)
)
c(exp) %<-% CST_Load(var=fi_var_name,
exp=exp,obs=NULL,
sdates=sdates,
storefreq='daily',
output='lonlat',
nprocs=8)
#Check dimensions
nsdates <- dim(exp$data)['sdate']
ftime <- dim(exp$data)['ftime']
nmem <- dim(exp$data)['member']
nlon <- dim(exp$data)['lon']
nlat <- dim(exp$data)['lat']
for ( idate in seq(1:nsdates)) {
#Define starting year and month for the file name
isdate <- 1 + (idate - 1) * ftime
iedate <- idate * ftime
sdate <- exp$Dates[[1]][isdate]
edate <- exp$Dates[[1]][iedate]
csyear <- sprintf('%g',year(sdate))
csmonth <- sprintf('%g',month(sdate))
zipname <- paste0(dpath,paste(exp_name,paste(glat,glon,sep='-'),csyear,csmonth,paste(fi_var_names,collapse='_'),sep='_'),'.zip')
dirname <- paste0(dpath,csyear,csmonth,fi_var_name,'/')
unlink(dirname,recursive=TRUE)
dir.create(dirname)
for ( iens in seq(1:nmem)) {
cens <- sprintf('%g',iens)
filename <- paste0(dirname,exp_name,'_',csyear,csmonth,fi_var_name,'_',cens,'.csv')
print(filename)
exp_tosave <- exp$data[1,iens,idate,,which.min(abs(glat-exp$lat)),which.min(abs(glon-exp$lon))]
dates_tosave <- as.Date(exp$Dates[[1]][isdate:iedate])
data_tosave <- cbind.data.frame(dates_tosave,exp_tosave)
names(data_tosave) <- c()
write.csv(format(data_tosave,digits=1,scientific=FALSE),filename,append=FALSE,sep=',', row.names=FALSE,col.names=c(),quote=FALSE)
}
}
}
#Create the ZIP file
setwd(dpath)
for ( idate in seq(1:nsdates)) {
#Define starting year and month for the file name
isdate <- 1 + (idate - 1) * ftime
iedate <- idate * ftime
sdate <- exp$Dates[[1]][isdate]
edate <- exp$Dates[[1]][iedate]
csyear <- sprintf('%g',year(sdate))
csmonth <- sprintf('%g',month(sdate))
zipname <- paste0(dpath,'ZIP/',paste(exp_name,paste(glat,glon,sep='-'),csyear,csmonth,paste(fi_var_names,collapse='_'),sep='_'),'.zip')
zip_files <- paste0('./',dir(path='.',pattern=paste0('^',csyear,csmonth)))
zip(zipname,zip_files,zip='/usr/bin/zip')
}
} # End loop over grids