1
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
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
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
175
176
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
207 r={}; r['dac']=366
208 return(r['dac'])
209
211 r={}; r['fid']=1
212 return(r['fid'])
213
215 r={}; r['efid']=12
216 return(r['efid'])
217
221
225
229
233
237
241
243 r={}; r['stationloc_longitude']=Decimal(binary.signedIntFromBV(bv[100:128]))/Decimal('600000')
244 return(r['stationloc_longitude'])
245
247 r={}; r['stationloc_latitude']=Decimal(binary.signedIntFromBV(bv[128:155]))/Decimal('600000')
248 return(r['stationloc_latitude'])
249
253
255 r={}; r['datum']=int(bv[171:176])
256 return(r['datum'])
257
261
263 r={}; r['o']=int(bv[208:216])
264 return(r['o'])
265
267 r={}; r['levelinferred']=bool(int(bv[216:217]))
268 return(r['levelinferred'])
269
271 r={}; r['flat_tolerance_exceeded']=bool(int(bv[217:218]))
272 return(r['flat_tolerance_exceeded'])
273
275 r={}; r['rate_tolerance_exceeded']=bool(int(bv[218:219]))
276 return(r['rate_tolerance_exceeded'])
277
279 r={}; r['temp_tolerance_exceeded']=bool(int(bv[219:220]))
280 return(r['temp_tolerance_exceeded'])
281
283 r={}; r['expected_height_exceeded']=bool(int(bv[220:221]))
284 return(r['expected_height_exceeded'])
285
287 r={}; r['link_down']=bool(int(bv[221:222]))
288 return(r['link_down'])
289
291 r={}; r['timeLastMeasured_month']=int(bv[222:226])
292 return(r['timeLastMeasured_month'])
293
295 r={}; r['timeLastMeasured_day']=int(bv[226:231])
296 return(r['timeLastMeasured_day'])
297
299 r={}; r['timeLastMeasured_hour']=int(bv[231:236])
300 return(r['timeLastMeasured_hour'])
301
303 r={}; r['timeLastMeasured_min']=int(bv[236:242])
304 return(r['timeLastMeasured_min'])
305
307 r={}; r['timeLastMeasured_sec']=int(bv[242:248])
308 return(r['timeLastMeasured_sec'])
309
311 '''Print a waterlevel message to stdout.
312
313 Fields in params:
314 - dac(uint): Designated Area Code (field automatically set to "366")
315 - fid(uint): Functional Identifier (field automatically set to "1")
316 - efid(uint): extended functional identifier (field automatically set to "12")
317 - timetag_month(uint): Time the measurement represents month 1..12
318 - timetag_day(uint): Time the measurement represents day of the month 1..31
319 - timetag_hour(uint): Time the measurement represents UTC hours 0..23
320 - timetag_min(uint): Time the measurement represents minutes
321 - timetag_sec(uint): Time the measurement represents seconds
322 - stationid(aisstr6): Character identifier of the station. Usually a number.
323 - stationloc_longitude(decimal): Location of the sensor taking the water level measurement or position of prediction East West location
324 - stationloc_latitude(decimal): Location of the sensor taking the water level measurement or position of prediction North South location
325 - waterlevel(int): Water level in centimeters
326 - datum(uint): What reference datum applies to the value
327 - sigma(float): Standard deviation of 1 second samples used to compute the water level height
328 - o(uint): Count of number of samples that fall outside a 3-sigma band about the mean
329 - levelinferred(bool): indicates that the water level value has been inferred
330 - flat_tolerance_exceeded(bool): flat tolerance limit was exceeded. Need better descr
331 - rate_tolerance_exceeded(bool): rate of change tolerance limit was exceeded
332 - temp_tolerance_exceeded(bool): temperature difference tolerance limit was exceeded
333 - expected_height_exceeded(bool): either the maximum or minimum expected water level height limit was exceeded
334 - link_down(bool): Unable to communicate with the tide system. All data invalid
335 - timeLastMeasured_month(uint): Time since last measured value was available month 1..12
336 - timeLastMeasured_day(uint): Time since last measured value was available day of the month 1..31
337 - timeLastMeasured_hour(uint): Time since last measured value was available UTC hours 0..23
338 - timeLastMeasured_min(uint): Time since last measured value was available minutes
339 - timeLastMeasured_sec(uint): Time since last measured value was available seconds
340 @param params: Dictionary of field names/values.
341 @param out: File like object to write to
342 @rtype: stdout
343 @return: text to out
344 '''
345
346 out.write("waterlevel:\n")
347 out.write(" dac: "+str(params['dac'])+"\n")
348 out.write(" fid: "+str(params['fid'])+"\n")
349 out.write(" efid: "+str(params['efid'])+"\n")
350 out.write(" timetag_month: "+str(params['timetag_month'])+"\n")
351 out.write(" timetag_day: "+str(params['timetag_day'])+"\n")
352 out.write(" timetag_hour: "+str(params['timetag_hour'])+"\n")
353 out.write(" timetag_min: "+str(params['timetag_min'])+"\n")
354 out.write(" timetag_sec: "+str(params['timetag_sec'])+"\n")
355 out.write(" stationid: "+str(params['stationid'])+"\n")
356 out.write(" stationloc_longitude: "+str(params['stationloc_longitude'])+"\n")
357 out.write(" stationloc_latitude: "+str(params['stationloc_latitude'])+"\n")
358 out.write(" waterlevel: "+str(params['waterlevel'])+"\n")
359 out.write(" datum: "+str(params['datum'])+"\n")
360 out.write(" sigma: "+str(params['sigma'])+"\n")
361 out.write(" o: "+str(params['o'])+"\n")
362 out.write(" levelinferred: "+str(params['levelinferred'])+"\n")
363 out.write(" flat_tolerance_exceeded: "+str(params['flat_tolerance_exceeded'])+"\n")
364 out.write(" rate_tolerance_exceeded: "+str(params['rate_tolerance_exceeded'])+"\n")
365 out.write(" temp_tolerance_exceeded: "+str(params['temp_tolerance_exceeded'])+"\n")
366 out.write(" expected_height_exceeded: "+str(params['expected_height_exceeded'])+"\n")
367 out.write(" link_down: "+str(params['link_down'])+"\n")
368 out.write(" timeLastMeasured_month: "+str(params['timeLastMeasured_month'])+"\n")
369 out.write(" timeLastMeasured_day: "+str(params['timeLastMeasured_day'])+"\n")
370 out.write(" timeLastMeasured_hour: "+str(params['timeLastMeasured_hour'])+"\n")
371 out.write(" timeLastMeasured_min: "+str(params['timeLastMeasured_min'])+"\n")
372 out.write(" timeLastMeasured_sec: "+str(params['timeLastMeasured_sec'])+"\n")
373
374 return
375
376
377
378
379
380
381 import unittest
383 '''Return a params file base on the testvalue tags.
384 @rtype: dict
385 @return: params based on testvalue tags
386 '''
387 params = {}
388 params['dac'] = 366
389 params['fid'] = 1
390 params['efid'] = 12
391 params['timetag_month'] = 2
392 params['timetag_day'] = 28
393 params['timetag_hour'] = 23
394 params['timetag_min'] = 45
395 params['timetag_sec'] = 58
396 params['stationid'] = 'A234567'
397 params['stationloc_longitude'] = Decimal('-122.16328055555556')
398 params['stationloc_latitude'] = Decimal('37.424458333333334')
399 params['waterlevel'] = -97
400 params['datum'] = 0
401 params['sigma'] = -1.234
402 params['o'] = 240
403 params['levelinferred'] = False
404 params['flat_tolerance_exceeded'] = True
405 params['rate_tolerance_exceeded'] = False
406 params['temp_tolerance_exceeded'] = False
407 params['expected_height_exceeded'] = True
408 params['link_down'] = False
409 params['timeLastMeasured_month'] = 2
410 params['timeLastMeasured_day'] = 28
411 params['timeLastMeasured_hour'] = 23
412 params['timeLastMeasured_min'] = 45
413 params['timeLastMeasured_sec'] = 58
414
415 return params
416
418 '''Uses the testvalue tag text from each type to build a test case for the waterlevel message'''
420
421 params = waterlevelTestParams()
422 bits = waterlevelEncode(params)
423 r = waterlevelDecode(bits)
424
425
426 self.failUnlessEqual(r['dac'],params['dac'])
427 self.failUnlessEqual(r['fid'],params['fid'])
428 self.failUnlessEqual(r['efid'],params['efid'])
429 self.failUnlessEqual(r['timetag_month'],params['timetag_month'])
430 self.failUnlessEqual(r['timetag_day'],params['timetag_day'])
431 self.failUnlessEqual(r['timetag_hour'],params['timetag_hour'])
432 self.failUnlessEqual(r['timetag_min'],params['timetag_min'])
433 self.failUnlessEqual(r['timetag_sec'],params['timetag_sec'])
434 self.failUnlessEqual(r['stationid'],params['stationid'])
435 self.failUnlessAlmostEqual(r['stationloc_longitude'],params['stationloc_longitude'],5)
436 self.failUnlessAlmostEqual(r['stationloc_latitude'],params['stationloc_latitude'],5)
437 self.failUnlessEqual(r['waterlevel'],params['waterlevel'])
438 self.failUnlessEqual(r['datum'],params['datum'])
439 self.failUnlessAlmostEqual(r['sigma'],params['sigma'],7)
440 self.failUnlessEqual(r['o'],params['o'])
441 self.failUnlessEqual(r['levelinferred'],params['levelinferred'])
442 self.failUnlessEqual(r['flat_tolerance_exceeded'],params['flat_tolerance_exceeded'])
443 self.failUnlessEqual(r['rate_tolerance_exceeded'],params['rate_tolerance_exceeded'])
444 self.failUnlessEqual(r['temp_tolerance_exceeded'],params['temp_tolerance_exceeded'])
445 self.failUnlessEqual(r['expected_height_exceeded'],params['expected_height_exceeded'])
446 self.failUnlessEqual(r['link_down'],params['link_down'])
447 self.failUnlessEqual(r['timeLastMeasured_month'],params['timeLastMeasured_month'])
448 self.failUnlessEqual(r['timeLastMeasured_day'],params['timeLastMeasured_day'])
449 self.failUnlessEqual(r['timeLastMeasured_hour'],params['timeLastMeasured_hour'])
450 self.failUnlessEqual(r['timeLastMeasured_min'],params['timeLastMeasured_min'])
451 self.failUnlessEqual(r['timeLastMeasured_sec'],params['timeLastMeasured_sec'])
452
453
454 if __name__=='__main__':
455
456 from optparse import OptionParser
457 myparser = OptionParser(usage="%prog [options]",
458 version="%prog "+__version__)
459
460
461 myparser.add_option('--doc-test',dest='doctest',default=False,action='store_true',
462 help='run the documentation tests')
463 myparser.add_option('--unit-test',dest='unittest',default=False,action='store_true',
464 help='run the unit tests')
465 myparser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true',
466 help='Make the test output verbose')
467
468 (options,args) = myparser.parse_args()
469 success=True
470
471 if options.doctest:
472 import os; print os.path.basename(sys.argv[0]), 'doctests ...',
473 sys.argv= [sys.argv[0]]
474 if options.verbose: sys.argv.append('-v')
475 import doctest
476 numfail,numtests=doctest.testmod()
477 if numfail==0: print 'ok'
478 else:
479 print 'FAILED'
480 success=False
481
482 if not success:
483 sys.exit('Something Failed')
484
485 del success
486
487 if options.unittest:
488 sys.argv = [sys.argv[0]]
489 if options.verbose: sys.argv.append('-v')
490 unittest.main()
491