Commit 1296eff3 authored by Wesseling, Jan's avatar Wesseling, Jan
Browse files

Calculation procedure included

parent 6c195e4a
......@@ -32,6 +32,18 @@ module Types
mutable struct InflowFrom
number :: Int64
subCatchment :: Array{Inflow}
subcatchment :: Array{Inflow}
end
mutable struct Balance
fromNonStorage :: Float64
intoStorage :: Float64
infiltration :: Float64
inflow :: Float64
totalIntoStorage :: Float64
storage :: Float64
maxStorage :: Float64
outflow :: Float64
end
end
......@@ -6,15 +6,18 @@ include("Types.jl")
catchment = Nothing
evap = Nothing
precip = Nothing
prec = Nothing
inflowFrom = Nothing
orderOfCalculation = Nothing
balance = Nothing
total = Nothing
infiltrationTime = 12.0
function createCatchment(aData :: DataFrame)
global catchment = Array{Types.CatchmentData}(undef,1)
try
try
resize!(catchment, size(aData,1)-1)
# println(size(catchment,1))
for i in 2:size(aData,1)
id = -1
if !ismissing(aData[i,1])
......@@ -154,7 +157,6 @@ function createCatchment(aData :: DataFrame)
global catchment[i-1] = Types.CatchmentData(id,area,cultivated,storage,maxHeight,runoffCoefficient,
infiltrationRate,infiltrationPercentage,outflowsTo1,
percentage1,outflowsTo2,percentage2)
# println(catchment[i-1])
end
catch e
println("???Error in createCatchment: ",e)
......@@ -168,7 +170,6 @@ function createETp(aData :: DataFrame)
try
try
resize!(evap, size(aData,1)-1)
# println(size(catchment,1))
for i in 2:size(aData,1)
year = -1
if !ismissing(aData[i,1])
......@@ -193,10 +194,7 @@ function createETp(aData :: DataFrame)
end
global evap[i-1] = Types.Evapotranspiration(year, etp)
# println(evap[i-1])
end
catch e
println("???Error in createETp: ",e)
end
......@@ -209,9 +207,7 @@ function createPrecipitationData(aData :: DataFrame)
try
try
resize!(prec, size(aData,1)-1)
# println(size(catchment,1))
for i in 2:size(aData,1)
# println(i)
year = -1
if !ismissing(aData[i,1])
if typeof(aData[i,1]) == String
......@@ -222,7 +218,6 @@ function createPrecipitationData(aData :: DataFrame)
year = aData[i,1]
end
end
# println(i, " ",year)
leap = isleapyear(Date(year,1,1))
ev = Array{Float64}(undef,1)
p = Types.Precipitation(year, ev)
......@@ -246,7 +241,6 @@ function createPrecipitationData(aData :: DataFrame)
p.event[j-1] = event
end
global prec[i-1] = p
# println(evap[i-1])
end
catch e
println("???Error in createPrecipitationData: ",e)
......@@ -259,9 +253,7 @@ function readData()
fileName = "/home/wesseling/DataDisk/Wesseling/Work/Ammar/Original/WHCatch2.xlsm"
try
try
println("Reading....")
xf = XLSX.readxlsx(fileName)
println("After readxlsx")
sheet = xf["Catchment"]
data = dropmissing!(DataFrame(sheet[:]))
createCatchment(data)
......@@ -270,9 +262,7 @@ function readData()
createETp(data)
sheet = xf["Rainfall"]
data = DataFrame(sheet[:])
# println(first(data,10))
createPrecipitationData(data)
println("OK")
catch e
println("???ERROR in readData: ",e)
end
......@@ -281,39 +271,242 @@ function readData()
end
function determineInflow()
global inflowFrom = Array{Types.InflowFrom}(undef,1)
n = size(catchment,1)
resize!(inflowFrom,n)
for i in 1:n
x = Types.Inflow(-1,-1,0.0)
arr = Array{Types.Inflow}(undef,1)
arr[1] = x
tmp = Types.InflowFrom(0,arr)
inflowFrom[i] = tmp
try
try
global inflowFrom = Array{Types.InflowFrom}(undef,1)
n = size(catchment,1)
resize!(inflowFrom,n)
for i in 1:n
x = Types.Inflow(-1,-1,0.0)
arr = Array{Types.Inflow}(undef,1)
arr[1] = x
tmp = Types.InflowFrom(0,arr)
inflowFrom[i] = tmp
end
for i in 1:n
if catchment[i].outflowTo1 > 0 && catchment[i].percentage1 > 0.0
global inflowFrom[catchment[i].outflowTo1].number += 1
resize!(inflowFrom[catchment[i].outflowTo1].subcatchment, inflowFrom[catchment[i].outflowTo1].number)
tmp = Types.Inflow(i,1,0.01*catchment[i].percentage1)
global inflowFrom[catchment[i].outflowTo1].subcatchment[inflowFrom[catchment[i].outflowTo1].number] = tmp
end
if catchment[i].outflowTo2 > 0 && catchment[i].percentage2 > 0.0
global inflowFrom[catchment[i].outflowTo2].number += 1
resize!(inflowFrom[catchment[i].outflowTo2].subcatchment, inflowFrom[catchment[i].outflowTo2].number)
tmp = Types.Inflow(i,1,0.01*catchment[i].percentage2)
global inflowFrom[catchment[i].outflowTo2].subcatchment[inflowFrom[catchment[i].outflowTo2].number] = tmp
end
end
catch e
println("???ERROR in determineInflow: ", e)
end
finally
end
for i in 1:n
if catchment[i].outflowTo1 > 0 && catchment[i].percentage1 > 0.0
global inflowFrom[catchment[i].outflowTo1].number += 1
resize!(inflowFrom[catchment[i].outflowTo1].subCatchment, inflowFrom[catchment[i].outflowTo1].number)
tmp = Types.Inflow(i,1,0.01*catchment[i].percentage1)
global inflowFrom[catchment[i].outflowTo1].subCatchment[inflowFrom[catchment[i].outflowTo1].number] = tmp
end
function findOrder()
try
try
n = size(catchment,1)
global orderOfCalculation = Array{Int64}(undef,n)
selected = Array{Bool}(undef,n)
for i in 1:n
global orderOfCalculation[i] = -1
selected[i] = false
end
number = 0
# subcatchments without inflow
for i in 1:n
if inflowFrom[i].number <= 0
number += 1
selected[i] = true
global orderOfCalculation[number] = i
end
end
# the others
iter = 0
maxIter = 2 * n
while number < n && iter < maxIter
iter += 1
for i in 1:n
if !selected[i]
ok = true
for j in 1:inflowFrom[i].number
if !selected[inflowFrom[i].subcatchment[j].from]
ok = false
break
end
end
if ok
number += 1
global orderOfCalculation[number] = i
selected[i] = true
end # if ok
end # if !selected
end # for i
end # while
catch e
println("???ERROR in findOrder: ",e)
end
if catchment[i].outflowTo2 > 0 && catchment[i].percentage2 > 0.0
global inflowFrom[catchment[i].outflowTo2].number += 1
resize!(inflowFrom[catchment[i].outflowTo2].subCatchment, inflowFrom[catchment[i].outflowTo2].number)
tmp = Types.Inflow(i,1,0.01*catchment[i].percentage2)
global inflowFrom[catchment[i].outflowTo2].subCatchment[inflowFrom[catchment[i].outflowTo2].number] = tmp
finally
end
end
function zeroBalance(aBalance :: Types.Balance)
aBalance.fromNonStorage = 0.0
aBalance.intoStorage = 0.0
aBalance.infiltration = 0.0
aBalance.inflow = 0.0
aBalance.totalIntoStorage = 0.0
aBalance.storage = 0.0
aBalance.maxStorage = 0.0
aBalance.outflow = 0.0
end
function createBalance()
try
try
n = size(catchment,1)
global balance = Array{Types.Balance}(undef, n)
global total = Array{Types.Balance}(undef,n)
for i in 1:n
myBalance = Types.Balance(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
global balance[i] = myBalance
zeroBalance(balance[i])
myBalance = Types.Balance(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
global total[i] = myBalance
zeroBalance(total[i])
end
catch e
println("???ERROR in createBalance: ",e)
end
finally
end
end
function initialize()
readData()
determineInflow()
println(inflowFrom)
findOrder()
createBalance()
end
function balanceOfSubcatchment(aPrecipitation :: Float64, aSubcatchment :: Int64)
try
try
myBalance = Types.Balance(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
myBalance.fromNonStorage = aPrecipitation * catchment[aSubcatchment].runoffCoefficient *
(catchment[aSubcatchment].area - catchment[aSubcatchment].storage)
myBalance.intoStorage = aPrecipitation * catchment[aSubcatchment].storage
myBalance.inflow = 0.0
if inflowFrom[aSubcatchment].number > 0
for i in 1:inflowFrom[aSubcatchment].number
myBalance.inflow += balance[inflowFrom[aSubcatchment].subcatchment[i].from].outflow *
inflowFrom[aSubcatchment].subcatchment[i].fraction
end
end
myBalance.totalIntoStorage = myBalance.fromNonStorage + myBalance.intoStorage + myBalance.inflow
myBalance.infiltration = 0.001 * catchment[aSubcatchment].infiltrationRate * catchment[aSubcatchment].storage * infiltrationTime
if myBalance.infiltration > myBalance.totalIntoStorage
myBalance.infiltration = myBalance.totalIntoStorage
end
myBalance.storage = myBalance.totalIntoStorage - myBalance.infiltration
myBalance.maxStorage = catchment[aSubcatchment].storage * catchment[aSubcatchment].maxHeight
myBalance.outflow = 0.0
if myBalance.storage > myBalance.maxStorage
myBalance.outflow = myBalance.storage - myBalance.maxStorage
myBalance.storage = myBalance.maxStorage
end
balance[aSubcatchment] = myBalance
catch e
println("???ERROR in balanceOfSubcatchment: ",e)
end
finally
end
end
function processDailyValues(aPrecipitation :: Float64)
try
try
prec = 0.001 * aPrecipitation
for i in size(orderOfCalculation,1)
mySub = orderOfCalculation[i]
balanceOfSubcatchment(aPrecipitation, mySub)
end
catch e
println("???ERROR in processDailyValue: ",e)
end
finally
end
end
function setTotalToZero()
try
try
for i in 1:size(total,1)
zeroBalance(total[i])
end
catch e
println("???ERROR in setTotalToZero: ",e)
end
finally
end
end
function addToTotal()
try
try
for i in 1:size(balance,1)
global total[i].fromNonStorage += balance[i].fromNonStorage
global total[i].intoStorage += balance[i].intoStorage
global total[i].infiltration += balance[i].infiltration
global total[i].inflow += balance[i].inflow
global total[i].totalIntoStorage += balance[i].totalIntoStorage
global total[i].storage += balance[i].storage
global total[i].maxStorage += balance[i].maxStorage
global total[i].outflow += balance[i].outflow
end
catch e
println("???ERROR in addToTotal: ", e)
end
finally
end
end
function prepareOutput()
try
try
catch e
println("???ERROR in prepareOutput: ", e)
end
finally
end
end
function process()
try
try
prepareOutput()
for i in 1:size(prec,1)
setTotalToZero()
for j in size(prec[i].event,1)
processDailyValues(prec[i].event[j])
addToTotal()
end
end
catch e
println("???ERROR in process: ",e)
end
finally
end
end
initialize()
process()
println("Finished")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment