%SILTPROFILER_MAIN  main script for processing all siltprofiler data
%
% ... some proper documentation ...
%
% See also: siltprofiler_read_asc, siltprofiler_plot, siltprofiler_plot_debug

clear all
close all
clc
%%  LOAD
OPT.rawbase = 'C:\Users\Eigenaar\Documents\CiTG\Master thesis\Master Thesis\files'; % TS-laptop
OPT.rawbase = '..\..\raw\'; % GJdB
OPT.rawbase = 'D:\smolders\Master thesis\files'; % TS



%%   CALCULATE
OPT.CalculateSalinity    = 1; %calculates the salinity from pressure and conductivity and temperature.
OPT.CalculateDepth       = 1; %calculates the Depth from Pressure and Latitude.
OPT.CalculateFittedOBS   = 0; % calculates several fits for the OBS data.
OPT.BinSalinity          = 1; % bins salinity.

%%  PLOT
OPT.pause             = 0 ; % gives a pausedisp after each file. 
OPT.silt_plot         = 0; % makes plots of T,C,OBS,Supply,Salinity,Chlorophyl,Altimeter .
OPT.debug_plot        = 0; % debug plotter, used for filtering out air bubbles.
OPT.KML_plot          = 0; % makes a scatterplot to view the track of the
                           % vessel during the measurement in GoogleEarth.                           
OPT.fitted_OBS_plot   = 0; % makes a plot of the OBS data and 2 fitted curves.
OPT.KNMI_plot         = 0; % makes a plot of the knmi wind data. 
OPT.PlotSalinity      = 0; % plots the salinity at one location for several measurements times.
OPT.salinity_plot     = 0; % makes solitary salinity plots.
OPT.Wave_plot         = 0; % plot significant wave heights. 
OPT.Tide_plot         = 0; % plot Tide near Scheveningen in cm plus NAP
OPT.PlotTemperature   = 0;  
OPT.Plotslinger       = 0; % plots slinger only if the right xls-file is used.
OPT.PlotVeloslinger   = 0; % plots slinger for velocity and direction.
%% get information

L    = xls2struct('slingerplot.xls');
K    = ('potwind_330_2001\potwind_330_2001');
W    = knmi_potwind([OPT.rawbase,filesep,K]);
%Wave = rws_waterbase_read('D:\smolders\Master thesis\files\id22-IJMDMNTSPS-20090701000-200908032359','method','fgetl');
%Wavedir =rws_waterbase_read('D:\smolders\Master thesis\files\id23-IJMDMNTSPS-200907290000-200908012359.txt','method','fgetl');
%Tide = rws_waterbase_read('D:\smolders\Master thesis\files\id1-SCHEVNGN-200907010000-200908032359.txt','method','fgetl');

%%
for ifile=36:length(L.filename)

   disp(['Processing ',num2str(ifile,'%0.3d'),': ',L.filename{ifile}])

%% Read

  jfile=ifile;
  
  A(ifile) = siltprofiler_read_asc([OPT.rawbase,filesep,L.filename{ifile}]); % Reads the ASCII-files into struct A
 %A(ifile) = siltprofiler_read_bin([OPT.rawbase,filesep,L.filename{ifile}],L.inifile{ifile}); % ultimately: lees direct uit bin mbv ini
  if ~isnan(L.gpsfilename{jfile})
  D(jfile) = siltprofiler_read_gps([OPT.rawbase,filesep,L.gpsfilename{jfile}]);%Reads the GPS-files into struct D
  else
  D(jfile) = siltprofiler_read_gps(''); % dummy
  end

  ADCPData{ifile} =ADCP_readAquaVisionTekal([OPT.rawbase,filesep,L.adcpname{jfile}]);
  
%% Analyses

   count=0; % count is used for creating a filename for kmlplot
   for j=1:jfile
      count=count+1;
   end
   count;

  % LAT(jfile)=D(jfile).lat(1)
 
  %A(ifile).tidal_phase = siltprofiler_datenum2tidal_phase  (A(file).datenum,wateratanded_hoek_van holland, waterstande_den_helder);
  if OPT.CalculateSalinity
  A(ifile).salinity    = conductivity2salinity(A(ifile).c,A(ifile).t,A(ifile).pres);
  end
  
  if OPT.CalculateDepth
  if ~isempty(D(ifile).lat)
  A(ifile).depth       = pressure2depth(A(ifile).pres,D(ifile).lat(1));
  end
  end
  
  if OPT.BinSalinity
  B(ifile)= siltprofiler_bin_salinity(A(ifile).depth,...
                                      A(ifile).salinity,...
                                      A(ifile).obs,...
                                      A(ifile).chlo,...
                                      A(ifile).t,...w
                                      L.down_start(ifile),...
                                      L.down_end(ifile),...
                                      D(ifile).lon(1),...
                                      D(ifile).lat(1));  
  end
  
 %[A(ifile).samp1,A(ifile).samp2,A(ifile).samp3] = shift_in_pos(A(ifile).depth); 
 if OPT.CalculateFittedOBS
 [A(ifile).fit1, A(ifile).fit2, A(ifile).fit3, A(ifile).fit4] = fitje(A(ifile).depth, A(ifile).obs,L.down_start(ifile),...
                   L.down_end(ifile));
 end
%% Plotten
  
  
  if OPT.debug_plot
   siltprofiler_plot_debug(A(ifile),...
                           L.index_start(ifile),...
                           L.index_end(ifile)); 
                            
  end                      
  
  if OPT.silt_plot                          
   siltprofiler_plot      (A(ifile),...
                           L.down_start(ifile),...
                           L.down_end(ifile));  
  end
  
  if OPT.salinity_plot
   siltprofiler_salinity_plot      (A(ifile),...
                                   L.down_start(ifile),...
                                   L.down_end(ifile)); 
                                   
  end                                   
  if OPT.KML_plot
    siltprofiler_gps_kmlplot(D(jfile).lat,D(jfile).lon,count);
                             
  end
  
  if OPT.fitted_OBS_plot                          
     siltprofiler_plot_fitted_obs(A(ifile),...
                                   L.down_start(ifile),...
                                   L.down_end(ifile));     
  end                       

  
      if OPT.pause
         pausedisp
      end
end 

if OPT.PlotSalinity
siltprofiler_plot_salinity(A,L) 
end

if OPT.PlotTemperature
siltprofiler_plot_temperature(A,L) 
end
   
if OPT.KNMI_plot
      KNMI_plot(W.datenum,W.UP,W.DD)
end

if OPT.Wave_plot
    Wave_plot(Wave)
end

if OPT.Tide_plot
    Tide_plot(Tide,A)
end

if OPT.Plotslinger
    slinger(A,B,D)
end

if OPT.PlotVelsSlinger
    meanvel(ADCPData,A,D)
end