1
2
3 __version__ = '$Revision: 4791 $'.split()[1]
4 __date__ = '$Date: 2006-09-24 14:01:41 -0400 (Sun, 24 Sep 2006) $'.split()[1]
5 __author__ = 'Kurt Schwehr'
6
7 __doc__='''
8
9 Split an USCG N-AIS log file into one file per receiving station.
10
11 @requires: U{epydoc<http://epydoc.sourceforge.net/>} > 3.0alpha3
12
13 @author: U{'''+__author__+'''<http://schwehr.org/>}
14 @version: ''' + __version__ +'''
15 @copyright: 2006
16 @var __date__: Date of last svn commit
17 @undocumented: __version__ __author__ __doc__ myparser
18 @since: 2006-Sep-24
19 @status: under development
20 @organization: U{CCOM<http://ccom.unh.edu/>}
21 @license: GPL v2
22
23 @todo: add a link to generated doc string to bring up the html for the pretty version
24
25 @bug: NOT complete
26 @todo: make sure binary is only used in AIS ITU messages and not within the binary messages!
27 '''
28
29 import sys, os
30
31
32
34 '''
35 Return the station/tower portion of the message
36 @param withR: True keeps the leading r
37 @rtype: str or None
38 @return: Station name
39 '''
40 fields = msg.split(',')
41 numFields = len(fields)
42
43 for i in range(numFields-1,-1,-1):
44
45 curField = fields[i]
46 if curField[0]=='r':
47 if withR: return curField
48 return curField[1:]
49 if curField[1]=='*':
50 return None
51 return None
52
53
55 '''
56 One file per receive station/tower
57
58 The tower is specified in the USCG extensions after the checksum in the field starting with a 'r'.
59 In this message, the station is r3669961. This is not located in a fixed position.
60
61 !AIVDM,1,1,,B,15MgF90017JWnghFFuLeJrW608D;,0*7C,x161434,s26256,d-109,T34.43128733,r3669961,1152921695
62 '''
63
64 subdir = options.subdir
65 if options.useSubdir:
66 if not os.access(subdir,os.X_OK):
67 os.mkdir(subdir)
68 if subdir[-1]!='/': subdir += '/'
69 print 'Using subdir ...',subdir
70 else:
71 subdir = ''
72
73 unknown = None
74 stationFiles={}
75 for filename in filenames:
76 lineNum = 0
77 for line in file(filename):
78 lineNum += 1
79 if lineNum % 20000 == 0: print 'line',lineNum
80 if line[0]=='#': continue
81 station = getStation(line)
82 if None==station:
83 print 'Line had no station:',line
84 if None==unknown: unknown = file(subdir+'unknown','w')
85 unknown.write(line)
86 if station not in stationFiles:
87 stationFilename = subdir+station
88
89 stationFiles[station] = file(stationFilename,'w')
90
91
92 stationFiles[station].write(line)
93
94
95 if __name__=='__main__':
96 from optparse import OptionParser
97 myparser = OptionParser(usage="%prog [options]",
98 version="%prog "+__version__)
99
100 myparser.add_option('--doc-test',dest='doctest',default=False,action='store_true',
101 help='run the documentation tests')
102
103 myparser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true',
104 help='run the tests run in verbose mode')
105
106 myparser.add_option('-n','--nosubdir',dest='useSubdir',default=True,action='store_false',
107 help='Prevent use of a subdirectory. Will make a mess.')
108
109 myparser.add_option('-s','--subdir',dest='subdir',default='towers',
110 help='Where to make the tower files [default: %default]')
111
112 (options,args) = myparser.parse_args()
113
114 success=True
115
116 if options.doctest:
117 import os; print os.path.basename(sys.argv[0]), 'doctests ...',
118 argvOrig = sys.argv
119 sys.argv= [sys.argv[0]]
120 if options.verbose: sys.argv.append('-v')
121 import doctest
122 numfail,numtests=doctest.testmod()
123 if numfail==0: print 'ok'
124 else:
125 print 'FAILED'
126 success=False
127 sys.argv = argvOrig
128 del argvOrig
129 sys.exit()
130
131 towersplit(options, args)
132