Difference between revisions of "Moorea mergeIOP"

From Pnb
Jump to navigation Jump to search
(Created page with " </function BVal_mergeIOP() %Mfile to merge LISST, SBE25 data and BB9 data. %first lisst date needs to be converted as it is in sepparate columns %day+hr and min+seconds, then...")
 
 
Line 1: Line 1:
  
</function BVal_mergeIOP()
+
<pre>
 +
function BVal_mergeIOP()
 
%Mfile to merge LISST, SBE25 data and BB9 data.
 
%Mfile to merge LISST, SBE25 data and BB9 data.
 
%first lisst date needs to be converted as it is in sepparate columns
 
%first lisst date needs to be converted as it is in sepparate columns
Line 883: Line 884:
  
  
return>
+
return
 +
</pre>

Latest revision as of 13:23, 27 October 2015

function BVal_mergeIOP()
%Mfile to merge LISST, SBE25 data and BB9 data.
%first lisst date needs to be converted as it is in sepparate columns
%day+hr and min+seconds, then lisst time must be set to zero start. Also
%bb9 must be set to zero start, then bb9 and lisst can be merged.  CTD and
%LISST + BB9 will have to be interpolated to match time sample rates
%minilogger sample frequency 1Hz
%lisst sample freq 1Hz
%ctd sample freq 8Hz
%BB9 sample freq 1Hz
%ACS sample freq 4 Hz

pathname = pwd;
dhnumber = pathname(end-2:end);
%=====LISST DATA SET MODULE========
data_lst = load('LISST_indexarr');
lst_depth = .01*data_lst(:,37);
lst_date1 = num2str(data_lst(:,39));
lst_date2 = num2str(data_lst(:,40));
lst_day = str2num(lst_date1(:,1:3));
lst_hrs = str2num(lst_date1(:,4:5));

%when lisst minutes reset every 60 sec
for ml =1:length(lst_date2)
    hld = lst_date2(ml,1:2);
    char(hld);
    hld = str2num(hld);
    if isempty(hld)
        lst_min(ml) =0;
        continue
    end
    lst_min(ml) = hld;%str2num(lst_date2(ml,1:2))
    
end
lst_min = (lst_min)';
lst_sec = str2num(lst_date2(:,3:4));

JD = lst_hrs.*3600 + lst_min.*60 + lst_sec; %this is in seconds
%start from zero
JD = JD-JD(1); %lisst time in seconds starting from zero
%======END LISST MODULE========


%=====BB9 MODULE========
% since BB9 time is more accurate, use it for merging
ECOname = strcat('archive_23_ECO.',dhnumber);
%[BB9_data,BB9_HDR] = hdrload('archive_23_ECO.040');
[BB9_data,BB9_HDR] = hdrload(ECOname);
BB9_time  = BB9_data(:,1); %(in ms) si
BB9_time = BB9_time - BB9_time(1);
BB9_time = BB9_time./1000;


%====CTD MODULE=======
CTDname = strcat('archive_22_SBE25CTD.',dhnumber)

[CTD_dat,CTD_hdr] = hdrload(CTDname);

%[CTD_dat,CTD_hdr] = hdrload('archive_22_SBE25CTD.040');
ctd_time = CTD_dat(:,1); %ms
ctd_time = ctd_time./1000; %seconds
%ctd_depth = CTD_dat(:,2);
%ctd_temp = CTD_dat(:,3);
%ctd_cond = CTD_dat(:,4);
%ctd_sal = CTD_dat(:,5);

%====ACS MODULE=======
ACSname = strcat('archive_21_ACS.',dhnumber)
[ACS_dat,ACS_hdr] = hdrload(ACSname);
%ACS_D = interp1(nmini30s_T,nmini30s_D,ctd_time);


%=====MERGING MODULE=====

BB_new = interp1(BB9_time,BB9_data,ctd_time);
sample =[];
if issorted(data_lst(:,1)) == 0
    issorted(JD)
    issorted(ctd_time)
    issorted(data_lst(:,1))
    issorted(data_lst(:,37))
    
    %at this point CTD and BB9 data is parsed.  BB data is matched to CTD time
    
    %======= FILTERING NAN IF TIME RECORDS GO OVER =====
    nn= isnan(BB_new);
    non= find(nn(:,1) ==1);
    CTD_dat(non,:) = [];
    BB_new(non,:) = [];
    %=====
    ctd_time = CTD_dat(:,1); %ms
    ctd_time = ctd_time./1000; %seconds
    ctd_depth = CTD_dat(:,2);
    ctd_temp = CTD_dat(:,3);
    ctd_cond = CTD_dat(:,4);
    ctd_sal = CTD_dat(:,5);
    
    file = CTDname;
% % %     file = input('What is the minilogger file name?','s');
% % %     fid = fopen(file)
% % %     %fid = fopen('Asc-5264.IOPBVAL9');
% % %     
% % %     tline = fgetl(fid);
% % %     %tline = fgetl(fid); %junk line test
% % %     % Split header
% % %     A(1,:) = regexp(tline, '\,', 'split');
% % %     
% % %     % Parse and read rest of file
% % %     ctr = 1;
% % %     while(~feof(fid))
% % %         if ischar(tline)
% % %             ctr = ctr + 1;
% % %             tline = fgetl(fid);
% % %             A(ctr,:) = regexp(tline, '\,', 'split');
% % %         else
% % %             break;
% % %         end
% % %     end
% % %     fclose(fid);
% % %     
% % %     %====minilogger parameters======
% % %     mini_time = cell2mat(A(:,2));
% % %     mni_hr = str2num(mini_time(:,1:2)) + 3; %convert LOCAL BDA to GMT
% % %     mni_mn = str2num(mini_time(:,4:5));
% % %     mni_sc = str2num(mini_time(:,7:8));
% % %     mni_depth = str2num(char(A(:,4)));
% % %     mni_temp = str2num(char(A(:,3)));
% % %     mini_sc_tot = mni_sc(:,:) + mni_mn.*60 + mni_hr.*3600; %time record from minilogger converted to seconds
% % %     
% % %     
% % %     %=========== laborious part to fix times and depth=========
% % %     %get run summary info start time to set CTD absolute start time
% % %     
% % %     rundir = '/home/eriks/BVAL/BVal47/BV47_IOP/DH-4-113/';
% % %     runnum = str2num(dhnumber);
% % %     runnumch = num2str(runnum);
% % %     runsubdir = strcat('run',runnumch);
% % %     sumfile = strcat('/Summary.',dhnumber);
% % %     
% % %     sumfid = fopen([rundir,runsubdir,sumfile],'r');
% % %     ln = fgetl(sumfid);
% % %     sumhr = str2num(ln(end-7:end-6));
% % %     summin = str2num(ln(end-4:end-3));
% % %     sumsec = str2num(ln(end-1:end));
% % %     
% % %     DH4_sc_total = sumhr*3600 + summin*60 + sumsec %sum total seconds of DH4 start time
% % %     mini_DH4_summary = (abs(mini_sc_tot - DH4_sc_total)); %the absolute min time difference between DH4 start
% % %     mindex = find(mini_DH4_summary == (min(mini_DH4_summary))); %this is where minilogger time and the DH4 START time intersect.
% % %     new_mini_time = mini_sc_tot(mindex:end); %starts minilogger record at DH4 start time
% % %     nmini_depth = mni_depth(mindex:end);
% % %     nmini_temp = mni_temp(mindex:end);
% % %     nmini_time = (new_mini_time-new_mini_time(1)); %sets new minilogger time to start at 0 seconds
% % %     
% % %     %=== CTD time record starts from zero but is 30 seconds after DH4 is
% % %     %plugged in.
% % %     
% % %     %============== important notes=================
% % %     %now add 30 seconds to mini start time and that will be
% % %     %start of recorded data.
% % %     %Interal LISST time is absolute and will start 10 seconds after time stamp in DH4
% % %     %summary file
% % %     %LISST files recorded in DH4 will start 30s after sart time in summary file
% % %     %to minimize the amount of data altering, get new depths for each
% % %     %instrument and process, don't alter data
% % %     %start time for everything = nmini_time(1) + 30s
% % %     
% % %     %==================================================
% % %     %==========================================================================
% % %     %==================================================
% % %     nmini30s_T = nmini_time(30:end);
% % %     nmini30s_D = nmini_depth(30:end);
% % %     nmini30s_Temp = nmini_temp(30:end);
% % %     nmini30s_T = nmini30s_T - nmini30s_T(1); %zeros the mini time in for 30s




set (gcf,'units','inches', 'pos',[2 1 9 5])
					% declare variables
u=[];					% holds the points
d=[];					% holds the index's only
cast=0;
plot(ctd_time,-ctd_depth,'.');grid; 

  cast=cast+1
  which=['Selecting points for cast: ',num2str(cast)];
					% first point
  disp('select the first region')
  title(which);
  [x,y]=ginput(2);
					% zoom to start point of the cast
  if y(1)<y(2)
    if x(1)<x(2)
      axis([x(1),x(2),y(1),y(2)])
    else
      axis([x(2),x(1),y(1),y(2)])
    end
  else
    if x(1)<x(2)
      axis([x(1),x(2),y(2),y(1)])
    else
      axis([x(2),x(1),y(2),y(1)])
    end					% end if
  end					% end if
					% pretty the graph
  hold on
  plot(ctd_time,-ctd_depth,'ro'); grid; title(which);
  plot([x(1), x(2)], [0, 0], 'g-.'); 
  hold off;
					% find specific start point
  disp('click on start of cast')
  [x,y]=ginput(1);
                                        % add the points to cast_id array
  u=[u;x,y];				
					% last point
  plot(ctd_time,-ctd_depth,'.');grid; title(which);
  hold on;
  %d=ceil(u(:,1))
  d=[];

  for id=1:size(u,1),
    dd=find(ctd_time<u(id,1));%___________________________
    d=[d; dd(end)];%-----------------------------------------
  end
  for ip=1:2:size(d,1),
      plot(u(:,1),u(:,2),'ro');
  end
% $$$   plot(mat_in(d(1):d(end),1),...
% $$$        mat_in(d(1):d(end),2),'g',u(:,1),u(:,2),'ro');
  disp('select the end region')
  [x,y]=ginput(2);
					% zoom to end point of the cast
  if y(1)<y(2)
    if x(1)<x(2)
      axis([x(1),x(2),y(1),y(2)])
    else
      axis([x(2),x(1),y(1),y(2)])
    end
  else
    if x(1)<x(2)
      axis([x(1),x(2),y(2),y(1)])
    else
      axis([x(2),x(1),y(2),y(1)])
    end					% end if
  end					% end if
					% pretty the graph
  hold on
  plot(ctd_time,-ctd_depth,'ro');grid; title(which);
  plot([x(1), x(2)], [0, 0], 'g-.'); 
  hold off;
					% find specific end point
  disp('click on end of cast')
  [x,y]=ginput(1);
                                        % add the points to cast_id array
  u=[u;x,y];			
					% are we done yet?
  axis('auto');
  plot(ctd_time,-ctd_depth,'.');grid; title(which);
  hold on;
  %d=ceil(u(:,1))
  d=[];
  for id=1:size(u,1),
    dd=find(ctd_time<u(id,1));
    d=[d; dd(end)];
  end



ctd_depth = ctd_depth(d(1):d(2)); 
ctd_time = ctd_time(d(1):d(2)); 
CTD30s_D = ctd_depth;%interp1(nmini30s_T,nmini30s_D,ctd_time); %gets a depth dimention relative to 8Hz ctd time (same dimension as ctd data)
CTD_dat = CTD_dat(d(1):d(2),:);    

    ACS_D = interp1(ctd_time,ctd_depth,.001*ACS_dat(:,1));
    BB9_D = interp1(ctd_time,ctd_depth,BB9_time);
    CTD_dat(:,2) = ctd_depth;%CTD30s_D; %repair CTD depth column in data
    
    
    %=========Trim size of all data array to interpolation==========
    
    ACS_dat(isnan(ACS_D),:) = [];
    BB9_data(isnan(BB9_D),:) = [];
    BB9_D(isnan(BB9_D)) =[];
    ACS_D(isnan(ACS_D)) =[];
end

LST_D = interp1(ctd_time,ctd_depth,JD); %size of JD

data_lst(isnan(LST_D),:) = [];
LST_D(isnan(LST_D)) = [];
LISST_new = [data_lst(:,1:32),.01*LST_D];



% % pause

%filter out bad bb data=======
%=============================
%badbb = find(bb440 > (mean(bb440)+3*std(bb440)));


% % for t = 2:(size(BB_new,2))
% %     badbb = find(abs(BB_new(:,t)) > (mean(BB_new(:,t)) + 3*std(BB_new(:,t))));
% %
% % ctd_temp(badbb) = [];
% % ctd_depth(badbb) = [];
% % ctd_sal(badbb) = [];
% % ctd_time(badbb) = [];
% % ctd_cond(badbb) = [];
% % %bb440(badbb) = [];
% % BB_new(badbb,:) =[];
% % CTD30s_D(badbb,:) =[];
% % CTD_dat(badbb,:) =[];
% % end
%filter out bad ACS =========
%============================

%TempC(badbb) = [];
%Depth(badbb) = [];
%Sal(badbb) = [];
% ACSC442(badC2,:) = [];
% ACS_dat(badC2,:) =[];
% ACS_D(badC2,:) =[];

for t = 2:(size(ACS_dat,2) -5)
    badC2 = find(ACS_dat(:,t) > 2);
    ACS_dat(badC2,:) =[];
    ACS_D(badC2,:) =[];
end

% for t = 2:(size(ACS_dat,2) -5)
%     badC = find(abs(ACS_dat(:,t)) > (mean(ACS_dat(:,t)) + 3*std(ACS_dat(:,t))));
%     ACS_dat(badC,:) =[];
%     ACS_D(badC,:) =[];
% end

%===filter bad lisst==============
%==============================


for t = 1:32
    badC = find(abs(LISST_new(:,t)) > (mean(LISST_new(:,t)) + 3*std(LISST_new(:,t))));
    LISST_new(badC,:) =[];
    LST_D(badC,:) =[];
end
sm_LISST = smooth(LISST_new(:,:)); %smoothed lisst data
SMLST_D = smooth(LST_D); %smoothed lisst depth
%=============================

%===========================================
%==========================================================================
%==================LISST====================



% % % % % plot(smooth(ctd_temp),smooth(-CTD30s_D))
% % % % % title('ctd temp ctd depth')
% % % % % figure
% % % % % plot(ctd_time,-CTD30s_D)
% % % % % title('ctd time ctd depth')
% % % % % figure
% % % % % plot(smooth(nmini30s_Temp),smooth(-nmini30s_D))
% % % % % title('mini temp mini depth')
% % % % % figure
% % % % % plot(smooth(ctd_sal),smooth(-CTD30s_D))
% % % % % title('ctd sal ctd depth')
% % % % % figure
% % % % % plot(LISST_new(:,9),-LST_D) %moved below filtering section and changed data_lst to LISST_new
% % % % % title('interpolated lisst depth and original data')
% % % % % figure
% % % % % plot(data_lst(:,9),-lst_depth)
% % % % % title('original lisst depth and original data')
% % % % % figure
% % % % % plot(LISST_new_mn(:,9),-nmini30s_D)
% % % % % title('interpolated lisst data and minilogger depth')
% % % % % keyboard
% % % % %
% % % % % for i = 2:size(BB_new,2)
% % % % %     plot(BB_new(:,i),-CTD30s_D);
% % % % %     axis([0 .01 -110 0])
% % % % %     tline = strcat('bbchan',num2str(i));
% % % % %     title(tline)
% % % % %     figure
% % % % %
% % % % % end
% % % % % %
% % % % % for i = 1:size(ACS_dat,2)
% % % % %     plot(smooth(ACS_dat(:,i)),smooth(-ACS_D));
% % % % %     axis([-.05 .1 -110 0])
% % % % %     tline = strcat('ACSchan',num2str(i));
% % % % %     title(tline)
% % % % %     figure
% % % % %
% % % % % end
% % % % % for i = 1:size(LISST_new,2)
% % % % %     plot(smooth(LISST_new(:,i)),-SMLST_D);
% % % % %     tline = strcat('SMLISSTchan',num2str(i));
% % % % %     title(tline)
% % % % %     figure
% % % % %
% % % % % end
% % % % % pause
% % % % % for i = 1:size(LISST_new,2)
% % % % %     plot(LISST_new(:,i),-LST_D);
% % % % %     tline = strcat('LISSTchan',num2str(i));
% % % % %     title(tline)
% % % % %     figure
% % % % %
% % % % % end

% % JD_new = interp1(JD,ctd_time);

%==============End LISST===================



%===========================================
%===========================================
%=========BB9 profile plotting==============
h2 = figure;
q = jet(9);
sepparation_index =  find(BB9_D == max(BB9_D)); %where to split cast
sep_ind = sepparation_index(1);
% % % maxd = find(BB9_D == max(BB9_D));
% % % t = maxd(1);
% % % while t>1;%t = maxd:-1:1  %cut out 20m downcast
% % %     if abs(BB9_D(t-15)) > abs(BB9_D(t)) && BB9_D(t) < 20 %make sure not a false min
% % %         % plot(depth(t:end),data(t:end,1))
% % %         break
% % %     end
% % %     t = t -1;
% % % end

%BB9_downcast = smooth(BB_new(1:sep_ind,:));
%BB9_downcast(blw0,i) = NaN;

subplot(1,4,1)
set(gca, 'ColorOrder', q)
hold on
%plot(BB_new(:,4),-CTD30s_D,'o-');
scatter(smooth(BB9_data(1:sep_ind,4)),smooth(-BB9_D(1:sep_ind)),3)
xlabel('bbp (1/m)')
ylabel('Depth (m)')
title('412');
legend(strcat('mean ',num2str(mean(BB9_data(1:sep_ind,4)))))
axis([-.001 0.002 -Inf 0])

subplot(1,4,2)
set(gca, 'ColorOrder', q)
hold on
%plot(BB_new(:,8),-CTD30s_D,'o-');
scatter(smooth(BB9_data(1:sep_ind,8)),smooth(-BB9_D(1:sep_ind)),3)
xlabel('bbp (1/m)')
%ylabel('Depth (m)')
title('440');

legend(strcat('mean ',num2str(mean(BB9_data(1:sep_ind,8)))))
axis([0 0.002 -Inf 0])

subplot(1,4,3)
set(gca, 'ColorOrder', q)
hold on
%plot(BB_new(:,24),-CTD30s_D,'o-');
scatter(smooth(BB9_data(1:sep_ind,12)),smooth(-BB9_D(1:sep_ind)),3)
xlabel('bbp 1/m ')
% ylabel('Depth (m)')
title('488');
legend(strcat('mean ',num2str(mean(BB9_data(1:sep_ind,12)))))
axis([0 0.002 -Inf 0])

subplot(1,4,4)
set(gca, 'ColorOrder', q)
hold on
%plot(BB_new(:,32),-CTD30s_D);
scatter(smooth(BB9_data(1:sep_ind,16)),smooth(-BB9_D(1:sep_ind)),3)
xlabel('bbp (1/m)')
legend(strcat('mean ',num2str(mean(BB9_data(1:sep_ind,16)))))
% ylabel('Depth (m)')
title('510');
axis([0 .002 -Inf 0]) %was .95
suptitle(strcat('BB9 Particle Backscatter',file(end-8:end)));
saveas(gcf, ['BBprofile412to510',file(end-7:end)], 'png')
%====================================
figure
subplot(1,5,1)
set(gca, 'ColorOrder', q)
hold on
%plot(BB_new(:,4),-CTD30s_D,'o-');
scatter(smooth(BB9_data(1:sep_ind,20)),smooth(-BB9_D(1:sep_ind)),3)
refline(0,mean(BB9_data(1:sep_ind,20)))
xlabel('bbp (1/m)')
ylabel('Depth (m)')
title('532');
axis([-.001 0.002 -Inf 0])

subplot(1,5,2)
set(gca, 'ColorOrder', q)
hold on
%plot(BB_new(:,8),-CTD30s_D,'o-');
scatter(smooth(BB9_data(1:sep_ind,24)),smooth(-BB9_D(1:sep_ind)),3)
xlabel('bbp (1/m)')
%ylabel('Depth (m)')
title('595');
axis([0 0.002 -Inf 0])

subplot(1,5,3)
set(gca, 'ColorOrder', q)
hold on
%plot(BB_new(:,24),-CTD30s_D,'o-');
scatter(smooth(BB9_data(1:sep_ind,28)),smooth(-BB9_D(1:sep_ind)),3)
xlabel('bbp 1/m ')
% ylabel('Depth (m)')
title('660');
axis([0 0.002 -Inf 0])

subplot(1,5,4)
set(gca, 'ColorOrder', q)
hold on
%plot(BB_new(:,32),-CTD30s_D);
scatter(smooth(BB9_data(1:sep_ind,32)),smooth(-BB9_D(1:sep_ind)),3)
xlabel('bbp (1/m)')
% ylabel('Depth (m)')
title('676');
axis([0 .002 -Inf 0]) %was .95
suptitle(strcat('BB9 Particle Backscatter',file(end-8:end)));

subplot(1,5,5)
set(gca, 'ColorOrder', q)
hold on
%plot(BB_new(:,32),-CTD30s_D);

scatter(smooth(BB9_data(1:sep_ind,36)),smooth(-BB9_D(1:sep_ind)),3)
xlabel('bbp (1/m)')
% ylabel('Depth (m)')
title('715');
axis([0 .002 -inf 0]) %was .95
suptitle(strcat('BB9 Particle Backscatter',file(end-8:end)));
saveas(gcf, ['BBprofile532to715',file(end-7:end)], 'png')



% % %
% % %
% % % %=======BB9 spectra plotting=========
% % % sepparation_index =  find(CTD30s_D == max(CTD30s_D)); %where to split cast
% % % sep_ind = sepparation_index(1);
% % %     BB9_downcast = smooth(BB_new(1:sep_ind,:));
% % %
% % %     for  i= 1:size(BB9_downcast,2)
% % %      blw0 = find(BB9_downcast(:,i) < 0);
% % %      if ~isempty(blw0);
% % %      BB9_downcast(blw0,i) = NaN;
% % %      end
% % %     %BB9_downcast(find(BB9_downcast(:,i) < 0),i) = 'NAN'
% % %     end
% % %     %BB9_downcast < 0 == 'NAN'
% % %
% % %     m35_index = find(abs(CTD30s_D(1:sep_ind) -35) == min(abs(CTD30s_D(1:sep_ind) - 35)))
% % %     m50_index = find(abs(CTD30s_D(1:sep_ind) -50) == min(abs(CTD30s_D(1:sep_ind) - 50)))
% % %     m80_index = find(abs(CTD30s_D(1:sep_ind) -80) == min(abs(CTD30s_D(1:sep_ind) - 80)))
% % %     m95_index = find(abs(CTD30s_D(1:sep_ind) -95) == min(abs(CTD30s_D(1:sep_ind) - 95)))



bwindow =5;
[binned,stats,stdev,medi]=databinner(BB9_D(1:sep_ind),BB9_data(1:sep_ind,:),bwindow)

header_std = strcat([' stdbbp','412 '],{' '},['stdbbp','440 '],{' '},['stdbbp','488 '],{' '},['stdbbp','510 '],{' '},['stdbbp','532 '],{' '},['stdbbp','595 '],{' '},['stdbbp','660 '],{' '},['stdbbp','676 '],{' '},['stdbbp','715']);
header_b = strcat(['binbbp','412 '],{' '},['binbbp','440 '],{' '},['binbbp','488 '],{' '},['binbbp','510 '],{' '},['binbbp','532 '],{' '},['binbbp','595 '],{' '},['binbbp','660 '],{' '},['binbbp','676 '],{' '},['binbbp','715']);
header_med = strcat([' median','412 '],{' '},['median','440 '],{' '},['median','488 '],{' '},['median','510 '],{' '},['median','532 '],{' '},['median','595 '],{' '},['median','660 '],{' '},['medianbbp','676 '],{' '},['medianbbp','715']);
header_stat = strcat({' '},['pts'],{' '},['meandepth'])
total_hdr = char(strcat(header_b,header_std,header_med,header_stat));
total_dat = [binned,stdev,medi,stats];

stid = fopen([file(end-7:end),'BB9stats'],'w+');
fprintf(stid,'%s\n',total_hdr);
dlmwrite([file(end-7:end),'BB9stats'],total_dat, '-append','delimiter',' ')



%figure
% for pt = 1:length(stats)
%    figure
%     plot(412:33.6667:715,binned(pt,4:4:36))%,412:33.6667:715,binned(pt,8),412:33.6667:715,binned(pt,12),412:33.6667:715,binned(pt,16),412:33.6667:715,binned(pt,20),412:33.6667:715,binned(pt,24),412:33.6667:715,binned(pt,28),412:33.6667:715,binned(pt,32),412:33.6667:715,binned(pt,36))
%     dph = strcat('depth',num2str(stats(pt,2)));
%     title(['BB9 spectra',file(end-7:end)])
%     ylabel('bbp data 1/m')
%     xlabel('412-715nm');
%     legend(strcat('depth',num2str(dph)))
%     hold on
%
%
%
% end

clear binned stats stdev medi


%=======end BB9 plotting=============




%=====================================
%=====================================
%========CTD plotting=================
%just downcast after 20m debubble
maxd = find(CTD30s_D == max(CTD30s_D));
t=1;
% % % t = maxd(1);
% % % while t>1;%t = maxd:-1:1  %cut out 20m downcast
% % %     if abs(CTD30s_D(t-15)) > abs(CTD30s_D(t)) && CTD30s_D(t) < 20 %make sure not a false min
% % %         break
% % %     end
% % %     t = t -1;
% % % end
sepparation_index =  find(CTD30s_D == max(CTD30s_D)); %where to split cast
sep_ind = sepparation_index(1);
ctd_temp = ctd_temp(1:sep_ind);
CTD30s_Df = CTD30s_D(1:sep_ind);
ctd_sal = ctd_sal(1:sep_ind);
ctd_time = ctd_time(1:sep_ind);
ctd_cond = ctd_cond(1:sep_ind);

%plot temp, sal, cond
h2 = figure;
q = jet(8);

subplot(1,3,1)
set(gca, 'ColorOrder', q)
hold on
%plot(ctd_temp(t:sep_ind),-CTD30s_Df(t:sep_ind),'o-');
scatter(ctd_temp(t:sep_ind),-CTD30s_Df(t:sep_ind),3);
xlabel('Temp (C)')
ylabel('Depth (m)')
title('CTD');
axis([15 35 -Inf 0])

subplot(1,3,2)
set(gca, 'ColorOrder', q)
hold on
%plot(smooth(ctd_sal(t:sep_ind)),-CTD30s_Df(t:sep_ind),'o-');
scatter(smooth(ctd_sal(t:sep_ind)),-CTD30s_Df(t:sep_ind),3);
xlabel('Sal (PSU)')
%ylabel('Depth (m)')
title('CTD');
axis([34 38 -Inf 0])

subplot(1,3,3)
set(gca, 'ColorOrder', q)
hold on

%plot(ctd_cond(t:sep_ind),-CTD30s_Df(t:sep_ind),'o-');
scatter(ctd_cond(t:sep_ind),-CTD30s_Df(t:sep_ind),3);
xlabel('Conductivity')
%ylabel('Depth (m)')
title('CTD');
axis([4 7 -Inf 0])
suptitle(strcat('SBE25 Output',file(end-8:end)));
saveas(gcf, ['CTD',file(end-7:end)], 'png')
%========end CTD plotting===============
%=======================================
%=======================================

bwindow =5;
disp('doing CTD binning')
[binned,stats,stdev,medi]=databinner(CTD30s_Df(1:sep_ind),CTD_dat(1:sep_ind,:),bwindow)

header_std = strcat([' std','time(s)'],{' '},['std','Depth(m) '],{' '},['std','Temp(C) '],{' '},['std','Cond '],{' '},['std','Sal(PSU) ']);
header_b = strcat(['bin','time(s) '],{' '},['bin','Depth(m) '],{' '},['bin','Temp(C) '],{' '},['bin','Cond '],{' '},['bin','Sal(PSU) ']);
header_med = strcat([' median','time(s) '],{' '},['median','Depth(m) '],{' '},['median','Temp(C) '],{' '},['median','Cond '],{' '},['median','Sal(PSU) ']);
header_stat = strcat({' '},['pts'],{' '},['meandepth'])
total_hdr = char(strcat(header_b,header_std,header_med,header_stat));
total_dat = [binned,stdev,medi,stats];
stid = fopen([file(end-7:end),'CTDstats'],'w+');
fprintf(stid,'%s\n',total_hdr);
dlmwrite([file(end-7:end),'CTDstats'],total_dat, '-append','delimiter',' ')




%===============================
%===============================
%===============================
%====ACS spectra plotting=======

disp('FIX ACS CHANNELS IN SCRIPT TO CONTINUE WITH ACS PROC, THEN COMMENT THIS return LINE')
continue

sepparation_index =  find(ACS_D == max(ACS_D)); %where to split cast
sep_ind = sepparation_index(1);
ACS_downcast = (ACS_dat(1:sep_ind,:));
ACS_D = smooth(ACS_D(1:sep_ind,:));

Tref = 22.6 %(from factory cal)
header_b = ['Time(ms)  c400.9	c404.1	c407.3	c410.3	c413.7	c417.1	c420.5	c424.2	c427.9	c431.7	c435.2	c438.6	c442.5	c446.3	c450.0	c454.3	c458.1	c462.2	c466.2	c470.5	c474.5	c478.5	c482.7	c486.7	c490.6	c494.6	c498.6	c502.4	c506.6	c510.7	c514.7	c518.9	c522.7	c526.8	c530.8	c534.6	c538.4	c542.3	c546.1	c549.9	c553.6	c557.4	c561.0	c564.7	c568.1	c571.3	c574.7	c577.8	c581.2	c584.6	c588.3	c591.8	c595.2	c598.9	c602.6	c606.3	c610.2	c613.9	c617.7	c621.5	c625.2	c629.2	c632.9	c636.8	c640.7	c644.3	c648.4	c652.4	c656.0	c660.0	c663.9	c667.8	c671.3	c674.9	c679.1	c682.7	c686.3	c689.7	c693.4	c697.0	c700.5	c703.9	c707.1	c710.7	c714.2	c717.4	c720.8	c724.1	c727.5	c730.5	c734.2	a402.6	a405.9	a408.9	a411.9	a415.3	a418.7	a422.4	a426.0	a429.5	a433.3	a436.8	a440.3	a444.0	a447.9	a452.0	a455.9	a459.9	a463.8	a468.2	a472.2	a476.4	a480.4	a484.5	a488.5	a492.3	a496.1	a500.1	a504.1	a508.3	a512.5	a516.5	a520.3	a524.4	a528.2	a532.3	a536.1	a539.7	a543.9	a547.6	a551.4	a555.2	a558.7	a562.8	a566.2	a569.0	a572.2	a575.6	a579.0	a582.4	a585.8	a589.3	a592.7	a596.4	a599.9	a603.6	a607.3	a611.3	a615.0	a618.9	a622.7	a626.4	a630.2	a634.0	a638.0	a641.8	a645.6	a649.6	a653.5	a657.5	a661.1	a665.0	a668.8	a672.6	a676.3	a680.1	a683.8	a687.1	a690.8	a694.7	a698.1	a701.5	a705.0	a708.4	a711.8	a715.2	a718.5	a721.7	a725.1	a728.4	a731.8	a735.0	iTemp(C)	Conduct	Depth	XTemp	Salinity'];
legendvals = regexp(header_b,'\s','split')
channels = legendvals(2:end-5); %generating a list of ACS channels for TScal
for t2 = 2:length(channels)
    var = char(channels(t2))
    var  = var(2:end);
    chanlist(t2,:) = str2num(var)
end
TScorrected = ACS_TScal(ACS_downcast,Tref,ctd_time,ctd_sal,chanlist);%doing temperature and salinity correction.
keyboard

ACS_newdncast = [ACS_downcast(:,1),TScorrected,ACS_downcast(:,end-4:end)];


bwindow =5;
disp('doing ACS binning')
[binned,stats,stdev,medi]=databinner(ACS_D(1:sep_ind),ACS_newdncast,bwindow)
%[binned,stats,stdev,medi]=databinner(ACS_D(1:sep_ind),ACS_downcast,bwindow)
for i=1:length(legendvals)
    header_std{i} = char([' std',legendvals{i}])';
end
header_std = char(header_std)';
for i=1:length(legendvals)
    header_med{i} = char([' median',legendvals{i}])';
end
header_med = char(header_med)';

%header_std = strcat([' stdbbp','412 '],{' '},['stdbbp','440 '],{' '},['stdbbp','488 '],{' '},['stdbbp','510 '],{' '},['stdbbp','532 '],{' '},['stdbbp','595 '],{' '},['stdbbp','660 '],{' '},['stdbbp','676 '],{' '},['stdbbp','715']);
%header_b = strcat(['binACS','412 '],{' '},['binACS','440 '],{' '},['binACS','488 '],{' '},['binACS','510 '],{' '},['binACS','532 '],{' '},['binACS','595 '],{' '},['binACS','660 '],{' '},['binACS','676 '],{' '},['binACS','715']);
%header_med = strcat([' median','412 '],{' '},['median','440 '],{' '},['median','488 '],{' '},['median','510 '],{' '},['median','532 '],{' '},['median','595 '],{' '},['median','660 '],{' '},['medianbbp','676 '],{' '},['medianbbp','715']);
header_stat = strcat({' '},['pts'],{' '},['meandepth'])
total_hdr = char(strcat(header_b,header_std(1,:),header_med,header_stat));
total_dat = [binned,stdev,medi,stats];
stid = fopen([file(end-7:end),'ACSstats'],'w+');
fprintf(stid,'%s\n',total_hdr);
%dlmwrite([file(end-7:end),'ACSstats'],total_dat, '-append','delimiter',' ')


m35_index = find(abs(ACS_D(1:sep_ind) -35) == min(abs(ACS_D(1:sep_ind) - 35)))
m50_index = find(abs(ACS_D(1:sep_ind) -50) == min(abs(ACS_D(1:sep_ind) - 50)))
m80_index = find(abs(ACS_D(1:sep_ind) -80) == min(abs(ACS_D(1:sep_ind) - 80)))
m95_index = find(abs(ACS_D(1:sep_ind) -95) == min(abs(ACS_D(1:sep_ind) - 95)))

figure
plot(400:3.8202:740,ACS_downcast(m35_index(1),2:91),400:3.8202:740,ACS_downcast(m50_index(1),2:91),400:3.8202:740,ACS_downcast(m80_index(1),2:91),400:3.8202:740,ACS_downcast(m95_index(1),2:91))%,400:3.8202:740,avg_spec_001May2012unfilb(:,2:91),    400:3.8202:740,avg_spec_001May2012filc(:,2:91),400:3.8202:740,avg_spec_001May2012unfilc(:,2:91))
title(['ACS 001 spectra',file(end-7:end)])
xlabel('400-740nm');
ylabel('C data 1/m')
legend('35m','50m','80m','95m')%,'May2012-unfilter','May2012-newfilter','May2012-newunfilter','May2012-newfilter runII','May2012-newunfilter runII')
%saveas(gcf, ['ACS001_C',file(end-7:end)], 'png')

figure
plot(400:3.7363:740,ACS_downcast(m35_index(1),92:182),400:3.7363:740,ACS_downcast(m50_index(1),92:182),400:3.7363:740,ACS_downcast(m80_index(1),92:182),400:3.7363:740,ACS_downcast(m95_index(1),92:182))%,400:3.7363:740,avg_spec_001May2012unfilb(:,92:182),    400:3.7363:740,avg_spec_001May2012filc(:,92:182),400:3.7363:740,avg_spec_001May2012unfilc(:,92:182))
title(['ACS 001 spectra',file(end-7:end)])
xlabel('400-740nm');
ylabel('A data 1/m')
legend('35m','50m','80m','95m')%'May2012-unfilter','May2012-newfilter','May2012-newunfilter','May2012-newfilter runII','May2012-newunfilter runII')
%saveas(gcf, ['ACS001_A',file(end-7:end)], 'png')


%TS corrected ACS data

figure

plot(400:3.8202:740,ACS_newdncast(m35_index(1),2:91),400:3.8202:740,ACS_newdncast(m50_index(1),2:91),400:3.8202:740,ACS_newdncast(m80_index(1),2:91),400:3.8202:740,ACS_newdncast(m95_index(1),2:91))%,400:3.8202:740,avg_spec_001May2012unfilb(:,2:91),    400:3.8202:740,avg_spec_001May2012filc(:,2:91),400:3.8202:740,avg_spec_001May2012unfilc(:,2:91))
title(['ACS 001 TScor spectra',file(end-7:end)])
xlabel('400-740nm');
ylabel('C data 1/m')
legend('35m','50m','80m','95m')%,'May2012-unfilter','May2012-newfilter','May2012-newunfilter','May2012-newfilter runII','May2012-newunfilter runII')
%saveas(gcf, ['ACS001TSCOR_C',file(end-7:end)], 'png')

figure
plot(400:3.7363:740,ACS_newdncast(m35_index(1),92:182),400:3.7363:740,ACS_newdncast(m50_index(1),92:182),400:3.7363:740,ACS_newdncast(m80_index(1),92:182),400:3.7363:740,ACS_newdncast(m95_index(1),92:182))%,400:3.7363:740,avg_spec_001May2012unfilb(:,92:182),    400:3.7363:740,avg_spec_001May2012filc(:,92:182),400:3.7363:740,avg_spec_001May2012unfilc(:,92:182))
title(['ACS 001 TScor spectra',file(end-7:end)])
xlabel('400-740nm');
ylabel('A data 1/m')
legend('35m','50m','80m','95m')%'May2012-unfilter','May2012-newfilter','May2012-newunfilter','May2012-newfilter runII','May2012-newunfilter runII')
%saveas(gcf, ['ACS001TSCOR_A',file(end-7:end)], 'png')


%========end ACS plotting=============





%====ACS profile plotting======
% % %
% % %
% % %
% % %     h3 = figure;
% % %     q = jet(4);
% % %
% % %     subplot(1,4,1)
% % %     set(gca, 'ColorOrder', q)
% % %     hold on
% % %     %plot(BB_new(:,4),-CTD30s_D,'o-');
% % %     scatter(smooth(ACS_downcast(:,6)),-ACS_D,3)
% % %     xlabel('c (1/m)')
% % %     ylabel('Depth (m)')
% % %     title('413.7');
% % %     axis([-.1 0.1 -Inf Inf])
% % %
% % %     subplot(1,4,2)
% % %     set(gca, 'ColorOrder', q)
% % %     hold on
% % %     %plot(BB_new(:,8),-CTD30s_D,'o-');
% % %     scatter(smooth(ACS_downcast(:,14)),-ACS_D,3)
% % %     xlabel('c (1/m)')
% % %     %ylabel('Depth (m)')
% % %     title('442.5');
% % %     axis([0 0.1 -Inf Inf])
% % %
% % %     subplot(1,4,3)
% % %     set(gca, 'ColorOrder', q)
% % %     hold on
% % %     %plot(BB_new(:,24),-CTD30s_D,'o-');
% % %     scatter(smooth(ACS_downcast(:,54)),-ACS_D,3)
% % %     xlabel('c 1/m ')
% % %    % ylabel('Depth (m)')
% % %     title('595.2');
% % %     axis([0 0.1 -Inf Inf])
% % %
% % %     subplot(1,4,4)
% % %     set(gca, 'ColorOrder', q)
% % %     hold on
% % %     %plot(BB_new(:,32),-CTD30s_D,'o-');
% % %     scatter(smooth(ACS_downcast(:,75)),-ACS_D,3)
% % %     xlabel('c (1/m)')
% % %    % ylabel('Depth (m)')
% % %     title('674.9');
% % %     axis([0 .1 -Inf Inf]) %was .95
% % %     suptitle(strcat('ACS attenuation',file(end-8:end)));
% % %     saveas(gcf, ['ACSCprofile',file(end-7:end)], 'png')
% % %
% % %  h3 = figure;
% % %     q = jet(4);
% % %
% % %     subplot(1,4,1)
% % %     set(gca, 'ColorOrder', q)
% % %     hold on
% % %     %plot(BB_new(:,4),-CTD30s_D,'o-');
% % %     scatter(smooth(ACS_downcast(:,96)),-ACS_D,3)
% % %     xlabel('a (1/m)')
% % %     ylabel('Depth (m)')
% % %     title('411.9');
% % %     axis([-.1 0.1 -Inf Inf])
% % %
% % %     subplot(1,4,2)
% % %     set(gca, 'ColorOrder', q)
% % %     hold on
% % %     %plot(BB_new(:,8),-CTD30s_D,'o-');
% % %     scatter(smooth(ACS_downcast(:,105)),-ACS_D,3)
% % %     xlabel('a (1/m)')
% % %     %ylabel('Depth (m)')
% % %     title('444');
% % %     axis([0 0.1 -Inf Inf])
% % %
% % %     subplot(1,4,3)
% % %     set(gca, 'ColorOrder', q)
% % %     hold on
% % %     %plot(BB_new(:,24),-CTD30s_D,'o-');
% % %     scatter(smooth(ACS_downcast(:,145)),-ACS_D,3)
% % %     xlabel('a 1/m ')
% % %    % ylabel('Depth (m)')
% % %     title('596.4');
% % %     axis([0 0.1 -Inf Inf])
% % %
% % %     subplot(1,4,4)
% % %     set(gca, 'ColorOrder', q)
% % %     hold on
% % %     %plot(BB_new(:,32),-CTD30s_D,'o-');
% % %     scatter(smooth(ACS_downcast(:,166)),-ACS_D,3)
% % %     xlabel('a (1/m)')
% % %    % ylabel('Depth (m)')
% % %     title('676.3');
% % %     axis([0 .1 -Inf Inf]) %was .95
% % %     suptitle(strcat('ACS absorption',file(end-8:end)));
% % %     saveas(gcf, ['ACSAprofile',file(end-7:end)], 'png')

%======= FINDING/FILTERING NAN IF TIME RECORDS GO OVER =====
nn= isnan(BB_new);
non= find(nn(:,1) ==1);
CTD_dat(non,:) = [];
BB_new(non,:) = [];



return