HWRF  trunk@4391
hwrf_multistorm_sort.py
1 #! /usr/bin/env python
2 
3 import logging, os, sys, re
4 import produtil.setup
6 
7 def hrd_multistorm_sorter(a,b):
8  """A drop-in replacement for "cmp" that can be used for sorting or
9  comparison. Returns -1 if a<b, 1 if a>b or 0 if a=b. Decision is
10  made in this order:
11 
12  User priority (a.userprio): lower (priority 1) is "more
13  important" than higher numbers (priority 9999 is fill value).
14 
15  Invest vs. non-invest: invest is less important
16 
17  wind: stronger wind is more important than weaker wind
18 
19  North Atlantic (L) storms: farther west is more important
20 
21  North East Pacific (E) storms: farther East is more important
22 
23  If all of the above values are equal, 0 is returned."""
24  a_userprio=getattr(a,'userprio',9999)
25  b_userprio=getattr(b,'userprio',9999)
26  a_invest=1 if (a.stormname=='INVEST') else 0
27  b_invest=1 if (b.stormname=='INVEST') else 0
28 
29  c = cmp(a_userprio,b_userprio) or cmp(a_invest,b_invest) or\
30  -cmp(a.wmax,b.wmax) or\
31  (a.basin1=='L' and b.basin1=='L' and cmp(a.lon,b.lon)) or \
32  (a.basin1=='E' and b.basin1=='E' and -cmp(a.lon,b.lon))
33  return c
34 
35 vitfiles=[
36  '/com/arch/prod/syndat/syndat_tcvitals.%Y',
37  '/lfs3/projects/hwrf-data/hwrf-input/SYNDAT-PLUS/syndat_tcvitals.%Y',
38  '/scratch1/portfolios/NCEPDEV/hwrf/noscrub/input/SYNDAT-PLUS/syndat_tcvitals.%Y']
39 
40 def main(args):
41  # Set up logging, etc. Disable DBN alerts and stdout logging:
42  produtil.setup.setup(send_dbn=False,ologlevel=99,jloglevel=99,
43  eloglevel=logging.INFO)
44  logger=logging.getLogger(__file__)
45  strcycle=args[1]
46  cyc=hwrf.numerics.to_datetime(strcycle)
47  YMDH=cyc.strftime('%Y%m%d%H')
48  basins=''
49  userprios=dict()
50 
51  for arg in args[2:]:
52  if len(arg)==1:
53  if 'LECWPQSAB'.find(arg.upper())<0:
54  logger.error('Invalid basin %s'%(arg,))
55  exit(2)
56  basins=basins+arg.upper()
57  continue
58  m=re.match('(\d\d[LECWPQSAB])=(\d+)',arg.upper())
59  if not m:
60  logger.error('Unrecognized argument: '+arg)
61  exit(2)
62  (storm,userprio)=m.groups()
63  userprios[storm]=int(userprio)
64 
65  rv=hwrf.revital.Revital(logger=logger)
66  rv.readfiles([ cyc.strftime(v) for v in vitfiles ],
67  raise_all=False)
68  #rv.renumber(threshold=14)
69  #rv.renumber(unrenumber=True)
70  rv.delete_invest_duplicates()
71  rv.clean_up_vitals()
72  rv.discard_except(lambda v: v.YMDH==YMDH)
73  if basins:
74  logger.info('Only keep basins: <%s>'%(basins,))
75  rv.discard_except(lambda v: v.basin1 in basins)
76  rv.clean_up_vitals()
77  for v in rv:
78  if v.stormid3 in userprios:
79  userprio=int(userprios[v.stormid3])
80  logger.info('User priority for %s is %d'%(v.stormid3,userprio))
81  setattr(v,'userprio',userprio)
82 
83  rv.sort_by_function(hrd_multistorm_sorter)
84  for v in rv:
85  print v.as_tcvitals()
86 
87 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