Code structure help

M

Martin De Kauwe

Hi,

I have been working on re-writing a model in python and have been
trying to adopt some of the advise offered on here to recent
questions. However I am not sure how easy on the eye my final
structure is and would appreciate any constructive comments/
suggestions. So broadly the model estimates plant growth using a
number of related sub functions which I have grouped into classes and
they all live in separate files. My main issue at the moment is I
think I have a lot of verbose class instances but I really can't see a
better way to do it. I have left some of the bones of the code out but
this is basically what would it looks like.

thanks.


import constants as const
from file_parser import ConfigFileParser
from plant_growth import PlantGrowth
from print_outputs import PrintOutput
from litter import LitterFlows
from decomp import DecompFactors
from soil_cnflows import CarbonFlows, NitrogenFlows
from nmineralisation import Mineralisation
from update_pools import CarbonPools, NitrogenPools
....etc...

class ModelName(object):
def __init__(self, cfg_fname=None):
""" Set everything up

Read meterological forcing file, any user adjusted files. If
there
is anything in the user file then adjust the model parameters,
control
or initial state attributes that are used within the code.
"""

# sweep the cmd line
options, args = cmdline_parser()

# quit if asked only to dump default paramater files
if options.DUMP_INPUT == True:
...call some stuff...

# read in user defined variables (stored in dictionaries)
pars = ConfigFileParser(cfg_fname=cfg_fname)
(control, params, state, files, fluxes, met_data) =
pars.main()

# objects holding model state, fluxes, parameters and control
flags
self.met_data = met_data
self.control = control
self.params = params
self.state = state
self.fluxes = fluxes

# instances of other model parts..
self.pr = PrintOutput(self.params, self.state, self.fluxes,
self.control, self.files, dump=False)

# start date of simulation
self.date = self.simulation_start_date()


...etc....

def run_sim(self):

mi = Mineralisation(self.control, self.params, self.state,
self.fluxes)
cf = CarbonFlows(self.control, self.params, self.state,
self.fluxes)
nf = NitrogenFlows(self.control, self.params, self.state,
self.fluxes)
de = Derive(self.control, self.params, self.state,
self.fluxes)
dc = DecompFactors(self.control, self.params, self.state,
self.fluxes)
lf = LitterFlows(self.control, self.params, self.state,
self.fluxes)
pg = PlantGrowth(self.control, self.params, self.state,
self.fluxes,
self.met_data)
cpl = CarbonPools(self.control, self.params, self.state,
self.fluxes)
npl = NitrogenPools(self.control, self.params, self.state,
self.fluxes)

for i in self.met_data.doy:
project_day = i - 1

# N:C ratios
leafnc, rootnc = self.leaf_root_ncratio()

# litterfall rate: C and N fluxes
lf.flows(leafnc, rootnc)

# plant growth
pg.grow(project_day, self.date, leafnc)

# calculate model decay rates
dc.decay_rates()

# soil model fluxes
cf.calculate_cflows()
nf.calculate_nflows()

# N uptake and loss
mi.calculate_mineralisation()

# carbon sink or source?
self.fluxes.nep = (self.fluxes.npp -
self.fluxes.hetero_resp -
self.fluxes.ceaten *
(1. - self.params.fracfaeces))

# soil model - update pools
cact, cslo, cpas = cpl.calculate_cpools()
npl.calculate_npools(cact, cslo, cpas)

if self.control.print_options == 1:
self.pr.print_state()
self.pr.print_fluxes()

self.increment_date()

if self.control.print_options == 2:
self.pr.print_state()
self.pr.print_fluxes()

# house cleaning, close ouput files
self.pr.tidy_up()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top