Parses RTCM3 messages into their constituent data fields - DF002, DF003, etc. Refer to the RTCM_MSGIDS dictionary in rtcmtypes_core.py for a list of message types currently implemented. Additional message types can be readily added - see Extensibility.
pyrtcm is compatible with Python >=3.10. In the following, python3 & pip refer to the Python 3 executables. You may need to substitute python for python3, depending on your particular environment (on Windows it's generally python).
The recommended way to install the latest version of pyrtcm is with pip:
python3 -m pip install --upgrade pyrtcm
If required, pyrtcm can also be installed into a virtual environment, e.g.:
class pyrtcm.rtcmreader.RTCMReader(stream, **kwargs)
You can create a RTCMReader object by calling the constructor with an active stream object.
The stream object can be any data stream which supports a read(n) -> bytes method (e.g. File or Serial, with
or without a buffer wrapper). pyrtcm implements an internal SocketStream class to allow sockets to be read in the same way as other streams (see example below).
Individual RTCM messages can then be read using the RTCMReader.read() function, which returns both the raw binary data (as bytes) and the parsed data (as a RTCMMessage, via the parse() method). The function is thread-safe in so far as the incoming data stream object is thread-safe. RTCMReader also implements an iterator.
The constructor accepts the following optional keyword arguments:
labelmsm: 1 = use RINEX code eg. "2C" (default), 2 = use Frequency band e.g. "L2" (see e.g. GPS-SIG-MAP). This argument affects the representation of CELLSIG values in RTCM3 MSM (multiple signal message) payloads:
labelmsm=1: CELLSIG_01=2C, CELLSIG_02=2W, CELLSIG_03=5I, etc.
labelmsm=2: CELLSIG_01=L2, CELLSIG_02=L2, CELLSIG_03=L5, etc.
Example A - Serial input, logging any errors and displaying any CELLSIG values in RINEX format:
from serial import Serial
from pyrtcm import RTCMReader, ERR_LOG
with Serial('/dev/tty.usbmodem14101', 9600, timeout=3) as stream:
rtr = RTCMReader(stream, quitonerror=ERR_LOG, labelmsm=1)
raw_data, parsed_data = rtr.read()
if parsed_data is not None:
print(parsed_data)
Example B - File input (using iterator) - this will terminate on error.
from pyrtcm import RTCMReader, ERR_RAISE
with open('rtcmdata.log', 'rb') as stream:
rtr = RTCMReader(stream, quitonerror=ERR_RAISE, labelmsm=1)
for raw_data, parsed_data in rtr:
print(parsed_data)
Example C - Socket input (using iterator), ignoring any errors and displaying any CELLSIG values in Frequency band format:
import socket
from pyrtcm import RTCMReader, ERR_IGNORE
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as stream:
stream.connect(("localhost", 50007))
rtr = RTCMReader(stream, quitonerror=ERR_IGNORE, labelmsm=2)
for raw_data, parsed_data in rtr:
print(parsed_data)
You can parse individual RTCM messages using the static RTCMReader.parse(data) function, which takes a bytes array containing a binary RTCM message and returns a RTCMMessage object.
NB: Once instantiated, an RTCMMessage object is immutable.
The parse() method accepts the following optional keyword arguments:
The RTCMMessage object exposes different public attributes depending on its message type or 'identity'. Attributes are defined as data fields (DF002, DF003, etc.) e.g. the 1097 multiple signal message (MSM) contains the following data fields:
Note that MSM message payloads comprise two discrete repeating groups - a 'SAT' group of size NSat relating to PRN (e.g. PRN_01=007, PRN_02=027, etc.), and a 'CELL' group of size NCell relating to a combination of PRN and Signal (e.g. CELLPRN_01=007, CELLSIG_01=1C, CELLPRN_02=007 CELLSIG_02=2C, CELLPRN_03=027, CELLSIG_03=1C, CELLPRN_04=027, CELLSIG_04=2C, etc.).
Attributes within repeating groups are parsed with a two-digit suffix (DF419_01, DF419_02, etc. See example below for an illustration of how to iterate through grouped attributes).
Helper methods are available to interpret the individual datafields:
from pyrtcm import RTCM_DATA_FIELDS, datadesc
dfname = "DF012"
print(RTCM_DATA_FIELDS[dfname])
print(datadesc(dfname))
To iterate through a group of one or more repeating attributes in a given RTCMMessage object, the following construct can be used (in this illustration, repeating attributes CELLPRN, CELLSIG, DF405, DF406, DF407, DF408, DF420 and DF404 are extracted from an MSM 1077 message msg and collated in the array msmarray):
msmarray = []
for i in range(msg.NCell): # msg = MSM 1077, number of cells = NCell
vals = []
for attr in ("CELLPRN", "CELLSIG", "DF405", "DF406", "DF407", "DF408", "DF420", "DF404"):
val = getattr(msg, f"{attr}_{i+1:02d}")
vals.append(val)
msmarray.append(vals)
print(msmarray)
The following examples are available in the /examples folder:
rtcmpoller.py - illustrates how to read and display RTCM messages 'concurrently' with other tasks using threads and queues. This represents a useful generic pattern for many end user applications.
rtcmfile.py - illustrates how to stream RTCM data from binary log file.
rtcmsocket.py - illustrates how to implement a TCP Socket reader for RTCM messages using RTCMReader iterator functionality.
msmparser.py - illustrates how to parse RTCM3 MSM (multiple signal messages) into a series of iterable data arrays keyed on satellite PRN and signal ID.
rtcm_ntrip_client.py - illustrates a simple NTRIP client using pyrtcm to parse the RTCM3 output.
The RTCM protocol is principally defined in the modules rtcmtypes_core.py and rtcmtypes_get.py as a series of dictionaries. RTCM uses a series of pre-defined data fields ("DF002", DF003" etc.), each of which has a designated data type (UINT32, etc.). Message payload definitions must conform to the following rules:
1. datafield names must be unique within each message class
2. datafield types must be one of the valid data fields ("DF026", "DF059", etc.)
3. repeating or bitfield groups must be defined as a tuple ('numr', {dict}), where:
'numr' is either:
a. an integer representing a fixed number of repeats e.g. 32
b. a string representing the name of a preceding attribute containing the number of repeats e.g. 'DF029'
{dict} is the nested dictionary of repeating items or bitfield group
Repeating attribute names are parsed with a two-digit suffix ("DF030_01", "DF030_02", etc.). Nested repeating groups are supported.
A command line utility gnssstreamer is available via the pygnssutils package. This is capable of reading and parsing NMEA, UBX and RTCM3 data from a variety of input sources (e.g. serial, socket and file) and outputting to a variety of media in a variety of formats. See https://github.com/semuconsulting/pygnssutils for further details.
pyrtcm is maintained entirely by unpaid volunteers. It receives no funding from advertising or corporate sponsorship. If you find the utility useful, please consider sponsoring the project with the price of a coffee...