% 21202901_RTqc.m

% create matrix of profnum, for easier indexing
profnum = ones(size(s.pres,1),1)*(1:size(s.pres,2)); 

% indicies for bad ctd data. 
i8ctd = ...
    (profnum == 60) | ...; % whole profile looks off. even temperaure. I'm guessing inlet was clogged, so it wasn't getting flushed properly. 
    (profnum == 504 & s.pres < 600 & s.pres > 100) | ... % clogged ctd; salinity looks OK, but pH and O2 do not. 
    (profnum == 645 & s.pres < 20) | ... % something clogged ctd around 20. pH & O2 look vertical above 50 m, but I think that's real looking at other profiles
    (profnum == 650 & s.pres < 50) | ... % looks like a ctd clog. temperature probably still oK. 
    (profnum == 685 & s.pres < 76 & s.pres > 74) | ... % spike in salinity
    (profnum == 690 & s.pres < 41 & s.pres > 40) | ... % spike in salinity
    (profnum == 757 & s.pres <50) | ... % clog in ctd
    (profnum == 814 & s.pres < 96 & s.pres > 90) | ... % spike in salinity, DO, pH
    (profnum == 940) | ... % looks like something slightly blocking inlet of ctd
    (profnum == 960) | ... % looks like something slightly blocking inlet of ctd
    (profnum == 963) | ... % looks like something slightly blocking inlet of ctd
    (profnum == 973); % salinity is noisy. probably 'ok', but be conservative


%     (profnum == 419 & s.pres < 175 & s.pres > 35) | ... % clog for most of the profile
%     (profnum == 432 & s.pres < 130 & s.pres > 90) | ...; % small clog in salinity 
%     (profnum == 512 & s.pres < 105 & s.pres > 50) | ...;
%     (profnum == 569 & s.pres < 176 & s.pres > 175) | ...; 
%     (profnum == 696 & s.pres > 120); % clogged salinity at pressure  


% addiitonal QC for pH and O2 to correct for slow response
i8phox = i8ctd; % | ...
%     (profnum == 146 & s.pres > 20 & s.pres < 120) | ... % clogged salinity; 
%     (profnum == 419 & s.pres < 175 & s.pres < 26) | ...
%     (profnum == 512 & s.pres < 105 & s.pres < 40) | ...
%     (profnum == 696 & s.pres > 110); % clogged salinty at depth; give 10 extra m buffer for pHOx
     

% write ctd QC flgs. Chl-a too, because it's in the pumped flow stream
s.psal_QC(i8ctd) = 8;
s.pdens_QC(i8ctd) = 8;
s.opt_QC(i8ctd) = 8; 
% write pHOx QC flags
s.moxy_QC(i8phox) = 8;
s.doxy_QC(i8phox) = 8;
s.pHin_QC(i8phox) = 8;
s.o2satper_QC(i8phox) = 8;


% QC variables that exist at this point in the process. The derived variables should carry pHOx
% flags
% 'pres_QC', 'tc_QC', 'psal_QC', 'opt_QC', 'moxy_QC', 'doxy_QC', 'pdens_QC', 'pHin_QC', 'depth_QC', 'o2satper_QC', 'opt_QC'














