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})')
21 def guess_wave_filenames(path,maxdom=3):
22 arbase=os.path.basename(path)
24 m=namere.search(arbase)
27 base=
"%s%s%s.%s.out4wave" % m.group(1,2,3,4)
28 return [
"%s_d%02d" % (base,1+i)
for i
in xrange(maxdom) ]
31 logging.basicConfig(level=verbosity,format=
'%(asctime)s %(levelname)8s - %(message)s')
34 logging.critical(
"""FORMAT: hwrf_get_ens_wave.py [options] stormprefix cycle
35 Example: hwrf_get_ens_wave.py humberto09l 2013091306
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:
43 where 20 refers to ensemble member 20. Generally all ensemble members
44 have the same storms and cycles run for them.
49 Overrides the directory to go for ensemble data
54 """%(basedir,basedir))
56 logging.critical(
'SCRIPT IS EXITING DUE TO INCORRECT ARGUMENTS.')
66 opts,args = getopt.gnu_getopt(sys.argv,
'h:v')
67 except getopt.GetoptError
as err:
71 verbosity=logging.DEBUG
76 usage(
'SYNTAX ERROR: Specify exactly two non-option arguments: storm and cycle')
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)
92 logging.info(
'Making output directory %s'%outdir)
94 except(IOError,OSError)
as e:
95 logging.info(
'%s: cannot make directory: %s'%(outdir,repr(e)))
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))
106 a=hwrf.archive.hwrfarchive(archivename)
109 logging.debug(
'archive: '+repr(a.name()))
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)
121 logging.warning(
'%s: missing'%wfile)
122 except(IOError,OSError)
as e:
123 logging.error(
'ens %02d: uncaught exception: %s'%(ens,repr(e)))
126 logging.info(
'Success.')
128 if __name__==
'__main__': main()
Change directory, handle temporary directories.
This class is intended to be used with the Python "with TempDir() as t" syntax.