Python Interface to MAPL: Difference between revisions

Line 56: Line 56:
     mapl.go()
     mapl.go()


== A python component with Children in Fortran ==
== A python component with Children implemented in Fortran ==


In this example, the '''GEOSgcs''' component is implemented in Python, with its children, components '''ANA''' and '''GCM''', implemented in Fortran.
In this example, the '''GEOSgcs''' component is implemented in Python, with its children, components '''ANA''' and '''GCM''', implemented in Fortran.
Line 94: Line 94:


Notice that the standard '''Initialize()''', '''Run()''' and '''Finalize()''' methods are inherited from the base class '''MAPL.GridComp'''.
Notice that the standard '''Initialize()''', '''Run()''' and '''Finalize()''' methods are inherited from the base class '''MAPL.GridComp'''.
== A python component with Children implemented in Python ==
In this example, the '''GEOSgcs''' component is implemented in Python, with its children, components '''ANA''' and '''GCM''', also implemented in Python.
In this case we implement the standard '''Run()''' method, but inherit '''Initialize()''' and '''Finalize()''' from the base class '''MAPL.GridComp'''
import MAPL
import GEOSana
import GEOSgcm
class GridComp(MAPL.GridComp):
    """
    Implements the GEOSgcs component by subclassing MAPL.GridComp.
    """
    def SetServices(self):
        """
        SetServices for a composite component.
        """
 
#      Instantiate Children component
#      ------------------------------
        ana = GEOSana()
        gcm = GEOSgcm()
#      Adopt children
#      --------------
        self.AddChild(ana)
        self.AddChild(gcm)
#      Set connectivity among children
#      -------------------------------
        vars = ('Tinc', 'Sinc', 'Uinc', 'Vinc', 'SSHinc' )
        self.AddConnectivity(src=[ana,vars], dst=[gcm,vars])
        self.AddConnectivity(src=[gcm,'LWI'], dst=[ana,'ORO'])
#      Run the base class SetServices
#      ------------------------------
        MAPL.GridComp.__SetServices__(self)
    def Run(self):
        """
        Implementation of a simple run method.
        """
       
#      Run the base class method
#      -------------------------
        MAPL.GridComp.__Run__(self)
#      Retrieve some data from a state
#      -------------------------------
        uwnd, vwnd, = self.Export.GetPointer(['UWND', 'VWND'])
#      Just print it
#      -------------
        print "u-wind = ", uwnd.min(), uwnd.max()
        print "v-wind = ", vwnd.min(), vwnd.max()