Table Of Contents

Previous topic

caller.py

Next topic

managementcaller.py

This Page

geotablecaller.py

>>> from simo.builder.modelbase.modelbase import ModelbaseDef
>>> tdf = open('../../simulator/xml/schemas/Typedefs_SIMO.xsd')
>>> typedef = tdf.read()
>>> tdf.close()
>>> sf = open('../../simulator/xml/schemas/geo_table.xsd')
>>> schema = sf.read()
>>> sf.close()
>>> xml = u'''<geo_tables xmlns="http://www.simo-project.org/simo"
...     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
...     <geo_table name="dem" desc="Digital elevation model">
...         <filename>test.latlonarray</filename>
...         <ulx>21.0</ulx>
...         <uly>61.0</uly>
...         <nrow>3</nrow>
...         <ncol>3</ncol>
...         <xdim>0.5</xdim>
...         <ydim>0.5</ydim>
...         <nvar>1</nvar>
...         <coordinate_variables>
...             <longitude>
...                 <variable>GEO_X</variable>
...                 <level>comp_unit</level>
...             </longitude>
...             <latitude>
...                 <variable>GEO_Y</variable>
...                 <level>comp_unit</level>
...             </latitude>
...         </coordinate_variables>
...         <targets>
...             <target>
...                 <variable>ALT</variable>
...                 <level>comp_unit</level>
...             </target>
...         </targets>
...     </geo_table>
... </geo_tables>'''

>>> class Validator(object):
...     def add_error(msg):
...         print msg

>>> class Lexicon(object):
...     def __init__(self):
...         self.models = {}
...     def get_variable_ind(self, level, variable, active=False):
...         if variable == 'GEO_X':
...             return (1,1)
...         elif variable == 'GEO_Y':
...             return (1,2)
...         elif variable == 'ALT':
...             return (1,3)
...         elif variable == 'TS':
...             return (1,4)
...     def get_level_ind(self, level):
...         return 1
...     def add_model(self, mtype, mname):
...         if mtype not in self.models:
...             self.models[mtype] = set()
...         self.models[mtype].add(mname)

>>> mb = ModelbaseDef(typedef)
>>> mb.schema = ('geo_table', schema)
>>> mb.xml = ('testxml', xml, Lexicon(),
...           ['./builder/modelbase/test/data'], 'geo_table')
>>> mb.xml['geo_table']['testxml'][:11]
u'<geo_tables'

class GeoTableCaller(Caller):

Construct geo table model calls and process results.

def __init__(self):

Construct geo table from the parsed geo table xml:

>>> from simo.simulation.caller.geotablecaller import GeoTableCaller
>>> gt = mb.obj['geo_table']['testxml']['dem']
>>> gt.load_function(Validator())
>>> gtc = GeoTableCaller()

def __find_geo_indices(self, tind, coord, ulaxis, gridstep, maxsteps, axis):

Find indice from the geo table by coordinate variable values based on the upper left corner and spacing of the grid.

def __which_values_to_extract(self, gtable, params):

Which values to return from the geo table

def execute(self, gtable, params, sim, depth):

Execute a geo_table model for the data objects (objs) using the settings from the modelchain (param) for the simulator instance (sim):

>>> import numpy
>>> from lxml import etree
>>> from simo.builder.modelbase.geotable import GeoTableParam

>>> etind = numpy.array([[0,0,0,0,1], [0,0,1,1,2], [0,0,2,2,3], [0,0,3,3,4],
...                      [0,0,4,4,5]], dtype=int)
>>> class Data(object):
...     var_name = {1:{1:'GEO_X', 2:'GEO_Y', 3:'ALT', 4:'TS'}}
...     level_name = {1:'comp_unit', 2:'stratum'}
...     obj_id = numpy.array([['o1'], ['o2'], ['o3'], ['o4'], ['o5']])
...     def get_value(self, tind, attr):
...         size = len(tind[:,0])
...         if attr == 1:
...             if size == 3:
...                 return numpy.array((21.1, 21.1, 22.1)), None
...             elif size == 4:
...                 return numpy.array((21.1, 21.1, 23.1, 21.1)), None
...             elif size == 5:
...                 val = numpy.array((21.1, None, None, None, None),
...                                    dtype=float)
...                 err = ([('missing value!', [1,2,3,4])], etind,
...                        set([1,2,3,4]))
...                 return val, err
...         elif attr == 2:
...             if size == 3:
...                 return numpy.array((59.9, 60.4, 60.9)), None
...             elif size == 4:
...                 return numpy.array((59.9, 64.0, 60.9, 60.4)), None
...             elif size == 5:
...                 val = numpy.array((59.9, None, None, None, None),
...                                    dtype=float)
...                 err = ([('missing value!', [1,2,3,4])], etind,
...                        set([1,2,3,4]))
...                 return val, err
...     def get_tind(self, level, depth):
...         tind = numpy.zeros((depth, 5), dtype=int)
...         tind[:,2] = range(depth)
...         return tind, set([])

>>> from minimock import Mock
>>> sim = Mock('Simulator')
>>> sim.level = 1
>>> sim.value_fixing = False
>>> sim.data = Data()
>>> sim.data.set_value = Mock('Simulator.data.set_value')

>>> pelem = etree.XML('''<geo_table><only_vars><var><name>ALT</name>
...               <level>comp_unit</level></var></only_vars>
...               </geo_table>''')
>>> execfile('simulation/caller/test/mocktask.py')
>>> param = GeoTableParam('', pelem, task, gt)

Calling with valid data:

>>> gtc.execute(gt, param, sim, 3)
Called Simulator.data.set_value(
    1,
    array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0]]),
    3,
    array([ 100.,  103.,  108.], dtype=float32),
    'dem',
    False)

Calling with incomplete coordinate data:

>>> gtc.execute(gt, param, sim, 5)
Called Simulator.add_error(
    'geo table caller: missing value!',
    None,
    array([[0, 0, 1, 1, 2],
       [0, 0, 2, 2, 3],
       [0, 0, 3, 3, 4],
       [0, 0, 4, 4, 5]]))
Called Simulator.add_error(
    'geo table caller: outside bound X coordinates',
    None,
    array([[0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0],
       [0, 0, 3, 0, 0],
       [0, 0, 4, 0, 0]]))
Called Simulator.add_error(
    'geo table caller: missing value!',
    None,
    array([[0, 0, 1, 1, 2],
       [0, 0, 2, 2, 3],
       [0, 0, 3, 3, 4],
       [0, 0, 4, 4, 5]]))
Called Simulator.add_error(
    'geo table caller: outside bound Y coordinates',
    None,
    array([[0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0],
       [0, 0, 3, 0, 0],
       [0, 0, 4, 0, 0]]))
Called Simulator.data.set_value(
    1,
    array([[0, 0, 0, 0, 0]]),
    3,
    array([ 100.], dtype=float32),
    'dem',
    False)

Calling “with out of bounds” values:

>>> gtc.execute(gt, param, sim, 4)
Called Simulator.add_error(
    'geo table caller: outside bound X coordinates',
    None,
    array([[0, 0, 2, 0, 0]]))
Called Simulator.add_error(
    'geo table caller: outside bound Y coordinates',
    None,
    array([[0, 0, 1, 0, 0]]))
Called Simulator.data.set_value(
    1,
    array([[0, 0, 0, 0, 0],
       [0, 0, 3, 0, 0]]),
    3,
    array([ 100.,  103.], dtype=float32),
    'dem',
    False)

Calling with invalid evaluation level:

>>> sim.data.values = None
>>> sim.level = 2
>>> gtc.execute(gt, param, sim, 3)  
 Called Simulator.lexicon.get_level_name(2)
 Called Simulator.lexicon.get_level_name(1)
 Called Simulator.lexicon.get_variable_name(1, 3)
 Called Simulator.add_error(
     "geo table caller: Level of (None) target variable 'None'
     is different from model chain evaluation level (None)",
     None,
     array([[0, 0, 0, 0, 0],
        [0, 0, 1, 0, 0],
        [0, 0, 2, 0, 0]]))