%disp('openophir')
% Inputs data from ASCII data files
%  Note:  hhh=hour

fclose('all'); 
if hhh<10,
   dfx2=['0',num2str(hhh)];
else,
   dfx2=num2str(hhh);
end;

if ddd<10,
   dfx1=['00',num2str(ddd)];
elseif ddd<100,
   dfx1=['0',num2str(ddd)];
else,
   dfx1=num2str(ddd);
end;

dfx=[dfx1 dfx2];

%   Open OPHIR file

dfl=['/home/jeh/fastex/P4_',dfx,';1'];%disp(dfl)
flist=fopen(dfl,'r');
headr=fgetl(flist);%disp(headr)
headr=fgetl(flist);
%disp('     ###############     Ophir Evaluation     ###############     ')
z=fscanf(flist,'%g,%g,%g%g%g',[5,inf]);
if z~=[];
   z=z(:,1:length(z)-1);
   t1=z(1,:)-z(1,1);t2=z(2,:)/10000-z(2,1)/10000;
   Oclear=z(4,:);Oabsorb=z(3,:);
   tophir=t1+t2;clear t1 t2;
   a=1.051*(1-0.0017*25);
   x=((288.15/273.15)^0.8)*56.231;

   mm=1:12000;

   for pp=1:6;
      ii=mm+12000*(pp-1);
      if pp==6;
         ii=length(Oclear)-12000+1:length(Oclear);
      end;
      Oclear_10=detrend(Oclear(ii));Oabsorb_10=detrend(Oabsorb(ii));
      med_Oabsorb=median(Oabsorb_10);med_Oclear=median(Oclear_10);
      sort_Oabsorb=sort(Oabsorb_10);sort_Oclear=sort(Oclear_10);
      sig_Oabsorb=0.5*(sort_Oabsorb(med_Oabsorb+2/3*length(sort_Oabsorb))-sort_Oabsorb(med_Oabsorb+1/3*length(sort_Oabsorb)));
      sig_Oclear=0.5*(sort_Oclear(med_Oclear+2/3*length(sort_Oclear))-sort_Oclear(med_Oclear+1/3*length(sort_Oclear)));
      if abs(Oclear_10(1)-med_Oclear) > 7*sig_Oclear,
         Oclear_10(1)=med_Oclear;
      end;
      if abs(Oabsorb_10(1)-med_Oabsorb) > 7*sig_Oabsorb,
         Oabsorb_10(1)=med_Oabsorb;
      end;
      for kk=2:length(Oclear_10);
         if abs(Oclear_10(kk)-med_Oclear) > 7*sig_Oclear,
            Oclear_10(kk)=Oclear_10(kk-1);
         end;   
         if abs(Oabsorb_10(kk)-med_Oabsorb) > 7*sig_Oabsorb,
            Oabsorb_10(kk)=Oabsorb_10(kk-1);
         end;
      end;

      rc=(Oabsorb_10+median(Oabsorb(ii)))./(Oclear_10+median(Oclear(ii)));
      abshum=x*(-log(rc/a)).^1.362;

      Ophir_stat_clear=std(Oclear_10);
      Ophir_stat_absorb=std(Oabsorb_10);
      abshum_mean=mean(abshum);

     disp(['Standard Deviation ',num2str(pp),'/6    Clear = ',num2str(Ophir_stat_clear),'    Absorb = ',num2str(Ophir_stat_absorb),'   Mean abs hum = ',num2str(abshum_mean),'  [g/m/m/m]'])
     disp(['     Median Raw Counts    Clear = ',num2str(median(Oclear(ii))),'     Absorb = ',num2str(median(Oabsorb(ii)))])
     if pp==1,
        stdC_1=Ophir_stat_clear;
        stdA_1=Ophir_stat_absorb;
        medC_1=median(Oclear(ii));
        medA_1=median(Oabsorb(ii));
     elseif pp==2,
        stdC_2=Ophir_stat_clear;
        stdA_2=Ophir_stat_absorb;
        medC_2=median(Oclear(ii));
        medA_2=median(Oabsorb(ii));
     elseif pp==3,
        stdC_3=Ophir_stat_clear;
        stdA_3=Ophir_stat_absorb;
        medC_3=median(Oclear(ii));
        medA_3=median(Oabsorb(ii));
     elseif pp==4,
        stdC_4=Ophir_stat_clear;
        stdA_4=Ophir_stat_absorb;
        medC_4=median(Oclear(ii));
        medA_4=median(Oabsorb(ii));
     elseif pp==5,
        stdC_5=Ophir_stat_clear;
        stdA_5=Ophir_stat_absorb;
        medC_5=median(Oclear(ii));
        medA_5=median(Oabsorb(ii));
     elseif pp==6,
        stdC_6=Ophir_stat_clear;
        stdA_6=Ophir_stat_absorb;
        medC_6=median(Oclear(ii));
        medA_6=median(Oabsorb(ii));
     end;  
   %figure;plot(tophir(ii),Oclear_10+median(Oclear(ii)),tophir(ii),Oabsorb_10+median(Oabsorb(ii)));title(['JD',dfx1,' Hr',dfx2,' UTC  Ophir raw counts']);xlabel('time in sec');ylabel('counts');
 %  figure;plot(tophir(ii),abshum);title(['JD',dfx1,' Hr',dfx2,' UTC  Ophir abshum']);xlabel('time in sec');ylabel('abshum');
   %Ch=psd(abshum,8192);[Ch1,FF]=specsmooth(Ch);figure;loglog(FF,Ch1.*FF);title(['JD',dfx1,' Hr',dfx2,' UTC  Spectrum Ophir abshum']);xlabel('freq in Hz');ylabel('F*S(abshum)');
   end;
   rc=Oabsorb./Oclear;
   abshum=x*(-log(rc/a)).^1.362;

   if length(abshum) > length(su),
      abshum=abshum(1:length(su));
   end;
else,
   abshum=(1:72000).*0;
end;
clear z x sort_Oclear sort_Oabsorb sig_Oclear sig_Oabsorb rc pp mm med_Oclear med_Oabsorb kk ii headr
clear flist dfx dfl ans abshum_mean a Ophir_stat_clear Ophir_stat_absorb Oclear_10 Oabsorb_10
clear Ch Ch1 FF