Examples¶
Initialising arrays and assigning values in the script¶
The first example demonstrates a simple dynamic script with the usage of index variables, assigning values and looping over over the array indices:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pcraster import *
from pcraster.framework import *
from pcraster.collection import *
import os
import random
class SimpleLoops():
def __init__(self, cloneMap):
setclone(cloneMap)
def initial(self):
# oldcalc: Plants = [TG,SG];
self.Plants = Index(["TG", "SG"])
# initialising QMax[Plants]
self.QMax = VariableCollection([self.Plants], value=None)
# and assigning values manually
self.QMax[self.Plants.TG] = 10000.0
self.QMax[self.Plants.SG] = 15000.0
# initialising Cvr[Plants]
self.Cvr = VariableCollection([self.Plants], value=None)
# and assigning values manually
self.Cvr[self.Plants.TG] = readmap(os.path.join("maps", "CvrTG.imp"))
self.Cvr[self.Plants.SG] = readmap(os.path.join("maps", "CvrSG.imp"))
# oldcalc:
#foreach p in Plants {
# dH[p] = 0;
# dH_1[p] = 0;
#}
self.dH = VariableCollection([self.Plants], value=0)
self.dH_1 = VariableCollection([self.Plants], value=0)
# initialising timeseries report
self.CvrTss = VariableCollection([self.Plants], value=ValueTimeoutputTimeseries("Cvr", self, idMap="clone.cln", noHeader=False))
def dynamic(self):
# just some random calculations
for plant in self.Plants:
self.dH[plant] = random.random()
self.dH_1[plant] += self.dH[plant]
self.Cvr[plant] += self.dH_1[plant]
# reporting timeseries
self.CvrTss[plant].sample(self.Cvr[plant])
model = SimpleLoops("clone.cln")
dynModel = DynamicFramework(model, endTimeStep=5, firstTimestep=1)
dynModel.run()
In the initial section we create an index type, called Plants with two types of array-index, namely TG and SG. After that, two variables, QMax and Cvr are initialised and individual values are assigned. For two other variables, dH and dH_1, array-indeces are initialised with an default value of 0.
In the dynamic section we loop over the array-indices and execute some simple operations.
Using parameter files for variable initialisation¶
The second example simplifies the initialisation of variables by external files:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pcraster import *
from pcraster.framework import *
from pcraster.collection import *
import random
class ParameterFileModel(object):
def __init__(self, cloneMap):
setclone(cloneMap)
def initial(self):
# Plants = [TG,SG];
self.Plants = Index(["TG", "SG"])
# initialising QMax[Plants]
self.QMax = VariableCollection([self.Plants], value=ValueFromParameterTable("QMax", "plant.tbl", Scalar))
# initialising Cvr[Plants]
self.Cvr = VariableCollection([self.Plants], value=ValueFromParameterTable("Cvr", "plant.tbl", Scalar))
#foreach p in Plants {
# dH[p] = 0;
# dH_1[p] = 0;
#}
self.dH = VariableCollection([self.Plants], value=0)
self.dH_1 = VariableCollection([self.Plants], value=0)
# initialising timeseries report
self.CvrTss =VariableCollection([self.Plants], value=ValueTimeoutputTimeseries("Cvr", self, idMap="clone.cln", noHeader=False))
def dynamic(self):
# just some random calculations
for plant in self.Plants:
self.dH[plant] = random.random()
self.dH_1[plant] += self.dH[plant]
self.Cvr[plant] += self.dH_1[plant]
# reporting timeseries
self.CvrTss[plant].sample(self.Cvr[plant])
model = ParameterFileModel("clone.cln")
dynModel = DynamicFramework(model, endTimeStep=5, firstTimestep=1)
dynModel.run()
The functionality of the example above equals the one of the first example. Here, the variables QMax and Cvr are initialised from a parameter file ‘plant.tbl’.
Parameter files¶
The structure of the parameter files must be the same as used for oldcalc. Therefore, the second example uses a parameter file like:
# TG - TallGrass
# SG - ShortGass
# initial cover, can be a spatial
Cvr TG maps\CvrTG.imp
Cvr SG maps\CvrSG.imp
# quality of plant material [J g-1]
QMax TG 10000.
QMax SG 15000.
# maximum vegetation density [g m-2]
kv TG 1000.
kv SG 500.
# maximum relative growth rate [g g-1 d-1]
rv TG 0.05
rv SG 0.1
# minimum and maximum value for the average distance between patches
AvDstPatchMin TG 2.
AvDstPatchMin SG 50.
AvDstPatchMax TG 2.
AvDstPatchMax SG 100.
# minimum and maximum value for the standard deviation for distance between patches.
StdevDstPatchMin TG 0.
StdevDstPatchMin SG 0.
StdevDstPatchMax TG 0.
StdevDstPatchMax SG 0.
# 'dimension' of the grass, 2D or 3D
Dim TG 3.
Dim SG 2.
# ratio stem to foliage at the end of the growing season, represents quality
fLeaf2Stalk TG 0.50
fLeaf2Stalk SG 0.95