# -*- coding: utf-8 -*-
"""
**Command line interface to the omdpb package**.
"""
import sys
import argparse
from os import path
import pandas as pd
from omdpb import open_meteo_data as omd
[docs]def cmd_parser():
""" *Command line argument parser for the openmeteodata entry point*.
Returns
-------
argparse.ArgumentParser
The specified argument parser.
"""
parser = argparse.ArgumentParser(
usage="""%(prog)s <dtype> [<args>] [<options>]""",
description="""Download meteorological data from the Open Meteo
Data Api of the Province of Bolzano.""",
epilog="""Api url:
http://daten.buergernetz.bz.it/services/meteo/v1/""")
# Create an argument group for data types:
# Choices are metadata of stations, metadata of sensors and time series
# of sensors
arggroup = parser.add_argument_group('Data types')
# Positional (required) argument datatype
arggroup.add_argument('Dtype',
default='timeseries', action='store',
help="""The metadata of a station (stations), the
metadata of a sensor (sensors) or the time series
of a sensor (timeseries)""",
choices=['stations', 'sensors', 'timeseries'],
metavar='<dtype>')
# Create an argument group for station code and sensor type
arggroup = parser.add_argument_group('Station code and sensor type')
# The station code, default is None for interactive selection on console
arggroup.add_argument('-stc', '--station_code',
default=None, dest='SCODE', action='store',
help='The station identifier (SCODE)',
metavar='<scode>')
# The sensor type, default is None for interactive selection on console
arggroup.add_argument('-snc', '--sensor_code',
default=None, dest='TYPE', action='store',
help='The sensor identifier(s) (TYPE)',
nargs='+',
metavar='<type>')
# Create an argument group for time series period
arggroup = parser.add_argument_group('Time series period')
# The starting date, default is None returning last 24h of data
arggroup.add_argument('-df', '--date_from',
default=None, dest='date_from', action='store',
help='The starting date of the time series',
metavar='<date>')
# The ending date, default is None returning last 24h of data
arggroup.add_argument('-dt', '--date_to',
default=None, dest='date_to', action='store',
help='The ending date of the time series',
metavar='<date>')
# Create an argument group for output handling
arggroup = parser.add_argument_group('Output options')
# Optional argument for saving output to file
arggroup.add_argument('-s', '--save',
default=False, action='store_true',
help='Save the output to file')
# Output file format
arggroup.add_argument('-f', '--format',
default='csv', action='store',
help='Output file format',
choices=['csv', 'json'],
metavar='<format>')
# Output file path
arggroup.add_argument('-p', '--path',
default=path.expanduser('~\\Documents\\'),
action='store', help='Output file path',
metavar='<path>')
# Output file name
arggroup.add_argument('-fn', '--file_name',
default='omdpb', action='store',
help='Output file name',
metavar='<name>')
return parser
[docs]def openmeteodata_io(argv):
""" *The openmeteodata command line tool*.
Parameters
----------
args: list
output of sys.args[1:].
"""
# Try to parse the arguments with the specified parser
parser = cmd_parser()
# if no arguments are specified, print the help
if not argv:
parser.print_help()
sys.exit()
# if arguments are specified, try to parse them with the defined parser
try:
args = parser.parse_args(argv)
except SystemExit:
return
# Check input and perform respective actions
if args.Dtype == 'stations':
out = omd.station_metadata()
if args.Dtype == 'sensors':
out = omd.sensor_metadata(station_code=args.SCODE,
sensor_code=args.TYPE)
if args.Dtype == 'timeseries':
out = omd.sensor_data(station_code=args.SCODE,
sensor_code=args.TYPE,
date_from=args.date_from,
date_to=args.date_to,
save=args.save,
sformat=args.format,
path=args.path,
filename=args.file_name)
with pd.option_context('display.max_rows', 1000,
'display.max_columns', None,
'display.width', 1000):
print(out)
if not args.save:
return
if args.format == 'csv':
out.to_csv(args.path + args.file_name + '.csv',
sep=',', encoding='utf-8')
else:
out.to_json(args.path + args.file_name + '.json',
orient='columns')
[docs]def openmeteodata():
""" *Entry point for the omdpb package*"""
openmeteodata_io(sys.argv[1:])