Package aisutils :: Module uscg
[hide private]
[frames] | no frames]

Source Code for Module aisutils.uscg

  1  #!/usr/bin/env python 
  2  __version__ = '$Revision: 2275 $'.split()[1] 
  3  __date__ = '$Date: 2006-07-10 16:22:35 -0400 (Mon, 10 Jul 2006) $'.split()[1] 
  4  __author__ = 'Kurt Schwehr' 
  5   
  6  __doc__=''' 
  7  Connect to a socket and forward what is received to another port. 
  8  Filter to a list of AIS receivers/basestations. 
  9   
 10  @author: '''+__author__+''' 
 11  @version: ''' + __version__ +''' 
 12  @copyright: 2006 
 13  @var __date__: Date of last svn commit 
 14  @undocumented: __version__ __author__ __doc__ myparser 
 15  @status: under development 
 16  @license: GPL v2 
 17  @since: Jan 2008 
 18  ''' 
 19   
 20  import datetime 
 21  import unittest 
 22  import sys 
 23   
24 -class UscgNmea:
25 - def __init__(self,nmeaStr=None):
26 ''' 27 Fields: 28 - rssi ('s'): relative signal strength indicator 29 - signalStrength ('d') - signal strendth in dBm 30 - timeOfArrival ('T') - time of arrive from receiver - seconds within the minute 31 - slotNumber ('S') - Receive slot number 32 - station ('r' or 'b') - station name or id that received the message 33 - stationTypeCode - first letter of the station name indicating 'b'asestation or 'r'eceive only (I think) 34 - cg_sec - receive time of the message from the logging software. Unix UTC second timestamp 35 - timestamp - python datetime object in UTC derived from the cg_sec 36 @todo: parse the other fields? 37 ''' 38 if None!=nmeaStr: 39 fields = nmeaStr.split(',') 40 self.cg_sec=float(fields[-1]) 41 self.timestamp = datetime.datetime.utcfromtimestamp(self.cg_sec) 42 43 for i in range(len(fields)-1,5,-1): 44 if len(fields[i])==0: 45 continue # maybe it should throw a parse exception instead? 46 f = fields[i] 47 c = f[0] # first charater determines what the field is 48 if c in ('b','r'): 49 self.station = f # FIX: think we want to keep the code in the first char 50 self.stationTypeCode = self.station[0] 51 continue 52 #break # Found it so ditch the for loop 53 if c == 's': 54 self.rssi=int(f[1:]) 55 continue 56 if c == 'd': 57 self.signalStrength = int(f[1:]) 58 continue 59 if c == 'T': 60 self.timeOfArrival = float(f[1:]) 61 continue 62 if c == 'S': 63 self.slotNumber = int(f[1:]) 64 continue
65 66
67 -class TestUscgNmea(unittest.TestCase):
68 - def testUscgNmea(self):
69 un = UscgNmea('!AIVDM,1,1,,B,15Cjtd0Oj;Jp7ilG7=UkKBoB0<06,0*63,s1234,d-119,T12.34567123,r003669958,S4321,1085889680') 70 self.failUnlessEqual(un.rssi,1234) 71 self.failUnlessEqual(un.signalStrength,-119) 72 self.failUnlessEqual(un.timeOfArrival,12.34567123) 73 self.failUnlessEqual(un.slotNumber,4321) 74 self.failUnlessEqual(un.station,'r003669958') 75 self.failUnlessEqual(un.stationTypeCode,'r') 76 self.failUnlessEqual(un.cg_sec,float(1085889680))
77 78 ############################################################ 79 if __name__=='__main__': 80 81 from optparse import OptionParser 82 parser = OptionParser(usage="%prog [options]", 83 version="%prog "+__version__) 84 85 parser.add_option('--doc-test',dest='doctest',default=False,action='store_true' 86 ,help='run the documentation tests') 87 parser.add_option('--unit-test',dest='unittest',default=False,action='store_true' 88 ,help='run the unit tests') 89 parser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true' 90 ,help='Make the test output verbose') 91 92 (options,args) = parser.parse_args() 93 94 if options.doctest: 95 success=True 96 import os; print os.path.basename(sys.argv[0]), 'doctests ...', 97 sys.argv= [sys.argv[0]] 98 if options.verbose: sys.argv.append('-v') 99 import doctest 100 numfail,numtests=doctest.testmod() 101 if numfail==0: print 'ok' 102 else: 103 print 'FAILED' 104 success=False 105 106 if not success: sys.exit('Something Failed') 107 #del success # Hide success from epydoc 108 109 if options.unittest: 110 sys.argv = [sys.argv[0]] 111 if options.verbose: sys.argv.append('-v') 112 unittest.main() 113