3 # needs ~/seepark.ini with
7 # 3319578 for Obsteig, AT
9 from pprint import pprint
16 from seeparklib.openweathermap import openweathermap_json
21 def fromtimestamp(timestamp, format):
22 return datetime.datetime.fromtimestamp(timestamp).strftime(format)
25 # https://stackoverflow.com/questions/7490660/converting-wind-direction-in-angles-to-text-words
26 def degToCompass(num):
27 if num is None or num is math.nan:
29 val=int((num/22.5)+.5)
30 arr=["N","NNO","NO","ONO","O","OSO", "SO", "SSO","S","SSW","SW","WSW","W","WNW","NW","NNW"]
31 return arr[(val % 16)]
34 def extractweatherdata(w):
37 sunrise = w['sys']['sunrise'],
38 sunset = w['sys']['sunset'],
39 temp = w['main']['temp'],
40 pressure = w['main']['pressure'],
41 humidity = w['main']['humidity'],
42 visibility = w['visibility'],
43 weather = w['weather'][0]['description'],
44 sky = w['weather'][0]['main'],
45 windspeed = w['wind']['speed'],
46 cloudiness = w['clouds']['all'],
49 data['sunrise_t'] = fromtimestamp(data['sunrise'], '%H:%M:%S')
50 data['sunset_t'] = fromtimestamp(data['sunset'], '%H:%M:%S')
51 data['date'] = fromtimestamp(data['datetime'], '%Y-%m-%d')
52 data['time'] = fromtimestamp(data['datetime'], '%H:%M:%S')
53 data['winddegrees'] = w['wind']['deg'] if 'deg' in w['wind'] else math.nan
54 data['winddirection'] = degToCompass(data['winddegrees'])
55 data['precipitation'] = w['rain']['3h'] if 'rain' in w else math.nan
60 def write_csv(csv_file, weather_data):
61 """output like wetter.at.pl"""
62 with open(csv_file, "a", newline="") as file:
63 writer = csv.writer(file, dialect="excel", delimiter=';')
67 weather_data['sunrise_t'],
68 weather_data['sunset_t'],
69 "{:.2f}".format(weather_data['temp']),
70 "{:.2f} mm/h".format(weather_data['precipitation']),
71 "{:.1f} km/h {}".format(weather_data['windspeed'], weather_data['winddirection']),
72 weather_data['weather'],
73 "{}".format(weather_data['cloudiness'])
78 config = configparser.ConfigParser()
79 config.read(configfile)
80 apikey = config.get('openweathermap', 'apikey')
81 cityid = config.get('openweathermap', 'cityid')
82 csvfile = config.get("openweathermap", 'csvfilename')
84 weather_raw = openweathermap_json(apikey, cityid)
87 weather = extractweatherdata(weather_raw)
95 write_csv(os.path.expanduser(csvfile), weather)
98 if __name__ == '__main__':
99 default_config_file = os.path.expanduser('~/seewasser.ini')
100 parser = argparse.ArgumentParser(description='Get OpenWeathermap data')
101 parser.add_argument('--config', default=default_config_file, help='configuration file')
102 args = parser.parse_args()