% WHOTS 2012 :: 2009-07-11 :: LB
% Read 1-minute data processed by the Campbell Scientific
% data logger MEAN2.
% Reads PSP and PIRs (Eppley and K&Z) and BP

jd_mean2=zeros(1,1440)+NaN;
% jd_mean2=ddd+(0:1439)/60/24;
mean2=zeros(46,1440)+NaN;
stx=[];
jax1=1;
clear jd2


for jam=0:23,					    %cycle thru 24 hourly stats files
    hr=sprintf('%02i',jam);   
    e=[way_raw_data_flux, yyyy(3:4),jd,'\me20' yyyy(3:4) jd hr '_raw.txt'];
    disp(['Reading means file from hour ',hr]);
    flist=fopen(e,'r');
   
    if flist>0,					    %if the file exists, 
%         clear stx;
stx=[];
        while ~feof(flist)
          try
                                 %1                        %10                            %20                           %30                         %40 %41                   
          temp =textscan(flist,'%2f%2f%3f %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 %f %f %f %f %f %f %f %f %*[^\n]', 'delimiter', ', ', 'headerlines', 1,  'emptyvalue', NaN);
          stx=[stx cell2mat(temp)'];

          catch
            % if one line is cut short, blank it out totally
            [l8,c8]=size(temp{1,46});
            if l8==0 || c8==0; continue; end % read next batch of lines
            for ll=1:8
                if length(temp{1,ll})~=l8
                    temp{1,ll}(l8+1)=[];
                end;
            end;
            if isempty(temp{1,1});
                temp{1,46}=temp{1,46}';
            end;
            stx=[stx cell2mat(temp)'];
          end; %end try/catch 
                
        end;  %end while ~feof(flist)     
              
        [nr1,nl1]=size(stx);
        mean2(:,jax1:jax1+nl1-1)=stx;
        jd2=ddd + (jam+(stx(1,:)+(stx(2,:)+stx(3,:)/1000)/60)/60)/24;
%                 jd_mean2 = [jd_mean2 jd2];
        jd_mean2(:,jax1:jax1+nl1-1)=jd2;
        jax1=jax1+nl1;
    else
          disp(sprintf('\b<--File not found.'))
    end;                            %end if flist
end;                                %end for jam
if length(mean2)<1440,
    mean2=[mean2 ones(nr1,1440-length(mean2))*NaN];
    jd_mean2=[jd_mean2 ones(1,1440-length(jd_mean2))*NaN];
elseif length(mean2)>1440,
    mean2=mean2(:,1:1440);
    jd_mean2=jd_mean2(:,1:1440);
end
% meant=spline(jd_mean2, mean2,jd_mean1);