-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin_SWOT_CMEMS_processing.py
1829 lines (1406 loc) · 80.9 KB
/
join_SWOT_CMEMS_processing.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 numpy as np
import matplotlib.pyplot as plt
import xarray as xr
import pandas as pd
import os
import cartopy.crs as ccrs
import warnings
import cartopy.feature as cfeature
from tqdm import tqdm
import statsmodels.api as sm # for LOWESS filter
# Set the warning filter to "ignore" to suppress all warnings
warnings.filterwarnings("ignore")
import loess_smooth_handmade as loess # for LOESS filter
import math
from sklearn.preprocessing import MinMaxScaler
# ----------------------------------PARAMETERS------------------------------------------------------------------------
# Strategy for selecting CMEMS data points around tide gauge locations
# 0: Average nearby SSH values within the radius
# 1: Select the closest SSH value within the radius
strategy = 0
# Maximum distance from each CMEMS point to the tide gauge location
dmedia = np.arange(100, 101, 1) # Array of distances from 5 to 110 km in 5 km increments
# Window size for the LOESS filter (in days)
day_window = 7
# % max of NaNs to drop stations 90-(90*0.2) = 72
min_valid_vals = 70
# Function for calculating the Haversine distance between two points
def haversine(lon1, lat1, lon2, lat2):
# convert decimal degrees to radians
lon1 = np.deg2rad(lon1)
lon2 = np.deg2rad(lon2)
lat1 = np.deg2rad(lat1)
lat2 = np.deg2rad(lat2)
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = np.sin(dlat/2)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2)**2
c = 2 * np.arcsin(np.sqrt(a))
r = 6371
return c * r
# Function for calculating the Z-scores of a series and removing outliers
def calculate_z_scores(series):
mean = series.mean()
std = series.std()
return (series - mean) / std
threshold_outliers = 5 # Threshold for Z-scores to remove outliers
def process_ts(time_series, product_n):
"""
Process the given time series by standarizing, removing outliers and smooth the data.
"""
ts_mean = time_series[product_n].mean()
ts_demean = time_series[product_n] - ts_mean
time_series['demean'] = ts_demean
# z_scores = calculate_z_scores(time_series['demean'])
# time_series = time_series[np.abs(z_scores) <= threshold_outliers]
# Apply a LOESS filter to the time series
frac_loess = 1 / day_window
time_series['demean_filtered'] = loess.loess_smooth_handmade(time_series['demean'].values, frac_loess)
if len(time_series) < min_valid_vals:
empty_stations.append(station)
print(f"Station {sorted_names[station]} has more than 20% of NaNs")
# Drop time series which have more than 20% of NaNs
return time_series if len(time_series) >= min_valid_vals else None
def compute_combined_rmsd(rmsds, threshold):
"""Calculate the mean of the RMSD's values that are below a threshold
taking in account the non-linearity of the RMSD values."""
# Step 1: Square each RMSD value and filter by threshold
squared_rmsd = [x**2 for x in rmsds if x < threshold]
# Step 2: Compute the mean of the squared RMSD values
mean_squared_rmsd = sum(squared_rmsd)/ len(rmsds)
# Step 3: Take the square root of the mean
combined_rmsd = math.sqrt(mean_squared_rmsd)
return combined_rmsd
# Function to plot the time series of a tide gauge and a altimetry products
def plot_time_series(alt_ts, tg_ts, rmsd, correlation, product_name, station, rad, plot_path):
plt.figure(figsize=(8, 6))
demean = 'demean_filtered'
plt.plot(alt_ts['time'], alt_ts[demean], label=f'{product_name}', linewidth=3, color='b')
plt.plot(alt_ts['time'], alt_ts['demean'], label=f'{product_name} unfiltered', linestyle='--', color='b', alpha=0.5)
plt.plot(tg_ts['time'], tg_ts[demean], label='TGs', linewidth=3, color='g')
plt.plot(tg_ts['time'], tg_ts['demean'], label='TGs unfiltered', linestyle='--', color='g', alpha=0.5)
plt.title(f'{names_tg_short_sorted[station]}, {rad}km rad, {day_window}dLoess, {product_name}', fontsize=18)
plt.legend(fontsize=8, loc='upper left')
plt.xticks(rotation=20)
plt.yticks(np.arange(-15, 18, 3))
plt.grid(True, alpha=0.2)
plt.ylabel('SSHA (cm)', fontsize=12)
plt.tick_params(axis='both', which='major', labelsize=12)
# Use LaTeX-like syntax to set cm^2 as superscript
plt.text(0.02, 0.80, f'RMSD: {rmsd:.2f} cm$^2$', fontsize=8, color='black',
transform=plt.gca().transAxes, ha='left', bbox=dict(facecolor='white', alpha=0.2))
plt.text(0.02, 0.75, f'CORRELATION: {correlation:.2f}', fontsize=8, color='black',
transform=plt.gca().transAxes, ha='left', bbox=dict(facecolor='white', alpha=0.2))
plt.savefig(f'{plot_path}{sorted_names[station]}_{rad}km_{day_window}dLoess_{product_name}.png')
plt.show()
# Function to calculate the errors of combined RMSD values
def bootstrap_rmsd(data, num_bootstrap=1000):
bootstrap_samples = np.random.choice(data, (num_bootstrap, len(data)), replace=True)
bootstrap_rmsd = np.mean(bootstrap_samples, axis=1)
return bootstrap_rmsd
names_tg_short_sorted = ['Porquerolles', 'La Capte', 'Les Oursinieres', 'Saint Louis Mourillon',
'Toulon', 'Baie Du Lazaret', 'Port De St.Elme', 'Tamaris', 'Bregaillon',
'Le_Brusc', 'La Ciotat', 'Cassis', 'Marseille', 'Port de la Redonne',
'Port De Carro', 'Fos Sur Mer', 'Mahon', 'Porto Cristo',
'Palma de Mallorca', 'Barcelona', 'Tarragona']
# Path to the general SWOT data folder -----------------------------------------------------------------------------------
path ='/home/dvega/anaconda3/work/SWOT/'
# ALTIMETRY DATA PRODUCTS PATHS ----------------------------------------------------------------------------------------
# Define the products to process
products = [
{ # DUACS (SWOT L4) GLOBAL
'folder_path': f'{path}SWOT_L4/',
'plot_path': f'{path}figures/radius_comparisons_SWOT_L4/',
'product_name': 'DUACS (SWOT_L4)'
},
{ # Near real time (NRT) EUROPE
'folder_path': f'{path}CMEMS_data/SEALEVEL_EUR_PHY_L4_NRT_008_060/daily',
'plot_path': f'{path}figures/radius_comparisons_CMEMS_EUR_NRT/',
'product_name': 'CMEMS_NRT_EUR'
},
{ # Near real time (NRT) GLOBAL
'folder_path': f'{path}CMEMS_data/SEALEVEL_GLO_PHY_L4_NRT_008_046',
'plot_path': f'{path}figures/radius_comparisons_CMEMS_GLO_NRT/',
'product_name': 'CMEMS_NRT_GLO'
},
{ # SWOT L3
'folder_path' : f'{path}swot_basic_1day/003_016_passv1.0/', # Folder of 016 and 003 passes
'plot_path': f'{path}figures/radius_comparisons_rmsdCorrected_7dLoess_SWOT/',
'product_name': 'SWOT L3'},
# { # SWOT L3 UNSMOOTHED
# 'folder_path' : f'{path}ftp_data/unsmoothed/', # Folder of 016 and 003 unsmoothed passes
# 'plot_path': f'{path}figures/radius_comparisons_rmsdCorrected_7dLoess_SWOT_unsmoothed/',
# 'product_name': 'SWOT L3 unsmoothed'
# }
]
products_names = [product['product_name'] for product in products]
# tide gauge data paths ---------------------------------------------------
data_tg = np.load(f'{path}mareografos/TGresiduals1d_2023_European_Seas_SWOT_FSP.npz')
names_tg = pd.read_csv(f'{path}mareografos/GLOBAL_TGstations_CMEMS_SWOT_FSP_Feb2024', header=None)
# # Dropping wrong tide gauges (errors in tide gauge raw data)
drop_tg_names = ['station_GL_TS_TG_TamarisTG',
'station_GL_TS_TG_BaieDuLazaretTG',
'station_GL_TS_TG_PortDeCarroTG',
'station_GL_TS_TG_CassisTG',
'station_MO_TS_TG_PORTO-CRISTO']
# ---------------------------------- READING TIDE GAUGE DATA --------------------------------------------------------
sla = data_tg.get('resdacTOTday') * 100 # Convert to centimeters (cm)
time = data_tg.get('timeTOTday')
lat_tg = data_tg.get('latTOT')
lon_tg = data_tg.get('lonTOT')
# Convert datenum into datetime
epoch_start = pd.Timestamp('1970-01-01')
time_days = np.array(time, dtype='timedelta64[D]') # Convert 'time' to a timedelta array
time_datetime = epoch_start.to_numpy() + time_days
data_arrays = []
for i in range(lat_tg.size): # Assuming lat_tg and lon_tg are 1D arrays with length 21
# Create a DataArray for each station with its corresponding time and SLA values
station_name = names_tg[0][i]
da = xr.DataArray(
data=sla[i, :], # SLA data for the i-th station
dims=["time"],
coords={
"time": time_datetime[i, :], # Time data for the i-th station
"latitude": lat_tg[i],
"longitude": lon_tg[i]
},
name=f"station_{station_name}"
)
data_arrays.append(da)
# Order by longitude from east to west:
# Pair each station name with its corresponding longitude
name_longitude_pairs = [(da.name, da.longitude.item()) for da in data_arrays]
# Sort the pairs by longitude in descending order
sorted_pairs = sorted(name_longitude_pairs, key=lambda x: x[1], reverse=True)
# Extract the sorted names
sorted_names = [name for name, _ in sorted_pairs]
sorted_names_short = [
"Porquerolles",
"La Capte",
"Les Oursinieres",
"Saint Louis Mourillon",
"Port De St.Elme",
"Bregaillon",
"Le Brusc",
"La Ciotat",
"Port De La Redonne",
"Mahon",
"Palma de Mallorca",
"Barcelona",
"Tarragona"
]
# Setting the names_tg per longitude order from east to west
# Create a new list to store ordered data arrays
ordered_data_arrays = []
ordered_lat = []
ordered_lon = []
# Loop through the sorted station names and get the corresponding DataArray
for name in sorted_names:
for da in data_arrays:
if da.name == name:
ordered_data_arrays.append(da)
ordered_lat.append(np.float64(da['latitude'].values))
ordered_lon.append(np.float64(da['longitude'].values))
break # Exit the inner loop once the matching DataArray is found
# Replace the original data_arrays list with the ordered version
data_arrays = ordered_data_arrays
# Create a DataFrame to store tide gauge data
df_tg = []
# Iterate over each DataArray
for da in data_arrays:
# Extracting values from the DataArray
time_values = da["time"].values
sla_values = da.values
station_name = da.name # Extracting station name from DataArray name
latitude = da.latitude.values
longitude = da.longitude.values
# Create a DataFrame for the current DataArray
tg_data = pd.DataFrame({
'time': time_values,
'ssha': sla_values,
'station': [station_name] * len(time_values),
'latitude': latitude,
'longitude': longitude
})
# Append the DataFrame to the list
df_tg.append(tg_data)
# Concatenate all DataFrames into a single DataFrame
df_tg = pd.concat(df_tg, ignore_index=True) # Keep NaNs for calculating the percentage of NaNs per station
# Drop wrong stations from the tide gauge data
df_tg = df_tg[~df_tg['station'].isin(drop_tg_names)].reset_index(drop=True)
df_tg.set_index('time', inplace=True)
df_tg.sort_index(inplace=True)
df_tg_dropna = df_tg.dropna(how='any')
# ------------------------ PROCESSING CMEMS DATA AROUND TG LOCATIONS --------------------------------------------------
lolabox = [-2, 8, 35, 45]
results_rad_comparison = [] # List to store results for each radius
# Create empty lists to store errors for each radius
errors_swot_l3 = []
errors_cmems_eur = []
errors_cmems_glo = []
errors_duacs_swot_l4 = []
# Loop through each radius size
for rad in dmedia:
print(f'Beggining processing radius {rad} km')
products_timeseries = [] # List to store time series for each product
# Processing each product for each radius size
for product in products:
print(f'Processing product {product["product_name"]}')
all_altimetry_timeseries = [] # List to store all CMEMS time series (one list per radius size)
folder_path = product['folder_path']
plot_path = product['plot_path']
product_name = product['product_name']
# Loop through all netCDF files in the product folder
nc_files = [f for f in os.listdir(folder_path) if f.endswith('.nc')]
for filename in tqdm(nc_files):
file_path = os.path.join(folder_path, filename)
if product_name in ['SWOT L3', 'SWOT L3 unsmoothed']: # ------------------------------------------------------------------------------------------
ds = xr.open_dataset(file_path)
# Extract data from variables
lon = ds['longitude'].values.flatten()
lat = ds['latitude'].values.flatten()
ssh = ds['ssha_noiseless'].values.flatten()
# ssh = ds['ssha'].values.flatten()
time_values = ds['time'].values # Adding a new
time = np.tile(time_values[:, np.newaxis], (1, 69)).flatten() # Not efficient
# Find indices of non-NaN values
valid_indices = np.where(~np.isnan(ssh))
lon = lon[valid_indices]
lat = lat[valid_indices]
time = time[valid_indices]
ssh = ssh[valid_indices]
# Loop through each tide gauge location
for idx, (gauge_lon, gauge_lat) in enumerate(zip(ordered_lon, ordered_lat)):
# Calculate distance for each data point
distances = haversine(lon, lat, gauge_lon, gauge_lat)
# Find indices within the specified radius
in_radius = distances <= rad
# Average nearby SSH values (if any)
if np.any(in_radius):
ssh_tmp = np.nanmean(ssh[in_radius])
ssh_serie = ssh_tmp * 100 # Convert to centimeters (cm)
time_serie = time[in_radius][~np.isnan(time[in_radius])][0] # Picking the first value of time within the radius
# Store the latitudes and longitudes of SWOT within the radius
lat_within_radius = lat[in_radius]
lon_within_radius = lon[in_radius]
# Store closest distance and number of points used for the average
min_distance_point = distances[in_radius].min()
n_idx = sum(in_radius) # How many values are used for compute the mean value
else:
ssh_serie = np.nan # No data within radius (remains NaN)
time_serie = time[in_radius][~np.isnan(time[in_radius])]
n_idx = np.nan # Number of points for the average within the radius
min_distance_point = np.nan
# If there's no SWOT data within the radius, set latitudes and longitudes to None
lat_within_radius = None
lon_within_radius = None
# print(f"No SWOT data within {dmedia} km radius of tide gauge {sorted_names[idx]}")
# Create a dictionary to store tide gauge and SWOT data
selected_data = {
"station": sorted_names[idx], # Access station name
"longitude": gauge_lon, # Longitude of tide gauge
"latitude": gauge_lat, # Latitude of tide gauge
"ssha": ssh_serie, # Retrieved SSH value
"ssha_raw": ssh[in_radius], # Raw SSH values within the radius
"time": time_serie,
"n_val": n_idx, # Number of points for the average within the radius
"lat_within_radius": lat_within_radius, # Latitudes of SWOT within the radius
"lon_within_radius": lon_within_radius, # Longitudes of SWOT within the radius
"min_distance": min_distance_point, # Closest distance within the radius
"product": product_name # Product name
}
all_altimetry_timeseries.append(selected_data)
else:
# DUACS PRODUCTS ----------------------------------------------------------------------------------------------------
ds_all = xr.open_dataset(file_path)
# Select all latitudes and the last 370 longitudes
ds = ds_all.sel(latitude=slice(lolabox[2], lolabox[3]), longitude=slice(lolabox[0], lolabox[1]))
time = ds['time'].values[0]
ssh = ds['sla'][0, :, :]
lat = ds['latitude']
lon = ds['longitude']
# Loop through each tide gauge location
for tg_idx, (tg_lon, tg_lat) in enumerate(zip(ordered_lon, ordered_lat)):
# Calculate distances
distances = haversine(lon, lat, tg_lon, tg_lat)
mask = distances <= rad # Create a mask to select points within the radius
# Convert the mask to a numpy array to use it for indexing
in_radius = np.array(mask)
# Apply the mask to the ssh array
ssh_serie_within_rad = ssh.values[in_radius]
# Average nearby SSH values (if any)
if np.any(in_radius):
# Apply the mask to get boolean indexes for all dimensions
ssh_indexes = np.where(in_radius)
# Use the boolean indexes to subset ssh, lat, and lon
ssh_series = ssh[ssh_indexes].values
ssh_serie = np.nanmean(ssh_serie_within_rad) * 100 # Convert to centimeters (cm)
lat_within_radius = lat.values[ssh_indexes[0]]
lon_within_radius = lon.values[ssh_indexes[1]]
n_idx = np.sum(in_radius) # How many values are used for computing the mean value
min_distance = np.min(distances.values[in_radius]) # Store minimum distance within the radius
else:
ssh_serie = np.nan # No data within radius (remains NaN)
# Create a dictionary to store tide gauge and CMEMS data
selected_data = {
"station": sorted_names[tg_idx], # Access station name
"longitude": tg_lon, # Longitude of tide gauge
"latitude": tg_lat, # Latitude of tide gauge
"ssha": ssh_serie, # Retrieved SSH values
"ssha_raw": ssh_series.flatten(), # Raw SSH values within the radius
"time": time,
"n_val": n_idx, # Number of points within the radius
"lat_within_radius": lat_within_radius,
"lon_within_radius": lon_within_radius,
"min_distance": min_distance, # Mean distance between CMEMS and tide gauge
"product": product_name # Product name
}
# Append the selected data to the list
all_altimetry_timeseries.append(selected_data)
products_timeseries.extend(all_altimetry_timeseries)
# CONVERT TO DATAFRAME for easier managing
prod_df = pd.DataFrame(products_timeseries)
prod_df['time'] = prod_df['time'].apply(lambda x: pd.NaT if isinstance(x, np.ndarray) and x.size == 0 else x) # Convert empty arrays to NaT
prod_df_dropna = pd.DataFrame(products_timeseries).dropna(how='any')
# for i in range(len(sorted_names)): # CHECKING HOW MANY NANS THERE ARE ACCORDING TO THE RADIUS SIZE
# print((df2[df2['station_name'] == sorted_names[i]]['ssha']).isna().sum())
prod_df_dropna = prod_df_dropna[~prod_df_dropna['station'].isin(drop_tg_names)].reset_index(drop=True)
prod_df = prod_df[~prod_df['station'].isin(drop_tg_names)].reset_index(drop=True)
# # Convert from Series of ndarrays containing dates to Series of timestamps
# prod_df_dropna['time'] = prod_df_dropna['time'].apply(lambda x: x[0])
# Obtain the time overlapping time index that is not NaNs
# Droped NaNs----------------------------------------------------------------------------------------------------------------
prod_df_dropna['time'] = pd.to_datetime(prod_df_dropna['time'])
prod_df_dropna.sort_values(by='time', inplace=True)
# Round SWOT time to nearest day
prod_df_dropna['time'] = prod_df_dropna['time'].dt.floor('d')
prod_df_dropna.set_index('time', inplace=True)
# Crop the time series to the overlapping period
DUACS_SWOT_L4_dropna = prod_df_dropna[prod_df_dropna['product']==products_names[0]]
CMEMS_NRT_EUR_dropna = prod_df_dropna[prod_df_dropna['product']==products_names[1]]
CMEMS_NRT_GLO_dropna = prod_df_dropna[prod_df_dropna['product']==products_names[2]]
SWOT_L3_dropna = prod_df_dropna[prod_df_dropna['product']==products_names[3]]
# SWOT_L3_unsmoothed_dropna = prod_df_dropna[prod_df_dropna['product']==products_names[4]]
# min_time = max(DUACS_SWOT_L4_dropna.index.min(), CMEMS_NRT_EUR_dropna.index.min(), CMEMS_NRT_GLO_dropna.index.min(), SWOT_L3_dropna.index.min(), SWOT_L3_unsmoothed_dropna.index.min(), df_tg_dropna.index.min())
# max_time = min(DUACS_SWOT_L4_dropna.index.max(), CMEMS_NRT_EUR_dropna.index.max(), CMEMS_NRT_GLO_dropna.index.max(), SWOT_L3_dropna.index.max(), SWOT_L3_unsmoothed_dropna.index.max(), df_tg_dropna.index.max())
min_time = max(DUACS_SWOT_L4_dropna.index.min(), CMEMS_NRT_EUR_dropna.index.min(), CMEMS_NRT_GLO_dropna.index.min(), SWOT_L3_dropna.index.min(), df_tg_dropna.index.min())
max_time = min(DUACS_SWOT_L4_dropna.index.max(), CMEMS_NRT_EUR_dropna.index.max(), CMEMS_NRT_GLO_dropna.index.max(), SWOT_L3_dropna.index.max(), df_tg_dropna.index.max())
# Containing NaNs-----------------------------------------------------------------------------------------------------------
prod_df['time'] = pd.to_datetime(prod_df['time'])
prod_df.sort_values(by='time', inplace=True)
# Round SWOT time to nearest day
prod_df['time'] = prod_df['time'].dt.floor('d')
prod_df.set_index('time', inplace=True)
# Crop the time series of each product to the overlapping period
DUACS_SWOT_L4 = prod_df[prod_df['product']==products_names[0]]
CMEMS_NRT_EUR = prod_df[prod_df['product']==products_names[1]]
CMEMS_NRT_GLO = prod_df[prod_df['product']==products_names[2]]
SWOT_L3 = prod_df[prod_df['product']==products_names[3]]
# SWOT_L3_unsmoothed = prod_df[prod_df['product']==products_names[4]]
DUACS_SWOT_L4 = DUACS_SWOT_L4[['station', 'ssha', 'min_distance', 'n_val']]
CMEMS_NRT_EUR = CMEMS_NRT_EUR[['station', 'ssha', 'min_distance', 'n_val']]
CMEMS_NRT_GLO = CMEMS_NRT_GLO[['station', 'ssha', 'min_distance', 'n_val']]
SWOT_L3 = SWOT_L3[['station', 'ssha', 'min_distance', 'n_val']]
# SWOT_L3_unsmoothed = SWOT_L3_unsmoothed[['station', 'ssha', 'min_distance', 'n_val']]
df_tg_p1 = df_tg[['station', 'ssha']]
DUACS_SWOT_L4 = DUACS_SWOT_L4[(DUACS_SWOT_L4.index >= min_time) & (DUACS_SWOT_L4.index <= max_time)]
CMEMS_NRT_EUR = CMEMS_NRT_EUR[(CMEMS_NRT_EUR.index >= min_time) & (CMEMS_NRT_EUR.index <= max_time)]
CMEMS_NRT_GLO = CMEMS_NRT_GLO[(CMEMS_NRT_GLO.index >= min_time) & (CMEMS_NRT_GLO.index <= max_time)]
SWOT_L3 = SWOT_L3[(SWOT_L3.index >= min_time) & (SWOT_L3.index <= max_time)]
# SWOT_L3_unsmoothed = SWOT_L3_unsmoothed[(SWOT_L3_unsmoothed.index >= min_time) & (SWOT_L3_unsmoothed.index <= max_time)]
df_tg_p2 = df_tg_p1[(df_tg_p1.index >= min_time) & (df_tg_p1.index <= max_time)]
DUACS_SWOT_L4 = DUACS_SWOT_L4.rename(columns={'ssha': 'DUACS (SWOT_L4)', 'min_distance':"min_distance_duacs_swot_l4", 'n_val': 'n_val_duacs_swot_l4'})
CMEMS_NRT_EUR = CMEMS_NRT_EUR.rename(columns={'ssha': 'CMEMS_NRT_EUR', 'min_distance': 'min_distance_cmems_eur', 'n_val': 'n_val_cmems_eur'})
CMEMS_NRT_GLO = CMEMS_NRT_GLO.rename(columns={'ssha': 'CMEMS_NRT_GLO', 'min_distance': 'min_distance_cmems_glo', 'n_val': 'n_val_cmems_glo'})
SWOT_L3 = SWOT_L3.rename(columns={'ssha': 'SWOT L3', 'min_distance': 'min_distance_swot_l3', 'n_val': 'n_val_swot_l3'})
# SWOT_L3_unsmoothed = SWOT_L3_unsmoothed.rename(columns={'ssha': 'SWOT L3 unsmoothed', 'min_distance': 'min_distance_swot_l3_unsmoothed', 'n_val': 'n_val_swot_l3_unsmoothed'})
df_tg_p3 = df_tg_p2.rename(columns={'ssha': 'TG'})
# MERGE ALL DATAFRAMES-----------------------------------------------------------------------------------------------------
# Reset index
df_tg_reset = df_tg_p3.reset_index()
SWOT_L3_reset = SWOT_L3.reset_index()
CMEMS_NRT_EUR_reset = CMEMS_NRT_EUR.reset_index()
CMEMS_NRT_GLO_reset = CMEMS_NRT_GLO.reset_index()
DUACS_SWOT_L4_reset = DUACS_SWOT_L4.reset_index()
# SWOT_L3_unsmoothed_reset = SWOT_L3_unsmoothed.reset_index()
matched_df = df_tg_reset.merge(SWOT_L3_reset, left_on=['time', 'station'], right_on=['time', 'station'])
matched_df = matched_df.merge(CMEMS_NRT_EUR_reset, left_on=['time', 'station'], right_on=['time', 'station'])
matched_df = matched_df.merge(CMEMS_NRT_GLO_reset, left_on=['time', 'station'], right_on=['time', 'station'])
matched_df = matched_df.merge(DUACS_SWOT_L4_reset, left_on=['time', 'station'], right_on=['time', 'station'])
# matched_df = matched_df.merge(SWOT_L3_unsmoothed_reset, left_on=['time', 'station'], right_on=['time', 'station'])
# Drop rows which have more than 20% of valid values 90*0.2 = 18
# ---------------- MANAGING COMPARISON BETWEEN TG AND SWO ------------------------------------------------
empty_stations = []
correlations_swot_l3 = []
correlations_cmems_eur = []
correlations_cmems_glo = []
correlations_duacs_swot_l4 = []
correlations_swot_l3_unsmoothed = []
rmsds_swot_l3 = []
rmsds_cmems_eur = []
rmsds_cmems_glo = []
rmsds_duacs_swot_l4 = []
rmsds_swot_l3_unsmoothed = []
variances_tg = []
variances_swot_l3 = []
variances_cmems_eur = []
variances_cmems_glo = []
variances_duacs_swot_l4 = []
variances_swot_l3_unsmoothed = []
variances_diff_swot_l3 = []
variances_diff_cmems_eur = []
variances_diff_cmems_glo = []
variances_diff_duacs_swot_l4 = []
variances_diff_swot_l3_unsmoothed = []
days_used_per_gauge_swot_l3 = []
days_used_per_gauge_cmems_eur = []
days_used_per_gauge_cmems_glo = []
days_used_per_gauge_duacs_swot_l4 = []
days_used_per_gauge_swot_l3_unsmoothed = []
min_distances_swot_l3 = []
min_distances_cmems_eur = []
min_distances_cmems_glo = []
min_distances_duacs_swot_l4 = []
min_distances_swot_l3_unsmoothed = []
n_val_swot_l3 = []
n_val_cmems_eur = []
n_val_cmems_glo = []
n_val_duacs_swot_l4 = []
n_val_swot_l3_unsmoothed = []
lolabox = [1, 8, 35, 45]
# Define the column name for the demeaned values according if the data is filtered or not
# demean = 'demean'
demean = 'demean_filtered'
idx_tg = np.arange(len(sorted_names))
for station in idx_tg:
try:
ssh_station = matched_df[matched_df['station'] == sorted_names[station]].copy() # Corrected warnings
if ssh_station.empty: # Check if the DataFrame is empty
empty_stations.append(station)
print(f"No CMEMS data found for station {sorted_names[station]}")
continue
else:
# ssh_station.sort_values(by='time', inplace=True) # Sort values by time
# # tg_station = closest_tg_times[station].dropna(dim='time')
ssh_station.dropna(how='any', inplace=True) # Drop NaNs in time and ssha
# SUBSTRACT THE MEAN VALUE OF EACH TIME SERIE FOR COMPARING
tg_ts = process_ts(ssh_station[['time', 'TG']], 'TG')
cmems_eur = process_ts(ssh_station[['time', 'CMEMS_NRT_EUR', 'min_distance_cmems_eur', 'n_val_cmems_eur']], 'CMEMS_NRT_EUR')
cmems_glo = process_ts(ssh_station[['time', 'CMEMS_NRT_GLO', 'min_distance_cmems_glo', 'n_val_cmems_glo']], 'CMEMS_NRT_GLO')
swot_l3 = process_ts(ssh_station[['time', 'SWOT L3', 'min_distance_swot_l3', 'n_val_swot_l3']], 'SWOT L3')
duacs_swot_l4 = process_ts(ssh_station[['time', 'DUACS (SWOT_L4)', 'min_distance_duacs_swot_l4', 'n_val_duacs_swot_l4']], 'DUACS (SWOT_L4)')
# swot_l3_unsmoothed = process_ts(ssh_station[['time', 'SWOT L3 unsmoothed', 'min_distance_swot_l3_unsmoothed', 'n_val_swot_l3_unsmoothed']], 'SWOT L3 unsmoothed')
if tg_ts is None or cmems_eur is None or cmems_glo is None or swot_l3 is None or duacs_swot_l4 is None:
# if tg_ts is None or cmems_eur is None or cmems_glo is None or swot_l3 is None or duacs_swot_l4 is None or swot_l3_unsmoothed is None:
empty_stations.append(station_name)
print(f"One or more products missing for station {station_name}")
continue
# empty_stations.append(station)
# print(f"Station {sorted_names[station]} has no CMEMS data")
# print(f"Station {sorted_names[station]} has more than 20% of NaNs")
# continue
# Calculate correlation between cmems and tg
corr_swot_l3 = swot_l3[demean].corr(tg_ts[demean])
corr_cmems_eur = cmems_eur[demean].corr(tg_ts[demean])
corr_cmems_glo = cmems_glo[demean].corr(tg_ts[demean])
corr_duacs_swot_l4 = duacs_swot_l4[demean].corr(tg_ts[demean])
# corr_swot_l3_unsmoothed = swot_l3_unsmoothed[demean].corr(tg_ts[demean])
# Calculate RMSD between cmems and tg
rmsd_swot_l3 = np.sqrt(np.mean((swot_l3[demean] - tg_ts[demean]) ** 2))
rmsd_cmems_eur = np.sqrt(np.mean((cmems_eur[demean] - tg_ts[demean]) ** 2))
rmsd_cmems_glo = np.sqrt(np.mean((cmems_glo[demean] - tg_ts[demean]) ** 2))
rmsd_duacs_swot_l4 = np.sqrt(np.mean((duacs_swot_l4[demean] - tg_ts[demean]) ** 2))
# rmsd_swot_l3_unsmoothed = np.sqrt(np.mean((swot_l3_unsmoothed[demean] - tg_ts[demean]) ** 2))
# Calculate variances of products and tg
var_swot_l3 = swot_l3[demean].var()
var_cmems_eur = cmems_eur[demean].var()
var_cmems_glo = cmems_glo[demean].var()
var_duacs_swot_l4 = duacs_swot_l4[demean].var()
# var_swot_l3_unsmoothed = swot_l3_unsmoothed[demean].var()
var_tg = tg_ts[demean].var()
# Calculate the variance of the difference between cmems and tg
var_diff_swot_l3 = (swot_l3[demean] - tg_ts[demean]).var()
var_diff_cmems_eur = (cmems_eur[demean] - tg_ts[demean]).var()
var_diff_cmems_glo = (cmems_glo[demean] - tg_ts[demean]).var()
var_diff_duacs_swot_l4 = (duacs_swot_l4[demean] - tg_ts[demean]).var()
# var_diff_swot_l3_unsmoothed = (swot_l3_unsmoothed[demean] - tg_ts[demean]).var()
# Append the results to the lists
rmsds_swot_l3.append(rmsd_swot_l3)
rmsds_cmems_eur.append(rmsd_cmems_eur)
rmsds_cmems_glo.append(rmsd_cmems_glo)
rmsds_duacs_swot_l4.append(rmsd_duacs_swot_l4)
# rmsds_swot_l3_unsmoothed.append(rmsd_swot_l3_unsmoothed)
correlations_swot_l3.append(corr_swot_l3)
correlations_cmems_eur.append(corr_cmems_eur)
correlations_cmems_glo.append(corr_cmems_glo)
correlations_duacs_swot_l4.append(corr_duacs_swot_l4)
# correlations_swot_l3_unsmoothed.append(corr_swot_l3_unsmoothed)
variances_tg.append(var_tg)
variances_swot_l3.append(var_swot_l3)
variances_cmems_eur.append(var_cmems_eur)
variances_cmems_glo.append(var_cmems_glo)
variances_duacs_swot_l4.append(var_duacs_swot_l4)
# variances_swot_l3_unsmoothed.append(var_swot_l3_unsmoothed)
variances_diff_swot_l3.append(var_diff_swot_l3)
variances_diff_cmems_eur.append(var_diff_cmems_eur)
variances_diff_cmems_glo.append(var_diff_cmems_glo)
variances_diff_duacs_swot_l4.append(var_diff_duacs_swot_l4)
# variances_diff_swot_l3_unsmoothed.append(var_diff_swot_l3_unsmoothed)
# Num days used
days_used_per_gauge_swot_l3.append(len(swot_l3))
days_used_per_gauge_cmems_eur.append(len(cmems_eur))
days_used_per_gauge_cmems_glo.append(len(cmems_glo))
days_used_per_gauge_duacs_swot_l4.append(len(duacs_swot_l4))
# days_used_per_gauge_swot_l3_unsmoothed.append(len(swot_l3_unsmoothed))
# Average min distances
min_distances_swot_l3.append(swot_l3['min_distance_swot_l3'].min())
min_distances_cmems_eur.append(cmems_eur['min_distance_cmems_eur'].min())
min_distances_cmems_glo.append(cmems_glo['min_distance_cmems_glo'].min())
min_distances_duacs_swot_l4.append(duacs_swot_l4['min_distance_duacs_swot_l4'].min())
# min_distances_swot_l3_unsmoothed.append(swot_l3_unsmoothed['min_distance_swot_l3_unsmoothed'].min())
# Number of values used for the average
n_val_swot_l3.append(swot_l3['n_val_swot_l3'].mean())
n_val_cmems_eur.append(cmems_eur['n_val_cmems_eur'].mean())
n_val_cmems_glo.append(cmems_glo['n_val_cmems_glo'].mean())
n_val_duacs_swot_l4.append(duacs_swot_l4['n_val_duacs_swot_l4'].mean())
# n_val_swot_l3_unsmoothed.append(swot_l3_unsmoothed['n_val_swot_l3_unsmoothed'].mean())
# PLOTTING TIME SERIES
plot_time_series(swot_l3, tg_ts, rmsd_swot_l3, corr_swot_l3, product_name='SWOT L3', station=station, rad=rad, plot_path=products[3]['plot_path'])
plot_time_series(cmems_eur, tg_ts, rmsd_cmems_eur, corr_cmems_eur, product_name='CMEMS NRT EUR', station=station, rad=rad, plot_path=products[1]['plot_path'])
plot_time_series(cmems_glo, tg_ts, rmsd_cmems_glo, corr_cmems_glo, product_name='CMEMS NRT GLO', station=station, rad=rad, plot_path=products[2]['plot_path'])
plot_time_series(duacs_swot_l4, tg_ts, rmsd_duacs_swot_l4, corr_duacs_swot_l4, product_name='DUACS SWOT L4', station=station, rad=rad, plot_path=products[0]['plot_path'])
# plot_time_series(swot_l3_unsmoothed, tg_ts)
# MAP PLOT OF CMEMS LOCATIONS OBTAINED FROM EACH GAUGE!
# fig, ax = plt.subplots(figsize=(10.5, 11), subplot_kw=dict(projection=ccrs.PlateCarree()))
# # Set the extent to focus on the defined lon-lat box
# ax.set_extent(lolabox, crs=ccrs.PlateCarree())
# # Add scatter plot for specific locations
# ax.scatter(tg_ts['longitude'][0], tg_ts['latitude'][0], c='black', marker='o', s=50, transform=ccrs.Geodetic(), label='Tide Gauge')
# ax.scatter(cmems_ts['cmems_lon_within_radius'][0], cmems_ts['cmems_lat_within_radius'][0], c='blue', marker='o', s=50, transform=ccrs.Geodetic(), label='CMEMS data')
# # Add coastlines and gridlines
# ax.coastlines()
# ax.gridlines(draw_labels=True)
# ax.legend(loc="upper left")
# ax.title(f'Station {sorted_names[station]}')
except (KeyError, ValueError): # Handle cases where station might not exist in df or time has NaNs
# Print message or log the issue and save the station index for dropping the empty stations
print(f'empty station {station}')
# empty_stations.append(station)
# print(f"Station {sorted_names[station]} not found in CMEMS data or time series has NaNs")
continue # Skip to the next iteration
# empty_stations = [5, 7, 11, 14, 17]
# Drop stations variables with no CMEMS data for matching with the table
sorted_names_mod = [x for i, x in enumerate(sorted_names) if i not in empty_stations]
ordered_lat_mod = [x for i, x in enumerate(ordered_lat) if i not in empty_stations]
ordered_lon_mod = [x for i, x in enumerate(ordered_lon) if i not in empty_stations]
# n_val = [x for i, x in enumerate(n_val) if i not in empty_stations]
table_all_swot_l3 = pd.DataFrame({'station': sorted_names_mod,
'correlation_swot_l3': correlations_swot_l3,
'rmsd_swot_l3': rmsds_swot_l3,
'var_TG': variances_tg,
'var_swot_l3': variances_swot_l3,
'var_diff_swot_l3': variances_diff_swot_l3,
'num_points_swot_l3': n_val_swot_l3,
'n_days_swot_l3': days_used_per_gauge_swot_l3,
'latitude': ordered_lat_mod,
'longitude': ordered_lon_mod,
'min_distance_swot_l3': min_distances_swot_l3,
# 'nans_percentage': nans_percentage
})
table_all_cmems_eur = pd.DataFrame({'station': sorted_names_mod,
'correlation_cmems_eur': correlations_cmems_eur,
'rmsd_cmems_eur': rmsds_cmems_eur,
'var_TG': variances_tg,
'var_cmems_eur': variances_cmems_eur,
'var_diff_cmems_eur': variances_diff_cmems_eur,
'num_points_cmems_eur': n_val_cmems_eur,
'n_days_cmems_eur': days_used_per_gauge_cmems_eur,
'latitude': ordered_lat_mod,
'longitude': ordered_lon_mod,
'min_distance_cmems_eur': min_distances_cmems_eur,
# 'nans_percentage': nans_percentage
})
table_all_cmems_glo = pd.DataFrame({'station': sorted_names_mod,
'correlation_cmems_glo': correlations_cmems_glo,
'rmsd_cmems_glo': rmsds_cmems_glo,
'var_TG': variances_tg,
'var_cmems_glo': variances_cmems_glo,
'var_diff_cmems_glo': variances_diff_cmems_glo,
'num_points_cmems_glo': n_val_cmems_glo,
'n_days_cmems_glo': days_used_per_gauge_cmems_glo,
'latitude': ordered_lat_mod,
'longitude': ordered_lon_mod,
'min_distance_cmems_glo': min_distances_cmems_glo,
# 'nans_percentage': nans_percentage
})
table_all_duacs_swot_l4 = pd.DataFrame({'station': sorted_names_mod,
'correlation_duacs_swot_l4': correlations_duacs_swot_l4,
'rmsd_duacs_swot_l4': rmsds_duacs_swot_l4,
'var_TG': variances_tg,
'var_CMEMS_duacs_swot_l4': variances_duacs_swot_l4,
'var_diff_duacs_swot_l4': variances_diff_duacs_swot_l4,
'num_points_duacs_swot_l4': n_val_duacs_swot_l4,
'n_days_duacs_swot_l4': days_used_per_gauge_duacs_swot_l4,
'latitude': ordered_lat_mod,
'longitude': ordered_lon_mod,
'min_distance_duacs_swot_l4': min_distances_duacs_swot_l4,
# 'nans_percentage': nans_percentage
})
# table_all_swot_l3_unsmoothed = pd.DataFrame({'station': sorted_names_mod,
# 'correlation_swot_l3_unsmoothed': correlations_swot_l3_unsmoothed,
# 'rmsd_swot_l3_unsmoothed': rmsds_swot_l3_unsmoothed,
# # 'var_TG': variances_tg,
# 'var_swot_l3_unsmoothed': variances_swot_l3_unsmoothed,
# 'var_diff_swot_l3_unsmoothed': variances_diff_swot_l3_unsmoothed,
# 'num_points_swot_l3_unsmoothed': n_val_swot_l3_unsmoothed,
# 'n_days_swot_l3_unsmoothed': days_used_per_gauge_swot_l3_unsmoothed,
# 'latitude': ordered_lat_mod,
# 'longitude': ordered_lon_mod,
# 'min_distance_swot_l3_unsmoothed': min_distances_swot_l3_unsmoothed,
# # 'nans_percentage': nans_percentage
# })
# table_all_swot_l3.to_excel(f'{path}tables/table_all_swot_l3_{rad}.xlsx')
# table_all_cmems_eur.to_excel(f'{path}tables/table_all_cmems_eur_{rad}.xlsx')
# table_all_cmems_glo.to_excel(f'{path}tables/table_all_cmems_glo_{rad}.xlsx')
# table_all_duacs_swot_l4.to_excel(f'{path}tables/table_all_duacs_swot_l4_{rad}.xlsx')
# # table_all_swot_l3_unsmoothed.to_excel(f'table_all_swot_l3_unsmoothed_{rad}.xlsx')
# PLOT HISTOGRAMS OF VARIANCES PER STATION TO CHECK THE DISTRIBUTION
table_all_tg = table_all_swot_l3[['station', 'var_TG']]
table_all_tg = table_all_duacs_swot_l4.rename(columns={'var_TG': 'var'}) # Use the same name for the TG variances
table_all_swot_l3 = table_all_swot_l3.rename(columns={'var_swot_l3': 'var', 'rmsd_swot_l3': 'rmsd'})
table_all_cmems_eur = table_all_cmems_eur.rename(columns={'var_cmems_eur': 'var', 'rmsd_cmems_eur': 'rmsd'})
table_all_cmems_glo = table_all_cmems_glo.rename(columns={'var_cmems_glo': 'var', 'rmsd_cmems_glo': 'rmsd'})
table_all_duacs_swot_l4 = table_all_duacs_swot_l4.rename(columns={'var_CMEMS_duacs_swot_l4': 'var', 'rmsd_duacs_swot_l4': 'rmsd'})
table_all_tg['source'] = 'tg'
table_all_swot_l3['source'] = 'table_all_swot_l3'
table_all_cmems_eur['source'] = 'table_all_cmems_eur'
table_all_cmems_glo['source'] = 'table_all_cmems_glo'
table_all_duacs_swot_l4['source'] = 'table_all_duacs_swot_l4'
# table_all_swot_l3_unsmoothed['source'] = 'table_all_swot_l3_unsmoothed'
combined_df = pd.concat([table_all_tg, table_all_swot_l3, table_all_cmems_eur, table_all_cmems_glo, table_all_duacs_swot_l4])
# Pivot the combined dataframe to have sources as columns
pivot_df_var = combined_df.pivot(index='station', columns='source', values='var')
pivot_df_var = pivot_df_var.reset_index()
pivot_df_rmsd = combined_df.pivot(index='station', columns='source', values='rmsd')
pivot_df_rmsd = pivot_df_rmsd.reset_index()
# PLOTTING HISTOGRAM FOR VARIANCE
stations = pivot_df_var['station']
num_stations = len(stations)
bar_width = 1.5
space_between_groups = 2 # Adjust this to set the space between groups
# Create a new x array with extra space between groups
x = np.arange(num_stations) * (5 * bar_width + space_between_groups)
# Plotting
fig, ax = plt.subplots()
# Reorder the plotting sequence to put TG first
ax.bar(x - 3 * bar_width, pivot_df_var['tg'], width=bar_width, label='TG', color="#9467bd") # TG first
ax.bar(x - 2 * bar_width, pivot_df_var['table_all_swot_l3'], width=bar_width, label='SWOT L3', color='#1f77b4')
ax.bar(x - 1 * bar_width, pivot_df_var['table_all_duacs_swot_l4'], width=bar_width, label='DUACS_SWOT_L4', color='#ff7f0e')
ax.bar(x - 0 * bar_width, pivot_df_var['table_all_cmems_glo'], width=bar_width, label='NRT_GLO', color='#2ca02c')
ax.bar(x + 1 * bar_width, pivot_df_var['table_all_cmems_eur'], width=bar_width, label='NRT_EUR', color='#d62728')
# Add labels and title
ax.set_xlabel('Stations', fontsize=12)
ax.set_ylabel('Variance (cm²)', fontsize=12)
ax.set_title(f'Variance of altimetry products at {rad} km radius', fontsize=14)
ax.set_xticks(x)
ax.set_xticklabels(stations.index, fontsize=12)
ax.set_yticklabels(stations.index, fontsize=12)
ax.legend(fontsize='small')
ax.grid(True, which='both', axis='both', color='gray', linestyle='--', linewidth=0.5, alpha=0.5)
plt.show()
# plt.savefig(f'{path}histograms/variances_{rad}.png')
# PLOTTING HISTOGRAM FOR RMSD
# Define the positions for the bars
stations = pivot_df_rmsd['station']
bar_width = 0.2
x = np.arange(len(stations))
# Plotting
fig, ax = plt.subplots()
ax.bar(x - 1.5 * bar_width, pivot_df_rmsd['table_all_swot_l3'], width=bar_width, label='SWOT L3')
ax.bar(x - 0.5 * bar_width, pivot_df_rmsd['table_all_duacs_swot_l4'], width=bar_width, label='DUACS_SWOT_L4')
ax.bar(x + 0.5 * bar_width, pivot_df_rmsd['table_all_cmems_glo'], width=bar_width, label='NRT_GLO')
ax.bar(x + 1.5 * bar_width, pivot_df_rmsd['table_all_cmems_eur'], width=bar_width, label='NRT_EUR')
# Add labels and title
ax.set_xlabel('Stations', fontsize=12)
ax.set_ylabel('RMSD (cm²)', fontsize=12)
ax.set_title(f'RMSD of altimetry products at {rad} km radius', fontsize=14)
ax.set_xticks(x)
# ax.set_xticklabels(stations, rotation=90, fontsize='small')
ax.set_xticklabels(stations.index, fontsize=12)
ax.set_yticklabels(stations.index, fontsize=12)
ax.legend(fontsize=8)
ax.grid(True, which='both', axis='both', color='gray', linestyle='--', linewidth=0.5, alpha=0.5)
plt.show()
# PERCENTAGE OF IMPROVEMENT
# improvement_cmems_eur = (table_all_swot_l3['rmsd'] - table_all_cmems_eur['rmsd'])/table_all_swot_l3['rmsd']*100
improvement_swot_l3 = (table_all_cmems_glo['rmsd'] - table_all_swot_l3['rmsd'])/table_all_cmems_glo['rmsd']*100
improvement_duacs_swot_l4 = (table_all_cmems_glo['rmsd'] - table_all_duacs_swot_l4['rmsd'])/table_all_cmems_glo['rmsd']*100
# Example station names (replace with your actual station names if different)
# Combine improvements into a DataFrame
# improvement_df = pd.DataFrame({
# 'Station': stations,
# # 'CMEMS EUR': improvement_cmems_eur.values.flatten(),
# 'CMEMS GLO': improvement_cmems_glo.values.flatten(),
# 'DUACS SWOT L4': improvement_duacs_swot_l4.values.flatten()
# })
improvement_df = pd.DataFrame({
'Station': stations,
# 'CMEMS EUR': improvement_cmems_eur.values.flatten(),
'SWOT L3': improvement_swot_l3.values.flatten(),
'DUACS SWOT L4': improvement_duacs_swot_l4.values.flatten()
})
# Set 'Station' as the index
# improvement_df.set_index('Station', inplace=True)
# Plotting
fig, ax = plt.subplots(figsize=(6, 6)) # Adjust the figure size as needed
# Define bar width and positions
bar_width = 0.2
x = np.arange(len(improvement_df))
# Plot bars for each product and store the bar containers
bars_swot_l3 = ax.bar(x, improvement_df['SWOT L3'], width=bar_width, label='SWOT L3')
bars_duacs_swot_l4 = ax.bar(x + bar_width, improvement_df['DUACS SWOT L4'], width=bar_width, label='DUACS SWOT L4')
# Get the colors of the bars
color_swot_l3 = bars_swot_l3[0].get_facecolor()
color_duacs_swot_l4 = bars_duacs_swot_l4[0].get_facecolor()
# Add labels and title
ax.set_xlabel('Stations', fontsize=12)
ax.set_ylabel('Percentage Improvement (%)', fontsize=12)
ax.set_title('Improvement compared to CMEMS NRT GLO', fontsize=18)
ax.set_xticks(x)
ax.set_xticklabels(improvement_df.index, fontsize=12)
# Draw horizontal lines using the exact bar colors
plt.axhline(improvement_df['SWOT L3'].mean(), color=color_swot_l3, linestyle='--', zorder=0)
plt.axhline(improvement_df['DUACS SWOT L4'].mean(), color=color_duacs_swot_l4, linestyle='--', zorder=0)
# Set fontsize for x-ticks and y-ticks
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
ax.legend(fontsize=12)
ax.grid(True, which='both', axis='y', color='gray', linestyle='--', linewidth=0.5, alpha=0.5)
# Show the plot
plt.tight_layout()
plt.show()
# Average RMSD values taking in account the non-linear behaviour of the RMSD
# Delete the wrong rows/tgs from rmsds
threshold = 10 # RMSD > 5 out
combined_rmsd_swot_l3 = compute_combined_rmsd(rmsds_swot_l3, threshold)
combined_rmsd_cmems_eur = compute_combined_rmsd(rmsds_cmems_eur, threshold)
combined_rmsd_cmems_glo = compute_combined_rmsd(rmsds_cmems_glo, threshold)
combined_rmsd_duacs_swot_l4 = compute_combined_rmsd(rmsds_duacs_swot_l4, threshold)
# Apply bootstrap method to calculate the confidence interval of RMSD
rmsds_error_swot_l3 = bootstrap_rmsd(rmsds_swot_l3)
rmsds_error_cmems_eur = bootstrap_rmsd(rmsds_cmems_eur)