
% -- 
% Jeff Sevadjian
% Monterey Bay Aquarium Research Institute
% 7700 Sandholdt Rd
% Moss Landing, CA 95039, USA
% ph: +1 831-775-1888

% Human-in-the-loop QC procedure to generate final QC'd WG data set.

% All the 'processing' (applying cal's, deriving of engineering units, etc.)
%  is now done without human input in python scripts, either in
%  real-time (makeWGwebQC_wgTiny.py), or post-cruise (loadWGdata_wgTiny.py).
% This Matlab script is used only to identify and remove bad data.

% Outputs final .csv files in the /QC directory
% A final .nc file (merged to a common 10-min time stamp) needs to be
%  created after this script is run using makeWGnetCDF_wgTiny.py.

% Updated on 03/21/19 by Devon Northcott to add in the ability to remove
% NaN's that were incorectly applied, and to streamline most of the
% operations into sub-funtions, hopefully making the script more modular,
% and making it easier to build QC scripts for other platforms

% Updated on 06/09/22 by Nicholas Trautman to add a path to the QC
% subfunctions

%depl info
 veh = 'wgTiny';
 dep = '20171019';

%directories for post-cruise and QC'd data
 atlas = '//atlas.shore.mbari.org/';
mydirPC = [atlas 'WaveGlider/deployment_data/' veh '/' dep '/postCruise/'];                       
mydirQC = [atlas 'WaveGlider/deployment_data/' veh '/' dep '/QC/'];                             
%  mydirPC = [atlas 'dnorthcott/' dep '/postCruise/'];
%  mydirQC = [atlas 'dnorthcott/' dep '/QC/'];
 clear veh dep;
 
%add path to QC subfunctions:
addpath(genpath('\\atlas\diatom\users\dsancho\MATLAB\bog_processingscripts\QCCode'));

%create /QC dir if doesn't exist
 if ~exist(mydirQC,'dir')
   mkdir(mydirQC);
 end

% read-in postCruise .csv files.  NaN zeros, convert time stamp
% .csv files must be in the format indicated otherwise the QC functions 
% will fail.
 dn = datenum([1970 1 1]);
 %Position [time,lon,lat]
  Position = Load_CSV_for_QC([mydirPC 'Position.csv']);
 %CO2 [time,pH, pCO2water, pCO2air]
  CO2 = Load_CSV_for_QC([mydirPC 'CO2.csv']);
 %CTD [time,T, S, O2]
  CTD = Load_CSV_for_QC([mydirPC 'CTD.csv']);
 %Eco-Puck [time,fluor, bb470, bb650]
  EP = Load_CSV_for_QC([mydirPC 'EcoPuck.csv']);
 %MET [time,airPress, airTemp]
  MET = Load_CSV_for_QC([mydirPC 'MET.csv']);
 %MET wind [time,windDir, windSpd]
  METw = Load_CSV_for_QC([mydirPC 'METwind.csv']);
 %CO2 Equil
  CO2equil = Load_CSV_for_QC([mydirPC 'CO2equil.csv']);
 %CO2 Zero
  CO2zero = Load_CSV_for_QC([mydirPC 'CO2zero.csv']);
 %CO2 Air
  CO2air = Load_CSV_for_QC([mydirPC 'CO2air.csv']);
 %CO2 Stnd
  CO2stnd = Load_CSV_for_QC([mydirPC 'CO2stnd.csv']);
 %CO2 Hum
  CO2hum= Load_CSV_for_QC([mydirPC 'CO2hum.csv']);
  
%Plot all sensor data in one fig
% This is to compare different data sets for simultaneous 'events' -
%  i.e. if there is a spike in O2 and pCO2w at the same time, they 
%  are both likely to be 'real.' This is to aid removal of data which
%  is implemented in the individual sensor break-out figures.
 qcWG_plotAll(Position,CO2,CTD,EP,MET,METw);

%plot map 
 load('CaCoast.mat')
 figure; hold on;
 scatter(Position(:,2),Position(:,3),3,Position(:,1),'filled');
 colormap(jet);
 plot(CaCoast.lon,CaCoast.lat,'k','LineWidth',2);
 plot(-122.02203,36.75060,'k.','MarkerSize',10); %M1
 plot(-122.378,36.697,'k.','MarkerSize',10); %M2
 plot(-121.847,36.797,'k.','MarkerSize',10); %C1
 axis([min(Position(:,2))-0.2,max(Position(:,2))+0.2,...
     min(Position(:,3))-0.2,max(Position(:,3))+0.2])
 
CTD=QC_CTD(CTD,[mydirQC 'CTD.csv']);

CO2=QC_pCO2(CO2,CO2equil,CO2zero,CO2air,CO2stnd,CO2hum,CTD,...
    [mydirQC 'CO2.csv']);

EP=QC_EP(EP,[mydirQC 'EcoPuck.csv']);

MET=QC_MET(MET,[mydirQC 'MET.csv']);

METw=QC_METw(METw,[mydirQC 'METwind.csv']);

Position=QC_Position(Position,[mydirQC 'Position.csv']);

BogLine=QC_BogLine(Position,[mydirQC 'Bog_Line.csv']);

%simply copy the Waypoint.csv file without any user-input
 copyfile([mydirPC 'Waypoint.csv'],[mydirQC 'Waypoint.csv']);
 
%------------%

%Final Check:
 qcWG_plotAll(Position,CO2,CTD,EP,MET,METw);
