[Tutor] Code structure help

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


Hi,

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.

thanks,

Martin
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, 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()
        ...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()

            # 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()
-------------- 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