forked from gerardoalcala/MiniHidroPower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-MiniHidroPower.SinMascaras.R
362 lines (328 loc) · 16.1 KB
/
1-MiniHidroPower.SinMascaras.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# MinihidroPower
# Calculation of MHP potential
# Coded by: Gerardo Alcal'a
# Universidad Veracruzana
# First Version December 19 2018
# Updated December 09 2019
####I. Procesos Iniciales
{
graphics.off()
remove(list=ls())
##setwd("C:/Users/Gerardo Alcal?/Desktop/MiniHidro")
setwd(Sys.getenv("PWD"))
library(sp)
library(raster)
library(rgeos)
library(rgdal)
library(tools)
library(gdistance)
radIntake <- 2000
radTurbina <- 2500
print(paste0("radIntake: ",radIntake," radTurbina: ",radTurbina))
}
###II. Raster y Vectoriales
{
## Leer Rasters y Vectoriales
RasRio <- brick("A-RasterRio.tif")
RasDEM <- raster("B-RasterDEM.tif")
## Homologar Proyecci?n
RasRio <- projectRaster(RasRio,RasDEM)
## Homologar Extension
RasRio <- crop(RasRio,RasDEM)
## Nombres Atributo
names(RasRio) <- c("ID","Gasto","Base","Entrada","Salida")
names(RasDEM) <- "Altura"
}
###III. Malla de puntos
{
## a. Puntos Rio (Points from Raster)
SPPointsRio <- as(RasRio,"SpatialPointsDataFrame")
SPPointsRio <- extract(RasDEM,SPPointsRio,sp=TRUE)
## b. Raster MHP (Variables Calculadas)
RasMHP <- RasDEM
RasMHP$PotenciakW <- NA
RasMHP$GastoMax <- 0
RasMHP$HMax <- 0
RasMHP$xTurbina <- NA
RasMHP$yTurbina <- NA
RasMHP$zTurbina <- NA
RasMHP$BaseTurbina <- NA
RasMHP$xIntake <- NA
RasMHP$yIntake <- NA
RasMHP$zIntake <- NA
RasMHP$BaseIntake <- NA
## c. Raster MHP a Malla de Puntos (OJO! Celdas con puro NA's no generan punto)
SPPointsGrid <- as(RasDEM,"SpatialPointsDataFrame")
if(length(SPPointsGrid)!=ncell(RasMHP)) {
cat("Error, hay celdas del Raster con puro NA's")
stop("FIN")
} else {
cat("No hay NA's en el Raster: El programa puede seguir\n")
}
}
###IV. Funcion de NumCruces. Intersecta Linea Recta con Rio (Sitios Radio)
fNumCruces <- function(NumCoordsRioRadiox,NumCoordsRioRadioy,NumCoordPiletaix,NumCoordPiletaiy)
{
Numxc <- c(NumCoordsRioRadiox, NumCoordPiletaix)
Numyc <- c(NumCoordsRioRadioy, NumCoordPiletaiy)
Matxyc <- cbind(Numxc,Numyc)
SLLineaRecta <- spLines(Matxyc,crs=crs(RasMHP))
SPolLineaRecta <- buffer(SLLineaRecta, width=res(RasMHP)[1]*0.75)
InterCruces <- intersect(SPPointsRioRadio,SPolLineaRecta)
NumCruces <- dim(InterCruces)[1]
}
##x11()
##plot(RasDEM)
##plot(SPPointsRio,col='blue',add=TRUE,pch=19,cex=0.1)
##mult <- 5000
##############################################
###VII INICIA CICLO POR EL GRID
library(doParallel)
##cores=Sys.getenv("SLURM_NTASKS_PER_NODE")
cores=40
print(paste0("Running program with : ",cores[1]," cores."))
print(paste0("Grid size : ",length(SPPointsGrid)))
cl <- makeCluster(cores[1],outfile="")
registerDoParallel(cl)
## for (i in c(1,50000,100000,250000,350000,450000,550000,600000,700000,850000,900000,1050000,1100000,1250000,1300000,1400000,1500000,1600000,1700000,1800000,1900000,2000000,2100000,2200000,2300000)) {
## foreach(i=1:600000) %dopar% {
## foreach(i=c(1,50000,100000,250000,350000,450000,550000,600000,700000,850000,900000,1050000,1100000,1250000,1300000,1400000,1500000,1600000,1700000,1800000,1900000,2000000,2100000,2200000,2300000)) %do% {
ptime <- system.time({
test <- foreach(i=1:length(SPPointsGrid),.combine=rbind) %dopar% {
##test <- foreach(i=c(1,50000,100000,250000,350000,450000,550000,600000,700000,850000,900000,1050000,1100000,1250000,1300000,1400000,1500000,1600000,1700000,1800000,1900000,2000000,2100000,2200000,2300000),.combine=rbind) %dopar% {
PotenciakW <-0
HMax <- 0
GastoMax <- 0
NumCruces <- 0
xTurbina <- 0
yTurbina <- 0
zTurbina <- 0
BaseTurbina <- 0
xIntake <- 0
yIntake <- 0
zIntake <- 0
BaseIntake <- 0
library(sp)
library(raster)
sink("log.txt", append=TRUE)
cat(paste("Starting iteration",i,"\n"))
sink()
### PILETA ###
###1. Inicio: Ubicar la pileta
##if((i/mult)%%1==0){cat("\n Punto", i ,"de",length(SPPointsGrid),"\n")}
SPPointPiletai <- SPPointsGrid[i,]
hi <- SPPointPiletai$Altura
## Coordenadas Pileta (punto actual sobre el DEM)
NumCoordPiletaix <- SPPointPiletai@coords[1]
NumCoordPiletaiy <- SPPointPiletai@coords[2]
##plot(SPPointPiletai,pch=19,col='red',add=TRUE,cex=0.7)
###2. Si rio esta fuera del radio de la Pileta (NEXT)
## a. Crear buffer para Intake y Turbina
SPolRadio <- buffer(SPPointPiletai, width=radTurbina)
SPolRadio2 <- buffer(SPPointPiletai, width=radIntake)
SPPointsRioRadio <- intersect(SPPointsRio,SPolRadio)
SPPointsRioRadio2 <- intersect(SPPointsRio,SPolRadio2)
## b. Verificar si se intersecta el rio, sino terminar Iteracion
if(length(SPPointsRioRadio)==0 | length(SPPointsRioRadio2)==0) {
##SPPointsGrid$PotenciakW[i] <- 0
PotenciakW <- 0
if(length(SPPointsRioRadio)==0){
##SPPointsGrid$HMax[i] <- 0
HMax <- 0
##cat("\n Paso2. Turbina Fuera del Alcance: Hmax =0 ",i)
}
if(length(SPPointsRioRadio2)==0){
##SPPointsGrid$GastoMax[i] <- 0
GastoMax <- 0
##cat("\n Paso2. Intake Fuera del Alcance: Qmax =0",i)
}
##plot(SPPointPiletai,pch=4,col='black',add=TRUE,cex=0.6)
##next
} else {
###3 TURBINA ###
## a. Sitios con menor y mayor altura a la Pileta (else NEXT)
k1 <- which(SPPointsRioRadio$Altura+4 < SPPointPiletai$Altura)
j1 <- which(hi+3<SPPointsRioRadio2$Altura)
## b. En caso que no haya sitios mas bajos o altos que la pileta
if(length(k1)==0 | length(j1)==0) {
##SPPointsGrid$PotenciakW[i] <- 0
PotenciakW <- 0
if(length(k1) ==0){
##SPPointsGrid$HMax[i] <- 0
HMax <- 0
##cat("\n Paso3. No hay sitios mas bajos que la pileta: Hmax=0",i)
}
if(length(j1) ==0){
##SPPointsGrid$GastoMax[i] <- 0
GastoMax <- 0
##cat("\n Paso3. No hay sitios mas altos que la pileta: Qmax=0",i)
}
##plot(SPPointPiletai,pch=4,col='black',add=TRUE,cex=0.6)
##next
} else {
###4. Localizacion de la Turbina
## a. Cruces Turbina
SPPointsTurbina <- SPPointsRioRadio[k1,]
NumCoordsTurbinax <- SPPointsTurbina@coords[,1]
NumCoordsTurbinay <- SPPointsTurbina@coords[,2]
##print(paste0("point",i,"coords",length(NumCoordsTurbinax)))
##SPPointsTurbina$NumCruces <- mapply(fNumCruces,NumCoordsTurbinax,NumCoordsTurbinay,NumCoordPiletaix,NumCoordPiletaiy)
##k2 <- which(SPPointsTurbina$NumCruces ==1)
NumCruces <- mapply(fNumCruces,NumCoordsTurbinax,NumCoordsTurbinay,NumCoordPiletaix,NumCoordPiletaiy)
k2 <- which(NumCruces ==1)
if(length(k2) == 0) {
##SPPointsGrid$PotenciakW[i] <- 0;
PotenciakW <- 0
##SPPointsGrid$HMax[i] <- 0;
HMax <- 0
##plot(SPPointPiletai,pch=4,col='black',add=TRUE,cex=0.6)
##cat("\n Paso3. No hay ruta Turbina que toque 1 sola vez al rio: Hmax=0",i)
##next
} else {
## b.
SPPointsTurbinaUnCruce <- SPPointsTurbina[k2,]
k3 <- which.min(SPPointsTurbinaUnCruce$Altura)
## Posicion con menor altura sin obstaculos
SPPointTurb <- SPPointsTurbinaUnCruce[k3[1],]
## c. Gradiente de altura maximo
HTurb <- SPPointTurb$Altura
##HMax <- hi-HTurb
## d. Guardamos Gradiente Altura maxima
##SPPointsGrid$HMax[i] <- HMax
HMax <- hi-HTurb
## e. Guardamos la coordenada de la turbina
## SPPointsGrid$xTurbina[i] <- SPPointTurb@coords[1]
## SPPointsGrid$yTurbina[i] <- SPPointTurb@coords[2]
## SPPointsGrid$zTurbina[i] <- HTurb
## SPPointsGrid$BaseTurbina[i] <- SPPointTurb$Base
xTurbina <- SPPointTurb@coords[1]
yTurbina <- SPPointTurb@coords[2]
zTurbina <- HTurb
BaseTurbina <- SPPointTurb$Base
###5. Ruta Recta de la Turbina
## if((i/(10*mult))%%1==0) {
##Numxc <- c(SPPointTurb@coords[1], SPPointPiletai@coords[1])
##Numyc <- c(SPPointTurb@coords[2], SPPointPiletai@coords[2])
##Matxyc <- cbind(Numxc,Numyc)
##SLTurbinai <- spLines(Matxyc,crs=crs(RasDEM)) # SpatialLinea
## }
###6. Graficas Turbina
##plot(SPolRadio,border='orange',pch=19,add=TRUE)
##plot(SPPointTurb,col='red',pch=19,add=TRUE)
##lines(SLTurbinai,col='orange',lwd=2)
### DESVIACION ###
###7. El Intake pertenece a la misma rama del rio (else NEXT)
j2 <- (SPPointsRioRadio2$Base==SPPointTurb$Base | SPPointsRioRadio2$Salida==SPPointTurb$Entrada)
SPPointsMismaRamaIntake <- SPPointsRioRadio2[j2,]
###8. Sitios con mayor altura que la pileta (else NEXT)
j3 <- which(hi+3<SPPointsMismaRamaIntake$Altura)
if(length(j3)==0) {
## Guardamos Gasto Maximo
QMax <- 0
##SPPointsGrid$GastoMax[i] <- QMax;
GastoMax <- QMax
##SPPointsGrid$PotenciakW[i] <- 0
PotenciakW <- 0
##plot(SPPointPiletai,pch=4,col='black',add=TRUE,cex=0.6)
##cat("\n Paso9. El intake no tiene altura suficiente: Gasto=0",i)
##next
} else {
###9. Localizacion de Intake
## a. Cruces Intake
SPPointsIntake <- SPPointsMismaRamaIntake[j3,]
NumCoordsIntakex <- SPPointsIntake@coords[,1]
NumCoordsIntakey <- SPPointsIntake@coords[,2]
##SPPointsIntake$NumCruces <- mapply(fNumCruces,NumCoordsIntakex,NumCoordsIntakey,NumCoordPiletaix,NumCoordPiletaiy)
##j4 <- which(SPPointsIntake$NumCruces ==1)
NumCruces <- mapply(fNumCruces,NumCoordsIntakex,NumCoordsIntakey,NumCoordPiletaix,NumCoordPiletaiy)
j4 <- which(NumCruces ==1)
if(length(j4) ==0) {
##SPPointsGrid$PotenciakW[i] <- 0;
PotenciakW <- 0
##SPPointsGrid$GastoMax[i] <- 0;
GastoMax <- 0
##plot(SPPointPiletai,pch=4,col='black',add=TRUE,cex=0.6)
##cat("\n Paso3. No hay ruta Intake que toque 1 sola vez al rio: Hmax=0",i)
##next
} else{
SPPointsIntakeUnCruce <- SPPointsIntake[j4,]
## b. Localizacion Intake
j4 <- which.max(SPPointsIntakeUnCruce$Gasto)
SPPointIntakei <- SPPointsIntakeUnCruce[j4[1],] # Posicion con mayor gasto sin obstaculos
## c. Guardamos Gasto Maximo
QMax <- SPPointIntakei$Gasto
##SPPointsGrid$GastoMax[i] <- QMax
GastoMax <- QMax
## d. Guardamos puntos el Intake
## SPPointsGrid$xIntake[i] <- SPPointIntakei@coords[1]
## SPPointsGrid$yIntake[i] <- SPPointIntakei@coords[2]
## SPPointsGrid$zIntake[i] <- SPPointIntakei$Altura
## SPPointsGrid$BaseIntake[i] <- SPPointIntakei$Base
xIntake <- SPPointIntakei@coords[1]
yIntake <- SPPointIntakei@coords[2]
zIntake <- SPPointIntakei$Altura
BaseIntake <- SPPointIntakei$Base
###10. Linea Intake Pilet
## if((i/(10*mult))%%1==0) {
## Numxc <- c(SPPointIntakei@coords[1], SPPointPiletai@coords[1])
## Numyc <- c(SPPointIntakei@coords[2], SPPointPiletai@coords[2])
## Matxyc <- cbind(Numxc,Numyc)
## SLIntakei <- spLines(Matxyc,crs=crs(RasDEM)) # SpatialLinea
## }
###11. Graficas
##plot(SPolRadio2,add=TRUE,border='black')
##plot(SPPointIntakei,add=TRUE,pch=19,col='yellow')
##lines(SLIntakei,col='yellow',lwd=2)
### POTENCIA ###
###12. Asignacion PotenciakW
##SPPointsGrid$PotenciakW[i] <- 1000*9.81*QMax*HMax/1000
PotenciakW <- 1000*9.81*QMax*HMax/1000
}
}
}
}
}
return(data.frame("index"=i,"PotenciakW"=PotenciakW,"HMax"=HMax,"GastoMax"=GastoMax,"xTurbina"=xTurbina,"yTurbina"=yTurbina,"zTurbina"=zTurbina,"BaseTurbina"=BaseTurbina,"xIntake"=xIntake,"yIntake"=yIntake,"zIntake"=zIntake,"BaseIntake"=BaseIntake))
}
})[3]
stopCluster(cl)
ptime
##print(test)
#### Termina Ciclo for del grid
##################################
### FIN CICLO POR TODO EL GRID ###
##16. Guardar Raster
{
## print("writing table:")
## write.table(test,"1-RasterMHP.txt")
print("merging dataframes:")
SPPointsGrid$index <- 1:length(SPPointsGrid)
SPPointsGrid <- merge(SPPointsGrid,test)
print("writing raster:")
##View(SPPointsGrid@data)
RasFinal <- raster(SPPointsGrid,ncol=ncol(RasMHP),nrow=nrow(RasMHP),ext=extent(RasMHP),res=res(RasMHP),crs=crs(RasMHP))
RasFinal$PotenciakW <- SPPointsGrid$PotenciakW # 1
RasFinal$Altura <- SPPointsGrid$Altura # 2
RasFinal$GastoMax <- SPPointsGrid$GastoMax # 3
RasFinal$HMax <- SPPointsGrid$HMax # 4
RasFinal$xTurbina <- SPPointsGrid$xTurbina # 5
RasFinal$yTurbina <- SPPointsGrid$yTurbina # 6
RasFinal$zTurbina <- SPPointsGrid$zTurbina # 7
RasFinal$BaseTurbina <- SPPointsGrid$BaseTurbina # 8
RasFinal$xIntake <- SPPointsGrid$xIntake # 9
RasFinal$yIntake <- SPPointsGrid$yIntake # 10
RasFinal$zIntake <- SPPointsGrid$zIntake # 11
RasFinal$BaseIntake <- SPPointsGrid$BaseIntake # 12
RasFinal$RadioTurbina <- radTurbina # 13
RasFinal$RadioIntake <- radIntake # 14
RasFinal <- brick(RasFinal)
## Con todas las columnas de Atributos
x <- writeRaster(RasFinal, '1-RasterMHP.tif', overwrite=TRUE)
dffinal <- data.frame(SPPointsGrid)
archivof1 <- paste("DatosMHP.csv",sep="")
write.csv(dffinal, file = archivof1,row.names=FALSE)
##View(dffinal)
##x11()
##plot(RasFinal$PotenciakW)
##plot(SPPointsRio, col='blue', add=TRUE)
}