-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_floats.m
1776 lines (1623 loc) · 102 KB
/
prepare_floats.m
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
% PREPARE_FLOATS loads float nc-files from your download folder, checks
% and prepares float data for ow_calibration, as well as adding flags
% and other DMQC parameters.
%
% DMQC-fun v0.9.
% J. Even Ø. Nilsen, Ingrid M. Angel-Benavides, Birgit Klein, Malgorzata Merchel, and Kjell Arne Mork.
% Last updated: Thu Oct 26 15:14:12 2023 by jan.even.oeie.nilsen@hi.no
%
% You will likely run this at least twice when new profiles come in.
% You set which floats to operate on etc. in INIT_DMQC. You also select
% direction in INIT_DMQC, in order to inspect both ascending and
% descending profiles.
%
% This script basicly follows the Argo QC manual, and does the following:
% - Ingests individual R-files
% - Loads QC flags from previous DMQC sessions (see below about flagging)
% - Fills scientific calib information
% - Delayed-mode procedures for coordinates and pressure
% - Pressure adjustments for APEX floats
% - Delayed-mode procedures for temperature and salinity:
% Automated tests prior to visual control
% Facilitates visual control of all profiles and flags
% - Makes several graphics necessary for DMQC
% - Applies flags to the data (in matlab)
% - Corrects deep argo/arvor floats pressure dependent conductivity bias
% - Saves results for both OWC and WRITE_D
% - Creates several parts of the report:
% snippets of text, e.g., for the technical table
% altimetry section
% reference data appendix
% visual test example figures
% Overall comparison with the reference data figures
% Sections about the APEX and deep corrections
% The flagging summary table
%
% No editing in this file!
%
% ----- On flagging throughout PREPARE_FLOATS and WRITE_D: -------------
%
% The flag matrices at work here are:
%
% *qco = old flags collected from the R-files, and for previously
% DMQC'ed profiles filled with flags from previously read
% R-files, thus *qco is always RTQC flags.
%
% *qcn = new flags collected from previously saved DMQC or just
% initiated as blank for new profiles. DMQC flags of new (and
% possibly old) profiles are added to this if changed in a
% session. New flags can also be changed in a later session.
%
% *qct = temporary flags based on *qco flags, to which flags from
% automatic and visual DMQC is added, before being assigned
% to *qcn if they are different from flags in *qco and
% *qcn. Flags in *qct are only used by the automatic and
% visual controls within this script.
%
% Remember that you can choose to rerun DMQC on any profiles by setting
% the 'checked' parameter in INIT_DMQC back to whichever profile you
% want to start DMQC from, and stop during visual control whenever you
% like. Note that you will then start from scratch on those profiles and
% see only RTQC flags and the automatic checks. Flags from previous DMQC
% will not show nor carry through to *qcn for those particular profiles.
%
% The flags in *qco and *qcn will be kept apart at all stages in order
% to keep the statistics of RTQC and DMQC for the whole life of the
% float. They are saved in a file in the float_working_dir (a directory
% your system should keep backups of) so that one can separate these
% through several instances of DMQC. This is necessary for good
% statistics in the report, as D-files do not discern between RTQC and
% DMQC flags (Note that only DMQC flags of type '4' and '1' enter
% D-files).
%
% However, for floats DMQC'ed before using this version of DMQC-fun the
% DMQC flags will have been written to the D-files back then, and thus
% RTQC and DMQC flags of previously inspected profiles will look the
% same. Thus, for plot marks and statistics in PREPARE_FLOATS older DMQC
% flags will be marked and counted as RTQC flags.
%
% ----- On columns, profiles, and cycles -------------------------------
%
% Each column of the matrices used herein corresponds to a file in the
% Rfiles list, which is not (necessarily) the same as cycle numbers. But
% since the Rfiles list is ported to WRITE_D and used there, the output
% of columns from the matrices will be put in the correct D-files. But
% do take care to use the variable CYCLE_NUMBER throughout when needed
% in plots and text!
%
% ----------------------------------------------------------------------
clear all; close all
init_dmqc; % Paths and filenames.
grey=[.5 .5 .5]; % Colour setting for maps.
% Follow selection of reference data set in INIT_DMQC:
tardir=tardir(itardir);
tartyp=tartyp(itardir);
for I=1:length(download_dir) % Loop floats
close all
clear hP hPo hPn LO LA scientific_calib
% ----- INITIAL INFO EXTRACTION: --------------------------------------
% Specifics for ascending and descending profiles:
switch direction
case 'A',
disp(['prepare_floats: Processing ascending profiles from float ',float_names{I},'.']);
float_working_dir=[my_working_dir,'DMQC',filesep,float_names{I}];
checked_name='checked';
case 'D'
disp(['prepare_floats: Processing DESCENDING profiles from float ',float_names{I},'.']);
float_working_dir=[my_working_dir,'DMQC',filesep,float_names{I},filesep,'descending'];
if ~exist(float_working_dir,'dir'), mkdir(float_working_dir); end
outfiles{I}=[outfiles{I}(1:end-4),'D',outfiles{I}(end-3:end)];
checked{I}=Dchecked{I};
checked_name='Dchecked';
% For descending profiles all snippets, tex files (fprintf) and the
% cluster-data mat-files will go in the descending subdirectory. The
% figures (print) and final saved mat-file with results will all be
% marked D like the input.
end
% Go to that float's correct working dir:
cd(float_working_dir);
% Extract the MAP_P_EXCLUDE from the float-specific setup file:
aa=strip(readlines([my_working_dir,'DMQC',filesep,float_names{I},filesep,'ow_config.txt'])); % Read local ow_config.txt
find(contains(aa,'MAP_P_EXCLUDE=') & ~startsWith(aa,'%')); % Scan string
eval([char(aa(ans(end))),';']); % Get the last definition of MAP_P_EXCLUDE
% Generate snippets of text for the report (from the high level files):
snippet(refdir);
ncread(metafiles{I},'DATA_CENTRE');
switch ans'
case 'IF', 'Coriolis';
end
snippet(ans,'DAC','_deblank');
ncread(metafiles{I},'FLOAT_SERIAL_NO'); snippet(ans','float-serial-no','_deblank');
platyp=ncread(metafiles{I},'PLATFORM_TYPE'); platyp=snippet(platyp','platform-type','_deblank');
ncread(metafiles{I},'TRANS_SYSTEM'); snippet(ans(:,1)','transmission-system','_deblank');
sensor=ncread(metafiles{I},'SENSOR')';
sensormodel=ncread(metafiles{I},'SENSOR_MODEL')';
sensorsnr=ncread(metafiles{I},'SENSOR_SERIAL_NO')';
contains(string(sensor),'CTD_TEMP')|contains(string(sensor),'CTD_CNDC'); ctdi=find(ans); othi=find(~ans);
ctdmodel=snippet(sensormodel(ctdi(1),:),'ctdmodel','_deblank');
snippet(sensorsnr(ctdi(1),:),'ctdsensorsnr','_deblank');
snippet(sensor(othi,:),'othersensors','_deblank');
snippet(sensormodel(othi,:),'othersensormodels','_deblank');
snippet(sensorsnr(othi,:),'othersensorsnr','_deblank');
ncread(metafiles{I},'LAUNCH_DATE'); snippet([ans(7:8)','/',ans(5:6)','/',ans(1:4)'],'depdate');
ncread(metafiles{I},'LAUNCH_LATITUDE'); snippet(ans,'deplat');
ncread(metafiles{I},'LAUNCH_LONGITUDE'); snippet(ans,'deplon');
ncread(trajfiles{I},'REPRESENTATIVE_PARK_PRESSURE'); snippet([int2str(round(nanmedian(ans)/100)*100),' m'],'park-depth');
%ncread(trajfiles{I},'REPRESENTATIVE_PARK_PRESSURE'); snippet([int2str(round(max(ans(end))/100)*100),' m'],'park-depth');
%ncread(proffiles{I},'PRES'); snippet([int2str(round(max(ans(:,end))/100)*100),' m'],'profile-depth');
ncread(proffiles{I},'PRES');
profdepth=round(nanmax(ans,[],'all')/100)*100; snippet([int2str(profdepth),' m'],'profile-depth');
ncread(trajfiles{I},'JULD_ASCENT_END'); t=ans; snippet([int2str(round(nanmedian(diff(t,1,1)))),' days'],'cycle-time');
%ncread(proffiles{I},'JULD'); t=ans; snippet([int2str(round(nanmean(diff(t,1,1)))),' days'],'cycle-time');
ncread(metafiles{I},'DEPLOYMENT_PLATFORM'); snippet(ans(:,1)','ship','_deblank');
ncread(metafiles{I},'PI_NAME'); snippet(ans(:,1)','PI','deblank');
ncread(metafiles{I},'END_MISSION_STATUS');
switch ans'
case ' ', 'Active';
case 'T', 'No more transmission received';
case 'R', 'Retrieved';
end
snippet(ans,'float-status','_deblank');
snippet([num2str(diff(mima(t))/365,'%4.1f'),' yrs'],'age');
inCYCLE_NUMBER=ncread(infiles{I},'CYCLE_NUMBER')'; snippet(max(inCYCLE_NUMBER),'last-cycle');
snippet(zipnumstr(setdiff(1:max(inCYCLE_NUMBER),inCYCLE_NUMBER)),'missing-cycles');
% Grey list:
[g1,g2,g3,g4,g5,g6,g7]=textread([download_parent_dir,'coriolis_greylist.csv'],'%s%s%s%s%s%s%s','delimiter',',');
ii=find(contains(g1,float_names{I}));
glist=''; for i=1:length(ii), glist=[glist,g2{ii(i)},',',g5{ii(i)},',',g6{ii(i)},'; ']; end
snippet(glist,'grey-list','_');
% The set minimum depth in OWC:
snippet(MAP_P_EXCLUDE);
% Transfer the operator info to the report:
snippet(dmqc_operator(1).name,'dmqc_operator_name');
snippet(dmqc_operator(1).orcid,'dmqc_operator_orcid');
snippet(dmqc_operator(1).institution,'dmqc_operator_institution');
snippet(dmqc_operator(1).address,'dmqc_operator_address');
% Make some necessary definitions for LaTeX report:
fid=fopen('wmonumdef.tex','w'); fprintf(fid,'%s%s%s','\newcommand{\WMOnum}{',float_names{I},'}'); fclose(fid);
fid=fopen('floatsourcedef.tex','w'); fprintf(fid,'%s%s%s','\newcommand{\floatsource}{',float_dir,'}'); fclose(fid);
fid=fopen('floatplotsdef.tex','w'); fprintf(fid,'%s%s%s','\newcommand{\floatplots}{',plot_dir,'}'); fclose(fid);
fid=fopen('downloaddirdef.tex','w'); fprintf(fid,'%s%s%s','\newcommand{\downloaddir}{',download_dir{I},'}'); fclose(fid);
% ------ INGEST FLOAT FILES: -----------------------------------------
% Init of some objects:
comments=''; % String for report on what is done here (obsolete)
nowchar=replace(datestr(now,30),'T',''); % Datetime-stamp
PARS=ncread(infiles{I},'PARAMETER'); % Parameters in the _prof file
npars=size(PARS,2); % Number of parameters in the _prof file
% Find the complete height of matrix (not ideal method, but the
% R-files have different length profiles and we need a matrix here):
PRES=ncread(infiles{I},'PRES');
m=size(PRES,1)+1; % Add one to make sure every profile ends with a NaN.
% Find the profile-files to check here:
Rfiles=edir(rootdirin{I}); % Will contain both D and R files, just
%% EDIR only works on UNIX/Linux since it applies find and grep,
% using dir here:
%Rfiles=dir(rootdirin{I}); % Will contain both D and R files, just
% like on the Coriolis server.
% [] And A files?
%Rfiles=strcat(rootdirin{I},{Rfiles.name}'); % Add the path
%Rfiles=Rfiles(~contains(Rfiles,{'\.','/.'})); % Remove the relative directory references
% Now edir works also without unix/Linux.
% Select file names for chosen direction:
switch direction
case 'A', Rfiles=Rfiles(~contains(Rfiles,'D.nc'));
case 'D', Rfiles=Rfiles( contains(Rfiles,'D.nc'));
end
n=length(Rfiles);
% Sort the file names by cycle number in file name:
if n>1, split(Rfiles,'_');
[~,AI]=sort(ans(:,2)); Rfiles=Rfiles(AI); end
if direction=='D'
if n<1
['There are no direction ''',direction,''' files for float ',float_names{I},'. '];
disp(['prepare_floats:', ans]); snippet(ans,'descending_profiles');
continue
else
['Direction ''',direction,''' profiles have been checked for float ',...
float_names{I},', but results are not shown in this report.'];
snippet(ans,'descending_profiles');
end
end
[PRES,PSAL,TEMP]=deal(nan(m,n)); % Originals, filled but not to be changed at all!
[PRESqco,PSALqco,TEMPqco]=deal(repmat(' ',m,n));
[CYCLE_NUMBER,LONG,LAT,JULD]=deal(nan(1,n));
[POSqco,JULDqco,DIRECTION]=deal(repmat(' ',1,n));
%[PROFILE_PSAL_QC]=deal(repmat(' ',1,n));
[PARAMETER]=deal(repmat(' ',16,npars,1,n));
for i=1:n % loop Rfiles
ncread(Rfiles{i},'LONGITUDE')'; LONG(1,i)=ans(1,1);
ncread(Rfiles{i},'LATITUDE')'; LAT(1,i) =ans(1,1);
ncread(Rfiles{i},'JULD')'; JULD(1,i)=ans(1,1);
ncread(Rfiles{i},'PRES'); PRES(1:size(ans,1),i)=ans(:,1);
ncread(Rfiles{i},'PSAL'); PSAL(1:size(ans,1),i)=ans(:,1);
ncread(Rfiles{i},'TEMP'); TEMP(1:size(ans,1),i)=ans(:,1);
ncread(Rfiles{i},'PRES_QC'); PRESqco(1:size(ans,1),i)=ans(:,1);
ncread(Rfiles{i},'TEMP_QC'); TEMPqco(1:size(ans,1),i)=ans(:,1);
ncread(Rfiles{i},'PSAL_QC'); PSALqco(1:size(ans,1),i)=ans(:,1);
% ncread(Rfiles{i},'PROFILE_PRES_QC'); PROFILE_PRES_QC(1,i)=ans(1,1);
% ncread(Rfiles{i},'PROFILE_TEMP_QC'); PROFILE_TEMP_QC(1,i)=ans(1,1);
% ncread(Rfiles{i},'PROFILE_PSAL_QC'); PROFILE_PSAL_QC(1,i)=ans(1,1);
ncread(Rfiles{i},'CYCLE_NUMBER')'; CYCLE_NUMBER(1,i)=ans(1,1);
ncread(Rfiles{i},'DIRECTION')'; DIRECTION(1,i)=ans(1,1);
ncread(Rfiles{i},'POSITION_QC')'; POSqco(1,i)=ans(1,1);
ncread(Rfiles{i},'JULD_QC')'; JULDqco(1,i)=ans(1,1);
ncread(Rfiles{i},'PARAMETER');
N=size(ans,2); % Number of parameters
IC=ans(:,:,1,1); % Parameter names (of first layer)
para={'PRES','TEMP','PSAL'}; % Valid parameters for DMQC in the right order
ic={'NB_SAMPLE_CTD','MTIME','TEMP_CNDC','PRES_MED','TEMP_MED','PSAL_MED','TEMP_STD','PSAL_STD'}; % Intermediate parameters
for j=1:N
pname=strip(IC(:,j)'); % The j-th parameter name
if any(strcmp(pname,para)) % Is it a valid parameter for DMQC?
find(strcmp(pname,para)); % Which one is it?
PARAMETER(:,ans,1,i)=IC(:,j); % Place it in correct column
elseif any(strcmp(pname,ic)) % Or is it just an intermediate parameter?
% Then deal the default calib information:
[scientific_calib.equation.(pname)(i,:),...
scientific_calib.coefficient.(pname)(i,:),...
scientific_calib.comment.(pname)(i,:)] = deal('not applicable');
scientific_calib.date.(pname)(i,:) = nowchar;
% Nothing more is supposed to be done to these intermediate parameters.
else % Unknown parameter!
error([Rfiles{i},' has hitherto unknown parameter ',pname','!',...
' This parameter will not get SCENTIFIC_CALIB information.',...
' Check documentation and recode float ingestion in PREPARE_FLOATS.']);
end
% PARS is the array from the _prof file. PARAMETER is filled from
% the R/D-files, and its size is based on PARS. None of these are
% used after this, not here and not in WRITE_D.
end % Parameter name loop
end % R-file loop
% Original number of measurements in each profile (for PROFILE_<PARAMETER>_QC in write_D):
PROFILE_PRES_N=sum(~isnan(PRES)); PROFILE_TEMP_N=sum(~isnan(TEMP)); PROFILE_PSAL_N=sum(~isnan(PSAL));
% Just a test on how percentage is calculated:
% PROFILE_PSAL_QC=repmat(char(32),1,size(SALqco,2));
% sum(SALqco=='1' | SALqco=='2' | SALqco=='5' | SALqco=='8')*100./PROFILE_PSAL_N*100;
% PROFILE_PSAL_QC(ans==0)='F', PROFILE_PSAL_QC(ans>0)='E', PROFILE_PSAL_QC(ans>=25)='D'
% PROFILE_PSAL_QC(ans>=50)='C', PROFILE_PSAL_QC(ans>=75)='B', PROFILE_PSAL_QC(ans==100)='A'
% Get start date for datenum
REFERENCE_DATE_TIME=ncread(Rfiles{1},'REFERENCE_DATE_TIME');
% Only reading first column from file! Other profiles are not part of DMQC.
% This part is obsolete, since descending profile(s) are removed by
% filename above, but check in case '*D.nc' is not sufficient:
if any(DIRECTION~=direction)
error(['There are profiles not in the ',direction,'-direction!']);
end
% whos CYCLE_NUMBER LONG LAT JULD PRES SAL TEMP DIRECTION REFERENCE* HISTORY*
% ----- INPUT PREVIOUS QC FLAGS: -------------------------------------
% Replace qco and qcn for previously DMQC'ed profiles with saved matrices: ----
% New full length blank flag matrices to add previous DMQC to:
[POSqcnb,JULDqcnb] = deal(repmat(' ',1,n));
[PRESqcnb,PSALqcnb,TEMPqcnb] = deal(repmat(' ',m,n));
% Use qco as read from the nc-files as matrices to add saved RTQC to:
POSqcob=POSqco; JULDqcob=JULDqco; PRESqcob=PRESqco; PSALqcob=PSALqco; TEMPqcob=TEMPqco;
newflagfile=['savedflags_',float_names{I},'.mat'];
if exist(newflagfile,'file') % There has been previous DMQC.
load(newflagfile,'*qcn','*qco'); % Load from previous DMQC.
disp(['prepare_floats: Loading previous RTQC and DMQC flags from ',pwd,filesep,newflagfile]);
[M,N]=size(PRESqcn); % Size of previous flag matrices
% Test that the saved sets of flags concur with the nc-files from
% previous DMQC (D-files), i.e, *qco with addition of '1' and '4'
% from *qcn should be the same as *qcob from the ncfiles:
for i=1:length(para) % Loop parameters
eval(['qco=',para{i},'qco;']); % Temporary variable for <PARAM>qco
eval(['qco(',para{i},'qcn==''1'')=''1'';']); % Add reversing DMQC flags to RTQC flags
eval(['qco(',para{i},'qcn==''4'')=''4'';']); % Add new DMQC flags to RTQC flags
eval(['any(qco~=',para{i},'qcob(1:M,1:N));']); % Compare
if any(ans) & any(contains(Rfiles(find(ans)),'//D')) % Warn about D-files only
warning(['Mismatch between previously saved DMQC flags and flags in D-files for ',para{i},'!']);
end
clear qco
end
% Insert previous DMQC flags into the blank matrices:
POSqcnb(1,1:N)=POSqcn; JULDqcnb(1,1:N)=JULDqcn;
PRESqcnb(1:M,1:N)=PRESqcn; PSALqcnb(1:M,1:N)=PSALqcn; TEMPqcnb(1:M,1:N)=TEMPqcn;
% Insert previous RTQC flags into the qco from nc-files matrices:
POSqcob(1,1:N)=POSqco; JULDqcob(1,1:N)=JULDqco;
PRESqcob(1:M,1:N)=PRESqco; PSALqcob(1:M,1:N)=PSALqco; TEMPqcob(1:M,1:N)=TEMPqco;
end
% Make the now filled matrices the current qcn and qco matrices:
POSqcn=POSqcnb; JULDqcn=JULDqcnb; PRESqcn=PRESqcnb; PSALqcn=PSALqcnb; TEMPqcn=TEMPqcnb; clear *qcnb
POSqco=POSqcob; JULDqco=JULDqcob; PRESqco=PRESqcob; PSALqco=PSALqcob; TEMPqco=TEMPqcob; clear *qcob
% The first time around, qcn (DMQC flags) are all empty and qco (RTQC
% flags) is as loaded from float-files. For subsequent times qcn is
% what was done to previous profiles and blank for new profiles, and
% qco is what has been saved for previous profiles and what has been
% read from new (R) files. The qco matrices never change in the
% scripts, so it will always contain RTQC only.
%
% Finally make temporary RTQC-flag matrices for the automatic tests
% below to fill:
POSqct=POSqco; JULDqct=JULDqco;
PRESqct=PRESqco; PSALqct=PSALqco; TEMPqct=TEMPqco;
% whos *qco *qct *qcn
% ---- More info for the report (including the whole appendix) only for ascending profiles: ----------------
if direction=='A'
% The Altimetry section
fid=fopen('altimetry_sec.tex','w');
if exist([download_dir{I},float_names{I},'.png'],'file')
fprintf(fid,'%s\n','Figure~\ref{Altim} shows the comparison with altimetry.');
fprintf(fid,'%s\n','\begin{figure}[H]');
fprintf(fid,'%s\n',' \centering');
fprintf(fid,'%s\n',' \includegraphics[width=\textwidth,natwidth=810,natheight=650]{\downloaddir\WMOnum .png}');
fprintf(fid,'%s\n',' \caption{Float \WMOnum. The comparison between the sea level anomaly (SLA) from the satellite altimeter and dynamic height anomaly (DHA) extracted from the Argo float temperature and salinity. The figure is created by the CLS/Coriolis, distributed by Ifremer (ftp://ftp.ifremer.fr/ifremer/argo/etc/argo-ast9-item13-AltimeterComparison/figures/).}');
fprintf(fid,'%s\n',' \label{Altim}');
fprintf(fid,'%s\n','\end{figure}');
else
fprintf(fid,'%s\n','An altimetry report was not available at the time of reporting.');
end
% The reference data plots for the appendix:
~(isnan(LONG)|isnan(LAT));wmosquares=unique(findwmo(LONG(ans),LAT(ans))); snippet(wmosquares);
fid=fopen('appendix.tex','w');
for i=1:length(wmosquares)
fprintf(fid,'%s\n','\begin{figure}[ph]');
fprintf(fid,'%s\n','\centering');
reffig=false;
for j=1:length(tardir) % one fig per type of refdata
% [] Note that page is not formatted with room for all three types. When
% bottle data is added, will need to accomodate for this.
wmofig=[tardir{j},filesep,tartyp{j},'_',int2str(wmosquares(i)),'.eps'];
if exist(wmofig,'file') % if file exist make graphics input line
reffig=true;
fprintf(fid,'%s%s%s\n','\includegraphics[width=\textwidth]{',wmofig,'}');
end
end
if reffig
fprintf(fid,'%s%u%s\n','\caption{Overview of reference data in WMO square ',wmosquares(i),' which is traversed by Float~\WMOnum . Upper set of graphs are for CTD reference data, and lower set is for historical ARGO data (if only one set is displayed, see title of second panel). Colouring of positions in map pairs illustrate temporal coverage and latitude, respectively. The following TS and profile plots use the latter colormap.}');
else
fprintf(fid,'%s%u%s\n','\caption{There are no reference data in WMO square ',wmosquares(i),' traversed by Float~\WMOnum .}');
end
fprintf(fid,'%s%u%s\n','\label{reference',wmosquares(i),'}');
fprintf(fid,'%s\n','\end{figure}');
end % loop wmosquares
fclose(fid);
end % if direction 'A'
% Create empty files for the report if they do not exist, as some procedures may not be performed:
if ~exist('shipctd.tex','file'), fid=fopen('shipctd.tex','w'); fclose(fid); end
if ~exist('coincidence.tex','file'), fid=fopen('coincidence.tex','w'); fclose(fid); end
if ~exist('coincidence_appendix.tex','file'), fid=fopen('coincidence_appendix.tex','w'); fclose(fid); end
if ~exist('owc.tex','file'), fid=fopen('owc.tex','w'); fclose(fid); end
if ~exist('scientific_calib_tabular.tex','file'), fid=fopen('scientific_calib_tabular.tex','w'); fclose(fid); end
% ----- Initialise the new calibration information objects: ----------
% (For all profiles, but for now just with no size)
% When data are good and no adjustment is needed and assuming that scientific_calib should be 'none' throughout:
[scientific_calib.equation.PRES, scientific_calib.coefficient.PRES, scientific_calib.comment.PRES, ...
scientific_calib.equation.PSAL, scientific_calib.coefficient.PSAL, scientific_calib.comment.PSAL, ...
scientific_calib.equation.TEMP, scientific_calib.coefficient.TEMP, scientific_calib.comment.TEMP] = deal(repmat('none',n,1)); % First new entry
% "Regardless of whether an adjustment has been applied or not, the date of delayed-mode qc for each measurement parameter should be
% recorded in SCIENTIFIC_CALIB_DATE, in the format YYYYMMDDHHMISS."
[scientific_calib.date.PRES , ...
scientific_calib.date.PSAL , ...
scientific_calib.date.TEMP ] = deal(repmat(nowchar,n,1));
% Known default comments when no adjustments is done:
scientific_calib.comment.TEMP = repmat('The quoted error is manufacturer specified accuracy with respect to ITS-90 at time of laboratory calibration.',n,1); % Default entry
% Default equations (PSAL is treated in WRITE_D):
scientific_calib.equation.PRES = repmat('PRES_ADJUSTED = PRES. ',n,1); % Default entry
scientific_calib.equation.TEMP = repmat('TEMP_ADJUSTED = TEMP. ',n,1); % Default entry
% All superfluous 'none' will be removed in WRITE_D.
% --------- DMQC: ----------------------------------------------------------------
%%%%%%% PHASE 1: Delayed-mode procedures for coordinates _and_ pressure adjustments %%%%%%%%
%%%%%%% PHASE 2: Sea Surface Pressure adjustments %%%%%%%%
%
% W21 3.2. Delayed-mode procedures for JULD, LATITUDE, LONGITUDE
%
% Delayed-mode operators should check that JULD in the profiles
% are in chronological order. Erroneous or missing JULD values should
% be replaced with another telemetered value if available, or replaced
% with interpolated values and marked with JULD_QC = ‘8’.
fid=fopen('JULDtest.tex','w'); [automsg,manualmsg]=deal('');
union( find(diff(JULD)<0)+1, find(isnan(JULD)) );
if any(ans)
JULD(ans)=NaN; JULDqcn(ans)='8'; JULD=efill(JULD); % Interpolate to fill NaNs
automsg=['Non-chronological or missing JULD in cycles ',zipnumstr(CYCLE_NUMBER(find(ans))),'.'];
disp(['prepare_floats: ',automsg]);
%error('Some JULD not chronological! Check!');
end
fprintf(fid,'%s %s\n',automsg,manualmsg);
fclose(fid);
% Calculate DATES as OWC wants them:
REFERENCE_DATE_TIME'; time=datenum([ans(1:8),'T',ans(9:14)],'yyyymmddTHHMMSS')+JULD;
DATES=dyear(time);
[~,MONTHS]=datevec(time); % For the seasonal aspect
% JULDqcn is set now.
% Sort cycle number (not sure this should be done, so just a check and error for now):
% Double cycle numbers messes up the rest. Pick out upcasts.
% Also Rfiles may be ordered wrong.
[ans,IA]=sort(CYCLE_NUMBER); % C = A(IA);
if ~all(ans==CYCLE_NUMBER)
error('Cycle numbers not in temporal succession!');
LONG=LONG(IA); LAT=LAT(IA); DATES=DATES(IA); CYCLE_NUMBER=CYCLE_NUMBER(IA); PRES=PRES(:,IA); PSAL=PSAL(:,IA); TEMP=TEMP(:,IA);
CYCLE_NUMBER=ans;
end
snippet([int2str(CYCLE_NUMBER(1)),'-',int2str(CYCLE_NUMBER(end))],'cycle-numbers');
% Size of data geometry is final after sorting:
[m,n]=size(PRES);
PROFILE_NO=1:n;
% Default adjusted values (after sorting):
PRES_ADJUSTED=PRES; TEMP_ADJUSTED=TEMP; PSAL_ADJUSTED=PSAL;
% [] How to know about wrong dates not being just shifted? Relation
% to cycle number?
% Profile positions in LONGITUDE, LATITUDE should be checked for
% outliers. Erroneous or missing LONGITUDE, LATITUDE values should
% be replaced with another telemetered value if available, or replaced
% with interpolated values and marked with POSITION_QC = ‘8’.
%
% Automated pre-check:
if any(PROFILE_NO>checked{I})
fid=fopen('POStest.tex','w'); [automsg,manualmsg]=deal('');
% Extrapolated data, i.e. 'interpolated' data at the end of series
% cannot be used (extrapolation makes no sense):
j=find(POSqco=='8' | POSqco=='9');
if any(j), if j(end)==n % Missing position at end
[~,gi]=groups(diff(j));
if j(gi(end))+1==n % Several, actually
j=j(gi(1,end):gi(2,end)+1);
POSqct(j)='4'; % Flag them
automsg=['There were extrapolated (!) or missing positions at the end of series. ' ...
'Hence, positions of cycles ',zipnumstr(CYCLE_NUMBER(j)),' as a rule flagged as bad.'];
disp(['prepare_floats: ',automsg]);
end
end, end
LAT<-90 | 90<LAT;
if any(ans)
POSqct(ans)=='4';
'There were latitudes outside 90S and 90N.'; disp(['prepare_floats: ',ans]); automsg=[automsg,' ',ans];
end
% Visual test of positions:
POSqct = check_profiles(CYCLE_NUMBER,LONG,LAT,POSqct);
% Add only different flags as new flags:
POSqct~=POSqco; POSqcn(ans)=POSqct(ans);
if any(ans)
manualmsg=['Automatic and visual DMQC have changed the position flags of cycles ',zipnumstr(CYCLE_NUMBER(find(ans))),'.'];
disp(['prepare_floats: ',manualmsg]);
end
fprintf(fid,'%s %s\n',automsg,manualmsg);
fclose(fid);
end
% Apply LATITUDE, LONGITUDE flags immediately in order for the visual tests and plots hereafter to function:
POSqco=='4' & POSqcn~='1' | POSqcn=='4'; LONG(ans)=NaN; LAT(ans)=NaN;
% whos LONG LAT POSqcn JULDqcn
% whos *_ADJUSTED
if direction=='D'
% Apply salinity flags from the RTQC in the surface, to the
% _ADJUSTED already here since the descending profiles contain
% above surface salinities. This is done mainly to avoid too many
% unneccessary detections in the automatic test below, and not to
% zoom out the automatic profile plots with far off values. This
% does not affect the visual control, as that uses non adjusted
% data.
PSALqco~='1' & PRES<10; % Find data to omit
igi=find(ans); % For testing after
igiS=PSAL_ADJUSTED(igi); % Save to put back in after the tests and checks
PSAL_ADJUSTED(ans)=NaN; % Omit data for the tests
PSALqct(ans & PSALqco~='4')='Y'; % Assign new flags (used in manual control part)
PSALqcn(ans & PSALqco~='4')='4'; % Assign new flags (used when no manual control)
% Special case: Just omit and flag all shallow (useless) data:
% if contains(platyp,{'PROVOR'})
% PRES<10;
% TEMPqct(ans)='Y'; % Assign new flags (used in manual control part)
% PSALqct(ans)='Y'; % Assign new flags (used in manual control part)
% end
%
% Skipped, because it looks not so good to do this
% indiscriminately. The user must just realise that descending
% floats are not trustworthy in the top.
end
% Plot the input data raw:
DENS = sw_pden(PSAL_ADJUSTED,TEMP_ADJUSTED,PRES_ADJUSTED,0); % Potential density
zerow=zeros(1,n); % Row of zeros
plot_profiles; % Plots based on ADJUSTED parameters
print(gcf,'-depsc',[outfiles{I}(1:end-4),'_raw.eps']); % Print to file
% whos DENS
% W21 3.3. Delayed-mode procedures for pressure
%
% Bad data points identified by visual inspection from delayed-mode
% analysts are recorded with PRES_ADJUSTED_QC = ‘4’ and PRES_QC =
% '4'. For these bad data points, TEMP_QC, TEMP_ADJUSTED_QC, PSAL_QC,
% PSAL_ADJUSTED_QC should also be set to ‘4’. Please note that
% whenever PARAM_ADJUSTED_QC = ‘4’, both PARAM_ADJUSTED and
% PARAM_ADJUSTED_ERROR should be set to FillValue.
%
% √ This is taken care of both here for OWC and in WRITE_D.
%
% PRESqcn TEMPqcn PSALqcn % Change QC flags if necessary, all in this case.
%
% Check for negative pressures:
PRES_ADJUSTED<0;
if any(ans,'all'),
warning('Negative pressures detected prior to correction!');
end
% Pressure adjustments for APEX, or not:
fid=fopen('pressure-adjustment.tex','w');
fprintf(fid,'%s\n','Sea surface pressure adjustments should be done for APEX floats (Wong et al., 2021). ');
if contains(platyp,'APEX')
% 3.3.1 Delayed-mode pressure adjustment for APEX and NAVIS floats
SP.CN=ncread(techfiles{I},'CYCLE_NUMBER')';
SP.TPN=string(ncread(techfiles{I},'TECHNICAL_PARAMETER_NAME')');
SP.TPV=ncread(techfiles{I},'TECHNICAL_PARAMETER_VALUE')';
iSP=find(contains(SP.TPN,'PRES_SurfaceOffset'));
if ~isempty(iSP)
fprintf(fid,'%s%s%s%s\n','This is an ',...
platyp,...
' float. Upper panel of Figure~\ref{fig:pres} shows the original and despiked PRES$\_$SurfaceOffsetNotTruncated$\_$dbar (SP)', ...
' in addition to the adjusted pressures from the top of each profile. Adjustment is done when valid SP values are available. ');
SP.CN=SP.CN(iSP); SP.TPN=SP.TPN(iSP); SP.TPV=str2num(SP.TPV(iSP,:));
% (1):
unique(SP.TPN);
if length(ans)>1, error('Technical parameter name for SP not unique!'); end
if contains(ans,'PRES_SurfaceOffsetTruncatedPlus5dbar_dbar')
SP.TPV=SP.TPV-5;
error('Truncated surface pressure! You need to code for TNPD.');
end
% (2):
diff(SP.TPV); SP.TPV(find((ans(1:end-1)>5 & ans(2:end)<-5) | (ans(1:end-1)<-5 & ans(2:end)>5))+1)=NaN;
SP.mn=mean(SP.TPV,'omitnan');
SP.mf=medfilt1(SP.TPV-SP.mn,5)+SP.mn;
SP.SP=SP.TPV;
SP.SP(abs(SP.SP-SP.mf)>1)=NaN;
% (3):
SP.SP=efill(SP.SP)';
% (4):
% Get Cond to re-calculate PSAL:
Cond=nan(m,n);
ii=find(~isnan(PRES)&~isnan(PSAL)&~isnan(TEMP)); % All parameters non-NaN because
Cond(ii)=gsw_C_from_SP(PSAL(ii),TEMP(ii),PRES(ii)); % gsw_ can't take any NaNs! :-(
% PRES_ADJUSTED (cycle i) = PRES (cycle i) – SP (cycle i+1).
[C,SP.IA,SP.IB] = intersect(CYCLE_NUMBER+1,SP.CN,'stable'); % IA and IB such that C = A(IA,:) and C = B(IB,:).
PRES_ADJUSTED(:,SP.IA) = PRES_ADJUSTED(:,SP.IA) - SP.SP(SP.IB)';
PSAL_ADJUSTED(ii) = gsw_SP_from_C(Cond(ii),TEMP_ADJUSTED(ii),PRES_ADJUSTED(ii)); % SP = gsw_SP_from_C(C,t,p) % Re-calculate SAL
% Calibration info:
'PRES_ADJUSTED = PRES - dP. ';
scientific_calib.equation.PRES(SP.IA,1:size(ans,2)) = repmat(ans,length(SP.IA),1); % First DMQC entry; overwrite
char(strcat({'dP = '},num2str(SP.SP(SP.IB)),' dbar. '));
scientific_calib.coefficient.PRES(SP.IA,1:size(ans,2)) = ans; % First DMQC entry; overwrite
'Pressures adjusted by using pressure offset at the sea surface. The quoted error is manufacturer specified accuracy in dbar. ';
scientific_calib.comment.PRES(SP.IA,1:size(ans,2)) = repmat(ans,length(SP.IA),1); % First DMQC entry; overwrite
scientific_calib.date.PRES(SP.IA,:) = repmat(nowchar,length(SP.IA),1); % Always overwrite date
% 3.3.2. Truncated negative pressure drift (TNPD) in APEX floats
% [] Not relevant for our NorARGO floats, so not implemented. Sorry! Feel free to add code and notify on github.
else
clear SP
fprintf(fid,'%s%s%s%s\n','This is an ',...
platyp,...
' float, but it has no valid PRES$\_$SurfaceOffsetNotTruncated$\_$dbar (SP) values. ',...
' Instead, upper panel of Figure~\ref{fig:pres} shows the surface pressure from the top of each profile. ');
end
else % Ikke APEX-float, men plot tid mot overflatetrykk, sensortrykk.
fprintf(fid,'%s%s%s\n','This is an ',...
platyp,...
' float. Instead, upper panel of Figure~\ref{fig:pres} shows the surface pressure from the top of each profile. ');
end
fprintf(fid,'%s\n','\begin{figure}[H]');
fprintf(fid,'%s\n','\centering');
fprintf(fid,'%s\n','\includegraphics[width=\textwidth,natwidth=1500,natheight=1250]{\floatsource\WMOnum_PRES.png}');
fprintf(fid,'%s\n',['\caption{Float \WMOnum\ pressure data. ',...
'Upper panel: Top of profile pressure series. ',...
'Lower panel: Pressure data for all profiles presented by every 10th (and the deepest) measurement in each profile. ',...
'Blue dots indicate pressure value in the real-time. ',...
'Any extra marks show RTQC and DMQC flags as explained in the legend. ',...
'Any ''4'', or ''8'' are shown regardless of depth, not just at every 10th.}']);
fprintf(fid,'%s\n','\label{fig:pres}');
fprintf(fid,'%s\n','\end{figure}');
fclose(fid);
% 'PRES_ADJUSTED = PRES - dP';
% Check for negative pressures after the correction:
PRES_ADJUSTED<0;
if any(ans,'all')
warning('Negative pressures detected after the correction! (removed)');
PRESqct(ans)='X'; % For the visual control below
end
% whos PRES_ADJUSTED PSAL_ADJUSTED PRESqct
% For the plots later, but also PTMPo here:
PTMP = sw_ptmp(PSAL,TEMP,PRES,0);
% whos PTMP
% Save ASD profiles salinities and ptmp from PSAL and PTMP (before test plots):
ASD=n+1; % Add 1 just since -1 is used later
% For stability, extract your own explicitly set calseries in set_calseries.m.
jj=find(cal_action{I}==4); % Which cal action (if any) is 4?
aa=strip(readlines([my_working_dir,'DMQC',filesep,float_names{I},filesep,'set_calseries.m'])); % Read local set_calseries.m
find(contains(aa,'calseries = [ones') & ~startsWith(aa,'%')); % Scan set_calseries.m
eval(aa(ans(end))); % Get the last definition of calseries
if any(jj) & direction=='A' & exist('calseries','var')
[C,IA] = unique(calseries,'stable'); % Find the shifts in calseries
ASD=IA(jj); % The one before the first profile of the jjth action (4).
SALo=PSAL(:,ASD:end); % Save to fill SAL with in the end
PTMPo=PTMP(:,ASD:end); % Save to fill PTMP with in the end
disp(['prepare_floats: Conserving salinity profiles with ASD (profiles ',int2str(ASD),'-',int2str(n),').']);
end
% whos ASD SALo PTMPo
% ----- PHASE 3 and PHASE 4 ------------------------------------------
% W21 3.4. Delayed-mode procedures for temperature
%
% Bad data points identified by visual inspection from delayed-mode
% analysts are recorded with TEMP_ADJUSTED_QC = ‘4’ and TEMP_QC =
% '4'. Please note that whenever PARAM_ADJUSTED_QC = ‘4’,
% PARAM_ADJUSTED = FillValue, and PARAM_ADJUSTED_ERROR = FillValue.
%
% TEMP_ADJUSTED, TEMP_ADJUSTED_ERROR, and TEMP_ADJUSTED_QC should be
% filled even when the data are good and no adjustment is needed. In
% these cases, TEMP_ADJUSTED_ERROR can be the manufacturer’s quoted
% accuracy at deployment, which is 0.002°C.
% √ Done in WRITE_D.
%
% Please use the SCIENTIFIC CALIBRATION section in the netCDF files to
% record details of the delayed-mode adjustment.
%
% TEMPqc % Change QC flags if necessary
% W21 3.5. Delayed-mode procedures for salinity
%
% It is recommended that float salinity be adjusted for pressure
% offset and cell thermal mass error before sensor drift
% adjustment.
% [√] Done above.
%
%W Operators should also ensure that other float measurements (PRES,
%W TEMP, LATITUDE, LONGITUDE, JULD) are accurate or adjusted before
%W they input them into the statistical tools for estimating reference
%W salinity.
%
% PSALqcn % Change QC flags if necessary
J=find(PROFILE_NO>checked{I});
%J=[]; visd(2)=0; % Use this for no DMQC checks
if any(J) % IF NEW PROFILES DO AUTOMATIC AND MANUAL CONTROL OF _NEW_PROFILES_
%%%%%%% PHASE 3: Automated tests prior to visual control %%%%%%%
% These tests:
% - Checks new/wanted profiles
% - Plots warning plots not used in report
% - Flags with different letters in temprary flag matrices
% NOTE: For Deep Arvor profiles, values deeper than 2000 m PRES and
% TEMP are flagged '2' and PSAL flagged '3' already in R-files. The
% same is the case for previously DMQC'd files (D-files).
% Initial check, since the code here adds flag 'X' to errors found in automatic tests:
if any(PRESqco=='Y' | PSALqco=='Y' | TEMPqco=='Y' | PRESqcn=='Y' | PSALqcn=='Y' | TEMPqcn=='Y' | ...
PRESqco=='X' | PSALqco=='X' | TEMPqco=='X' | PRESqcn=='X' | PSALqcn=='X' | TEMPqcn=='X' | ...
PRESqco=='S' | PSALqco=='S' | TEMPqco=='S' | PRESqcn=='S' | PSALqcn=='S' | TEMPqcn=='S' | ...
PRESqco=='D' | PSALqco=='D' | TEMPqco=='D' | PRESqcn=='D' | PSALqcn=='D' | TEMPqcn=='D' | ...
PRESqco=='G' | PSALqco=='G' | TEMPqco=='G' | PRESqcn=='G' | PSALqcn=='G' | TEMPqcn=='G' | ...
PRESqco=='I' | PSALqco=='I' | TEMPqco=='I' | PRESqcn=='I' | PSALqcn=='I' | TEMPqcn=='I')
error('There are already flag Y, X, S, D, G or I from RTQC or previous DMQC! Must recode PREPARE_FLOATS.');
end
DENS = sw_pden(PSAL_ADJUSTED,TEMP_ADJUSTED,PRES_ADJUSTED,0); % For the plotting of warning plots
zerow=zeros(1,n); % Row of zeros used in tests
[snb2,tnb2,snb1,tnb1,snbG,tnbG]=deal([]);
% Pressure increasing test / monotonically increasing pressure test:
logical([zerow;diff(PRES_ADJUSTED,1,1)<=0]); % The testvalue
ans(PRES_ADJUSTED<=MAP_P_EXCLUDE)=logical(0); % Do not be concerned with monotonicity above this depth.
if any(ans,'all')
jnb=find(any(ans)); pnb=ans; nbt='Non-monotonic pressure';
disp(['prepare_floats: ',nbt,'in float ',float_names{I},' cycle(s): ',int2str(CYCLE_NUMBER(jnb)),' !']); % Only warning
plot_profiles; print(gcf,'-depsc',[outfiles{I}(1:end-4),'_pressure_warning.eps'])
% [] If there is a region of constant pressure, all but the
% first of the consecutive levels of constant pressure should be
% flagged as bad data (‘4’).
% If there is a region where pressure reverses, all of the
% pressures in the reversed part of the profile should be flagged
% as bad data.
% All pressures flagged as bad data and all of the associated
% temperatures and salinities should be removed.
% NO ACTION. IMPLEMENT WHEN IT HAPPENS.
% Monotonicity is a test in CHECK_PROFILES below.
comments=[comments,lower(nbt),' (>',int2str(MAP_P_EXCLUDE),' m); '];
clear jnb nbt
else
system(['rm -f ',outfiles{I}(1:end-4),'_pressure_warning.eps']); % Clean up any previous plot
end
% NEW! THE DOUBLE-POINTED SPIKE TEST:
% Some times the spike is made up of two deviating points.
% Test value = | (V2 + V3)/2 – (V1 + V4)/2 | – | (V3 – V2) / 2 | – | (V4 – V1) / 2 |
% For this test the testvalues are placed on the top point of the
% imagined double point spike, hence, any finds will have to be
% supplemented with a true value below as well. There is no chance
% of looping over to next profile, since we pad with zeros at the
% bottom of the testvalue matrix. We use the same criteria as for
% single spikes.
V=PSAL_ADJUSTED;
testvalue = [zerow; abs( (V(2:end-2,:)+V(3:end-1,:))/2 - (V(4:end,:)+V(1:end-3,:))/2 ) ...
- abs( (V(2:end-2,:)-V(3:end-1,:))/2 ) - abs( (V(4:end,:)-V(1:end-3,:))/2 ) ; zerow ; zerow];
[PRES_ADJUSTED<500 & testvalue>0.9 | PRES_ADJUSTED>=500 & testvalue>0.02 | PRES_ADJUSTED>=1000 & testvalue>0.005]; % By experience clear spikes in the Nordic Seas
ans(find(ans)+1)=logical(1); % Also the next value
if any(ans,'all')
jnb=find(any(ans)); snb2=ans; nbt='Double-pointed salinity spike(s)';
disp(['prepare_floats: ',nbt,' in float ',float_names{I},' cycle(s): ',int2str(CYCLE_NUMBER(jnb)),' !']);
plot_profiles; print(gcf,'-depsc',[outfiles{I}(1:end-4),'_S-double_spike_warning.eps']);
PSALqct(snb2)='D';
comments=[comments,lower(nbt),'; '];
clear jnb nbt
else
system(['rm -f ',outfiles{I}(1:end-4),'_S-double_spike_warning.eps']);
end
V=TEMP_ADJUSTED;
testvalue = [zerow; abs( (V(2:end-2,:)+V(3:end-1,:))/2 - (V(4:end,:)+V(1:end-3,:))/2 ) ...
- abs( (V(2:end-2,:)-V(3:end-1,:))/2 ) - abs( (V(4:end,:)-V(1:end-3,:))/2 ) ; zerow ; zerow];
[PRES_ADJUSTED<500 & testvalue>6 | PRES_ADJUSTED>=500 & testvalue>2 | PRES_ADJUSTED>=1000 & testvalue>1]; % By experience in the Nordic Seas
ans(find(ans)+1)=logical(1); % Also the next value
if any(ans,'all')
jnb=find(any(ans)); tnb2=ans; nbt='Double-pointed temperature spike(s)';
disp(['prepare_floats: ',nbt,' in float ',float_names{I},' cycle(s): ',int2str(CYCLE_NUMBER(jnb)),' !']);
plot_profiles; print(gcf,'-depsc',[outfiles{I}(1:end-4),'_T-double_spike_warning.eps']);
TEMPqct(tnb2)='D';
comments=[comments,lower(nbt),'; '];
clear jnb nbt
else
system(['rm -f ',outfiles{I}(1:end-4),'_T-double_spike_warning.eps']);
end
% Spike tests (RTQC double check and stricter DMQC check for some):
% Test value = | V2 – (V3 + V1)/2 | – | (V3 – V1) / 2 |
% according to EuroGOOS, where V2 is the measurement being tested
% as a spike, and V1 and V3 are the values above and below.
V=PSAL_ADJUSTED; V(snb2)=NaN; % Remove above test results
testvalue = [zerow; abs(V(2:end-1,:)-(V(3:end,:)+V(1:end-2,:))/2) - abs((V(3:end,:)-V(1:end-2,:))/2) ; zerow];
[PRES_ADJUSTED<500 & testvalue>0.9 | PRES_ADJUSTED>=500 & testvalue>0.02 | PRES_ADJUSTED>=1000 & testvalue>0.005]; % By experience clear spikes in the Nordic Seas
if any(ans,'all')
jnb=find(any(ans)); snb1=ans; nbt='Salinity spike(s)';
disp(['prepare_floats: ',nbt,' in float ',float_names{I},' cycle(s): ',int2str(CYCLE_NUMBER(jnb)),' !']);
plot_profiles; print(gcf,'-depsc',[outfiles{I}(1:end-4),'_S-spike_warning.eps']);
PSALqct(snb1)='S';
comments=[comments,lower(nbt),'; '];
clear jnb nbt
else
system(['rm -f ',outfiles{I}(1:end-4),'_S-spike_warning.eps']);
end
V=TEMP_ADJUSTED; V(tnb2)=NaN; % Remove above test results
testvalue = [zerow; abs(V(2:end-1,:)-(V(3:end,:)+V(1:end-2,:))/2) - abs((V(3:end,:)-V(1:end-2,:))/2) ; zerow];
[PRES_ADJUSTED<500 & testvalue>6 | PRES_ADJUSTED>=500 & testvalue>2 | PRES_ADJUSTED>=1000 & testvalue>1]; % By experience in the Nordic Seas
if any(ans,'all')
jnb=find(any(ans)); tnb1=ans; nbt='Temperature spike(s)';
disp(['prepare_floats: ',nbt,' in float ',float_names{I},' cycle(s): ',int2str(CYCLE_NUMBER(jnb)),' !']);
plot_profiles; print(gcf,'-depsc',[outfiles{I}(1:end-4),'_T-spike_warning.eps']);
TEMPqct(tnb1)='S';
comments=[comments,lower(nbt),'; '];
clear jnb nbt
else
system(['rm -f ',outfiles{I}(1:end-4),'_T-spike_warning.eps']);
end
% Gradient test:
% Test value = | V2 − (V3 + V1)/2 |
% where V2 is the measurement being tested, and V1 and V3 are the values above and below.
V=PSAL_ADJUSTED; V(snb2)=NaN; V(snb1)=NaN; % Remove above test results
testvalue = [zerow ; abs(V(2:end-1,:)-(V(3:end,:)+V(1:end-2,:))/2) ; zerow];
switch direction
case 'A'
[PRES_ADJUSTED<500 & testvalue>1.5 | PRES_ADJUSTED>=500 & testvalue>0.5]; % The RTQC9 limits
case 'D'
% The RTQC9 limits plus ignore near surface which is often hampered by bad salinities in descending profiles
[PRES_ADJUSTED>2 & (PRES_ADJUSTED<500 & testvalue>1.5 | PRES_ADJUSTED>=500 & testvalue>0.5)];
end
if any(ans,'all')
jnb=find(any(ans)); snbG=ans; nbt='Salinity gradient(s)';
disp(['prepare_floats: ',nbt,' in float ',float_names{I},' cycle(s): ',int2str(CYCLE_NUMBER(jnb)),' !']);
plot_profiles; print(gcf,'-depsc',[outfiles{I}(1:end-4),'_S-gradient_warning.eps']);
PSALqct(snbG)='G';
comments=[comments,lower(nbt),'; '];
clear jnb nbt
else
system(['rm -f ',outfiles{I}(1:end-4),'_S-gradient_warning.eps']);
end
V=TEMP_ADJUSTED; V(tnb2)=NaN; V(tnb1)=NaN; % Remove above test results
testvalue = [zerow ; abs(V(2:end-1,:)-(V(3:end,:)+V(1:end-2,:))/2) ; zerow];
[PRES_ADJUSTED<500 & testvalue>9 | PRES_ADJUSTED>=500 & testvalue>3]; % The RTQC9 limits
if any(ans,'all')
jnb=find(any(ans)); tnbG=ans; nbt='Temperature gradient(s)';
disp(['prepare_floats: ',nbt,' in float ',float_names{I},' cycle(s): ',int2str(CYCLE_NUMBER(jnb)),' !']);
plot_profiles; print(gcf,'-depsc',[outfiles{I}(1:end-4),'_T-gradient_warning.eps']);
TEMPqct(tnbG)='G';
comments=[comments,lower(nbt),'; '];
clear jnb nbt
else
system(['rm -f ',outfiles{I}(1:end-4),'_T-gradient_warning.eps']);
end
% Density inversion test:
pres=PRES_ADJUSTED; pres(PRESqco=='4'|PSALqco=='4'|TEMPqco=='4')=NaN; % Skip already flagged data
PR = pres - [zerow; nandiff(pres)/2]; % Reference pressure
dens = sw_pden(PSAL_ADJUSTED,TEMP_ADJUSTED,pres,PR);% Potential density
dens(snb2)=NaN; dens(tnb2)=NaN; dens(snb1)=NaN; dens(tnb1)=NaN; dens(snbG)=NaN; dens(tnbG)=NaN; % Remove above test results
downw=nandiff(dens)<-0.03; % Downward test
pres([downw;logical(zerow)])=NaN; % Remove found
PR = pres - [zerow; nandiff(pres)/2]; % Reference pressure again
dens = sw_pden(PSAL_ADJUSTED,TEMP_ADJUSTED,pres,PR);% Potential density again
upw=nandiff(dens)<-0.03; % Upward test
logical([downw;zerow]+[zerow;upw]); % Logical for bad data
if any(ans,'all')
jnb=find(any(ans)); inb=ans; nbt='Density inversion(s)';
disp(['prepare_floats: ',nbt,' in float ',float_names{I},' cycle(s): ',int2str(CYCLE_NUMBER(jnb)),' !']); % Only warning
plot_profiles; print(gcf,'-depsc',[outfiles{I}(1:end-4),'_inversion_warning.eps'])
PSALqct(inb)='I'; TEMPqct(inb)='I';
comments=[comments,lower(nbt),'; '];
clear inb jnb nbt
else
system(['rm -f ',outfiles{I}(1:end-4),'_inversion_warning.eps']);
end
% A little text about profiles with RTQC flags (in all profiles):
jnb=find(any(PRESqco=='4'|TEMPqco=='4'|PSALqco=='4'));
if any(jnb)
disp(['prepare_floats: RTQC flags ''4'' exists in float ',float_names{I},' cycle(s): ',int2str(CYCLE_NUMBER(jnb)),' !']);
else
disp(['prepare_floats: No RTQC flags ''4'' exists in float ',float_names{I},'.']);
end
clear jnb
%%%%%%% PHASE 4: Visual control of all profiles and flags %%%%%%%
% Load the oposite direction profiles to use as parallel profiles in
% visual control:
switch direction
case 'D', [outfiles{I}(1:end-5),outfiles{I}(end-3:end)]; % Remove the D
case 'A', [outfiles{I}(1:end-4),'D',outfiles{I}(end-3:end)]; % Add the D
end
try
pp=load(ans,'PRES','TEMP','PSAL','CYCLE_NUMBER');
% In order to send these to check_profiles as the matching oposite
% parallel profiles, the cycle numbers have to be matched up:
jj=find(ismember(pp.CYCLE_NUMBER,CYCLE_NUMBER));
pp.CYCLE_NUMBER=pp.CYCLE_NUMBER(jj);
pp.PRES=pp.PRES(:,jj); pp.TEMP=pp.TEMP(:,jj); pp.PSAL=pp.PSAL(:,jj);
catch, pp=[];
end
% If the number of parallel profiles is less than the current
% dataset, check_profiles handles it. In the automatic tests,
% temporary flags are given, but _ADJUSTED variables are NOT changed
% (NaN'ed) anymore. Also, the flags given there are qct='X', so that
% they can be visually inspected and marked below. The warnings from
% the above tests should be duly noted while doing the visual check.
% Apart from that, at this point qct is identical to qco (apart from
% the top-removals in descending profiles), and qcn is flagging from
% previous DMQC.
%
[PRESqct,TEMPqct,PSALqct] = check_profiles(PRES,TEMP,PSAL,PRESqct,TEMPqct,PSALqct,PROFILE_NO(J),LONG,LAT,CYCLE_NUMBER,'refdatadir',tardir,'time',time,'parallelprofiles',pp);
%
% Change any flags from the automatic checks overlooked in visual:
PRESqct(PRESqct=='Y')='4'; TEMPqct(TEMPqct=='Y')='4'; PSALqct(PSALqct=='Y')='4';
PRESqct(PRESqct=='X')='4'; TEMPqct(TEMPqct=='X')='4'; PSALqct(PSALqct=='X')='4';
PRESqct(PRESqct=='S')='4'; TEMPqct(TEMPqct=='S')='4'; PSALqct(PSALqct=='S')='4';
PRESqct(PRESqct=='D')='4'; TEMPqct(TEMPqct=='D')='4'; PSALqct(PSALqct=='D')='4';
PRESqct(PRESqct=='G')='4'; TEMPqct(TEMPqct=='G')='4'; PSALqct(PSALqct=='G')='4';
PRESqct(PRESqct=='I')='4'; TEMPqct(TEMPqct=='I')='4'; PSALqct(PSALqct=='I')='4';
% If you started the visual not at the first profile or stopped the
% visual before checking all profiles, the remaining columns of qct
% will be blank. Automatic flags from those columns will be lost
% too, since they are not controlled visually. The first and last
% checked profile will then be number:
find((~all(PRESqct==' ' & TEMPqct==' ' & PSALqct==' ')));
visd=mima(ans);
% Now change new flags only when
% - profile is checked in present DMQC
% - and temporary flags differ from RTQC flags
mask=false(m,n); mask(:,visd(1):visd(2))=true;
PRESqcn(mask)=' '; TEMPqcn(mask)=' '; PSALqcn(mask)=' '; % Clear qcn for checked profiles
PRESqct~=PRESqco & mask ; PRESqcn(ans)=PRESqct(ans); % Add only new p flags in checked profiles
TEMPqct~=TEMPqco & mask ; TEMPqcn(ans)=TEMPqct(ans); % Add only new T flags in checked profiles
PSALqct~=PSALqco & mask ; PSALqcn(ans)=PSALqct(ans); % Add only new S flags in checked profiles
% % - temporary flags differ from old (RTQC or previous DMQC) flags,
% % i.e., new flags or reversal of RTQC flags;
% % - and temporary flags differ from DMQC flags, i.e. DMQC is
% % re-done on some profiles (changed) or on new profiles (from
% % blanks);
% % - but only overwrite profiles that you actually have checked
% % this time.
% % mask=false(m,n); mask(:,visd(1):visd(2))=true;
% % PRESqct~=PRESqco & PRESqct~=PRESqcn & mask; PRESqcn(ans)=PRESqct(ans);
% % TEMPqct~=TEMPqco & TEMPqct~=TEMPqcn & mask; TEMPqcn(ans)=TEMPqct(ans);
% % PSALqct~=PSALqco & PSALqct~=PSALqcn & mask; PSALqcn(ans)=PSALqct(ans);
%clear *qct
% Reversal of old DMQC is also possible and fine, since qcn is
% updated and saved after each session.
% qct is now gone, while *qco and *qcn do not change from here on until saved.
%
disp(['prepare_floats: Profiles ',int2str(visd(1)),'-',int2str(visd(2)),' checked. Update the parameter ''',...
checked_name,''' in INIT_DMQC from ',int2str(checked{I}),' to ',int2str(visd(2)),...
' for Float ',float_names{I},', and re-run prepare_floats!']);
else % IF NO NEW/UNCHECKED PROFILES => MAKE THE MULTIPANEL PLOTS:
% This is the case when visual control is already done on all
% profiles, but you re-run this script in order to make the
% multipanel plots about the flags.
fid=fopen('RTQCcheck.tex','w'); % For the report
% Find profiles with any bad-flags:
badpoints = PRESqco=='4' | PSALqco=='4' | TEMPqco=='4' | PRESqcn=='4' | PSALqcn=='4' | TEMPqcn=='4';
J=find(any(badpoints(:,1:ASD-1),1)); % Limit to cycles prior to ASD
% If any bad flags, make plots, but only for ascending profiles (descending almost always have flags)
if any(J) & direction=='A'
N=length(J);
for j=1:N % Loop all columns with bad-flags
% Vertical indices for old and new flags in this profile:
iPo=find(PRESqco(:,J(j))=='4'); iPn=find(PRESqcn(:,J(j))=='4');
iTo=find(TEMPqco(:,J(j))=='4'); iTn=find(TEMPqcn(:,J(j))=='4');
iSo=find(PSALqco(:,J(j))=='4'); iSn=find(PSALqcn(:,J(j))=='4');
jj=J(j)+[-3:3]; % indices for neighbouring profiles