disp('openophi.m')
% Inputs data from ASCII data files
%  Note:  hhh=hour
%Oclear2=[];
%Oabsorb2=[];

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=['/cdrom/DAY',dfx1,'/P4_',dfx,';1'];%disp(dfl)

dfl=[drive,'\DAY',dfx1,'\P4_',dfx];

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 ~isempty(z);
   z=z(:,1:length(z)-1);
   t1=z(1,:)-z(1,1);t2=z(2,:)/10000-z(2,1)/10000;
   Oclear=z(4,:);
   Oclear=medfilt1(Oclear,3);
   Oabsorb=z(3,:);
   Oabsorb=medfilt1(Oabsorb,3);
   tophir=t1+t2;clear t1 t2;
%   a=1.051*(1-0.0017*25);

Tcan=mean(ship_Tair)+10;

A0_ophir=1.045;     %UNIT # 89111 CALIBRATION  07/96  ASGAMAGE & FASTEX
A1_ophir=-0.00181;  %UNIT # 89111 CALIBRATION  07/96  ASGAMAGE & FASTEX
k_ophir=0.0507;     %UNIT # 89111 CALIBRATION  07/96  ASGAMAGE & FASTEX
b_ophir=.7278;      %UNIT # 89111 CALIBRATION  07/96  ASGAMAGE & FASTEX
a_ophir=A0_ophir*(1+A1_ophir*Tcan);

%   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(fix((2/3)*length(sort_Oabsorb)))-sort_Oabsorb(fix((1/3)*length(sort_Oabsorb))));
%      sig_Oclear=0.5*(sort_Oclear(fix((2/3)*length(sort_Oclear)))-sort_Oclear(fix((1/3)*length(sort_Oclear))));
%      if abs(Oclear_10(1)-med_Oclear) > 5*sig_Oclear,
%         Oclear_10(1)=med_Oclear;
%      end;
%      if abs(Oabsorb_10(1)-med_Oabsorb) > 5*sig_Oabsorb,
%         Oabsorb_10(1)=med_Oabsorb;
%      end;
%      for kk=2:length(Oclear_10);
%         if abs(Oclear_10(kk)-med_Oclear) > 5*sig_Oclear,
%            Oclear_10(kk)=Oclear_10(kk-1);
%         end;   
%         if abs(Oabsorb_10(kk)-med_Oabsorb) > 5*sig_Oabsorb,
%            Oabsorb_10(kk)=Oabsorb_10(kk-1);
%         end;
%      end;

      %rc=(Oabsorb_10+median(Oabsorb(ii)))./(Oclear_10+median(Oclear(ii)));

%      Ophir_stat_clear=std(Oclear_10);
%      Ophir_stat_absorb=std(Oabsorb_10);

%     disp(['Standard Deviation ',num2str(pp),'/12    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;  
%      DD=20;
%      Oclear2=[Oclear2 Oclear_10+median(Oclear(ii))];
%      Oabsorb2=[Oabsorb2 Oabsorb_10+median(Oabsorb(ii))];
%   end;
%   clear Oabsorb Oclear

x=(((mean(ship_Tair)+273.15)/273.15)^1.54);

rc=Oabsorb./Oclear;
for ii=1:length(rc);
   if rc(ii)==0,
      rc(ii)=0.01;
   end;
end;
abshum=x*((-log(rc/a_ophir))./k_ophir).^(1./b_ophir);   % !!!!!!!! units are g/m3 !!!!!!!
   
if ~isreal(abshum(1));abshum(1)=0;end;
for iii=2:length(abshum);
   if ~isreal(abshum(iii));
      abshum(iii)=abshum(iii-1);
   end;
end;
else,
   abshum=(1:72000).*0;
   Oabsorb=(1:72000).*0;
   Oclear=(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 dfl ans a Ophir_stat_clear Ophir_stat_absorb Oclear_10 Oabsorb_10
clear Ch1 FF DD A0_ophir A1_ophir Oabsorb2 Oclear2 Tcan a_ophir k_ophir b_ophir dfx dfx1 dfx2 iii tophir
fclose(flist);