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

% Loads internally-logged CTD data, already processed in SBE
%  Data Processing software (T, S, p, O2, fluor) from .cnv files;
%  Loads internally-logged pH data and converts raw V1 to pH [total scale];
%  Saves all internally-logged data to one merged .mat file.

mydir = ['//atlas.shore.mbari.org/Diatom/socal moorings/' ...
  'deployment_data/20161025/internal/'];

%cal info - manually entered from OCSD.Sensor.Inventory.xlsx
 %pHcal    de-amp slope  de-amp int  E0        Ts
  pHcal = [1             0          -0.3962653 294.24; ... % #78
           1             0          -0.3895079 294.44; ... % #79
           1             0          -0.4178267 294.20; ... % #80
           0.0920257    -0.0359259  -0.4027596 294.27];    % Sfc 

%deployment beg, end
 tBeg = datenum([2016 10 26 22 0 0]);
 tEnd = datenum([2017 1 25 20 30 0]);

%--%

% First, read-in processed CTD data as matrices

dn = datenum([2016 1 0]); %data are in 'year-day'

%CTD78
 fid = fopen([mydir 'CTD/cnv/SBE16plus-IM_01650078_2017_01_27.cnv']);
 %skip header
  fline = '';
  while isempty(strfind(fline,'*END*'))
    fline = fgetl(fid);
  end
 %read-in
  CTD78int = fscanf(fid,repmat('%f',1,10),[10,Inf])';
 %close
  fclose(fid);
 %convert year-day to serial datenum
  CTD78int(:,1) = CTD78int(:,1) + dn;
 %chop to deployment period
  f = find(CTD78int(:,1)<tBeg | CTD78int(:,1)>tEnd);
  CTD78int(f,:) = [];
  clear fid fline f ans;

%CTD79
 fid = fopen([mydir 'CTD/cnv/SBE16plus-IM_01650079_2017_01_27.cnv']);
 fline = '';
 while isempty(strfind(fline,'*END*'))
   fline = fgetl(fid);
 end
 CTD79int = fscanf(fid,repmat('%f',1,9),[9,Inf])';
 fclose(fid);
 CTD79int(:,1) = CTD79int(:,1) + dn;
 f = find(CTD79int(:,1)<tBeg | CTD79int(:,1)>tEnd);
 CTD79int(f,:) = [];
 clear fid fline f ans;

%CTD80
 fid = fopen([mydir 'CTD/cnv/SBE16plus-IM_01650080_2017_01_27.cnv']);
 fline = '';
 while isempty(strfind(fline,'*END*'))
   fline = fgetl(fid);
 end
 CTD80int = fscanf(fid,repmat('%f',1,9),[9,Inf])';
 fclose(fid);
 CTD80int(:,1) = CTD80int(:,1) + dn;
 f = find(CTD80int(:,1)<tBeg | CTD80int(:,1)>tEnd);
 CTD80int(f,:) = [];
 clear dn fid fline f ans;

%--%

% Now, read-in internally-logged pH data and 
%  convert raw 'V1' voltages to pH [total scale] using CTD T

dn = datenum([1900 1 1]);

%pH78
 fid = fopen([mydir 'pH/pH_SN78_N44928.csv']);
 %skip header
  fline = '';
  while isempty(strfind(fline,'# ID #'))
    fline = fgetl(fid);
  end
 %read-in
  pH78int = [];
  fline = fgetl(fid);
  while isempty(strfind(fline,'"[End Reading]"'))
    st = strsplit(fline,{'"',','});
    pH78int = [pH78int; str2double(st(3:4))];
    fline=fgetl(fid);
  end
 %close
  fclose(fid);
 %convert to serial datenum
  pH78int(:,1) = pH78int(:,1) + dn - 2; %subtract 2 days from time stamp
 %chop to deployment period
  f = find(pH78int(:,1)<tBeg | pH78int(:,1)>tEnd);
  pH78int(f,:) = [];
 %convert V1 to pH [total scale]
  tempi = interp1(CTD78int(:,1),CTD78int(:,2),pH78int(:,1));
  pH78int(:,3) = calc_pH(pHcal(1,:),tempi,pH78int(:,2));
  clear fid fline st f tempi ans;

%pH79
 fid = fopen([mydir 'pH/pH_SN79_N96163.csv']);
 fline = '';
 while isempty(strfind(fline,'# ID #'))
   fline = fgetl(fid);
 end
 pH79int = [];
 fline = fgetl(fid);
 while isempty(strfind(fline,'"[End Reading]"'))
   st = strsplit(fline,{'"',','});
   pH79int = [pH79int; str2double(st(3:4))];
   fline=fgetl(fid);
 end
 fclose(fid);
 pH79int(:,1) = pH79int(:,1) + dn - 2;
 f = find(pH79int(:,1)<tBeg | pH79int(:,1)>tEnd);
 pH79int(f,:) = [];
 tempi = interp1(CTD79int(:,1),CTD79int(:,2),pH79int(:,1));
 pH79int(:,3) = calc_pH(pHcal(2,:),tempi,pH79int(:,2));
 clear fid fline st f tempi ans;

%pH80
 fid = fopen([mydir 'pH/pH_SN80_N75170.csv']);
 fline = '';
 while isempty(strfind(fline,'# ID #'))
   fline = fgetl(fid);
 end
 pH80int = [];
 fline = fgetl(fid);
 while isempty(strfind(fline,'"[End Reading]"'))
   st = strsplit(fline,{'"',','});
   pH80int = [pH80int; str2double(st(3:4))];
   fline=fgetl(fid);
 end
 fclose(fid);
 pH80int(:,1) = pH80int(:,1) + dn - 2;
 f = find(pH80int(:,1)<tBeg | pH80int(:,1)>tEnd);
 pH80int(f,:) = [];
 tempi = interp1(CTD80int(:,1),CTD80int(:,2),pH80int(:,1));
 pH80int(:,3) = calc_pH(pHcal(3,:),tempi,pH80int(:,2));
 clear dn fid fline st f tempi ans;

%--%

%save converted data
 save([mydir 'CTD_pH_conv.mat'],'CTD78int','CTD79int','CTD80int',...
   'pH78int','pH79int','pH80int');
