Commit cdeee406 authored by Wesseling, Jan's avatar Wesseling, Jan
Browse files

CSV output

parent 1296eff3
......@@ -21,6 +21,7 @@ module Types
mutable struct Precipitation
year :: Int64
total :: Float64
event :: Array{Float64}
end
......@@ -46,4 +47,9 @@ module Types
outflow :: Float64
end
mutable struct outputRecord
year :: Int64
value :: Array{Float64}
end
end
using XLSX
using Dates
using DataFrames
using Printf
using OffsetArrays
include("Types.jl")
......@@ -11,7 +13,21 @@ inflowFrom = Nothing
orderOfCalculation = Nothing
balance = Nothing
total = Nothing
infiltrationTime = 12.0
infiltrationTime = 4.0
factorForSmax = 0.9
outputVx = Nothing
outputSmax = Nothing
outputVin = Nothing
outputVinf = Nothing
outputVs = Nothing
outputdV = Nothing
outputRunoff = Nothing
yearToShow = 1992
firstYear = 1980
lastYear = 2100
function createCatchment(aData :: DataFrame)
global catchment = Array{Types.CatchmentData}(undef,1)
......@@ -151,9 +167,6 @@ function createCatchment(aData :: DataFrame)
end
end
global catchment[i-1] = Types.CatchmentData(id,area,cultivated,storage,maxHeight,runoffCoefficient,
infiltrationRate,infiltrationPercentage,outflowsTo1,
percentage1,outflowsTo2,percentage2)
......@@ -203,11 +216,35 @@ function createETp(aData :: DataFrame)
end
function createPrecipitationData(aData :: DataFrame)
global prec = Array{Types.Precipitation}(undef,1)
try
try
resize!(prec, size(aData,1)-1)
for i in 2:size(aData,1)
yearFirst = -1
if !ismissing(aData[3,1])
if typeof(aData[3,1]) == String
if strip(aData[3,1]) != ""
yearFirst = tryparse(Int64, aData[3,1])
end
else
yearFirst = aData[3,1]
end
end
yearLast = -1
n = size(aData,1)
while n > 2 && yearLast < 0
if !ismissing(aData[n,1])
if typeof(aData[n,1]) == String
if strip(aData[n,1]) != ""
yearLast = tryparse(Int64, aData[n,1])
end
else
yearLast = aData[n,1]
end
else
n -= 1
end
end
global prec = OffsetArray{Types.Precipitation}(undef,yearFirst:yearLast)
for i in 3:size(aData,1)
year = -1
if !ismissing(aData[i,1])
if typeof(aData[i,1]) == String
......@@ -220,7 +257,7 @@ function createPrecipitationData(aData :: DataFrame)
end
leap = isleapyear(Date(year,1,1))
ev = Array{Float64}(undef,1)
p = Types.Precipitation(year, ev)
p = Types.Precipitation(year, 0.0, ev)
p.year = year
days = 365
if leap
......@@ -239,8 +276,11 @@ function createPrecipitationData(aData :: DataFrame)
end
end
p.event[j-1] = event
if event > 0.0
p.total += event
end
end
global prec[i-1] = p
global prec[year] = p
end
catch e
println("???Error in createPrecipitationData: ",e)
......@@ -414,13 +454,13 @@ function balanceOfSubcatchment(aPrecipitation :: Float64, aSubcatchment :: Int64
myBalance.infiltration = myBalance.totalIntoStorage
end
myBalance.storage = myBalance.totalIntoStorage - myBalance.infiltration
myBalance.maxStorage = catchment[aSubcatchment].storage * catchment[aSubcatchment].maxHeight
myBalance.maxStorage = factorForSmax * 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
global balance[aSubcatchment] = myBalance
catch e
println("???ERROR in balanceOfSubcatchment: ",e)
end
......@@ -432,7 +472,7 @@ function processDailyValues(aPrecipitation :: Float64)
try
try
prec = 0.001 * aPrecipitation
for i in size(orderOfCalculation,1)
for i in 1:size(orderOfCalculation,1)
mySub = orderOfCalculation[i]
balanceOfSubcatchment(aPrecipitation, mySub)
end
......@@ -466,7 +506,7 @@ function addToTotal()
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].maxStorage = balance[i].maxStorage
global total[i].outflow += balance[i].outflow
end
catch e
......@@ -479,6 +519,22 @@ end
function prepareOutput()
try
try
n = 0
if yearToShow > 0
n = 365
if yearToShow % 4 == 0
n = 366
end
else
n = lastYear - firstYear + 1
end
global outputVx = Array{Types.outputRecord}(undef,n)
global outputSmax = Array{Types.outputRecord}(undef,n)
global outputVin = Array{Types.outputRecord}(undef,n)
global outputVinf = Array{Types.outputRecord}(undef,n)
global outputVs = Array{Types.outputRecord}(undef,n)
global outputdV = Array{Types.outputRecord}(undef,n)
global outputRunoff = Array{Types.outputRecord}(undef,n)
catch e
println("???ERROR in prepareOutput: ", e)
end
......@@ -486,17 +542,363 @@ function prepareOutput()
end
end
function selectOutputPerDay(aDay :: Int64)
try
try
n = size(catchment,1)
out = Array{Float64}(undef,n)
for j in 1:n
out[j] = balance[j].inflow
end
global outputVx[aDay] = Types.outputRecord(aDay, out)
out = Array{Float64}(undef,n)
for j in 1:n
out[j] = balance[j].maxStorage
end
global outputSmax[aDay] = Types.outputRecord(aDay, out)
out = Array{Float64}(undef,n)
for j in 1:n
out[j] = balance[j].fromNonStorage
end
global outputVin[aDay] = Types.outputRecord(aDay, out)
out = Array{Float64}(undef,n)
for j in 1:n
out[j] = balance[j].infiltration
end
global outputVinf[aDay] = Types.outputRecord(aDay, out)
out = Array{Float64}(undef,n)
for j in 1:n
out[j] = balance[j].intoStorage
end
global outputVs[aDay] = Types.outputRecord(aDay, out)
out = Array{Float64}(undef,n)
for j in 1:n
out[j] = balance[j].storage
end
global outputdV[aDay] = Types.outputRecord(aDay, out)
out = Array{Float64}(undef,n)
for j in 1:n
out[j] = balance[j].outflow
end
global outputRunoff[aDay] = Types.outputRecord(aDay, out)
catch e
println("???ERROR in selectOutputPerDay: ", e)
end
finally
end
end
function selectOutputPerYear(aYear :: Int64)
try
try
recNr = aYear - firstYear + 1
n = size(balance,1)
out = Array{Float64}(undef,n)
for i in 1:n
out[i] = total[i].inflow
end
global outputVx[recNr] = Types.outputRecord(aYear, out)
out = Array{Float64}(undef,n)
for i in 1:n
out[i] = total[i].maxStorage
end
global outputSmax[recNr] = Types.outputRecord(aYear, out)
out = Array{Float64}(undef,n)
for i in 1:n
out[i] = total[i].fromNonStorage
end
global outputVin[recNr] = Types.outputRecord(aYear, out)
out = Array{Float64}(undef,n)
for i in 1:n
out[i] = total[i].infiltration
end
global outputVinf[recNr] = Types.outputRecord(aYear, out)
out = Array{Float64}(undef,n)
for i in 1:n
out[i] = total[i].intoStorage
end
global outputVs[recNr] = Types.outputRecord(aYear, out)
out = Array{Float64}(undef,n)
for i in 1:n
out[i] = total[i].storage
end
global outputdV[recNr] = Types.outputRecord(aYear, out)
out = Array{Float64}(undef,n)
for i in 1:n
out[i] = total[i].outflow
end
global outputRunoff[recNr] = Types.outputRecord(aYear, out)
catch e
println("???ERROR in selectOutputPerYear: ", e)
end
finally
end
end
function saveOutputToFile()
try
try
m = 366
n = size(catchment,1)
if yearToShow > 0
m = size(catchment,1)
n = 365
if yearToShow % 4 == 0
n = 366
end
end
# Vout
line = "Vout"
for i in 1:m+1
line *= ", "
end
line *= "\n,P"
for i in 1:m
line *= "," * string(i)
end
line *= "\n"
for i in 1:n
line *= string(outputVx[i].year)
if yearToShow == 0
line *= "," * @sprintf("%.1f",prec[outputVx[i].year].total)
else
line *= "," * @sprintf("%.2f",prec[yearToShow].event[i])
end
for j in 1:size(outputVx[i].value,1)
line *= "," * @sprintf("%.5e",outputVx[i].value[j])
end
line *= "\n"
end
out = open("/home/wesseling/DataDisk/Wesseling/Work/Ammar/whcatch/csv/outputVx.csv", "w")
write(out, line)
close(out)
# Smax
line = "Smax"
for i in 1:m+1
line *= ", "
end
line *= "\n,P"
for i in 1:m
line *= "," * string(i)
end
line *= "\n"
for i in 1:n
line *= string(outputSmax[i].year)
if yearToShow == 0
line *= "," * @sprintf("%.1f",prec[outputSmax[i].year].total)
else
line *= "," * @sprintf("%.2f",prec[yearToShow].event[i])
end
for j in 1:size(outputVx[i].value,1)
line *= "," * @sprintf("%.5e",outputSmax[i].value[j])
end
line *= "\n"
end
out = open("/home/wesseling/DataDisk/Wesseling/Work/Ammar/whcatch/csv/outputSmax.csv", "w")
write(out, line)
close(out)
# Vin
line = "Vin"
for i in 1:m+1
line *= ", "
end
line *= "\n,P"
for i in 1:m
line *= "," * string(i)
end
line *= "\n"
for i in 1:n
line *= string(outputVin[i].year)
if yearToShow == 0
line *= "," * @sprintf("%.1f",prec[outputVin[i].year].total)
else
line *= "," * @sprintf("%.2f",prec[yearToShow].event[i])
end
for j in 1:size(outputVin[i].value,1)
line *= "," * @sprintf("%.5e",outputVin[i].value[j])
end
line *= "\n"
end
out = open("/home/wesseling/DataDisk/Wesseling/Work/Ammar/whcatch/csv/outputVin.csv", "w")
write(out, line)
close(out)
# Vinf
line = "Vinf"
for i in 1:m+1
line *= ", "
end
line *= "\n,P"
for i in 1:m
line *= "," * string(i)
end
line *= "\n"
for i in 1:n
line *= string(outputVinf[i].year)
if yearToShow == 0
line *= "," * @sprintf("%.1f",prec[outputVinf[i].year].total)
else
line *= "," * @sprintf("%.2f",prec[yearToShow].event[i])
end
for j in 1:size(outputVinf[i].value,1)
line *= "," * @sprintf("%.5e",outputVinf[i].value[j])
end
line *= "\n"
end
out = open("/home/wesseling/DataDisk/Wesseling/Work/Ammar/whcatch/csv/outputVinf.csv", "w")
write(out, line)
close(out)
# Vs
line = "Vs"
for i in 1:m+1
line *= ", "
end
line *= "\n,P"
for i in 1:m
line *= "," * string(i)
end
line *= "\n"
for i in 1:n
line *= string(outputVs[i].year)
if yearToShow == 0
line *= "," * @sprintf("%.1f",prec[outputVs[i].year].total)
else
line *= "," * @sprintf("%.2f",prec[yearToShow].event[i])
end
for j in 1:size(outputVs[i].value,1)
line *= "," * @sprintf("%.5e",outputVs[i].value[j])
end
line *= "\n"
end
out = open("/home/wesseling/DataDisk/Wesseling/Work/Ammar/whcatch/csv/outputVs.csv", "w")
write(out, line)
close(out)
# dV
line = "Vs"
for i in 1:m+1
line *= ", "
end
line *= "\n,P"
for i in 1:m
line *= "," * string(i)
end
line *= "\n"
for i in 1:n
line *= string(outputdV[i].year)
if yearToShow == 0
line *= "," * @sprintf("%.1f",prec[outputdV[i].year].total)
else
line *= "," * @sprintf("%.2f",prec[yearToShow].event[i])
end
for j in 1:size(outputdV[i].value,1)
line *= "," * @sprintf("%.5e",outputVs[i].value[j])
end
line *= "\n"
end
out = open("/home/wesseling/DataDisk/Wesseling/Work/Ammar/whcatch/csv/outputdV.csv", "w")
write(out, line)
close(out)
# Runoff
line = "Runoff"
for i in 1:m+1
line *= ", "
end
line *= "\n,P"
for i in 1:m
line *= "," * string(i)
end
line *= "\n"
for i in 1:n
line *= string(outputRunoff[i].year)
if yearToShow == 0
line *= "," * @sprintf("%.1f",prec[outputRunoff[i].year].total)
else
line *= "," * @sprintf("%.2f",prec[yearToShow].event[i])
end
for j in 1:size(outputRunoff[i].value,1)
line *= "," * @sprintf("%.5e",outputRunoff[i].value[j])
end
line *= "\n"
end
out = open("/home/wesseling/DataDisk/Wesseling/Work/Ammar/whcatch/csv/outputRunoff.csv", "w")
write(out, line)
close(out)
catch e
println("???ERROR in saveOutputToFile: ", 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()
if yearToShow > 0
global firstYear = yearToShow
global lastYear = yearToShow
end
for i in first(axes(prec,1)):last(axes(prec,1))
if prec[i].year >= firstYear && prec[i].year <= lastYear
setTotalToZero()
for j in 1:size(prec[i].event,1)
processDailyValues(prec[i].event[j])
if yearToShow > 0
selectOutputPerDay(j)
else
addToTotal()
end
end
if yearToShow == 0
selectOutputPerYear(prec[i].year)
end
end
end
saveOutputToFile()
catch e
println("???ERROR in process: ",e)
end
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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