Module waterlevel
[hide private]
[frames] | no frames]

Source Code for Module waterlevel

  1  #!/usr/bin/env python 
  2   
  3  __version__ = '$Revision: 4791 $'.split()[1] 
  4  __date__ = '$Date: 2006-11-30 $'.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 waterlevelEncode(params, validate=False):
48 '''Create a waterlevel binary message payload to pack into an AIS Msg 8 (binary message). 49 50 Fields in params: 51 - dac(uint): Designated Area Code (field automatically set to "366") 52 - fid(uint): Functional Identifier (field automatically set to "1") 53 - efid(uint): extended functional identifier (field automatically set to "12") 54 - timetag_month(uint): Time the measurement represents month 1..12 55 - timetag_day(uint): Time the measurement represents day of the month 1..31 56 - timetag_hour(uint): Time the measurement represents UTC hours 0..23 57 - timetag_min(uint): Time the measurement represents minutes 58 - timetag_sec(uint): Time the measurement represents seconds 59 - stationid(aisstr6): Character identifier of the station. Usually a number. 60 - stationloc_longitude(decimal): Location of the sensor taking the water level measurement or position of prediction East West location 61 - stationloc_latitude(decimal): Location of the sensor taking the water level measurement or position of prediction North South location 62 - waterlevel(int): Water level in centimeters 63 - datum(uint): What reference datum applies to the value 64 - sigma(float): Standard deviation of 1 second samples used to compute the water level height 65 - o(uint): Count of number of samples that fall outside a 3-sigma band about the mean 66 - levelinferred(bool): indicates that the water level value has been inferred 67 - flat_tolerance_exceeded(bool): flat tolerance limit was exceeded. Need better descr 68 - rate_tolerance_exceeded(bool): rate of change tolerance limit was exceeded 69 - temp_tolerance_exceeded(bool): temperature difference tolerance limit was exceeded 70 - expected_height_exceeded(bool): either the maximum or minimum expected water level height limit was exceeded 71 - link_down(bool): Unable to communicate with the tide system. All data invalid 72 - timeLastMeasured_month(uint): Time since last measured value was available month 1..12 73 - timeLastMeasured_day(uint): Time since last measured value was available day of the month 1..31 74 - timeLastMeasured_hour(uint): Time since last measured value was available UTC hours 0..23 75 - timeLastMeasured_min(uint): Time since last measured value was available minutes 76 - timeLastMeasured_sec(uint): Time since last measured value was available seconds 77 @param params: Dictionary of field names/values. Throws a ValueError exception if required is missing 78 @param validate: Set to true to cause checking to occur. Runs slower. FIX: not implemented. 79 @rtype: BitVector 80 @return: encoded binary message ready for wrapping in an AIS Msg 8 81 ''' 82 83 bvList = [] 84 bvList.append(binary.setBitVectorSize(BitVector(intVal=366),16)) 85 bvList.append(binary.setBitVectorSize(BitVector(intVal=1),4)) 86 bvList.append(binary.setBitVectorSize(BitVector(intVal=12),12)) 87 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['timetag_month']),4)) 88 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['timetag_day']),5)) 89 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['timetag_hour']),5)) 90 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['timetag_min']),6)) 91 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['timetag_sec']),6)) 92 if 'stationid' in params: 93 bvList.append(aisstring.encode(params['stationid'],42)) 94 else: 95 bvList.append(aisstring.encode('@@@@@@@',42)) 96 if 'stationloc_longitude' in params: 97 bvList.append(binary.bvFromSignedInt(int(Decimal(params['stationloc_longitude'])*Decimal('600000')),28)) 98 else: 99 bvList.append(binary.bvFromSignedInt(108600000,28)) 100 if 'stationloc_latitude' in params: 101 bvList.append(binary.bvFromSignedInt(int(Decimal(params['stationloc_latitude'])*Decimal('600000')),27)) 102 else: 103 bvList.append(binary.bvFromSignedInt(54600000,27)) 104 if 'waterlevel' in params: 105 bvList.append(binary.bvFromSignedInt(params['waterlevel'],16)) 106 else: 107 bvList.append(binary.bvFromSignedInt(-32768,16)) 108 if 'datum' in params: 109 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['datum']),5)) 110 else: 111 bvList.append(binary.setBitVectorSize(BitVector(intVal=31),5)) 112 bvList.append(binary.float2bitvec(params['sigma'])) 113 if 'o' in params: 114 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['o']),8)) 115 else: 116 bvList.append(binary.setBitVectorSize(BitVector(intVal=255),8)) 117 if params["levelinferred"]: bvList.append(TrueBV) 118 else: bvList.append(FalseBV) 119 if params["flat_tolerance_exceeded"]: bvList.append(TrueBV) 120 else: bvList.append(FalseBV) 121 if params["rate_tolerance_exceeded"]: bvList.append(TrueBV) 122 else: bvList.append(FalseBV) 123 if params["temp_tolerance_exceeded"]: bvList.append(TrueBV) 124 else: bvList.append(FalseBV) 125 if params["expected_height_exceeded"]: bvList.append(TrueBV) 126 else: bvList.append(FalseBV) 127 if params["link_down"]: bvList.append(TrueBV) 128 else: bvList.append(FalseBV) 129 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['timeLastMeasured_month']),4)) 130 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['timeLastMeasured_day']),5)) 131 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['timeLastMeasured_hour']),5)) 132 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['timeLastMeasured_min']),6)) 133 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['timeLastMeasured_sec']),6)) 134 135 return binary.joinBV(bvList)
136
137 -def waterlevelDecode(bv, validate=False):
138 '''Unpack a waterlevel binary message bit payload from an AIS Msg 8 (binary message) 139 140 Fields in params: 141 - dac(uint): Designated Area Code (field automatically set to "366") 142 - fid(uint): Functional Identifier (field automatically set to "1") 143 - efid(uint): extended functional identifier (field automatically set to "12") 144 - timetag_month(uint): Time the measurement represents month 1..12 145 - timetag_day(uint): Time the measurement represents day of the month 1..31 146 - timetag_hour(uint): Time the measurement represents UTC hours 0..23 147 - timetag_min(uint): Time the measurement represents minutes 148 - timetag_sec(uint): Time the measurement represents seconds 149 - stationid(aisstr6): Character identifier of the station. Usually a number. 150 - stationloc_longitude(decimal): Location of the sensor taking the water level measurement or position of prediction East West location 151 - stationloc_latitude(decimal): Location of the sensor taking the water level measurement or position of prediction North South location 152 - waterlevel(int): Water level in centimeters 153 - datum(uint): What reference datum applies to the value 154 - sigma(float): Standard deviation of 1 second samples used to compute the water level height 155 - o(uint): Count of number of samples that fall outside a 3-sigma band about the mean 156 - levelinferred(bool): indicates that the water level value has been inferred 157 - flat_tolerance_exceeded(bool): flat tolerance limit was exceeded. Need better descr 158 - rate_tolerance_exceeded(bool): rate of change tolerance limit was exceeded 159 - temp_tolerance_exceeded(bool): temperature difference tolerance limit was exceeded 160 - expected_height_exceeded(bool): either the maximum or minimum expected water level height limit was exceeded 161 - link_down(bool): Unable to communicate with the tide system. All data invalid 162 - timeLastMeasured_month(uint): Time since last measured value was available month 1..12 163 - timeLastMeasured_day(uint): Time since last measured value was available day of the month 1..31 164 - timeLastMeasured_hour(uint): Time since last measured value was available UTC hours 0..23 165 - timeLastMeasured_min(uint): Time since last measured value was available minutes 166 - timeLastMeasured_sec(uint): Time since last measured value was available seconds 167 @type bv: BitVector 168 @param bv: Bits defining a message 169 @param validate: Set to true to cause checking to occur. Runs slower. FIX: not implemented. 170 @rtype: dict 171 @return: params 172 ''' 173 174 r = {} 175 r['dac']=366 176 r['fid']=1 177 r['efid']=12 178 r['timetag_month']=int(bv[32:36]) 179 r['timetag_day']=int(bv[36:41]) 180 r['timetag_hour']=int(bv[41:46]) 181 r['timetag_min']=int(bv[46:52]) 182 r['timetag_sec']=int(bv[52:58]) 183 r['stationid']=aisstring.decode(bv[58:100]) 184 r['stationloc_longitude']=Decimal(binary.signedIntFromBV(bv[100:128]))/Decimal('600000') 185 r['stationloc_latitude']=Decimal(binary.signedIntFromBV(bv[128:155]))/Decimal('600000') 186 r['waterlevel']=binary.signedIntFromBV(bv[155:171]) 187 r['datum']=int(bv[171:176]) 188 r['sigma']=binary.bitvec2float(bv[176:208]) 189 r['o']=int(bv[208:216]) 190 r['levelinferred']=bool(int(bv[216:217])) 191 r['flat_tolerance_exceeded']=bool(int(bv[217:218])) 192 r['rate_tolerance_exceeded']=bool(int(bv[218:219])) 193 r['temp_tolerance_exceeded']=bool(int(bv[219:220])) 194 r['expected_height_exceeded']=bool(int(bv[220:221])) 195 r['link_down']=bool(int(bv[221:222])) 196 r['timeLastMeasured_month']=int(bv[222:226]) 197 r['timeLastMeasured_day']=int(bv[226:231]) 198 r['timeLastMeasured_hour']=int(bv[231:236]) 199 r['timeLastMeasured_min']=int(bv[236:242]) 200 r['timeLastMeasured_sec']=int(bv[242:248]) 201 return r
202 203 204 ###################################################################### 205 # UNIT TESTING 206 ###################################################################### 207 import unittest
208 -def waterlevelTestParams():
209 '''Return a params file base on the testvalue tags. 210 @rtype: dict 211 @return: params based on testvalue tags 212 ''' 213 params = {} 214 params['dac'] = 366 215 params['fid'] = 1 216 params['efid'] = 12 217 params['timetag_month'] = 2 218 params['timetag_day'] = 28 219 params['timetag_hour'] = 23 220 params['timetag_min'] = 45 221 params['timetag_sec'] = 58 222 params['stationid'] = 'A234567' 223 params['stationloc_longitude'] = Decimal('-122.16328055555556') 224 params['stationloc_latitude'] = Decimal('37.424458333333334') 225 params['waterlevel'] = -97 226 params['datum'] = 0 227 params['sigma'] = -1.234 228 params['o'] = 240 229 params['levelinferred'] = False 230 params['flat_tolerance_exceeded'] = True 231 params['rate_tolerance_exceeded'] = False 232 params['temp_tolerance_exceeded'] = False 233 params['expected_height_exceeded'] = True 234 params['link_down'] = False 235 params['timeLastMeasured_month'] = 2 236 params['timeLastMeasured_day'] = 28 237 params['timeLastMeasured_hour'] = 23 238 params['timeLastMeasured_min'] = 45 239 params['timeLastMeasured_sec'] = 58 240 241 return params
242
243 -class Testwaterlevel(unittest.TestCase):
244 '''Uses the testvalue tag text from each type to build a test case for the waterlevel message'''
245 - def testEncodeDecode(self):
246 247 params = waterlevelTestParams() 248 bits = waterlevelEncode(params) 249 r = waterlevelDecode(bits) 250 251 # Check that each parameter came through ok. 252 self.failUnlessEqual(r['dac'],params['dac']) 253 self.failUnlessEqual(r['fid'],params['fid']) 254 self.failUnlessEqual(r['efid'],params['efid']) 255 self.failUnlessEqual(r['timetag_month'],params['timetag_month']) 256 self.failUnlessEqual(r['timetag_day'],params['timetag_day']) 257 self.failUnlessEqual(r['timetag_hour'],params['timetag_hour']) 258 self.failUnlessEqual(r['timetag_min'],params['timetag_min']) 259 self.failUnlessEqual(r['timetag_sec'],params['timetag_sec']) 260 self.failUnlessEqual(r['stationid'],params['stationid']) 261 self.failUnlessAlmostEqual(r['stationloc_longitude'],params['stationloc_longitude'],5) 262 self.failUnlessAlmostEqual(r['stationloc_latitude'],params['stationloc_latitude'],5) 263 self.failUnlessEqual(r['waterlevel'],params['waterlevel']) 264 self.failUnlessEqual(r['datum'],params['datum']) 265 self.failUnlessAlmostEqual(r['sigma'],params['sigma'],7) 266 self.failUnlessEqual(r['o'],params['o']) 267 self.failUnlessEqual(r['levelinferred'],params['levelinferred']) 268 self.failUnlessEqual(r['flat_tolerance_exceeded'],params['flat_tolerance_exceeded']) 269 self.failUnlessEqual(r['rate_tolerance_exceeded'],params['rate_tolerance_exceeded']) 270 self.failUnlessEqual(r['temp_tolerance_exceeded'],params['temp_tolerance_exceeded']) 271 self.failUnlessEqual(r['expected_height_exceeded'],params['expected_height_exceeded']) 272 self.failUnlessEqual(r['link_down'],params['link_down']) 273 self.failUnlessEqual(r['timeLastMeasured_month'],params['timeLastMeasured_month']) 274 self.failUnlessEqual(r['timeLastMeasured_day'],params['timeLastMeasured_day']) 275 self.failUnlessEqual(r['timeLastMeasured_hour'],params['timeLastMeasured_hour']) 276 self.failUnlessEqual(r['timeLastMeasured_min'],params['timeLastMeasured_min']) 277 self.failUnlessEqual(r['timeLastMeasured_sec'],params['timeLastMeasured_sec'])
278 279 ############################################################ 280 if __name__=='__main__': 281 282 from optparse import OptionParser 283 myparser = OptionParser(usage="%prog [options]", 284 version="%prog "+__version__) 285 286 #sys.exit('EARLY EXIT - FIX: remove') 287 myparser.add_option('--doc-test',dest='doctest',default=False,action='store_true', 288 help='run the documentation tests') 289 myparser.add_option('--unit-test',dest='unittest',default=False,action='store_true', 290 help='run the unit tests') 291 myparser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true', 292 help='Make the test output verbose') 293 294 (options,args) = myparser.parse_args() 295 success=True 296 297 if options.doctest: 298 import os; print os.path.basename(sys.argv[0]), 'doctests ...', 299 sys.argv= [sys.argv[0]] 300 if options.verbose: sys.argv.append('-v') 301 import doctest 302 numfail,numtests=doctest.testmod() 303 if numfail==0: print 'ok' 304 else: 305 print 'FAILED' 306 success=False 307 308 if not success: 309 sys.exit('Something Failed') 310 311 del success # Hide success from epydoc 312 313 if options.unittest: 314 sys.argv = [sys.argv[0]] 315 if options.verbose: sys.argv.append('-v') 316 unittest.main() 317