-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain_functions.R
207 lines (140 loc) · 6.39 KB
/
main_functions.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
##############################################################################
########################## Condiciones iniciales #############################
##############################################################################
initial_conditions <- function(data, system) {
if(system == "rainfed"){
## Extraer Condiciones iniciales de los archivos de suelos
SOIL <- readLines(paste(data)) ## Archivo de Suelo
profiles <- grep("SLB", SOIL) ## Posicion en la que coincide con las variables a extraer
imp.head <- scan(paste(data), what = "character", skip = profiles[1] - 1, nlines = 1, quiet = T) # Encabezado (titulo para las columnas)
seps <- c(6, -6, 6) ## Separadores para las variables
Initial_conditions <- read.fwf("SOIL.SOL", width = seps, header = F, skip = profiles[1], n = profiles[2] - profiles[1] - 1)
pat <- "SLB|SDUL"
headers <- imp.head[grep(pat, imp.head, perl = TRUE)]
## Borrar easta linea cuando no se corra Papa Secano
#Initial_conditions <- data.frame(Initial_conditions[, 1], rep(-99, dim(Initial_conditions)[1]))
colnames(Initial_conditions) <- headers
return(Initial_conditions)
}
if(system == "irrigation"){
## Extraer Condiciones iniciales de los archivos de suelos
SOIL <- readLines(paste(data)) ## Archivo de Suelo
profiles <- grep("SLB", SOIL) ## Posicion en la que coincide con las variables a extraer
imp.head <- scan(paste(data), what = "character", skip = profiles[1] - 1, nlines = 1, quiet = T) # Encabezado (titulo para las columnas)
seps <- c(6, -6, 6) ## Separadores para las variables
Initial_conditions <- read.fwf("SOIL.SOL", width = seps, header = F, skip = profiles[1], n = profiles[2] - profiles[1] - 1)
pat <- "SLB|SDUL"
headers <- imp.head[grep(pat, imp.head, perl = TRUE)]
Initial_conditions <- data.frame(Initial_conditions[, 1], rep(-99, dim(Initial_conditions)[1]))
colnames(Initial_conditions) <- headers
return(Initial_conditions)
}
}
##############################################################################
########################## Convert day for DSSAT #############################
##############################################################################
convert_date <- function(date, year) {
## esta es la funcion que se agrega pero sino sirve 60 dias antes dejar como estaba antes
year_simul <- year
year <- year + 1
if(date <= 0){
date1 <- 365 + date
date1 <- paste0(year_simul, date1)
}
############################################# lo que funcionaba
if(date < 10 & date > 0) {
date1 <- paste0(year, "00", date)
}
if(date < 100 & date >= 10 & date > 0) {
date1 <- paste0(year, "0", date)
}
if(date >= 100 & date > 0) {
date1 <- paste0(year, date)
}
return(date1)
}
##############################################################################
########################## Settings leap year ################################
##############################################################################
leap_year <- function(year) {
## Settings leap year
## yrs to create the label .WTH
## yrs2 Year Julian days
if((year %% 4) == 0) {
if((year %% 100) == 0) {
if((year %% 400) == 0) {
# print(paste(year,"is a leap year"))
yrs <- year
yrs2 <- (yrs * 100):((yrs * (100)) + 366)
yrs <- (yrs * 1000):((yrs * (1000)) + 366)
yrs <- yrs[-1]
yrs2 <- yrs2[-1]
} else {
# print(paste(year,"is not a leap year"))
yrs <- year
yrs2 <- (yrs * 100):((yrs * (100)) + 365)
yrs <- (yrs * 1000):((yrs * (1000)) + 365)
yrs <- yrs[-1] ## day 00 is not possible
yrs2 <- yrs2[-1] ## day 00 is not possible
}
} else {
# print(paste(year,"is a leap year"))
yrs <- year
yrs2 <- (yrs * 100):((yrs * (100)) + 366)
yrs <- (yrs * 1000):((yrs * (1000)) + 366)
yrs <- yrs[-1] ## day 00 is not possible
yrs2 <- yrs2[-1] ## day 00 is not possible
}
} else {
# print(paste(year,"is not a leap year"))
yrs <- year
yrs2 <- (yrs * 100):((yrs * (100)) + 365)
yrs <- (yrs * 1000):((yrs * (1000)) + 365)
yrs <- yrs[-1] ## day 00 is not possible
yrs2 <- yrs2[-1] ## day 00 is not possible
}
years <- list(yrs, yrs2)
names(years) <- c("yrs", "yrs2")
return(years)
}
##############################################################################
########################## Leer Summary.OUT DSSAT ############################
##############################################################################
# path <- "/home/jeisonmesa/Proyectos/BID/DSSAT/bin/csm45_1_23_bin_ifort"
# setwd(path)
#
# pat <- "SDAT|PDAT|ADAT|MDAT|IRCM|HWAH|HIAM|EPCM|NICM|NDCH|PRCP|ETCP|CWAM"
# imp.head <- scan("Summary.OUT", what = "character", skip = 3, nlines = 1, quiet = T)
# headers <- imp.head[grep(pat, imp.head, perl = TRUE)]
#
# # Read in main table in fixed width format
# #seps <- c(-92, 8, 8, -8, 8, 8, -30, 8, -36, 6, -30, 6, -315, 7, 7)
# seps <- c(-92, 8, 8, -8, 8, 8, -14, 8, -8, 8, -36, 6, -12, 6, -12, 6, -30, 6, -242, 6, -31, 7, 7)
#
# imp.dat <- read.fwf("Summary.OUT", width = seps, skip = 4, header = F, sep = "")
# colnames(imp.dat) <- headers
################## Leer Overview.out #######################################
read.overview <- function(...){
overview <- readLines("OVERVIEW.OUT")
stress <- grep("CROP GROWTH", overview)
imp.head <- scan("OVERVIEW.OUT", what = "character", skip = stress[1], nlines = 1, quiet = T)
path <- getwd()
seps <- c(-1, 6, 5, 11, 8, 7, 6, 5, 5, 6, 6, 6, 6, 6)
length.stress <- function(datos, path){
k<-0
test <- try(strsplit(overview[datos + 3],split = path),silent = TRUE)
while(!is.na(length(test)==1 && test[[1]]!="")){
test <- try(strsplit(overview[datos+k],split = path),silent = TRUE)
k<- k+1
}
return(k-2)
}
year.overview <- lapply(1:length(stress), function(i) read.fwf("OVERVIEW.OUT", width = seps, skip = stress[i] + 2, header = F, n = length.stress(stress[i], getwd()) - 2))
headers <- function(datos){
colnames(datos) <- imp.head
return(datos)
}
year.overview <- lapply(year.overview, headers)
return(year.overview)
}
###