HWRF  trunk@4391
hwrf_multistorm_sort_test.py
1 #! /usr/bin/env python
2 
3 import logging, os, sys, re
4 import produtil.setup
6 
7 ##@namespace ush.hwrf_multistorm_sort_test
8 # A test script for the multistorm sort function. This script orders storms by
9 # some internal priority information defined by hrd_multistorm_sorter().
10 # Call like so:
11 # @code{.sh}
12 # hwrf_multistorm_sort_test.py YYYYMMDDHH list of storms and basins
13 # @endcode
14 #
15 # Where:
16 # * YYYYMMDDHH --- the ten digit date and hour for the cycle of interest
17 # * stormid=priority --- 19L=3, 18E=1, 03A=5, etc. A storm to run for
18 # that cycle and a user-specified priority for the storm.
19 # * a basin --- L, E, C, etc. All storms will be run from that basin.
20 
21 def hrd_multistorm_sorter(a,b):
22  """!A drop-in replacement for "cmp" that can be used for sorting or
23  comparison. Returns -1 if a<b, 1 if a>b or 0 if a=b. Decision is
24  made in this order:
25 
26  User priority (a.userprio): lower (priority 1) is "more
27  important" than higher numbers (priority 9999 is fill value).
28 
29  Invest vs. non-invest: invest is less important
30 
31  wind: stronger wind is more important than weaker wind
32 
33  North Atlantic (L) storms: farther west is more important
34 
35  North East Pacific (E) storms: farther East is more important
36 
37  If all of the above values are equal, 0 is returned."""
38  a_userprio=getattr(a,'userprio',9999)
39  b_userprio=getattr(b,'userprio',9999)
40  a_invest=1 if (a.stormname=='INVEST') else 0
41  b_invest=1 if (b.stormname=='INVEST') else 0
42 
43  c = cmp(a_userprio,b_userprio) or cmp(a_invest,b_invest) or\
44  -cmp(a.wmax,b.wmax) or\
45  (a.basin1=='L' and b.basin1=='L' and cmp(a.lon,b.lon)) or \
46  (a.basin1=='E' and b.basin1=='E' and -cmp(a.lon,b.lon))
47  return c
48 
49 ##@var vitfiles
50 # List of known tcvitals file locations. Each is intended to be sent
51 # through a datetime.datetime.format to get the final filename.
52 vitfiles=[
53  '/com/arch/prod/syndat/syndat_tcvitals.%Y',
54  '/lfs3/projects/hwrf-data/hwrf-input/SYNDAT-PLUS/syndat_tcvitals.%Y',
55  '/scratch3/NCEPDEV/hwrf/noscrub/input/SYNDAT-PLUS/syndat_tcvitals.%Y',
56  '/scratch1/portfolios/NCEPDEV/hwrf/noscrub/input/SYNDAT-PLUS/syndat_tcvitals.%Y']
57 
58 def main(args):
59  """!Set up logging, reads vitals, outputs storm list."""
60 
61  # Set up logging, disable dbn alerts:
62  produtil.setup.setup(send_dbn=False,ologlevel=99,jloglevel=99,
63  eloglevel=logging.INFO)
64  logger=logging.getLogger('hwrf_multistorm_sort_test.py')
65  strcycle=args[1]
66  cyc=hwrf.numerics.to_datetime(strcycle)
67  YMDH=cyc.strftime('%Y%m%d%H')
68  basins=''
69  userprios=dict()
70 
71  for arg in args[2:]:
72  if len(arg)==1:
73  if 'LECWPQSAB'.find(arg.upper())<0:
74  logger.error('Invalid basin %s'%(arg,))
75  exit(2)
76  basins=basins+arg.upper()
77  continue
78  m=re.match('(\d\d[LECWPQSAB])=(\d+)',arg.upper())
79  if not m:
80  logger.error('Unrecognized argument: '+arg)
81  exit(2)
82  (storm,userprio)=m.groups()
83  userprios[storm]=int(userprio)
84 
85  rv=hwrf.revital.Revital(logger=logger)
86  rv.readfiles([ cyc.strftime(v) for v in vitfiles ],
87  raise_all=False)
88  #rv.renumber(threshold=14)
89  #rv.renumber(unrenumber=True)
90  rv.delete_invest_duplicates()
91  rv.clean_up_vitals()
92  rv.discard_except(lambda v: v.YMDH==YMDH)
93  if basins:
94  logger.info('Only keep basins: <%s>'%(basins,))
95  rv.discard_except(lambda v: v.basin1 in basins)
96  rv.clean_up_vitals()
97  for v in rv:
98  if v.stormid3 in userprios:
99  userprio=int(userprios[v.stormid3])
100  logger.info('User priority for %s is %d'%(v.stormid3,userprio))
101  setattr(v,'userprio',userprio)
102 
103  rv.sort_by_function(hrd_multistorm_sorter)
104  for v in rv:
105  print v.as_tcvitals()
106 
107 if __name__=='__main__': main(sys.argv)
Contains setup(), which initializes the produtil package.
Definition: setup.py:1
Defines the Revital class which manipulates tcvitals files.
Definition: revital.py:1
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 to_datetime(d)
Converts the argument to a datetime.
Definition: numerics.py:346
Time manipulation and other numerical routines.
Definition: numerics.py:1
This class reads one or more tcvitals files and rewrites them as requested.
Definition: revital.py:38