HWRF  trunk@4391
track.py
1 #!/usr/bin/env python
2 
3 ##@namespace pom.track
4 #This module has a simple "get_vitals" function to extract vitals
5 #from NHC vital file (syndat_tcvitals.year).
6 #
7 #@note Please report bugs/questions/comments to bijuthomas(at)mail(dot)uri(dot)edu.
8 #@author Biju Thomas, GSO, University of Rhode Island.
9 #@date June 11, 2014
10 
11 ##@var __all__
12 # List of symbols to export by "from pom.track import *"
13 __all__ = [ 'get_vitals' ]
14 
15 import os.path
16 import re
17 from util import counter
18 from produtil.fileop import isnonempty
19 import logging
20 
21 def get_vitals(vitalfile, centerid, stormid, yyyy, trackfile):
22  """!Reads tcvitals
23  @param vitalfile File to parse
24  @param centerid Forecast center: NHC or JTWC
25  @param stormid three character storm ID (ie.: 12L, 19W)
26  @param yyyy Year.
27  @param trackfile Output file."""
28  try:
29  vitals = []
30  dates = []
31  with open(vitalfile, "rt") as f:
32  for line in f:
33  fields = re.split('[\s]\s*', line)
34  if fields[0] == centerid.upper() and \
35  fields[1] == stormid.upper():
36  if len(dates) == 0:
37  dates.append(int(''.join([fields[3],fields[4]])))
38  vitals.append(line)
39  else:
40  if int(''.join([fields[3],fields[4]])) in dates:
41  n=dates.index(int( ''.join([fields[3],fields[4]])))
42  if len(line) >= len(vitals[n]):
43  dates[n] = int(''.join([fields[3],fields[4]]))
44  vitals[n] = line
45  else:
46  dates.append(int(''.join([fields[3],fields[4]])))
47  vitals.append(line)
48  vitals = [y for (x,y) in sorted(zip(dates,vitals))]
49  #vitals.sort(key=dict(zip(vitals,dates)).get)
50  c = counter()
51  with open(trackfile, "wt") as f:
52  for line in vitals:
53  c.up()
54  r1s = qck(line[69:73], line[73:78])
55  r2s = qck(line[69:73], line[78:83])
56  r3s = qck(line[69:73], line[83:88])
57  r4s = qck(line[69:73], line[88:93])
58  if len(line) <= 96:
59  f.write("%s%s%s%s%s%s%s%s \n"%(line[:19],line[21:36],line[37:42], \
60  line[43:73],r1s,r2s,r3s,r4s))
61  else:
62  r5s = qck(line[69:73], line[95:100])
63  r6s = qck(line[69:73], line[100:105])
64  r7s = qck(line[69:73], line[105:110])
65  r8s = qck(line[69:73], line[110:115])
66  f.write("%s%s%s%s%s%s%s%s%s%s%s%s \n"%(line[:19],line[21:36],line[37:42], \
67  line[43:73],r1s,r2s,r3s,r4s,r5s,r6s,r7s,r8s))
68  f.write("%s\n"%("000 000 00000 000000 0000 000 0000 000 000 0000 0000"))
69  return c.value
70  except IOError as err:
71  print("%s" %err)
72 def qck(astr,bstr):
73  """!Quality check function: if a>b or b is 999, returns -999, otherwise returns b
74  @param astr,bstr string values containing integers a and b"""
75  if int(astr) > int(bstr) or int(bstr) == 999:
76  nstr = " -999"
77  else:
78  nstr = bstr
79  return nstr
80 
81 def track_shorten(fin, fout, ymdh, logger=None):
82  """!Removes unwanted parts of the track file from get_vitals()
83  @param fin,fout input and output files
84  @param ymdh date and hour of interest as a ten digit number in a string
85  @param logger a logging.Logger for log messages
86  @returns 0 on error, or the number of lines kept in fin otherwise"""
87  if logger is None: logger=logging.getLogger('pom')
88  if not os.path.exists(fin) or not isnonempty(fin):
89  logger.error("%s does not exists " %(fin))
90  print("%s does not exists " %(fin))
91  return 0
92  with open(fin) as fid:
93  with open(fout,'wt') as fo:
94  c = counter()
95  warn = True
96  for l in fid:
97  fo.write(l)
98  c.up()
99  if l[19:25] == ymdh[2:8] and l[26:28] == ymdh[8:10]:
100  warn = False
101  break
102  fo.write("%s\n"%("000 000 00000 000000 0000 000 0000 000 000 0000 0000"))
103  if (warn):
104  logger.error("%s does not Found in %s"%(ymdh,fin))
105  print("%s does not Found in %s"%(ymdh,fin))
106  return 0
107  else:
108  return c.value
109 
This module provides a set of utility functions to do filesystem operations.
Definition: fileop.py:1
def track_shorten
Removes unwanted parts of the track file from get_vitals()
Definition: track.py:81
def get_vitals(vitalfile, centerid, stormid, yyyy, trackfile)
Reads tcvitals.
Definition: track.py:21
def qck(astr, bstr)
Quality check function: if a>b or b is 999, returns -999, otherwise returns b.
Definition: track.py:72