-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
1006 lines (863 loc) · 30 KB
/
utils.py
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json, math
from bs4 import BeautifulSoup
from requests import Response
import polars as pl
from datetime import datetime, date, time, timedelta, timezone
from typing import Dict, List
MONTH_MAPPING = {
"Ene": "01",
"Feb": "02",
"Mar": "03",
"Abr": "04",
"May": "05",
"Jun": "06",
"Jul": "07",
"Ago": "08",
"Sep": "09",
"Oct": "10",
"Nov": "11",
"Dic": "12",
}
INTERNAL_DATE_STR_FORMAT = "%d/%m/%Y"
INTERNAL_TIME_STR_FORMAT = "%H:%M:%S"
FRONT_END_DATE_FORMAT = "%A, %d de %B de %Y"
CONTRARIES = {"N": "S", "S": "N", "E": "W", "W": "E"}
def calculate_energy(wave_height, wave_period, width=1.0, water_density=1025):
g = 9.81
wavelength = (g * wave_period**2) / (2 * 3.14159)
energy_density = (1 / 8) * water_density * g * wave_height**2
energy_joules = energy_density * wavelength * width
energy_kj = energy_joules / 1000
return math.ceil(energy_kj)
def generate_energy(wave_heights: list, wave_periods: list):
return [
calculate_energy(wave_height=wave_height, wave_period=wave_period)
for wave_height, wave_period in zip(wave_heights, wave_periods)
]
def punta_mujeres_conditions(
wind_direction_predominant: str,
wave_direction_predominant: str,
wind_direction: str,
wave_direction: str,
wave_energy: int,
):
if punta_mujeres_favorable_wind(wind_direction_predominant, wind_direction) & (
punta_mujeres_low_wind_conditions(
wave_direction_predominant, wave_direction, wave_energy
)
):
return True
return False
def punta_mujeres_low_wind_conditions(
wave_direction_predominant: str,
wave_direction: str,
wave_energy: int,
):
punta_mujeres_wave_directions = ["N", "NE", "E", "NNW", "NNE", "NW"]
unwanted_wave_directions = ["WNW"]
if (
(
(wave_direction_predominant in punta_mujeres_wave_directions)
| (wave_direction in punta_mujeres_wave_directions)
)
& (wave_energy >= 1000)
& (wave_direction not in unwanted_wave_directions)
& (wave_direction_predominant not in unwanted_wave_directions)
):
return True
return False
def papagayo_conditions(
wind_direction_predominant: str,
wave_direction_predominant: str,
wind_direction: str,
wave_direction: str,
wave_energy: int,
tide_percentage: float,
):
if papagayo_favorable_wind(wind_direction_predominant, wind_direction) & (
papagayo_low_wind_conditions(
wave_direction_predominant, wave_direction, wave_energy, tide_percentage
)
):
return True
return False
def papagayo_low_wind_conditions(
wave_direction_predominant: str,
wave_direction: str,
wave_energy: int,
tide_percentage: float,
):
papagayo_wave_directions = [
"W",
"WNW",
]
if (
(
(wave_direction_predominant in papagayo_wave_directions)
| (wave_direction in papagayo_wave_directions)
)
& (wave_energy >= 900)
& (tide_percentage <= 50)
):
return True
return False
def west_swell_high_tide_conditions(
wind_direction_predominant: str,
wave_direction_predominant: str,
wind_direction: str,
wave_direction: str,
wave_energy: int,
tide_percentage: float,
):
if papagayo_favorable_wind(
wind_direction_predominant, wind_direction
) & west_swell_high_tide_low_wind_conditions(
wave_direction_predominant, wave_direction, wave_energy, tide_percentage
):
return True
return False
def west_swell_high_tide_low_wind_conditions(
wave_direction_predominant: str,
wave_direction: str,
wave_energy: int,
tide_percentage: float,
):
papagayo_wave_directions = [
"W",
"WNW",
]
if (
(
(wave_direction_predominant in papagayo_wave_directions)
| (wave_direction in papagayo_wave_directions)
)
& (wave_energy >= 900)
& (tide_percentage > 50)
):
return True
return False
def papelillo_conditions(
wind_direction_predominant: str,
wave_direction_predominant: str,
wind_direction: str,
wave_direction: str,
tide_percentage: float,
wave_energy: int,
):
if papelillo_favorable_high_wind(
wind_direction_predominant, wind_direction
) & papelillo_low_wind_conditions(
wave_direction_predominant, wave_direction, tide_percentage, wave_energy
):
return True
return False
def papelillo_low_wind_conditions(
wave_direction_predominant: str,
wave_direction: str,
tide_percentage: float,
wave_energy: int,
):
papelillo_wave_directions = [
"N",
"NW",
"NE",
]
if (
(
(wave_direction_predominant in papelillo_wave_directions)
| (wave_direction in papelillo_wave_directions)
)
& (tide_percentage <= 50)
& (wave_energy >= 100)
):
return True
return False
def caleta_caballo_low_wind_conditions(
wave_direction_predominant: str,
wave_direction: str,
wave_energy: int,
):
caleta_caballo_wave_directions = ["N", "NW", "NE"]
unwanted_wave_directions = ["WNW"]
if (
(
(wave_direction_predominant in caleta_caballo_wave_directions)
| (wave_direction in caleta_caballo_wave_directions)
)
& (wave_direction not in unwanted_wave_directions)
& (wave_energy >= 100 and wave_energy <= 1000)
):
return True
return False
def caleta_caballo_conditions(
wind_direction_predominant: str,
wave_direction_predominant: str,
wind_direction: str,
wave_direction: str,
wave_energy: int,
):
if caleta_caballo_favorable_wind(wind_direction_predominant, wind_direction) & (
caleta_caballo_low_wind_conditions(
wave_direction_predominant, wave_direction, wave_energy
)
):
return True
return False
def san_juan_low_wind_conditions(
wave_direction_predominant: str,
wave_direction: str,
wave_energy: int,
):
san_juan_wave_directions = ["N", "NW", "NE"]
unwanted_wave_directions = ["WNW"]
if (
(
(wave_direction_predominant in san_juan_wave_directions)
| (wave_direction in san_juan_wave_directions)
)
& (wave_direction not in unwanted_wave_directions)
& (wave_energy > 1000)
):
return True
return False
def san_juan_conditions(
wind_direction_predominant: str,
wind_direction: str,
wave_direction_predominant: str,
wave_direction: str,
wave_energy: int,
):
if san_juan_favorable_wind(wind_direction_predominant, wind_direction) & (
san_juan_low_wind_conditions(
wave_direction_predominant, wave_direction, wave_energy
)
):
return True
return False
def bastian_conditions(
wind_direction_predominant: str,
wave_direction_predominant: str,
wind_direction: str,
wave_direction: str,
wind_speed: float,
tide_percentage: float,
):
bastian_wind_directions = ["NE", "N"]
bastian_wave_directions = ["N", "NW", "NE", "E", "S"]
if (
(
(
(wind_direction_predominant in bastian_wind_directions)
| (wind_direction in bastian_wind_directions)
)
& (
(wave_direction_predominant in bastian_wave_directions)
| (wave_direction in bastian_wave_directions)
)
)
& (wind_speed >= 19.0)
& (tide_percentage >= 50)
):
return True
return False
def barcarola_conditions(
wind_direction_predominant: str,
wave_direction_predominant: str,
wind_direction: str,
wave_direction: str,
wind_speed: float,
wave_period: int,
tide_percentage: float,
):
barcarola_wind_directions = ["NE", "N"]
barcarola_wave_directions = ["N", "NW", "NE", "E", "S"]
if (
(
(
(wind_direction_predominant in barcarola_wind_directions)
| (wind_direction in barcarola_wind_directions)
)
& (
(wave_direction_predominant in barcarola_wave_directions)
| (wave_direction in barcarola_wave_directions)
)
)
& (wind_speed >= 19.0)
& (wave_period >= 10)
& (tide_percentage <= 50)
):
return True
return False
def lasanta_conditions(
wind_direction_predominant: str,
wave_direction_predominant: str,
wind_direction: str,
wave_direction: str,
wave_energy: int,
):
if lasanta_favorable_wind(
wind_direction_predominant, wind_direction
) & lasanta_low_wind_conditions(
wave_direction_predominant, wave_direction, wave_energy
):
return True
return False
def lasanta_low_wind_conditions(
wave_direction_predominant: str,
wave_direction: str,
wave_energy: int,
):
lasanta_wave_directions = ["N", "NW", "NE"]
unwanted_wave_directions = ["WNW"]
if (
(
(wave_direction_predominant in lasanta_wave_directions)
| (wave_direction in lasanta_wave_directions)
)
& (wave_direction not in unwanted_wave_directions)
) & (wave_energy >= 100 and wave_energy <= 1000):
return True
return False
def famara_conditions(
wind_direction_predominant: str,
wave_direction_predominant: str,
wind_direction: str,
wave_direction: str,
wave_energy: int,
):
if famara_favorable_wind(wind_direction_predominant, wind_direction) & (
famara_low_wind_conditions(
wave_direction_predominant, wave_direction, wave_energy
)
):
return True
return False
def famara_low_wind_conditions(
wave_direction_predominant: str,
wave_direction: str,
wave_energy: int,
):
famara_wave_directions = ["N", "NW", "NE"]
unwanted_wave_directions = ["WNW"]
if (
(
(wave_direction_predominant in famara_wave_directions)
| (wave_direction in famara_wave_directions)
)
& (wave_direction not in unwanted_wave_directions)
) & (wave_energy >= 100 and wave_energy <= 1000):
return True
return False
def big_waves_conditions(
wave_height: float, wave_direction: str, wave_direction_predominant: str
):
big_wave_directions = ["N", "NE", "E"]
unwanted_wave_directions = ["WNW"]
if (
(
(wave_direction_predominant in big_wave_directions)
| (wave_direction in big_wave_directions)
)
& (wave_direction not in unwanted_wave_directions)
& (wave_height > 2.5) # Si en 2.5 no hay nada, revisar en 3
):
return True
return False
def famara_favorable_wind(wind_direction_predominant, wind_direction):
famara_wind_directions = ["S"]
return is_favorable_wind(
wind_direction_predominant, wind_direction, famara_wind_directions
)
def east_favorable_wind(wind_direction_predominant, wind_direction):
east_wind_directions = ["E", "NE", "SE"]
return is_favorable_wind(
wind_direction_predominant, wind_direction, east_wind_directions
)
def lasanta_favorable_wind(wind_direction_predominant, wind_direction):
return east_favorable_wind(wind_direction_predominant, wind_direction)
def papagayo_favorable_wind(wind_direction_predominant, wind_direction):
return east_favorable_wind(wind_direction_predominant, wind_direction)
def papelillo_favorable_high_wind(wind_direction_predominant, wind_direction):
east_wind_directions = ["E", "SE"]
return is_favorable_wind(
wind_direction_predominant, wind_direction, east_wind_directions
)
def papelillo_favorable_low_wind(wind_direction_predominant, wind_direction):
east_wind_directions = ["NE", "E", "SE"]
return is_favorable_wind(
wind_direction_predominant, wind_direction, east_wind_directions
)
def punta_mujeres_favorable_wind(wind_direction_predominant, wind_direction):
punta_mujeres_wind_directions = ["N", "NW"]
return is_favorable_wind(
wind_direction_predominant, wind_direction, punta_mujeres_wind_directions
)
def san_juan_favorable_wind(wind_direction_predominant, wind_direction):
san_juan_wind_directions = ["S", "SW", "SE"]
return is_favorable_wind(
wind_direction_predominant, wind_direction, san_juan_wind_directions
)
def caleta_caballo_favorable_wind(wind_direction_predominant, wind_direction):
caleta_caballo_wind_directions = ["W", "SW", "WNW"]
return is_favorable_wind(
wind_direction_predominant, wind_direction, caleta_caballo_wind_directions
)
def is_favorable_wind(
wind_direction_predominant: str, wind_direction: str, target_wind_directions: str
):
if (wind_direction_predominant in target_wind_directions) | (
wind_direction in target_wind_directions
):
return True
return False
def get_low_wind_spot(
wind_direction_predominant,
wind_direction,
wave_direction_predominant,
wave_direction,
wave_energy,
tide_percentage,
):
if famara_low_wind_conditions(
wave_direction_predominant,
wave_direction,
wave_energy,
) and famara_favorable_wind(wind_direction_predominant, wind_direction):
return "Famara"
elif papagayo_low_wind_conditions(
wave_direction_predominant,
wave_direction,
wave_energy,
tide_percentage,
) and papagayo_favorable_wind(wind_direction_predominant, wind_direction):
return "Papagayo-Tiburón (Fuerza oeste - vacía)"
elif papelillo_low_wind_conditions(
wave_direction_predominant, wave_direction, tide_percentage, wave_energy
) and papelillo_favorable_low_wind(wind_direction_predominant, wind_direction):
return "Papelillo"
elif lasanta_low_wind_conditions(
wave_direction_predominant,
wave_direction,
wave_energy,
) and lasanta_favorable_wind(wind_direction_predominant, wind_direction):
return "La Santa"
elif san_juan_low_wind_conditions(
wave_direction_predominant, wave_direction, wave_energy
) and san_juan_favorable_wind(wind_direction_predominant, wind_direction):
return "San Juan - Cagao - El Muelle"
elif punta_mujeres_low_wind_conditions(
wave_direction_predominant, wave_direction, wave_energy
) and punta_mujeres_favorable_wind(wind_direction_predominant, wind_direction):
return "Punta Mujeres"
elif caleta_caballo_low_wind_conditions(
wave_direction_predominant, wave_direction, wave_energy
) and caleta_caballo_favorable_wind(wind_direction_predominant, wind_direction):
return "Caleta Caballo"
elif west_swell_high_tide_low_wind_conditions(
wave_direction_predominant,
wave_direction,
wave_energy,
tide_percentage,
):
return "Fuerza oeste - llena"
elif papagayo_low_wind_conditions(
wave_direction_predominant,
wave_direction,
wave_energy,
tide_percentage,
):
return "Papagayo-Tiburón (Fuerza oeste - vacía)"
elif punta_mujeres_low_wind_conditions(
wave_direction_predominant,
wave_direction,
wave_energy,
):
return "Punta Mujeres"
elif caleta_caballo_low_wind_conditions(
wave_direction_predominant,
wave_direction,
wave_energy,
):
return "Caleta Caballo"
elif lasanta_low_wind_conditions(
wave_direction_predominant,
wave_direction,
wave_energy,
):
return "La Santa"
elif famara_low_wind_conditions(
wave_direction_predominant,
wave_direction,
wave_energy,
):
return "Famara"
elif san_juan_low_wind_conditions(
wave_direction_predominant,
wave_direction,
wave_energy,
):
return "San Juan - Cagao - El Muelle"
else:
return "No Clasificado"
def is_low_wind(wind_speed: float) -> bool:
return wind_speed < 10.0
def generate_spot_name(
wind_direction_predominant: str,
wind_direction: str,
wind_speed: float,
tide_percentage: int,
wave_period: int,
wave_energy: int,
wave_direction: str,
wave_direction_predominant: str,
) -> str:
if is_low_wind(wind_speed=wind_speed):
spot = get_low_wind_spot(
wind_direction_predominant=wind_direction_predominant,
wind_direction=wind_direction,
wave_direction_predominant=wave_direction_predominant,
wave_direction=wave_direction,
wave_energy=wave_energy,
tide_percentage=tide_percentage,
)
return spot
# elif big_waves_conditions(
# wave_height=wh,
# wave_direction=wave_direction,
# wave_direction_predominant=wad_predominant,
# ):
# return "Olas grandes > 2.5m, revisar costa de playa honda"
elif barcarola_conditions(
wind_direction_predominant=wind_direction_predominant,
wave_direction_predominant=wave_direction_predominant,
wind_direction=wind_direction,
wave_direction=wave_direction,
wind_speed=wind_speed,
tide_percentage=tide_percentage,
wave_period=wave_period,
):
return "Barcarola"
elif bastian_conditions(
wind_direction_predominant=wind_direction_predominant,
wave_direction_predominant=wave_direction_predominant,
wind_direction=wind_direction,
wave_direction=wave_direction,
wind_speed=wind_speed,
tide_percentage=tide_percentage,
):
return "Bastián"
elif punta_mujeres_conditions(
wind_direction_predominant=wind_direction_predominant,
wave_direction_predominant=wave_direction_predominant,
wind_direction=wind_direction,
wave_direction=wave_direction,
wave_energy=wave_energy,
):
return "Punta Mujeres"
elif papagayo_conditions(
wind_direction_predominant=wind_direction_predominant,
wave_direction_predominant=wave_direction_predominant,
wind_direction=wind_direction,
wave_direction=wave_direction,
wave_energy=wave_energy,
tide_percentage=tide_percentage,
):
return "Papagayo-Tiburón (Fuerza oeste - vacía)"
elif west_swell_high_tide_conditions(
wind_direction_predominant=wind_direction_predominant,
wave_direction_predominant=wave_direction_predominant,
wind_direction=wind_direction,
wave_direction=wave_direction,
wave_energy=wave_energy,
tide_percentage=tide_percentage,
):
return "Fuerza oeste - llena"
elif papelillo_conditions(
wind_direction_predominant=wind_direction_predominant,
wave_direction_predominant=wave_direction_predominant,
wind_direction=wind_direction,
wave_direction=wave_direction,
tide_percentage=tide_percentage,
wave_energy=wave_energy,
):
return "Papelillo"
elif caleta_caballo_conditions(
wind_direction_predominant=wind_direction_predominant,
wave_direction_predominant=wave_direction_predominant,
wind_direction=wind_direction,
wave_direction=wave_direction,
wave_energy=wave_energy,
):
return "Caleta Caballo"
elif famara_conditions(
wind_direction_predominant=wind_direction_predominant,
wave_direction_predominant=wave_direction_predominant,
wind_direction=wind_direction,
wave_direction=wave_direction,
wave_energy=wave_energy,
):
return "Famara"
elif lasanta_conditions(
wind_direction_predominant=wind_direction_predominant,
wave_direction_predominant=wave_direction_predominant,
wind_direction=wind_direction,
wave_direction=wave_direction,
wave_energy=wave_energy,
):
return "La Santa"
elif san_juan_conditions(
wind_direction_predominant=wind_direction_predominant,
wind_direction=wind_direction,
wave_direction_predominant=wave_direction_predominant,
wave_direction=wave_direction,
wave_energy=wave_energy,
):
return "San Juan - Cagao - El Muelle"
else:
return "No Clasificado"
def generate_spot_names(forecast: Dict[str, list]) -> list:
spot_names = []
wind_direction_predominant = forecast["wind_direction_predominant"]
wave_direction_predominant = forecast["wave_direction_predominant"]
wind_direction = forecast["wind_direction"]
wave_direction = forecast["wave_direction"]
wind_speed = forecast["wind_speed"]
wave_period = forecast["wave_period"]
tide_percentage = forecast["tide_percentage"]
energy = forecast["energy"]
for wid_predominant, wad_predominant, wid, wad, ws, tp, wp, e in zip(
wind_direction_predominant,
wave_direction_predominant,
wind_direction,
wave_direction,
wind_speed,
tide_percentage,
wave_period,
energy,
):
spot_name = generate_spot_name(
wid_predominant, wid, ws, tp, wp, e, wad, wad_predominant
)
spot_names.append(spot_name)
return spot_names
def combine_df(df1, df2):
df = pl.concat([df1, df2])
return df
def convert_datestr_format(datestr):
day = int(datestr.split(" ")[-1].strip())
month_name = datestr.split(" ")[-2].strip()
month = MONTH_MAPPING[month_name]
current_year = datetime.now().year
date_obj = datetime(current_year, int(month), day)
return date_obj.strftime("%d/%m/%Y")
def create_date_name_column(dates: list) -> list:
date_names = []
today_dt = datetime.now().date()
tomorrow_dt = today_dt + timedelta(days=1)
day_after_tomorrow_dt = today_dt + timedelta(days=2)
yesterday_dt = today_dt - timedelta(days=1)
for date in dates:
if date.date() == today_dt:
date_names.append("Hoy")
elif date.date() == tomorrow_dt:
date_names.append("Mañana")
elif date.date() == day_after_tomorrow_dt:
date_names.append("Pasado")
elif date.date() == yesterday_dt:
date_names.append("Ayer")
else:
date_names.append("Otro día")
return date_names
def get_day_name(days_to_add: float) -> str:
today = date.today()
day = today + timedelta(days=days_to_add)
day_name_number = day.strftime("%m-%d, %A")
return day_name_number
def create_direction_predominant_column(directions: list) -> list:
return [get_predominant_direction(int(dir)) for dir in directions]
def final_forecast_format(df: pl.DataFrame):
if not df.is_empty():
df.sort(by=["date", "time", "spot_name"], descending=[True, True, True])
times = df["time"]
df = df.with_columns(pl.col("datetime").dt.convert_time_zone("UTC"))
datetimes = df["datetime"]
_6_AM = time(hour=6, minute=0, second=0, microsecond=0)
_19_PM = time(hour=19, minute=0, second=0, microsecond=0)
now = datetime.now(timezone.utc)
mask = (times >= _6_AM) & (times <= _19_PM) & (datetimes >= now)
df = df.filter(mask)
common_columns = [
"date_name",
"date_friendly",
"time_friendly",
"energy",
"wave_period",
"wind_direction",
"wind_speed",
"tide_percentage",
"nearest_tide",
"tide",
"datetime",
"date",
"time",
"time_graph",
"spot_name",
"wind_direction_predominant",
"wave_direction_predominant",
"wave_height",
"wave_direction",
"wind_direction_degrees",
"wave_direction_degrees",
]
df = df[common_columns]
return df
def degrees_to_direction(degrees):
directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
index = round(degrees / 45) % 8
return directions[index]
def datetime_to_frontend_str(dt: datetime) -> str:
return dt.strftime(FRONT_END_DATE_FORMAT).capitalize()
def construct_date_selection_list(
min_value: datetime, max_value: datetime, scraped_datetime_list: list
) -> list:
date_selection = []
for scraped_datetime in scraped_datetime_list:
if scraped_datetime >= min_value and scraped_datetime <= max_value:
date_selection.append(scraped_datetime)
return date_selection
def datestr_to_datetime(dtstr, format) -> datetime:
return datetime.strptime(dtstr, format)
def generate_tides(tide_data: list, forecast_datetimes: list) -> list:
tide_status_list = []
tides_datetimes_list = [item["datetime"] for item in tide_data]
tides_tide_list = [item["tide"] for item in tide_data]
for forecast_datetime in forecast_datetimes:
closest_datetime = min(
tides_datetimes_list, key=lambda dt: abs(dt - forecast_datetime)
)
closest_datetime_index = tides_datetimes_list.index(closest_datetime)
tide_status = tides_tide_list[closest_datetime_index]
try:
next_tide_datetime = tides_datetimes_list[closest_datetime_index + 1]
except IndexError:
# Marea alta y baja hay 6h y 12.5 min
next_tide_datetime = closest_datetime + timedelta(hours=6, minutes=12.5)
tide_hour = closest_datetime.time()
next_tide_hour = next_tide_datetime.time()
if forecast_datetime > closest_datetime:
status = "Bajando" if tide_status == "pleamar" else "Subiendo"
s = f"{status} hasta las {next_tide_hour.strftime('%H:%M')}"
elif forecast_datetime < closest_datetime:
status = "Subiendo" if tide_status == "pleamar" else "Bajando"
s = f"{status} hasta las {tide_hour.strftime('%H:%M')}"
else:
s = f"{tide_status} del todo a las {tide_hour.strftime('%H:%M')}"
tide_status_list.append(s)
return tide_status_list
def generate_nearest_tides(tide_data: dict, forecast_datetimes: dict) -> list:
nearest_tides = []
tides_datetimes_list = [item["datetime"] for item in tide_data]
tides_tide_list = [item["tide"] for item in tide_data]
for forecast_datetime in forecast_datetimes:
closest_datetime = min(
tides_datetimes_list, key=lambda dt: abs(dt - forecast_datetime)
)
closest_datetime_index = tides_datetimes_list.index(closest_datetime)
tide_status = tides_tide_list[closest_datetime_index]
tide_status = "Llena" if tide_status == "pleamar" else "Vacía"
nearest_tides.append(tide_status)
return nearest_tides
def generate_tide_percentages(tide_data: dict, forecast_datetimes: list) -> list:
tide_percentages = []
tides_datetimes_list = [item["datetime"] for item in tide_data]
tides_tide_list = [item["tide"] for item in tide_data]
for forecast_datetime in forecast_datetimes:
sorted_datetimes = sorted(
tides_datetimes_list, key=lambda dt: abs(dt - forecast_datetime)
)
closest_datetime_1 = sorted_datetimes[0]
closest_datetime_2 = sorted_datetimes[1]
closest_datetime_index_1 = tides_datetimes_list.index(closest_datetime_1)
if tides_tide_list[closest_datetime_index_1] == "pleamar":
high_tide_hour = closest_datetime_1
low_tide_hour = closest_datetime_2
else:
high_tide_hour = closest_datetime_2
low_tide_hour = closest_datetime_1
tide_percentage = calculate_tide_percentage(
high_tide_hour, forecast_datetime, low_tide_hour
)
tide_percentages.append(tide_percentage)
return tide_percentages
def find_next_tide(tides_datetimes_list, tides_tide_list, start_index, tide_type):
for i in range(start_index + 1, len(tides_datetimes_list)):
if tides_tide_list[i].lower() == tide_type:
return tides_datetimes_list[i]
raise ValueError(f"No {tide_type} found after index {start_index}")
from datetime import datetime, timedelta
def calculate_tide_percentage(
high_tide_hour: datetime, current_hour: datetime, low_tide_hour: datetime
) -> float:
if high_tide_hour > low_tide_hour:
total_cycle_duration = (high_tide_hour - low_tide_hour).total_seconds()
else:
total_cycle_duration = (low_tide_hour - high_tide_hour).total_seconds()
if current_hour < high_tide_hour:
deviation = (high_tide_hour - current_hour).total_seconds()
percentage = (deviation / total_cycle_duration) * 100
elif current_hour > low_tide_hour:
deviation = (current_hour - low_tide_hour).total_seconds()
percentage = 100 - (deviation / total_cycle_duration) * 100
else:
deviation = (current_hour - high_tide_hour).total_seconds()
percentage = (deviation / total_cycle_duration) * 100
percentage = max(0, min(100, percentage))
calculed_percentage = math.ceil(percentage)
returned_percentage = 100 - calculed_percentage
return returned_percentage
def get_predominant_direction(direction: float) -> str:
if direction == 0 or direction == 360:
return "N"
elif direction == 90:
return "E"
elif direction == 180:
return "S"
elif direction == 270:
return "W"
elif direction > 0 and direction < 90:
return "NE"
elif direction > 90 and direction < 180:
return "SE"
elif direction > 180 and direction < 270:
return "SW"
elif direction > 270 and direction < 360:
return "NW"
def degrees_to_direction(degrees: int) -> str:
compass_directions = [
"N",
"NNE",
"NE",
"ENE",
"E",
"ESE",
"SE",
"SSE",
"S",
"SSW",
"SW",
"WSW",
"W",
"WNW",
"NW",
"NNW",
]
sector_size = 360 / len(compass_directions)
degrees = degrees % 360
index = int((degrees + sector_size / 2) // sector_size) % len(compass_directions)
return compass_directions[index]
def generate_forecast_moments(initstamp, hours):