forked from JeremyGrosser/noaaport
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwmo.py
More file actions
executable file
·43 lines (37 loc) · 1.46 KB
/
wmo.py
File metadata and controls
executable file
·43 lines (37 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/python
import os,sys
ETX = chr(0x03)
SOH = chr(0x01)
class WMO(object):
"""
This class is to be used with python-emwin to format its output messages
in such a way that we can stuff them into ldm's pqing
"""
def __init__(self, outch):
self.count = 0
# Use the write method of our output channel
# this makes it easy to go from stdout to a popened
# file handle to pqing(1)
self.sw = outch.write
# send a string of ETXs to make sure we're not in any products
# and then follow with a proper WMO ending
self.sw(ETX + ETX + ETX + "\r\r\n" + ETX)
def seq(self):
# WMO sequence numbers are from 000 to 999. We format to three digits in the output
# this is a little sillier than returning the ++'d counter since we need
# it to start at 0, have to keep it under 1000, and want it done in one place
r = self.count
self.count += 1
if self.count > 999:
self.count = 0
return r
def emit(self, content):
# from http://www.nws.noaa.gov/tg/head.php
# WMO messges start with SOH, the CRCRLF string, a sequence number, and
# since we're using python-emwin, we need another \r\r\n before the proper
# address header
self.sw(SOH + "\r\r\n%03d\r\r\n" % self.seq())
# and now we emit the content
self.sw(content)
# and finally the footer
self.sw("\r\r\n" + ETX)