forked from marians/dwd-weather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwdweather.py
642 lines (585 loc) · 23.3 KB
/
dwdweather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# encoding: utf8
import sys
import os
import os.path
from ftplib import FTP
from zipfile import ZipFile
import sqlite3
from datetime import datetime
import math
import re
import StringIO
"""
Reads weather data from DWD Germany.
See Github repository for latest version:
https://github.com/marians/dwd-weather
Code published unter the terms of the MIT license.
See here for details.
https://github.com/marians/dwd-weather/blob/master/LICENSE
"""
class DwdWeather(object):
# DWD FTP server host name
server = "ftp-cdc.dwd.de"
# FTP server path for our files
serverpath = "/pub/CDC/observations_germany/climate/hourly"
# database Field definition:
# key = internal field name
# value = (sqlite type, value category, source column name)
fields = {
"air_temperature": (
("temphum_quality_level", "int"), # Qualitaets_Niveau
("temphum_structure_version", "int"), # Struktur_Version
("temphum_temperature", "real"), # LUFTTEMPERATUR
("temphum_humidity", "real"), # REL_FEUCHTE
),
"precipitation": (
("precipitation_quality_level", "int"), # Qualitaets_Niveau
("precipitation_fallen", "int"), # NIEDERSCHLAG_GEFALLEN_IND
("precipitation_height", "real"), # NIEDERSCHLAGSHOEHE
("precipitation_form", "int"), # NIEDERSCHLAGSFORM
),
"soil_temperature": (
("soiltemp_quality_level", "int"), # Qualitaets_Niveau
("soiltemp_1_temperature", "real"), # ERDBODENTEMPERATUR
("soiltemp_1_depth", "real"), # MESS_TIEFE
("soiltemp_2_temperature", "real"), # ERDBODENTEMPERATUR
("soiltemp_2_depth", "real"), # MESS_TIEFE
("soiltemp_3_temperature", "real"), # ERDBODENTEMPERATUR
("soiltemp_3_depth", "real"), # MESS_TIEFE
("soiltemp_4_temperature", "real"), # ERDBODENTEMPERATUR
("soiltemp_4_depth", "real"), # MESS_TIEFE
("soiltemp_5_temperature", "real"), # ERDBODENTEMPERATUR
("soiltemp_5_depth", "real"), # MESS_TIEFE
),
"solar": (
("solar_quality_level", "int"), # Qualitaets_Niveau
("solar_duration", "int"), # SONNENSCHEINDAUER
("solar_sky", "real"), # DIFFUS_HIMMEL_KW_J
("solar_global", "real"), # GLOBAL_KW_J
("solar_atmosphere", "real"), # ATMOSPHAERE_LW_J
("solar_zenith", "real"), # SONNENZENIT
#("solar_TODO", "int"), # MESS_DATUM_WOZ
),
"sun": (
("sun_quality_level", "int"), # Qualitaets_Niveau
("sun_structure_version", "int"), # Struktur_Version
("sun_duration", "real"), # STUNDENSUMME_SONNENSCHEIN
),
"wind": (
("wind_quality_level", "int"), # Qualitaets_Niveau
("wind_structure_version", "int"), # Struktur_Version
("wind_speed", "real"), # WINDGESCHWINDIGKEIT
("wind_direction", "int"), # WINDRICHTUNG
)
}
# Categories of measurements on the server
# key=<category (folder name)> , value=<file name code>
categories = {
"precipitation": "RR",
"soil_temperature": "EB",
"solar": "ST",
"sun": "SD",
"air_temperature": "TU",
"wind": "FF"
}
def __init__(self, **kwargs):
"""
Use all keyword arguments as configuration
- user
- passwd
- cachepath
"""
cp = None
if "cachepath" in kwargs:
cp = kwargs["cachepath"]
self.cachepath = self.init_cache(cp)
# fetch latest data into cache
self.user = "anonymous"
self.passwd = "[email protected]"
self.verbosity = 0
if "verbosity" in kwargs:
self.verbosity = kwargs["verbosity"]
def dict_factory(self, cursor, row):
"""
For emission of dicts from sqlite3
"""
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
def init_cache(self, path):
"""
Creates .dwd-weather directory in the current
user's home, where a cache database and config
file will reside
"""
if path is None:
home = os.path.expanduser("~") + os.sep + ".dwd-weather"
else:
home = path
if not os.path.exists(home):
os.mkdir(home)
self.db = sqlite3.connect(home + os.sep + "dwd-weather.db")
self.db.row_factory = self.dict_factory
c = self.db.cursor()
# Create measures table and index
create = """CREATE TABLE IF NOT EXISTS measures
(
station_id int,
datetime int, """
create_fields = []
for category in sorted(self.fields.keys()):
for fieldname, fieldtype in self.fields[category]:
create_fields.append("%s %s" % (fieldname, fieldtype))
create += ",\n".join(create_fields)
create += ")"
c.execute(create)
index = """CREATE UNIQUE INDEX IF NOT EXISTS unq
ON measures (station_id, datetime)"""
c.execute(index)
# create stations table and index
create = """CREATE TABLE IF NOT EXISTS stations
(
station_id int,
date_start int,
date_end int,
geo_lon real,
geo_lat real,
height int,
name text,
state text
)"""
index = """CREATE UNIQUE INDEX IF NOT EXISTS station_unique
ON stations (station_id, date_start)"""
c.execute(create)
c.execute(index)
self.db.commit()
return home
def import_stations(self):
"""
Load station meta data from DWD server.
"""
if self.verbosity > 0:
print("Importing stations data from FTP server")
ftp = FTP(self.server)
ftp.login(self.user, self.passwd)
for cat in self.categories:
if cat == "solar":
# workaround - solar has no subdirs
path = "%s/%s" % (self.serverpath, cat)
else:
path = "%s/%s/recent" % (self.serverpath, cat)
ftp.cwd(path)
# get directory contents
serverfiles = []
ftp.retrlines('NLST', serverfiles.append)
for filename in serverfiles:
if "Beschreibung_Stationen" not in filename:
continue
if self.verbosity > 1:
print("Reading file %s/%s" % (path, filename))
f = StringIO.StringIO()
ftp.retrbinary('RETR ' + filename, f.write)
self.import_station(f.getvalue())
f.close()
def import_station(self, content):
"""
Takes the content of one station metadata file
and imports it into the database
"""
content = content.strip()
content = content.replace("\r", "")
content = content.replace("\n\n", "\n")
content = content.decode("latin1")
insert_sql = """INSERT OR IGNORE INTO stations
(station_id, date_start, date_end, geo_lon, geo_lat, height, name, state)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)"""
update_sql = """UPDATE stations
SET date_end=?, geo_lon=?, geo_lat=?, height=?, name=?, state=?
WHERE station_id=? AND date_start=?"""
cursor = self.db.cursor()
#print content
linecount = 0
for line in content.split("\n"):
linecount += 1
line = line.strip()
if line == "" or line == u'\x1a':
continue
#print linecount, line
if linecount > 2:
# frist 7 fields
parts = re.split(r"\s+", line, 6)
# seperate name from Bundesland
(name, bundesland) = parts[6].rsplit(" ", 1)
name = name.strip()
del parts[6]
parts.append(name)
parts.append(bundesland)
#print parts
for n in range(len(parts)):
parts[n] = parts[n].strip()
station_id = int(parts[0])
station_height = int(parts[3])
station_lat = float(parts[4])
station_lon = float(parts[5])
station_start = int(parts[1])
station_end = int(parts[2])
station_name = parts[6]
station_state = parts[7]
# issue sql
cursor.execute(insert_sql, (
station_id,
station_start,
station_end,
station_lon,
station_lat,
station_height,
station_name,
station_state))
cursor.execute(update_sql, (
station_end,
station_lon,
station_lat,
station_height,
station_name,
station_state,
station_id,
station_start))
self.db.commit()
def import_measures(self, station_id, latest=True, historic=False):
"""
Load data from DWD server.
Parameter:
station_id: e.g. 2667 (Köln-Bonn airport)
latest: Load most recent data (True, False)
historic: Load older values
We download ZIP files for several categories
of measures. We then extract one file from
each ZIP. This path is then handed to the
CSV -> Sqilte import function.
"""
if self.verbosity > 0:
print("Importing measures for station %d from FTP server" % station_id)
# Which files to import
timeranges = []
if latest:
timeranges.append("recent")
if historic:
timeranges.append("historical")
ftp = FTP(self.server)
ftp.login(self.user, self.passwd)
importfiles = []
def download_and_import(path, filename, cat, timerange=None):
output_path = self.cachepath + os.sep + filename
if timerange is None:
timerange = "-"
data_filename = "data_%s_%s_%s.txt" % (station_id, timerange, cat)
if self.verbosity > 1:
print("Reading file %s/%s from FTP server" % (path, filename))
ftp.retrbinary('RETR ' + filename, open(output_path, 'wb').write)
with ZipFile(output_path) as myzip:
for f in myzip.infolist():
if "Terminwerte" in f.filename:
# this is our data file
myzip.extract(f, self.cachepath + os.sep)
os.rename(self.cachepath + os.sep + f.filename,
self.cachepath + os.sep + data_filename)
importfiles.append([cat, self.cachepath + os.sep + data_filename])
os.remove(output_path)
for cat in self.categories.keys():
if self.verbosity > 1:
print("Handling category %s" % cat)
if cat == "solar":
path = "%s/%s" % (self.serverpath, cat)
ftp.cwd(path)
# list dir content, get right file name
serverfiles = []
ftp.retrlines('NLST', serverfiles.append)
filename = None
for fn in serverfiles:
if ("_%05d." % station_id) in fn:
filename = fn
break
if filename is None:
if self.verbosity > 1:
print("Station %s has no data for category '%s'" % (station_id, cat))
continue
else:
download_and_import(path, filename, cat)
else:
for timerange in timeranges:
timerange_suffix = "akt"
if timerange == "historical":
timerange_suffix = "hist"
path = "%s/%s/%s" % (self.serverpath, cat, timerange)
ftp.cwd(path)
# list dir content, get right file name
serverfiles = []
ftp.retrlines('NLST', serverfiles.append)
filename = None
for fn in serverfiles:
if ("_%05d_" % station_id) in fn:
filename = fn
break
if filename is None:
if self.verbosity > 1:
print("Station %s has no data for category '%s'" % (station_id, cat))
continue
download_and_import(path, filename, cat, timerange)
for item in importfiles:
self.import_measures_textfile(item[0], item[1])
os.remove(item[1])
def import_measures_textfile(self, category, path):
"""
Import content of source text file into database
"""
f = open(path, "rb")
content = f.read()
f.close()
content = content.strip()
sets = []
# create SQL template
for fieldname, fieldtype in self.fields[category]:
sets.append(fieldname + "=?")
insert_template = """INSERT OR IGNORE INTO measures (station_id, datetime) VALUES (?, ?)"""
update_template = "UPDATE measures SET %s WHERE station_id=? AND datetime=?" % ", ".join(sets)
# create data rows
insert_datasets = []
update_datasets = []
count = 0
for line in content.split("\n"):
count += 1
line = line.strip()
if line == "" or line == '\x1a':
continue
line = line.replace(";eor", "")
parts = line.split(";")
for n in range(len(parts)):
parts[n] = parts[n].strip()
#print parts
if count > 1:
# station id
parts[0] = int(parts[0])
# timestamp
if ":" in parts[1]:
parts[1] = parts[1].split(":")[0]
parts[1] = int(parts[1])
insert_datasets.append([parts[0], parts[1]])
dataset = []
# station_id and datetime
#if category == "soil_temp":
# print fields[category]
# print parts
for n in range(2, len(parts)):
(fieldname, fieldtype) = self.fields[category][(n - 2)]
if parts[n] == "-999":
parts[n] = None
elif fieldtype == "real":
parts[n] = float(parts[n])
elif fieldtype == "int":
try:
parts[n] = int(parts[n])
except ValueError:
sys.stderr.write("Error in converting field '%s', value '%s' to int.\n" % (
fieldname, parts[n]))
(t, val, trace) = sys.exc_info()
import traceback
traceback.print_tb(trace)
sys.exit()
dataset.append(parts[n])
# station_id and datetime for WHERE clause
dataset.append(parts[0])
dataset.append(parts[1])
update_datasets.append(dataset)
c = self.db.cursor()
c.executemany(insert_template, insert_datasets)
c.executemany(update_template, update_datasets)
self.db.commit()
def get_data_age(self):
"""
Return age of latest dataset as datetime.timedelta
"""
sql = "SELECT MAX(datetime) AS maxdatetime FROM measures"
c = self.db.cursor()
c.execute(sql)
item = c.fetchone()
if item["maxdatetime"] is not None:
latest = datetime.strptime(str(item["maxdatetime"]), "%Y%m%d%H")
return datetime.utcnow() - latest
def query(self, station_id, hour, recursion=0):
"""
Get values from cache.
station_id: Numeric station ID
hour: datetime object
"""
if recursion < 2 :
sql = "SELECT * FROM measures WHERE station_id=? AND datetime=?"
c = self.db.cursor()
c.execute(sql, (station_id, hour.strftime("%Y%m%d%H")))
out = c.fetchone()
if out is None:
# cache miss
age = (datetime.utcnow() - hour).total_seconds() / 86400
if age < 360:
self.import_measures(station_id, latest=True)
elif age >= 360 and age <= 370:
self.import_measures(station_id, latest=True, historic=True)
else:
self.import_measures(station_id, historic=True)
return self.query(station_id, hour, recursion=(recursion + 1))
c.close()
return out
def haversine_distance(self, origin, destination):
lon1, lat1 = origin
lon2, lat2 = destination
radius = 6371000 # meters
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c
return d
def stations(self, historic=False):
"""
Return list of dicts with all stations
"""
out = []
sql = """SELECT s2.*
FROM stations s1
LEFT JOIN stations s2 ON (s1.station_id=s2.station_id AND s1.date_end=s1.date_end)
GROUP BY s1.station_id"""
c = self.db.cursor()
for row in c.execute(sql):
out.append(row)
c.close()
if len(out) == 0:
# cache miss - have to import stations.
self.import_stations()
out = self.stations()
return out
def nearest_station(self, lon, lat):
# select most current stations datasets
closest = None
closest_distance = 99999999999
for station in self.stations():
d = self.haversine_distance((lon, lat),
(station["geo_lon"], station["geo_lat"]))
if d < closest_distance:
closest = station
closest_distance = d
return closest
def stations_geojson(self):
out = {
"type": "FeatureCollection",
"features": []
}
for station in self.stations():
out["features"].append({
"type": "Feature",
"properties": {
"id": station["station_id"],
"name": station["name"]
},
"geometry": {
"type": "Point",
"coordinates": [station["geo_lon"], station["geo_lat"]]
}
})
import json
return json.dumps(out)
def stations_csv(self, delimiter=","):
"""
Return stations list as CSV
"""
import csv
csvfile = StringIO.StringIO()
# assemble field list
headers = ["station_id", "date_start", "date_end",
"geo_lon", "geo_lat", "height", "name"]
writer = csv.writer(csvfile, delimiter=delimiter, quoting=csv.QUOTE_MINIMAL)
writer.writerow(headers)
stations = self.stations()
for station in stations:
row = []
for n in range(len(headers)):
val = station[headers[n]]
if val is None:
val = ""
elif type(val) == int:
val = str(val)
elif type(val) == float:
val = "%.4f" % val
elif type(val) == unicode:
val = val.encode("utf8")
row.append(val)
writer.writerow(row)
contents = csvfile.getvalue()
csvfile.close()
return contents
if __name__ == "__main__":
def get_station(args):
dw = DwdWeather(cachepath=args.cachepath, verbosity=args.verbosity)
import json
print json.dumps(dw.nearest_station(lon=args.lon, lat=args.lat), indent=4)
def get_stations(args):
dw = DwdWeather(cachepath=args.cachepath, verbosity=args.verbosity)
output = ""
if args.type == "geojson":
output = dw.stations_geojson()
elif args.type == "csv":
output = dw.stations_csv()
elif args.type == "plain":
output = dw.stations_csv(delimiter="\t")
if args.output_path is None:
print output
else:
f = open(args.output_path, "wb")
f.write(output)
f.close()
def get_weather(args):
hour = datetime.strptime(str(args.hour), "%Y%m%d%H")
dw = DwdWeather(cachepath=args.cachepath, verbosity=args.verbosity)
import json
print json.dumps(dw.query(args.station_id, hour), indent=4)
import argparse
argparser = argparse.ArgumentParser(prog="dwdweather",
description="Get weather information for Germany.")
argparser.add_argument("-v", dest="verbosity", action="count",
help="Activate verbose output. Use -vv or -vvv to increase verbosity.",
default=0)
argparser.add_argument("-c", dest="cachepath",
help="Path to cache directory. Defaults to .dwd-weather in user's home dir.",
default=os.path.expanduser("~") + os.sep + ".dwd-weather")
subparsers = argparser.add_subparsers(title="Actions", help="Main client actions.")
def float_range(min, max):
def check_range(x):
x = float(x)
if x < min or x > max:
raise argparse.ArgumentTypeError("%r not in range [%r, %r]"%(x,min,max))
return x
return check_range
# station options
parser_station = subparsers.add_parser('station',
help='Find a station')
parser_station.set_defaults(func=get_station)
parser_station.add_argument("lon", type=float_range(-180, 180),
help="Geographic longitude (x) component as float, e.g. 7.2")
parser_station.add_argument("lat", type=float_range(-90, 90),
help="Geographic latitude (y) component as float, e.g. 53.9")
# stations options
parser_stations = subparsers.add_parser('stations',
help='List or export stations')
parser_stations.set_defaults(func=get_stations)
parser_stations.add_argument("-t", "--type", dest="type",
choices=["geojson", "csv", "plain"], default="plain",
help="Export format")
parser_stations.add_argument("-f", "--file", type=str, dest="output_path",
help="Export file path. If not given, STDOUT is used.")
# weather options
parser_weather = subparsers.add_parser('weather', help='Get weather data for a station and hour')
parser_weather.set_defaults(func=get_weather)
parser_weather.add_argument("station_id", type=int, help="Numeric ID of the station, e.g. 2667")
parser_weather.add_argument("hour", type=int, help="Time in the form of YYYYMMDDHH")
args = argparser.parse_args()
args.func(args)