Menu

Python Run Script

Maarten Krol

The Python Run Script

We focus on the first lines first:

 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, os, glob, rc
from optparse import OptionParser
sys.dont_write_bytecode = True

parser = OptionParser(usage='%prog [options] rc_file')
parser.add_option('-s', '--start', dest='startTime')
parser.add_option('-e', '--end', dest='endTime')
# Should we generate emission?
parser.add_option('--emis', dest='emission', action="store_true", default=False)
# Should we compile the model?
parser.add_option('--compile', dest='compile', action="store_true", default=False)
# Are there rc keys in need of modification?
parser.add_option('-m', '--modify', action="append", dest='rc_args')
(options, args) = parser.parse_args()

os.environ['pyshell.rc'] = os.path.realpath(args[0])
# Add the proper python paths
rcf = rc.RcFile(os.environ['pyshell.rc'])
base_py_dir = os.path.join(rcf.get('my.home'), rcf.get('my.tm5.base'), 'py')
f2py_py_dir = os.path.join(rcf.get('my.home'), rcf.get('my.tm5.base'), 'f2py', 'lib')
proj_py_dir = os.path.join(rcf.get('my.home'), rcf.get('my.proj.root'), 'tracer',         rcf.get('my.tracer'), 'py')
sys.path.append(base_py_dir)
sys.path.append(f2py_py_dir)
sys.path.append(proj_py_dir)

Handling the actions is done by python, a general purpose programming language. First, the import statements add functionality to python, like OptionParser. This functionality os then used to process the call. Notice that python is object-oriented, so the object parser is created, and operations can be performed upon it.
Important functionality comes from the module rc. In fact, I found out that the module rc has to be present in your PYTHONPATH. On my system, I can use ipython and type:

import rc
rc?

And I see:

Type:       module
String Form:<module 'rc' from '/home/sbasu/pythonpackages/lib/python2.7/site-packages/rc.pyc'>
File:       /home/sbasu/pythonpackages/lib/python2.7/site-packages/rc.py
Docstring:
Deal with model settings in `rc` format.

RCFILES

A rcfile is a text file with key/value pairs seperated by a ':', e.g.

  my.flag    :  T
  my.answer  :  42

After parsing the header, the python-script assigns our .rc file to the environment variable pyshell.rc. Thus, all options are set (and can be read) in the file rc/rc/nam1x1-dummy_tr.rc.

The next thing that is done, is that the search path is extended, such that extra functionality can be added. I guess that the order in which this is done is important. I expect that these values are thus set in the .rc file, so lets have a look:

! common folders for this tree
#include include/folders.rc

! common job characteristics
#include include/jobs.rc

! variables unique to this run
my.project  : test_project
my.runmode  : 1 ! 1 for RUN_FORWARD, 2 for RUN_BACKWARD
par.openmp  : True
......etc.....

No keys like my.home, my.tm5.base, my.proj.root. I appears that (for readability) these settings are in #include statements, as you see. Indeed, the needed keys are found in include/folders.rc.

Lets browse further down in the run_fwd.py:

1
2
3
4
5
6
7
8
9
#!/usr/bin/env python
# -*- coding: utf-8 -*-
...
d1 = options.startTime.split(',')
d2 = options.endTime.split(',')
d1 = tuple([int(x) for x in d1])
d2 = tuple([int(x) for x in d2])
from RunTM5 import RunTM5
r = RunTM5(d1, d2)

This means that the supplied dates are turned into python variables and a RunTM5 object r is created with the start and end times as arguments. Further down in the script we see that such a RunTM5 object has so-called methods that can operate on it. That makes it very convenient, also to run it interactively using ipython.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#!/usr/bin/env python
# -*- coding: utf-8 -*-
...
r = RunTM5(d1, d2)
...
r.SetupEmissions(Emission)
...
r.Compile()
...
r.RunForward()

Time for Sourish to explain the following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/usr/bin/env python
# -*- coding: utf-8 -*-
if options.emission:
    emis_module = r.rcf.get('my.emission.class')
    _temp = __import__('Emissions', fromlist=[emis_module])
    try:
        Emission = _temp.__dict__[emis_module]
    except KeyError:
        sys.stderr.write("Class %s not defined in %s\n"%(emis_module,'Emission'))
        sys.exit()
    except:
        sys.stderr.write("Unknown error importing %s\n"%emis_module)
        sys.exit()
    r.SetupEmissions(Emission)

[How the general use of Emission class is organized]

Now lest move on and look a the output generated by r.SetupEmissions(Emission)

[SetupEmissions]


Related

Wiki: A guide to run with hurdles
Wiki: Home
Wiki: How the general use of Emission class is organized
Wiki: SetupEmissions

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.