HWRF  trunk@4391
exhwrf_post.py
1 #! /usr/bin/env python
2 
3 ##@namespace scripts.exhwrf_post
4 # Runs the Unified Post Processor on the output of the
5 # exhwrf_forecst script. Note that this script is restartable: if it
6 # fails, and you call it again, it will pick up where it left off. To
7 # force a reprocessing of the entire post-processing system, call
8 # exhwrf_unpost first.
9 
10 import os, produtil.cd, produtil.sigsafety, cProfile, logging, time
12 from produtil.log import jlogger
13 from produtil.cd import NamedDir
14 from produtil.datastore import COMPLETED, FAILED, UNSTARTED
15 
16 def done(task):
17  """!Is this task done?
18  @param task a produtil.datastore.Task
19  @returns True if the task's state is equal to
20  produtil.datastore.COMPLETED or produtil.datastore.FAILED. Will
21  return False for any other status."""
22  state=task.getstate()
23  return state == COMPLETED or state==FAILED
24 
25 def post():
26  """!Runs the post-processor"""
28  jlogger.info('starting post')
29  import hwrf_expt
31 
32  run_copier=hwrf_expt.conf.getbool('config','post_runs_wrfcopier',False)
33  run_satpost=hwrf_expt.conf.getbool('config','run_satpost',True)
34 
35  # Make sure we check all tasks to see if they're posted:
36  hwrf_expt.nonsatpost.state=UNSTARTED
37  hwrf_expt.satpost.state=UNSTARTED
38 
39  if run_copier:
40  hwrf_expt.wrfcopier.state=UNSTARTED
41 
42  logger=logging.getLogger('exhwrf_post')
43 
44  # Change to a temp directory to run the post:
45  with NamedDir(hwrf_expt.WORKhwrf,logger=logger) as t:
46  #hwrf_expt.ds.dump() # dump entire database state to stdout
47  alldone=False
48  while not alldone:
49  before=int(time.time())
50  if run_copier:
51  if not done(hwrf_expt.wrfcopier):
52  hwrf_expt.wrfcopier.runpart()
53  if not done(hwrf_expt.nonsatpost): hwrf_expt.nonsatpost.runpart()
54  if not done(hwrf_expt.nonsatpost): hwrf_expt.nonsatpost.runpart()
55  if run_satpost:
56  if not done(hwrf_expt.satpost): hwrf_expt.satpost.runpart()
57  if not done(hwrf_expt.nonsatpost): hwrf_expt.nonsatpost.runpart()
58  alldone = ( done(hwrf_expt.satpost) or not run_satpost ) \
59  and done(hwrf_expt.nonsatpost) \
60  and ( not run_copier or done(hwrf_expt.wrfcopier) )
61  after=int(time.time())
62  took=after-before
63  threshold=5
64  sleeptime=20
65  if took < threshold:
66  logger.info(
67  'Post loop iteration took only %d seconds, which is '
68  'less than the threshold of %d seconds. Will sleep '
69  '%d seconds.'%(took,threshold,sleeptime))
70  time.sleep(sleeptime)
71  else:
72  logger.info('Post loop iteration took %d seconds, '
73  'which is above the threshold of %d. '
74  'Sleeping only one second.'%(took,threshold))
75  time.sleep(1) # avoid thrash loop in case of logic error
76  logger.info('Done sleeping.')
77 
78  jlogger.info('completed post')
79 
80 if __name__=='__main__':
81  post()
82 #cProfile.run('post()') # use instead of post() to get profiling info
Change directory, handle temporary directories.
Definition: cd.py:1
Contains setup(), which initializes the produtil package.
Definition: setup.py:1
def done(task)
Is this task done?
Definition: exhwrf_post.py:16
Sets up signal handlers to ensure a clean exit.
Definition: sigsafety.py:1
def init_module
Initializes the HWRF object structure.
Definition: hwrf_expt.py:384
def setup(ignore_hup=False, dbnalert_logger=None, jobname=None, cluster=None, send_dbn=None, thread_logger=False, thread_stack=2 **24, kwargs)
Initializes the produtil package.
Definition: setup.py:15
Stores products and tasks in an sqlite3 database file.
Definition: datastore.py:1
This subclass of TempDir takes a directory name, instead of generating one automatically.
Definition: cd.py:228
def post()
Runs the post-processor.
Definition: exhwrf_post.py:25
Configures logging.
Definition: log.py:1