data=[];
for i = [6: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);
    data=[data; pco2_num(:,[2 10 15 24])];
    clear pco2_num ;
end;

end_time=max(data(:,1));
now_time=data(1,1);
window=(now_time-3/1440);
lag=[];
di=[];
i=1;

while end_time>=now_time
    insttime_ind=find(data(:,1)>=window&data(:,1)<=now_time);
    ssttime_ind=find(data(:,3)>=(window-5/1440)&data(:,3)<=(now_time-1/1440));
    if ~isempty(ssttime_ind)
        max_insttemp=find(data(insttime_ind,2)==max(data(insttime_ind,2)));
        max_sst=find(data(ssttime_ind,4)==max(data(ssttime_ind,4)));
        min_insttemp=find(data(insttime_ind,2)==min(data(insttime_ind,2)));
        min_sst=find(data(ssttime_ind,4)==min(data(ssttime_ind,4)));
        max_diff=data(insttime_ind(max_insttemp(1)),1)-data(ssttime_ind(max_sst(1)),3);
        min_diff=data(insttime_ind(min_insttemp(1)),1)-data(ssttime_ind(min_sst(1)),3);
        di=[di;(max_diff+min_diff)/2];
        i=i+1;
        if i==4
            ll=length(lag)+1;
            lag(ll,1)=now_time;
            lag(ll,2)=mean(di);
            clear di;
            di=[];
            i=1;
        end;
        
        clear insttime_ind; clear ssttime_ind clear max_insttemp; clear min_insttemp; clear max_sst;clear max_diff; clear min_diff;
    end;
    now_time=now_time+(10/1440);
    window=(now_time-3/1440);
end;


    
    