-
Notifications
You must be signed in to change notification settings - Fork 0
/
QC_NITRATE.R
190 lines (98 loc) · 3.29 KB
/
QC_NITRATE.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
QC_NITRATE <-function(filenc_B,index_nitrate,NITRATE_TCSS,RMS_NITRATE_TCSS,ASW240_NITRATE_TCSS,SATURATION_NITRATE) {
### CONSTANTS
MIN_RANGE=-2
MAX_RANGE=50
SPIKE_VALUE=1
FIT_RANGE=0.003
ABS240_MED=0.8
ABS240_MAX=1.1
# Profile index of the NITRATE
i_prof_nitrate=index_nitrate[,2]
# param index of the NITRATE
i_param_nitrate=index_nitrate[,1]
# Read the variables in the file
PRES=ncvar_get(filenc_B,"PRES")
NITRATE=ncvar_get(filenc_B,"NITRATE")
NITRATE_ADJUSTED=ncvar_get(filenc_B,"NITRATE_ADJUSTED")
NITRATE_QC=ncvar_get(filenc_B,"NITRATE_QC")
NITRATE_ADJUSTED_QC=ncvar_get(filenc_B,"NITRATE_ADJUSTED_QC")
### PRESSURE FOR NITRATE
PRES_NITRATE=PRES[,i_prof_nitrate]
NITRATE=NITRATE[,i_prof_nitrate]
NITRATE_ADJUSTED=NITRATE_ADJUSTED[,i_prof_nitrate]
###########################################################
# QC tests ################################################
###########################################################
#### DEFINE QC
NITRATE_ADJUSTED_QC_value = rep(" ", length(PRES_NITRATE) )
NITRATE_QC_value = rep(" ", length(PRES_NITRATE) )
#### Number of measurements
Ndepth=length(PRES_NITRATE[!is.na(PRES_NITRATE)])
#### Filter the data with a median
if (length(which(!is.na(NITRATE))) >= 5 ) {
MED_NITRATE=RunningFilter(2,NITRATE,na.fill=T, ends.fill=T, Method="Median")
MED_NITRATE_ADJUSTED=RunningFilter(2,NITRATE_ADJUSTED,na.fill=T, ends.fill=T, Method="Median")
} else {
MED_NITRATE=NITRATE
MED_NITRATE_ADJUSTED=NITRATE_ADJUSTED
}
for(p in 1:Ndepth){
if (is.na(NITRATE[p])) {
NITRATE_QC_value[p]=4
} else {
#### Initialise
NITRATE_QC_value[p]=3
### Saturation
if(SATURATION_NITRATE[p]) {
NITRATE_QC_value[p]=3
}
### Absorbance
if ( (ASW240_NITRATE_TCSS[p] < ABS240_MAX ) & ( ASW240_NITRATE_TCSS[p] >= ABS240_MED ) ) {
NITRATE_QC_value[p]=3
}
if ( ASW240_NITRATE_TCSS[p] >= ABS240_MAX ) {
NITRATE_QC_value[p]=4
}
### Fit TEST
if ( RMS_NITRATE_TCSS[p] >= FIT_RANGE ) {
NITRATE_QC_value[p]=4
}
### Range Test
if ( (NITRATE[p] < MIN_RANGE) | (NITRATE[p] > MAX_RANGE) ) NITRATE_QC_value[p]=4
### Spike test
if ( abs(MED_NITRATE[p]-NITRATE[p]) >= SPIKE_VALUE ) NITRATE_QC_value[p]=4
}
if (is.na(NITRATE_ADJUSTED[p])) {
NITRATE_ADJUSTED_QC_value[p] =4
} else {
#### Initialise
NITRATE_ADJUSTED_QC_value[p] =2
### Saturation
if(SATURATION_NITRATE[p]) {
NITRATE_ADJUSTED_QC_value[p]=3
}
### Absorbance
if ( (ASW240_NITRATE_TCSS[p] < ABS240_MAX ) & ( ASW240_NITRATE_TCSS[p] >= ABS240_MED ) ) {
NITRATE_ADJUSTED_QC_value[p]=3
}
if ( ASW240_NITRATE_TCSS[p] >= ABS240_MAX ) {
NITRATE_ADJUSTED_QC_value[p]=4
}
### Fit TEST
if ( RMS_NITRATE_TCSS[p] >= FIT_RANGE ) {
NITRATE_ADJUSTED_QC_value[p]=4
}
### Range Test
if ( (NITRATE_ADJUSTED[p] < MIN_RANGE) | (NITRATE_ADJUSTED[p] > MAX_RANGE) ) NITRATE_ADJUSTED_QC_value[p]=4
### Spike test
if ( abs(MED_NITRATE_ADJUSTED[p]-NITRATE_ADJUSTED[p]) >= SPIKE_VALUE ) NITRATE_ADJUSTED_QC_value[p]=4
}
}
### Write the QC in the file
NITRATE_QC[i_prof_nitrate]=paste(NITRATE_QC_value,collapse="")
NITRATE_ADJUSTED_QC[i_prof_nitrate]=paste(NITRATE_ADJUSTED_QC_value,collapse="")
ncvar_put(filenc_B,"NITRATE_QC",NITRATE_QC)
ncvar_put(filenc_B,"NITRATE_ADJUSTED_QC",NITRATE_ADJUSTED_QC)
QC="Done and Write"
return(QC)
}