% WHOTS 2009 :: 2009-07-11 :: LB
% Read 1-minute data processed by the Campbell Scientific
% data logger MEAN1.
% Reads Sea Snake, T/RH from Vaisala, ORG and airflow

jd_mean1=zeros(1,1440)+NaN;
% jd_mean1=ddd+(0:1439)/60/24;
mean1=zeros(26,1440)+NaN;
stx=[];
jax1=1;

for jam=0:23,					    %cycle thru 24 hourly stats files
    hr=sprintf('%02i',jam);   
    e=[way_raw_data_flux, yyyy(3:4),jd,'\me10' 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                 %27                          
%               temp =textscan(flist,'%2f%2f%3f %f/%f/%f %*7c %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %*3c %*[^\n]', 'delimiter', ', ', 'headerlines', 1,  'emptyvalue', NaN);
                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', 'delimiter', ',', 'headerlines', 1,  'emptyvalue', NaN);
                stx=cell2mat(temp)';
%                 stx(4:79,any(isnan(stx),1))=NaN;      %put NaN into any rows containing NaNs (to remove bad lines, gap...), except JD
            catch
                % if one line is cut short, blank it out totally
                [l8,c8]=size(temp{1,26});
                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,26}=temp{1,26}';
                end;
                stx=[stx cell2mat(temp)'];
            end; %end try/catch 
                
        end;  %end while ~feof(flist)      
                
        [nr1,nl1]=size(stx)
        mean1(:,jax1:jax1+nl1-1)=stx;
        jd1=ddd + (jam+(stx(1,:)+(stx(2,:)+stx(3,:)/1000)/60)/60)/24;
%                 jd_mean1 = [jd_mean1 jd1];
        jd_mean1(:,jax1:jax1+nl1-1)=jd1;
        jax1=jax1+nl1;
    else
          disp(sprintf('\b<--File not found.'))
    end;                            %end if flist
end;                                %end for jam
if length(mean1)<1440,
    mean1=[mean1 ones(nr1,1440-length(mean1))*NaN];
    jd_mean1=[jd_mean1 ones(1,1440-length(jd_mean1))*NaN];
elseif length(mean1)>1440,
    mean1=mean1(:,1:1440);
    jd_mean1=jd_mean1(:,1:1440);
end