HWRF  trunk@4391
exhwrf_check_init.py
1 #! /usr/bin/env python
2 
3 ##@namespace scripts.exhwrf_check_init
4 #Checks the initialization jobs' output and database entries to see
5 #if the initialization appears to have succeeded. This is not a
6 #comprehensive check, but it tries to be as thorough as possible
7 #without taking more than a half minute to run.
8 
9 import os, sys
11 import hwrf.mpipomtc
12 import hwrf_expt
13 from produtil.log import jlogger
14 
15 def check_ocean_init(logger):
16  """!Checks to see if the ocean initialization succeeded for the chosen ocean model.
17  @param logger a logging.Logger for log messages
18  @returns True if the ocean init ran, False otherwise"""
19  conf=hwrf_expt.conf
20  run_ocean=conf.getbool('config','run_ocean',True)
21  ocean_model=conf.getstr('config','ocean_model','')
22 
23  if not run_ocean:
24  logger.info('Ocean is disabled via run_ocean option. Skipping ocean checks.')
25  return True
26 
27  if not hwrf_expt.ocstatus.get(logger):
28  logger.warning('The ocean status file says ocean is disabled.')
29  return False
30 
31  if ocean_model=='HYCOM':
32  if not hwrf_expt.hycominit.is_completed():
33  logger.warning('The hycominit completion flag is off. '
34  'Ocean init failed.')
35  return False
36  elif ocean_model=='POM':
37  if not hwrf_expt.pominit.is_completed():
38  logger.warning('The pominit completion flag is off. '
39  'Ocean init failed.')
40  return False
41  else:
42  logger.error('Invalid value %s for ocean_model.'%(repr(ocean_model),))
43  return False
44 
45  return True
46 
47 def check_wave_init(logger):
48  conf=hwrf_expt.conf
49  run_wave=conf.getbool('config','run_wave',True)
50  wave_model=conf.getstr('config','wave_model','')
51 
52  if not run_wave:
53  logger.info('Wave is disabled via run_wave option. Skipping wave checks.')
54  return True
55 
56  if not hwrf_expt.wvstatus.get(logger):
57  logger.warning('The wave status file says wave is disabled.')
58  return False
59 
60  if wave_model=='WW3':
61  if not hwrf_expt.ww3init.is_completed():
62  logger.warning('The ww3init completion flag is off. '
63  'Wave init failed.')
64  return False
65  else:
66  logger.error('Invalid value %s for wave_model.'%(repr(wave_model),))
67  return False
68 
69  return True
70 
71 def check_gsi(logger):
72  """!Checks to see if the GSI data assimilation system ran
73  correctly for all domains for which it should have run.
74  @param logger a logging.Logger for log messages
75  @returns True if the GSI ran for all expected domains, False otherwise"""
76  okay=True
77  conf=hwrf_expt.conf
78  if hwrf.gsi.get_gsistatus(conf,'gsi_d02',logger) and not hwrf_expt.gsi_d02.is_completed():
79  logger.warning(
80  'GSI gsi_d02 Task state is not COMPLETED. Domain 2 GSI failed.')
81  okay=False
82 
83  if hwrf.gsi.get_gsistatus(conf,'gsi_d03',logger) and not hwrf_expt.gsi_d03.is_completed():
84  logger.warning(
85  'GSI gsi_d03 Task state is not COMPLETED. Domain 3 GSI failed.')
86  okay=False
87  return okay
88 
89 def check_relocate(conf,who,init,relocate,logger,when):
90  """!Checks to see if the vortex relocation system ran correctly.
91  @param conf an hwrf.config.HWRFConfig for configuration info
92  @param who a string name for this relocation job, for messages
93  @param init an hwrf.init.HWRFInit or subclass, the grids
94  input to the relocation
95  @param relocate an hwrf.relocate.RelocationTask to check
96  @param logger a logging.Logger for messages
97  @param when the expected forecast time of this relocation"
98  @returns True if the relocation job succeeded, or False otherwise"""
99  i=init
100  r=relocate
101  okay=True
102  if not r.completed:
103  logger.warning('%s: FAIL: relocation job state is not COMPLETE (state %s)'
104  %(who,r.strstate))
105  okay=False
106  for domain in i.wrfghost:
107  # Get the product for this domain and time:
108  if domain.is_moad():
109  p=r.wrfinput_at_time(when,domain)
110  else:
111  p=r.get_ghost(domain)
112 
113  # Is the product available, and does it have a valid location?
114  if not p.available:
115  logger.warning('%s: FAIL: relocation output product %s '
116  'not available'%(who,p.did))
117  okay=False
118  if p.location is None or p.location=='':
119  logger.warning('%s: FAIL: relocation output product %s has '
120  'no location'%(who,p.did))
121  okay=False
122  elif not produtil.fileop.isnonempty(p.location):
123  logger.warning(
124  '%s: FAIL: relocation output %s is empty or does not exist at '
125  '%s'%(who,p.did,p.location))
126  okay=False
127 
128  # Get the cold/warm start info and such:
129  try:
130  rinfo=r.rinfo
131  logger.info('%s: relocation info: %s'%(who,rinfo))
132  logger.info('%s: from file: %s'%(who,rinfo.from_file))
133  if rinfo.warm_cold_flag is None:
134  logger.warning(
135  '%s: FAIL: warm/cold state is unknown (None).'%(who,))
136  okay=False
137  elif rinfo.warm_cold_flag is not hwrf.relocate.WARM:
138  if rinfo.cold_ok:
139  logger.info('%s: cold start, but cold_ok=True'%(who,))
140  elif conf.getbool('config','expect_cold_start',False):
141  logger.warning('%s: cold start: no prior cycle found'%(who,))
142  else:
143  logger.error('%s: FAIL: unexpected cold start'%(who,))
144  okay=False
145  else:
146  logger.info('%s: warm start'%(who,))
147  except Exception as e:
148  logger.error('%s: error determining cold vs. warm start: %s'
149  %(who,str(e)),exc_info=True)
150  okay=False
151  return okay
152 
153 def check_fgat_relocate(conf,logger):
154  """!Checks all steps of the FGAT relocation.
155  @param conf the hwrf.config.HWRFConfig for configuration info
156  @param logger a logging.Logger for log messages
157  @returns True if the relocation jobs ALL succeeded, or False otherwise"""
158  okay=True
159  reloc=hwrf_expt.fgat_init.get_relocates()
160  for t,r in reloc.iteritems():
161  i=hwrf_expt.fgat_init.init_at_time(t)
162  who="FGAT time "+t.strftime('%Y%m%d%H')
163  if not check_relocate(conf,who,i,r,logger,t):
164  okay=False
165  logger.error(who+': failed.')
166  return okay
167 
168 def check_gfs_relocate(conf,logger):
169  """!Calls check_relocate() to check the GFS relocation
170  @param conf an hwrf.config.HWRFConfig for configuration info
171  @param logger a logging.Logger for log messages
172  @returns True if the GFS relocation ran correctly and False otherwise"""
173  if not check_relocate(conf,'GFS relocate',hwrf_expt.gfs_init,
174  hwrf_expt.gfs_init.rstage3,logger,
175  hwrf_expt.conf.cycle):
176  logger.error('GFS relocate failed.')
177  return False
178  return True
179 
180 def main():
181  """!Checks the entire HWRF initialization to see if all needed
182  inputs to the forecast are present and all init tasks ran
183  correctly."""
186  conf=hwrf_expt.conf
187  logger=conf.log("check_init")
188 
189  ocean_flag=conf.getbool('config','run_ocean')
190  wave_flag=conf.getbool('config','run_wave')
191  gsi_flag=conf.getbool('config','run_gsi')
192  reloc_flag=conf.getbool('config','run_relocation')
193 
194  okay=True
195 
196  if ocean_flag:
197  if check_ocean_init(logger):
198  logger.info('Ocean init succeeded.')
199  elif conf.syndat.basin1 in hwrf_expt.non_ocean_basins:
200  logger.info('Ocean init aborted, but basin is not supported.')
201  hwrf_expt.runwrf.remove_ocean()
202  else:
203  logger.error('Ocean init failed.')
204  okay=False
205  hwrf_expt.runwrf.remove_ocean()
206  else:
207  logger.info('Ocean is disabled. Skipping ocean checks.')
208 
209  if wave_flag:
210  if check_wave_init(logger):
211  logger.info('Wave init succeeded.')
212  elif conf.syndat.basin1 in hwrf_expt.non_wave_basins:
213  logger.info('Wave init aborted, but basin is not supported.')
214  hwrf_expt.runwrf.remove_wave()
215  else:
216  logger.error('Wave init failed.')
217  okay=False
218  hwrf_expt.runwrf.remove_wave()
219  else:
220  logger.info('Wave is disabled. Skipping wave checks.')
221 
222  if gsi_flag:
223  if not check_gsi(logger):
224  logger.error('GSI failed.')
225  okay=False
226  if not check_fgat_relocate(conf,logger):
227  logger.error('FGAT relocate failed.')
228  okay=False
229  elif reloc_flag:
230  logger.info('GSI is disabled. Skipping GSI and FGAT '
231  'relocation checks.')
232  if not check_gfs_relocate(conf,logger):
233  logger.error('GFS relocate failed.')
234  else:
235  logger.info('Relocation and GSI are disabled. '
236  'Skipping relocation checks.')
237 
238  logger.info('Asking the forecast object to check if all input files '
239  'are available.')
240  print type(hwrf_expt.runwrf).__name__
241  have_input=hwrf_expt.runwrf.check_all_inputs()
242  if not have_input:
243  okay=False
244  logger.error('FAILURE: WRF or POM inputs are missing')
245  if not have_input:
246  logger.critical('FORECAST INPUTS ARE MISSING!!')
247  sys.exit(1)
248  elif not okay:
249  logger.critical('INIT JOBS DID NOT SUCCEED!!')
250  sys.exit(1)
251 
252 if __name__=='__main__':
253  try:
254  main()
255  except Exception as e:
256  jlogger.critical('HWRF check init is aborting: '+str(e),exc_info=True)
257  sys.exit(2)
258 
Runs the POM initialization and POM-WRF coupled forecast.
Definition: mpipomtc.py:1
Contains setup(), which initializes the produtil package.
Definition: setup.py:1
def check_gsi(logger)
Checks to see if the GSI data assimilation system ran correctly for all domains for which it should h...
def init_module
Initializes the HWRF object structure.
Definition: hwrf_expt.py:384
def check_fgat_relocate(conf, logger)
Checks all steps of the FGAT relocation.
def get_gsistatus
Checks the gsi status for a specific domain.
Definition: gsi.py:1273
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
def isnonempty(filename)
Returns True if the filename refers to an existent file that is non-empty, and False otherwise...
Definition: fileop.py:333
def check_gfs_relocate(conf, logger)
Calls check_relocate() to check the GFS relocation.
def check_relocate(conf, who, init, relocate, logger, when)
Checks to see if the vortex relocation system ran correctly.
def check_ocean_init(logger)
Checks to see if the ocean initialization succeeded for the chosen ocean model.
Configures logging.
Definition: log.py:1
def main()
Checks the entire HWRF initialization to see if all needed inputs to the forecast are present and all...