-
Notifications
You must be signed in to change notification settings - Fork 0
/
model1c.R
154 lines (134 loc) · 6.63 KB
/
model1c.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
# Author: B.P. Ottow
################################# Model starts ############################################
HydroPowerMonthly <- function(DEMfile = "input/DEM.tif", Pdir = "input/P", ETdir = "input/ET", coord,
channelLength = 500, minimumHead = 25, minimumDebiet = 0.1, minimumPotential = 10000,
plotToGE = TRUE, work_env = "", plotMethod = "raster") {
# load in input and packages
require(raster)
require(rgdal)
require(sp)
require(RSAGA)
if(class(work_env) == "character"){
work_env <- rsaga.env()
}
dir.create("step")
dir.create("output")
rasterOptions(tmpdir=paste(getwd(),"step", sep="/"))
ETfiles <- list.files(ETdir, pattern=".tif", full.names=TRUE)
Pfiles <- list.files(Pdir, pattern=".tif", full.names=TRUE)
noSteps <- min(length(ETfiles), length(Pfiles))
ET <- stack(ETfiles[1:noSteps])
P <- stack(Pfiles[1:noSteps])
# Fill and clip DEM:
source("scripts/watershed.R")
filledDEM <- getCatchment(DEMfile, coord, work_env)
writeRaster(filledDEM, filename="step/filledDEM.tif", format="GTiff", overwrite=TRUE)
rsaga.geoprocessor(lib="io_gdal", module=0,param=list(GRIDS=paste(getwd(),"step/filledDEM.sgrd",sep="/"),
FILES=paste(getwd(), "step/filledDEM.tif",sep="/"),
TRANSFORM=TRUE, INTERPOL=1),
env=work_env, show.output.on.console = FALSE, warn = FALSE)
# runoff calc
print("calculating runoff...")
Presample <- resample(P, ET, method='bilinear')
runoff <- Presample - ET
runoffRes <- resample(runoff, filledDEM, method='bilinear')
for (i in 1:noSteps){
writeRaster(runoffRes[[i]], filename=sprintf("step/runoffRes%02d.tif",i), format="GTiff", overwrite=TRUE)
}
# Flow accumulation
print("calculating flow accumulation...")
for (i in 1:noSteps) {
rsaga.geoprocessor(lib="io_gdal", module=0,param=list(GRIDS=sprintf("step/runoffRes%02d.sgrd",i),
FILES=sprintf("step/runoffRes%02d.tif",i),
TRANSFORM=TRUE, INTERPOL=1),
env=work_env, show.output.on.console = FALSE, warn = FALSE)
file.copy(c("step/filledDEM.sgrd", "step/filledDEM.sdat", "step/filledDEM.prj", "step/filledDEM.mgrd"),
c("step/runoffResa.sgrd", "step/runoffResa.sdat", "step/runoffResa.prj", "step/runoffResa.mgrd"))
rsaga.geoprocessor(lib="grid_tools", module=0,
param=list(INPUT=sprintf("step/runoffRes%02d.sgrd",i), KEEP_TYPE=TRUE,
TARGET=1, SCALE_UP_METHOD=1, SCALE_DOWN_METHOD=1,
GRID_GRID="step/runoffResa.sgrd"),
env=work_env, show.output.on.console = FALSE, warn = FALSE)
rsaga.geoprocessor(lib="grid_analysis", module=18,
param=list(SURFACE="step/filledDEM.sgrd", INPUT="step/runoffResa.sgrd",
FLUX=sprintf("step/cell_acc%02d.sgrd",i), OPERATION=0),
env=work_env, show.output.on.console = FALSE, warn = FALSE)
print(sprintf("%d step(s) finished", i))
file.remove(c("step/runoffResa.sgrd", "step/runoffResa.sdat", "step/runoffResa.prj", "step/runoffResa.mgrd"))
}
cellAcc <- stack(sprintf("step/cell_acc%02d.sdat", 1:noSteps))
debiet <- calc(cellAcc, fun=function(x) x / 1000 / 24 / 3600 * res(cellAcc)[1] ^ 2)
# Runoff delay scenarios
source("scripts/runoffStorage.R")
factors <- list(0.6,0.3)
runoffs <- lapply(factors,FUN=storage.fun,debiet=debiet,noSteps=noSteps)
runoffs <- c(debiet, runoffs)
##poi <- SpatialPoints(matrix(c(737522.424973, 706557.393475),nrow=1),
## proj4string=CRS(projection(debiet[[1]])))
#poi <- SpatialPoints(coord,
# proj4string=wgs84)
#poi <- spTransform(poi, utm51)
#testplot.storages(runoffs,debiet,poi, factors)
# head
print("calculating head...")
source("scripts/costrasters.R")
head <- HeadOnRiver.large(filledDEM, max(debiet), minimumDebiet=minimumDebiet,
channelLength=channelLength, cores=6)
writeRaster(head, filename="step/head.tif", format="GTiff", overwrite=TRUE)
#head <- raster("step/head.tif")
# hydro potential
print("calculating potential...")
runoffs <- lapply(runoffs, FUN=crop, y=head)
head[head < minimumHead] <- NA
potentials <- lapply(runoffs, FUN=function(x, head) {x * head * 9.81 * 1000}, head=head) # Joule / second
# filters
# filter.fun <- function(pot, deb, head, minDeb, minHead){
# pot[deb < minDeb] <- NA
# pot[head < minHead] <- NA
# pot
# }
# potentials <- mapply(FUN=filter.fun, pot=potentials, deb=runoffs,
# MoreArgs=list(head=head, minDeb=minimumDebiet, minHead=minimumHead),
# SIMPLIFY=FALSE)
#names <- lapply(1:3,FUN=function(x){sprintf("output/potential%i_%02d.tif", x, 1:noSteps)})
#mapply(writeRaster, potentials, names, MoreArgs=list(bylayer=TRUE, format="GTiff",
# overwrite=TRUE))
#potentials <- lapply(X=names, FUN=stack)
highPotentials <- potentials
high.pot <- function(x){
x[x < minimumPotential] <- NA
return(x)
}
highPotentials <- mapply(FUN=calc, highPotentials, MoreArgs=list(fun=high.pot))
points.fun <-function(x){
y <- ncell(x) - summary(x)[6,]
return(y)
}
noOfPoints <- lapply(highPotentials, FUN=points.fun)
print("Number of points:")
print(noOfPoints)
# output
print("saving output...")
source("scripts/plotPotential.R")
setwd("output")
names <- lapply(1:3,FUN=function(x){sprintf("potential%i_%02d.tif", x, 1:noSteps)})
mapply(writeRaster, potentials, names, MoreArgs=list(bylayer=TRUE, format="GTiff",
overwrite=TRUE))
names <- lapply(1:3,FUN=function(x){sprintf("highPotential%i_%02d.tif", x, 1:noSteps)})
mapply(writeRaster, highPotentials, names, MoreArgs=list(bylayer=TRUE, format="GTiff",
overwrite=TRUE))
writeRaster(debiet, filename=sprintf("../step/debiet%02d.tif", 1:noSteps),
bylayer=TRUE, format="GTiff", overwrite=TRUE)
# tot hierrrrr....
# plot
names(potential) <- sprintf("potentialRaster%02d", seq(1,12))
names(highPotential) <- sprintf("potentialRaster%02d", seq(1,12))
if (plotMethod == "raster"){
PlotTimeRaster()
} else if (plotMethod == "vector"){
PlotTimeVector()
} else if (plotMethod == "both") {
PlotTimeRaster()
PlotTimeVector()
}
}