Hallo zusammen,
ich möchte euch gerne eine Funktion zeigen, bei deren Mathematik ich mir das Hirn zermartere.
Eine korrete (mit Hand nachgerechnet) Lösung gibt sie nun aus, aber ich habe einen ganzen Abend damit zugebracht.
Jetzt ist die große Frage, ob das nicht wesentlich einfacher geht
Also, erst mal zum Hintergrund:
Mein Programm ruft sich bei OpenWeatherMap.org eine Wettervorhersage in Form eines JSON Strings.
In der hier gezeigten Funktion möchte ich mir eine künstliche Temperatur ausrechnen, welche zwei Dinge berücksichtigt:
1) Gewichtung der Vorhersage
2) Anzahl der Tage
Meine Schwierigkeit liegt darin, dass die Anzahl der Tage einen Einfluss auf die Gewichtungsfaktoren haben.
Erklären kann ich das nicht so richtig, also mal zwei Beispiele.
Beispiel 1:
Gewichtungsfaktor: 0.7
Tage: 2
Temperatur = Temperatur_1 * 0.7 + Temperatur_2 * 0.3 bzw.
Temperatur = Temperatur_1 * 0.7 + Temperatur_2 * (1 - 0.7)
Beispiel 2:
Gewichtungsfaktor: 0.8
Tage: 3
Temperatur = Temperatur_1 * 0.8 + Temperatur_2 * 0.16 + Temperatur_3 * 0.04 bzw.
Temperatur = Temperatur_1 * 0.8 + (Temperatur_2 * 0.8 + Temperatur_3 * 0.2 ) * 0.2
def __get_owm_forecast_temperature(progression):
""" Calculates the forecast temperature from OpenWeatherMap.org data.
This private function calculates the temperature forecast from the OpenWeatherMap.org data.
The result depends on the progression factor and the number of forecast days.
Args:
progression (float): weighting factor for forecast data (allowable range: 0.6 - 0.99)
Returns:
str: forecast temperature
"""
# confirm that 'progression' is in the allowable range and fix it if required
# values outside this range do not make sense
if progression < 0.7:
progression = 0.7
if progression > 0.99:
progression = 0.99
import json
# TODO: 3 days is hard-coded for the moment. Needs to read a setting from ini file
days = 3
data = json.loads(__get_owm_jsonstring(days))
temperature_avg = 0
# Run the loop "day-1" times only because the last "loop" needs another calculation
for i in range(days-1):
print("DEBUG: Variable i: ", str(i))
try:
temperature = data["list"][i]["temp"]["max"]
except KeyError:
temperature = 15
print("Temperatur an der Stelle ", str(i), ": ", temperature)
temperature_avg += temperature * progression * ((1-progression)** i )
print("Aktuelle Durchschnittstemperatur: ", temperature_avg)
# now we do the missing "loop"
try:
temperature = data["list"][days-1]["temp"]["max"]
except KeyError:
temperature = 15
print("Temperatur an der Stelle ", str(days-1), ": ", temperature)
temperature_avg += temperature * ((1-progression)** (days-1) )
print("Aktuelle Durchschnittstemperatur: ", temperature_avg)
return temperature_avg
if debug:
progression = 0.8
print("DEBUG: Progression: ", progression, " ; Temperature Average: ", __get_owm_forecast_temperature(progression), "degree C")
Alles anzeigen
Kann ich das alles in einer einzigen Schleife erledigen?
--- Momentan läuft die Schleife ja quasi einen Tag zu wenig, und danach wird der gesamte try-except Block wiederholt, weil ich das letzte Element einen anderen Faktor brauche.
Vielen Dank vorweg für Tipps!
Gruß,
Marcus
PS: Sofern da weiterer Kontext nötig ist: https://github.com/mzurhorst/wass…weather_data.py
(läuft allerdings nicht ohne einen separaten API-Key, den ich da nicht hochladen wollte. Bei Interesse gerne PM an mich)