HWRF  trunk@4391
exhwrf_hycomab_archive.py
1 #! /bin/env python
2 
3 ##@namespace scripts.exhwrf_hycomab_archive
4 # Archives *.a and *.b files output by the hycom component of the
5 # coupled HWRF forecast job.
6 #
7 # This Python script will archive all *archv*.[ab] files found in the
8 # run directory of the forecast job, found via the
9 # hwrf_expt.runwrf.location. It is controlled by the [archive]
10 # section hycomab option, which must be set to an archive path of this
11 # syntax:
12 #
13 # @code
14 # [archive]
15 # hycomab=hpss:/NCEPDEV/2year/emc-marine/{ENV[USER]}/{SUBEXPT}/hycomab/{out_prefix}.tar
16 # @endcode
17 #
18 # The hycomab option, if present, will be expanded to get the path to
19 # the target archive location. The archive is then created with HTAR,
20 # sending the files that matched the *archv*.[ab] glob.
21 
22 import os, sys, glob
24 import hwrf.numerics
25 import hwrf_expt
26 
27 from produtil.log import jlogger
28 from produtil.run import exe, checkrun, run
29 
30 def main():
32  conf=hwrf_expt.conf
33  if not conf.has_option('archive','hycomab'):
34  jlogger.info('No hycomab option in [archive] section. '
35  'Will not make hycomab archive.')
36  sys.exit(0)
37 
38  logger=conf.log()
39  rundir=hwrf_expt.runwrf.location
40  abfiles=list()
41  with produtil.cd.NamedDir(rundir):
42  for globme in [ "*archv*.[ab]" ]:
43  globout=list(glob.glob(globme))
44  if not globout:
45  logger.warning('There are no files matching %s in %s'%(
46  globme, rundir))
47  abfiles.extend(globout)
48  if not abfiles:
49  # Send an error at critical level so it ends up in the jlogfile
50  logger.critical('Could not find any hycom output files. '
51  'Hycom failed. Will now cry.')
52  sys.exit(1)
53 
54  # List the files to archive at info level (stdout):
55  logger.info('Found %d hycom files:\n %s'%(
56  len(abfiles),"\n ".join(abfiles)))
57 
58  thearchive=conf.timestrinterp('archive','{hycomab}',0)
59  if thearchive[0:5]!='hpss:':
60  logger.error('The hycomab archive path must begin with "hpss:": '+
61  thearchive)
62  sys.exit(1)
63  thearchive=thearchive[5:]
64  adir=os.path.dirname(thearchive)
65 
66  # Send a message directly to the jlogfile about the archiving:
67  jlogger.info('%s: archiving %d hycom files to HPSS.'
68  %(thearchive,len(abfiles)))
69 
70  mkdir=exe(conf.getexe('hsi'))['-P','mkdir','-p',adir]
71  run(mkdir,logger=logger)
72  cmd=exe(conf.getexe('htar'))['-cpf',thearchive][abfiles]
73  checkrun(cmd,logger=logger)
74 
75 if __name__=='__main__':
76  try:
78  jlogger.info("HWRF hycomab archive job starting")
79  main()
80  jlogger.info("HWRF hycomab archive job completed")
81  except Exception as e:
82  jlogger.critical('HWRF hycomab archive is aborting: '+str(e),
83  exc_info=True)
84  sys.exit(2)
Change directory, handle temporary directories.
Definition: cd.py:1
Contains setup(), which initializes the produtil package.
Definition: setup.py:1
def init_module
Initializes the HWRF object structure.
Definition: hwrf_expt.py:384
A shell-like syntax for running serial, MPI and OpenMP programs.
Definition: run.py:1
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
This subclass of TempDir takes a directory name, instead of generating one automatically.
Definition: cd.py:228
Time manipulation and other numerical routines.
Definition: numerics.py:1
Configures logging.
Definition: log.py:1