Table Of Contents

Previous topic

expression.py

Next topic

opres.py

This Page

inline.py

class OutputInline(Output):

Module for data output in hierarchical “data in columns” format. (writing tables to files)

def __init__(self, datadb, data_type, id_list, main_level, result_type, output_filename, output_constraint=None, default_decimal_places=1, archiving=False, result_padding=True, aggregation_def=None, expression_def=None, opres_vars=None):

Creates the output class. fully inherited, executes self.run()

>>> import datetime
>>> from pprint import pprint
>>> from simo.output.test.init_objects import InitData, TestLogger
>>> idata = InitData()
>>> idata.init()
>>> testdb = idata.testdb
>>> const_obj = idata.const_obj
>>> aggr_obj = idata.aggr_obj
>>> expr_obj = idata.expr_obj
>>> origvars = const_obj.variables
>>> const_obj.variables = {('stratum',2):origvars[('stratum', 2)]}
>>> from simo.output.inline import OutputInline
>>> out = OutputInline(testdb, 'result', ['stand1'], 'stratum',
...               'optimized',
...               'output/test/inlined.txt', TestLogger(),
...               const_obj, 1, False, True, aggr_obj, expr_obj,
...               ['cash_flow', 'Income', 'Scrapwood', 'Volume',
...               'BIOMASS_branches', 'BIOMASS_stumps'])
>>> try: 
...     file = open('output/test/inlined.txt', 'r')
...     for line in file:
...         print line.rstrip('\n')
... finally:
...     file.close()
 sim unit; branch; iteration; data level;           id; date;  SP;   BA;  HgM
   stand1;      0;         0;    stratum; o-stratum1-1; 2009; 1.0; 17.0; 19.0
   stand1;      0;         0;    stratum; o-stratum1-2; 2009; 2.0;  8.0; 18.5
   stand1;      0;         1;    stratum; o-stratum1-1; 2009; 1.0; 17.0; 19.0
   stand1;      0;         1;    stratum; o-stratum1-2; 2009; 2.0;  8.0; 18.5
>>> const_obj.variables = origvars
>>> out = OutputInline(testdb, 'result', ['stand1'], 'stratum',
...               'optimized',
...               'output/test/inlined.txt', TestLogger(),
...               const_obj, 1, False, True, aggr_obj,
...               ['cash_flow', 'Income', 'Scrapwood', 'Volume',
...               'BIOMASS_branches', 'BIOMASS_stumps'])
>>> try: 
...     file = open('output/test/inlined.txt', 'r')
...     for line in file:
...         print line.rstrip('\n')
... finally:
...     file.close()
 sim unit; branch; iteration; data level;           id; date; AREA;   SC
   stand1;      0;         0;  comp_unit;     o-stand1; 2009;  1.0;  3.0
 sim unit; branch; iteration; data level;           id; date;   SP;   BA;  HgM
   stand1;      0;         0;    stratum; o-stratum1-1; 2009;  1.0; 17.0; 19.0
 sim unit; branch; iteration; data level;           id; date;    d;    h
   stand1;      0;         0;       tree;  o-tree1-1-1; 2009; 17.0; 18.0
 sim unit; branch; iteration; data level;           id; date;   SP;   BA;  HgM
   stand1;      0;         0;    stratum; o-stratum1-2; 2009;  2.0;  8.0; 18.5
 sim unit; branch; iteration; data level;           id; date;    d;    h
   stand1;      0;         0;       tree;  o-tree1-2-1; 2009; 22.0; 21.0
 sim unit; branch; iteration; data level;           id; date; AREA;   SC
   stand1;      0;         1;  comp_unit;     o-stand1; 2009;  1.0;  3.0
 sim unit; branch; iteration; data level;           id; date;   SP;   BA;  HgM
   stand1;      0;         1;    stratum; o-stratum1-1; 2009;  1.0; 17.0; 19.0
 sim unit; branch; iteration; data level;           id; date;    d;    h
   stand1;      0;         1;       tree;  o-tree1-1-1; 2009; 17.0; 18.0
 sim unit; branch; iteration; data level;           id; date;   SP;   BA;  HgM
   stand1;      0;         1;    stratum; o-stratum1-2; 2009;  2.0;  8.0; 18.5
 sim unit; branch; iteration; data level;           id; date;    d;    h
   stand1;      0;         1;       tree;  o-tree1-2-1; 2009; 22.0; 21.0

def run(self):

Uses data from init to run the class-specific output (self.inlined)

def _get_strings(self, constraints=None, dates=None, date_res=0, level=1, sim_unit=None):

Returns a formatted table as a list of rows, starting from the given level, using the given constraints (a dictionary of key: [op, good value, good value, ...] pairs (several values only if op==’in’)), date range (dates = (datetime.date(X,Y,Z), datetime.date(X,Y,Z)), date resolution (0 = year, 1 = full date) and simulation unit (this shouldn’t be set by the user)

>>> result = out._get_strings([('iteration', 'eq', 0)], (datetime.date(2008, 1, 1), datetime.date(2011, 12, 30)), 0, 3, 'test')
>>> pprint(result)
[' sim unit; branch; iteration; data level;           id; date;    d;    h',
 u'     test;      0;         0;       tree;  o-tree1-1-1; 2009; 17.0; 18.0',
 u'     test;      0;         0;       tree;  o-tree1-2-1; 2009; 22.0; 21.0',
 u'     test;      0;         0;       tree;  o-tree2-1-1; 2009; 30.0; 29.0',
 u'     test;      0;         0;       tree;  o-tree2-2-1; 2009; 26.0; 24.0',
 u'     test;      0;         0;       tree;  o-tree2-2-2; 2009; 28.0; 27.0']
>>> result = out._get_strings([('iteration', 'eq', 0)], (datetime.date(2008, 1, 1), datetime.date(2011, 12, 30)), 1, 3, 'test')
>>> pprint(result)
[' sim unit; branch; iteration; data level;           id;       date;    d;    h',
 u'     test;      0;         0;       tree;  o-tree1-1-1; 2009-02-07; 17.0; 18.0',
 u'     test;      0;         0;       tree;  o-tree1-2-1; 2009-02-07; 22.0; 21.0',
 u'     test;      0;         0;       tree;  o-tree2-1-1; 2009-02-08; 30.0; 29.0',
 u'     test;      0;         0;       tree;  o-tree2-2-1; 2009-02-08; 26.0; 24.0',
 u'     test;      0;         0;       tree;  o-tree2-2-2; 2009-02-08; 28.0; 27.0']
>>> out._get_strings([('iteration', 'eq', 0)], (datetime.date(2011, 5, 7), datetime.date(2011, 12, 30)), 0, 3, 'test')
[]
>>> out._get_strings([('iteration', 'eq', 0)], (datetime.date(2008, 5, 7), datetime.date(2008, 12, 30)), 0, 3, 'test')
[]
>>> result2 = out._get_strings(dates=(datetime.date(2008, 1, 1), datetime.date(2011, 12, 30)), date_res=0)
>>> for item in out._get_strings():
...     found = False
...     for ritem in result2:
...         if ritem == item:
...             found = True
...     if not found:
...         print item, 'not found!'

If your result_variables.xml skips a level, the others will still be output:

>>> old_const = out.output_level_constraint
>>> out.output_level_constraint = set((1, 3))
>>> pprint(out._get_strings())
[' sim unit; branch; iteration; data level;           id;       date; AREA;   SC',
 u'   stand1;      0;         0;  comp_unit;     o-stand1;       2009;  1.0;  3.0',
 ' sim unit; branch; iteration; data level;           id;       date;    d;    h',
 u'   stand1;      0;         0;       tree;  o-tree1-1-1;       2009; 17.0; 18.0',
 u'   stand1;      0;         0;       tree;  o-tree1-2-1;       2009; 22.0; 21.0',
 ' sim unit; branch; iteration; data level;           id;       date; AREA;   SC',
 u'   stand1;      0;         1;  comp_unit;     o-stand1;       2009;  1.0;  3.0',
 ' sim unit; branch; iteration; data level;           id;       date;    d;    h',
 u'   stand1;      0;         1;       tree;  o-tree1-1-1;       2009; 17.0; 18.0',
 u'   stand1;      0;         1;       tree;  o-tree1-2-1;       2009; 22.0; 21.0',
 ' sim unit; branch; iteration; data level;           id;       date; AREA;   SC',
 u'   stand2;      0;         0;  comp_unit;     o-stand2;       2009;  2.0;  2.0',
 ' sim unit; branch; iteration; data level;           id;       date;    d;    h',
 u'   stand2;      0;         0;       tree;  o-tree2-1-1;       2009; 30.0; 29.0',
 u'   stand2;      0;         0;       tree;  o-tree2-2-1;       2009; 26.0; 24.0',
 u'   stand2;      0;         0;       tree;  o-tree2-2-2;       2009; 28.0; 27.0',
 ' sim unit; branch; iteration; data level;           id;       date; AREA;   SC',
 u'   stand2;      0;         1;  comp_unit;     o-stand2;       2009;  2.0;  2.0',
 ' sim unit; branch; iteration; data level;           id;       date;    d;    h',
 u'   stand2;      0;         1;       tree;  o-tree2-1-1;       2009; 30.0; 29.0',
 u'   stand2;      0;         1;       tree;  o-tree2-2-1;       2009; 26.0; 24.0',
 u'   stand2;      0;         1;       tree;  o-tree2-2-2;       2009; 28.0; 27.0',
 ' sim unit; branch; iteration; data level;           id;       date; AREA;   SC',
 u'   stand2;      1;         0;  comp_unit;     o-stand2;       2009;  2.0;  2.0',
 u'   stand2;      1;         1;  comp_unit;     o-stand2;       2009;  2.0;  2.0']
>>> out.output_level_constraint = old_const

def inlined(self, file, constraints=None, dates=None, date_res=0):

This function will get the datadb strings (with _get_strings, using the given restrictions and date resolution) and print them to the given file.

>>> out.inlined('output/test/inlined.txt')
>>> try:
...     file = open('output/test/inlined.txt', 'r')
...     for line in file:
...         print line.rstrip('\n')
... finally:
...     file.close()
 sim unit; branch; iteration; data level;           id; date; AREA;   SC
   stand1;      0;         0;  comp_unit;     o-stand1; 2009;  1.0;  3.0
 sim unit; branch; iteration; data level;           id; date;   SP;   BA;  HgM
   stand1;      0;         0;    stratum; o-stratum1-1; 2009;  1.0; 17.0; 19.0
 sim unit; branch; iteration; data level;           id; date;    d;    h
   stand1;      0;         0;       tree;  o-tree1-1-1; 2009; 17.0; 18.0
 sim unit; branch; iteration; data level;           id; date;   SP;   BA;  HgM
   stand1;      0;         0;    stratum; o-stratum1-2; 2009;  2.0;  8.0; 18.5
 sim unit; branch; iteration; data level;           id; date;    d;    h
   stand1;      0;         0;       tree;  o-tree1-2-1; 2009; 22.0; 21.0
 sim unit; branch; iteration; data level;           id; date; AREA;   SC
   stand1;      0;         1;  comp_unit;     o-stand1; 2009;  1.0;  3.0
 sim unit; branch; iteration; data level;           id; date;   SP;   BA;  HgM
   stand1;      0;         1;    stratum; o-stratum1-1; 2009;  1.0; 17.0; 19.0
 sim unit; branch; iteration; data level;           id; date;    d;    h
   stand1;      0;         1;       tree;  o-tree1-1-1; 2009; 17.0; 18.0
 sim unit; branch; iteration; data level;           id; date;   SP;   BA;  HgM
   stand1;      0;         1;    stratum; o-stratum1-2; 2009;  2.0;  8.0; 18.5
 sim unit; branch; iteration; data level;           id; date;    d;    h
   stand1;      0;         1;       tree;  o-tree1-2-1; 2009; 22.0; 21.0
 sim unit; branch; iteration; data level;           id; date; AREA;   SC
   stand2;      0;         0;  comp_unit;     o-stand2; 2009;  2.0;  2.0
 sim unit; branch; iteration; data level;           id; date;   SP;   BA;  HgM
   stand2;      0;         0;    stratum; o-stratum2-1; 2009;  2.0; 24.0; 31.0
 sim unit; branch; iteration; data level;           id; date;    d;    h
   stand2;      0;         0;       tree;  o-tree2-1-1; 2009; 30.0; 29.0
 sim unit; branch; iteration; data level;           id; date;   SP;   BA;  HgM
   stand2;      0;         0;    stratum; o-stratum2-2; 2009;  3.0; 12.0; 26.0
 sim unit; branch; iteration; data level;           id; date;    d;    h
   stand2;      0;         0;       tree;  o-tree2-2-1; 2009; 26.0; 24.0
   stand2;      0;         0;       tree;  o-tree2-2-2; 2009; 28.0; 27.0
 sim unit; branch; iteration; data level;           id; date; AREA;   SC
   stand2;      0;         1;  comp_unit;     o-stand2; 2009;  2.0;  2.0
 sim unit; branch; iteration; data level;           id; date;   SP;   BA;  HgM
   stand2;      0;         1;    stratum; o-stratum2-1; 2009;  2.0; 24.0; 31.0
 sim unit; branch; iteration; data level;           id; date;    d;    h
   stand2;      0;         1;       tree;  o-tree2-1-1; 2009; 30.0; 29.0
 sim unit; branch; iteration; data level;           id; date;   SP;   BA;  HgM
   stand2;      0;         1;    stratum; o-stratum2-2; 2009;  3.0; 12.0; 26.0
 sim unit; branch; iteration; data level;           id; date;    d;    h
   stand2;      0;         1;       tree;  o-tree2-2-1; 2009; 26.0; 24.0
   stand2;      0;         1;       tree;  o-tree2-2-2; 2009; 28.0; 27.0
 sim unit; branch; iteration; data level;           id; date; AREA;   SC
   stand2;      1;         0;  comp_unit;     o-stand2; 2009;  2.0;  2.0
 sim unit; branch; iteration; data level;           id; date;   SP;   BA;  HgM
   stand2;      1;         0;    stratum; o-stratum2-3; 2009;  2.0;  0.0;  0.0
 sim unit; branch; iteration; data level;           id; date; AREA;   SC
   stand2;      1;         1;  comp_unit;     o-stand2; 2009;  2.0;  2.0
 sim unit; branch; iteration; data level;           id; date;   SP;   BA;  HgM
   stand2;      1;         1;    stratum; o-stratum2-3; 2009;  2.0;  0.0;  0.0