% PCO2_UWAY_CORRECT.M
%declare a tracking variable
lag_table=load('Sofex_LagTable.txt');

for i = [7:12 14:24 26:42]     %These are Julian days - Jan 1 = 1 (Duh!). Change this for the days you want to process.
    
    in_file = sprintf('pco2_2002%03d_merge.txt', i);     %open the merged pCO2 input file
    fprintf('starting julian day %d\n', i);
    pco2_num = load(in_file);
    out_file = sprintf('pco2_2002%03d_corrected.txt', i);     %Open the output file
    f_out = fopen(out_file, 'wt');
    % Write the header    
    %fprintf(f_out, '%%pco2_time [sdn],pco2_time [jday],co2_raw [mV],licor_temp [C],xh2o [mmol/mol],pressure [kPa],gas_flow [cc/min],water_flow [l/min],sample_mode,no_of_samples,licor_errors,ad_errors,ship_time [sdn],air_temp [C],air_pressure [hPa],q_short [W/m2],rel_hum [percent],rel_wind_speed [m/s],rel_wind_dir [deg],true_wind_speed [m/s],true_wind_dir [deg],sig_t [kg/m3],water_flow [gpm],oxygen [ml/l],flr [volts],PAR [W/m2],lat [deg],lon [deg],cog [deg],sog [kn],drifter_lat [deg],drifter_lon [deg],x_pos [m],y_pos [m],tsg_sal [psu],Phosphate(false),Silicate(false),water_temp [C],Pressure(1),tsg_temp [C],Pressure(2),Alkalinity(false),xco2 [umol/mol]\n');
    
    [y,m,d,h,mi,s] = datevec(pco2_num(:,2));
    x = find(mi == 4 & s >=30);     % Find all 'air' samples, but only those from the second half of each minute when pressure reading has stabilized
    pressure_correct = pco2_num(:,7)./interp1(pco2_num(x,2), pco2_num(x,7), pco2_num(:,2));
    y = find(pco2_num(:,2) > max(pco2_num(x,2)));
    pressure_correct(y) = pco2_num(y,7)/pco2_num(max(x),7);
    micon = mi;
    bad_sec = find(round(s)==60); %find xx:00:5x that may be final zero;
    micon(bad_sec) = micon(bad_sec)+1;
    scon = s;
    scon(bad_sec) = 0;
    xy=find(micon==1 & round(scon)==0);%Find all final zero cycle numbers
    zero_correct = pco2_num(xy,4);
    z = find(mi >= 6);     % Find all 'water' samples, but only those from 6 to 60 minutes past the hour (minutes 0 through 4 are stands etc, 5th minute is 'contaminated')
    pco2_num(z,4) = pco2_num(z,4).*pressure_correct(z);
    for subi=1:length(xy) %loop through and remove zeroes (top of hour)
        if subi==length(xy)
        subind=find(pco2_num(:,2)>pco2_num(xy(subi),2));
        else
        subind=find(pco2_num(:,2)>pco2_num(xy(subi),2)&pco2_num(:,2)<pco2_num(xy(subi+1),2));
        end;
        
        
        pco2_num(subind,4)=pco2_num(subind,4)-abs(zero_correct(subi));
    end;
    
    for i=1:length(pco2_num(:,2))
        time_ind(i)=near(lag_table(:,4),pco2_num(i,2),1);
        tsg_ind(i)=near(pco2_num(:,2),(pco2_num(i,2)-lag_table(time_ind(i),5)),1);
    end;
    
    pco2_numX(:,1:14)=pco2_num(:,1:14);
    pco2_numX(:,15:28)=pco2_num(tsg_ind,15:28);
    pco2_numX(:,29)=pco2_num(:,29);
    pco2_numX(:,30:38)=pco2_num(tsg_ind,30:38);
    pco2_num=pco2_numX;
    
    temp_bad=find(pco2_num(:,2)>=datenum('04-Feb-2002 06:57:00')&pco2_num(:,2)<=datenum('07-Feb-2002 11:55:00'));
    
    if ~isempty(temp_bad)
        pco2_num(temp_bad,10)=pco2_num(temp_bad,24)+2.343;
    end;
        
    % hard-code regions of known bad data
    bad = find(mi < 6);
    pco2_num(bad,4) = NaN;
    bad = find(pco2_num(:,9) < 0.5 | pco2_num(:,9) > 1.5);
    pco2_num(bad,4) = NaN;
    bad = find(pco2_num(:,4) < 330 | pco2_num(:,4) >430);
    pco2_num(bad,4) = NaN;
    
    bad_start = ['07-Jan-2002 04:00';
        '07-Jan-2002 07:40';
        '09-Jan-2002 14:00';
        '11-Jan-2002 06:18';
        '11-Jan-2002 14:40';
        '11-Jan-2002 17:20';
        '11-Jan-2002 18:00';
        '15-Jan-2002 12:10';
        '17-Jan-2002 17:15';
        '18-Jan-2002 08:49'; 
        '19-Jan-2002 05:00'; 
        '19-Jan-2002 09:16'; 
        '19-Jan-2002 14:20'; 
        '20-Jan-2002 01:50'; 
        '20-Jan-2002 10:00'; 
        '20-Jan-2002 17:30'; 
        '21-Jan-2002 00:00'; 
        '21-Jan-2002 11:25'; 
        '22-Jan-2002 02:30'; 
        '22-Jan-2002 08:06'; 
        '23-Jan-2002 16:18'; 
        '24-Jan-2002 00:23'; 
        '24-Jan-2002 03:56';
        '26-Jan-2002 00:00';
        '28-Jan-2002 03:35';
        '28-Jan-2002 05:00';
        '28-Jan-2002 15:00';
        '30-Jan-2002 11:13';
        '30-Jan-2002 16:26';
        '30-Jan-2002 19:05';
        '31-Jan-2002 01:29';
        '01-Feb-2002 04:50';
        '02-Feb-2002 12:40';
        '04-Feb-2002 06:30';
        '05-Feb-2002 21:00';];
    
    bad_end = ['07-Jan-2002 04:28';
        '07-Jan-2002 08:00';
        '09-Jan-2002 14:15';
        '11-Jan-2002 07:00';
        '11-Jan-2002 15:40';
        '11-Jan-2002 17:40';
        '11-Jan-2002 19:35';
        '15-Jan-2002 12:40';
        '17-Jan-2002 17:35';
        '18-Jan-2002 11:45'; 
        '19-Jan-2002 05:25'; 
        '19-Jan-2002 11:50'; 
        '19-Jan-2002 14:42'; 
        '20-Jan-2002 02:30'; 
        '20-Jan-2002 14:50'; 
        '20-Jan-2002 24:00'; 
        '21-Jan-2002 00:30'; 
        '21-Jan-2002 12:43'; 
        '22-Jan-2002 03:10'; 
        '22-Jan-2002 10:18'; 
        '23-Jan-2002 24:00'; 
        '24-Jan-2002 00:49'; 
        '24-Jan-2002 04:28';
        '26-Jan-2002 03:30';
        '28-Jan-2002 04:20';
        '28-Jan-2002 05:45';
        '28-Jan-2002 15:45';
        '30-Jan-2002 14:00';
        '30-Jan-2002 18:05';
        '30-Jan-2002 21:15';
        '31-Jan-2002 05:33';
        '01-Feb-2002 13:05';
        '02-Feb-2002 16:15';
        '04-Feb-2002 10:10';
        '06-Feb-2002 01:00';];
    
    for k = 1:size(bad_start,1)
        bad = find(pco2_num(:,2) >= datenum(bad_start(k,:)) & pco2_num(:,2) <= datenum(bad_end(k,:)));
        pco2_num(bad,4) = NaN;
    end
    
    
    good = find(~isnan(pco2_num(:,4)));
    for k = 1:length(good)
        fprintf(f_out, '%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,0,0,%f,0,%f,0,2300,%6.2f\n', ...
            pco2_num(good(k),2),pco2_num(good(k),1),pco2_num(good(k),3),pco2_num(good(k),5:9),pco2_num(good(k),11:23),pco2_num(good(k),26:38),pco2_num(good(k),25),pco2_num(good(k),10),pco2_num(good(k),24),pco2_num(good(k),4));
    end
    clear pco2_num; clear date_str; clear pressure_correct; clear good; clear bad; clear x; clear y; clear z;clear tsg_ind;clear time_ind;clear pco2_numX;
    fclose(f_out);
end
% pco2_num = [];
% 
% 
% for i = 1:length(infiles)    %These are Julian days - Jan 1 = 1 (Duh!). Change this for the days you want to process.
%     in_file = sprintf('pco2_2002%03d_correct2.txt', infiles(i));     %open the pCO2 input file
%     data = load(in_file);
%     pco2_num = [pco2_num; data];
%     clear data;
% end
% 
% 
% 
% outfile = sprintf('pco2_2002_corrected.txt');%file to run through CO2.exe
% fid=fopen(outfile,'wt');
% 
% 
% for i=1:length(pco2_num(:,15))
%     
% 
% 
% for i=1:length(pco2_num(:,15))
% fprintf(fid,'%f,%f,%f,%f,%f,0.0,0.0,%f,0,%f,0,2300,%f\n',pco2_num(i,15),pco2_num(i,29),pco2_num(i,31),pco2_num(i,32),pco2_num(i,25),pco2_num(timeind(i),10),pco2_num(i,24),pco2_num(timeind(i),4));
% %time,fluorometer,lat,long,salinity,Ph(false),Si(false),Water Temp at Licor,Pressure,Water Temp at Inlet,Pressure,Alkalinity(false),raw pco2(fully corrected);
% end;
% 
% fclose(fid);
% 
% 
