disp('read_ozone_neaqs_AMMA.m')


    jd=num2str(ddd);
    if  ddd<100;
        jd=['0' num2str(ddd)];	
    end;

    hr=num2str(hhh);
    if hhh<10;
       hr=['0' num2str(hhh)];	
    end;

        fid=fopen([way_rawdata, '\AMMA\day',jd,'\fos008',jd,hr ,'_raw.txt']);
        disp(['fos008', jd,hr]);

        %dump=fgetl(fid);       
        if fid<0
            ozone_data=zeros(36000,5);
        end;
        
        if fid>0,	   				%if the file exists,
            while feof(fid)==0,
            
        
                try
                     temp =textscan(fid,'%2f%2f%3f %f %f %*[^\n]','delimiter', ',', 'headerlines', 1,  'emptyvalue', NaN,'treatAsEmpty', 'Sonic');
                     ozone_data=[cell2mat(temp)];
                catch
%         ozone_data=fscanf(fid, '%f %f %f', [3,inf]);   
%         ozone_data=ozone_data';
                end;
            end;
        end;
        
        ozone_data(:,any(isnan(ozone_data),1))=[]; %remove lines containing NaNs (bad lines, too long...)

    
        ozonelength=length(ozone_data)
        if length(ozone_data)<35000
         ozone_data=zeros(36000,5);
        end
        
        timeO3=ozone_data(:,1)*60+ozone_data(:,2)+ozone_data(:,3)/1000; %real raw time in s
        slowO3=ozone_data(:,4);
        fastO3_raw=ozone_data(:,5);

        if timeO3(1)==0 & timeO3(2)==0
           timeO3=0:0.1:3599.9;
           timeO3=timeO3';
           [test, m, n] =unique(timeO3);
        end;
        
        ii=find(isnan(fastO3_raw));
        fastO3_raw(ii)=fastO3_raw(ii-1);

 %%%%%%%%%%%%%%%%%% Filter %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                      
        fastO3=O3LPfilter(fastO3_raw);
           
                     
                           
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Polyfit %%%%%%%%%%%%%%%%%%%%     
%         mean_1=mean(fastO3)
%         
%         p = polyfit(timeO3,fastO3,26);
%         covEpol = polyval(p,timeO3);
%         
% %         figure;plot(timeO3,fastO3,'.',timeO3,covEpol,'r-');title('fastO3_raw and covEpol')
% %         figure;plot(timeO3,(fastO3-covEpol+mean(covEpol)),'.');title('fastO3-covEpol+mean(covEpol)')
%         fastO3=(fastO3-covEpol+mean(covEpol));
% %         figure;plot(timeO3,fastO3,'.');title('fastO3_corrected')
%         mean_2=mean(fastO3)
% %         
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        
        ii=find(isnan(fastO3));
        fastO3(ii)=fastO3(ii-1);
        
        ozonelength=length(fastO3)

        if length(fastO3)<=35990
           [test, m, n] =unique(timeO3);
           fastO3_real=interp1(timeO3(m),fastO3(m),tref,'spline');fastO3_real=fastO3_real';ii=find(~isnan(fastO3_real));msu=mean(fastO3_real(ii));ii=find(isnan(fastO3_real));fastO3_real(ii)=msu; 
        end   
        if length(fastO3)<36000 & length(fastO3)> 35990
            ii=length(fastO3):36000;
            fastO3_real=fastO3;
            fastO3_real(ii)=fastO3(length(fastO3));
        end
        if length(fastO3)>36000
           fastO3_real=fastO3(1:36000);
        end;
        if length(fastO3)==36000
           fastO3_real=fastO3;
        end;
        ii=find(isnan(fastO3_real));
        fastO3_real(ii)=fastO3_real(ii-1);
               
        
        
        if length(slowO3)<=35990
           [test, m, n] =unique(timeO3);
           slowO3_real=interp1(timeO3(m),slowO3(m),tref,'spline');slowO3_real=slowO3_real';ii=find(~isnan(slowO3_real));msu=mean(slowO3_real(ii));ii=find(isnan(slowO3_real));slowO3_real(ii)=msu; 
        end   
        if length(slowO3)<36000 & length(slowO3)> 35990
            ii=length(slowO3):36000;
            slowO3_real=slowO3;
            slowO3_real(ii)=slowO3(length(slowO3));
        end
        if length(slowO3)>36000
           slowO3_real=slowO3(1:36000);
        end;
        if length(slowO3)==36000
           slowO3_real=fastO3;
        end;
        ii=find(isnan(slowO3_real));
        slowO3_real(ii)=slowO3_real(ii-1);
         
        
        

%%%%%%%%%  file hhh=oo always starts late by 6-7s. Fix the interpolation issues at beginning of hhh 00 %%%%%%%%%%%%%%%%%%%%%%
if hhh==00
    t1=floor(timeO3(1)*10);
    fastO3_real(1:t1+5)=fastO3_real(t1+10);
    slowO3_real(1:t1+5)=slowO3_real(t1+10);
end; 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

