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-12-12 $'.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 waterlevel. 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 (for binary messages, this needs to be wrapped in a 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 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 #Would be nice to check the bit count here.. 175 #if validate: 176 # assert (len(bv)==FIX: SOME NUMBER) 177 r = {} 178 r['dac']=366 179 r['fid']=1 180 r['efid']=12 181 r['timetag_month']=int(bv[32:36]) 182 r['timetag_day']=int(bv[36:41]) 183 r['timetag_hour']=int(bv[41:46]) 184 r['timetag_min']=int(bv[46:52]) 185 r['timetag_sec']=int(bv[52:58]) 186 r['stationid']=aisstring.decode(bv[58:100]) 187 r['stationloc_longitude']=Decimal(binary.signedIntFromBV(bv[100:128]))/Decimal('600000') 188 r['stationloc_latitude']=Decimal(binary.signedIntFromBV(bv[128:155]))/Decimal('600000') 189 r['waterlevel']=binary.signedIntFromBV(bv[155:171]) 190 r['datum']=int(bv[171:176]) 191 r['sigma']=binary.bitvec2float(bv[176:208]) 192 r['o']=int(bv[208:216]) 193 r['levelinferred']=bool(int(bv[216:217])) 194 r['flat_tolerance_exceeded']=bool(int(bv[217:218])) 195 r['rate_tolerance_exceeded']=bool(int(bv[218:219])) 196 r['temp_tolerance_exceeded']=bool(int(bv[219:220])) 197 r['expected_height_exceeded']=bool(int(bv[220:221])) 198 r['link_down']=bool(int(bv[221:222])) 199 r['timeLastMeasured_month']=int(bv[222:226]) 200 r['timeLastMeasured_day']=int(bv[226:231]) 201 r['timeLastMeasured_hour']=int(bv[231:236]) 202 r['timeLastMeasured_min']=int(bv[236:242]) 203 r['timeLastMeasured_sec']=int(bv[242:248]) 204 return r
205
206 -def waterlevelPrint(params, out=sys.stdout):
207 '''Print a waterlevel message to stdout. 208 209 Fields in params: 210 - dac(uint): Designated Area Code (field automatically set to "366") 211 - fid(uint): Functional Identifier (field automatically set to "1") 212 - efid(uint): extended functional identifier (field automatically set to "12") 213 - timetag_month(uint): Time the measurement represents month 1..12 214 - timetag_day(uint): Time the measurement represents day of the month 1..31 215 - timetag_hour(uint): Time the measurement represents UTC hours 0..23 216 - timetag_min(uint): Time the measurement represents minutes 217 - timetag_sec(uint): Time the measurement represents seconds 218 - stationid(aisstr6): Character identifier of the station. Usually a number. 219 - stationloc_longitude(decimal): Location of the sensor taking the water level measurement or position of prediction East West location 220 - stationloc_latitude(decimal): Location of the sensor taking the water level measurement or position of prediction North South location 221 - waterlevel(int): Water level in centimeters 222 - datum(uint): What reference datum applies to the value 223 - sigma(float): Standard deviation of 1 second samples used to compute the water level height 224 - o(uint): Count of number of samples that fall outside a 3-sigma band about the mean 225 - levelinferred(bool): indicates that the water level value has been inferred 226 - flat_tolerance_exceeded(bool): flat tolerance limit was exceeded. Need better descr 227 - rate_tolerance_exceeded(bool): rate of change tolerance limit was exceeded 228 - temp_tolerance_exceeded(bool): temperature difference tolerance limit was exceeded 229 - expected_height_exceeded(bool): either the maximum or minimum expected water level height limit was exceeded 230 - link_down(bool): Unable to communicate with the tide system. All data invalid 231 - timeLastMeasured_month(uint): Time since last measured value was available month 1..12 232 - timeLastMeasured_day(uint): Time since last measured value was available day of the month 1..31 233 - timeLastMeasured_hour(uint): Time since last measured value was available UTC hours 0..23 234 - timeLastMeasured_min(uint): Time since last measured value was available minutes 235 - timeLastMeasured_sec(uint): Time since last measured value was available seconds 236 @param params: Dictionary of field names/values. 237 @param out: File like object to write to 238 @rtype: stdout 239 @return: text to out 240 ''' 241 242 out.write("waterlevel:\n") 243 out.write(" dac: "+str(+params['dac'])+"\n") 244 out.write(" fid: "+str(+params['fid'])+"\n") 245 out.write(" efid: "+str(+params['efid'])+"\n") 246 out.write(" timetag_month: "+str(+params['timetag_month'])+"\n") 247 out.write(" timetag_day: "+str(+params['timetag_day'])+"\n") 248 out.write(" timetag_hour: "+str(+params['timetag_hour'])+"\n") 249 out.write(" timetag_min: "+str(+params['timetag_min'])+"\n") 250 out.write(" timetag_sec: "+str(+params['timetag_sec'])+"\n") 251 out.write(" stationloc_longitude: "+str(+params['stationloc_longitude'])+"\n") 252 out.write(" stationloc_latitude: "+str(+params['stationloc_latitude'])+"\n") 253 out.write(" waterlevel: "+str(+params['waterlevel'])+"\n") 254 out.write(" datum: "+str(+params['datum'])+"\n") 255 out.write(" sigma: "+str(+params['sigma'])+"\n") 256 out.write(" o: "+str(+params['o'])+"\n") 257 out.write(" levelinferred: "+str(+params['levelinferred'])+"\n") 258 out.write(" flat_tolerance_exceeded: "+str(+params['flat_tolerance_exceeded'])+"\n") 259 out.write(" rate_tolerance_exceeded: "+str(+params['rate_tolerance_exceeded'])+"\n") 260 out.write(" temp_tolerance_exceeded: "+str(+params['temp_tolerance_exceeded'])+"\n") 261 out.write(" expected_height_exceeded: "+str(+params['expected_height_exceeded'])+"\n") 262 out.write(" link_down: "+str(+params['link_down'])+"\n") 263 out.write(" timeLastMeasured_month: "+str(+params['timeLastMeasured_month'])+"\n") 264 out.write(" timeLastMeasured_day: "+str(+params['timeLastMeasured_day'])+"\n") 265 out.write(" timeLastMeasured_hour: "+str(+params['timeLastMeasured_hour'])+"\n") 266 out.write(" timeLastMeasured_min: "+str(+params['timeLastMeasured_min'])+"\n") 267 out.write(" timeLastMeasured_sec: "+str(+params['timeLastMeasured_sec'])+"\n") 268 269 return # Nothing to return
270 271 272 273 ###################################################################### 274 # UNIT TESTING 275 ###################################################################### 276 import unittest
277 -def waterlevelTestParams():
278 '''Return a params file base on the testvalue tags. 279 @rtype: dict 280 @return: params based on testvalue tags 281 ''' 282 params = {} 283 params['dac'] = 366 284 params['fid'] = 1 285 params['efid'] = 12 286 params['timetag_month'] = 2 287 params['timetag_day'] = 28 288 params['timetag_hour'] = 23 289 params['timetag_min'] = 45 290 params['timetag_sec'] = 58 291 params['stationid'] = 'A234567' 292 params['stationloc_longitude'] = Decimal('-122.16328055555556') 293 params['stationloc_latitude'] = Decimal('37.424458333333334') 294 params['waterlevel'] = -97 295 params['datum'] = 0 296 params['sigma'] = -1.234 297 params['o'] = 240 298 params['levelinferred'] = False 299 params['flat_tolerance_exceeded'] = True 300 params['rate_tolerance_exceeded'] = False 301 params['temp_tolerance_exceeded'] = False 302 params['expected_height_exceeded'] = True 303 params['link_down'] = False 304 params['timeLastMeasured_month'] = 2 305 params['timeLastMeasured_day'] = 28 306 params['timeLastMeasured_hour'] = 23 307 params['timeLastMeasured_min'] = 45 308 params['timeLastMeasured_sec'] = 58 309 310 return params
311
312 -class Testwaterlevel(unittest.TestCase):
313 '''Uses the testvalue tag text from each type to build a test case for the waterlevel message'''
314 - def testEncodeDecode(self):
315 316 params = waterlevelTestParams() 317 bits = waterlevelEncode(params) 318 r = waterlevelDecode(bits) 319 320 # Check that each parameter came through ok. 321 self.failUnlessEqual(r['dac'],params['dac']) 322 self.failUnlessEqual(r['fid'],params['fid']) 323 self.failUnlessEqual(r['efid'],params['efid']) 324 self.failUnlessEqual(r['timetag_month'],params['timetag_month']) 325 self.failUnlessEqual(r['timetag_day'],params['timetag_day']) 326 self.failUnlessEqual(r['timetag_hour'],params['timetag_hour']) 327 self.failUnlessEqual(r['timetag_min'],params['timetag_min']) 328 self.failUnlessEqual(r['timetag_sec'],params['timetag_sec']) 329 self.failUnlessEqual(r['stationid'],params['stationid']) 330 self.failUnlessAlmostEqual(r['stationloc_longitude'],params['stationloc_longitude'],5) 331 self.failUnlessAlmostEqual(r['stationloc_latitude'],params['stationloc_latitude'],5) 332 self.failUnlessEqual(r['waterlevel'],params['waterlevel']) 333 self.failUnlessEqual(r['datum'],params['datum']) 334 self.failUnlessAlmostEqual(r['sigma'],params['sigma'],7) 335 self.failUnlessEqual(r['o'],params['o']) 336 self.failUnlessEqual(r['levelinferred'],params['levelinferred']) 337 self.failUnlessEqual(r['flat_tolerance_exceeded'],params['flat_tolerance_exceeded']) 338 self.failUnlessEqual(r['rate_tolerance_exceeded'],params['rate_tolerance_exceeded']) 339 self.failUnlessEqual(r['temp_tolerance_exceeded'],params['temp_tolerance_exceeded']) 340 self.failUnlessEqual(r['expected_height_exceeded'],params['expected_height_exceeded']) 341 self.failUnlessEqual(r['link_down'],params['link_down']) 342 self.failUnlessEqual(r['timeLastMeasured_month'],params['timeLastMeasured_month']) 343 self.failUnlessEqual(r['timeLastMeasured_day'],params['timeLastMeasured_day']) 344 self.failUnlessEqual(r['timeLastMeasured_hour'],params['timeLastMeasured_hour']) 345 self.failUnlessEqual(r['timeLastMeasured_min'],params['timeLastMeasured_min']) 346 self.failUnlessEqual(r['timeLastMeasured_sec'],params['timeLastMeasured_sec'])
347 348 ############################################################ 349 if __name__=='__main__': 350 351 from optparse import OptionParser 352 myparser = OptionParser(usage="%prog [options]", 353 version="%prog "+__version__) 354 355 #sys.exit('EARLY EXIT - FIX: remove') 356 myparser.add_option('--doc-test',dest='doctest',default=False,action='store_true', 357 help='run the documentation tests') 358 myparser.add_option('--unit-test',dest='unittest',default=False,action='store_true', 359 help='run the unit tests') 360 myparser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true', 361 help='Make the test output verbose') 362 363 (options,args) = myparser.parse_args() 364 success=True 365 366 if options.doctest: 367 import os; print os.path.basename(sys.argv[0]), 'doctests ...', 368 sys.argv= [sys.argv[0]] 369 if options.verbose: sys.argv.append('-v') 370 import doctest 371 numfail,numtests=doctest.testmod() 372 if numfail==0: print 'ok' 373 else: 374 print 'FAILED' 375 success=False 376 377 if not success: 378 sys.exit('Something Failed') 379 380 del success # Hide success from epydoc 381 382 if options.unittest: 383 sys.argv = [sys.argv[0]] 384 if options.verbose: sys.argv.append('-v') 385 unittest.main() 386