-
Notifications
You must be signed in to change notification settings - Fork 1
/
Quenching_correction.R
405 lines (236 loc) · 11.7 KB
/
Quenching_correction.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
########################################################################
## Quenching Correction based on Louis Terrats Work
##
## Inputs : Dark corrected CHLA profiles
##
## Version : 13 January 2021
## Version : 22 November 2021
##
## Bug Correction : light threshold and same value in the first 10m
########################################################################
library(ncdf4)
library(stringr)
source("./MLD_calc.R")
source("./IPAR_15_DEPTH.R")
source("./RunningFilter.R")
uf=commandArgs()
file_dark <- uf[2]
### reading the dark value
DARK=read.table(as.character(file_dark),header=FALSE, as.is=TRUE)
DARK_CHLA=as.numeric(DARK$V1)
#### Creating the list of files for which we need to recompute
liste_to_do=read.table("liste_all_B",header=FALSE, as.is=TRUE)
# List of the file to process
LIST_nc=liste_to_do$V1
for (IDnc in LIST_nc) {
### IDnc
print(IDnc)
### Initialisation FLAG_SHALLOW
FLAG_SHALLOW=FALSE
### Initialisation FLAG_SHALLOW
FLAG_NO_PAR=FALSE
#### Getting the name of the core file
file_in_C=str_replace(IDnc,"/B","/")
# if B and C are not in the same mode
if (!file.exists(file_in_C)) file_in_C=str_replace(file_in_C,"profiles/R","profiles/D")
if (!file.exists(file_in_C)) file_in_C=str_replace(file_in_C,"profiles/D","profiles/R")
#########################
# Open the nc File
#########################
# Open the C file
filenc_C=nc_open(file_in_C,readunlim=FALSE,write=FALSE)
# Open the B file
filenc_B=nc_open(IDnc,readunlim=FALSE,write=TRUE)
############################################################
## work on index
#############################################################
#### Get the list of parameters in the profile
STATION_PARAMETERS=ncvar_get(filenc_B,"STATION_PARAMETERS")
# Stations parameters has a fixed length 64 characters
CHLA_STRING=str_pad("CHLA",64,"right")
PAR_STRING=str_pad("DOWNWELLING_PAR",64,"right")
# Find the profile containing CHLA/BBP and PAR
index_chla=which(STATION_PARAMETERS == CHLA_STRING, arr.ind=TRUE)
index_par=which(STATION_PARAMETERS == PAR_STRING, arr.ind=TRUE)
if (length(index_chla)==0) {
next # jump to the next profile if no chla in the profile
} else {
iprof_chla=index_chla[,2]
}
index_par=which(STATION_PARAMETERS == PAR_STRING, arr.ind=TRUE)
if (length(index_par)==0) {
FLAG_NO_PAR=TRUE
} else {
iprof_par=index_par[,2]
}
##########################################################
# MLD estimation
##########################################################
#### Read the C file to estimate the MLD
TEMP_CTD=ncvar_get(filenc_C,"TEMP")
PSAL_CTD=ncvar_get(filenc_C,"PSAL")
PRES_CTD=ncvar_get(filenc_C,"PRES")
MLD=MLD_calc(PRES_CTD, PSAL_CTD , TEMP_CTD)
###############################################################################
######## Jump to next profile or dont take PAR into account if PRES_QC is too bad
###############################################################################
PROFILE_PRES_CTD_QC=strsplit(ncvar_get(filenc_C,"PROFILE_PRES_QC"),split="")
if (PROFILE_PRES_CTD_QC[[1]][iprof_chla]=="E" | PROFILE_PRES_CTD_QC[[1]][iprof_chla]=="F") next
if ( !FLAG_NO_PAR ) {
if (PROFILE_PRES_CTD_QC[[1]][iprof_par]=="E" | PROFILE_PRES_CTD_QC[[1]][iprof_par]=="F") FLAG_NO_PAR=TRUE
}
###########################################################
# Light Threshold
###########################################################
#### ipar_15_depth
if ( !FLAG_NO_PAR ) {
DOWNWELLING_PAR=ncvar_get(filenc_B,"DOWNWELLING_PAR")
##FEV22 Filtering the par to get the correct depth of ipar15
FILTER_PAR=DOWNWELLING_PAR
FILTER_PAR[,iprof_par]=RunningFilter(2,DOWNWELLING_PAR[,iprof_par],na.fill=T, ends.fill=T, Method="Median")
if (length(which(!is.na(DOWNWELLING_PAR[,iprof_par]))) < 2 ) FLAG_NO_PAR=TRUE
ipar_15_depth=IPAR_15_DEPTH(PRES_CTD,DOWNWELLING_PAR)
print(ipar_15_depth)
ipar_15_depth=IPAR_15_DEPTH(PRES_CTD,FILTER_PAR)
print(ipar_15_depth)
} else {
ipar_15_depth=MLD
}
###########################################################
# Testing shallow mixing
###########################################################
if (ipar_15_depth > MLD) FLAG_SHALLOW=TRUE
############################################################
# Get PAR and CHLA on the same levels
############################################################
#### Working on PAR
if (!FLAG_NO_PAR) {
PAR_CHLA=approx(PRES_CTD[,iprof_par],FILTER_PAR[,iprof_par],PRES_CTD[,iprof_chla], rule=2)$y
#### Filtering a bit the PAR
MED_PAR_CHLA=RunningFilter(2,PAR_CHLA,na.fill=T, ends.fill=T, Method="Median")
}
################################################################
#### Getting BBP700 and CHLA
################################################################
### Getting CHLA
CHLA=ncvar_get(filenc_B,"CHLA")
### DARK correction with input from quentin's Estimation
CHLA=CHLA-DARK_CHLA
### Get the CHLA_QC and change them into CHLA_ADJUSTED_QC
CHLA_ADJUSTED_QC=ncvar_get(filenc_B,"CHLA_QC")
### PROFILE_CHLA
PROFILE_CHLA_QC=str_squish(ncvar_get(filenc_B,"PROFILE_CHLA_QC"))
### Initialising the CHLA derived variables without the quenching
MED_CHLA=CHLA # unspiked CHLA
CHLA_NPQ=CHLA # final CHLA corrected from NPQ and SPIKED
CHLA_NPQ_D=CHLA # CHLA corrected from NPQ and despiked
### Getting BBP
BBP700=ncvar_get(filenc_B,"BBP700")
MED_BBP700=BBP700 # Filtered BBP
############################################################################################
# Filtering the signal To remove the spikes for BBP and CHLA (Briggs et al.)
# 1. median filter 5
# 2. Mean filter 7
############################################################################################
### 1st median filter 5
MED_CHLA[,iprof_chla]=RunningFilter(2,CHLA[,iprof_chla],na.fill=T, ends.fill=T, Method="Median")
MED_BBP700[,iprof_chla]=RunningFilter(2,BBP700[,iprof_chla],na.fill=T, ends.fill=T, Method="Median")
### 2nd Mean filter 7 for BBP and CHLA
MED_CHLA[,iprof_chla]=RunningFilter(3,MED_CHLA[,iprof_chla],na.fill=T, ends.fill=T, Method="Mean")
MED_BBP700[,iprof_chla]=RunningFilter(3,MED_BBP700[,iprof_chla],na.fill=T, ends.fill=T, Method="Mean")
### Isolating the CHLA spikes
SPIKE_CHLA=CHLA-MED_CHLA
### index of the MLD
i_mld=which.min(abs(PRES_CTD[,iprof_chla]-MLD))
### index of ipar15
i_ipar15=which.min(abs(PRES_CTD[,iprof_chla]-ipar_15_depth))
### inndex of i_10
i_10=which.min(abs(PRES_CTD[,iprof_chla]-10))
### RAPPORT in the MLD with the light limitation
RAPP_in_MLD=(MED_CHLA[1:i_ipar15,iprof_chla])/(MED_BBP700[1:i_ipar15,iprof_chla])
### Sackmann, max of the Rapp between CHLA and BBP
i_sack=which.max(RAPP_in_MLD)
#######################################################################
### Estimating the NPQ correction on a smoothed CHLA
#######################################################################
if (FLAG_SHALLOW & !FLAG_NO_PAR) {
# sigmoid
# CHLA_NPQ_D[(i_mld+1):i_ipar15,iprof_chla]=MED_CHLA[(i_mld+1):i_ipar15,iprof_chla]/(0.092+0.908/(1+(MED_PAR_CHLA[(i_mld+1):i_ipar15]/261)^2.2))
CHLA_NPQ_D[i_mld:i_ipar15,iprof_chla]=MED_CHLA[i_mld:i_ipar15,iprof_chla]/(0.092+0.908/(1+(MED_PAR_CHLA[i_mld:i_ipar15]/261)^2.2))
RAPP_at_MLD=(CHLA_NPQ_D[i_mld,iprof_chla])/(MED_BBP700[i_mld,iprof_chla])
# shallow than the MLD
CHLA_NPQ_D[1:i_mld,iprof_chla]=RAPP_at_MLD*MED_BBP700[1:i_mld,iprof_chla]
} else {
CHLA_NPQ_D[1:i_sack,iprof_chla]=MED_BBP700[1:i_sack,iprof_chla]*max(RAPP_in_MLD)
} # end if FLAG_SHALLOW
######## CATHERINE #############################################################################
######## Double check that we won't miss a max in the MLD
if ( length(which(CHLA_NPQ_D[1:i_mld,iprof_chla]<CHLA[1:i_mld,iprof_chla])) > 5 & median(CHLA_NPQ_D[1:i_mld,iprof_chla]-CHLA[1:i_mld,iprof_chla])>0.05 ) {
CHLA_NPQ_D=CHLA
CHLA_NPQ_D[1:i_sack,iprof_chla]=MED_BBP700[1:i_sack,iprof_chla]*max(RAPP_in_MLD)
}
######## CATHERINE ##############################################################################
#########################################################################################
#### Estimating the Q_NPQ (factor of correction to apply to correct of the quenching) till i_npq index of the pressure level
#########################################################################################
i_npq=max(i_ipar15,i_sack)
Q_NPQ=CHLA_NPQ_D[1:i_npq,iprof_chla]/MED_CHLA[1:i_npq,iprof_chla]
Q_NPQ[which(abs(MED_CHLA[1:i_npq,iprof_chla])<0.007)]=NA ### no other idea for that point
#### Only make sense when Q_NPQ > 0 and apply a smooth filtering
Q_NPQ[which(Q_NPQ<=0)]=NA
#MED_Q_NPQ=RunningFilter(2,Q_NPQ,na.fill=T, ends.fill=T, Method="Median")
#########################################################################################
### Correct the spikes of the quenching
#########################################################################################
#SPIKE_CHLA_NPQ=SPIKE_CHLA[1:i_npq,iprof_chla]*MED_Q_NPQ
#SPIKE_CHLA_NPQ[is.na(SPIKE_CHLA_NPQ)]=0.0
#########################################################################################
### Put the spikes back on the CHLA filtered corrected from the Quenching
#########################################################################################
# CHLA_NPQ[1:i_npq,iprof_chla]=CHLA_NPQ_D[1:i_npq,iprof_chla]+SPIKE_CHLA_NPQ
CHLA_NPQ[1:i_npq,iprof_chla]=CHLA_NPQ_D[1:i_npq,iprof_chla]
# Same value in the first 10m Try_cat (Xing 2018)
print(CHLA_NPQ_D[i_10,iprof_chla])
print(median(CHLA_NPQ[1:i_10,iprof_chla]))
if(i_sack>i_10)CHLA_NPQ[1:i_10,iprof_chla]=median(CHLA_NPQ_D[1:i_10,iprof_chla])
#########################################
# PLOT
#########################################
MINDEPTH=max(PRES_CTD[,iprof_chla])
MAXCHLA=max(CHLA_NPQ,na.rm=TRUE)
MINDEPTH=MLD+50
name_file=str_sub(IDnc,str_length(IDnc)-15,str_length(IDnc)-3)
png(file=paste(name_file,".png",sep=""))
matplot(CHLA_NPQ[,iprof_chla],PRES_CTD[,iprof_chla],col=2,lwd=2,type="l",ylab="Depth [m]",cex.lab=1.5,cex.axis=1.5,xlab=expression("Chlorophyll a [mg."*m ^ -3 * "]"),xlim=c(-0.2,MAXCHLA+0.5),ylim=rev(c(0, MINDEPTH)))
matplot(CHLA[,iprof_chla],PRES_CTD[,iprof_chla],col=5,lwd=2,type="l",ylab="Depth [m]",cex.lab=1.5,cex.axis=1.5,xlab=expression("Chlorophyll a [mg."*m ^ -3 * "]"),xlim=c(-0.2,MAXCHLA+0.5),ylim=rev(c(0, MINDEPTH)),add=TRUE)
legend("bottomright",c("CHLA_NPQ","CHLA"),pch=c(".","."),lwd=c(2,2),col=c(2,5),lty=c(1,1),cex=1.2)
#text(MAXJULD-100,MINGAIN-0.1,paste("mean GAIN m =",round(mean(GAIN_month,na.rm=TRUE),4)," error from sd =",err_month))
#text(MAXJULD-100,MINGAIN-0.15,paste("mean GAIN a =",round(mean(GAIN_ann,na.rm=TRUE),4)," error from sd =",err_ann))
dev.off()
###############################################################
#### changing the NPQ_QC in CHLA_ADJUSTED_QC (to confirm)
###############################################################
N_QC=nchar(CHLA_ADJUSTED_QC[iprof_chla])
QC_test=unlist(strsplit(CHLA_ADJUSTED_QC[iprof_chla],split="")) # to keep 4 set by GL
if ( length(which(QC_test != "4" | QC_test != " ")) == 0 ) PROFILE_QC = "GL"
index_qc=which(!is.na(CHLA[,iprof_chla]))
for (i_qc in seq(1,length(index_qc))) {
j=index_qc[i_qc] # to avoid _ filled_values
if ( j <= i_npq) {
substr(CHLA_ADJUSTED_QC[iprof_chla],j,j)<-as.character(5) ### Quenching QC
} else {
substr(CHLA_ADJUSTED_QC[iprof_chla],j,j)<-as.character(1)
}
#### and redo the Range Checking
if ( CHLA_NPQ[j,iprof_chla] < -0.1 | CHLA_NPQ[j,iprof_chla] > 50. | PROFILE_CHLA_QC == "F" ) {
substr(CHLA_ADJUSTED_QC[iprof_chla],j,j)<-as.character(4)
}
}
###########################################
# Write in the working files
###########################################
ncvar_put(filenc_B,"CHLA_ADJUSTED",CHLA_NPQ)
ncvar_put(filenc_B,"CHLA_ADJUSTED_QC",CHLA_ADJUSTED_QC)
nc_close(filenc_B)
nc_close(filenc_C)
}# end loop on B files