Skip to content

Instantly share code, notes, and snippets.

@sickel
Last active March 17, 2024 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sickel/18e9aee3d20e4151600ca385150f7cb6 to your computer and use it in GitHub Desktop.
Save sickel/18e9aee3d20e4151600ca385150f7cb6 to your computer and use it in GitHub Desktop.
Script to store forecasts from met.no
#!/usr/bin/python3
"""
Data are downloaded hourly from api.met.no using
/usr/bin/curl -s -A "<email>" "https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=59&lon=10" > /var/www/html/homelog2/weather.json 2>/dev/null
check https://api.met.no/ for terms and usage
After being downloaded, the data are available on a local http-server. The reason I am not reading the file directly in
this script is that I am also using it for other purposes that need the http
schema for table: https://gist.github.com/sickel/9ae88a47f665c0962120078ed93e103a
"""
import psycopg
import urllib.request
import json
from datetime import datetime
import time
import os
debug = os.environ.get('IMPORTMETDATA_DEBUG',None) is not None
url = "http://localhost/homelog2/weather.json"
# url is pointing to where the downloaded file is served
# dbconn: dbname=database user=username password=password
# Put a file with the info in the same directory as this script,
# or edit the file to have it directly
with open('dbconn.txt') as dbconnfile:
dbconn = dbconnfile.read()
dateformat='%Y-%m-%dT%H:%M:%Sz'#"YYYY-MM-DDTHH:MM:SSZ"
if debug:
print(dbconn)
CONN = psycopg.connect(dbconn) #,cursor_factory=psycopg.extras.DictCursor)
CUR = CONN.cursor()
INSERT = """
insert into forecast(parameter,value,unit,timestamp)
values(%s,%s,%s,to_timestamp(%s))
on conflict (parameter,timestamp)
DO UPDATE set value = EXCLUDED.value
"""
f=urllib.request.urlopen(url)
metdata=json.loads(f.read())
updated = metdata['properties']['meta']['updated_at']
if debug:
print(updated)
updatetime = datetime.strptime(updated,dateformat)
units = metdata['properties']['meta']['units']
datapoints = metdata['properties']['timeseries']
for cast in datapoints:
forecasttime = datetime.strptime(cast['time'],dateformat)
timedelta = (forecasttime - updatetime).seconds+(forecasttime - updatetime).days*24*3600
utime = int(time.mktime(forecasttime.timetuple()))
forecast = cast['data']['instant']['details']
if debug:
print(forecast)
for parameter in forecast:
CUR.execute(INSERT,[parameter,forecast[parameter],units[parameter],utime])
CONN.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment