HWRF  trunk@4391
hwrf_get_ens_wave.py
1 #! /usr/bin/env python
2 
3 import tempfile
4 import subprocess
5 import os
6 import re
7 import logging
8 import sys
9 import getopt
10 
11 import produtil.cd
12 import hwrf.archive
13 
14 basedir='/NCEPDEV/hpssuser/g01/hurpara/hwrf_trunk_2013_ens_GRIB2'
15 verbosity=logging.INFO
16 namere=re.compile('([a-zA-Z0-9_-]+)([0-9][0-9])([a-zA-Z])\.([0-9]{10})')
17 
18 ########################################################################
19 # Utility functions
20 
21 def guess_wave_filenames(path,maxdom=3):
22  arbase=os.path.basename(path)
23  # Get the storm name, cycle, ID and basin:
24  m=namere.search(arbase)
25  if m is None:
26  return []
27  base="%s%s%s.%s.out4wave" % m.group(1,2,3,4) # Get beginning of all out4wave filenames
28  return ["%s_d%02d" % (base,1+i) for i in xrange(maxdom) ] # append domain number and return
29 
30 def initlogging():
31  logging.basicConfig(level=verbosity,format='%(asctime)s %(levelname)8s - %(message)s')
32 
33 def usage(why=None):
34  logging.critical("""FORMAT: hwrf_get_ens_wave.py [options] stormprefix cycle
35 Example: hwrf_get_ens_wave.py humberto09l 2013091306
36 
37 Will grab the data for humberto09l cycle 2013091306 in a subdirectory
38 humberto09l.2013091306.out4wave of the current directory. For a list
39 of possible storms and cycles, see:
40 
41  hsi ls %s/20
42 
43 where 20 refers to ensemble member 20. Generally all ensemble members
44 have the same storms and cycles run for them.
45 
46 Options:
47 
48  -h /directory
49  Overrides the directory to go for ensemble data
50  Default: %s
51  -v
52  Verbose
53 
54 """%(basedir,basedir))
55  if why is None:
56  logging.critical('SCRIPT IS EXITING DUE TO INCORRECT ARGUMENTS.')
57  else:
58  logging.critical(why)
59  sys.exit(2)
60 
61 def main():
62  ########################################################################
63  # Parse arguments
64 
65  try:
66  opts,args = getopt.gnu_getopt(sys.argv,'h:v')
67  except getopt.GetoptError as err:
68  usage(str(err))
69  for opt,arg in opts:
70  if opt=='-v':
71  verbosity=logging.DEBUG
72  elif opt=='-h':
73  basedir=str(arg)
74 
75  if(len(sys.argv)!=3):
76  usage('SYNTAX ERROR: Specify exactly two non-option arguments: storm and cycle')
77  storm=sys.argv[1] #'humberto09l'
78  cycle=sys.argv[2] #'2013091306'
79 
80  initlogging()
81 
82  ########################################################################
83  # Create output directory, check for user stupidity:
84 
85  outdir='%s.%s.out4wave'%(storm,cycle)
86  if os.path.exists(outdir):
87  if not os.path.isdir(outdir):
88  logging.error('%s: I need to make this directory but there is already a non-directory file there'%outdir)
89  sys.exit(2)
90  else:
91  try:
92  logging.info('Making output directory %s'%outdir)
93  os.mkdir(outdir)
94  except(IOError,OSError) as e:
95  logging.info('%s: cannot make directory: %s'%(outdir,repr(e)))
96  sys.exit(2)
97 
98  ########################################################################
99  # Grab some data:
100 
101  with produtil.cd.TempDir('.tmpdir','archive.',os.getcwd()+'/.') as t:
102  for ens in range(1,21):
103  archivename="hpss:%s/%02d/%s.%s.tar" % (basedir,ens,storm,cycle)
104  logging.info('Archive name for ensemble id %02d: %s'%(ens,archivename))
105  try:
106  a=hwrf.archive.hwrfarchive(archivename)
107  if(ens<2):
108  a.sane()
109  logging.debug('archive: '+repr(a.name()))
110  if not a.exists():
111  logging.error('%s: member %d archive does not appear to exist'%(archivename,ens))
112  wavefiles=guess_wave_filenames(archivename)
113  logging.debug('wave guess: ['+','.join(wavefiles)+']')
114  a.get_files(wavefiles)
115  for wfile in wavefiles:
116  if os.path.exists(wfile):
117  newname='../%s/ens-%02d-%s'%(outdir,ens,os.path.basename(wfile))
118  logging.info('%s: move to %s'%(wfile,newname))
119  os.rename(wfile,newname)
120  else:
121  logging.warning('%s: missing'%wfile)
122  except(IOError,OSError) as e:
123  logging.error('ens %02d: uncaught exception: %s'%(ens,repr(e)))
124 
125 
126  logging.info('Success.')
127 
128 if __name__=='__main__': main()
Change directory, handle temporary directories.
Definition: cd.py:1
This class is intended to be used with the Python "with TempDir() as t" syntax.
Definition: cd.py:38