function invert_all(all_min_time,all_min_data)

% change to folder containing cruise of interest
cd ../
hdir = cd;

mdir   = ([hdir '\mfiles\']); 
figpath = [hdir '\figures\'];
datapath = [hdir '\mat_files\'];
calpath  = [hdir '\instrument_files\'];

ringareafile = [calpath 'ringarea_1285.asc']; 
dias         = load([calpath 'Dias32_b.asc']);
fact_zsc     = load([calpath 'factory_zsc_1285.asc']);

%% variables
    inversion_type = 1; %input('Which inversion would you like to use?  1=sphere, 2=random, 3=n 1.17 ... ');
    VCC = 45744;    % from InstrumentData.txt, changes with factory calibration
    flref = 1164.50; % from element 36 in factory_zsc file
    x = 1.25; %lower limit for first size range for type B, spherical particles
% get diameters (select for spheres or randomly shaped particles)
    rho=200^(1/32);
    bins(:,1) = x*rho.^([0:31]); %lower limit
    bins(:,2) = x*rho.^([1:32]); % upper limit
    bins(:,3) = sqrt(bins(:,1).*bins(:,2)); %mid-point
    dias = bins(:,3)';
    class_vol = (1/6)*pi*(dias.^3); %um^3, equivalent 4/3PIr^3 and 1/6PId^3
    class_vol = class_vol*1e-9; %ul, 10^9 um3 in a ul 
    binwidth = bins(:,2) - bins(:,1);
    clear bins rho x
              
%% process data

 % matlab date 
      sdate = datenum('1/0/2020') + all_min_time; % time in GMT hopefully
      dvec = datevec(sdate);
          
%  % plot dvec and data to check timing of fsw; correct for outliers
%     figure         
%     plot(dvec(:,5),all_min_data(:,20),'.')
%           
%  fsw comes in 0-10; time looks right with some outliers
%  filter oout bad fsw sections
    f=find(dvec(:,5)<9 & dvec(:,5)>1);
    for i=1:32
        b=find(all_min_data(f,i)>nanmean(all_min_data(f,i)) + nanstd(all_min_data(f,i)));
        plot(all_min_time(f),all_min_data(f,i),'.'); hold on
        plot(all_min_time(f(b)),all_min_data(f(b),i),'r.');
        pause
        clf
        avg_min_data(f(b),i) = NaN;
    end
    cd(mdir)
  % perform inversion for iterative sections of data using 1-10min fsw  
        vd(1:size(all_min_data,1),1:32)= NaN;
        zscfile=[cd '\zsc.asc'];
        logfile=[cd '\rdata.log'];  
                      
        for mo= nanmin(dvec(:,2)):nanmax(dvec(:,2)) %month. 
            mm = find(dvec(:,2)==mo);
        for d=nanmin(dvec(mm,3)):nanmax(dvec(mm,3)) %days
            for h=0:23
              whole_seg = find(dvec(:,2)==mo & dvec(:,3)==d & dvec(:,4)==h &  dvec(:,5)>11);
              fsw_seg = find(dvec(:,2)==mo & dvec(:,3)==d & dvec(:,4)==h &  dvec(:,5)<9 & dvec(:,5)>1);
               if (isempty(fsw_seg) || size(fsw_seg,1)==1)
                       fsw_seg = find(dvec(:,2)==mo & dvec(:,3)==d & dvec(:,4)==(h+1)& dvec(:,5) >1 & dvec(:,5)<9 );
               end
                if (isempty(whole_seg)==0)
                    fsw=nanmean(all_min_data(fsw_seg,:));
                    part=all_min_data(whole_seg,:);
                 save([cd,'\zsc.asc'],'fsw','-ascii');
            save([cd,'\rdata.log'],'part','-ascii');
           [scat,tau,zsc,data_out,cscat] = getscat(logfile,zscfile,1,ringareafile); %all data corrected for recent fsw
            % perform scattering inversion to get uncorrected volume distribution
                disp(['Inverting -HOT321: mo =' num2str(mo) ' d=' num2str(d) ' h=' num2str(h) ]);
               [vd_out, dias] = invert(cscat,2,0,0,0,0,1,15); % spherical w 15 iterations
        
                  vd(whole_seg,:) = vd_out;
                  tau_all(whole_seg,:) = tau;
                 clear vd_out
                end
            end
        end
        end
      
        
      % get corrected vd for all 
          lref = all_min_data(:,36); % get laser ref for VD correction
          vdc = vdcorr(vd,VCC,flref,lref); % correct for concentration and laser power
              
          clear fact_zsc fsw fsw_avg factory* fsw* data1 data2 data3 data_out
          clear fact_zsc flref h i inversion_type mo  ring* VCC whole* d fsw* zscfile lref pfile
          clear h mm mo psdp psdf r flref ans zsc bd logfile mm part cscat scat tau
     
              
     % calc normalized particle concentration (#/L/um) and #/L from volume concentration in uL/L
        psd = vdc./(repmat(class_vol,size(vdc,1),1));  % #/L
        psdn=psd./repmat(binwidth',length(vdc),1); %#/L/um
          
        cd(datapath) 
     
%% calculate size classes 
     % ul/L       
        VD_sm  = sum(vdc(:,1:3),2);   VD_sm(VD_sm<0)= NaN;% total particle volume, 1.25-2 um
        VD_mid = sum(vdc(:,4:17),2);  VD_mid(VD_mid<0)= NaN; % total particle volume, 2-20um
        VD_lg  = sum(vdc(:,18:27),2); VD_lg(VD_lg<0)= NaN; % total particle volume, 20-100 um
        VD_sum = sum(vdc(:,1:27),2);  VD_sum(VD_sum<0)= NaN;
             
    % #/L and correct for outliers       
        nLsum =     sum(psd(:,1:27),2);
     % Class carbon conversion factor (multiply for class abundance)
    class_vol = (1/6)*pi*(dias.^3); %um^3, equivalent 4/3PIr^3 and 1/6PId^3
    class_mdl = 0.2163*(class_vol).^0.939; %in pg C/cell Mender-Deuer and Lessard 2000
    
    lb      = size(psd,1);
    Cmd     = psd.*repmat(class_mdl,lb,1)*10^-6; % #/L* pg C/cell * 10^-6 = mg/m3
    Cmd_uM  = Cmd/12; % umol/L
    Cmd_sum = sum(Cmd_uM(:,1:27),2); Cmd_sum(Cmd_sum<0)= NaN; % umol/L
    Cmd_sm  = sum(Cmd_uM(:,1:3),2);     Cmd_sm(Cmd_sm<0)= NaN;% umol/L % total C, 1.25-2 um
    Cmd_med = sum(Cmd_uM(:,4:17),2);    Cmd_med(Cmd_med<0)= NaN;% umol/L % total C, 2-20 um
    Cmd_lg =  sum(Cmd_uM(:,18:27),2);   Cmd_lg(Cmd_lg<0)= NaN;% umol/L % total C, 20-100 um
    
    %PSD FIT
    ydat = log(psd(:,5:27)); %2.63-109.24
    xdat = log(dias(5:27)./dias(5));
    for k=1:size(ydat,1);
          param(k,:) = polyfit(xdat,ydat(k,:),1);
    end
% excise bad fit
    bad = find(param(:,1)<-4);
        Cmd_med(bad)=NaN;
        Cmd_sm(bad)=NaN;
        Cmd_lg(bad)=NaN;
        psd(bad,:)=NaN;
        psdn(bad,:)=NaN;
        sdate(bad,:)=NaN;
 

     save('HOT321_PROC.mat','*')
    
  
  end
  