3 import os, sys, logging, getopt, re, StringIO
10 from hwrf.input import InputSource, DataCatalog
39 'hwrf_input.conf',
'hwrf.conf',
'hwrf_holdvars.conf',
40 'hwrf_basic.conf',
'system.conf' ]
61 def parse_arguments():
62 global logger, outputdir, WORKhwrf, HOMEhwrf, cycles, inputscript
64 (optlist,args) = getopt.getopt(sys.argv[1:],
'o:w:vi:')
65 for opt,val
in optlist:
67 logger.setLevel(logging.DEBUG)
68 logger.debug(
'Verbosity enabled.')
70 logger.info(
'Work area set to %s'%(val,))
73 logger.info(
'Output area set to %s'%(val,))
76 logger.info(
'Input script set to %s'%(val,))
79 usage(
'Invalid option %s'%(opt,))
80 except (getopt.GetoptError,ValueError,TypeError)
as e:
83 HOMEhwrf=os.environ.get(
'HOMEhwrf',
'')
85 HOMEhwrf=os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
88 outputdir=os.path.join(cwd,
'hwrfdata')
90 WORKhwrf=os.path.join(cwd,
'input-temp')
91 if inputscript
is None:
93 usage(
'Input script and one cycle must be specified.')
97 usage(
'At least one cycle must be specified.')
100 logger.info(
'inputscript is %s'%(inputscript,))
101 logger.info(
'cycles: '+str(cycles))
103 def make_conf(cycle):
105 conf.set(
'config',
'cycle',cycle)
107 assert(conf.cycle
is not None)
108 for basename
in conf_input_list:
109 fullname=os.path.join(HOMEhwrf,
'parm',basename)
110 if not os.path.exists(fullname):
111 logger.error(
'%s: does not exist. HWRF is not installed in %s'%(
115 conf.set(
'holdvars',
'CASE_ROOT',
'HISTORY')
116 conf.set(
'hwrfdata',
'inputroot',outputdir)
117 conf.set(
'config',
'case_root',
'HISTORY')
118 conf.set(
'config',
'fcsthist',
'hist')
119 conf.set(
'config',
'realtime',
'false')
120 conf.guess_default_values()
121 assert(conf.cycle
is not None)
122 conf.set(
'dir',
'HOMEhwrf',HOMEhwrf)
123 conf.set(
'dir',
'WORKhwrf',WORKhwrf)
124 conf.set(
'dir',
'com',WORKhwrf)
127 def expand_lists(inputs,stack,names,lists):
130 inputs.append(stack[-1])
133 for item
in lists[0]:
134 addme=dict(stack[-1])
137 expand_lists(inputs,stack,names[1:],lists[1:])
139 def pull_command(inputs,dataset,atime,lists,args):
140 assert(isinstance(lists,dict))
144 pull.update(dataset=dataset,atime=atime,item=args[0])
147 pull[
'optional']=
True
148 elif len(arg)>1
and arg[0]==
'+':
149 if arg[1:]
not in lists:
150 raise Exception(
'%s: list not in %s'%(arg[1:],lists))
151 listflags.add(arg[1:])
152 elif '0123456789'.find(arg[0])>=0:
153 parts=arg.split(
'..')
157 ftimes.append(to_datetime_rel(sec ,atime))
159 start_fhr=int(parts[0])
160 end_fhr=int(parts[1])
161 for hr
in xrange(start_fhr,end_fhr+1):
163 ftimes.append(to_datetime_rel(sec,atime))
165 start_fhr=int(parts[0])
166 end_fhr=int(parts[2])
167 step_fhr=int(parts[1])
168 for hr
in xrange(start_fhr,end_fhr+1,step_fhr):
170 ftimes.append(to_datetime_rel(sec,atime))
172 raise Exception(
'Unrecognized argument %s'%(arg,))
175 listnames = [ listname
for listname
in listflags ]
176 listlists = [ lists[listname]
for listname
in listnames ]
179 addme.update(ftime=ftime)
182 expand_lists(inputs,stack,listnames,listlists)
184 def make_input_list(conf,inputstream):
190 for line
in inputstream:
191 stripped=line.strip()
192 if stripped[0:1]==
'#':
continue
193 parts=stripped.split()
194 if len(parts)<1:
continue
196 if cmd==
'DATASET' and len(parts)>1:
199 elif cmd==
'ATIME' and len(parts)==2:
201 atime=to_datetime_rel(rel*3600,cycle)
202 elif cmd==
'ATIME' and len(parts)==1:
204 elif ( cmd==
'FILL' or cmd==
'FILLINT')
and len(parts)==4:
205 nums=parts[3].split(
'..')
209 lists[listname]=(format%int(nums[0]))
212 [str(format)%int(num) \
213 for num
in xrange(int(nums[0]),int(nums[1])+1)]
216 [str(format)%int(num) \
217 for num
in xrange(int(nums[0]),int(nums[2])+1,int(nums[1]))]
220 lists[listname]=[ int(O)
for O
in old ]
221 elif cmd==
'SET' and len(parts)>2:
223 lists[parts[1]]=parts[2:]
224 elif cmd==
'SETINT' and len(parts)>2:
226 lists[parts[1]]=[ int(part)
for part
in parts[2:] ]
227 elif cmd==
'PULL' and len(parts)>1:
229 pull_command(inputs,dataset,atime,lists,parts[1:])
234 def dump_list(inputs):
235 logger.info(
'List of inputs to pull:')
237 s=
' '.join([
'%s=%s'%(k,v)
for k,v
in i.iteritems() ])
240 def transfer_inputs(inputs,conf,cycle):
241 hwrfdata=DataCatalog(conf,
'hwrfdata',cycle)
242 htar=exe(conf.getexe(
'htar'))
243 hsi=exe(conf.getexe(
'hsi'))
244 insec=conf.getstr(
'config',
'input_sources')
245 ins=InputSource(conf,insec,cycle,htar,logger,hsi)
246 return bool(ins.get(inputs,hwrfdata,
False,logger,
True))
251 logger=logging.getLogger(
'pull_inputs')
255 conf=make_conf(cycle)
256 with open(inputscript,
'rt')
as inf:
257 inputs=make_input_list(conf,inf)
259 okay=okay
and transfer_inputs(inputs,conf,cycle)
263 if __name__==
'__main__':
This module provides a set of utility functions to do filesystem operations.
Contains setup(), which initializes the produtil package.
Sets up signal handlers to ensure a clean exit.
A shell-like syntax for running serial, MPI and OpenMP programs.
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.
A replacement for the hwrf.config.HWRFConfig used throughout the HWRF system.
Time manipulation and other numerical routines.
Creates the initial HWRF directory structure, loads information into each job.