diff --git a/modules/class_load.py b/modules/class_load.py new file mode 100644 index 0000000..c025e48 --- /dev/null +++ b/modules/class_load.py @@ -0,0 +1,95 @@ +import json +from datetime import datetime, timedelta, timezone +import numpy as np +from pprint import pprint + +# Lade die .npz-Datei beim Start der Anwendung + +class LoadForecast: + def __init__(self, filepath=None, year_energy=None): + self.filepath = filepath + self.data = None + self.data_year_energy = None + self.year_energy = year_energy + self.load_data() + + + # def get_prices_for_date(self, query_date): + # query_date = datetime.strptime(query_date, '%Y-%m-%d').date() + # prices_for_date = [price for price in self.price_data if price.starts_at.date() == query_date] + # return prices_for_date + + # def get_price_for_datetime(self, query_datetime): + # query_datetime = datetime.strptime(query_datetime, '%Y-%m-%d %H').replace(minute=0, second=0, microsecond=0) + # query_datetime = query_datetime.replace(tzinfo=timezone(timedelta(hours=1))) + + # for price in self.price_data: + # #print(price.starts_at.replace(minute=0, second=0, microsecond=0) , " ", query_datetime, " == ",price.starts_at.replace(minute=0, second=0, microsecond=0) == query_datetime) + # if price.starts_at.replace(minute=0, second=0, microsecond=0) == query_datetime: + # return price + # return None + def get_daily_stats(self, date_str): + """ + Gibt den 24-Stunden-Verlauf mit Erwartungswert und Standardabweichung für ein gegebenes Datum zurück. + + :param data: NumPy Array mit Shape (365, 2, 24), repräsentiert Daten für ein Jahr + :param date_str: Datum als String im Format "YYYY-MM-DD" + :return: Ein Array mit Shape (2, 24), enthält Erwartungswerte und Standardabweichungen + """ + # Umwandlung des Datums-Strings in ein datetime-Objekt + date = datetime.strptime(date_str, "%Y-%m-%d") + + # Berechnung des Tages des Jahres (1 bis 365) + day_of_year = date.timetuple().tm_yday + + # Extraktion des 24-Stunden-Verlaufs für das gegebene Datum + daily_stats = self.data_year_energy[day_of_year - 1] # -1, da die Indizierung bei 0 beginnt + return daily_stats + + def get_hourly_stats(self, date_str, hour): + """ + Gibt Erwartungswert und Standardabweichung für eine spezifische Stunde eines gegebenen Datums zurück. + + :param data: NumPy Array mit Shape (365, 2, 24), repräsentiert Daten für ein Jahr + :param date_str: Datum als String im Format "YYYY-MM-DD" + :param hour: Spezifische Stunde (0 bis 23) + :return: Ein Array mit Shape (2,), enthält Erwartungswert und Standardabweichung für die spezifizierte Stunde + """ + # Umwandlung des Datums-Strings in ein datetime-Objekt + date = datetime.strptime(date_str, "%Y-%m-%d") + + # Berechnung des Tages des Jahres (1 bis 365) + day_of_year = date.timetuple().tm_yday + + # Extraktion von Erwartungswert und Standardabweichung für die gegebene Stunde + hourly_stats = self.data_year_energy[day_of_year - 1, :, hour] # Zugriff auf die spezifische Stunde + + return hourly_stats + + + + def load_data(self): + with open(self.filepath, 'r') as file: + data = np.load(self.filepath) + self.data = np.array(list(zip(data["yearly_profiles"],data["yearly_profiles_std"]))) + self.data_year_energy = self.data * self.year_energy + pprint(self.data_year_energy) + + def get_price_data(self): + # load_profiles_exp_l = load_profiles_exp*year_energy + # load_profiles_std_l = load_profiles_std*year_energy + + return self.price_data + +# Beispiel für die Verwendung der Klasse +if __name__ == '__main__': + filepath = r'..\load_profiles.npz' # Pfad zur JSON-Datei anpassen + lf = LoadForecast(filepath=filepath, year_energy=2000) + #load_forecast = lf.get_price_data + # + #price_forecast = HourlyElectricityPriceForecast(filepath) + specific_date_prices = lf.get_daily_stats('2024-02-16') # Datum anpassen + specific_date_prices = lf.get_hourly_stats('2024-02-16', 12) # Datum anpassen + print(specific_date_prices) + #for price in price_forecast.get_price_data(): + # print(price.get_starts_at(), price.get_total(), price.get_currency()) diff --git a/server_load_profile.py b/server_load_profile.py index e181156..838be9f 100644 --- a/server_load_profile.py +++ b/server_load_profile.py @@ -1,19 +1,12 @@ from flask import Flask, jsonify, request import numpy as np from datetime import datetime +import modules.class_load as cl +from pprint import pprint app = Flask(__name__) -# Lade die .npz-Datei beim Start der Anwendung -data = np.load('load_profiles.npz') -load_profiles_exp = data["yearly_profiles"] #.flatten().tolist() -load_profiles_std = data["yearly_profiles_std"] #.flatten().tolist() -print(load_profiles_exp) -print(load_profiles_exp.shape) -#load_profiles_exp = load_profiles_exp*1000.0 -print(load_profiles_exp) -print(load_profiles_exp.sum()) @app.route('/getdata', methods=['GET']) def get_data(): @@ -24,25 +17,27 @@ def get_data(): try: # Konvertiere das Datum in ein datetime-Objekt date_obj = datetime.strptime(date_str, '%Y-%m-%d') - - year_energy = float(year_energy) - - load_profiles_exp_l = load_profiles_exp*year_energy - load_profiles_std_l = load_profiles_std*year_energy + filepath = r'.\load_profiles.npz' # Pfad zur JSON-Datei anpassen + lf = cl.LoadForecast(filepath=filepath, year_energy=float(year_energy)) + specific_date_prices = lf.get_daily_stats('2024-02-16') + # Berechne den Tag des Jahres - day_of_year = date_obj.timetuple().tm_yday + #day_of_year = date_obj.timetuple().tm_yday # Konvertiere den Tag des Jahres in einen String, falls die Schlüssel als Strings gespeichert sind - day_key = int(day_of_year) + #day_key = int(day_of_year) #print(day_key) # Überprüfe, ob der Tag im Jahr in den Daten vorhanden ist - if day_key < len(load_profiles_exp): + array_list = lf.get_daily_stats(date_str) + pprint(array_list) + pprint(array_list.shape) + if array_list.shape == (2,24): + #if day_key < len(load_profiles_exp): # Konvertiere das Array in eine Liste für die JSON-Antwort + #((load_profiles_exp_l[day_key]).tolist(),(load_profiles_std_l)[day_key].tolist()) - array_list = ((load_profiles_exp_l[day_key]).tolist(),(load_profiles_std_l)[day_key].tolist()) - - return jsonify({date_str: array_list}) + return jsonify({date_str: array_list.tolist()}) else: return jsonify({"error": "Datum nicht gefunden"}), 404 except ValueError: