Skip to content

Commit

Permalink
Merge pull request #52 from LabSid-USP/8-refatorar-cdigo-para-contemp…
Browse files Browse the repository at this point in the history
…lar-nomenc

Update config file with EN naming
  • Loading branch information
LINAMARIAOSORIO authored Sep 10, 2021
2 parents 662cb23 + e193d17 commit eaca4a8
Showing 1 changed file with 106 additions and 104 deletions.
210 changes: 106 additions & 104 deletions rubem.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,112 +41,114 @@
from modules.interception import *
from modules.soil import *
from modules.surface_runoff import *

# Import util functions
from utilities.date_calc import *
from utilities.file_convertions import tss2csv
from utilities.file_generators import *


class Modelo(pcrfw.DynamicModel):
class RUBEM(pcrfw.DynamicModel):
"""Rainfall rUnoff Balance Enhanced Model.
Uses the PCRaster Dynamic Modelling Framework.
"""

def __init__(self):
"""Contains the initialization of the model class."""
pcrfw.DynamicModel.__init__(self)
print("RUBEM::Reading input files...", end=" ", flush=True)

# TODO: Check if loaded config info is valid
# Read file locations
self.inpath = config.get("FILES", "input")
self.dem_file = config.get("FILES", "dem")
self.demTif = config.get("FILES", "demtif")
self.clone_file = config.get("FILES", "clone")
# self.ldd_file = config.get('FILES', 'lddTif')
self.etp_path = config.get("FILES", "etp")
self.prec_path = config.get("FILES", "prec")
self.ndvi_path = config.get("FILES", "ndvi")
self.kp_path = config.get("FILES", "kp")
self.land_path = config.get("FILES", "landuse")
self.soil_path = config.get("FILES", "solo")
self.outpath = config.get("FILES", "output")
self.sampleLocs = config.get("FILES", "samples")
self.inpath = config.get("DIRECTORIES", "input")
self.outpath = config.get("DIRECTORIES", "output")
self.etp_path = config.get("DIRECTORIES", "etp")
self.prec_path = config.get("DIRECTORIES", "prec")
self.kp_path = config.get("DIRECTORIES", "Kp")
self.ndvi_path = config.get("DIRECTORIES", "ndvi")
self.land_path = config.get("DIRECTORIES", "landuse")

# Read temporal filenames prefix
self.etpPrefix = config.get("FILENAME_PREFIXES", "etp_prefix")
self.precPrefix = config.get("FILENAME_PREFIXES", "prec_prefix")
self.ndviPrefix = config.get("FILENAME_PREFIXES", "ndvi_prefix")
self.kpPrefix = config.get("FILENAME_PREFIXES", "kp_prefix")
self.coverPrefix = config.get("FILENAME_PREFIXES", "landuse_prefix")

self.dem_file = config.get("RASTERS", "dem")
self.demTif = config.get("RASTERS", "demtif")
self.clone_file = config.get("RASTERS", "clone")
# self.ldd_file = config.get('RASTERS', 'lddTif')
self.soil_path = config.get("RASTERS", "soil")
self.sampleLocs = config.get("RASTERS", "samples")
self.ndviMaxFile = config.get("RASTERS", "ndvi_max")
self.ndviMinFile = config.get("RASTERS", "ndvi_min")

# Set clone
pcr.setclone(self.clone_file)

# Read temporal filenames prefix
self.etpPrefix = config.get("FILES", "etpFilePrefix")
self.precPrefix = config.get("FILES", "precFilePrefix")
self.ndviPrefix = config.get("FILES", "ndviFilePrefix")
self.ndviMaxFile = config.get("FILES", "ndvimax")
self.ndviMinFile = config.get("FILES", "ndvimin")
self.kpPrefix = config.get("FILES", "kpFilePrefix")
self.coverPrefix = config.get("FILES", "landuseFilePrefix")

# Read text lookuptables from config file
self.rainyDaysTable = config.get("PARAMETERS", "rainydays")
self.aiTable = config.get("PARAMETERS", "a_i")
self.aoTable = config.get("PARAMETERS", "a_o")
self.asTable = config.get("PARAMETERS", "a_s")
self.avTable = config.get("PARAMETERS", "a_v")
self.manningTable = config.get("PARAMETERS", "manning")
self.dgTable = config.get("PARAMETERS", "dg")
self.KrTable = config.get("PARAMETERS", "kr")
self.TccTable = config.get("PARAMETERS", "capCampo")
self.TsatTable = config.get("PARAMETERS", "saturacao")
self.TwTable = config.get("PARAMETERS", "pontomurcha")
self.ZrTable = config.get("PARAMETERS", "zr")
self.KcminTable = config.get("PARAMETERS", "kcmin")
self.KcmaxTable = config.get("PARAMETERS", "kcmax")
self.rainyDaysTable = config.get("TABLES", "rainydays")
self.aiTable = config.get("TABLES", "a_i")
self.aoTable = config.get("TABLES", "a_o")
self.asTable = config.get("TABLES", "a_s")
self.avTable = config.get("TABLES", "a_v")
self.manningTable = config.get("TABLES", "manning")
self.dgTable = config.get("TABLES", "bulk_density")
self.KrTable = config.get("TABLES", "K_sat")
self.TccTable = config.get("TABLES", "T_fcap")
self.TsatTable = config.get("TABLES", "T_sat")
self.TwTable = config.get("TABLES", "T_wp")
self.ZrTable = config.get("TABLES", "rootzone_depth")
self.KcminTable = config.get("TABLES", "K_c_min")
self.KcmaxTable = config.get("TABLES", "K_c_max")

# TODO: Automatic calculation of cell area
# Cell area
self.A = config.getfloat("GRID", "grid")**2
self.A = config.getfloat("GRID", "grid") ** 2

# Read calibration parameters from config file
self.alfa = config.getfloat("CALIBRATION", "alfa")
self.alfa = config.getfloat("CALIBRATION", "alpha")
self.b = config.getfloat("CALIBRATION", "b")
self.w1 = config.getfloat("CALIBRATION", "w1")
self.w2 = config.getfloat("CALIBRATION", "w2")
self.w3 = config.getfloat("CALIBRATION", "w3")
self.w1 = config.getfloat("CALIBRATION", "w_1")
self.w2 = config.getfloat("CALIBRATION", "w_2")
self.w3 = config.getfloat("CALIBRATION", "w_3")
self.RCD = config.getfloat("CALIBRATION", "rcd")
self.f = config.getfloat("CALIBRATION", "f")
self.alfa_gw = config.getfloat("CALIBRATION", "alfa_gw")
self.alfa_gw = config.getfloat("CALIBRATION", "alpha_gw")
self.x = config.getfloat("CALIBRATION", "x")

# Read soil conditions from config file
# Initial moisture content of the root zone (fraction of saturation content)
self.ftur_ini = config.getfloat("INITIAL SOIL CONDITIONS", "ftur_ini")
self.ftur_ini = config.getfloat("INITIAL_SOIL_CONDITIONS", "T_ini")
# Initial baseflow
self.EBini = pcrfw.scalar(config.getfloat("INITIAL SOIL CONDITIONS", "eb_ini"))
self.EBini = pcrfw.scalar(config.getfloat("INITIAL_SOIL_CONDITIONS", "bfw_ini"))
# limit for baseflow
self.EBlim = pcrfw.scalar(config.getfloat("INITIAL SOIL CONDITIONS", "eb_lim"))
self.EBlim = pcrfw.scalar(config.getfloat("INITIAL_SOIL_CONDITIONS", "bfw_lim"))
# Initial moisture content of the saturated zone
self.Tusini = pcrfw.scalar(
config.getfloat("INITIAL SOIL CONDITIONS", "tus_ini")
config.getfloat("INITIAL_SOIL_CONDITIONS", "S_sat_ini")
)

# Constants
self.fpar_max = config.getfloat("CONSTANT", "fpar_max")
self.fpar_min = config.getfloat("CONSTANT", "fpar_min")
self.lai_max = config.getfloat("CONSTANT", "lai_max")
self.I_i = config.getfloat("CONSTANT", "i_imp")
self.fpar_max = config.getfloat("CONSTANTS", "fpar_max")
self.fpar_min = config.getfloat("CONSTANTS", "fpar_min")
self.lai_max = config.getfloat("CONSTANTS", "lai_max")
self.I_i = config.getfloat("CONSTANTS", "i_imp")

print("OK", flush=True) # RUBEM::Reading input files...

# # Initialize time series output
self.OutTssRun = "outRun"
self.OutTssPrec = "outPrec"
self.OutTssInt = "outInt"
self.OutTssBflow = "outBflow"
self.OutTssSfRun = "outSfRun"
self.OutTssEtp = "outEtp"
self.OutTssLf = "outLf"
self.OutTssRec = "outRec"
self.OutTssSsat = "outSsat"
self.OutTssRun = "tss_rnf"
# self.OutTssPrec = "tssPrec"
self.OutTssInt = "tss_itp"
self.OutTssBflow = "tss_bfw"
self.OutTssSfRun = "tss_srn"
self.OutTssEta = "tss_eta"
self.OutTssLf = "tss_lfw"
self.OutTssRec = "tss_rec"
self.OutTssSsat = "tss_smc"

# Report file
# name
Expand All @@ -159,7 +161,7 @@ def __init__(self):

def initial(self):
"""Contains the initialization of variables used in the model.
Contains operations to initialise the state of the model at time step 0.
Operations included in this section are executed once.
"""
Expand All @@ -184,9 +186,9 @@ def initial(self):
self.TssFileRun = pcrfw.TimeoutputTimeseries(
self.OutTssRun, self, self.sampleLocs, noHeader=True
)
self.TssFilePrec = pcrfw.TimeoutputTimeseries(
self.OutTssPrec, self, self.sampleLocs, noHeader=True
)
# self.TssFilePrec = pcrfw.TimeoutputTimeseries(
# self.OutTssPrec, self, self.sampleLocs, noHeader=True
# )
self.TssFileInt = pcrfw.TimeoutputTimeseries(
self.OutTssInt, self, self.sampleLocs, noHeader=True
)
Expand All @@ -196,8 +198,8 @@ def initial(self):
self.TssFileSfRun = pcrfw.TimeoutputTimeseries(
self.OutTssSfRun, self, self.sampleLocs, noHeader=True
)
self.TssFileEtp = pcrfw.TimeoutputTimeseries(
self.OutTssEtp, self, self.sampleLocs, noHeader=True
self.TssFileEta = pcrfw.TimeoutputTimeseries(
self.OutTssEta, self, self.sampleLocs, noHeader=True
)
self.TssFileLf = pcrfw.TimeoutputTimeseries(
self.OutTssLf, self, self.sampleLocs, noHeader=True
Expand Down Expand Up @@ -269,9 +271,9 @@ def initial(self):

def dynamic(self):
"""Contains the implementation of the dynamic section of the model.
Contains the operations that are executed consecutively each time step.
Results of a previous time step can be used as input for the current time step.
Contains the operations that are executed consecutively each time step.
Results of a previous time step can be used as input for the current time step.
The dynamic section is executed a specified number of timesteps.
"""
t = self.currentStep
Expand Down Expand Up @@ -386,7 +388,9 @@ def dynamic(self):
Cwp = Cwp_calc(self, pcr, Aimp, Cper, Cimp)
Csr = Csr_calc(self, pcr, Cwp, Pdm, self.RCD)

self.ES = ES_calc(self, pcr, Csr, Ch, precipitation, I, Ao, self.ET_ao, self.TUr, self.TUsat)
self.ES = ES_calc(
self, pcr, Csr, Ch, precipitation, I, Ao, self.ET_ao, self.TUr, self.TUsat
)

print("OK", flush=True) # print("\tSurface Runoff... OK", flush=True)

Expand Down Expand Up @@ -461,33 +465,33 @@ def dynamic(self):
if genTss:
# Function dictionary to export tss according to filename
genTssDic = {
"Int": self.TssFileInt.sample,
"Bflow": self.TssFileBflow.sample,
"SfRun": self.TssFileSfRun.sample,
"Etp": self.TssFileEtp.sample,
"Lf": self.TssFileLf.sample,
"Rec": self.TssFileRec.sample,
"Ssat": self.TssFileSsat.sample,
"Runoff": self.TssFileRun.sample,
"itp": self.TssFileInt.sample,
"bfw": self.TssFileBflow.sample,
"srn": self.TssFileSfRun.sample,
"eta": self.TssFileEta.sample,
"lfw": self.TssFileLf.sample,
"rec": self.TssFileRec.sample,
"smc": self.TssFileSsat.sample,
"rnf": self.TssFileRun.sample,
}

# Variable dictionary to export according to filename
varDic = {
"Int": I,
"Bflow": self.EB,
"SfRun": self.ES,
"Etp": self.ETr,
"Lf": self.LF,
"Rec": self.REC,
"Ssat": self.TUr,
"Runoff": self.runoff,
"itp": I,
"bfw": self.EB,
"srn": self.ES,
"eta": self.ETr,
"lfw": self.LF,
"rec": self.REC,
"smc": self.TUr,
"rnf": self.runoff,
}

for fileName, isSelected in genFilesDic.items():
# Check if the variable (fileName) has been selected for export
if isSelected:

# Export *.tiff raster
# Export *.tiff raster
if enableTIFFormat:
reportTif(
self,
Expand All @@ -497,8 +501,8 @@ def dynamic(self):
self.outpath,
dyn=True,
)
# Export *.map raster

# Export *.map raster
if enableMapFormat:
reportMapSeries(self, varDic.get(fileName), fileName)

Expand Down Expand Up @@ -543,42 +547,40 @@ def dynamic(self):
endDate = config.get("SIM_TIME", "end")

# Check whether the output directory exists
if not os.path.isdir(str(config.get("FILES", "output"))):
if not os.path.isdir(str(config.get("DIRECTORIES", "output"))):
# If the output directory doesn't exist create it
os.mkdir(str(config.get("FILES", "output")))
os.mkdir(str(config.get("DIRECTORIES", "output")))

# Store which variables have or have not been selected for export
genFilesList = ["Int", "Bflow", "SfRun", "Etp", "Lf", "Rec", "Ssat", "Runoff"]
genFilesList = ["itp", "bfw", "srn", "eta", "lfw", "rec", "smc", "rnf"]
genFilesDic = {}
for file in genFilesList:
genFilesDic[file] = config.getboolean("GENERATE_FILE", file)

# Store the setting that enables the export of time series
genTss = config.getboolean("GENERATE_FILE", "genTss")
genTss = config.getboolean("GENERATE_FILE", "tss")

# Store the format in which the resulting files will be exported (*.tif and/or *.map)
enableTIFFormat = config.getboolean("GENERATE_FILE_FORMAT", "enableTiff")
enableMapFormat = config.getboolean("GENERATE_FILE_FORMAT", "enableMapSeries")
enableTIFFormat = config.getboolean("RASTER_FILE_FORMAT", "tiff_raster_series")
enableMapFormat = config.getboolean("RASTER_FILE_FORMAT", "map_raster_series")

steps = totalSteps(startDate, endDate)
start = steps[0]
end = steps[1]

print("RUBEM::Running dynamic model...", flush=True)
myModel = Modelo()
dynamicModel = pcrfw.DynamicFramework(
myModel, lastTimeStep=end, firstTimestep=start
)
model = RUBEM()
dynamicModel = pcrfw.DynamicFramework(model, lastTimeStep=end, firstTimestep=start)
dynamicModel.run()
tempoExec = time.time() - t1
print(f"RUBEM::Dynamic model runtime: {tempoExec:.2f} seconds")

# Check whether the generation of time series has been activated
if genTss:
print("RUBEM::Converting *.tss files to *.csv...", end=" ", flush=True)
cols = [str(n) for n in myModel.sample_vals[1:]]
cols = [str(n) for n in model.sample_vals[1:]]
# Convert generated time series to .csv format and removes .tss files
tss2csv(myModel.outpath, cols)
tss2csv(model.outpath, cols)
print("OK", flush=True) # Converting *.tss files to *.csv...

print("RUBEM::Finished", flush=True)

0 comments on commit eaca4a8

Please sign in to comment.