Package ais :: Module sls_waterlevel
[hide private]
[frames] | no frames]

Source Code for Module ais.sls_waterlevel

  1  #!/usr/bin/env python 
  2   
  3  __version__ = '$Revision: 4791 $'.split()[1] 
  4  __date__ = '$Date: 2006-12-18 $'.split()[1] 
  5  __author__ = 'xmlbinmsg' 
  6   
  7  __doc__=''' 
  8   
  9  Autogenerated python functions to serialize/deserialize binary messages. 
 10   
 11  Generated by: ./aisxmlbinmsg2py.py 
 12   
 13  Need to then wrap these functions with the outer AIS packet and then 
 14  convert the whole binary blob to a NMEA string.  Those functions are 
 15  not currently provided in this file. 
 16   
 17  serialize: python to ais binary 
 18  deserialize: ais binary to python 
 19   
 20  The generated code uses translators.py, binary.py, and aisstring.py 
 21  which should be packaged with the resulting files. 
 22   
 23   
 24  @requires: U{epydoc<http://epydoc.sourceforge.net/>} > 3.0alpha3 
 25  @requires: U{BitVector<http://cheeseshop.python.org/pypi/BitVector>} 
 26   
 27  @author: '''+__author__+''' 
 28  @version: ''' + __version__ +''' 
 29  @var __date__: Date of last svn commit 
 30  @undocumented: __version__ __author__ __doc__ myparser 
 31  @status: under development 
 32  @license: Generated code has no license 
 33  ''' 
 34   
 35  import sys 
 36  from decimal import Decimal 
 37  from BitVector import BitVector 
 38   
 39  import binary, aisstring 
 40   
 41  TrueBV  = BitVector(bitstring="1") 
 42  "Why always rebuild the True bit?  This should speed things up a bunch" 
 43  FalseBV = BitVector(bitstring="0") 
 44  "Why always rebuild the False bit?  This should speed things up a bunch" 
 45   
 46   
47 -def sls_waterlevelEncode(params, validate=False):
48 '''Create a sls_waterlevel binary message payload to pack into an AIS Msg sls_waterlevel. 49 50 Fields in params: 51 - time_month(uint): Time tag of measurement month 1..12 52 - time_day(uint): Time tag of measurement day of the month 1..31 53 - time_hour(uint): Time tag of measurement UTC hours 0..23 54 - time_min(uint): Time tag of measurement minutes 55 - stationid(aisstr6): Character identifier of the station. Usually a number. 56 - pos_longitude(decimal): Location of measurement East West location 57 - pos_latitude(decimal): Location of measurement North South location 58 - type(uint): How to interpret the water level 59 - waterlevel(int): Water level in centimeters 60 - datum(uint): What reference datum applies to the value 61 - reserved(int): Reserved bits for future use (field automatically set to "0") 62 @param params: Dictionary of field names/values. Throws a ValueError exception if required is missing 63 @param validate: Set to true to cause checking to occur. Runs slower. FIX: not implemented. 64 @rtype: BitVector 65 @return: encoded binary message (for binary messages, this needs to be wrapped in a msg 8 66 ''' 67 68 bvList = [] 69 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['time_month']),4)) 70 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['time_day']),5)) 71 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['time_hour']),5)) 72 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['time_min']),6)) 73 if 'stationid' in params: 74 bvList.append(aisstring.encode(params['stationid'],42)) 75 else: 76 bvList.append(aisstring.encode('@@@@@@@',42)) 77 if 'pos_longitude' in params: 78 bvList.append(binary.bvFromSignedInt(int(Decimal(params['pos_longitude'])*Decimal('60000')),25)) 79 else: 80 bvList.append(binary.bvFromSignedInt(10860000,25)) 81 if 'pos_latitude' in params: 82 bvList.append(binary.bvFromSignedInt(int(Decimal(params['pos_latitude'])*Decimal('60000')),24)) 83 else: 84 bvList.append(binary.bvFromSignedInt(5460000,24)) 85 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['type']),1)) 86 if 'waterlevel' in params: 87 bvList.append(binary.bvFromSignedInt(params['waterlevel'],16)) 88 else: 89 bvList.append(binary.bvFromSignedInt(-32768,16)) 90 if 'datum' in params: 91 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['datum']),5)) 92 else: 93 bvList.append(binary.setBitVectorSize(BitVector(intVal=31),5)) 94 bvList.append(binary.bvFromSignedInt(0,14)) 95 96 return binary.joinBV(bvList)
97
98 -def sls_waterlevelDecode(bv, validate=False):
99 '''Unpack a sls_waterlevel message 100 101 Fields in params: 102 - time_month(uint): Time tag of measurement month 1..12 103 - time_day(uint): Time tag of measurement day of the month 1..31 104 - time_hour(uint): Time tag of measurement UTC hours 0..23 105 - time_min(uint): Time tag of measurement minutes 106 - stationid(aisstr6): Character identifier of the station. Usually a number. 107 - pos_longitude(decimal): Location of measurement East West location 108 - pos_latitude(decimal): Location of measurement North South location 109 - type(uint): How to interpret the water level 110 - waterlevel(int): Water level in centimeters 111 - datum(uint): What reference datum applies to the value 112 - reserved(int): Reserved bits for future use (field automatically set to "0") 113 @type bv: BitVector 114 @param bv: Bits defining a message 115 @param validate: Set to true to cause checking to occur. Runs slower. FIX: not implemented. 116 @rtype: dict 117 @return: params 118 ''' 119 120 #Would be nice to check the bit count here.. 121 #if validate: 122 # assert (len(bv)==FIX: SOME NUMBER) 123 r = {} 124 r['time_month']=int(bv[0:4]) 125 r['time_day']=int(bv[4:9]) 126 r['time_hour']=int(bv[9:14]) 127 r['time_min']=int(bv[14:20]) 128 r['stationid']=aisstring.decode(bv[20:62]) 129 r['pos_longitude']=Decimal(binary.signedIntFromBV(bv[62:87]))/Decimal('60000') 130 r['pos_latitude']=Decimal(binary.signedIntFromBV(bv[87:111]))/Decimal('60000') 131 r['type']=int(bv[111:112]) 132 r['waterlevel']=binary.signedIntFromBV(bv[112:128]) 133 r['datum']=int(bv[128:133]) 134 r['reserved']=0 135 return r
136
137 -def sls_waterlevelPrint(params, out=sys.stdout):
138 '''Print a sls_waterlevel message to stdout. 139 140 Fields in params: 141 - time_month(uint): Time tag of measurement month 1..12 142 - time_day(uint): Time tag of measurement day of the month 1..31 143 - time_hour(uint): Time tag of measurement UTC hours 0..23 144 - time_min(uint): Time tag of measurement minutes 145 - stationid(aisstr6): Character identifier of the station. Usually a number. 146 - pos_longitude(decimal): Location of measurement East West location 147 - pos_latitude(decimal): Location of measurement North South location 148 - type(uint): How to interpret the water level 149 - waterlevel(int): Water level in centimeters 150 - datum(uint): What reference datum applies to the value 151 - reserved(int): Reserved bits for future use (field automatically set to "0") 152 @param params: Dictionary of field names/values. 153 @param out: File like object to write to 154 @rtype: stdout 155 @return: text to out 156 ''' 157 158 out.write("sls_waterlevel:\n") 159 out.write(" time_month: "+str(params['time_month'])+"\n") 160 out.write(" time_day: "+str(params['time_day'])+"\n") 161 out.write(" time_hour: "+str(params['time_hour'])+"\n") 162 out.write(" time_min: "+str(params['time_min'])+"\n") 163 out.write(" pos_longitude: "+str(params['pos_longitude'])+"\n") 164 out.write(" pos_latitude: "+str(params['pos_latitude'])+"\n") 165 out.write(" type: "+str(params['type'])+"\n") 166 out.write(" waterlevel: "+str(params['waterlevel'])+"\n") 167 out.write(" datum: "+str(params['datum'])+"\n") 168 out.write(" reserved: "+str(params['reserved'])+"\n") 169 170 return # Nothing to return
171 172 173 174 ###################################################################### 175 # UNIT TESTING 176 ###################################################################### 177 import unittest
178 -def sls_waterlevelTestParams():
179 '''Return a params file base on the testvalue tags. 180 @rtype: dict 181 @return: params based on testvalue tags 182 ''' 183 params = {} 184 params['time_month'] = 2 185 params['time_day'] = 28 186 params['time_hour'] = 23 187 params['time_min'] = 45 188 params['stationid'] = 'A234567' 189 params['pos_longitude'] = Decimal('-122.16328') 190 params['pos_latitude'] = Decimal('37.42446') 191 params['type'] = 0 192 params['waterlevel'] = -97 193 params['datum'] = 0 194 params['reserved'] = 0 195 196 return params
197
198 -class Testsls_waterlevel(unittest.TestCase):
199 '''Uses the testvalue tag text from each type to build a test case for the sls_waterlevel message'''
200 - def testEncodeDecode(self):
201 202 params = sls_waterlevelTestParams() 203 bits = sls_waterlevelEncode(params) 204 r = sls_waterlevelDecode(bits) 205 206 # Check that each parameter came through ok. 207 self.failUnlessEqual(r['time_month'],params['time_month']) 208 self.failUnlessEqual(r['time_day'],params['time_day']) 209 self.failUnlessEqual(r['time_hour'],params['time_hour']) 210 self.failUnlessEqual(r['time_min'],params['time_min']) 211 self.failUnlessEqual(r['stationid'],params['stationid']) 212 self.failUnlessAlmostEqual(r['pos_longitude'],params['pos_longitude'],4) 213 self.failUnlessAlmostEqual(r['pos_latitude'],params['pos_latitude'],4) 214 self.failUnlessEqual(r['type'],params['type']) 215 self.failUnlessEqual(r['waterlevel'],params['waterlevel']) 216 self.failUnlessEqual(r['datum'],params['datum']) 217 self.failUnlessEqual(r['reserved'],params['reserved'])
218 219 ############################################################ 220 if __name__=='__main__': 221 222 from optparse import OptionParser 223 myparser = OptionParser(usage="%prog [options]", 224 version="%prog "+__version__) 225 226 #sys.exit('EARLY EXIT - FIX: remove') 227 myparser.add_option('--doc-test',dest='doctest',default=False,action='store_true', 228 help='run the documentation tests') 229 myparser.add_option('--unit-test',dest='unittest',default=False,action='store_true', 230 help='run the unit tests') 231 myparser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true', 232 help='Make the test output verbose') 233 234 (options,args) = myparser.parse_args() 235 success=True 236 237 if options.doctest: 238 import os; print os.path.basename(sys.argv[0]), 'doctests ...', 239 sys.argv= [sys.argv[0]] 240 if options.verbose: sys.argv.append('-v') 241 import doctest 242 numfail,numtests=doctest.testmod() 243 if numfail==0: print 'ok' 244 else: 245 print 'FAILED' 246 success=False 247 248 if not success: 249 sys.exit('Something Failed') 250 251 del success # Hide success from epydoc 252 253 if options.unittest: 254 sys.argv = [sys.argv[0]] 255 if options.verbose: sys.argv.append('-v') 256 unittest.main() 257