Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Code structure help

Reply
Thread Tools

Code structure help

 
 
Martin De Kauwe
Guest
Posts: n/a
 
      03-10-2011
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()



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between c structure and c++ structure raghunandan_1081@yahoo.com C++ 9 11-11-2011 07:34 AM
How to read a Structure from a matlab file to a structure in Vc++ 2003 meisterbartsch C++ 2 06-12-2007 08:47 PM
Memory allocation in Structure to Structure pra_ramli@rediffmail.com C++ 2 03-09-2006 05:51 AM
Copy String structure "A" to string structure "B" Leo Nunez C Programming 3 02-09-2005 05:14 AM
Pointers to structure and array of structure. Excluded_Middle C Programming 4 10-26-2004 05:39 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57