diff --git a/.gitignore b/.gitignore index 8f6a37b..d76d74a 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ __py* *.log *.zip dist/ +dwd/ diff --git a/src/main/python/fallback_csv.py b/src/main/python/fallback_csv.py index 47a3ed9..3a09b23 100644 --- a/src/main/python/fallback_csv.py +++ b/src/main/python/fallback_csv.py @@ -1,16 +1,53 @@ import glob +import datetime import os -def write_cachefile(cache_dir, data): - pass +SKIP_LINES = 13 -def generate(master_dir, from_time, to_time, cache_dir_fullpath): +def cache_content(from_time, to_time, data, dtype): + + return_string = "" + + skip_count = 0 + for d in data: + + date, temp, hum = d + + # skip outside timeframe # + if date < from_time or date > to_time: + continue + + # skip lines # + if skip_count < SKIP_LINES: + return_string += "\n" + skip_count += 1 + continue + + if dtype == "lufttemperatur-aussen": + content_number = temp + elif dtype == "luftfeuchte": + content_number = hum + else: + raise ValueError("Bad dtype: {}".format(dtype)) + + date_cache_format = date.strftime("%d.%m.%Y %H:%M") + content_str = "{:1f}".format(content_number).replace(".",",") + return_string += "{};{}\n".format(date_cache_format, content_str) + + return return_string + +def generate(master_dir, from_time, to_time, cache_file, dtype): timeframes = [] + if not os.path.isdir(master_dir): + os.mkdir(master_dir) + # read files - files = glob.glob(master_dir + "/*.txt"): - print(files) + files = glob.glob(master_dir + "/produkt_tu_stunde*.txt") + + if not files: + raise ValueError("Keine DWD_Datei in: {} gefunden. Bitte herunterladen und entpacken! https://www.dwd.de/DE/leistungen/klimadatendeutschland/klarchivstunden.html;jsessionid=C423E76B30D18F24C43F4E7E36744C8C.live21073?nn=16102") for fname in files: @@ -34,7 +71,10 @@ def generate(master_dir, from_time, to_time, cache_dir_fullpath): station_id, fulldate, dunno, temp, hum, dunno2 = line.split(";") # parse date # - date = datetime.datetime.strptime(fulldate, "%y%m%d%H") + date = datetime.datetime.strptime(fulldate, "%Y%m%d%H") + + # append data # + data.append((date, float(temp), float(hum))) # set start and end # if not start and date: @@ -47,12 +87,8 @@ def generate(master_dir, from_time, to_time, cache_dir_fullpath): # find a fitting frame # for start, end, data in timeframes: + print(start, end) if from_time >= start and to_time <= end: - write_cachefile(cache_dir_fullpath, data) - return + return cache_content(from_time, to_time, data, dtype) raise ValueError("Keine Datei mit passenden Daten gefunden. Bitte Readme lesen") - - - # generate a correct cache file from it return - # if no suiteable file is found return an appropriate error diff --git a/src/main/python/input_backend.py b/src/main/python/input_backend.py index 1010255..da17e04 100644 --- a/src/main/python/input_backend.py +++ b/src/main/python/input_backend.py @@ -8,6 +8,7 @@ import localization.de as de from dbfread import DBF import timeutils import codecs +import fallback_csv line_colors = ['b', 'r', 'g', 'c', 'm', 'y'] tname = CFG("temperatur_plot_name") @@ -130,10 +131,11 @@ def processExternalData(datapoints, plotNameKey, fromTime, toTime, dtype, qtText # check response code # if r.status_code != 200 or "nicht gefunden" in r.text.lower(): qtTextBrowser.append(de.failed_to_retrieve.format("NOT FOUND")) - raise ValueError("FAILED TO RETRIEVE DATA") - - qtTextBrowser.append(de.pg_request.format(url)) - content = r.content.decode('utf-8', "ignore") # ignore bad bytes + qtTextBrowser.append("Versuche von DWD-Datei zu laden - Dass kann einen Moment dauern") + content = fallback_csv.generate(CFG("dwd_dir"), fromTime, toTime, cacheFile, dtype) + else: + qtTextBrowser.append(de.pg_request.format(url)) + content = r.content.decode('utf-8', "ignore") # ignore bad bytes # cache data # if not os.path.isdir(cacheDir): @@ -141,7 +143,7 @@ def processExternalData(datapoints, plotNameKey, fromTime, toTime, dtype, qtText with open(fullpath, 'w') as f: f.write(content) - else: + if os.path.isfile(fullpath): # get data from cache otherwise qtTextBrowser.append(de.cache_hit.format(cacheFile)) diff --git a/ths_config.txt b/ths_config.txt index f87f698..c9723f0 100644 --- a/ths_config.txt +++ b/ths_config.txt @@ -105,3 +105,4 @@ fig_x_height_inches = 12100 fig_y_height_inches = 8075 default_target_dir = UseSourceDir default_source_dir = "." +dwd_dir = "./dwd/"