FLEXPART Testing Environment CTBTO WO8
 All Classes Namespaces Files Functions Variables Pages
Public Member Functions | Private Attributes | List of all members
flextest.FlexpartCase.FlexpartCase Class Reference
Inheritance diagram for flextest.FlexpartCase.FlexpartCase:
Inheritance graph
[legend]
Collaboration diagram for flextest.FlexpartCase.FlexpartCase:
Collaboration graph
[legend]

Public Member Functions

def __init__
 
def run
 
def success
 
def execution_time_seconds
 

Private Attributes

 _src_dir
 
 _dest_dir
 
 _met_dir
 
 _met_nest_dir
 
 _flexpart_exe
 
 _destdir_ok
 
 _execution_time_seconds
 

Detailed Description

Definition at line 32 of file FlexpartCase.py.

Constructor & Destructor Documentation

def flextest.FlexpartCase.FlexpartCase.__init__ (   self,
  src_dir = None,
  dest_dir = None,
  met_dir = None,
  met_nest_dir = None,
  flexpart_exe = None 
)
Set up the class

src_dir : a directory structure all set to run FLEXPART.  Note that
the paths in pathnames must be all relative within this directory.
This directory will also need to contain the met data in directory
met_data

dest_dir : location that src_dir will be copied to, and where the
run will be executed.  dest_dir cannot already exist.  We make
this restriction to prevent accidental overwriting of something
important.

met_dir : location of met files.  Assumed to have a valid 
AVAILABLE file in it


flexpart_exe : full path to a FLEXPART executable

Definition at line 38 of file FlexpartCase.py.

38 
39  met_dir=None, met_nest_dir=None, flexpart_exe=None):
40 
41  """Set up the class
42 
43  src_dir : a directory structure all set to run FLEXPART. Note that
44  the paths in pathnames must be all relative within this directory.
45  This directory will also need to contain the met data in directory
46  met_data
47 
48  dest_dir : location that src_dir will be copied to, and where the
49  run will be executed. dest_dir cannot already exist. We make
50  this restriction to prevent accidental overwriting of something
51  important.
52 
53  met_dir : location of met files. Assumed to have a valid
54  AVAILABLE file in it
55 
56 
57  flexpart_exe : full path to a FLEXPART executable
58  """
59 
60  # 2016-01-15 - DJM - this is something Delia and Christian introduced,
61  # but not sure why. I have modified logic so it's not needed.
62  #symlink_nest = False
63 
64  # test for a src_dir argument
65  if src_dir:
66 
67  # Test that src_dir is found
68  if os.path.isdir(src_dir):
69  self._src_dir = src_dir
70  else:
71  msg = 'src_dir not found: ' + src_dir
72  raise Exception(msg)
73  else:
74  msg = 'No src_dir argument'
75  raise Exception(msg)
76 
77  # test for a dest_dir argument
78  if dest_dir:
79  # Make sure it doesn't already exist
80  if os.path.isdir(dest_dir):
81  msg = 'dest_dir cannot already exist: ' + dest_dir
82  raise Exception(msg)
83  else:
84  self._dest_dir = dest_dir
85  else:
86  msg = 'No dest_dir argument'
87  raise Exception(msg)
88 
89  # test for a met_dir argument
90  if met_dir:
91  # Make sure it doesn't already exist
92  if os.path.isdir(met_dir):
93  self._met_dir = met_dir
94  else:
95  msg = 'met_dir not found: ' + met_dir
96  raise Exception(msg)
97  else:
98  msg = 'No met_dir argument'
99  raise Exception(msg)
100 
101  # test for a met_nest_dir argument
102  if met_nest_dir:
103  # Make sure it doesn't already exist
104  if os.path.isdir(met_nest_dir):
105  self._met_nest_dir = met_nest_dir
106  else:
107  msg = 'met_nest_dir not found: ' + met_nest_dir
108  raise Exception(msg)
109 
110  # Test that flexpart_exe exists and is executable
111  if flexpart_exe:
112  # Make sure it exists and is executable
113  if os.path.isfile(flexpart_exe) and os.access(flexpart_exe, os.X_OK):
114  self._flexpart_exe = flexpart_exe
115  else:
116  msg = 'flexpart_exe does not exist and/or is not executable'
117  raise Exception(msg)
118  else:
119  msg = 'No flexpart_exe argument'
120  raise Exception(msg)
121 
122 
124  self._destdir_ok = False # This indicates if copy to destdir was ok
125  init_success = True
126  # Make sure destdir does not already exist
127  if not os.path.isdir(dest_dir):
128  # Copy the distribution from src_dir into dest_dir and validate success
129  try:
130  #print src_dir, dest_dir
131  shutil.copytree(src_dir, dest_dir)
132  logging.info('FlexpartCase: copying case dir to temp location')
133 
134  # Compare the directories. Success would be indicated
135  # by an empty list diffs.diff_files
136  diffs = filecmp.dircmp(src_dir, dest_dir)
137  if diffs.diff_files:
138  logging.warning('FlexpartCase: src and dest differ')
139  init_success = False
140 
141 
142  except:
143  init_success = False
144  logging.warning('FlexpartCase: copy failed')
145  else:
146  init_success = False
147  logging.warning('FlexpartCase: dest_dir exists')
148 
149  if init_success:
150 
151  # Check that important files are present. If not, go ahead
152  # and try to make it, raise exception if fails
153  if not os.path.isdir(dest_dir + '/output'):
154  # Try to make one
155  try:
156  os.mkdir(dest_dir + '/output', 0755)
157  except:
158  msg = 'No output dir present in case directory'
159  raise Exception(msg)
160 
161  # Make a link to the met_dir
162  try:
163  os.symlink(self._met_dir, dest_dir + '/met_data')
164  except:
165  msg = 'Unable to create met_data symlink to: ' + self._met_dir
166  raise Exception(msg)
167 
168  # Make a link to the met_nest_dir
169  if met_nest_dir:
170  try:
171  os.symlink(self._met_nest_dir, dest_dir + '/met_data_nest')
172  except:
173  msg = 'Unable to create met_data_nest symlink to: ' + \
174  self._met_nest_dir
175  raise Exception(msg)
176 
177  # Make sure there is an AVAILABLE file
178  if not os.path.isfile(dest_dir + '/met_data/AVAILABLE'):
179  msg = 'Unable to find AVAILABLE in ' + dest_dir + '/met_data'
180  raise Exception(msg)
181 
182  if met_nest_dir:
183  if not os.path.isfile(dest_dir + '/met_data_nest/AVAILABLE'):
184  msg = 'Unable to find AVAILABLE in ' + dest_dir + '/met_data_nest'
185  raise Exception(msg)
186 
187 
188 
189 
190  self._destdir_ok = True
191 
192  # Make a link to the met directory
193 
194  self._execution_time_seconds = -9999.0
195 
196 
197 
198 

Member Function Documentation

def flextest.FlexpartCase.FlexpartCase.execution_time_seconds (   self)
Returns value of execution time in seconds

Definition at line 269 of file FlexpartCase.py.

270  def execution_time_seconds(self):
271 
272  """Returns value of execution time in seconds"""
273 
274  return self._execution_time_seconds
275 
276 
def flextest.FlexpartCase.FlexpartCase.run (   self)
Runs the case - assumes that the original case template was 
in good shape and that it was copied successfully.  The variable
self._destdir_ok provides a very simple, but incomplete check.

This routine will launch the executable, and then wait for it to 
complete.  stdout will go to file stdout.txt at the top of the
dest_dir.

Definition at line 199 of file FlexpartCase.py.

200  def run(self):
201  """Runs the case - assumes that the original case template was
202  in good shape and that it was copied successfully. The variable
203  self._destdir_ok provides a very simple, but incomplete check.
204 
205  This routine will launch the executable, and then wait for it to
206  complete. stdout will go to file stdout.txt at the top of the
207  dest_dir.
208  """
209 
210  stdoutFH = open(self._dest_dir + '/stdout.txt', 'w')
211 
212  start_time = time.time()
213  the_process = subprocess.Popen( self._flexpart_exe, shell=True,
214  stdout=stdoutFH,
215  cwd=self._dest_dir )
216 
217  status=the_process.wait()
218  stop_time = time.time()
219 
220  self._execution_time_seconds = stop_time - start_time
221 
222  stdoutFH.close()
223 
224  return True
def flextest.FlexpartCase.FlexpartCase.success (   self)
Looks at last line of stdout and insures it contains some of the
expected key words.  This is a very simple test and, in the future
should be replaced with something more robust that insures the expected 
output files are all present

Definition at line 225 of file FlexpartCase.py.

226  def success(self):
227  """Looks at last line of stdout and insures it contains some of the
228  expected key words. This is a very simple test and, in the future
229  should be replaced with something more robust that insures the expected
230  output files are all present
231  """
232 
233  so_far_so_good = True
234 
235  # Try to open the file for reading
236  try:
237  stdoutFH = open(self._dest_dir + '/stdout.txt', 'r')
238  except:
239  so_far_so_good = False
240 
241  if so_far_so_good:
242  # Try to get the contents of the last line and store in tokens
243  try:
244  for the_line in stdoutFH:
245  pass
246  last_line = the_line
247  stdoutFH.close()
248 
249  #print last_line
250 
251  tokens = last_line.split()
252  except:
253  so_far_so_good = False
254 
255 
256  # If no errors so far, check for several expected keywords in the
257  # tokens from the last line of the stdout file
258  if so_far_so_good and ('CONGRATULATIONS:' and \
259  'SUCCESSFULLLY' and 'COMPLETED' \
260  and 'FLEXPART' in tokens):
261  #print 'SUCCESS!!'
262  return_val = True
263  else:
264  #print 'FAILED!!'
265  return_val = False
266 
267  return return_val
268 

Member Data Documentation

flextest.FlexpartCase.FlexpartCase._dest_dir
private

Definition at line 83 of file FlexpartCase.py.

flextest.FlexpartCase.FlexpartCase._destdir_ok
private

Definition at line 123 of file FlexpartCase.py.

flextest.FlexpartCase.FlexpartCase._execution_time_seconds
private

Definition at line 193 of file FlexpartCase.py.

flextest.FlexpartCase.FlexpartCase._flexpart_exe
private

Definition at line 113 of file FlexpartCase.py.

flextest.FlexpartCase.FlexpartCase._met_dir
private

Definition at line 92 of file FlexpartCase.py.

flextest.FlexpartCase.FlexpartCase._met_nest_dir
private

Definition at line 104 of file FlexpartCase.py.

flextest.FlexpartCase.FlexpartCase._src_dir
private

Definition at line 68 of file FlexpartCase.py.


The documentation for this class was generated from the following file: