habitat.filters

Commonly required filters that are supplied with habitat.

Filters are small functions that can be run against incoming payload telemetry during the parse phase, either before attempts at callsign extraction, before the actual parse (but after the callsign has been identified) or after parsing is complete.

This module contains commonly used filters which are supplied with habitat, but end users are free to write their own and have habitat.loadable_manager load them for use.

Functions

invalid_always(data) Add the _fix_invalid key to data.
invalid_gps_lock(config, data) Checks a gps_lock field to see if the payload has a lock
invalid_location_zero(data) If the latitude and longitude are zero, the fix is marked invalid.
numeric_scale(config, data) Post filter that scales a key from data by a factor in config.
semicolons_to_commas(config, data) Intermediate filter that converts semicolons to commas.
simple_map(config, data) Post filter that maps source to destination values based on a dictionary.
habitat.filters.semicolons_to_commas(config, data)[source]

Intermediate filter that converts semicolons to commas.

All semicolons in the string are replaced with colons and the checksum is updated; crc16-ccitt is assumed by default but can be overwritten with config["checksum"].

>>> semicolons_to_commas({}, '$$testpayload,1,2,3;4;5;6*8A24')
'$$testpayload,1,2,3,4,5,6*888F'
habitat.filters.numeric_scale(config, data)[source]

Post filter that scales a key from data by a factor in config.

data[config["source"]] is multiplied by config["factor"] and written back to data[config["destination"]] if it exists, or data[config["source"]] if not. config["offset"] is also optionally applied along with config["round"].

>>> config = {"source": "key", "factor": 2.0}
>>> data = {"key": "4", "other": "data"}
>>> numeric_scale(config, data) == {'key': 8.0, 'other': 'data'}
True
>>> config["destination"] = "result"
>>> numeric_scale(config, data) == {'key': 8.0, 'result': 16.0, 'other':
...     'data'}
...
True
habitat.filters.simple_map(config, data)[source]

Post filter that maps source to destination values based on a dictionary.

data[config["source"]] is used as a lookup key in config["map"] and the resulting value is written to data[config["destination"]] if it exists, or data[config["source"]] if not.

A ValueError is raised if config["map"] is not a dictionary or does not contain the value read from data. >>> config = {“source”: “key”, “destination”: “result”, “map”: ... {1: ‘a’, 2: ‘b’}} ... >>> data = {“key”: 2} >>> simple_map(config, data) == {‘key’: 2, ‘result’: ‘b’} True

habitat.filters.invalid_always(data)[source]

Add the _fix_invalid key to data.

habitat.filters.invalid_location_zero(data)[source]

If the latitude and longitude are zero, the fix is marked invalid.

habitat.filters.invalid_gps_lock(config, data)[source]

Checks a gps_lock field to see if the payload has a lock

The source key is config[“source”], or “gps_lock” if that is not set.

The fix is marked invalid if data[source] is not in the list config[“ok”].

habitat.filters.zero_pad_coordinates(config, data)[source]

Post filter that inserts zeros after the decimal point in coordinates, to fix the common error of having the integer and fractional parts of a decimal degree value as two ints and outputting them using something like sprintf(“%i.%i”, int_part, frac_part);, resulting in values that should be 51.0002 being output as 51.2 or similar.

The fields to change is the list config[“fields”] and the correct post-decimal-point width is config[“width”]. By default fields is [“latitude”, “longitude”] and width is 5.

habitat.filters.zero_pad_times(config, data)[source]

Intermediate filter that zero pads times which have been incorrectly transmitted as e.g. 12:3:8 instead of 12:03:08. Only works when colons are used as delimiters.

The field position to change is config[“field”] and defaults to 2 (which is typical with $$PAYLOAD,ID,TIME). The checksum in use is config[“checksum”] and defaults to crc16-ccitt.