forked from qjutard/radiometry_QC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RT_QC_V7_oao.R
1319 lines (815 loc) · 31.7 KB
/
RT_QC_V7_oao.R
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
##############################################################################
# This program was designed in order to perform the RT-QC for the chlorophyll
# in the framework of Bioargo following the ADMT 14. on the OAO platform
#
# Based on Pabim White Book, Xing 2012 (NPQ)
#
# C. Schmechtig February 2014
# -adjusted in August 2014 in order to perform Adjusted CHLA no longer on request but automatically
# -adjusted in October 2014 in order to calculate BBP and to perform the RT_QC on it
# -calculate the quenching in 90% in the MLD
# -Adjusted in February 2015 in order to perform the RT_QC on radiometry with emanuele Organelli's work
# -Change the CHLA_ADJUSTED_QC in NPQ zone to 8 (according to coriolis recommendations)
# -Adjusted in december 2015 to perform RTQC on CDOM, small corrections on BBP QC, and on radiometry QC
# -Adjusted in March 2016 to be able to finish the QC processing even if the CTD is not working properly and restore the spike test for BBP
# -Change in May 2017 to adapt to RNetCDF library
#
# The values of DEPTH_LIMIT and DELTA_DEPTH
#
##############################################################################
##### loading RNetCDF library
library(ncdf4)
##### Require oceanographic routines to perform MLD
require(oce)
##### Require some Filter function from Xiao Gang
source("/home/bioargo/PROGRAM_TEST/RT_QC/RunningFilter.R")
##### Require the calculation of betasw from zhang et al
source("betasw124_ZHH2009.R")
##### Require Emanuele's Routine for RT_QC radiometry
source("RT_QC_radiometry_function_oao_2.R")
##### Some tests on the solar angle for the Radiometry
source("julian_to_date.R")
#### get the mission name
uf=commandArgs()
mission <- uf[2]
mis=substr(mission,start=7,stop=10)
##### Set up some threshold values on MLD
MLD_LIMIT=0.03
DEPTH_LIMIT=200
DELTA_DEPTH=50
#### Global Range Values
MIN_RANGE=-0.1
MAX_RANGE=50
#### FOR CDOM
MIN_RANGE_CDOM=-0.5
MAX_RANGE_CDOM=375
# FOR BBP
MIN_RANGE_BBP532=-0.000025
MIN_RANGE_BBP700=-0.000005
MAX_RANGE_BBP=0.1
#### Threshold for potential density
POTDENS_THR=0.03
#### mission with 2 BB for Eaims and UK bio-Argo
mission_type=substr(mission,1,3)
if ( mission_type == "met" | mission_type == "bas" | mission_type == "imr")
{
mission_2BB=TRUE
} else {
mission_2BB=FALSE
}
#### Factory Calibration Coefficient !!!!! Pick Up in the File !!!!
file_calib=paste("/home/bioargo/PROGRAM_TEST/PROFIL_NC/PREPROCESSING/TECHNIQUE/calibration_CHLA_",mission,".txt",sep="")
calib=read.table(file=file_calib,header=FALSE)
DARK_CHLA=calib$V1
SCALE_CHLA=calib$V2
if(!mission_2BB)
{
#### Factory Calibration Coefficient !!!!! Pick Up in the File !!!!
file_calib_CDOM=paste("/home/bioargo/PROGRAM_TEST/PROFIL_NC/PREPROCESSING/TECHNIQUE/calibration_CDOM_",mission,".txt",sep="")
calib_CDOM=read.table(file=file_calib_CDOM,header=FALSE)
DARK_CDOM=calib_CDOM$V1
SCALE_CDOM=calib_CDOM$V2
}
#### Creating the list of files to RT_QC
repNCDF=paste("/var/www/oao/BD_FLOAT/NETCDF/",mission,"/",sep="") # Directory
#LIST_nc=list.files(repNCDF,pattern=mission) # all Files
liste_to_do_file=paste(repNCDF,"liste_to_do",sep="")
################################
# testing the size of the file"
fileinfo=file.info(liste_to_do_file)
if(fileinfo$size != 0){
liste_to_do=read.table(liste_to_do_file)
} else {
stop("RT_QC already done")
}
###############################
LIST_nc=liste_to_do$V1
#### Init Hist_CHLA Value
last_file=paste(repNCDF,"lastNC_",mis,sep="")
last_calib=paste(repNCDF,"lastDARK_",mis,sep="")
if(!mission_2BB)
{
if(file.exists(c(last_calib))) {
lastDARK=read.table(last_calib)
HIST_CHLA=lastDARK$V1
FOND_CDOM_HIST=lastDARK$V2
FIRST_CDOM_FLAG=lastDARK$V3
SHIFT_CDOM=lastDARK$V4
} else {
HIST_CHLA=DARK_CHLA
FOND_CDOM_HIST=0
FIRST_CDOM_FLAG=FALSE
SHIFT_CDOM=0
}
} else {
if(file.exists(c(last_calib))) {
lastDARK=read.table(last_calib)
HIST_CHLA=lastDARK$V1
print(HIST_CHLA)
} else {
HIST_CHLA=DARK_CHLA
}
}
####################################################################
## Beginning of the loop on every profile
####################################################################
for (IDnc in LIST_nc) {
# Get the name of the file without extension
file=substr(IDnc,40,(nchar(IDnc)-3))
# Fichier de sortie du profil PRES,CHLA,CHLA_ADJUSTED et Flags
path_out_peigne=paste("/var/www/oao/BD_FLOAT/NETCDF/PEIGNE/",file,"_prof.txt",sep="")
# Fichier Netcdf
file_in=paste(IDnc,sep="")
filenc=nc_open(file_in,readunlim=FALSE,write=TRUE)
##################################################
#### 1. Get Data from the Netcdf File
##################################################
TEMP=ncvar_get(filenc,"TEMP")
PSAL=ncvar_get(filenc,"PSAL")
PRES=ncvar_get(filenc,"PRES")
CHLA=ncvar_get(filenc,"CHLA")
CHL_RAW=ncvar_get(filenc,"CHL_RAW")
CHLA_QC=ncvar_get(filenc,"CHLA_QC")
CHLA_ADJUSTED=ncvar_get(filenc,"CHLA_ADJUSTED")
CHLA_ADJUSTED_QC=ncvar_get(filenc,"CHLA_ADJUSTED_QC")
BACKSCATTERING=ncvar_get(filenc,"BACKSCATTERING")
BACKSCATTERING_QC=ncvar_get(filenc,"BACKSCATTERING_QC")
CDOM_RAW=ncvar_get(filenc,"CDOM_RAW")
CDOM_ADJUSTED=ncvar_get(filenc,"CDOM_ADJUSTED")
CDOM_ADJUSTED_QC=ncvar_get(filenc,"CDOM_ADJUSTED_QC")
CDOM=ncvar_get(filenc,"CDOM")
CDOM_QC=ncvar_get(filenc,"CDOM_QC")
# in case of 2BB mission the BB at 532 is stored in the CDOM
if(mission_2BB)
{
BBP532=CDOM
BBP532_QC=CDOM_QC
BETASW532=CDOM
}
## Initializing BBP to BACKSCATTERING and also BETASW,to have the right dimension
BBP700=BACKSCATTERING
#BBP700_ADJUSTED=BACKSCATTERING
BBP700_QC=BACKSCATTERING_QC
#BBP700_ADJUSTED_QC=CHLA_QC
BETASW700=BACKSCATTERING
#### Radiometry
JULD=ncvar_get(filenc,"JULD") # to test solar angle
LATITUDE=ncvar_get(filenc,"LATITUDE") # to test solar angle
LONGITUDE=ncvar_get(filenc,"LONGITUDE") # to test solar angle
IRR_380=ncvar_get(filenc,"DOWNWELLING_IRRADIANCE_380") #irradiance at 380 nm
DOWNWELLING_IRRADIANCE_380_QC=ncvar_get(filenc,"DOWNWELLING_IRRADIANCE_380_QC")
IRR_412=ncvar_get(filenc,"DOWNWELLING_IRRADIANCE_412") #irradiance at 412 nm
DOWNWELLING_IRRADIANCE_412_QC=ncvar_get(filenc,"DOWNWELLING_IRRADIANCE_412_QC")
IRR_490=ncvar_get(filenc,"DOWNWELLING_IRRADIANCE_490") #irradiance at 490 nm
DOWNWELLING_IRRADIANCE_490_QC=ncvar_get(filenc,"DOWNWELLING_IRRADIANCE_490_QC")
PAR=ncvar_get(filenc,"PAR") # Photosynthetically Available Radiation (PAR)
PAR_QC=ncvar_get(filenc,"PAR_QC") # Photosynthetically Available Radiation (PAR)
ind_norad=(IRR_380>=99999.)
ind_rad=which(IRR_380!=99999.)
test_radiometry=solar_angle_test(JULD,LATITUDE,LONGITUDE)
##################################################
#### 1.b Initialising the QC for ACRI stat
##################################################
# Global Range
CHLA_QC_GR=rep(0,length(PRES))
# Negative Spike Test
CHLA_QC_ST=rep(0,length(PRES))
# Mixed layer
CHLA_QC_ML=rep(0,length(PRES))
# Quenching Test
CHLA_QC_QT=rep(0,length(PRES))
# Density Inversion
CHLA_QC_DI=rep(0,length(PRES))
# STRONG Adjustment
CHLA_QC_SA=rep(0,length(PRES))
# Global range BBP700
BBP700_QC_GR=rep(0,length(PRES))
# Global range BBP532
BBP532_QC_GR=rep(0,length(PRES))
# Negative Spike Test BBP700
BBP700_QC_ST=rep(0,length(PRES))
# Negative Spike Test BBP532
BBP532_QC_ST=rep(0,length(PRES))
# Type radiometry 380
Type_380=rep(0,length(PRES))
# Type radiometry 412
Type_412=rep(0,length(PRES))
# Type radiometry 490
Type_490=rep(0,length(PRES))
# Type radiometry PAR
Type_PAR=rep(0,length(PRES))
##################################################################
# 2. Calculate MLD
# We only take care of Pressure, associated with CTD Measurements
###################################################################
ind_psal=which(PSAL!=99999.)
PSAL[which(PSAL>=99999.)]=NA
TEMP[which(TEMP>=99999.)]=NA
PRES_CTD=PRES[ind_psal]
TEMP_CTD=TEMP[ind_psal]
PSAL_CTD=PSAL[ind_psal]
# calcul de la temperature potentielle
THETA=swTheta(PSAL_CTD,TEMP_CTD,PRES_CTD)
# calcul de l anomalie de densite potentielle
POTDENS=swSigmaTheta(PSAL_CTD,TEMP_CTD,PRES_CTD)
#######################################################
# 2.1 TESTING DENSITY INVERSION (Test. 14)
#######################################################
FLAG_BAD_POTDENS=rep(FALSE,length(PRES_CTD))
if (length(PRES_CTD)>=5) {
for(i in 2 : length(PRES_CTD)) {
if(POTDENS[i]<=(POTDENS[i-1]-0.03)) FLAG_BAD_POTDENS[i-1]=TRUE
}
# without forgetting the last value
if(POTDENS[length(PRES_CTD)]<=(POTDENS[length(PRES_CTD)-1]-0.03)) FLAG_BAD_POTDENS[length(PRES_CTD)]=TRUE
}
CHLA_QC_DI[ind_psal[FLAG_BAD_POTDENS]]=1
#########################################################
# 2.2 Calculating the MLD
#########################################################
POTDENS[which(FLAG_BAD_POTDENS==TRUE)]=NA
ind_pot=which(FLAG_BAD_POTDENS==FALSE)
PRES_POT=PRES_CTD[ind_pot]
POTDENS_POT=POTDENS[ind_pot]
abs_pres_10=abs(PRES_POT-10)
# on calcule la densite potentielle a 10 m
POTDENS_10=max(POTDENS_POT[which(abs_pres_10==min(abs_pres_10))])
# index pour trouver la profondeur de la MLD
# on initialise au max de profondeur au cas ou on ne trouverait pas de MLD
MLD=max(PRES_POT)
if (length(PRES_CTD)>=5) {
MLD_CALC=(POTDENS_POT-POTDENS_10)
for(i in 1 : length(PRES_POT)) {
if (MLD_CALC[i]>MLD_LIMIT){
MLD=PRES_POT[i]
break
}
}
}
###################################
# evaluate the quenching in 90% MLD
###################################
MLD_NPQ=0.9*MLD
###################################################################
# 3. Value at Depth
###################################################################
# TEMPORARY
# define the index for which chlorophyll is correct or not !!!TEMPORARY!!!
ind_chl=which(CHL_RAW!=99999.)
CHL_RAW_CHL=CHL_RAW[ind_chl]
ind_nochl=which(CHL_RAW>=99999.)
CHL_RAW[ind_nochl]=NA
CHLA[ind_nochl]=NA
CHLA_ADJUSTED[ind_nochl]=NA
CHLA_QC[ind_chl]=1
CHLA_QC[ind_nochl]=" "
CHLA_ADJUSTED_QC[ind_chl]=1
CHLA_ADJUSTED_QC[ind_nochl]=" "
PRES_CHL=PRES[ind_chl]
CDOM_RAW[ind_nochl]=NA
CDOM_CHL=CDOM[ind_chl]
CDOM[ind_nochl]=NA
CDOM_ADJUSTED[ind_nochl]=NA
CDOM_QC[ind_chl]=1
CDOM_QC[ind_nochl]=" "
CDOM_ADJUSTED_QC[ind_chl]=1
CDOM_ADJUSTED_QC[ind_nochl]=" "
# END OF TEMPORARY
FLAG_MLD=FALSE # Not mixed
FLAG_NEWDARK=FALSE # No New Dark
FLAG_SP=FALSE # No Small pressure Profile
FLAG_STRONG=FALSE #
NEW_DARK=HIST_CHLA
###################################################################################
# if the calibration changed from the factory calibration the CHLA_QC is set to 3
# and we try to estimate a new value at depth, the 3 values is for the whole profile
###################################################################################
if(HIST_CHLA!=DARK_CHLA) CHLA_QC[ind_chl]=3
###################################################################################
if (length(PRES_CHL)>=5) {
if (max(PRES_CHL)<(MLD+DEPTH_LIMIT+DELTA_DEPTH)){
FLAG_SP=TRUE
FLAG_MLD=TRUE
CHLA_ADJUSTED=SCALE_CHLA*(CHL_RAW-HIST_CHLA)
CHLA_ADJUSTED_QC[ind_chl]=2 # No deep value estimated because of shallow profile so the adjusted value might be good
CHLA_QC_ML[ind_chl]=1
} else { # estimate a new deep value
ind_fond=which((PRES_CHL>=(max(PRES_CHL)-DELTA_DEPTH)))
NEW_DARK=round(median(CHL_RAW_CHL[ind_fond]),0)
if(NEW_DARK!=HIST_CHLA)FLAG_NEWDARK=TRUE
if(abs(NEW_DARK-DARK_CHLA)>0.2*DARK_CHLA){
FLAG_STRONG=TRUE
CHLA_QC_SA[ind_chl]=1
CHLA_QC[ind_chl]=3 # new dark value so the CHLOROPHYLL might be bad
CHLA_ADJUSTED=SCALE_CHLA*(CHL_RAW-HIST_CHLA)
CHLA_ADJUSTED_QC[ind_chl]=3 # Too strong variation for the chlorophyll adjustment
} else {
CHLA_QC[ind_chl]=3 # new dark value so the CHLOROPHYLL might be bad
CHLA_ADJUSTED=SCALE_CHLA*(CHL_RAW-NEW_DARK)
CHLA_ADJUSTED_QC[ind_chl]=1 # new dark value so the CHLOROPHYLL ADJUSTED might be good
HIST_CHLA=NEW_DARK
} # fin difference sup 20%
} # fin else shallow profile or MLD
}
###################################################################################
# FOR CDOM we should stay at the same value at depth if the water mass doesn't change
###################################################################################
if(!mission_2BB)
{
if (length(PRES_CHL)>=5) {
if (max(PRES_CHL)<(MLD+DEPTH_LIMIT+DELTA_DEPTH)){
CDOM_ADJUSTED=SCALE_CDOM*(CDOM_RAW-DARK_CDOM)+SHIFT_CDOM
CDOM_ADJUSTED_QC[ind_chl]=2 # No deep value estimated because of shallow profile so the adjusted value might be good
} else { # estimate a new deep value
ind_fond=which((PRES_CHL>=(max(PRES_CHL)-DELTA_DEPTH)))
FOND_CDOM=median(CDOM_CHL[ind_fond])
if(!FIRST_CDOM_FLAG){
FIRST_CDOM_FLAG=TRUE
FOND_CDOM_HIST=FOND_CDOM
}
# on calcule le shift par rapport a la valeur de fond
if(FOND_CDOM!=FOND_CDOM_HIST){
SHIFT_CDOM=FOND_CDOM_HIST-FOND_CDOM
CDOM_QC[ind_chl]=3
}
CDOM_ADJUSTED=SCALE_CDOM*(CDOM_RAW-DARK_CDOM)+SHIFT_CDOM
CDOM_ADJUSTED_QC[ind_chl]=1 # value adjusted might be good
} # fin else shallow profile or MLD for CDOM
} # fin length PRES_CHL
new_calibration_cdom=paste("Shift CDOM= ",SHIFT_CDOM," FOND_CDOM_HIST= ",FOND_CDOM_HIST,sep="")
##### And setting up the new value
CDOM_CHL=CDOM[ind_chl]
CDOM_FILTERED_CHL=CDOM[ind_chl]
CDOM_ADJUSTED_CHL=CDOM_ADJUSTED[ind_chl]
CDOM_ADJUSTED_FILTERED_CHL=CDOM_ADJUSTED[ind_chl]
}
new_calibration=paste("Factory Calibration, Dark= ",DARK_CHLA,", Actual calibration, Dark= ",HIST_CHLA,sep="")
CHLA_ADJUSTED_CHL=CHLA_ADJUSTED[ind_chl]
CHLA_ADJUSTED_FILTERED_CHL=CHLA_ADJUSTED[ind_chl]
###################################################
### 4. NPQ correction #######################
###################################################
SURF_CHLA_NPQ=0
PRES_NPQ=0
CHLA_NPQ=0
SURF_NORM=0
###### Before anything we must filter the data
#no sliding median if too few values
if(length(ind_chl)>=5)
{
MED_CHL=RunningFilter(2,CHLA_ADJUSTED_CHL,na.fill=T, ends.fill=T, Method="Median")
} else {
MED_CHL=CHLA_ADJUSTED_CHL
}
CHLA_ADJUSTED_FILTERED_CHL=MED_CHL
RES_CHLA_ADJUSTED_CHL=CHLA_ADJUSTED_CHL-MED_CHL
Q10=rep(2*quantile(RES_CHLA_ADJUSTED_CHL,0.10),length(PRES_CHL))
Q90=rep(2*quantile(RES_CHLA_ADJUSTED_CHL,0.90),length(PRES_CHL))
CHLA_ADJUSTED_FILTERED_CHL[which(RES_CHLA_ADJUSTED_CHL>Q90)]=NA
if(!FLAG_MLD){
ind_MLD=which(PRES_CHL<=MLD_NPQ)
CHLA_NPQ=max(CHLA_ADJUSTED_FILTERED_CHL[ind_MLD],na.rm=TRUE)
#print(CHLA_NPQ)
# index du max de chloro
i_NPQ=which.max(CHLA_ADJUSTED_FILTERED_CHL[ind_MLD])
PRES_CHL_NPQ=PRES_CHL[ind_MLD]
# Pression du max de chloro
PRES_NPQ=PRES_CHL_NPQ[i_NPQ]
# on corrige la chloro depuis la valeur du max a la surface
CHLA_ADJUSTED[which(PRES<=PRES_NPQ)]=CHLA_NPQ
CHLA_ADJUSTED[ind_nochl]=NA
# on affecte les QC
if( !FLAG_STRONG & !FLAG_SP ) CHLA_ADJUSTED_QC[which(PRES<=PRES_NPQ)]=8
CHLA_QC[which(PRES<=PRES_NPQ)]=3
CHLA_QC[ind_nochl]=" "
CHLA_ADJUSTED_QC[ind_nochl]=" "
CHLA_QC_QT[which(PRES<=PRES_NPQ)]=1
CHLA_QC_QT[ind_nochl]=0
}
CHLA_ADJUSTED_NONPQ=CHLA_ADJUSTED[ind_chl]
###################################################
### 5. Test 6, Global Range #######################
###################################################
ind_out_of_range=which((CHLA>MAX_RANGE) | (CHLA<MIN_RANGE))
CHLA_QC[ind_out_of_range]=4
ind_out_of_range_adjusted=which((CHLA_ADJUSTED>MAX_RANGE) | (CHLA_ADJUSTED<MIN_RANGE))
CHLA_ADJUSTED_QC[ind_out_of_range_adjusted]=4
CHLA_QC_GR[ind_out_of_range_adjusted]=1
###################################################
### 5. Test 6, Global Range CDOM ################
###################################################
if(!mission_2BB)
{
ind_out_of_range_CDOM=which((CDOM>MAX_RANGE_CDOM) | (CDOM<MIN_RANGE_CDOM))
CDOM_QC[ind_out_of_range_CDOM]=4
ind_out_of_range_CDOM_adjusted=which((CDOM_ADJUSTED>MAX_RANGE_CDOM) | (CDOM_ADJUSTED<MIN_RANGE_CDOM))
CDOM_ADJUSTED_QC[ind_out_of_range_CDOM_adjusted]=4
}
###################################################
### 6. Test 9, Spike Test #######################
###################################################
spike=rep(FALSE,length(PRES_CHL))
if(!FLAG_MLD){
spike[which((RES_CHLA_ADJUSTED_CHL<Q10) & (PRES_CHL > MLD))]=TRUE
} else {
spike[which(RES_CHLA_ADJUSTED_CHL<Q10)]=TRUE
}
ind_spike=ind_chl[spike]
CHLA_QC[ind_spike]=4
CHLA_ADJUSTED_QC[ind_spike]=4
CHLA_QC_ST[ind_spike]=1
###################################################
### 6. Test 9, Spike Test CDOM ##################
###################################################
if(!mission_2BB)
{
spike1_cdom=rep(FALSE,length(PRES_CHL))
spike2_cdom=rep(FALSE,length(PRES_CHL))
# Test 6.1
CDOM_RAW_CHL=CDOM_RAW[ind_chl]
CDOM_Q25=quantile(CDOM_RAW_CHL,0.25)
CDOM_Q75=quantile(CDOM_RAW_CHL,0.75)
IQR=(CDOM_Q75-CDOM_Q25)
spike1_cdom[which((CDOM_RAW_CHL>CDOM_Q75+2*IQR)|(CDOM_RAW_CHL<CDOM_Q25-2*IQR))]=TRUE
ind_spike1_cdom=ind_chl[spike1_cdom]
# Test 6.2
#no sliding median if too few values
if(length(ind_chl)>=51)
{
MED_CDOM_smooth=RunningFilter(25,CDOM_RAW_CHL,na.fill=T, ends.fill=T, Method="Average")
} else {
MED_CDOM_smooth=CDOM_RAW_CHL
}
spike2_cdom[which(abs(CDOM_RAW_CHL-MED_CDOM_smooth)>4)]=TRUE
ind_spike_cdom=ind_chl[spike1_cdom | spike2_cdom]
CDOM_QC[ind_spike_cdom]=4
CDOM_ADJUSTED_QC[ind_spike_cdom]=4
}
##################################################################
### 7. Additional Test : Pressure monotonically increasing
##################################################################
#FLAG_BAD_PRES=rep(FALSE,length(PRES_CHL))
#for(i in 2 : length(PRES_CHL)) {
# if((PRES_CHL[i]<=PRES_CHL[i-1])) FLAG_BAD_PRES[i-1]=TRUE
#}
# without forgetting the last value
#if(PRES_CHL[length(PRES_CHL)]<=PRES_CHL[length(PRES_CHL)-1]) FLAG_BAD_PRES[length(PRES_CHL)]=TRUE
#ind_bad_pres=ind_chl[FLAG_BAD_PRES]
#CHLA_QC[ind_bad_pres]=4
#CHLA_ADJUSTED_QC[ind_bad_pres]=4
###############################################################################
#######################################################################
# 7.a Work on BBP
#######################################################################
# Interpolate TEMP and PSAL through the whole profile
# and also extrapolate when needed rule=2
if (length(PRES_CTD)>=5)
{
TEMP_PRES=approx(PRES_CTD,TEMP_CTD,PRES,method="linear",rule=2)
TEMP_CHL=TEMP_PRES$y[ind_chl]
PSAL_PRES=approx(PRES_CTD,PSAL_CTD,PRES,method="linear",rule=2)
PSAL_CHL=PSAL_PRES$y[ind_chl]
# initialisation
# TEST sur le nom de mission au cas ou il y aurait deux BBP
BETASW124_CHL=PRES_CHL
# BB a 700
lambda=700
# Must set up a loop to calculate Betasw
for(i in 1 : length(PRES_CHL)) {
BETASW124_CHL[i]=calc_betasw124(lambda,PSAL_CHL[i],TEMP_CHL[i])
}
BETASW700[ind_chl]=BETASW124_CHL
BETASW700[ind_nochl]=NA
BACKSCATTERING[ind_nochl]=NA
BBP700=2*pi*1.076*(BACKSCATTERING-BETASW700)
BBP700[ind_nochl]=NA
BBP700_CHL=BBP700[ind_chl]
##############################
# missions a deux BB
##############################
if(mission_2BB)
{
lambda=532
# Must set up a loop to calculate Betasw
for(i in 1 : length(PRES_CHL)) {
BETASW124_CHL[i]=calc_betasw124(lambda,PSAL_CHL[i],TEMP_CHL[i])
}
BETASW532[ind_chl]=BETASW124_CHL
BETASW532[ind_nochl]=NA
CDOM[ind_nochl]=NA
BBP532=2*pi*1.076*(CDOM-BETASW532)
BBP532[ind_nochl]=NA
BBP532_CHL=BBP532[ind_chl]
#######################################################################
# PERFORM RT_QC for BBP532
#######################################################################
# a priori data are considered as correct
BBP532_QC[ind_chl]=1
BBP532_QC[ind_nochl]=" "
#### Global Range test ####
ind_out_of_range_BBP532=which((BBP532>MAX_RANGE_BBP) | (BBP532<MIN_RANGE_BBP532))
BBP532_QC[ind_out_of_range_BBP532]=3
BBP532_QC_GR[ind_out_of_range_BBP532]=1
#### BBP bad Offset and spike test ####
###### Before anything we must filter the data (sliding Median on 5 values)
if(length(ind_chl)>=5)
{
BBP532_MED_CHL=RunningFilter(2,BBP532_CHL,na.fill=T, ends.fill=T, Method="Median")
} else {
BBP532_MED_CHL=BBP532_CHL
}
# Spike test
RES_BBP532_CHL=BBP532_CHL-BBP532_MED_CHL
BBP532_Q10=rep(2*quantile(RES_BBP532_CHL,0.10),length(PRES_CHL))
spike_BBP532=rep(FALSE,length(PRES_CHL))
spike_BBP532[which(RES_BBP532_CHL<BBP532_Q10)]=TRUE
ind_spike_BBP532=ind_chl[spike_BBP532]
BBP532_QC[ind_spike_BBP532]=4
BBP532_QC_ST[ind_spike_BBP532]=1
# Bad Offset Test
BAD_OFFSET_532=-20*min(BBP532_MED_CHL)
BO_BBP532_H=rep(FALSE,length(PRES_CHL))
BO_BBP532_M=rep(FALSE,length(PRES_CHL))
ind_thresh_BBP532=which(BBP532_MED_CHL<MIN_RANGE_BBP532)
if(length(ind_thresh_BBP532)>0 ){
if(BBP532_CHL[ind_thresh_BBP532]>=BAD_OFFSET_532)
{
BO_BBP532_M[ind_thresh_BBP532]=TRUE
} else {
print("ok")
BO_BBP532_H[ind_thresh_BBP532]=TRUE
}
}
ind_BO_BBP532_M=ind_chl[BO_BBP532_M]
ind_BO_BBP532_H=ind_chl[BO_BBP532_H]
if(length(ind_BO_BBP532_H)>0){
if(BBP532_QC[ind_BO_BBP532_H]!=4)BBP532_QC[ind_BO_BBP532_H]=3
}
if(length(ind_BO_BBP532_M)>0){
if(BBP532_QC[ind_BO_BBP532_M]!=4)BBP532_QC[ind_BO_BBP532_M]=2
}
}# End mission 2BB
#######################################################################
# 7.b PERFORM RT_QC for BBP
#######################################################################
# a priori data are considered as correct
BBP700_QC[ind_chl]=1
BBP700_QC[ind_nochl]=" "
#BBP700_ADJUSTED_QC[ind_chl]=1
#BBP700_ADJUSTED_QC[ind_nochl]=" "
#### Global Range test ####
ind_out_of_range_BBP=which((BBP700>MAX_RANGE_BBP) | (BBP700<MIN_RANGE_BBP700))
BBP700_QC[ind_out_of_range_BBP]=3
BBP700_QC_GR[ind_out_of_range_BBP]=1
#### BBP bad Offset and spike test ####
###### Before anything we must filter the data (sliding Median on 5 values)
if(length(ind_chl)>=5)
{
BBP700_MED_CHL=RunningFilter(2,BBP700_CHL,na.fill=T, ends.fill=T, Method="Median")
} else {
BBP700_MED_CHL=BBP700_CHL
}
# Restore spike test
# Lets calculate the residual between the signal and the sliding median
RES_BBP700_CHL=BBP700_CHL-BBP700_MED_CHL
BBP700_Q10=rep(2*quantile(RES_BBP700_CHL,0.10),length(PRES_CHL))
spike_BBP700=rep(FALSE,length(PRES_CHL))
spike_BBP700[which(RES_BBP700_CHL<BBP700_Q10)]=TRUE
ind_spike_BBP700=ind_chl[spike_BBP700]
BBP700_QC[ind_spike_BBP700]=4
BBP700_QC_ST[ind_spike_BBP700]=1
## Bad Offset test
BAD_OFFSET_700=-20*min(BBP700_MED_CHL)
BO_BBP700_H=rep(FALSE,length(PRES_CHL))
BO_BBP700_M=rep(FALSE,length(PRES_CHL))
ind_thresh_BBP700=which(BBP700_MED_CHL<MIN_RANGE_BBP700)
if(length(ind_thresh_BBP700)>0 ){
if(BBP700_CHL[ind_thresh_BBP700]>=BAD_OFFSET_700)
{
BO_BBP700_M[ind_thresh_BBP700]=TRUE
} else {
print("ok")
BO_BBP700_H[ind_thresh_BBP700]=TRUE
}
}
ind_BO_BBP700_M=ind_chl[BO_BBP700_M]
ind_BO_BBP700_H=ind_chl[BO_BBP700_H]
if(length(ind_BO_BBP700_H)>0){
if(BBP700_QC[ind_BO_BBP700_H]!=4)BBP700_QC[ind_BO_BBP700_H]=3
}
if(length(ind_BO_BBP700_M)>0){
if(BBP700_QC[ind_BO_BBP700_M]!=4)BBP700_QC[ind_BO_BBP700_M]=2
}
} else {
BBP700_QC[ind_chl]=4
if(mission_2BB)
{
BBP532_QC[ind_chl]=4
}