mirror of
https://github.com/FAUSheppy/ths-datenlogger
synced 2025-12-06 20:21:35 +01:00
wip:
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -12,3 +12,4 @@ __py*
|
|||||||
*.log
|
*.log
|
||||||
*.zip
|
*.zip
|
||||||
dist/
|
dist/
|
||||||
|
dwd/
|
||||||
|
|||||||
@@ -1,16 +1,53 @@
|
|||||||
import glob
|
import glob
|
||||||
|
import datetime
|
||||||
import os
|
import os
|
||||||
|
|
||||||
def write_cachefile(cache_dir, data):
|
SKIP_LINES = 13
|
||||||
pass
|
|
||||||
|
|
||||||
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 = []
|
timeframes = []
|
||||||
|
|
||||||
|
if not os.path.isdir(master_dir):
|
||||||
|
os.mkdir(master_dir)
|
||||||
|
|
||||||
# read files
|
# read files
|
||||||
files = glob.glob(master_dir + "/*.txt"):
|
files = glob.glob(master_dir + "/produkt_tu_stunde*.txt")
|
||||||
print(files)
|
|
||||||
|
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:
|
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(";")
|
station_id, fulldate, dunno, temp, hum, dunno2 = line.split(";")
|
||||||
|
|
||||||
# parse date #
|
# 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 #
|
# set start and end #
|
||||||
if not start and date:
|
if not start and date:
|
||||||
@@ -47,12 +87,8 @@ def generate(master_dir, from_time, to_time, cache_dir_fullpath):
|
|||||||
|
|
||||||
# find a fitting frame #
|
# find a fitting frame #
|
||||||
for start, end, data in timeframes:
|
for start, end, data in timeframes:
|
||||||
|
print(start, end)
|
||||||
if from_time >= start and to_time <= end:
|
if from_time >= start and to_time <= end:
|
||||||
write_cachefile(cache_dir_fullpath, data)
|
return cache_content(from_time, to_time, data, dtype)
|
||||||
return
|
|
||||||
|
|
||||||
raise ValueError("Keine Datei mit passenden Daten gefunden. Bitte Readme lesen")
|
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
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import localization.de as de
|
|||||||
from dbfread import DBF
|
from dbfread import DBF
|
||||||
import timeutils
|
import timeutils
|
||||||
import codecs
|
import codecs
|
||||||
|
import fallback_csv
|
||||||
|
|
||||||
line_colors = ['b', 'r', 'g', 'c', 'm', 'y']
|
line_colors = ['b', 'r', 'g', 'c', 'm', 'y']
|
||||||
tname = CFG("temperatur_plot_name")
|
tname = CFG("temperatur_plot_name")
|
||||||
@@ -130,8 +131,9 @@ def processExternalData(datapoints, plotNameKey, fromTime, toTime, dtype, qtText
|
|||||||
# check response code #
|
# check response code #
|
||||||
if r.status_code != 200 or "nicht gefunden" in r.text.lower():
|
if r.status_code != 200 or "nicht gefunden" in r.text.lower():
|
||||||
qtTextBrowser.append(de.failed_to_retrieve.format("NOT FOUND"))
|
qtTextBrowser.append(de.failed_to_retrieve.format("NOT FOUND"))
|
||||||
raise ValueError("FAILED TO RETRIEVE DATA")
|
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))
|
qtTextBrowser.append(de.pg_request.format(url))
|
||||||
content = r.content.decode('utf-8', "ignore") # ignore bad bytes
|
content = r.content.decode('utf-8', "ignore") # ignore bad bytes
|
||||||
|
|
||||||
@@ -141,7 +143,7 @@ def processExternalData(datapoints, plotNameKey, fromTime, toTime, dtype, qtText
|
|||||||
with open(fullpath, 'w') as f:
|
with open(fullpath, 'w') as f:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
|
|
||||||
else:
|
if os.path.isfile(fullpath):
|
||||||
|
|
||||||
# get data from cache otherwise
|
# get data from cache otherwise
|
||||||
qtTextBrowser.append(de.cache_hit.format(cacheFile))
|
qtTextBrowser.append(de.cache_hit.format(cacheFile))
|
||||||
|
|||||||
@@ -105,3 +105,4 @@ fig_x_height_inches = 12100
|
|||||||
fig_y_height_inches = 8075
|
fig_y_height_inches = 8075
|
||||||
default_target_dir = UseSourceDir
|
default_target_dir = UseSourceDir
|
||||||
default_source_dir = "."
|
default_source_dir = "."
|
||||||
|
dwd_dir = "./dwd/"
|
||||||
|
|||||||
Reference in New Issue
Block a user