disp('opncmpn.m')
% Opens Channel #1 Oak Ridge, K-Gill, Lyman-alpha, Psychrometric sensors
% 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];

%disp('Open CO2 and H2O File')
dfl=['e:\DAY',dfx(1:3),'\P1_',dfx];       %H2O and CO2 channel

flist=fopen(dfl,'r');
headr=fgetl(flist);%disp(headr)
headr=fgetl(flist)                    % get rid of first line of data .... 
z=fscanf(flist,'%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g',[12,inf]);
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;

H2O=1.145.*(z(3,:).^2)+1.888.*z(3,:)+0.43;    %  [g/m3]
%disp([num2str(ddd),'   ',num2str(hhh),'  h2o ',num2str(std(H2O))])
CO2=96.9.*z(4,:)+441;                         % [mg/m3]
Tt=z(5,:).*28.1-8;
CO2=CO2(1:length(tcamp));
H2O=H2O(1:length(tcamp));
Tt=Tt(1:length(tcamp));
KGillTop=z(6,:)*13;
KGillBot=z(7,:)*13;                   %Wind speed = (sqrt (KGillBot.^2 + KGillTop.^2) ) / 0.83
KGillAzu=(z(8,:)-2.5)*71;         %0 deg is from bow ... 90 deg is from starboard 
%lyman=z(9,:);
Tdry=z(10,:)*10+15.5;              %ORIGINAL ELECTRONICS REPLACED JD006
Twet=z(11,:)*10+9.5;


%Tdry2=speed_T(Tdry,U,20,10);
%Twet2=speed_T(Twet,U,20,10);

% NEW CALIBRATION  01-08-97
%Tdry=z(10,:).*9.8371+14.9147;
%Twet=z(11,:).*9.4295+11.3186;
%Rain=z(13,:);Rain=100.*(Rain.^2);
%for yy=1:length(Rain),if Rain(yy)<0.07, Rain(yy)=0;end;end;

%if length(Rain) > 71000,
%   rain=(1:60).*0;
%   mm=1:1200;
%   for pp=1:60;
%      ii=mm+1200*(pp-1);
%      if pp==60;
%         ii=length(Rain)-1200+1:length(Rain);
%      end;
%      rain(pp)=median(Rain(ii));
%   end;   
% rain=rain';
%end;

%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)
%Tref=z(12,:)/0.0021-273.15;

if abs(Tt(1))>99,
   Tt(1)=0;
end;
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(Tt(jj))>9,
      Tt(jj)=Tt(jj-1);
   end;
   if abs(CO2(jj))>999,
      CO2(jj)=CO2(jj-1);
   end;
   if abs(H2O(jj))>99,
      H2O(jj)=H2O(jj-1);
   end;
end;

%[Q,Qs,RH]=q_gkg(Tdry,Twet);
%Q=Q.*1.3;
%disp([num2str(ddd),'   ',num2str(hhh),'  q ',num2str(std(Q))])

%ts=(0:specsize/2-1);ts=1/((specsize/2-1)./10)+ts*(10-1/((specsize/2-1)./10));ts=ts';

%[Ch,Fh]=psd(H2O,specsize,20);
%[Ch,Fh]=specsmoo(Ch,20);clear Ch2o
%Clyman=psd(lyman,specsize);[Cl,FF]=specsmoo(Clyman,20);clear Clyman
%[Ca,Fa]=psd(abshum,specsize,20);
%[Ca,Fa]=specsmoo(Ca,20);clear Cabshum
%Cqp=psd(Q,specsize);[Cq,Fq]=specsmoo(Cqp,20);clear Cqp

%Ch=Ch*2./20;
%Cl=Cl*2./20;
%Ca=Ca*2./20;
%Cq=Cq*2./20;

clear Cc Cc1 Ct Ct1 Tt Ch1 Ckt Ckt1 Ckb Ckb1 Cly Cly1 Ctd Ctd1 Ctw Ctw1 Ctr Ctr1
clear Cco Ch2 Ctt ans dfl dfx dfx2 flist headr jj specsize ts z dfx1 KGillAzu
