Skip to content

Commit

Permalink
Filter stations by country code
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Apr 10, 2019
1 parent 006beab commit ec8b524
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.rst
Expand Up @@ -10,6 +10,7 @@ in progress
- Fix published messages getting lost when not starting
the MQTT main loop after connecting to MQTT broker
- Refactor station list filter
- Filter stations by country code


2019-01-22 0.8.2
Expand Down
5 changes: 5 additions & 0 deletions doc/running.rst
Expand Up @@ -15,6 +15,7 @@ luftdatenpumpe --help

Options:
--source=<source> Data source, either "api" or "file://" [default: api].
--country=<countries> Filter data by given country codes, comma-separated.
--station=<stations> Filter data by given location ids, comma-separated.
--sensor=<sensors> Filter data by given sensor ids, comma-separated.
--sensor-type=<sensor-types> Filter data by given sensor types, comma-separated.
Expand All @@ -28,8 +29,12 @@ luftdatenpumpe --help
--debug Enable debug messages
-h --help Show this screen


Station list examples:

# Display metadata for given countries in JSON format
luftdatenpumpe stations --country=BE,NL,LU

# Display metadata for given stations in JSON format, with reverse geocoding
luftdatenpumpe stations --station=28,1071 --reverse-geocode

Expand Down
11 changes: 10 additions & 1 deletion luftdatenpumpe/commands.py
Expand Up @@ -28,6 +28,7 @@ def run():
Options:
--source=<source> Data source, either "api" or "file://" [default: api].
--country=<countries> Filter data by given country codes, comma-separated.
--station=<stations> Filter data by given location ids, comma-separated.
--sensor=<sensors> Filter data by given sensor ids, comma-separated.
--sensor-type=<sensor-types> Filter data by given sensor types, comma-separated.
Expand All @@ -44,6 +45,9 @@ def run():
Station list examples:
# Display metadata for given countries in JSON format
luftdatenpumpe stations --country=BE,NL,LU
# Display metadata for given stations in JSON format, with reverse geocoding
luftdatenpumpe stations --station=28,1071 --reverse-geocode
Expand Down Expand Up @@ -151,7 +155,7 @@ def run():

# B. Data processing targets

# Optionally, decode filters by station id and/or sensor id
# Optionally apply filters by country code, station id and/or sensor id
filter = {}

# Lists of Integers.
Expand All @@ -164,6 +168,11 @@ def run():
if options[filter_name]:
filter[filter_name] = list(map(str.lower, read_list(options[filter_name])))

# Lists of upper-case Strings.
for filter_name in ['country']:
if options[filter_name]:
filter[filter_name] = list(map(str.upper, read_list(options[filter_name])))

log.info('Applying filter: {}'.format(filter))

# Fake data source. Currently always LDI.
Expand Down
4 changes: 4 additions & 0 deletions luftdatenpumpe/core.py
Expand Up @@ -242,6 +242,7 @@ def apply_filter(self, data):
#log.info('item: %s', item)

# Decode JSON item
country_code = item['location']['country'].upper()
station_id = item['location']['id']
sensor_id = item['sensor']['id']

Expand All @@ -250,6 +251,9 @@ def apply_filter(self, data):
# TODO: Improve evaluating conditions.
if self.filter:
skip = False
if 'country' in self.filter:
if country_code not in self.filter['country']:
skip = True
if 'station' in self.filter:
if station_id not in self.filter['station']:
skip = True
Expand Down

0 comments on commit ec8b524

Please sign in to comment.