%% Process_LICOR_for_Kd.m: Script used to calculate the downward diffuse attenuation coefficient (Kd) from LICOR and Onset
%  Water Level sensor. This script assumes 3 casts are completed at each station. For each station, Kd is calculated 
%  for individual casts using function get_licor_kd, and then an average Kd, and standard error for each station is saved. 
%  For each station, A figure showing  downward irradiance and Kd for each cast is saved.
%  
%  Before running this script the Onset water level .csv files must be
%  "saved as" .xls files AND DELETE extra lines in the Licor.txt. 
%
%  User needs to update the cruise name (cruise) the station names (station) 
%
%   Calls the Following Functions:
%   1. convert_LICOR_text_to_mat
%   2. get_licor_kd
%
%
%KAF July 2016
%% Read in LICOR data and covert it to a mat file
cruise='YR160902';
datafile=[cruise, '.txt'];

year=2016;
month=9;
day=2;

licor=convert_LICOR_text_to_mat('YR160902_LICOR.txt');
xlsfile=[cruise '_LICORtime.xlsx'];
folder=('/Users/kelsey/Desktop/Dissertation/Profiler Surveys/York River/Axial Surveys/YR160902/LICOR/');

distance=[11
    18
    30
    39
    45
    56];
%% Define Stations

station=cellstr(['S5714'
    'S5715'
    'S5716'
    'S5717'
    'S5718'
    'S5719']);


for ii=1:length(station)
    [A,A_all,Kd(1,:)]=get_licor_kd(station(ii),licor,day,month,year,xlsfile,'A');
    [B,B_all,Kd(2,:)]=get_licor_kd(station(ii),licor,day, month, year,xlsfile,'B');
    [C,C_all,Kd(3,:)]=get_licor_kd(station(ii),licor,day, month, year,xlsfile,'C');

  figure(100)
  plot(log(A(:,4)./A(:,3)),A(:,2),'bo-',log(B(:,4)./B(:,3)),B(:,2),'rx-',log(C(:,4)./C(:,3)),C(:,2),'ks-','Markersize',8,'linewidth',2)
  set(gca,'Ydir','reverse')
  set(gca,'Fontsize',16)
  ylabel('Depth (m)')
  xlabel('ln(E_d/E_d_e_c_k)')
  title(station(ii))
  str_1=sprintf('%0.2f', Kd(1,2));
  str_2=sprintf('%0.2f', Kd(1,3));
  str_3=sprintf('%0.2f', Kd(2,2));
  str_4=sprintf('%0.2f', Kd(2,3));
  str_5=sprintf('%0.2f', Kd(3,2));
  str_6=sprintf('%0.2f', Kd(3,3));
  
  txt1=['K_d_A= ' str_1 ' +/- ' str_2];
  txt2=['K_d_B= ' str_3 ' +/- ' str_4];
  txt3=['K_d_C= ' str_5 ' +/- ' str_6];
 
  a=gca;
  
  text(max(a.XLim)-2,max(a.YLim)-0.25,txt3,'FontSize',14,'color','k')
  text(max(a.XLim)-2,max(a.YLim)-0.50,txt2,'FontSize',14,'color','r')
  text(max(a.XLim)-2,max(a.YLim)-0.75,txt1,'FontSize',14,'color','b')
  
  saveas(gcf,[char(station(ii)),'_irrad.fig'])
  close all
  
 
 AvgKd=[mean(Kd(:,1)) nanmean(Kd(:,2)) nanstd(Kd(:,2))./sqrt(length(isnan(Kd(2,:)==0)))];
  LICOR.A=A;
  LICOR.A_all=A_all;
  LICOR.B=B;
  LICOR.B_all=B_all;
  LICOR.C=C;
  LICOR.C_all=C_all;
  LICOR.Kd=Kd;
  LICOR.AvgKd=AvgKd;
 
 save(fullfile(folder,[char(station(ii)),'_avg.mat']), 'AvgKd', 'A','B' ,'C', 'Kd', 'A_all', 'B_all', 'C_all')
 %save(fullfile(folder,[char(station(ii)),'_avg.mat']), 'AvgKd', 'Kd', 'A_all', 'B_all', 'C_all')

 clear AvgKd A B C Kd A_all B_all C_all str_1 str_2 str_3 str_4 str_5 str_6 txt1 txt2 txt3
 
%save(fullfile(folder,char(station(ii))),'-struct','LICOR'); 
%save(fullfile(folder,char(station(ii))),'-struct','LICOR'); 

end

clear licor
create_data_struct_csv_LICOR(cruise,folder,distance,station)

