1
2
3 __version__ = '$Revision: 4791 $'.split()[1]
4 __date__ = '$Date: 2007-03-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__ parser
31 @status: under development
32 @license: Generated code has no license
33 @todo: FIX: put in a description of the message here with fields and types.
34 '''
35
36 import sys
37 from decimal import Decimal
38 from BitVector import BitVector
39
40 import binary, aisstring
41
42
43 TrueBV = BitVector(bitstring="1")
44 "Why always rebuild the True bit? This should speed things up a bunch"
45 FalseBV = BitVector(bitstring="0")
46 "Why always rebuild the False bit? This should speed things up a bunch"
47
48
49 fieldList = (
50 'time_month',
51 'time_day',
52 'time_hour',
53 'time_min',
54 'stationid',
55 'pos_longitude',
56 'pos_latitude',
57 'speed',
58 'gust',
59 'direction',
60 'atmpressure',
61 'airtemp',
62 'dewpoint',
63 'visibility',
64 'watertemp',
65 'reserved',
66 )
67
68 fieldListPostgres = (
69 'time_month',
70 'time_day',
71 'time_hour',
72 'time_min',
73 'stationid',
74 'pos',
75 'speed',
76 'gust',
77 'direction',
78 'atmpressure',
79 'airtemp',
80 'dewpoint',
81 'visibility',
82 'watertemp',
83 'reserved',
84 )
85
86 toPgFields = {
87 'pos_longitude':'pos',
88 'pos_latitude':'pos',
89 }
90 '''
91 Go to the Postgis field names from the straight field name
92 '''
93
94 fromPgFields = {
95 'pos':('pos_longitude','pos_latitude',),
96 }
97 '''
98 Go from the Postgis field names to the straight field name
99 '''
100
101 pgTypes = {
102 'pos':'POINT',
103 }
104 '''
105 Lookup table for each postgis field name to get its type.
106 '''
107
108 -def encode(params, validate=False):
109 '''Create a sls_weatherreport binary message payload to pack into an AIS Msg sls_weatherreport.
110
111 Fields in params:
112 - time_month(uint): Time tag of measurement month 1..12
113 - time_day(uint): Time tag of measurement day of the month 1..31
114 - time_hour(uint): Time tag of measurement UTC hours 0..23
115 - time_min(uint): Time tag of measurement minutes
116 - stationid(aisstr6): Character identifier of the station
117 - pos_longitude(decimal): Location of measurement East West location
118 - pos_latitude(decimal): Location of measurement North South location
119 - speed(udecimal): Average wind speed
120 - gust(udecimal): Wind gust
121 - direction(uint): Wind direction
122 - atmpressure(udecimal): Atmospheric pressure
123 - airtemp(decimal): Air temperature
124 - dewpoint(decimal): Dew Point
125 - visibility(udecimal): Visibility
126 - watertemp(decimal): Water Temperature
127 - reserved(uint): Reserved bits for future use (field automatically set to "0")
128 @param params: Dictionary of field names/values. Throws a ValueError exception if required is missing
129 @param validate: Set to true to cause checking to occur. Runs slower. FIX: not implemented.
130 @rtype: BitVector
131 @return: encoded binary message (for binary messages, this needs to be wrapped in a msg 8
132 @note: The returned bits may not be 6 bit aligned. It is up to you to pad out the bits.
133 '''
134
135 bvList = []
136 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['time_month']),4))
137 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['time_day']),5))
138 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['time_hour']),5))
139 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['time_min']),6))
140 if 'stationid' in params:
141 bvList.append(aisstring.encode(params['stationid'],42))
142 else:
143 bvList.append(aisstring.encode('@@@@@@@',42))
144 if 'pos_longitude' in params:
145 bvList.append(binary.bvFromSignedInt(int(Decimal(params['pos_longitude'])*Decimal('60000')),25))
146 else:
147 bvList.append(binary.bvFromSignedInt(10860000,25))
148 if 'pos_latitude' in params:
149 bvList.append(binary.bvFromSignedInt(int(Decimal(params['pos_latitude'])*Decimal('60000')),24))
150 else:
151 bvList.append(binary.bvFromSignedInt(5460000,24))
152 if 'speed' in params:
153 bvList.append(binary.setBitVectorSize(BitVector(intVal=int((Decimal(params['speed'])*Decimal('10')))),10))
154 else:
155 bvList.append(binary.setBitVectorSize(BitVector(intVal=int(1023)),10))
156 if 'gust' in params:
157 bvList.append(binary.setBitVectorSize(BitVector(intVal=int((Decimal(params['gust'])*Decimal('10')))),10))
158 else:
159 bvList.append(binary.setBitVectorSize(BitVector(intVal=int(1023)),10))
160 if 'direction' in params:
161 bvList.append(binary.setBitVectorSize(BitVector(intVal=params['direction']),9))
162 else:
163 bvList.append(binary.setBitVectorSize(BitVector(intVal=511),9))
164 if 'atmpressure' in params:
165 bvList.append(binary.setBitVectorSize(BitVector(intVal=int((Decimal(params['atmpressure'])*Decimal('10')))),14))
166 else:
167 bvList.append(binary.setBitVectorSize(BitVector(intVal=int(163830)),14))
168 if 'airtemp' in params:
169 bvList.append(binary.bvFromSignedInt(int(Decimal(params['airtemp'])*Decimal('10')),10))
170 else:
171 bvList.append(binary.bvFromSignedInt(-512,10))
172 if 'dewpoint' in params:
173 bvList.append(binary.bvFromSignedInt(int(Decimal(params['dewpoint'])*Decimal('10')),10))
174 else:
175 bvList.append(binary.bvFromSignedInt(-512,10))
176 if 'visibility' in params:
177 bvList.append(binary.setBitVectorSize(BitVector(intVal=int((Decimal(params['visibility'])*Decimal('10')))),8))
178 else:
179 bvList.append(binary.setBitVectorSize(BitVector(intVal=int(255)),8))
180 if 'watertemp' in params:
181 bvList.append(binary.bvFromSignedInt(int(Decimal(params['watertemp'])*Decimal('10')),10))
182 else:
183 bvList.append(binary.bvFromSignedInt(-512,10))
184 bvList.append(binary.setBitVectorSize(BitVector(intVal=0),4))
185
186 return binary.joinBV(bvList)
187
188 -def decode(bv, validate=False):
189 '''Unpack a sls_weatherreport message
190
191 Fields in params:
192 - time_month(uint): Time tag of measurement month 1..12
193 - time_day(uint): Time tag of measurement day of the month 1..31
194 - time_hour(uint): Time tag of measurement UTC hours 0..23
195 - time_min(uint): Time tag of measurement minutes
196 - stationid(aisstr6): Character identifier of the station
197 - pos_longitude(decimal): Location of measurement East West location
198 - pos_latitude(decimal): Location of measurement North South location
199 - speed(udecimal): Average wind speed
200 - gust(udecimal): Wind gust
201 - direction(uint): Wind direction
202 - atmpressure(udecimal): Atmospheric pressure
203 - airtemp(decimal): Air temperature
204 - dewpoint(decimal): Dew Point
205 - visibility(udecimal): Visibility
206 - watertemp(decimal): Water Temperature
207 - reserved(uint): Reserved bits for future use (field automatically set to "0")
208 @type bv: BitVector
209 @param bv: Bits defining a message
210 @param validate: Set to true to cause checking to occur. Runs slower. FIX: not implemented.
211 @rtype: dict
212 @return: params
213 '''
214
215
216
217
218 r = {}
219 r['time_month']=int(bv[0:4])
220 r['time_day']=int(bv[4:9])
221 r['time_hour']=int(bv[9:14])
222 r['time_min']=int(bv[14:20])
223 r['stationid']=aisstring.decode(bv[20:62])
224 r['pos_longitude']=Decimal(binary.signedIntFromBV(bv[62:87]))/Decimal('60000')
225 r['pos_latitude']=Decimal(binary.signedIntFromBV(bv[87:111]))/Decimal('60000')
226 r['speed']=Decimal(int(bv[111:121]))/Decimal('10')
227 r['gust']=Decimal(int(bv[121:131]))/Decimal('10')
228 r['direction']=int(bv[131:140])
229 r['atmpressure']=Decimal(int(bv[140:154]))/Decimal('10')
230 r['airtemp']=Decimal(binary.signedIntFromBV(bv[154:164]))/Decimal('10')
231 r['dewpoint']=Decimal(binary.signedIntFromBV(bv[164:174]))/Decimal('10')
232 r['visibility']=Decimal(int(bv[174:182]))/Decimal('10')
233 r['watertemp']=Decimal(binary.signedIntFromBV(bv[182:192]))/Decimal('10')
234 r['reserved']=0
235 return r
236
239
242
245
247 return int(bv[14:20])
248
251
254
257
259 return Decimal(int(bv[111:121]))/Decimal('10')
260
262 return Decimal(int(bv[121:131]))/Decimal('10')
263
265 return int(bv[131:140])
266
268 return Decimal(int(bv[140:154]))/Decimal('10')
269
272
275
277 return Decimal(int(bv[174:182]))/Decimal('10')
278
281
284
285
287 out.write("<h3>sls_weatherreport<h3>\n")
288 out.write("<table border=\"1\">\n")
289 out.write("<tr bgcolor=\"orange\">\n")
290 out.write("<th align=\"left\">Field Name</th>\n")
291 out.write("<th align=\"left\">Type</th>\n")
292 out.write("<th align=\"left\">Value</th>\n")
293 out.write("<th align=\"left\">Value in Lookup Table</th>\n")
294 out.write("<th align=\"left\">Units</th>\n")
295 out.write("\n")
296 out.write("<tr>\n")
297 out.write("<td>time_month</td>\n")
298 out.write("<td>uint</td>\n")
299 if 'time_month' in params:
300 out.write(" <td>"+str(params['time_month'])+"</td>\n")
301 out.write(" <td>"+str(params['time_month'])+"</td>\n")
302 out.write("</tr>\n")
303 out.write("\n")
304 out.write("<tr>\n")
305 out.write("<td>time_day</td>\n")
306 out.write("<td>uint</td>\n")
307 if 'time_day' in params:
308 out.write(" <td>"+str(params['time_day'])+"</td>\n")
309 out.write(" <td>"+str(params['time_day'])+"</td>\n")
310 out.write("</tr>\n")
311 out.write("\n")
312 out.write("<tr>\n")
313 out.write("<td>time_hour</td>\n")
314 out.write("<td>uint</td>\n")
315 if 'time_hour' in params:
316 out.write(" <td>"+str(params['time_hour'])+"</td>\n")
317 out.write(" <td>"+str(params['time_hour'])+"</td>\n")
318 out.write("</tr>\n")
319 out.write("\n")
320 out.write("<tr>\n")
321 out.write("<td>time_min</td>\n")
322 out.write("<td>uint</td>\n")
323 if 'time_min' in params:
324 out.write(" <td>"+str(params['time_min'])+"</td>\n")
325 out.write(" <td>"+str(params['time_min'])+"</td>\n")
326 out.write("</tr>\n")
327 out.write("\n")
328 out.write("<tr>\n")
329 out.write("<td>stationid</td>\n")
330 out.write("<td>aisstr6</td>\n")
331 if 'stationid' in params:
332 out.write(" <td>"+str(params['stationid'])+"</td>\n")
333 out.write(" <td>"+str(params['stationid'])+"</td>\n")
334 out.write("</tr>\n")
335 out.write("\n")
336 out.write("<tr>\n")
337 out.write("<td>pos_longitude</td>\n")
338 out.write("<td>decimal</td>\n")
339 if 'pos_longitude' in params:
340 out.write(" <td>"+str(params['pos_longitude'])+"</td>\n")
341 out.write(" <td>"+str(params['pos_longitude'])+"</td>\n")
342 out.write("<td>degrees</td>\n")
343 out.write("</tr>\n")
344 out.write("\n")
345 out.write("<tr>\n")
346 out.write("<td>pos_latitude</td>\n")
347 out.write("<td>decimal</td>\n")
348 if 'pos_latitude' in params:
349 out.write(" <td>"+str(params['pos_latitude'])+"</td>\n")
350 out.write(" <td>"+str(params['pos_latitude'])+"</td>\n")
351 out.write("<td>degrees</td>\n")
352 out.write("</tr>\n")
353 out.write("\n")
354 out.write("<tr>\n")
355 out.write("<td>speed</td>\n")
356 out.write("<td>udecimal</td>\n")
357 if 'speed' in params:
358 out.write(" <td>"+str(params['speed'])+"</td>\n")
359 if str(params['speed']) in speedDecodeLut:
360 out.write("<td>"+speedDecodeLut[str(params['speed'])]+"</td>")
361 else:
362 out.write("<td><i>Missing LUT entry</i></td>")
363 out.write("<td>kts</td>\n")
364 out.write("</tr>\n")
365 out.write("\n")
366 out.write("<tr>\n")
367 out.write("<td>gust</td>\n")
368 out.write("<td>udecimal</td>\n")
369 if 'gust' in params:
370 out.write(" <td>"+str(params['gust'])+"</td>\n")
371 if str(params['gust']) in gustDecodeLut:
372 out.write("<td>"+gustDecodeLut[str(params['gust'])]+"</td>")
373 else:
374 out.write("<td><i>Missing LUT entry</i></td>")
375 out.write("<td>kts</td>\n")
376 out.write("</tr>\n")
377 out.write("\n")
378 out.write("<tr>\n")
379 out.write("<td>direction</td>\n")
380 out.write("<td>uint</td>\n")
381 if 'direction' in params:
382 out.write(" <td>"+str(params['direction'])+"</td>\n")
383 out.write(" <td>"+str(params['direction'])+"</td>\n")
384 out.write("<td>degrees</td>\n")
385 out.write("</tr>\n")
386 out.write("\n")
387 out.write("<tr>\n")
388 out.write("<td>atmpressure</td>\n")
389 out.write("<td>udecimal</td>\n")
390 if 'atmpressure' in params:
391 out.write(" <td>"+str(params['atmpressure'])+"</td>\n")
392 out.write(" <td>"+str(params['atmpressure'])+"</td>\n")
393 out.write("<td>millibars</td>\n")
394 out.write("</tr>\n")
395 out.write("\n")
396 out.write("<tr>\n")
397 out.write("<td>airtemp</td>\n")
398 out.write("<td>decimal</td>\n")
399 if 'airtemp' in params:
400 out.write(" <td>"+str(params['airtemp'])+"</td>\n")
401 if str(params['airtemp']) in airtempDecodeLut:
402 out.write("<td>"+airtempDecodeLut[str(params['airtemp'])]+"</td>")
403 else:
404 out.write("<td><i>Missing LUT entry</i></td>")
405 out.write("<td>Celsius</td>\n")
406 out.write("</tr>\n")
407 out.write("\n")
408 out.write("<tr>\n")
409 out.write("<td>dewpoint</td>\n")
410 out.write("<td>decimal</td>\n")
411 if 'dewpoint' in params:
412 out.write(" <td>"+str(params['dewpoint'])+"</td>\n")
413 if str(params['dewpoint']) in dewpointDecodeLut:
414 out.write("<td>"+dewpointDecodeLut[str(params['dewpoint'])]+"</td>")
415 else:
416 out.write("<td><i>Missing LUT entry</i></td>")
417 out.write("<td>Celsius</td>\n")
418 out.write("</tr>\n")
419 out.write("\n")
420 out.write("<tr>\n")
421 out.write("<td>visibility</td>\n")
422 out.write("<td>udecimal</td>\n")
423 if 'visibility' in params:
424 out.write(" <td>"+str(params['visibility'])+"</td>\n")
425 if str(params['visibility']) in visibilityDecodeLut:
426 out.write("<td>"+visibilityDecodeLut[str(params['visibility'])]+"</td>")
427 else:
428 out.write("<td><i>Missing LUT entry</i></td>")
429 out.write("<td>km</td>\n")
430 out.write("</tr>\n")
431 out.write("\n")
432 out.write("<tr>\n")
433 out.write("<td>watertemp</td>\n")
434 out.write("<td>decimal</td>\n")
435 if 'watertemp' in params:
436 out.write(" <td>"+str(params['watertemp'])+"</td>\n")
437 if str(params['watertemp']) in watertempDecodeLut:
438 out.write("<td>"+watertempDecodeLut[str(params['watertemp'])]+"</td>")
439 else:
440 out.write("<td><i>Missing LUT entry</i></td>")
441 out.write("<td>Celsius</td>\n")
442 out.write("</tr>\n")
443 out.write("\n")
444 out.write("<tr>\n")
445 out.write("<td>reserved</td>\n")
446 out.write("<td>uint</td>\n")
447 if 'reserved' in params:
448 out.write(" <td>"+str(params['reserved'])+"</td>\n")
449 out.write(" <td>"+str(params['reserved'])+"</td>\n")
450 out.write("</tr>\n")
451 out.write("</table>\n")
452
453
455 '''KML (Keyhole Markup Language) for Google Earth, but without the header/footer'''
456 out.write("\ <Placemark>\n")
457 out.write("\t <name>"+str(params['stationid'])+"</name>\n")
458 out.write("\t\t<description>\n")
459 import StringIO
460 buf = StringIO.StringIO()
461 printHtml(params,buf)
462 import cgi
463 out.write(cgi.escape(buf.getvalue()))
464 out.write("\t\t</description>\n")
465 out.write("\t\t<styleUrl>#m_ylw-pushpin_copy0</styleUrl>\n")
466 out.write("\t\t<Point>\n")
467 out.write("\t\t\t<coordinates>")
468 out.write(str(params['pos_longitude']))
469 out.write(',')
470 out.write(str(params['pos_latitude']))
471 out.write(",0</coordinates>\n")
472 out.write("\t\t</Point>\n")
473 out.write("\t</Placemark>\n")
474
475 -def printFields(params, out=sys.stdout, format='std', fieldList=None, dbType='postgres'):
476 '''Print a sls_weatherreport message to stdout.
477
478 Fields in params:
479 - time_month(uint): Time tag of measurement month 1..12
480 - time_day(uint): Time tag of measurement day of the month 1..31
481 - time_hour(uint): Time tag of measurement UTC hours 0..23
482 - time_min(uint): Time tag of measurement minutes
483 - stationid(aisstr6): Character identifier of the station
484 - pos_longitude(decimal): Location of measurement East West location
485 - pos_latitude(decimal): Location of measurement North South location
486 - speed(udecimal): Average wind speed
487 - gust(udecimal): Wind gust
488 - direction(uint): Wind direction
489 - atmpressure(udecimal): Atmospheric pressure
490 - airtemp(decimal): Air temperature
491 - dewpoint(decimal): Dew Point
492 - visibility(udecimal): Visibility
493 - watertemp(decimal): Water Temperature
494 - reserved(uint): Reserved bits for future use (field automatically set to "0")
495 @param params: Dictionary of field names/values.
496 @param out: File like object to write to
497 @rtype: stdout
498 @return: text to out
499 '''
500
501 if 'std'==format:
502 out.write("sls_weatherreport:\n")
503 if 'time_month' in params: out.write(" time_month: "+str(params['time_month'])+"\n")
504 if 'time_day' in params: out.write(" time_day: "+str(params['time_day'])+"\n")
505 if 'time_hour' in params: out.write(" time_hour: "+str(params['time_hour'])+"\n")
506 if 'time_min' in params: out.write(" time_min: "+str(params['time_min'])+"\n")
507 if 'stationid' in params: out.write(" stationid: "+str(params['stationid'])+"\n")
508 if 'pos_longitude' in params: out.write(" pos_longitude: "+str(params['pos_longitude'])+"\n")
509 if 'pos_latitude' in params: out.write(" pos_latitude: "+str(params['pos_latitude'])+"\n")
510 if 'speed' in params: out.write(" speed: "+str(params['speed'])+"\n")
511 if 'gust' in params: out.write(" gust: "+str(params['gust'])+"\n")
512 if 'direction' in params: out.write(" direction: "+str(params['direction'])+"\n")
513 if 'atmpressure' in params: out.write(" atmpressure: "+str(params['atmpressure'])+"\n")
514 if 'airtemp' in params: out.write(" airtemp: "+str(params['airtemp'])+"\n")
515 if 'dewpoint' in params: out.write(" dewpoint: "+str(params['dewpoint'])+"\n")
516 if 'visibility' in params: out.write(" visibility: "+str(params['visibility'])+"\n")
517 if 'watertemp' in params: out.write(" watertemp: "+str(params['watertemp'])+"\n")
518 if 'reserved' in params: out.write(" reserved: "+str(params['reserved'])+"\n")
519 elif 'csv'==format:
520 if None == options.fieldList:
521 options.fieldList = fieldList
522 needComma = False;
523 for field in fieldList:
524 if needComma: out.write(',')
525 needComma = True
526 if field in params:
527 out.write(str(params[field]))
528
529 out.write("\n")
530 elif 'html'==format:
531 printHtml(params,out)
532 elif 'sql'==format:
533 sqlInsertStr(params,out,dbType=dbType)
534 elif 'kml'==format:
535 printKml(params,out)
536 elif 'kml-full'==format:
537 out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
538 out.write("<kml xmlns=\"http://earth.google.com/kml/2.1\">\n")
539 out.write("<Document>\n")
540 out.write(" <name>sls_weatherreport</name>\n")
541 printKml(params,out)
542 out.write("</Document>\n")
543 out.write("</kml>\n")
544 else:
545 print "ERROR: unknown format:",format
546 assert False
547
548 return
549
550 speedEncodeLut = {
551 '102.2 kts or greater':'102.2',
552 }
553
554 speedDecodeLut = {
555 '102.2':'102.2 kts or greater',
556 }
557
558 gustEncodeLut = {
559 '102.2 kts or greater':'102.2',
560 }
561
562 gustDecodeLut = {
563 '102.2':'102.2 kts or greater',
564 }
565
566 airtempEncodeLut = {
567 '-51.1 degrees C or lower':'-51.1',
568 '51.1 degrees C or greater':'51.1',
569 }
570
571 airtempDecodeLut = {
572 '-51.1':'-51.1 degrees C or lower',
573 '51.1':'51.1 degrees C or greater',
574 }
575
576 dewpointEncodeLut = {
577 '-51.1 degrees C or lower':'-51.1',
578 '51.1 degrees C or greater':'51.1',
579 }
580
581 dewpointDecodeLut = {
582 '-51.1':'-51.1 degrees C or lower',
583 '51.1':'51.1 degrees C or greater',
584 }
585
586 visibilityEncodeLut = {
587 '25.4 km or greater':'25.4',
588 }
589
590 visibilityDecodeLut = {
591 '25.4':'25.4 km or greater',
592 }
593
594 watertempEncodeLut = {
595 '-51.1 degrees C or lower':'-51.1',
596 '51.1 degrees C or greater':'51.1',
597 }
598
599 watertempDecodeLut = {
600 '-51.1':'-51.1 degrees C or lower',
601 '51.1':'51.1 degrees C or greater',
602 }
603
604
605
606
607
608 -def sqlCreateStr(outfile=sys.stdout, fields=None, extraFields=None
609 ,addCoastGuardFields=True
610 ,dbType='postgres'
611 ):
612 '''
613 Return the SQL CREATE command for this message type
614 @param outfile: file like object to print to.
615 @param fields: which fields to put in the create. Defaults to all.
616 @param extraFields: A sequence of tuples containing (name,sql type) for additional fields
617 @param addCoastGuardFields: Add the extra fields that come after the NMEA check some from the USCG N-AIS format
618 @param dbType: Which flavor of database we are using so that the create is tailored ('sqlite' or 'postgres')
619 @type addCoastGuardFields: bool
620 @return: sql create string
621 @rtype: str
622
623 @see: sqlCreate
624 '''
625
626 outfile.write(str(sqlCreate(fields,extraFields,addCoastGuardFields,dbType=dbType)))
627
628 -def sqlCreate(fields=None, extraFields=None, addCoastGuardFields=True, dbType='postgres'):
629 '''
630 Return the sqlhelp object to create the table.
631
632 @param fields: which fields to put in the create. Defaults to all.
633 @param extraFields: A sequence of tuples containing (name,sql type) for additional fields
634 @param addCoastGuardFields: Add the extra fields that come after the NMEA check some from the USCG N-AIS format
635 @type addCoastGuardFields: bool
636 @param dbType: Which flavor of database we are using so that the create is tailored ('sqlite' or 'postgres')
637 @return: An object that can be used to generate a return
638 @rtype: sqlhelp.create
639 '''
640 if None == fields: fields = fieldList
641 import sqlhelp
642 c = sqlhelp.create('sls_weatherreport',dbType=dbType)
643 c.addPrimaryKey()
644 if 'time_month' in fields: c.addInt ('time_month')
645 if 'time_day' in fields: c.addInt ('time_day')
646 if 'time_hour' in fields: c.addInt ('time_hour')
647 if 'time_min' in fields: c.addInt ('time_min')
648 if 'stationid' in fields: c.addVarChar('stationid',7)
649 if dbType != 'postgres':
650 if 'pos_longitude' in fields: c.addDecimal('pos_longitude',7,4)
651 if dbType != 'postgres':
652 if 'pos_latitude' in fields: c.addDecimal('pos_latitude',7,4)
653 if 'speed' in fields: c.addDecimal('speed',4,1)
654 if 'gust' in fields: c.addDecimal('gust',4,1)
655 if 'direction' in fields: c.addInt ('direction')
656 if 'atmpressure' in fields: c.addDecimal('atmpressure',5,1)
657 if 'airtemp' in fields: c.addDecimal('airtemp',4,1)
658 if 'dewpoint' in fields: c.addDecimal('dewpoint',4,1)
659 if 'visibility' in fields: c.addDecimal('visibility',3,1)
660 if 'watertemp' in fields: c.addDecimal('watertemp',4,1)
661 if 'reserved' in fields: c.addInt ('reserved')
662
663 if addCoastGuardFields:
664
665
666
667
668
669 c.addVarChar('cg_r',15)
670 c.addInt('cg_sec')
671
672 c.addTimestamp('cg_timestamp')
673
674 if dbType == 'postgres':
675 c.addPostGIS('pos','POINT',2);
676
677 return c
678
679 -def sqlInsertStr(params, outfile=sys.stdout, extraParams=None, dbType='postgres'):
680 '''
681 Return the SQL INSERT command for this message type
682 @param params: dictionary of values keyed by field name
683 @param outfile: file like object to print to.
684 @param extraParams: A sequence of tuples containing (name,sql type) for additional fields
685 @return: sql create string
686 @rtype: str
687
688 @see: sqlCreate
689 '''
690 outfile.write(str(sqlInsert(params,extraParams,dbType=dbType)))
691
692
693 -def sqlInsert(params,extraParams=None,dbType='postgres'):
694 '''
695 Give the SQL INSERT statement
696 @param params: dict keyed by field name of values
697 @param extraParams: any extra fields that you have created beyond the normal ais message fields
698 @rtype: sqlhelp.insert
699 @return: insert class instance
700 @todo: allow optional type checking of params?
701 @warning: this will take invalid keys happily and do what???
702 '''
703 import sqlhelp
704 i = sqlhelp.insert('sls_weatherreport',dbType=dbType)
705
706 if dbType=='postgres':
707 finished = []
708 for key in params:
709 if key in finished:
710 continue
711
712 if key not in toPgFields and key not in fromPgFields:
713 if type(params[key])==Decimal: i.add(key,float(params[key]))
714 else: i.add(key,params[key])
715 else:
716 if key in fromPgFields:
717 val = params[key]
718
719 i.addPostGIS(key,val)
720 finished.append(key)
721 else:
722
723 pgName = toPgFields[key]
724
725 valStr=pgTypes[pgName]+'('
726 vals = []
727 for nonPgKey in fromPgFields[pgName]:
728 vals.append(str(params[nonPgKey]))
729 finished.append(nonPgKey)
730 valStr+=' '.join(vals)+')'
731 i.addPostGIS(pgName,valStr)
732 else:
733 for key in params:
734 if type(params[key])==Decimal: i.add(key,float(params[key]))
735 else: i.add(key,params[key])
736
737 if None != extraParams:
738 for key in extraParams:
739 i.add(key,extraParams[key])
740
741 return i
742
743
744
745
746
749 '''
750 Return the LaTeX definition table for this message type
751 @param outfile: file like object to print to.
752 @type outfile: file obj
753 @return: LaTeX table string via the outfile
754 @rtype: str
755
756 '''
757 o = outfile
758
759 o.write('''
760 \\begin{table}%[htb]
761 \\centering
762 \\begin{tabular}{|l|c|l|}
763 \\hline
764 Parameter & Number of bits & Description
765 \\\\ \\hline\\hline
766 time\_month & 4 & Time tag of measurement month 1..12 \\\\ \hline
767 time\_day & 5 & Time tag of measurement day of the month 1..31 \\\\ \hline
768 time\_hour & 5 & Time tag of measurement UTC hours 0..23 \\\\ \hline
769 time\_min & 6 & Time tag of measurement minutes \\\\ \hline
770 stationid & 42 & Character identifier of the station \\\\ \hline
771 pos\_longitude & 25 & Location of measurement East West location \\\\ \hline
772 pos\_latitude & 24 & Location of measurement North South location \\\\ \hline
773 speed & 10 & Average wind speed \\\\ \hline
774 gust & 10 & Wind gust \\\\ \hline
775 direction & 9 & Wind direction \\\\ \hline
776 atmpressure & 14 & Atmospheric pressure \\\\ \hline
777 airtemp & 10 & Air temperature \\\\ \hline
778 dewpoint & 10 & Dew Point \\\\ \hline
779 visibility & 8 & Visibility \\\\ \hline
780 watertemp & 10 & Water Temperature \\\\ \hline
781 reserved & 4 & Reserved bits for future use\\\\ \\hline \\hline
782 Total bits & 196 & Appears to take 2 slots with 228 pad bits to fill the last slot \\\\ \\hline
783 \\end{tabular}
784 \\caption{AIS message number 8: St Lawrance Seaway wind information}
785 \\label{tab:sls_weatherreport}
786 \\end{table}
787 ''')
788
789
790
791
792
793 import unittest
795 '''Return a params file base on the testvalue tags.
796 @rtype: dict
797 @return: params based on testvalue tags
798 '''
799 params = {}
800 params['time_month'] = 2
801 params['time_day'] = 28
802 params['time_hour'] = 23
803 params['time_min'] = 45
804 params['stationid'] = 'A345678'
805 params['pos_longitude'] = Decimal('-122.16328')
806 params['pos_latitude'] = Decimal('37.42446')
807 params['speed'] = Decimal('0.7')
808 params['gust'] = Decimal('0.7')
809 params['direction'] = 41
810 params['atmpressure'] = Decimal('0.7')
811 params['airtemp'] = Decimal('-8')
812 params['dewpoint'] = Decimal('-7')
813 params['visibility'] = Decimal('12.3')
814 params['watertemp'] = Decimal('-0.2')
815 params['reserved'] = 0
816
817 return params
818
820 '''Use testvalue tag text from each type to build test case the sls_weatherreport message'''
822
823 params = testParams()
824 bits = encode(params)
825 r = decode(bits)
826
827
828 self.failUnlessEqual(r['time_month'],params['time_month'])
829 self.failUnlessEqual(r['time_day'],params['time_day'])
830 self.failUnlessEqual(r['time_hour'],params['time_hour'])
831 self.failUnlessEqual(r['time_min'],params['time_min'])
832 self.failUnlessEqual(r['stationid'],params['stationid'])
833 self.failUnlessAlmostEqual(r['pos_longitude'],params['pos_longitude'],4)
834 self.failUnlessAlmostEqual(r['pos_latitude'],params['pos_latitude'],4)
835 self.failUnlessAlmostEqual(r['speed'],params['speed'],1)
836 self.failUnlessAlmostEqual(r['gust'],params['gust'],1)
837 self.failUnlessEqual(r['direction'],params['direction'])
838 self.failUnlessAlmostEqual(r['atmpressure'],params['atmpressure'],1)
839 self.failUnlessAlmostEqual(r['airtemp'],params['airtemp'],1)
840 self.failUnlessAlmostEqual(r['dewpoint'],params['dewpoint'],1)
841 self.failUnlessAlmostEqual(r['visibility'],params['visibility'],1)
842 self.failUnlessAlmostEqual(r['watertemp'],params['watertemp'],1)
843 self.failUnlessEqual(r['reserved'],params['reserved'])
844
846 parser.add_option('-d','--decode',dest='doDecode',default=False,action='store_true',
847 help='decode a "sls_weatherreport" AIS message')
848 parser.add_option('-e','--encode',dest='doEncode',default=False,action='store_true',
849 help='encode a "sls_weatherreport" AIS message')
850 parser.add_option('--time_month-field', dest='time_monthField',metavar='uint',type='int'
851 ,help='Field parameter value [default: %default]')
852 parser.add_option('--time_day-field', dest='time_dayField',metavar='uint',type='int'
853 ,help='Field parameter value [default: %default]')
854 parser.add_option('--time_hour-field', dest='time_hourField',metavar='uint',type='int'
855 ,help='Field parameter value [default: %default]')
856 parser.add_option('--time_min-field', dest='time_minField',metavar='uint',type='int'
857 ,help='Field parameter value [default: %default]')
858 parser.add_option('--stationid-field', dest='stationidField',default='@@@@@@@',metavar='aisstr6',type='string'
859 ,help='Field parameter value [default: %default]')
860 parser.add_option('--pos_longitude-field', dest='pos_longitudeField',default=Decimal('181'),metavar='decimal',type='string'
861 ,help='Field parameter value [default: %default]')
862 parser.add_option('--pos_latitude-field', dest='pos_latitudeField',default=Decimal('91'),metavar='decimal',type='string'
863 ,help='Field parameter value [default: %default]')
864 parser.add_option('--speed-field', dest='speedField',default=Decimal('102.3'),metavar='udecimal',type='string'
865 ,help='Field parameter value [default: %default]')
866 parser.add_option('--gust-field', dest='gustField',default=Decimal('102.3'),metavar='udecimal',type='string'
867 ,help='Field parameter value [default: %default]')
868 parser.add_option('--direction-field', dest='directionField',default=511,metavar='uint',type='int'
869 ,help='Field parameter value [default: %default]')
870 parser.add_option('--atmpressure-field', dest='atmpressureField',default=Decimal('16383'),metavar='udecimal',type='string'
871 ,help='Field parameter value [default: %default]')
872 parser.add_option('--airtemp-field', dest='airtempField',default=Decimal('-51.2'),metavar='decimal',type='string'
873 ,help='Field parameter value [default: %default]')
874 parser.add_option('--dewpoint-field', dest='dewpointField',default=Decimal('-51.2'),metavar='decimal',type='string'
875 ,help='Field parameter value [default: %default]')
876 parser.add_option('--visibility-field', dest='visibilityField',default=Decimal('25.5'),metavar='udecimal',type='string'
877 ,help='Field parameter value [default: %default]')
878 parser.add_option('--watertemp-field', dest='watertempField',default=Decimal('-51.2'),metavar='decimal',type='string'
879 ,help='Field parameter value [default: %default]')
880
881
882 if __name__=='__main__':
883
884 from optparse import OptionParser
885 parser = OptionParser(usage="%prog [options]",
886 version="%prog "+__version__)
887
888 parser.add_option('--doc-test',dest='doctest',default=False,action='store_true',
889 help='run the documentation tests')
890 parser.add_option('--unit-test',dest='unittest',default=False,action='store_true',
891 help='run the unit tests')
892 parser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true',
893 help='Make the test output verbose')
894
895
896
897 typeChoices = ('binary','nmeapayload','nmea')
898 parser.add_option('-t','--type',choices=typeChoices,type='choice',dest='ioType'
899 ,default='nmeapayload'
900 ,help='What kind of string to write for encoding ('+', '.join(typeChoices)+') [default: %default]')
901
902
903 outputChoices = ('std','html','csv','sql' , 'kml','kml-full')
904 parser.add_option('-T','--output-type',choices=outputChoices,type='choice',dest='outputType'
905 ,default='std'
906 ,help='What kind of string to output ('+', '.join(outputChoices)+') [default: %default]')
907
908 parser.add_option('-o','--output',dest='outputFileName',default=None,
909 help='Name of the python file to write [default: stdout]')
910
911 parser.add_option('-f','--fields',dest='fieldList',default=None, action='append',
912 choices=fieldList,
913 help='Which fields to include in the output. Currently only for csv output [default: all]')
914
915 parser.add_option('-p','--print-csv-field-list',dest='printCsvfieldList',default=False,action='store_true',
916 help='Print the field name for csv')
917
918 parser.add_option('-c','--sql-create',dest='sqlCreate',default=False,action='store_true',
919 help='Print out an sql create command for the table.')
920
921 parser.add_option('--latex-table',dest='latexDefinitionTable',default=False,action='store_true',
922 help='Print a LaTeX table of the type')
923
924 dbChoices = ('sqlite','postgres')
925 parser.add_option('-D','--db-type',dest='dbType',default='postgres'
926 ,choices=dbChoices,type='choice'
927 ,help='What kind of database ('+', '.join(dbChoices)+') [default: %default]')
928
929 addMsgOptions(parser)
930
931 (options,args) = parser.parse_args()
932 success=True
933
934 if options.doctest:
935 import os; print os.path.basename(sys.argv[0]), 'doctests ...',
936 sys.argv= [sys.argv[0]]
937 if options.verbose: sys.argv.append('-v')
938 import doctest
939 numfail,numtests=doctest.testmod()
940 if numfail==0: print 'ok'
941 else:
942 print 'FAILED'
943 success=False
944
945 if not success: sys.exit('Something Failed')
946 del success
947
948 if options.unittest:
949 sys.argv = [sys.argv[0]]
950 if options.verbose: sys.argv.append('-v')
951 unittest.main()
952
953 outfile = sys.stdout
954 if None!=options.outputFileName:
955 outfile = file(options.outputFileName,'w')
956
957
958 if options.doEncode:
959
960 if None==options.time_monthField: parser.error("missing value for time_monthField")
961 if None==options.time_dayField: parser.error("missing value for time_dayField")
962 if None==options.time_hourField: parser.error("missing value for time_hourField")
963 if None==options.time_minField: parser.error("missing value for time_minField")
964 if None==options.stationidField: parser.error("missing value for stationidField")
965 if None==options.pos_longitudeField: parser.error("missing value for pos_longitudeField")
966 if None==options.pos_latitudeField: parser.error("missing value for pos_latitudeField")
967 if None==options.speedField: parser.error("missing value for speedField")
968 if None==options.gustField: parser.error("missing value for gustField")
969 if None==options.directionField: parser.error("missing value for directionField")
970 if None==options.atmpressureField: parser.error("missing value for atmpressureField")
971 if None==options.airtempField: parser.error("missing value for airtempField")
972 if None==options.dewpointField: parser.error("missing value for dewpointField")
973 if None==options.visibilityField: parser.error("missing value for visibilityField")
974 if None==options.watertempField: parser.error("missing value for watertempField")
975 msgDict={
976 'time_month': options.time_monthField,
977 'time_day': options.time_dayField,
978 'time_hour': options.time_hourField,
979 'time_min': options.time_minField,
980 'stationid': options.stationidField,
981 'pos_longitude': options.pos_longitudeField,
982 'pos_latitude': options.pos_latitudeField,
983 'speed': options.speedField,
984 'gust': options.gustField,
985 'direction': options.directionField,
986 'atmpressure': options.atmpressureField,
987 'airtemp': options.airtempField,
988 'dewpoint': options.dewpointField,
989 'visibility': options.visibilityField,
990 'watertemp': options.watertempField,
991 'reserved': '0',
992 }
993
994 bits = encode(msgDict)
995 if 'binary'==options.ioType: print str(bits)
996 elif 'nmeapayload'==options.ioType:
997
998 print "bitLen",len(bits)
999 bitLen=len(bits)
1000 if bitLen%6!=0:
1001 bits = bits + BitVector(size=(6 - (bitLen%6)))
1002 print "result:",binary.bitvectoais6(bits)[0]
1003
1004
1005
1006 elif 'nmea'==options.ioType: sys.exit("FIX: need to implement this capability")
1007 else: sys.exit('ERROR: unknown ioType. Help!')
1008
1009
1010 if options.sqlCreate:
1011 sqlCreateStr(outfile,options.fieldList,dbType=options.dbType)
1012
1013 if options.latexDefinitionTable:
1014 latexDefinitionTable(outfile)
1015
1016 if options.printCsvfieldList:
1017
1018 if None == options.fieldList: options.fieldList = fieldList
1019 import StringIO
1020 buf = StringIO.StringIO()
1021 for field in options.fieldList:
1022 buf.write(field+',')
1023 result = buf.getvalue()
1024 if result[-1] == ',': print result[:-1]
1025 else: print result
1026
1027 if options.doDecode:
1028 for msg in args:
1029 bv = None
1030
1031 if msg[0] in ('$','!') and msg[3:6] in ('VDM','VDO'):
1032
1033
1034 bv = binary.ais6tobitvec(msg.split(',')[5])
1035 else:
1036
1037 binaryMsg=True
1038 for c in msg:
1039 if c not in ('0','1'):
1040 binaryMsg=False
1041 break
1042 if binaryMsg:
1043 bv = BitVector(bitstring=msg)
1044 else:
1045 bv = binary.ais6tobitvec(msg)
1046
1047 printFields(decode(bv)
1048 ,out=outfile
1049 ,format=options.outputType
1050 ,fieldList=options.fieldList
1051 ,dbType=options.dbType
1052 )
1053