3 import os, sys, logging, getopt, re, StringIO
4 import produtil.sigsafety, produtil.fileop
5 import produtil.run, produtil.setup
6 import hwrf.numerics, hwrf.launcher, hwrf.input
8 from hwrf.numerics
import to_datetime_rel
9 from produtil.run
import exe
10 from hwrf.input
import InputSource, DataCatalog
13 global conf_input_list
41 'hwrf_input.conf',
'hwrf.conf',
'hwrf_holdvars.conf',
42 'hwrf_basic.conf',
'system.conf' ]
64 global logger, outputdir, WORKhwrf, HOMEhwrf, cycles, inputscript
67 (optlist,args) = getopt.getopt(sys.argv[1:],
"o:w:vi:")
68 for opt,val
in optlist:
69 logger.info(
"OPTLIST: ",optlist)
71 logger.setLevel(logging.DEBUG)
72 logger.debug(
'Verbosity enabled.')
74 logger.info(
'Work area set to %s'%(val,))
77 logger.info(
'Output area set to %s'%(val,))
80 logger.info(
'Input script set to %s'%(val,))
83 usage(
'Invalid option %s'%(opt,))
84 except (getopt.GetoptError,ValueError,TypeError)
as e:
85 logger.info(
"FAILED!")
88 HOMEhwrf=os.environ.get(
'HOMEhwrf',
'')
90 HOMEhwrf=os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
93 outputdir=os.path.join(cwd,
'hwrfdata')
95 WORKhwrf=os.path.join(cwd,
'input-temp')
96 if inputscript
is None:
98 usage(
'Input script and one cycle must be specified.')
102 usage(
'At least one cycle must be specified.')
106 if not os.path.exists(WORKhwrf): os.makedirs(WORKhwrf)
108 logger.info(
'inputscript is %s'%(inputscript,))
109 logger.info(
'cycles: '+str(cycles))
112 conf=hwrf.launcher.HWRFLauncher()
113 conf.set(
'config',
'cycle',cycle)
115 assert(conf.cycle
is not None)
116 for basename
in conf_input_list:
117 print "CONF_INPUT_LIST ", conf_input_list,basename
118 print "HOMEhwrf", HOMEhwrf
119 fullname=os.path.join(HOMEhwrf,
'parm',basename)
120 if not os.path.exists(fullname):
121 logger.error(
'%s: does not exist. HWRF is not installed in %s'%(
125 conf.set(
'holdvars',
'CASE_ROOT',
'HISTORY')
126 conf.set(
'hwrfdata',
'inputroot',outputdir)
127 conf.set(
'config',
'case_root',
'HISTORY')
128 conf.set(
'config',
'fcsthist',
'hist')
129 conf.set(
'config',
'realtime',
'false')
130 conf.guess_default_values()
131 assert(conf.cycle
is not None)
132 conf.set(
'dir',
'HOMEhwrf',HOMEhwrf)
133 conf.set(
'dir',
'WORKhwrf',WORKhwrf)
134 conf.set(
'dir',
'com',WORKhwrf)
140 inputs.append(stack[-1])
143 for item
in lists[0]:
144 addme=dict(stack[-1])
150 assert(isinstance(lists,dict))
154 pull.update(dataset=dataset,atime=atime,item=args[0])
157 pull[
'optional']=
True
158 elif len(arg)>1
and arg[0]==
'+':
159 if arg[1:]
not in lists:
160 raise Exception(
'%s: list not in %s'%(arg[1:],lists))
161 listflags.add(arg[1:])
162 elif '0123456789'.find(arg[0])>=0:
163 parts=arg.split(
'..')
167 ftimes.append(to_datetime_rel(sec ,atime))
169 start_fhr=int(parts[0])
170 end_fhr=int(parts[1])
171 for hr
in xrange(start_fhr,end_fhr+1):
173 ftimes.append(to_datetime_rel(sec,atime))
175 start_fhr=int(parts[0])
176 end_fhr=int(parts[2])
177 step_fhr=int(parts[1])
178 for hr
in xrange(start_fhr,end_fhr+1,step_fhr):
180 ftimes.append(to_datetime_rel(sec,atime))
182 raise Exception(
'Unrecognized argument %s'%(arg,))
185 listnames = [ listname
for listname
in listflags ]
186 listlists = [ lists[listname]
for listname
in listnames ]
189 addme.update(ftime=ftime)
200 for line
in inputstream:
201 stripped=line.strip()
202 if stripped[0:1]==
'#':
continue
203 parts=stripped.split()
204 if len(parts)<1:
continue
206 if cmd==
'DATASET' and len(parts)>1:
209 elif cmd==
'ATIME' and len(parts)==2:
211 atime=to_datetime_rel(rel*3600,cycle)
212 elif cmd==
'ATIME' and len(parts)==1:
214 elif ( cmd==
'FILL' or cmd==
'FILLINT')
and len(parts)==4:
215 nums=parts[3].split(
'..')
219 lists[listname]=(format%int(nums[0]))
222 [str(format)%int(num) \
223 for num
in xrange(int(nums[0]),int(nums[1])+1)]
226 [str(format)%int(num) \
227 for num
in xrange(int(nums[0]),int(nums[2])+1,int(nums[1]))]
230 lists[listname]=[ int(O)
for O
in old ]
231 elif cmd==
'SET' and len(parts)>2:
233 lists[parts[1]]=parts[2:]
234 elif cmd==
'SETINT' and len(parts)>2:
236 lists[parts[1]]=[ int(part)
for part
in parts[2:] ]
237 elif cmd==
'PULL' and len(parts)>1:
245 logger.info(
'List of inputs to pull:')
247 s=
' '.join([
'%s=%s'%(k,v)
for k,v
in i.iteritems() ])
251 hwrfdata=DataCatalog(conf,
'hwrfdata',cycle)
252 htar=exe(conf.getexe(
'htar'))
253 hsi=exe(conf.getexe(
'hsi'))
254 insec=conf.getstr(
'config',
'input_sources')
255 ins=InputSource(conf,insec,cycle,htar,logger,hsi)
256 return bool(ins.get(inputs,hwrfdata,
False,logger,
True))
260 produtil.setup.setup(send_dbn=
False,thread_logger=
True)
261 logger=logging.getLogger(
'pull_inputs')
266 with open(inputscript,
'rt')
as inf:
273 if __name__==
'__main__':