-
Notifications
You must be signed in to change notification settings - Fork 0
/
WRITE_DM.R
498 lines (310 loc) · 14.5 KB
/
WRITE_DM.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
#######################################################################################
# This program is designed to modify a BR file into BD file
# Catherine Schmechtig 2018 July
#
# V1.0 20190503 : Initial version
# V1.1 20190517 : robust to CTD
# V1.2 20190628 : OFFSET for DOXY out of drift
# V1.3 20200325 : Correct an issue on QC index
#
# -With an estimation of PARAM_ADJUSTED with a drift, a slope, an offset
# -Change the QC
# -Check DATA_MODE (If not already set to D)
# -Change the DATA_STATE_INDICATOR (Table 6 - Argo user Manual )
# -Change the PARAMETER_DATA_MODE
# -Calculate the PROFILE_PARAM_QC
# -Change the HISTORY section ( + Increase )
# -HISTORY_INSTITUTION (Table 4)
# -HISTORY_STEP (Table 12)
# -HISTORY_DATE
# -HISTORY_SOFTWARE (if applicable)
# -HISTORY_SOFTWARE_RELEASE (if applicable)
# -HISTORY_ACTION (Table 7)
#
# -Change the SCIENTIFIC_CALIB_xxx Section
# -SCIENTIFIC_CALIB_COMMENT
# -SCIENTIFIC_CALIB_COEFFICIENT
# -SCIENTIFIC_CALIB_EQUATION
# -SCIENTIFIC_CALIB_DATE
#
# -Inputs :
# output of the script ./Fill_DM.sh
# Bfiles
# -Output :
# Bfiles with a change in DM for one parameter
########################################################################################
library(ncdf4)
library(stringr)
require(oce)
source("./READ_CTD.R")
source("./PPOX_to_DOXY.R")
source("./DOXY_to_PPOX.R")
source("./DOXY_adj.R")
uf=commandArgs()
###################################################################################
# READ the INPUT FILE
###################################################################################
input_file <- uf[2]
input=read.table(file=input_file,header=TRUE,sep=";")
# header
# filename;filename_core;metadata_file;param;type;offset;slope;drift;param_error;qc;scientific_comment;date_update
# List of the file to process
LIST_nc=input$filename
# List of the associated core file
LIST_nc_core=input$filename_core
# Build the Variable Name
PARAM_name=input$param
# Get the type of correction (AD, Adjustment - GL, Grey list)
CORRECTION_TYPE=as.character(input$type)
# Get the correction values
OFFSET=input$offset
SLOPE=input$slope
DRIFT=input$drift
# Get the error
ERROR=input$param_error
#get the new value of the QC
PARAM_ADJUSTED_QC_value=input$qc
# Get the scientific Comment
scientific_comment=input$scientific_comment
# Get the date of the correction
date_update=as.character(input$date_update)
# Get the launch date to calculate the Drift (between the launch date and the date of the profile)
metadatafile=as.character(input$metadata_file)
#####################################################################################
# Loop on all the files in the List
#####################################################################################
for (i in seq(1,length(LIST_nc))) {
print(i)
FLAG_CTD=TRUE
IDnc=as.character(LIST_nc[i])
IDnc_core=as.character(LIST_nc_core[i])
# Open the B file
filenc=nc_open(IDnc,readunlim=FALSE,write=TRUE)
# Open the metadata file
filenc_meta=nc_open(metadatafile[i],readunlim=FALSE,write=FALSE)
# Open the core file
filenc_core=nc_open(IDnc_core,readunlim=FALSE,write=FALSE)
###################################################################################
#### Work on variable Name
###################################################################################
PARAM_ADJUSTED_name=paste(PARAM_name[i],"_ADJUSTED",sep="")
PARAM_ADJUSTED_QC_name=paste(PARAM_ADJUSTED_name,"_QC",sep="")
PARAM_QC_name=paste(PARAM_name[i],"_QC",sep="")
PARAM_ADJUSTED_ERROR_name=paste(PARAM_ADJUSTED_name,"_ERROR",sep="")
PARAM_STRING=str_pad(PARAM_name[i],64,"right")
PROFILE_PARAM_QC_name=paste("PROFILE_",PARAM_name[i],"_QC",sep="")
###################################################################################
#### Read the file
###################################################################################
PARAMETER=ncvar_get(filenc,"PARAMETER")
index_param=which(PARAMETER == PARAM_STRING , arr.ind=TRUE)
### Very IMPORTANT
### Next iteration if the parameter is not in the file
if ( length(index_param)==0 ) {
next
}
PARAM=ncvar_get(filenc,as.character(PARAM_name[i]))
PARAM_ADJUSTED=ncvar_get(filenc,as.character(PARAM_ADJUSTED_name))
PARAM_ADJUSTED_ERROR=ncvar_get(filenc,as.character(PARAM_ADJUSTED_ERROR_name))
PROFILE_PARAM_QC=ncvar_get(filenc,PROFILE_PARAM_QC_name)
PARAMETER_DATA_MODE=ncvar_get(filenc,"PARAMETER_DATA_MODE")
if ( (PARAM_name[i] == "DOXY") | (CORRECTION_TYPE[i] == "GL") ) {
PARAM_ADJUSTED_QC=ncvar_get(filenc,PARAM_QC_name)
} else {
PARAM_ADJUSTED_QC=ncvar_get(filenc,PARAM_ADJUSTED_QC_name)
}
####################################################################################
# PARAM_ADJUSTED Estimation
####################################################################################
# Please Check that we should work on Raw data and not on Adjusted Data but it could be an issue for quenching and factor 2 CATSCHM
if ( CORRECTION_TYPE[i] == "AD" ) {
### For the Drift calculation we have to work on date
### First some Work on the date
# get the Launch date in the metadata file
launch_date=ncvar_get(filenc_meta,"LAUNCH_DATE")
launch_date_juld=as.numeric(julian(as.POSIXlt(launch_date,format="%Y%m%d%H%M%S",origin="1950-01-01"),origin="1950-01-01",TZ="GMT"))
# get the actual date in the profile file
profile_date_juld=unique(ncvar_get(filenc,"JULD"))
# For DOXY we need to go through the adjustment of PPOX_DOXY
if ( PARAM_name[i] == "DOXY" ) {
DOXY_ADJ=DOXY_adj(filenc_core, filenc, PARAM_name[i] ,OFFSET[i], SLOPE[i], DRIFT[i], ERROR[i], profile_date_juld ,launch_date_juld )
PARAM_ADJUSTED=DOXY_ADJ$DOXY
PARAM_ADJUSTED_ERROR=DOXY_ADJ$DOXY_ERROR
FLAG_CTD=DOXY_ADJ$FLAG_CTD
} else {
PARAM_ADJUSTED=as.numeric((DRIFT[i]/100.*(profile_date_juld-launch_date_juld)/365.))+(SLOPE[i]*PARAM+OFFSET[i])
PARAM_ADJUSTED_ERROR=replace(PARAM,!is.na(PARAM),ERROR[i])
}
} else {
PARAM_ADJUSTED=rep(NA,length(PARAM_ADJUSTED))
PARAM_ADJUSTED_ERROR=rep(NA,length(PARAM_ADJUSTED))
}
####################################################################################
#### Work on index of the correction
#### We should get information from PARAMETER which is the list of PARAMETER
#### with CALIBRATION information
####################################################################################
for ( irow in seq(1,nrow(index_param))) {
i_param_param =index_param[irow,1]
i_calib_param =index_param[irow,2] # Check how N-CALIB increases CATSCHM
i_prof_param = index_param[irow,3]
index_scientific=append(index_param[irow,],1,after=0) # build the index to write the scientific_calib_comment at the right place
####################################################################################
# PARAM_ADJUSTED_QC MODIFICATION
####################################################################################
## Check how many QC should be set/change
if ((i_prof_param == 1) && nrow(index_param) == 1 ) {
N_QC= length(which(!is.na(PARAM)))
index_qc=which(!is.na(PARAM))
} else {
N_QC=length(which(!is.na(PARAM[,i_prof_param])))
index_qc=which(!is.na(PARAM[,i_prof_param]))
}
if ( N_QC > 0 ) {
if ( CORRECTION_TYPE[i] == "AD" ) {
for (i_qc in seq(1,N_QC)) {
j=index_qc[i_qc]
QC_test=substr(PARAM_ADJUSTED_QC[i_prof_param], j, j)
if ( ( QC_test == "3") || ( QC_test == "2") || (QC_test == " ") || ( QC_test == "1") ) { # To check
substr(PARAM_ADJUSTED_QC[i_prof_param], j , j)<- as.character(PARAM_ADJUSTED_QC_value[i])
}
if ( ( QC_test == "4" ) || ( PARAM_ADJUSTED_QC_value[i] == "4" ) ) { ## Fill value for QC=4 and QC=9
if (i_prof_param == 1 && nrow(index_param) == 1 ) {
PARAM_ADJUSTED[j]=NA
PARAM_ADJUSTED_ERROR[j]=NA
} else {
PARAM_ADJUSTED[j,i_prof_param]=NA
PARAM_ADJUSTED_ERROR[j,i_prof_param]=NA
}
}
}
} else {
for (i_qc in seq(1,N_QC)) {
j=index_qc[i_qc]
substr(PARAM_ADJUSTED_QC[i_prof_param], j , j) <- as.character(PARAM_ADJUSTED_QC_value[i])
}
}
}
####################################################################################
# SCIENTIFIC_CALIB_SECTION
####################################################################################
# scientific calib comment
SCIENTIFIC_CALIB_COMMENT=str_pad(scientific_comment[i],256,"right")
if (!FLAG_CTD) SCIENTIFIC_CALIB_COMMENT=str_pad("no adjustment is performed because of issues in CTD",256,"right")
ncvar_put(filenc,"SCIENTIFIC_CALIB_COMMENT",SCIENTIFIC_CALIB_COMMENT,start=index_scientific,count=c(256,1,1,1))
# scientific calib Coefficient and equation
if ( CORRECTION_TYPE[i] == "AD") {
scientific_coefficient=paste("OFFSET=",OFFSET[i],", SLOPE=",SLOPE[i],", DRIFT=",DRIFT[i])
if ( PARAM_name[i] == "DOXY" ) {
scientific_equation=paste("PPOX_ADJUSTED=OFFSET+(PPOX*SLOPE)*(1+DRIFT/100.*(profile_date_juld-launch_date_juld)/365.)")
if (!FLAG_CTD) {
scientific_coefficient="none"
scientific_equation="none"
}
} else {
scientific_equation=paste(PARAM_ADJUSTED_name,"=(",PARAM_name,"*SLOPE+OFFSET)+(DRIFT/100.*(profile_date_juld-launch_date_juld)/365.)")
}
} else {
scientific_coefficient="none"
scientific_equation="none"
}
SCIENTIFIC_CALIB_COEFFICIENT=str_pad(scientific_coefficient,256,"right")
ncvar_put(filenc,"SCIENTIFIC_CALIB_COEFFICIENT",SCIENTIFIC_CALIB_COEFFICIENT,start=index_scientific,count=c(256,1,1,1))
SCIENTIFIC_CALIB_EQUATION=str_pad(scientific_equation,256,"right")
ncvar_put(filenc,"SCIENTIFIC_CALIB_EQUATION",SCIENTIFIC_CALIB_EQUATION,start=index_scientific,count=c(256,1,1,1))
# Scientific calib date
ncvar_put(filenc,"SCIENTIFIC_CALIB_DATE",date_update[i],start=index_scientific,count=c(14,1,1,1))
#####################################################################################
# History Section
#####################################################################################
# N_HISTORY
N_HISTORY=filenc$dim[['N_HISTORY']]$len
i_history=N_HISTORY+1
### HISTORY INSTITUTION
## We should ask for Data center for table 4 of the argo user's manual (VF for VilleFranche ?) CATSCHM
HISTORY_INSTITUTION="VF "
ncvar_put(filenc,"HISTORY_INSTITUTION",HISTORY_INSTITUTION,start=c(1,i_prof_param,i_history),count=c(4,1,1))
### HISTORY_STEP
### Delayed mode code
HISTORY_STEP="ARSQ"
ncvar_put(filenc,"HISTORY_STEP",HISTORY_STEP,start=c(1,i_prof_param,i_history),count=c(4,1,1))
### HISTORY SOFTWARE Delayed Mode Filler Tool ;-)
HISTORY_SOFTWARE="DMFT"
ncvar_put(filenc,"HISTORY_SOFTWARE",HISTORY_SOFTWARE,start=c(1,i_prof_param,i_history),count=c(4,1,1))
### HISTORY SOFTWARE RELEASE ;-) My first version !!
HISTORY_SOFTWARE_RELEASE="V1.4"
ncvar_put(filenc,"HISTORY_SOFTWARE_RELEASE",HISTORY_SOFTWARE_RELEASE,start=c(1,i_prof_param,i_history),count=c(4,1,1))
### HISTORY_DATE (Same as Date update)
ncvar_put(filenc,"HISTORY_DATE",date_update[i],start=c(1,i_prof_param,i_history),count=c(14,1,1))
### HISTORY_ACTION (Change Value CV - I don't know If I should also said CF)
HISTORY_ACTION="CV "
ncvar_put(filenc,"HISTORY_ACTION",HISTORY_ACTION,start=c(1,i_prof_param,i_history),count=c(4,1,1))
#####################################################################################
# PROFILE_PARAM_QC Calculation
#####################################################################################
#### Definition
# " " -> no QC performed
# "A" -> N=100% All profile levels contain good data
# "B" -> 75% <= N < 100%
# "C" -> 50% <= N < 75%
# "D" -> 25% <= N < 50%
# "E" -> 0% <= N < 25%
# "F" -> N=0%; no profile levels have good data
# Initialisation
N_good=0
# Split the string to count
QC=unlist(strsplit(PARAM_ADJUSTED_QC[i_prof_param],split=""))
N_QC_1=length(which(QC == "1"))
N_QC_2=length(which(QC == "2"))
N_QC_5=length(which(QC == "5"))
N_QC_8=length(which(QC == "8"))
if ( N_QC != 0) {
N_good=100 * ( N_QC_1 + N_QC_2 + N_QC_5 + N_QC_8 ) / N_QC
} else {
N_QC_tot=nchar(PARAM_ADJUSTED_QC[i_prof_param])
N_QC_9=length(which(QC == "9"))
N_QC_B=length(which(QC == " "))
if ( N_QC_tot == N_QC_9 ) N_good = -99
if ( N_QC_tot == N_QC_9+N_QC_B ) N_good = -99
}
if ( N_good == -99) substr(PROFILE_PARAM_QC,i_prof_param,i_prof_param) <-" "
if ( N_good == 0) substr(PROFILE_PARAM_QC,i_prof_param,i_prof_param) <-"F"
if ( N_good > 0 && N_good < 25 ) substr(PROFILE_PARAM_QC,i_prof_param,i_prof_param) <-"E"
if ( N_good >= 25 && N_good < 50 ) substr(PROFILE_PARAM_QC,i_prof_param,i_prof_param) <-"D"
if ( N_good >= 50 && N_good < 75 ) substr(PROFILE_PARAM_QC,i_prof_param,i_prof_param) <-"C"
if ( N_good >= 75 && N_good < 100 ) substr(PROFILE_PARAM_QC,i_prof_param,i_prof_param) <-"B"
if ( N_good == 100 ) substr(PROFILE_PARAM_QC,i_prof_param,i_prof_param) <-"A"
#####################################################################################
# DATA_MODE / PARAMETER_DATA_MODE / DATA_STATE_INDICATOR
#####################################################################################
# What I don't like is the fact that DATA_STATE_INDICATOR and DATA_MODE
# are linked to i_prof_param while we are not doing D for CDOM, BBP700 and CHLA
# at the same time CATSCHM
#####################################################################################
# We perform DM QC on the profile i_prof so DATA_MODE should be set to D
# For the good i_prof
ncvar_put(filenc,"DATA_MODE","D",start=c(i_prof_param),count=c(1))
substr(PARAMETER_DATA_MODE[i_prof_param],i_param_param,i_param_param)<-"D"
# We perform DM QC on the profile i_prof but it still can be improved
# so we should put DATA_STATE_INDICATOR as 2C for i_prof
ncvar_put(filenc,"DATA_STATE_INDICATOR","2C ",start=c(1,i_prof_param),count=c(4,1))
} # End loop on irow
### Enter the Adjusted _QC in the file
ncvar_put(filenc,PARAM_ADJUSTED_QC_name,PARAM_ADJUSTED_QC)
### Enter the Adjustment in the File (We move this here for isolated FLAG "4" set to NA)
ncvar_put(filenc,PARAM_ADJUSTED_name,PARAM_ADJUSTED)
ncvar_put(filenc,PARAM_ADJUSTED_ERROR_name,PARAM_ADJUSTED_ERROR)
### Enter the PROFILE_QC in the file
ncvar_put(filenc,PROFILE_PARAM_QC_name,PROFILE_PARAM_QC)
### ENTER the PARAMETER_DATA_MODE
ncvar_put(filenc,"PARAMETER_DATA_MODE",PARAMETER_DATA_MODE)
#####################################################################################
# DATE UPDATE
#####################################################################################
### DATE_UPDATE
ncvar_put(filenc,"DATE_UPDATE",date_update[i])
######### Close the Files
nc_close(filenc)
nc_close(filenc_meta)
nc_close(filenc_core)
} # end loop on file