[Tutor] Code structure help

Martin De Kauwe mdekauwe at gmail.com
Fri Mar 11 14:09:06 CET 2011


Note I have cross posted this as I have only just found this mailing list
and perhaps it is the more appropriate place (I am not sure)?

I have been working on re-writing a model in python. 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 how plants
grow 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. Is there a better way? How do other people do similar
things? I am talking largely about the instances in the method run_sim

I have left some of the bones of the code out but this is basically what
would it looks like.


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

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, etc
        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()

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

        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

            # soil model fluxes

            # N uptake and loss

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

            if self.control.print_options == 1:


        if self.control.print_options == 2:

        # house cleaning, close ouput files
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110312/0e9c60d3/attachment-0001.html>

More information about the Tutor mailing list