File size: 33,861 Bytes
60ef6dd
 
 
 
 
 
b63a760
 
 
 
 
 
 
 
25fa3a9
 
 
b63a760
30d1d68
86c9138
b63a760
 
 
 
 
 
8ad04d8
b63a760
 
 
a433b26
8ad04d8
b63a760
 
 
58ab263
8ad04d8
 
 
 
 
 
 
 
 
 
b63a760
8ad04d8
 
 
 
 
 
 
 
 
b63a760
8ad04d8
 
b63a760
8ad04d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b63a760
 
8ad04d8
 
 
 
 
 
b63a760
 
8ad04d8
 
b63a760
8ad04d8
 
b63a760
 
 
 
8ad04d8
 
 
 
a433b26
8ad04d8
b63a760
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a433b26
b63a760
 
 
0d86f55
 
 
 
 
 
 
 
 
 
 
 
 
b63a760
 
 
 
86c9138
 
29f59f7
86c9138
b63a760
bad9d6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b63a760
8ad04d8
 
 
b63a760
8ad04d8
 
 
b63a760
8ad04d8
bad9d6d
8ad04d8
 
 
 
58ab263
8ad04d8
993bbcb
58ab263
8ad04d8
 
91bd6f0
8ad04d8
 
 
b63a760
8ad04d8
 
 
 
a14e226
8ad04d8
a14e226
8ad04d8
a14e226
8ad04d8
 
a14e226
8ad04d8
 
 
 
 
 
a14e226
8ad04d8
 
a14e226
8ad04d8
 
b63a760
8ad04d8
 
 
91bd6f0
8ad04d8
 
 
 
b63a760
8ad04d8
 
 
993bbcb
8ad04d8
b63a760
8ad04d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b63a760
 
 
 
 
 
 
8ad04d8
b63a760
03b3284
5acc1b8
b63a760
03b3284
 
 
 
 
8ad04d8
03b3284
 
 
 
 
 
 
 
 
 
86c9138
 
 
b63a760
60ef6dd
a433b26
993bbcb
86c9138
 
b63a760
 
993bbcb
86c9138
 
a433b26
b63a760
 
58ab263
 
 
 
993bbcb
58ab263
 
 
 
 
 
b63a760
60ef6dd
 
6e2dd81
 
 
 
 
 
 
86c9138
a433b26
4ef9d0b
a433b26
 
6e2dd81
 
60ef6dd
6e2dd81
86c9138
a433b26
6e2dd81
 
86c9138
58ab263
a433b26
25fa3a9
 
5acc1b8
86c9138
 
5acc1b8
86c9138
 
 
5acc1b8
6e2dd81
86c9138
 
 
 
 
58ab263
86c9138
25fa3a9
60ef6dd
86c9138
5acc1b8
60ef6dd
86c9138
 
 
 
 
58ab263
25fa3a9
86c9138
 
a433b26
 
 
 
 
0f41898
82cce9f
 
 
 
 
0f41898
 
 
 
a433b26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ad04d8
a433b26
 
8ad04d8
a433b26
 
 
58ab263
 
 
a433b26
 
 
 
58ab263
a433b26
 
 
 
 
 
 
 
 
 
 
 
0f41898
a433b26
 
 
 
 
5acc1b8
a433b26
5acc1b8
a433b26
 
 
 
 
 
58ab263
 
 
 
a433b26
 
 
0f41898
5acc1b8
 
a433b26
 
 
 
 
 
 
 
 
 
 
 
5b21d40
a433b26
 
 
 
 
 
 
58ab263
 
 
 
 
a433b26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58ab263
 
a433b26
 
 
 
 
 
 
 
58ab263
a433b26
8ee33e1
a433b26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5acc1b8
a433b26
 
5acc1b8
a433b26
5acc1b8
a433b26
 
5acc1b8
a433b26
 
 
 
 
5acc1b8
 
 
 
a433b26
 
5acc1b8
 
a433b26
5acc1b8
 
a433b26
58ab263
5acc1b8
a433b26
25fa3a9
60ef6dd
f0ee368
25fa3a9
f0ee368
 
 
60ef6dd
 
 
 
 
 
 
 
 
f5cd3c0
5ef0e63
f5cd3c0
 
 
 
 
 
 
 
 
 
 
25fa3a9
6e2dd81
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
import streamlit as st
import requests
import pandas as pd
import json
import io
import datetime
import pandas as pd
import numpy as np
import requests
import base64
import json
from calendar import monthrange
import pymongo
from mongoengine import StringField, ListField, DateTimeField, DictField
import matplotlib.pyplot as plt
from matplotlib.dates import MonthLocator



def mongo_unavs_call(user_input_start_date, user_input_end_date, user_input_past_date):
    print("Starting mongo_unavs_call")
    # Connect to the MongoDB database
    user = "dmarroquin"
    passw = "tN9XpCCQM2MtYDme"
    host = "nucmonitordata.xxcwx9k.mongodb.net"
    client = pymongo.MongoClient(
        f"mongodb+srv://{user}:{passw}@{host}/?retryWrites=true&w=majority&connectTimeoutMS=100000"
    )

    db = client["data"]
    collection_past_unavs = db["unavs"]
    collection_unavs = db["unavs_update"]

    start_date = f"{user_input_start_date}T00:00:00"
    end_date = f"{user_input_end_date}T23:59:59"
    past_date = f"{user_input_past_date}T23:59:59"


    pipeline_v4 = [
        # 1) Expand each results element into its own doc
        { "$unwind": "$results" },

        # 2) Expand each generation_unavailabilities element
        { "$unwind": "$results.generation_unavailabilities" },

        # 3) Keep only those that match your fuel_type + date criteria
        {
            "$match": {
                "results.generation_unavailabilities.production_type": "NUCLEAR",
                "results.generation_unavailabilities.updated_date": { "$lte": past_date },
                "results.generation_unavailabilities.start_date": { "$lte": end_date },
                "results.generation_unavailabilities.start_date": { "$gte": start_date },
                "results.generation_unavailabilities.end_date": { "$gte": start_date },
                "results.generation_unavailabilities.end_date": { "$lte": end_date }

            }
        },

        # 4) Replace the entire document with just that sub-doc
        {
            "$replaceRoot": {
                "newRoot": "$results.generation_unavailabilities"
            }
        }
    ]


    pipeline_v6 = [
        # 1) Expand each results element into its own doc
        { "$unwind": "$results" },

        # 2) Expand each generation_unavailabilities element
        { "$unwind": "$results.generation_unavailabilities" },

        # 3) Keep only those that match your fuel_type + date criteria
        {
            "$match": {
                "results.generation_unavailabilities.fuel_type": "NUCLEAR",
                "results.generation_unavailabilities.publication_date": { "$lte": past_date },
                "results.generation_unavailabilities.start_date": { "$lte": end_date },
                # "results.generation_unavailabilities.start_date": { "$gte": start_date },
                "results.generation_unavailabilities.end_date": { "$gte": start_date },
                # "results.generation_unavailabilities.end_date": { "$lte": end_date }
            }
        },

        # 4) Replace the entire document with just that sub-doc
        {
            "$replaceRoot": {
                "newRoot": "$results.generation_unavailabilities"
            }
        }
    ]

    result1 = list(collection_past_unavs.aggregate(pipeline_v4))
    result2 = list(collection_unavs.aggregate(pipeline_v4))
    result_v6 = list(collection_unavs.aggregate(pipeline_v6))
    merge_results = result1 + result2 + result_v6
    
    return merge_results

# --------------------------------------------------------------------------------------- #

# Convert the dictionary of dictionaries to JSON
def convert_to_json(item):
    if isinstance(item, dict):
        return {str(k): convert_to_json(v) for k, v in item.items()}
    elif isinstance(item, list):
        return [convert_to_json(i) for i in item]
    elif isinstance(item, ObjectId):
        return str(item)
    else:
        return item
# --------------------------------------------------------------------------------------- #

# Function gives the total of the data. When printed as dataframe/excel,
# Will give a final row with the total for each plant and the total overall
def add_total(data):
    total_values = {}
    for key in data:
        daily_values = data[key]
        total = sum(daily_values.values())
        daily_values["Total"] = total
        for date, value in daily_values.items():
            if date not in total_values:
                total_values[date] = value
            else:
                total_values[date] += value
        
    data["Total"] = total_values

# --------------------------------------------------------------------------------------- #

def nuc_monitor(usr_start_date, usr_end_date, past_date, mongo_db_data):
    # # Slightly changed metadata to fit the data from the RTE API: ST-LAURENT B 2 --> ST LAURENT 2, ....

    plants_metadata = {"BELLEVILLE 1": 1310.0, "BELLEVILLE 2": 1310.0, "BLAYAIS 1": 910.0, "BLAYAIS 2": 910.0, 
                    "BLAYAIS 3": 910.0, "BLAYAIS 4": 910.0, "BUGEY 2": 910.0, "BUGEY 3": 910.0, "BUGEY 4": 880.0, 
                    "BUGEY 5": 880.0, "CATTENOM 1": 1300.0, "CATTENOM 2": 1300.0, "CATTENOM 3": 1300.0, 
                    "CATTENOM 4": 1300.0, "CHINON 1": 905.0, "CHINON 2": 905.0, "CHINON 3": 905.0, 
                    "CHINON 4": 905.0, "CHOOZ 1": 1500.0, "CHOOZ 2": 1500.0, "CIVAUX 1": 1495.0, 
                    "CIVAUX 2": 1495.0, "CRUAS 1": 915.0, "CRUAS 2": 915.0, "CRUAS 3": 915.0, "CRUAS 4": 915.0, 
                    "DAMPIERRE 1": 890.0, "DAMPIERRE 2": 890.0, "DAMPIERRE 3": 890.0, "DAMPIERRE 4": 890.0, 
                    "FLAMANVILLE 1": 1330.0, "FLAMANVILLE 2": 1330.0,  "FLAMANVILLE 3": 1620.0, "GOLFECH 1": 1310.0, "GOLFECH 2": 1310.0, 
                    "GRAVELINES 1": 910.0, "GRAVELINES 2": 910.0, "GRAVELINES 3": 910.0, "GRAVELINES 4": 910.0, 
                    "GRAVELINES 5": 910.0, "GRAVELINES 6": 910.0, "NOGENT 1": 1310.0, "NOGENT 2": 1310.0, 
                    "PALUEL 1": 1330.0, "PALUEL 2": 1330.0, "PALUEL 3": 1330.0, "PALUEL 4": 1330.0, "PENLY 1": 1330.0, 
                    "PENLY 2": 1330.0, "ST ALBAN 1": 1335.0, "ST ALBAN 2": 1335.0, "ST LAURENT 1": 915.0, 
                    "ST LAURENT 2": 915.0, "TRICASTIN 1": 915.0, "TRICASTIN 2": 915.0, "TRICASTIN 3": 915.0, 
                    "TRICASTIN 4": 915.0, "FESSENHEIM 1": 0.0, "FESSENHEIM 2": 0.0}

    # --------------------- INITIAL DATA CLEANING FOR MONGO DATA ------------------------ #    

    # # Create a DataFrame
    # mongo_data = mongo_unavs_call(usr_start_date, usr_end_date, past_date)
    # mongo_data = get_mongodb_data(usr_start_date, usr_end_date, past_date)
    # print(mongo_db_data)
    mongo_df = pd.DataFrame(mongo_db_data)

    # the five columns you care about
    _optional = {
        "updated_date",
        "type",
        "production_type",
        "unit",
        "status",
    }

    # see which of those actually exist
    present = set(mongo_df.columns) & _optional

    if _optional.issubset(mongo_df.columns):
        # 0. first, pick the columns you know exist, plus the always‑present ones
        cols = [
            "identifier", "version", "message_id",
            "values", "publication_date", "unavailability_type", "fuel_type",
            "affected_asset_or_unit_name", "affected_asset_or_unit_installed_capacity",
            "event_status"
        ] + list(_optional)
        mongo_df = mongo_df[cols]

        # 1. normalize “unit”
        unit_expanded = pd.json_normalize(mongo_df["unit"])
        mongo_df_2 = pd.concat([mongo_df.drop(columns=["unit"]), unit_expanded], axis=1)

        # 2. normalize the first element of “values”
        mongo_df_2["values_first"] = (
            mongo_df_2["values"]
            .apply(lambda lst: lst[0] if isinstance(lst, list) and lst else {})
        )
        values_expanded = pd.json_normalize(mongo_df_2["values_first"])
        mongo_df_2 = pd.concat(
            [mongo_df_2.drop(columns=["values", "values_first"]), values_expanded],
            axis=1
        )

        # 3. coalesce and drop old cols
        mongo_df_2["fuel_type"] = mongo_df_2["fuel_type"].combine_first(mongo_df_2["production_type"])
        mongo_df_2["publication_date"] = mongo_df_2["publication_date"].combine_first(mongo_df_2["updated_date"])
        mongo_df_2["event_status"] = mongo_df_2["event_status"].combine_first(mongo_df_2["status"])
        mongo_df_2["affected_asset_or_unit_installed_capacity"] = (
            mongo_df_2["affected_asset_or_unit_installed_capacity"]
            .combine_first(mongo_df_2["installed_capacity"])
        )
        mongo_df_2["affected_asset_or_unit_name"] = (
            mongo_df_2["affected_asset_or_unit_name"]
            .combine_first(mongo_df_2["name"])
        )
        mongo_df_2["unavailability_type"] = (
            mongo_df_2["unavailability_type"]
            .combine_first(mongo_df_2["type"].iloc[:, 0])
        )

        drop_cols = [
            "production_type",
            "updated_date",
            "status",
            "installed_capacity",
            "name",
            "type",
            "eic_code",
        ]
        # only drop those that are actually there
        drop_cols = [c for c in drop_cols if c in mongo_df_2.columns]
        mongo_df_2 = mongo_df_2.drop(columns=drop_cols)

        # now mongo_df_2 is your final
    else:
        # at least one of the required columns is missing:
        # present contains the ones you did find
        missing = _optional - present
        print(f"Skipping normalize process because these columns are missing: {missing}")
        mongo_df_2 = mongo_df.copy()  # or however you want to proceed

        mongo_df_2["values_first"] = mongo_df_2["values"].apply(
            lambda lst: lst[0] if isinstance(lst, list) and len(lst) > 0 else {}
        )

        # 2. Normalize that dict into separate columns
        values_expanded = pd.json_normalize(mongo_df_2["values_first"])
        #    e.g. this produces columns like “start_date”, “end_date”, etc.

        # 3. Concatenate back and drop the originals
        mongo_df_2 = pd.concat(
            [
                mongo_df_2.drop(columns=["values", "values_first", "start_date", "end_date"]),
                values_expanded
            ],
            axis=1
        )

    # Convert the date columns to datetime objects
    for col in ["publication_date", "start_date", "end_date"]:
        mongo_df_2[col] = pd.to_datetime(mongo_df_2[col], utc=True)

    # # Now convert everything to French time (CET/CEST):
    # for col in ["publication_date", "start_date", "end_date"]:
    #     mongo_df_2[col] = mongo_df_2[col].dt.tz_convert("Europe/Paris")

    # mongo_df_2 = mongo_df_2.drop_duplicates(subset='identifier', keep='first')
    mongo_df_2['version'] = mongo_df_2['version'].astype(float)
    # Sort by identifier and version to ensure the latest version is at the top
    # Method 1: Use groupby + idxmax to pick the row with the largest version per identifier
    idx = mongo_df_2.groupby("identifier")["version"].idxmax()
    mongo_df_2 = mongo_df_2.loc[idx].reset_index(drop=True)

    mongo_df_2 = mongo_df_2[mongo_df_2['event_status'] != 'DISMISSED']


    # Create the final dataframe
    final_df = pd.DataFrame()

    # Create the date column, with date range from start_date to end_date in daily granularity
    final_df['Date'] = pd.date_range(start=usr_start_date, end=usr_end_date, freq='D')
    final_df['Date'] = [ts.strftime("%Y-%m-%d") for ts in final_df['Date']]

    # For each plant create a new column with the plant name
    for plant, capacity in plants_metadata.items():
        # Create a new column for each plant
        final_df[plant] = np.nan  # Initialize with zeros

    mongo_df_3 = mongo_df_2.copy()

    dates_of_interest = list(pd.date_range(start=usr_start_date, end=usr_end_date, freq="D"))

    # Now convert each Timestamp → “YYYY-MM-DD” string:
    dates_of_interest = [ts.strftime("%Y-%m-%d") for ts in dates_of_interest]

    mongo_df_3['start_day'] = mongo_df_3['start_date'].dt.day
    mongo_df_3['start_hour'] = mongo_df_3['start_date'].dt.hour
    mongo_df_3['start_minute'] = mongo_df_3['start_date'].dt.minute
    mongo_df_3['end_day'] = mongo_df_3['end_date'].dt.day
    mongo_df_3['end_hour'] = mongo_df_3['end_date'].dt.hour
    mongo_df_3['end_minute'] = mongo_df_3['end_date'].dt.minute

    # mongo_df_3 = mongo_df_3.sort_values(by=['publication_date'], ascending=False)
    mongo_df_3 = mongo_df_3.sort_values(by=['publication_date'])

    # results_plants = {plant_name: {date: {"available_capacity": power, "publication_date": pd.to_datetime("1970-01-01", utc=True)} for date in dates_of_interest}
    #                 for plant_name, power in plants_metadata.items()}

    results_plants = {plant_name: {date: {"available_capacity": power, "publication_date": pd.to_datetime("1970-01-01", utc=True)} 
                                for date in dates_of_interest}
                                    for plant_name, power in plants_metadata.items()}

    for row in mongo_df_3.itertuples():
        # Get the start and end dates for the unavailability
        row_start_date = str(row.start_date.date())
        row_end_date = str(row.end_date.date())
        
        # Get the plant name and capacity
        plant_name = row.affected_asset_or_unit_name
        plant_capacity = plants_metadata.get(plant_name, 0)  # Default to 0 if not found

        results_current_plant = results_plants[plant_name]

        power_unavailability = row.available_capacity
        publication_date_unav = row.publication_date
        
        for day in dates_of_interest:
            # percentage_of_day = results_current_plant[day]["percentage_of_day"]


            if row_start_date <= day <= row_end_date:
                
                # Check if the day is already updated with a later (more recent) update_date; by sorting the DataFrame by publication_date, 
                # we ensure that the latest unavailability is applied
                # Get the percentage of day that the plant is unavailable

                # if day in final_df['Date'] and pd.notna(final_df.loc[final_df['Date'] == day, plant_name]).any():
                if (day in results_current_plant) and (publication_date_unav <= results_current_plant[day]["publication_date"]):
                    # If the plant's capacity for that day is already set, skip to the next day
                    continue

                # The unavailability starts and ends on the same day
                if row_start_date == day and day == row_end_date:
                    percentage_of_day = (row.end_hour * 60 + row.end_minute - row.start_hour * 60 - row.start_minute) / (24 * 60)
                    # results_current_plant[day]["percentage_of_day"] += percentage_of_day
                    # power_of_day = percentage_of_day * row.available_capacity + (1 - percentage_of_day) * plant_capacity
                    # final_df.loc[final_df['Date'] == day, plant_name] = power_of_day

                # The unavailability starts on the current day but ends on a later day
                elif row_start_date == day and day < row_end_date:
                    percentage_of_day = (24 * 60 - (row.start_hour * 60 + row.start_minute)) / (24 * 60)
                    # results_current_plant[day]["percentage_of_day"] += percentage_of_day

                    # power_of_day = percentage_of_day * row.available_capacity + (1 - percentage_of_day) * plant_capacity
                    # final_df.loc[final_df['Date'] == day, plant_name] = power_of_day

                # # The unavailability starts on a previous day and ends on the current day
                elif row_end_date == day and row_start_date < day:
                    percentage_of_day = (row.end_hour * 60 + row.end_minute) / (24 * 60)
                    # results_current_plant[day]["percentage_of_day"] += percentage_of_day

                    # power_of_day = percentage_of_day * row.available_capacity + (1 - percentage_of_day) * plant_capacity
                    # final_df.loc[final_df['Date'] == day, plant_name] = power_of_day
                
                else:
                    # The unavailability starts on a previous day and ends on a later day
                    percentage_of_day = 1
                    # power_of_day = percentage_of_day * row.available_capacity + (1 - percentage_of_day) * plant_capacity
                    # final_df.loc[final_df['Date'] == day, plant_name] = power_of_day

                power_of_day = percentage_of_day * power_unavailability + (1 - percentage_of_day) * plant_capacity

                # Update the available_capacity for the day only if it's not already updated with a later update_date
                if (day not in results_current_plant):
                    results_current_plant[day] = {"available_capacity": power_of_day, "publication_date": publication_date_unav}
                
                elif (day in results_current_plant) and (publication_date_unav > results_current_plant[day]["publication_date"]) \
                        and (power_of_day < results_current_plant[day]['available_capacity']):
                    # results_current_plant[day]["available_capacity"] *= power_of_day
                    # results_current_plant[day]["publication_date"] = publication_date_unav

                    results_current_plant[day] = {"available_capacity": power_of_day, "publication_date": publication_date_unav}

                else:
                    continue

    output_results = {}
    for plant, plant_data in results_plants.items():
        available_capacity_per_day = {str(date): data["available_capacity"] for date, data in plant_data.items()}
        output_results[plant] = available_capacity_per_day

    add_total(output_results)

    output_results = {plant: {str(date): power for date, power in plant_data.items()} for plant, plant_data in output_results.items()}
    output_results = pd.DataFrame(output_results)

    # -------------------------------------------------
    # Calculate the average of each column excluding the last row
    averages = output_results.iloc[:-1, :].mean()

    # Replace the last row with the calculated averages
    output_results.iloc[-1, :] = averages

    output_results = output_results.to_dict()

    def turn_total_row_to_avg(data):
        # Replace the last key of each dictionary with 'Averages'
        for key, value in data.items():
            last_key = list(value.keys())[-1]
            value['Averages'] = value.pop(last_key)

    turn_total_row_to_avg(output_results)

    json_data = json.dumps(output_results)
    # print(json_data)
    return json_data
    # -------------------------------------------------


# @st.cache_data
def get_mongodb_data(start_date, end_date, past_date):
    database_data = mongo_unavs_call(start_date, end_date, past_date)
    return database_data

# @st.cache_data
def get_nucmonitor_data(start_date, end_date, past_date):
    mongo = get_mongodb_data(start_date, end_date, past_date)
    response_nucmonitor = nuc_monitor(start_date, end_date, past_date, mongo)
    # nucmonitor_data = response_nucmonitor.json()
    # nucmonitor_json = json.loads(nucmonitor_data)
    # print(response_nucmonitor)
    df = pd.read_json(response_nucmonitor)
    return df

# @st.cache_data
def get_photodate_data(start_date, end_date, past_date):
    mongo = get_mongodb_data(start_date, end_date, past_date)
    response_nucmonitor = nuc_monitor(start_date, end_date, past_date, mongo)
    # nucmonitor_data = response_nucmonitor.json()
    # nucmonitor_json = json.loads(nucmonitor_data)
    # print(response_nucmonitor)
    df = pd.read_json(response_nucmonitor)
    return df

def run_app():

    st.title("Nucmonitor App")

    # Get user input (e.g., dates)
    start_date = st.date_input("Start Date")
    end_date = st.date_input("End Date")
    past_date = st.date_input("Cutoff Date")
    # winter_date = st.date_input("Winter Cutoff Date")

    current_date = datetime.datetime.now()
    
    with st.form("nucmonitor_form"):
        submitted = st.form_submit_button("Get Nucmonitor")

    if not submitted:
        st.write("Form not submitted")
    
    else:
        st.write("Data received from Flask:")
        df_nucmonitor = get_nucmonitor_data(start_date, end_date, current_date)
        df_photo_date = get_photodate_data(start_date, end_date, past_date)
        # df_winter_date = get_nucmonitor_data(start_date, end_date, winter_date)
        current_date_str = str(current_date.strftime('%Y-%m-%d'))
        past_date_str = str(past_date.strftime('%Y-%m-%d'))
        st.write(f"Current View Forecast at {current_date_str} (MW)")
        st.write(df_nucmonitor)  # Display DataFrame
        
        st.write(f"Past View Forecast at {past_date_str}")
        st.write(df_photo_date)

        # Get info on current forecast Nucmonitor
        st.write(f"Total Energy per Day at Current View Forecast {current_date_str} (MW)")
        
        # Remove the final row 'Total'
        df_nucmonitor_2 = df_nucmonitor.iloc[:-1, :]
        # Get the last column
        df_nucmonitor_2 = df_nucmonitor_2.iloc[:, -1]
        
        # print(df_nucmonitor_2)

        st.write(df_nucmonitor_2)

        # Get info on past date forecast Nucmonitor
        st.write(f"Total Energy per Day at Past View Forecast {past_date_str} (MW)")
        
        # Remove the final row 'Total'
        df_photo_date_2 = df_photo_date.iloc[:-1, :]
        # Get the last column
        df_photo_date_2 = df_photo_date_2.iloc[:, -1]
        
        # print(df_photo_date_2)

        st.write(df_photo_date_2)

# --------------------------------- AVERAGE EXPECTED AVAILABILITY M-1 M M+1 M+2 PIPELINE --------------------------------- #

        # Create a Table that displays the forecast of each dataframe total for two months before date and two months after
        # Filter dates for two months before and after the current date
        # Define date ranges
        # I am under the impression that I will need to use past_date for the offset
        # two_months_before = (current_date - pd.DateOffset(months=2)).strftime('%Y-%m')
        # one_month_before = (current_date - pd.DateOffset(months=1)).strftime('%Y-%m')
        # one_month_after = (current_date + pd.DateOffset(months=1)).strftime('%Y-%m')
        # two_months_after = (current_date + pd.DateOffset(months=2)).strftime('%Y-%m')

        two_months_before = (current_date - pd.DateOffset(months=2)).strftime('%Y-%m')
        one_month_before = (current_date - pd.DateOffset(months=1)).strftime('%Y-%m')
        one_month_after = (current_date + pd.DateOffset(months=1)).strftime('%Y-%m')
        two_months_after = (current_date + pd.DateOffset(months=2)).strftime('%Y-%m')

        # Assuming df is the DataFrame containing the date index and the 'Total' column

        # # Convert the index to datetime if it's not already
        # df_nucmonitor_2.index = pd.to_datetime(df_nucmonitor_2.index)
        # df_photo_date_2.index = pd.to_datetime(df_photo_date_2.index)

        # # Calculate monthly averages with date in yyyy-mm format
        # monthly_average_nucmonitor = df_nucmonitor_2.resample('M').mean()
        # monthly_average_photo_date = df_photo_date_2.resample('M').mean()

        # Convert the index to datetime if it's not already
        df_nucmonitor_2.index = pd.to_datetime(df_nucmonitor_2.index)
        df_photo_date_2.index = pd.to_datetime(df_photo_date_2.index)

        # Calculate monthly averages with date in yyyy-mm format
        monthly_average_nucmonitor = df_nucmonitor_2.resample('ME').mean()
        monthly_average_nucmonitor.index = monthly_average_nucmonitor.index.strftime('%Y-%m')

        monthly_average_photo_date = df_photo_date_2.resample('ME').mean()
        monthly_average_photo_date.index = monthly_average_photo_date.index.strftime('%Y-%m')


        # print(monthly_average_nucmonitor)
        # print(monthly_average_nucmonitor.index)
        # print(len(monthly_average_nucmonitor.index) < 5)
        if (len(monthly_average_nucmonitor.index) < 5) or (two_months_before not in monthly_average_nucmonitor.index or two_months_after not in monthly_average_nucmonitor.index):
            df_display_normal_bool = False

        else:
            # print(two_months_before, one_month_before, current_date.strftime('%Y-%m'), one_month_after, two_months_after)
            # Filter DataFrames based on date ranges
            df_nucmonitor_filtered = monthly_average_nucmonitor[
                (monthly_average_nucmonitor.index == two_months_before) |
                (monthly_average_nucmonitor.index == one_month_before) |
                (monthly_average_nucmonitor.index == current_date.strftime('%Y-%m')) |
                (monthly_average_nucmonitor.index == one_month_after) |
                (monthly_average_nucmonitor.index == two_months_after)
            ]

            df_photo_date_filtered = monthly_average_photo_date[
                (monthly_average_photo_date.index == two_months_before) |
                (monthly_average_photo_date.index == one_month_before) |
                (monthly_average_photo_date.index == current_date.strftime('%Y-%m')) |
                (monthly_average_photo_date.index == one_month_after) |
                (monthly_average_photo_date.index == two_months_after)
            ]

            # Display the filtered DataFrames
            st.write(f"Forecast at {current_date_str} (MW)")
            st.write(df_nucmonitor_filtered)
            st.write(f"Forecast at {past_date_str} (MW)")
            st.write(df_photo_date_filtered)

            current_forecast_update = df_nucmonitor_filtered.tolist()
            past_forecast_update = df_photo_date_filtered.tolist()
            delta = [current - past for current, past in zip(current_forecast_update, past_forecast_update)]

            # print('Dates:', [two_months_before, one_month_before, current_date.strftime('%Y-%m'), one_month_after, two_months_after])
            # print(f"Forecast update {current_date_str}", current_forecast_update)
            # print(f"Forecast update {past_date_str}", past_forecast_update,)
            # print('Delta', delta)

            # Create a DataFrame for display
            data_avg_expected_normal = {
                'Dates': [two_months_before, one_month_before, current_date.strftime('%Y-%m'), one_month_after, two_months_after],
                f"Forecast update {current_date_str} (MW)": current_forecast_update,
                f"Forecast update {past_date_str} (MW)": past_forecast_update,
                'Delta': delta
            }
            df_display_normal_bool = True

# --------------------------------- AVERAGE EXPECTED AVAILABILITY M-1 M M+1 M+2 PIPELINE --------------------------------- #

# --------------------------------- AVERAGE EXPECTED AVAILABILITY WINTER PIPELINE --------------------------------- #
        # Create a Table that displays the forecast of each dataframe for the Winter months (Nov, Dec, Jan, Feb, Mar)

        # Create a table that gets the forecast for winter. This involves creating a new dataframe with
        # only the winter months with the total of each day, and another dataframe with the average of each month. Each month
        # included will only be 20xx-11, 12, and 20xx+1-01, 02, 03
        
        # Define date ranges for winter months
        # winter_start_date = current_date.replace(month=11, day=1)
        # winter_end_date = (current_date.replace(year=current_date.year+1, month=3, day=31))
        winter_start = f"{current_date.year}-11"
        winter_end = f"{current_date.year+1}-03"
        winter_start_str = str(winter_start)
        winter_end_str = str(winter_end)
        # print("winter_start_str", winter_start)
        # print("winter_end_str", winter_end)
        # print("monthly_average_nucmonitor.index", monthly_average_nucmonitor.index)
        # print(monthly_average_nucmonitor.index == winter_start)
        # print(monthly_average_nucmonitor.index == winter_end)
        if monthly_average_nucmonitor.index.any() != winter_start or monthly_average_nucmonitor.index.any() != winter_end:
            df_display_winter_bool = False

        else:
            # Filter DataFrames based on winter date range
            df_nucmonitor_winter = monthly_average_nucmonitor[(monthly_average_nucmonitor.index >= winter_start_str) & (monthly_average_nucmonitor.index <= winter_end_str)]

            df_photo_date_winter = monthly_average_photo_date[(monthly_average_photo_date.index >= winter_start_str) & (monthly_average_photo_date.index <= winter_end_str)]

            # Display the forecast DataFrames for winter
            st.title("Forecast for Winter Months")
            st.write(f"Forecast for {current_date.year}-{current_date.year+1} (Nov, Dec, Jan, Feb, Mar)")
            st.write("Nucmonitor Forecast:")
            st.write(df_nucmonitor_winter)
            st.write("Photo Date Forecast:")
            st.write(df_photo_date_winter)
            
            current_winter_forecast_update = df_nucmonitor_winter.tolist()
            past_winter_forecast_update = df_photo_date_winter.tolist()
            winter_delta = [current - past for current, past in zip(current_winter_forecast_update, past_winter_forecast_update)]
            # print("current_winter_forecast_update:", current_winter_forecast_update)
            # print("past_winter_forecast_update:", past_winter_forecast_update)

            # Create a DataFrame for display
            data_avg_expected_winter = {
                'Dates': [f'Nov-{current_date.year}', f'Dec-{current_date.year}', f'Jan-{current_date.year+1}', f'Feb-{current_date.year+1}', f'Mar-{current_date.year+1}'],
                f"Forecast update {current_date_str}": current_winter_forecast_update,
                f"Forecast update {past_date_str}": past_winter_forecast_update,
                'Delta': winter_delta
            }
            # print(data_avg_expected_winter)
            df_display_winter_bool = True

# --------------------------------- AVERAGE EXPECTED AVAILABILITY WINTER PIPELINE --------------------------------- #

# --------------------------------- VISUALIZE --------------------------------- #
        if df_display_normal_bool:
            df_display_normal = pd.DataFrame(data_avg_expected_normal)
            # Display the DataFrame as a horizontal table
            st.write("Table 1. Average expected availability on the French nuclear fleet (MW) - M-1, M, M+1, M+2, M+3")
            st.table(df_display_normal)
        
        if df_display_winter_bool:
            df_display_winter = pd.DataFrame(data_avg_expected_winter)
            st.write(f"Table 2. Average expected availability on the French nuclear fleet (MW) - Winter {winter_start}/{winter_end}")
            st.table(df_display_winter)

        # Line charts of the forecasts (need to combine them so they appear in the same chart)
        st.write("Current forecast (MW)")
        st.line_chart(df_nucmonitor_2)

        st.write("Previous forecast (MW)")
        st.line_chart(df_photo_date_2)
        # Create a new dataframe out of df_nucmonitor_2 call real_avail that contains df_nucmonitor_2 up until current_date

        # Slice the DataFrame to include data up until current_date
        real_avail = df_nucmonitor_2.loc[df_nucmonitor_2.index <= current_date_str]

        # Winter forecast still not the correct one, this is just a placeholder
        # winter_forecast = df_nucmonitor_2.loc[(df_nucmonitor_2.index >= winter_start_date) & (df_nucmonitor_2.index <= winter_end_date)]
        
        # Optionally, if you want to reset the index
        # real_avail = real_avail.reset_index()
        # print(real_avail)
        st.write("Observed Historical Availability (MW)")
        st.line_chart(real_avail)

        # Combine dataframes
        # combined_df = pd.concat([df_nucmonitor_2, df_photo_date_2, real_avail, winter_forecast], axis=1)
        combined_df = pd.concat([df_nucmonitor_2, df_photo_date_2, real_avail], axis=1)

        # combined_df.columns = [f'Forecast {current_date_str}', f'Forecast {past_date_str}', 'Observed Historical Availability', f'Winter forecast {winter_start}/{winter_end}']
        combined_df.columns = [f'Forecast {current_date_str} (MW)', f'Forecast {past_date_str} (MW)', 'Observed Historical Availability (MW)']

        # print(combined_df)
        st.write(f"Graph 1. {start_date} to {end_date} (MW)")
        st.line_chart(combined_df)

        # Add a download button
        # Create a BytesIO object to hold the Excel data

        excel_buffer = io.BytesIO()

    
        current_datetime = datetime.datetime.now()
        current_year = current_datetime.strftime('%Y')
        current_month = current_datetime.strftime('%m')
        current_day = current_datetime.strftime('%d')
        current_hour = current_datetime.strftime('%H')
        current_minute = current_datetime.strftime('%M')
        current_second = current_datetime.strftime('%S')


        # Save the DataFrame to the BytesIO object as an Excel file
        df_nucmonitor.to_excel(excel_buffer, index=True)
        # Set the cursor position to the beginning of the BytesIO object
        excel_buffer.seek(0)

        # Provide the BytesIO object to the download button
        download_button = st.download_button(
            label="Download Excel",
            data=excel_buffer,
            file_name=f"nucmonitor_data_{current_year}-{current_month}-{current_day}-h{current_hour}m{current_minute}s{current_second}.xlsx",
            mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        )


if __name__ == '__main__':
    run_app()