From cb2bc705335233f97de4cd0751e800ba51a7a8bc Mon Sep 17 00:00:00 2001
From: Michel Spils <msp@informatik.uni-kiel.de>
Date: Thu, 24 Oct 2024 14:04:44 +0200
Subject: [PATCH] zrxp export function

---
 configs/db_new.yaml     |  3 +++
 src/predict_database.py |  5 +++--
 src/utils/db_tools.py   | 24 ++++++++++++++++++++++++
 3 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/configs/db_new.yaml b/configs/db_new.yaml
index a70db6f..08bed34 100644
--- a/configs/db_new.yaml
+++ b/configs/db_new.yaml
@@ -2,12 +2,15 @@ main_config:
   max_missing : 120
   db_params:
     user: c##mspils
+    #Passwort ist für eine lokale Datenbank ohne verbindung zum internet. Kann ruhig online stehen.
     password: cobalt_deviancy
     dsn: localhost/XE
   # Auch als Liste möglich
   zrxp_folder : ../data/dwd_ens_zrpx/
   sensor_folder : ../data/db_in/
   zrxp_out_folder : ../data/zrxp_out/
+  #If True exports the forecasts if successful to zrxp
+  export_zrxp : True
   #if True tries to put  all files in folder in the external forecast table
   load_sensor : False
   load_zrxp : True
diff --git a/src/predict_database.py b/src/predict_database.py
index 19207e7..28e8c34 100644
--- a/src/predict_database.py
+++ b/src/predict_database.py
@@ -34,6 +34,7 @@ def get_configs(passed_args: argparse.Namespace) -> Tuple[dict, List[dict]]:
     if isinstance(main_config["zrxp_folder"], str):
         main_config["zrxp_folder"] = [main_config["zrxp_folder"]]
     main_config["zrxp_folder"] = list(map(Path, main_config["zrxp_folder"]))
+    main_config["zrxp_out_folder"] = Path(main_config["zrxp_out_folder"])
     main_config["sensor_folder"] = Path(main_config["sensor_folder"])
     main_config["start"] = pd.to_datetime(main_config["start"])
     if "end" in main_config:
@@ -98,8 +99,8 @@ def parse_args() -> argparse.Namespace:
         help="The start date for the prediction. Format: YYYY-MM-DD HH:MM, overwrites start/end/range in the config file.",
     )
     parser.add_argument(
-        "--zrxp", action="store_true", help="Save predictions as ZRXP files (not yet implemented)"
-    ) #TODO: Implement ZRXP
+        "--zrxp", action="store_true", help="Save predictions as ZRXP files"
+    )
     return parser.parse_args()
 
 
diff --git a/src/utils/db_tools.py b/src/utils/db_tools.py
index 7692012..2601a25 100644
--- a/src/utils/db_tools.py
+++ b/src/utils/db_tools.py
@@ -431,6 +431,30 @@ class OracleWaVoConnection:
             fcst_values = {f"h{i}": None for i in range(1, 49)}
         else:
             fcst_values = {f"h{i}": forecast[i - 1].item() for i in range(1, 49)}
+            
+            if self.main_config.get("export_zrxp"):
+                target_file =self.main_config["zrxp_out_folder"] / f"{end_time.strftime("%Y%m%d%H")}_{sensor_name}_{model_name}_{member}.zrx"
+                #2023 11 19 03
+                df_zrxp = pd.DataFrame(forecast,columns=["value"])
+                df_zrxp["timestamp"] = end_time
+                df_zrxp["forecast"] = pd.date_range(start=end_time,periods=49,freq="1h")[1:]
+                df_zrxp["member"] = member        
+                df_zrxp = df_zrxp[['timestamp','forecast','member','value']]
+
+
+                with open(target_file , 'w', encoding="utf-8") as file:
+                    file.write('#REXCHANGEWISKI.' + model_name.split("_")[0] + '.W.KNN|*|\n')
+                    file.write('#RINVAL-777|*|\n')
+                    file.write('#LAYOUT(timestamp,forecast, member,value)|*|\n')
+
+                df_zrxp.to_csv(path_or_buf = target_file,
+                            header = False,
+                            index=False,
+                            mode='a',
+                            sep = ' ',
+                            date_format = '%Y%m%d%H%M')
+
+
 
         stmt = select(PegelForecasts).where(
             PegelForecasts.tstamp == bindparam("tstamp"),
-- 
GitLab