earthobservations / luftdatenpumpe

Acquire and process live and historical air quality data without efforts. Filter by station-id, sensor-id and sensor-type, apply reverse geocoding, store into time-series and RDBMS databases, publish to MQTT, output as JSON, or visualize in Grafana. Data sources: Sensor.Community (luftdaten.info), IRCELINE, and OpenAQ.
https://luftdatenpumpe.readthedocs.io/
GNU Affero General Public License v3.0
35 stars 3 forks source link

Read list of station or sensor identifiers from file #46

Open amotl opened 1 year ago

amotl commented 1 year ago

Hi there,

at ^1, @ohobby asked about how he would be able to feed a list of sensor identifiers from a file to the luftdatenpumpe stations or luftdatenpumpe readings commands.

I could also provide a list in a file with the sensor_id’s. Is it possible to include this list in the query so I don’t have to type all sensor_id’s in the query?

Indeed, it would be sweet if Luftdatenpumpe provided such an option.

With kind regards, Andreas.

amotl commented 1 year ago

Workaround

As a workaround, a few commands of Unix magic can be applied to the problem. This incantation will read the file sensor_ids.txt, containing a list of sensor identifiers, separated by newlines, and will reformat it to be comma-separated.

cat sensor_ids.txt | printf "%s" "$(< /dev/stdin)" | tr '\n' ','

In this way, it is suitable for feeding it to the --sensor= option of Luftdatenpumpe. Imagine you have a file sensor_ids.txt, you could use a wrapper program like ldipumpe.sh, attached below, by invoking ./ldipumpe.sh sensor_ids.txt --reverse-geocode --target=....

Wrapper program

#!/bin/bash
#
# Use Luftdatenpumpe to acquire data by list of
# sensor identifiers from file.
#
# Synopsis:
#
# ./ldipumpe.sh sensor_ids.txt --reverse-geocode
#

# Read input file name.
infile=$1
shift

# Sanity checks.
if [ -z "${infile}" ]; then
  echo "ERROR: No input file given"
  exit 1
fi

# Read and reformat text file to comma-separated list.
sensor_ids=$(cat "${infile}" | printf "%s" "$(< /dev/stdin)" | tr '\n' ',')
echo "Using sensor identifiers: ${sensor_ids}"

# Invoke Luftdatenpumpe with given sensor identifiers,
# forwarding other command line options.
luftdatenpumpe stations \
  --network=ldi --sensor="${sensor_ids}" \
  "$@"