-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmake_utils.py
More file actions
1542 lines (1310 loc) · 62.5 KB
/
make_utils.py
File metadata and controls
1542 lines (1310 loc) · 62.5 KB
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 sys
import warnings, argparse, os, configparser
import xxlimited
from datetime import datetime as dt
from datetime import timedelta
import pandas as pd
from netCDF4 import Dataset
import numpy as np
warnings.filterwarnings("ignore", category=UserWarning)
parser = argparse.ArgumentParser(
description="Obtaining information for running MDB_builder.")
parser.add_argument('-m', "--mode", help='Mode option',
choices=["test", "test_chla_bal", "check_neg_olci_values", "correct_neg_olci_values",
"correct_neg_olci_values_slurm","image_stats","compare_images","cyano_stats","plot_cyano","sensormask_stats"],
required=True)
parser.add_argument('-i', "--input_path", help="Input path.")
parser.add_argument('-o', "--output", help="Output file.")
parser.add_argument('-c',"--config_file", help="Configuration file")
parser.add_argument("-sd", "--start_date", help="Start date.")
parser.add_argument("-ed", "--end_date", help="End date.")
parser.add_argument("-r", "--region", help="Region.", choices=["BS", "MED", "BAL", "ARC"])
parser.add_argument("-ncores", "--num_cores",
help="Number of cores for correct_neg_olci_values_slurm mode (Deafult:12)", default=12)
# parser.add_argument('-s', "--source_path", help="Source path.",default="/dst04-data1/OC/OLCI/daily_v202311_bc")
# parser.add_argument('-p', "--param", help="Param for TEST")
args = parser.parse_args()
def check_neg_olci_values(input_path, output_file, start_date, end_date, region):
work_date = start_date
fw = open(output_file, 'w')
fw.write('Date;OFile;WL;MinValue;MinAttr;NInvalid')
bands = ['400', '412_5', '442_5', '490', '510', '560', '620', '665', '673_75', '681_25', '708_75', '753_75',
'778_75', '865', '885', '1020']
bands_n = [float(x.replace('_', '.')) for x in bands]
while work_date <= end_date:
yyyy = work_date.strftime('%Y')
jjj = work_date.strftime('%j')
yyyymmdd = work_date.strftime('%Y-%m-%d')
print(f'[INFO] Work date: {yyyymmdd}')
for iband, band in enumerate(bands):
# file_a = os.path.join(input_path, yyyy, jjj, f'Oa{yyyy}{jjj}-rrs{band}-{region.lower()}-fr.nc')
# file_b = os.path.join(input_path, yyyy, jjj, f'Ob{yyyy}{jjj}-rrs{band}-{region.lower()}-fr.nc')
file_o = os.path.join(input_path, yyyy, jjj, f'O{yyyy}{jjj}-rrs{band}-{region.lower()}-fr.nc')
print(f'[INFO]--> File: {file_o}')
# min_a = 'N/A'
# min_b = 'N/A'
min_o = 'N/A'
attr = 'N/A'
n_invalid = -1
# if os.path.exists(file_a):
# min_a = get_min_rrs_value(file_a,band)
# if os.path.exists(file_b):
# min_b = get_min_rrs_value(file_b,band)
if os.path.exists(file_o):
min_o, attr, n_invalid = get_min_rrs_value(file_o, band)
# line = f'{yyyymmdd};{bands_n[iband]};{min_a};{min_b};{min_o}'
line = f'{yyyymmdd};O{yyyy}{jjj}-rrs{band}-{region.lower()}-fr.nc;{bands_n[iband]};{min_o};{attr};{n_invalid}'
fw.write('\n')
fw.write(line)
work_date = work_date + timedelta(hours=24)
fw.close()
def correct_neg_olci_values(input_path, start_date, end_date, region):
work_date = start_date
bands = ['400', '412_5', '442_5', '490', '510', '560', '620', '665', '673_75', '681_25', '708_75', '753_75',
'778_75', '865', '885', '1020']
while work_date <= end_date:
yyyy = work_date.strftime('%Y')
jjj = work_date.strftime('%j')
yyyymmdd = work_date.strftime('%Y-%m-%d')
print(f'[INFO] Work date: {yyyymmdd}')
for band in bands:
file_a = os.path.join(input_path, yyyy, jjj, f'Oa{yyyy}{jjj}-rrs{band}-{region.lower()}-fr.nc')
file_b = os.path.join(input_path, yyyy, jjj, f'Ob{yyyy}{jjj}-rrs{band}-{region.lower()}-fr.nc')
file_o = os.path.join(input_path, yyyy, jjj, f'O{yyyy}{jjj}-rrs{band}-{region.lower()}-fr.nc')
file_out = f'{file_o[:-3]}_temp.nc'
neg_band = f'RRS{band}'
print(f'[INFO]--> File: {file_o}')
correct_neg_olci_values_impl(file_o, file_out, file_a, file_b, neg_band)
os.rename(file_out, file_o)
work_date = work_date + timedelta(hours=24)
def correct_neg_olci_values_impl(input_file, output_file, file_a, file_b, neg_band):
input_dataset = Dataset(input_file)
ncout = Dataset(output_file, 'w', format='NETCDF4')
# new_array = input_array.copy()
dataset_a = Dataset(file_a)
array_a = dataset_a.variables[neg_band][:]
dataset_a.close()
dataset_b = Dataset(file_b)
array_b = dataset_b.variables[neg_band][:]
dataset_b.close()
new_array = np.ma.mean(np.ma.concatenate([array_a, array_b], axis=0), axis=0)
##TESTING CHECK CODE
# input_array = np.ma.squeeze(input_dataset.variables[neg_band][:])
# array_a = np.ma.squeeze(array_a)
# array_b = np.ma.squeeze(array_b)
# indices = np.where(np.logical_and(array_a>0,array_b>0))
# prev_valid = input_array[indices]
# new_valid = new_array[indices]
# ratio = prev_valid/new_valid
# print(np.ma.min(ratio),np.ma.max(ratio))
# print('old', np.ma.min(input_array),np.ma.max(input_array))
# print('new', np.ma.min(new_array), np.ma.max(new_array))
# copy global attributes all at once via dictionary
ncout.setncatts(input_dataset.__dict__)
# copy dimensions
for name, dimension in input_dataset.dimensions.items():
ncout.createDimension(
name, (len(dimension) if not dimension.isunlimited() else None))
for name, variable in input_dataset.variables.items():
fill_value = None
if '_FillValue' in list(variable.ncattrs()):
fill_value = variable._FillValue
ncout.createVariable(name, variable.datatype, variable.dimensions, fill_value=fill_value, zlib=True,
complevel=6)
# copy variable attributes all at once via dictionary
ncout[name].setncatts(input_dataset[name].__dict__)
if name == neg_band:
ncout[name][:] = new_array[:]
else:
ncout[name][:] = input_dataset[name][:]
ncout.close()
input_dataset.close()
def correct_neg_olci_values_slurm(dir_log, start_date, end_date, region):
try:
nmax = int(args.num_cores)
except:
print(f'[ERROR] Number of cores option -ncores (--num_cores) must be an integer value')
return
input_olci_path = '/dst04-data1/OC/OLCI/daily_v202311_bc'
work_path = '/home/gosuser/Processing/gos-oc-processingchains_v202411/s3olciProcessing'
line_py_base = f'python {work_path}/make_merge_olci_202311.py -d DATE -a {region.lower()} -p RRS -v'
file_list = []
date_str_list = []
bands = ['400', '412_5', '442_5', '490', '510', '560', '620', '665', '673_75', '681_25', '708_75', '753_75',
'778_75', '865', '885', '1020']
min_values = [-0.0063, -0.0058, -0.0046, -0.0029, -0.0024, -0.0017, -0.0012, -0.00083, -0.000794, -0.00071,
-0.00065, 0.0, 0.0, 0.0, 0.0, 0.0]
work_date = start_date
while work_date <= end_date:
yyyy = work_date.strftime('%Y')
jjj = work_date.strftime('%j')
fslurm = os.path.join(dir_log, f'LaunchMergeOLCI_{work_date.strftime("%Y%m%d")}.slurm')
fw = open(fslurm, 'w')
fw.write('#!/bin/bash')
add_new_line(fw, '#SBATCH --nodes=1')
add_new_line(fw, '#SBATCH --ntasks=1')
add_new_line(fw, f'#SBATCH --output {fslurm.replace(".slurm", ".log")}')
add_new_line(fw, '#SBATCH -p octac_rep')
add_new_line(fw, '#SBATCH --mail-type=BEGIN,END,FAIL')
add_new_line(fw, '#SBATCH --mail-user=luis.gonzalezvilas@artov.ismar.cnr.it,lorenzo.amodio@artov.ismar.cnr.it')
add_new_line(fw, '')
add_new_line(fw, 'source /home/gosuser/load_miniconda3.source')
add_new_line(fw, 'conda activate op_proc_202211v2')
add_new_line(fw, f'cd {work_path}')
add_new_line(fw, '')
line_py = line_py_base.replace("DATE", work_date.strftime('%Y-%m-%d'))
add_new_line(fw, line_py)
add_new_line(fw, '')
add_new_line(fw, 'wait')
add_new_line(fw, '')
for iband, band in enumerate(bands):
file_o = os.path.join(input_olci_path, yyyy, jjj, f'O{yyyy}{jjj}-rrs{band}-{region.lower()}-fr.nc')
if os.path.exists(file_o):
line = f'ncatted -h -a valid_min,RRS{band},o,d,{min_values[iband]} {file_o}'
add_new_line(fw, line)
fw.close()
file_list.append(fslurm)
date_str_list.append(work_date.strftime("%Y-%m-%d"))
work_date = work_date + timedelta(hours=24)
file_sh = os.path.join(dir_log, f'Launcher_{int(dt.now().timestamp())}.sh')
file_mail = os.path.join(dir_log, f'Launcher_{int(dt.now().timestamp())}.mail')
fmail = open(file_mail, 'w')
fmail.write('LAUNCHING MULTIPLE CORRECTION OF NEGATIVE OLCI VALUES')
add_new_line(fmail, f'Region: {region}')
add_new_line(fmail, f'Start date: {start_date.strftime("%Y-%m-%d")}')
add_new_line(fmail, f'End date: {end_date.strftime("%Y-%m-%d")}')
add_new_line(fmail, f'SH file: {file_sh}')
add_new_line(fmail, '')
fmail.close()
fw = open(file_sh, 'w')
fw.write('#!/bin/bash')
add_new_line(fw, '')
add_new_line(fw, f'tfile={file_mail}')
add_new_line(fw, '')
for ifile, file in enumerate(file_list):
if ifile >= nmax:
iwait = ifile - nmax
line = f'job{ifile}=$(sbatch --dependency=afterany:$jobid{iwait} {file})'
else:
line = f'job{ifile}=$(sbatch {file})'
add_new_line(fw, line)
line = f'jobid{ifile}=$(echo "$job{ifile}" | awk \'{{print $NF}}\')'
add_new_line(fw, line)
if ifile >= nmax:
line = f'echo " Date: {date_str_list[ifile]} Slurm id: $jobid{ifile} Log file: {file.replace(".slurm", ".log")} Processed after slurm id: $jobid{iwait}">>$tfile'
else:
line = f'echo " Date: {date_str_list[ifile]} Slurm id: $jobid{ifile} Log file: {file.replace(".slurm", ".log")} ">>$tfile'
add_new_line(fw, line)
add_new_line(fw, '')
add_new_line(fw, '')
add_new_line(fw, '')
add_new_line(fw, '')
add_new_line(fw, '##start e-mail')
add_new_line(fw,
f'subject="LAUNCH MULTIPLE OLCI MERGING - {region} {start_date.strftime("%Y-%m-%d")} - {end_date.strftime("%Y-%m-%d")}"')
add_new_line(fw,
f'mailrcpt="luis.gonzalezvilas@artov.ismar.cnr.it,lorenzo.amodio@artov.ismar.cnr.it,filippo.manfredonia@artov.ismar.cnr.it"')
add_new_line(fw, f'cat $tfile | mail -s "$subject" "$mailrcpt"')
fw.close()
import subprocess
cmd = f'sh {file_sh}'
prog = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)
out, err = prog.communicate()
if err:
print(f'[ERROR]{err}')
def add_new_line(fw, line):
fw.write('\n')
fw.write(line)
def get_min_rrs_value(file, band):
dataset = Dataset(file, 'r')
array = dataset.variables[f'RRS{band}'][:]
min_v = np.ma.min(array)
attr = dataset.variables[f'RRS{band}'].valid_min
ninvalid_s = np.ma.count(array[array < (-400)])
dataset.close()
return f'{min_v}', attr, ninvalid_s
def make_stats(options_stats,start_date,end_date,output_file):
print(f'[INFO] Started making stats...')
basic_stats = ['N','AVG','STD','MEDIAN','MIN','MAX','P25','P75','RANGE','IQR']
corr_stats = ['N', 'SLOPE_I', 'SLOPE_II', 'OFFSET_I', 'OFFSET_II', 'STD_ERR_I', 'STD_SLOPE_II', 'STD_OFFSET_II',
'R', 'P_VALUE', 'R2', 'RMSD','BIAS','MdBIAS','APD','RPD','MdAPD','MdRPD','CRMSE','MAD','MdAD']
col_names = ['DATE']
for var in options_stats['var_list']:
for stat in basic_stats:
col_names.append(f'{var}_{stat}')
if options_stats['alt_var_list'] is not None:
for var in options_stats['alt_var_list']:
for stat in basic_stats:
col_names.append(f'alt_{var}_{stat}')
if options_stats['correlation_stats_var'] is not None:
for var_pair in options_stats['correlation_stats_var']:
for stat in corr_stats:
col_names.append(f'{var_pair[0]}_{var_pair[1]}_{stat}')
if options_stats['alt_correlation_stats_var'] is not None:
for var_pair in options_stats['alt_correlation_stats_var']:
for stat in corr_stats:
col_names.append(f'{var_pair[0]}_alt_{var_pair[1]}_{stat}')
use_log = options_stats['use_log']
alt_use_log = options_stats['alt_use_log']
var_list = options_stats['var_list']
alt_var_list = options_stats['alt_var_list']
ndates = (end_date-start_date).days+1
df = pd.DataFrame(index=range(ndates),columns=col_names)
work_date = start_date
index = 0
while work_date<=end_date:
print(f'[INFO] Working stats for day: {work_date}')
df.loc[0,"DATE"] = work_date.strftime('%Y-%m-%d')
##BASIC STATS
arrays = get_arrays_for_stats(options_stats,work_date,False)
if arrays is not None:
for ivar,var in enumerate(var_list):
use_log_here = use_log[ivar] if len(use_log)==len(var_list) else use_log[0]
results = compute_basic_stats(arrays[var],var,use_log_here)
df.loc[0,results.keys()] = results.values()
##ALT BASIC STATS
if options_stats['alt_input_path'] is not None:
arrays_alt = get_arrays_for_stats(options_stats, work_date, True)
if arrays_alt is not None:
for ivar, var in enumerate(alt_var_list):
use_log_here = alt_use_log[ivar] if len(alt_use_log) == len(alt_var_list) else alt_use_log[0]
results = compute_basic_stats(arrays_alt[var], f'alt_{var}', use_log_here)
df.loc[0, results.keys()] = results.values()
##CORRELATION STATS
if options_stats['correlation_stats_var'] is not None:
for var_pair in enumerate(options_stats['correlation_stats_var']):
ivar_0 = var_list.index(var_pair[0])
use_log_here_0 = use_log[ivar_0] if len(use_log) == len(var_list) else use_log[0]
ivar_1 = var_list.index(var_pair[0])
use_log_here_1 = use_log[ivar_1] if len(use_log) == len(var_list) else use_log[0]
use_log_here = use_log_here_0 if use_log_here_0==use_log_here_1 else None
if use_log_here is None:
print(f'[WARNING] Variables to compute correlations should both have the same use_log values')
else:
array_0 = arrays[var_pair[0]]
array_1 = arrays[var_pair[1]]
var_prefix = f'{var_pair[0]}_{var_pair[1]}_'
results = compute_correlation_stats(array_0,array_1,var_prefix,use_log_here)
index = index + 1
work_date = work_date+timedelta(hours=24)
def compute_correlation_stats(xarray,yarray,xbitmask,ybitmask,var_prefix,use_log):
indices_non_mask = np.where(np.logical_and(xarray.mask==False,yarray.mask==False))
xarray = xarray[indices_non_mask].compressed()
yarray = yarray[indices_non_mask].compressed()
if xbitmask is not None and ybitmask is not None:
xbitmask = xbitmask[indices_non_mask].compressed()
ybitmask = ybitmask[indices_non_mask].compressed()
if use_log:
valid_array = np.logical_and(yarray > 0, xarray > 0)
xarray = xarray[valid_array]
yarray = yarray[valid_array]
rel_diff = 100 * ((yarray - xarray) / xarray) #for computing APD, RPD, MdRPD, MdAPD
if use_log:
xarray = np.log10(xarray)
yarray = np.log10(yarray)
try:
from scipy import stats
slope, intercept, r_value, p_value, std_err = stats.linregress(xarray, yarray)
except:
slope, intercept, r_value, p_value, std_err = [-999.0]*5
try:
from pylr2 import regress2
results_r2 = regress2(np.array(xarray, dtype=np.float64), np.array(yarray, dtype=np.float64),_method_type_2="reduced major axis")
slope_II = results_r2['slope']
intercept_II = results_r2['intercept']
std_slope_II = results_r2['std_slope']
std_intercept_II = results_r2['std_intercept']
except:
slope_II,intercept_II,std_slope_II,std_intercept_II = [-999.0]*4
xdiff = xarray - np.mean(xarray)
ydiff = yarray - np.mean(yarray)
abs_diff = np.abs(rel_diff)
adiff = np.abs(yarray - xarray)
histo,bin_edges = np.histogram(adiff,1000)
res_histo = pd.DataFrame(index=range(1000),columns=['min','max','num'])
res_histo['min'] = bin_edges[:-1]
res_histo['max'] = bin_edges[1:]
res_histo['num'] = histo[:]
#n_low_adiff = np.count_nonzero(adiff<1e-4)
# n_bad = np.count_nonzero(adiff>1e-4)
# xbitmask_bad = xbitmask[adiff>1e-4]
# ybitmask_bad = ybitmask[adiff>1e-4]
# nx_zero = np.count_nonzero(xbitmask_bad==0)
# nx_case2 = np.count_nonzero(xbitmask_bad == 1024)
# ny_zero = np.count_nonzero(ybitmask_bad == 0)
# ny_case2 = np.count_nonzero(ybitmask_bad == 1024)
# # xbitmask_bad_unique = np.unique(xbitmask_bad)
# # ybitmask_bad_unique = np.unique(ybitmask_bad)
# print(f'{adiff.shape[0]} {n_low_adiff} {n_bad} {n_low_adiff+n_bad}')
# print(f'{len(xbitmask_bad)}->{nx_zero}->{nx_case2}')
# print(f'{len(ybitmask_bad)}->{ny_zero}->{ny_case2}')
results = {
f'{var_prefix}_N': xarray.shape[0],
f'{var_prefix}_SLOPE_I': slope,
f'{var_prefix}_SLOPE_II': slope_II,
f'{var_prefix}_OFFSET_I': intercept,
f'{var_prefix}_OFFSET_II': intercept_II,
f'{var_prefix}_STD_ERR_I': std_err,
f'{var_prefix}_STD_SLOPE_II': std_slope_II,
f'{var_prefix}_STD_OFFSET_II': std_intercept_II,
f'{var_prefix}_R': r_value,
f'{var_prefix}_PVALUE': p_value,
f'{var_prefix}_R2': r_value * r_value,
f'{var_prefix}_RMSD': rmse(yarray,xarray),
f'{var_prefix}_BIAS': np.mean(yarray - xarray),
f'{var_prefix}_MdBIAS': np.median(yarray-xarray),
f'{var_prefix}_APD': np.mean(abs_diff),
f'{var_prefix}_RPD': np.mean(rel_diff),
f'{var_prefix}_MdAPD': np.median(abs_diff),
f'{var_prefix}_MdRPD': np.median(rel_diff),
f'{var_prefix}_CRMSE': rmse(ydiff,xdiff),
f'{var_prefix}_MAD': np.mean(adiff),
f'{var_prefix}_MdAD': np.median(adiff),
'HISTO_RES': res_histo
}
if use_log:
##convert statistict to linear scale again
stats_to_convert = ['RMSD', 'CRMSE', 'MAD', 'MdAD']
for stat in stats_to_convert:
results[f'{var_prefix}_{stat}'] = np.power(10,results[f'{var_prefix}_{stat}'])
sign_stats_to_convert = ['BIAS', 'MdBIAS']
for stat in sign_stats_to_convert:
bias_neg = results[f'{var_prefix}_{stat}'] < 0
results[f'{var_prefix}_{stat}'] = np.power(10, np.abs(results[f'{var_prefix}_{stat}']))
if bias_neg:
results[f'{var_prefix}_{stat}'] = results[f'{var_prefix}_{stat}'] * (-1)
return results
# self.valid_stats['RPD'] = np.mean(rel_diff)
# # the mean of absolute (unsigned) percent differences
# self.valid_stats['APD'] = np.mean(np.abs(rel_diff))
# the median of relative (signed) percent differences
# rel_diff = 100 * ((sat_obs - ref_obs) / ref_obs)
# self.valid_stats['MdRPD'] = np.median(rel_diff)
# # the median of absolute (unsigned) percent differences
# self.valid_stats['MdAPD'] = np.median(np.abs(rel_diff))
def rmse(predictions, targets):
return np.sqrt(((np.asarray(predictions) - np.asarray(targets)) ** 2).mean())
def compute_basic_stats(array,var_name,use_log):
#basic_stats = ['N', 'AVG', 'STD', 'MEDIAN', 'MIN', 'MAX', 'P25', 'P75', 'RANGE', 'IQR']
data = array.compressed()
if use_log:
data = np.log10(data)
results = {
f'{var_name}_N': data.shape[0],
f'{var_name}_AVG': np.mean(data),
f'{var_name}_STD': np.std(data),
f'{var_name}_MEDIAN': np.median(data),
f'{var_name}_MIN': np.min(data),
f'{var_name}_MAX': np.max(data),
f'{var_name}_P25': np.percentile(data,25),
f'{var_name}_P75': np.percentile(data,75),
f'{var_name}_RANGE': -999.0,
f'{var_name}_IQR': -999.0,
}
results[f'{var_name}_RANGE']=results[f'{var_name}_MAX']-results[f'{var_name}_MIN']
results[f'{var_name}_IQR'] = results[f'{var_name}_P75'] - results[f'{var_name}_P25']
return results
def get_arrays_for_stats(options_stats,work_date,isalt):
input_path = options_stats['alt_input_path'] if isalt else options_stats['input_path']
org = options_stats['alt_input_path_organization'] if isalt else options_stats['input_path_organization']
dataset_name_file = options_stats['alt_dataset_name_file'] if isalt else options_stats['dataset_name_file']
dataset_name_format_date = options_stats['alt_dataset_name_format_date'] if isalt else options_stats['dataset_name_format_date']
file_date = get_file_date(input_path,org,dataset_name_file,dataset_name_format_date,work_date)
if file_date is None:
return None
arrays = {}
var_list = options_stats['alt_var_list'] if isalt else options_stats['var_list']
for var in var_list:
dataset = Dataset(file_date,'r')
arrays[var] = dataset.variables[var][:]
dataset.close()
return arrays
def get_file_date(input_path,org,dataset_name_file,dataset_name_format_date,work_date):
input_path_date = input_path
if org!='NONE':
for tformat in org.split('/'):
input_path_date = os.path.join(input_path_date,work_date.strftime(tformat))
if not os.path.isdir(input_path_date):
print(f'[WARNING] Input path for date {work_date.strftime("%Y-%m-%d")}: {input_path_date} is not available. Skipping...')
return None
name_file = dataset_name_file.replace('$DATE$',work_date.strftime(dataset_name_format_date))
file_date = os.path.join(input_path_date,name_file)
if not os.path.isfile(file_date):
print(f'[WARNING] File date for date {work_date.strftime("%Y-%m-%d")}: {file_date} is not available. Skipping...')
return None
return file_date
def make_chl_comparison():
dir_out = '/mnt/c/Users/LuisGonzalez/OneDrive - NOLOGIN OCEANIC WEATHER SYSTEMS S.L.U/CNR/OCTAC_WORK/POLYMER_TEST_V5/comparison_valid_v4_v5'
dir_1 = '/mnt/c/Users/LuisGonzalez/OneDrive - NOLOGIN OCEANIC WEATHER SYSTEMS S.L.U/CNR/OCTAC_WORK/POLYMER_TEST_V5/v4'
dir_2 = '/mnt/c/Users/LuisGonzalez/OneDrive - NOLOGIN OCEANIC WEATHER SYSTEMS S.L.U/CNR/OCTAC_WORK/POLYMER_TEST_V5/v5'
file_1 = os.path.join(dir_1,'S3B_OL_2_WFR____20240619T094235_20240619T094535_20240620T122332_0179_094_193_1980_MAR_O_NT_002_POLYMER_BAL202411.nc')
file_2 = os.path.join(dir_2,'S3B_OL_2_WFR____20240619T094235_20240619T094535_20240620T122332_0179_094_193_1980_MAR_O_NT_002_POLYMER_OUT_BAL202411.nc')
dataset_1 = Dataset(file_1)
chla_1 = dataset_1.variables['CHL'][:]
dataset_1.close()
dataset_2 = Dataset(file_2)
chla_2 = dataset_2.variables['CHL'][:]
dataset_2.close()
valid = np.logical_and(chla_1.mask==False,chla_2.mask==False)
chla_1 = chla_1[valid].compressed()
chla_2 = chla_2[valid].compressed()
dif_log_abs = np.abs(np.log10(chla_1)-np.log10(chla_2))
valid = np.zeros(chla_1.shape)
valid[dif_log_abs > 1e-3] = 1
valid[dif_log_abs > 1e-2] = 2
valid[dif_log_abs > 1e-1] = 3
array_end = np.column_stack([chla_1, chla_2,valid])
nvalid = chla_1.shape[0]
df = pd.DataFrame(index=range(nvalid),columns=['v4','v5','valid'],data=array_end)
file_out = os.path.join(dir_out,'chla_comparison.csv')
df.to_csv(file_out,sep=';',index=False)
for idx in range(4):
df_here = df[df['valid']==idx]
file_out = os.path.join(dir_out, f'chla_comparison_{idx}.csv')
df_here.to_csv(file_out,sep=';',index=False)
def make_image_comparison():
dir_out = '/mnt/c/Users/LuisGonzalez/OneDrive - NOLOGIN OCEANIC WEATHER SYSTEMS S.L.U/CNR/OCTAC_WORK/POLYMER_TEST_V5/comparison_valid_v4_v5'
dir_1 = '/mnt/c/Users/LuisGonzalez/OneDrive - NOLOGIN OCEANIC WEATHER SYSTEMS S.L.U/CNR/OCTAC_WORK/POLYMER_TEST_V5/v4'
dir_2 = '/mnt/c/Users/LuisGonzalez/OneDrive - NOLOGIN OCEANIC WEATHER SYSTEMS S.L.U/CNR/OCTAC_WORK/POLYMER_TEST_V5/v5'
cols = ['v4','v5','bitmask_1','bitmask_2','bitmask','valid']
wl_list = ['400','412','443','490','510','560','620','665','674','681','709','754','779','865']
# wl_list = ['412', '443', '490', '510', '560', '620', '665', '674', '681', '709', '754', '779', '865']
# wl_list = ['400']
file_stats = os.path.join(dir_out,'Stats_Valid.csv')
col_stats = ['NAME','WL','N4_14','N15','NCOMMON','R2','RMSD','MdBIAS','MdAD','MdRPD','MdAPD','N_VALID','N_ADIF-4','N_ADIF-3','N_ADIF-2','P_VALID','P_ADIF-4','P_ADIF-3','P_ADIF-2']
df_stats = None
for name in os.listdir(dir_1):
file_1 = os.path.join(dir_1,name)
file_2 = os.path.join(dir_2,name)
if os.path.exists(file_1) and os.path.exists(file_2):
print(f'[INFO] File: {name}')
dataset_1 = Dataset(file_1)
dataset_2 = Dataset(file_2)
##flagging analysis
##v.4 or v. 4.14
# desc = dataset_1.variables['bitmask'].description
# flags = [int(x.strip().split(':')[1]) for x in desc.split(',')]
# flags_meanings = [x.strip().split(':')[0] for x in desc.split(',')]
# meanings = " ".join(flags_meanings)
#bitmask = dataset_1.variables['bitmask'][:]
##v.5
# flags = dataset_2.variables['flags'].flag_masks
# meanings = dataset_2.variables['flags'].flag_meanings
# flags_meanings = [x.strip() for x in meanings.split(' ')]
# bitmask = dataset_2.variables['flags'][:]
#
# sys.path.append('/home/lois/PycharmProjects/hypernets_val/COMMON')
# from Class_Flags_OLCI import Class_Flags_Polymer
# cPolymer = Class_Flags_Polymer(flags,meanings)
# bitmask = bitmask.compressed()
# for flag in flags_meanings:
# flag_array = cPolymer.Mask(bitmask,[flag])
# nflag = np.count_nonzero(flag_array>0)
# print(f'{flag};{nflag}')
for wl in wl_list:
print(f'[INFO] --> {wl}')
stats_here = {key:[''] for key in col_stats}.copy()
stats_here['NAME'][0] = name
stats_here['WL'][0] = wl
array_1 = dataset_1.variables[f'Rw{wl}'][:]
nprev_1 = np.ma.count(array_1)
bitmask_1 = dataset_1.variables['bitmask'][:]
valid_1 = np.logical_or(bitmask_1==0,bitmask_1==1024)
#array_1[np.bitwise_and(bitmask_1,1023)!=0] = np.ma.masked
array_1[valid_1==False] = np.ma.masked
bitmask_1[array_1.mask] = np.ma.masked
nafter_1 = np.ma.count(array_1)
array_2 = dataset_2.variables[f'rho_w_{wl}'][:]
nprev_2 = np.ma.count(array_2)
bitmask_2 =dataset_2.variables['flags'][:]
valid_2 = np.logical_or(bitmask_2 == 0, bitmask_2 == 1024)
#array_2[np.bitwise_and(bitmask_2,1023) != 0] = np.ma.masked
array_2[valid_2 == False] = np.ma.masked
bitmask_2[array_2.mask] = np.ma.masked
nafter_2 = np.ma.count(array_2)
print(f'[INFO] Array 1: {nprev_1}->{nafter_1} Array 2: {nprev_2}->{nafter_2} ')
stats_here['N4_14'][0] = nafter_1
stats_here['N15'][0] = nafter_2
##working with common indices
results_here = compute_correlation_stats(array_1,array_2,bitmask_1,bitmask_2,'x',False)
indices_common = np.where(np.logical_and(array_1.mask==False,array_2.mask==False))
array_1 = array_1[indices_common].compressed()
array_2 = array_2[indices_common].compressed()
stats_here['NCOMMON'][0] = array_1.shape[0]
stats_here['R2'][0] = results_here['x_R2']
stats_here['RMSD'][0] = results_here['x_RMSD']
stats_here['MdBIAS'][0] = results_here['x_MdBIAS']
stats_here['MdAD'][0] = results_here['x_MdAD']
stats_here['MdRPD'][0] = results_here['x_MdRPD']
stats_here['MdAPD'][0] = results_here['x_MdAPD']
#stats_here['N_LOW_ADIFF'][0] = results_here['x_N_LOW_ADIFF']
# histo_res = results_here['HISTO_RES']
# index_min = np.min(np.where(histo_res['num']<3000))
# th = histo_res.loc[index_min,'min']
th_1 = 1e-4
th_2 = 1e-3
th_3 = 1e-2
bitmask_1 = bitmask_1[indices_common].compressed()
bitmask_2 = bitmask_2[indices_common].compressed()
bitmask = np.zeros(bitmask_1.shape)
bitmask[np.logical_or(bitmask_1 > 0, bitmask_2 > 0)] = 1
a_diff = np.abs(array_1-array_2)
a_diff_th = np.zeros(a_diff.shape)
a_diff_th[a_diff > th_1] = 1
a_diff_th[a_diff > th_2] = 2
a_diff_th[a_diff > th_3] = 3
array_end = np.column_stack((array_1, array_2, bitmask_1, bitmask_2, bitmask,a_diff_th))
file_c = os.path.join(dir_out, name[:-3] + f'_{wl}.csv')
df = pd.DataFrame(index=range(array_1.shape[0]),columns=cols,data = array_end)
df.to_csv(file_c,sep=';',index=False)
histo_res = results_here['HISTO_RES']
file_histo = os.path.join(dir_out,f'histo_{wl}.csv')
histo_res.to_csv(file_histo,sep=';',index=False)
stats_here['N_VALID'][0] = np.count_nonzero(a_diff_th==0)
stats_here['N_ADIF-4'][0] = np.count_nonzero(a_diff_th==1)
stats_here['N_ADIF-3'][0] = np.count_nonzero(a_diff_th==2)
stats_here['N_ADIF-2'][0] = np.count_nonzero(a_diff_th == 3)
stats_here['P_VALID'][0] = (stats_here['N_VALID'][0]/array_1.shape[0])*100
stats_here['P_ADIF-4'][0] = (stats_here['N_ADIF-4'][0]/array_1.shape[0])*100
stats_here['P_ADIF-3'][0] = (stats_here['N_ADIF-3'][0] / array_1.shape[0]) * 100
stats_here['P_ADIF-2'][0] = (stats_here['N_ADIF-2'][0] / array_1.shape[0]) * 100
if df_stats is None:
df_stats = pd.DataFrame(stats_here)
else:
df_stats = pd.concat([df_stats,pd.DataFrame(stats_here)],ignore_index=True)
dataset_1.close()
dataset_2.close()
print(f'[INFO] Saving stats...')
if df_stats is not None:
df_stats.to_csv(file_stats, sep=';', index=False)
print(f'[INFO] Completed')
def make_cyano_stats():
dir_base = '/store3/OC/OLCI_BAL/dailyolci_202411'
work_date = dt(2016, 4, 26)
end_date = dt(2024, 12, 31)
file_out = os.path.join(dir_base, f'CyanoOlci.csv')
fw = open(file_out, 'w')
fw.write('Year;NValid;NSubSurface;NSurface;NBoth;NAny')
year_ref = 0
count = None
while work_date <= end_date:
yyyy = work_date.strftime('%Y')
jjj = work_date.strftime('%j')
jday = int(jjj)
if jday<161 or jday>270:
work_date = work_date + timedelta(hours=24)
continue
file_data = os.path.join(dir_base,yyyy,jjj,f'O{yyyy}{jjj}-chl-bal-fr.nc')
if os.path.exists(file_data):
if work_date.year!=year_ref:
if year_ref>0:
line_year = f'{year_ref};{count[0]};{count[1]};{count[2]};{count[3]};{count[4]}'
fw.write('\n')
fw.write(line_year)
year_ref = work_date.year
count = [0]*5
dataset = Dataset(file_data)
data = np.ma.squeeze(dataset.variables['CYANOBLOOM'][:])
dataset.close()
data_valid = data.compressed()
count[0] = count[0] + np.count_nonzero(data_valid>=0)
count[1] = count[1] + np.count_nonzero(data_valid == 1)
count[2] = count[2] + np.count_nonzero(data_valid == 2)
count[3] = count[3] + np.count_nonzero(data_valid == 3)
count[4] = count[4] + np.count_nonzero(data_valid >= 1)
work_date = work_date + timedelta(hours=24)
line_year = f'{year_ref};{count[0]};{count[1]};{count[2]};{count[3]};{count[4]}'
fw.write('\n')
fw.write(line_year)
fw.close()
def plot_cyano():
import os
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.gridspec as gridspec
## -- path for input and output
dir_base = '/mnt/c/Users/LuisGonzalez/OneDrive - NOLOGIN OCEANIC WEATHER SYSTEMS S.L.U/CNR/OCTAC_WORK/BAL_EVOLUTION_202411/CYANOBLOOM_EVOLUTION'
## -- input
file_cyano = os.path.join(dir_base, 'CyanoEvolution_CyanoPeriod.csv')
## -- output
file_out = os.path.join(dir_base, 'CyanoEvolutionPlot.png')
# -- Reading input data
df_cyano = pd.read_csv(file_cyano, sep=';')
sub = np.array(df_cyano['CoverageSub'][1:])
sur = np.array(df_cyano['CoverageSurface'][1:])
both = np.array(df_cyano['CoverageBoth'][1:])
year_start = 1998
year_end = 2024
# -- Figure size & division
fig = plt.figure(figsize=(11.7, 8.3))
gs = gridspec.GridSpec(2, 1, height_ratios=[100, 35])
# -- Define style of the plot
mpl.style.use('ggplot')
# -- 1st axis (the 2nd one is for the logos)
ax = plt.subplot(gs[0])
# -- Legends & title
plt.xlabel(' Year ', size='xx-large', fontweight='bold')
plt.ylabel(r'Coverage area (day·km$^2$·10$^6$)', size='xx-large', fontweight='bold')
tt = plt.title('Baltic Sea summer bloom coverage (1998-2024)', fontsize=18)
tt.set_position([0.5, 1.05])
## -- Time axis
time = np.arange(year_start, year_end + 1, 1)
ax.set_xticks(time)
time_labels = [str(x) if (x % 2) == 0 else '' for x in time]
ax.set_xticklabels(time_labels, size='small')
ax.set_xlim([time[0] - 1, time[-1] + 1])
ax.tick_params(labelsize=12)
# -- Plotting data
hsur = plt.bar(time, sur, color=(1.0, 0.65, 0), linewidth=1.0, edgecolor='k')
hboth = plt.bar(time, both, bottom=sur, color=(0.58, 0.44, 0.86), linewidth=1.0, edgecolor='k')
hsub = plt.bar(time, sub, bottom=sur + both, color=(0.0, 0.0, 1.0), linewidth=1.0, edgecolor='k')
# -- Y-axis
ydata = [0.0e6, 0.5e6, 1.0e6, 1.5e6, 2.0e6, 2.5e6, 3.0e6]
yticks = ['0', '0.5', '1.0', '1.5', '2.0', '2.5', '3.0']
plt.yticks(ydata, yticks)
plt.grid(True, ls='dotted', alpha=0.6)
# -- Legend
str_legend = ['Subsurface bloom (Rrs555)', 'Concurrent bloom', 'Surface bloom(Rrs670)']
plt.legend([hsub, hboth, hsur], str_legend, loc=9, fontsize=12)
# -- Credits and datatype
ax.text(1997.5, 2.9e6,
s='Datatype : Multi-Sensor Satellite Observation \nCredit : E.U. Copernicus Marine Service Information',
bbox={'facecolor': 'white', 'alpha': 0.5}, fontsize=9)
# -- Add the logos as subplot
logo = plt.imread(os.path.join(dir_base, 'LogosOMIBand-100-mv.png'))
axlogo = plt.subplot(gs[1])
axlogo.imshow(logo)
axlogo.axis('off')
# -- Figure caption
axlogo.text(-20, 150,
'Summer subsurface and surface bloom coverage time series for the Baltic Sea.\nConcurrent bloom are the areas where both surface and subsurface thresholds were exceeded.',
style='italic', fontsize=13)
# -- Graphical settings
plt.subplots_adjust(wspace=0, hspace=-0.7)
plt.tight_layout()
## -- Saving output file
plt.savefig(file_out, dpi=300)
def plot_cyano_kk():
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
from netCDF4 import Dataset
import matplotlib.gridspec as gridspec
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
from scipy.stats import linregress
file_cyano = '/mnt/c/Users/LuisGonzalez/OneDrive - NOLOGIN OCEANIC WEATHER SYSTEMS S.L.U/CNR/OCTAC_WORK/BAL_EVOLUTION_202411/CYANOBLOOM_EVOLUTION/CyanoEvolution_CyanoPeriod.csv'
file_out = os.path.join(os.path.dirname(file_cyano), 'CyanoEvolutionPlot.png')
df_cyano = pd.read_csv(file_cyano, sep=';')
sub = np.array(df_cyano['CoverageSub'][1:])
sur = np.array(df_cyano['CoverageSurface'][1:])
both = np.array(df_cyano['CoverageBoth'][1:])
year_start = 1998
year_end = 2024
# -- Figure size & division
fig = plt.figure(figsize=(11.7, 8.3))
gs = gridspec.GridSpec(2, 1, height_ratios=[100, 35])
#plt.grid(True, ls='dotted', alpha=0.6)
# -- Define style of the plot
mpl.style.use('ggplot')
# -- 1st axis (the 2nd one is for the logos)
ax = plt.subplot(gs[0])
# -- Legends & title
plt.xlabel(' Year ', size='xx-large', fontweight='bold')
plt.ylabel(r'Coverage area (day·km$^2$·10$^6$)', size='xx-large', fontweight='bold')
tt = plt.title('Baltic Sea summer bloom coverage (1998-2024)', fontsize=18)
tt.set_position([0.5, 1.05])
time = np.arange(year_start, year_end + 1, 1)
ax.set_xticks(time)
time_labels = [str(x) if (x%2)==0 else '' for x in time]
ax.set_xticklabels(time_labels,size='small')
#ax.set_xticklabels(time, rotation=30, ha='left', size='small') # size --> x-small,small,medium,large,...
ax.set_xlim([time[0] - 1, time[-1] + 1])
ax.tick_params(labelsize=12)
hsur = plt.bar(time, sur, color=(1.0, 0.65, 0), linewidth=1.0, edgecolor='k')
hboth = plt.bar(time, both, bottom=sur, color=(0.58, 0.44, 0.86), linewidth=1.0, edgecolor='k')
hsub = plt.bar(time, sub, bottom=sur + both, color=(0.0, 0.0, 1.0), linewidth=1.0, edgecolor='k')
ydata = [0.0e6, 0.5e6, 1.0e6, 1.5e6, 2.0e6, 2.5e6, 3.0e6]
yticks = ['0', '0.5', '1.0', '1.5', '2.0', '2.5', '3.0']
plt.yticks(ydata, yticks)
plt.grid(True, ls='dotted', alpha=0.6)
#plt.tick_params(axis='x', which='both', bottom=False)
# -- Credits and datatype
str_legend = ['Subsurface bloom (Rrs555)', 'Concurrent bloom', 'Surface bloom(Rrs670)']
plt.legend([hsub,hboth,hsur],str_legend,loc=9,fontsize=12)
##datacredits,location using data coordinates
ax.text(1997.5,2.9e6,s='Datatype : Multi-Sensor Satellite Observation \nCredit : E.U. Copernicus Marine Service Information',bbox = {'facecolor':'white', 'alpha':0.5},fontsize=9)
# -- Add the logos as subplot
logo = plt.imread(os.path.join(os.path.dirname(file_cyano),'LogosOMIBand-100-mv.png'))
axlogo = plt.subplot(gs[1])
img = axlogo.imshow(logo)
axlogo.axis('off')
# --
axlogo.text(-20, 150, 'Summer subsurface and surface bloom coverage time series for the Baltic Sea.\nConcurrent bloom are the areas where both surface and subsurface thresholds were exceeded.', style='italic',fontsize=13)
# -- Graphical settings
plt.subplots_adjust(wspace=0, hspace=-0.7)
plt.tight_layout()
plt.savefig(file_out, dpi=300)
def plot_cyano_deprecated():
file_cyano = '/mnt/c/Users/LuisGonzalez/OneDrive - NOLOGIN OCEANIC WEATHER SYSTEMS S.L.U/CNR/OCTAC_WORK/BAL_EVOLUTION_202411/CYANOBLOOM_EVOLUTION/CyanoEvolution_CyanoPeriod.csv'
file_out = os.path.join(os.path.dirname(file_cyano),'CyanoEvolutionPlot.png')
df_cyano = pd.read_csv(file_cyano,sep=';')
sub = np.array(df_cyano['CoverageSub'][1:])
sur = np.array(df_cyano['CoverageSurface'][1:])
both = np.array(df_cyano['CoverageBoth'][1:])
from matplotlib import pyplot as plt
from matplotlib.ticker import AutoMinorLocator
xdata = np.arange(1998,2025)
xticks = [str(x) if (x%2)==0 else '' for x in xdata]
plt.figure(figsize=(18.74,9.37),dpi=300)
hsur = plt.bar(xdata,sur,color=(1.0,0.65,0), linewidth=1.0, edgecolor='k')
hboth = plt.bar(xdata,both,bottom=sur,color=(0.58,0.44,0.86), linewidth=1.0, edgecolor='k')
hsub =plt.bar(xdata,sub,bottom=sur+both,color=(0.0,0.0,1.0), linewidth=1.0, edgecolor='k')
plt.xticks(xdata,xticks,fontsize=22,minor=False)
ydata = [0.0e6,0.5e6,1.0e6,1.5e6,2.0e6,2.5e6,3.0e6]
yticks = ['0','0.5','1.0','1.5','2.0','2.5','3.0']
plt.yticks(ydata,yticks,fontsize=22)
plt.gca().yaxis.set_minor_locator(AutoMinorLocator())
plt.tick_params(axis='x',which='both',bottom=False)
plt.tick_params(axis='y',which='major',direction='in',length=30)
plt.tick_params(axis='y', which='minor', direction='in', length=15,left=True,right=True)
plt.xlabel('Year',fontsize=22)
plt.ylabel(r'Coverage area (day·km$^2$·10$^6$)',fontsize=22)
plt.title('Summer bloom Baltic Sea',fontsize=22)
str_legend = ['Subsurface bloom (Rrs555)','Concurrent bloom','Surface bloom(Rrs670)']
plt.legend([hsub,hboth,hsur],str_legend,fontsize=22,bbox_to_anchor=(0.35,0.97),edgecolor='black')
plt.tight_layout()
plt.savefig(file_out,dpi=300)
def check_sensormask_stats():
from Class_Flags import Flags_General
dir_base = '/store3/OC/MULTI/daily_v202311_x'
file_out = '/store/COP2-OC-TAC/PQD/sensor_mask_multi_chl_med.csv'
##LOCAL TEST
dir_base = '/mnt/c/Users/LuisGonzalez/OneDrive - NOLOGIN OCEANIC WEATHER SYSTEMS S.L.U/NOW/OCTAC_QWG'
file_out = os.path.join(dir_base,'sensor_mask_multi_chl_med.csv')
work_date = dt(2023,5,1)
end_date = dt(2025,5,27)
fw = open(file_out,'w')
mask_list = None
flag_g = None
while work_date<=end_date:
yyyy = work_date.strftime('%Y')
jjj = work_date.strftime('%j')
file_date = os.path.join(dir_base,f'{yyyy}',f'{jjj}',f'X{yyyy}{jjj}-chl-med-hr.nc')
if os.path.isfile(file_date):
dataset = Dataset(file_date)
smask = dataset.variables['SENSORMASK'][:]
if mask_list is None:
comment = dataset.variables['SENSORMASK'].comment
comment = comment[0:comment.index('.')]
mask_list = {x.split('=')[0].strip():int(x.split('=')[1].strip()) for x in comment.split(';')}
first_line = f'Date;{";".join(list(mask_list.keys()))};All'
fw.write(first_line)
flag_g = Flags_General(list(mask_list.keys()),list(mask_list.values()),smask.dtype.name)
line = f'{work_date.strftime("%Y-%m-%d")}'
smask = smask.compressed()
for flag in flag_g.flagMeanings:
mask_here = flag_g.Mask(smask,[flag])
line = f'{line};{np.count_nonzero(mask_here)}'
line = f'{line};{np.count_nonzero(smask)}'
fw.write('\n')
fw.write(line)
dataset.close()
work_date = work_date + timedelta(hours=25)
fw.close()
def check_coverage():
dir_base = '/store3/OC/MULTI/daily_v202311_x'
file_out = '/store/COP2-OC-TAC/PQD/coverage_multi_chl_med.csv'
file_mask = '/store/COP2-OC-TAC/PQD/MED_Land_hr.nc'
coverage_dirs = [
'/store3/OC/MODISA/daily_v202311',
'/store3/OC/VIIRSJ/daily_v202311',
'/store3/OC/VIIRS/daily_v202311',
'/dst04-data1/OC/OLCI/daily_v202311_bc',
'/dst04-data1/OC/OLCI/daily_v202311_bc'