%disp('opencamp3r.m')
% Opens Channel #1 Oak Ridge, K-Gill, Lyman-alpha, Psychrometric sensors
% Inputs data from ASCII data files

%  Note:  hhh=hour
z=zeros(8,72000);
clear yy;clear yz;
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];

%disp('Open CO2 and H2O File')
%dfl=['/home/jeh/fastex/P1_',dfx,';1'];       %H2O and CO2 channel
dfl=['D:\hp_data\day',num2str(ddd),'\P1_',dfx];       

flist=fopen(dfl,'r');
headr1=fgetl(flist);%disp(headr)
headr=fgetl(flist);                    % get rid of first line of data .... 
z=fscanf(flist,'%g%*c%g%*c%g,%g,%g,%g,%g,%g',[8,inf]);
%                 1   2    3  4  5  6  7  8         
z=z(:,1:length(z)-1);
t1=z(1,:)-z(1,1);t2=z(2,:)/10000-z(2,1)/10000;
tcamp=t1+t2;clear t1 t2;
if length(z(3,:)) > 65535,
   specsize=65536;
elseif length(z(3,:)) > 32767,
   specsize=32768;   
elseif length(z(3,:)) > 16383,
   specsize=16384;   
elseif length(z(3,:)) > 8191,
   specsize=8192;   
elseif length(z(3,:)) > 4095,
   specsize=4096;   
elseif length(z(3,:)) > 2047,
   specsize=2048;   
elseif length(z(3,:)) > 1023,
   specsize=1024;   
elseif length(z(3,:)) > 511,
   specsize=512;   
elseif length(z(3,:)) > 255,
   specsize=256;   
elseif length(z(3,:)) > 127,
   specsize=128;   
elseif length(z(3,:)) > 63,
   specsize=64;   
elseif length(z(3,:)) > 31,
   specsize=32;   
elseif length(z(3,:)) > 15,
   specsize=16;   
end;

yy(1,:)=z(4,:)*1000;
yy(2,:)=z(3,:)*1000;
yy(3,:)=z(5,:)*1000;
yy(4,:)=z(6,:)*1000;
yz=licorcalc(yy);
LI_co2=yz(:,1);
LI_h2o=yz(:,2);%specific humidity
%H2O=1.145.*(z(8,:).^2)+1.888.*z(8,:)+0.43;    %  [g/m3]
ao=-.0025;bo=0.236;co=0.065;                       %CSIRO calibration forms
H2O=-bo/2/ao*(1-sqrt(1+4*(z(8,:)-co)*ao/bo^2));    %  [g/m3], unit 12 cal coeff 2/99
%CO2=96.9.*z(7,:)+441;                             % [mg/m3]
CO2=(z(7,:)+8.6)/.01817;                           % [mg/m3]  unit 11 cal
CO2=CO2/1.85.*(1+1.61*H2O/1000/1.15)+80;	   %factor 1.85 converts mg/m^3 to ppm 

CO2=CO2(1:length(tcamp));
H2O=H2O(1:length(tcamp));

%ss=['save /home/user1/hare/rain/r',dfx,'.dat rain -ascii'];%disp(ss);
%eval(ss);clear ss mm ii;

%[Q,Qs,RH]=q_gkg(Tdry,Twet);      %Tdry and Twet can be corrected for response
                                  % time with the speed_T function (Serhad Atakturk)

if abs(CO2(1))>999,
   CO2(1)=0;
end;
if abs(H2O(1))>99,
   H2O(1)=0;
end;

for jj=2:length(tcamp);
   if abs(CO2(jj))>999,
      CO2(jj)=CO2(jj-1);
   end;
   if abs(H2O(jj))>99,
      H2O(jj)=H2O(jj-1);
   end;
end;
P=1010;T=300;qml=mean(LI_h2o);
rhoa=P*100/T/287/(1+.61*qml/1000);
figure;plot(tcamp,H2O,tcamp,LI_h2o*rhoa);title(['JD',dfx1,' Hr',dfx2,' UTC  Oak Ridge & LICOR H2O']);ylabel('Water Vapor Denisty (g/m^3)');xlabel('time in sec');
figure;plot(tcamp,CO2,tcamp,LI_co2);title(['JD',dfx1,' Hr',dfx2,' UTC  Oak Ridge & LICOR CO2']);ylabel('CO2 Molar Ratio (ppm)');xlabel('time in sec');

%figure;plot(tcamp,Tt);title(['JD',dfx1,' Hr',dfx2,' UTC  Oak Ridge T']);ylabel('degrees');xlabel('time in sec')
%figure;plot(tcamp,KGillTop,tcamp,KGillBot);title(['JD',dfx1,' Hr',dfx2,' UTC  K-Gill']);ylabel('wind speed in m/s');xlabel('time in sec')
%figure;plot(tcamp,KGillAzu);title(['JD',dfx1,' Hr',dfx2,' UTC  K-Gill Azimuth']);ylabel('degrees');xlabel('time in sec')
%figure;plot(tcamp,lyman);title(['JD',dfx1,' Hr',dfx2,' UTC  Lyman-alpha']);ylabel('lyman-alpha Volts');xlabel('time in sec')
%figure;plot(tcamp,Tdry,tcamp,Twet);title(['JD',dfx1,' Hr',dfx2,' UTC  Tdry/Twet']);ylabel('degrees');xlabel('time in sec')
%figure;plot(1:length(rain),rain);title(['JD',dfx1,' Hr',dfx2,' UTC  Rainfall in mm/hr']);ylabel('millimeters per hour');xlabel('time in min')

%ts=(0:specsize/2-1);ts=1/((specsize/2-1)./10)+ts*(10-1/((specsize/2-1)./10));ts=ts';
DD=20;hm=mean(H2O);cm=mean(CO2);hml=mean(LI_h2o);cml=mean(LI_co2);
Ch2o=psd(H2O-hm,specsize);[Ch1,FF]=specsmooth(Ch2o,DD);clear ch2o;
Cco=psd(CO2-cm,specsize);[Cc1,FF]=specsmooth(Cco,DD);clear cco;
Ch2l=psd((LI_h2o-hml)*rhoa,specsize);[Chl1,FF]=specsmooth(Ch2l,DD);clear ch2l;
Ccol=psd(LI_co2-cml,specsize);[Ccl1,FF]=specsmooth(Ccol,DD);clear ccol

LI_co2=LI_co2';
LI_h2o=LI_h2o';

figure;loglog(FF,Cc1.*FF,FF,Ccl1.*FF);title(['JD',dfx1,' Hr',dfx2,' UTC  Spectrum Oak Ridge & LICOR CO2']);ylabel('Spectral Density F*S(co2)');xlabel('freq in Hz')
figure;loglog(FF,Ch1.*FF,FF,Chl1.*FF);title(['JD',dfx1,' Hr',dfx2,' UTC  Spectrum Oak Ridge & LICOR H2O']);ylabel('F*S(h2o)');xlabel('freq in Hz')


clear z yz yy tcamp specsize rhoa qml jj hml hm headr1 Cc1 Ccl1 Cco Cco1 Ch2l Ch2o
clear headr flist dfx1 dfx2 dfl dfx cml co cm ao bo T P ans FF DD Chl1 Ccol Ch1