%disp('openradiom.m')
% Opens Channel #2 Radiometers and Mean Tair, Tsea, and RH 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 Radiometer and Means File')
%dfl=['/home/jeh/fastex/P2_',dfx,';1'];   
dfl=['D:\hp_data\P2_',dfx];       
    
%disp(dfl)
flist=fopen(dfl,'r');
headr=fgetl(flist);%disp(headr)
norman=1;
R1Pg1=[];R2Pg1=[];THPg1=[];R1Pg2=[];R2Pg2=[];THPg2=[];THPr1=[];THPr2=[];
RS=[];TA=[];RH=[];org=[];
while norman==1,
   headr=fgetl(flist);
   if headr==-1,norman=0;break;end;
   for qq=1:length(headr)-9;
      if headr(qq:qq+2)==' 19',
         R1_pyrg1=str2num(headr(qq+3:qq+9));
         R1Pg1=[R1Pg1 R1_pyrg1];                                     
      end;   
      if headr(qq:qq+2)==' 18',
         R2_pyrg1=str2num(headr(qq+3:qq+9));
         R2Pg1=[R2Pg1 R2_pyrg1];
      end;
      if headr(qq:qq+3)==',25+' | headr(qq:qq+3)==',25-',
         Th_pyrg1=str2num(headr(qq+3:qq+9));
         THPg1=[THPg1 Th_pyrg1];
      end;
      if headr(qq:qq+2)==' 21',
         R1_pyrg2=str2num(headr(qq+3:qq+9));
         R1Pg2=[R1Pg2 R1_pyrg2];
      end;   
      if headr(qq:qq+2)==' 20',
         R2_pyrg2=str2num(headr(qq+3:qq+9));
         R2Pg2=[R2Pg2 R2_pyrg2];
      end;
      if headr(qq:qq+2)==' 26',
         Th_pyrg2=str2num(headr(qq+3:qq+9));
         THPg2=[THPg2 Th_pyrg2];
      end;
      if headr(qq:qq+2)==' 27',
         Th_pyra1=str2num(headr(qq+3:qq+9));
         THPr1=[THPr1 Th_pyra1];
      end;   
      if headr(qq:qq+2)==' 28',
         Th_pyra2=str2num(headr(qq+3:qq+9));
         THPr2=[THPr2 Th_pyra2];
      end;
      if headr(qq:qq+2)==' 22',
         Rsea=str2num(headr(qq+3:qq+9));
         RS=[RS Rsea];
      end;   
      if headr(qq:qq+2)==' 23',
         Tairv=str2num(headr(qq+3:qq+9));
         TA=[TA Tairv];
      end;
      if headr(qq:qq+2)==' 24',
         RHv=str2num(headr(qq+3:qq+9));
         RH=[RH RHv];
      end;
	   if headr(qq:qq+2)==' 29',
         orgx=str2num(headr(qq+3:qq+9));
         org=[org orgx];
      end;
   
   end;
end;
Td1(1:length(R1Pg1))=R1Pg1;%3563./(2.74+log(R1Pg1));
Td2(1:length(R1Pg2))=R1Pg2;%3563./(2.74+log(R1Pg2));
Tc1(1:length(R2Pg1))=R2Pg1;%3563./(2.74+log(R2Pg1));
Tc2(1:length(R2Pg2))=R2Pg2;%3563./(2.74+log(R2Pg2));
if length(Tc1)>length(Td1),
   Tc1=Tc1(1:length(Td1));
end;   
if length(Tc2)>length(Td2),
   Tc2=Tc2(1:length(Td2));
end;   
if length(Td1)>length(Tc1),
   Td1=Td1(1:length(Tc1));
end;   
if length(Td2)>length(Tc2),
   Td2=Td2(1:length(Tc2));
end;   
if length(THPg1)>length(Tc1),
   THPg1=THPg1(1:length(Tc1));
end;   
if length(THPg2)>length(Tc2),
   THPg2=THPg2(1:length(Tc2));
end;   

sigma=5.67e-8;
clear Tairv RHv
Tairv=TA;
RHv=RH;
Tsea_snake=RS;%3915.9./(5.127+log(RS-10))-273.15;
Solar1=THPr1;%./(9.03e-3);
Solar2=THPr2;%./(9.13e-3);
LongIR1=THPg1+(sigma*(Tc1.^4))+3.5*sigma*((Tc1.^4)-(Td1.^4));
LongIR2=THPg2+(sigma*(Tc2.^4))+3.5*sigma*((Tc2.^4)-(Td2.^4));

%figure;plot(1:length(RHv),RHv,(1:length(ship_hum))./4,ship_hum);title(['JD',dfx1,' Hr',dfx2,' UTC  Vaisala(yel) & Ship(mag) RH%']);ylabel('humidity in %');xlabel('time in minutes')
figure;plot(1:length(RHv),RHv);title(['JD',dfx1,' Hr',dfx2,' UTC  Vaisala(yel) & Ship(mag) RH%']);ylabel('humidity in %');xlabel('time in minutes')
%figure;plot(1:length(Tairv),Tairv,(1:length(ship_Tair))./4,ship_Tair);title(['JD',dfx1,' Hr',dfx2,' UTC Vaisala(yel) & Ship(mag) Air Temp']);ylabel('temperature in deg C');xlabel('time in minutes')
figure;plot(1:length(Tairv),Tairv,1:length(Tsea_snake),Tsea_snake);title(['JD',dfx1,' Hr',dfx2,' UTC Vaisala(yel) Air & Sea Temp']);ylabel('temperature in deg C');xlabel('time in minutes')
%figure;plot(1:length(Tsea_snake),Tsea_snake,(1:length(ship_Tsea))./4,ship_Tsea);title(['JD',dfx1,' Hr',dfx2,' UTC Snake(yel) & Ship(mag) Sea Sfc Temp']);ylabel('sea sfc temp in deg C');xlabel('time in minutes')
%figure;plot(1:length(Solar1),Solar1,1:length(Solar2),Solar2);title(['JD',dfx1,' Hr',dfx2,' UTC  Solar Flux']);ylabel('solar flux in W/m/m');xlabel('time in minutes')
figure;plot(1:length(Solar1),Solar1,1:length(Solar2),Solar2);title(['JD',dfx1,' Hr',dfx2,' UTC  Solar Flux']);ylabel('solar flux in W/m/m');xlabel('time in minutes')
figure;plot(1:length(LongIR1),LongIR1,1:length(LongIR2),LongIR2);title(['JD',dfx1,' Hr',dfx2,' UTC  Longwave Flux']);ylabel('IR flux in W/m/m');xlabel('time in minutes')
figure;plot(1:length(org),org);title(['JD',dfx1,' Hr',dfx2,' UTC  Rainrate from ORG']);ylabel('Rainrate *mm/hr');xlabel('time in minutes')


clear sigma qq norman headr flist dfx dfl ans Th_pyrg2 Th_pyrg1 Th_pyra1 Th_pyra2
clear Rsea RH R2_pyrg2 R2_pyrg1 R1_pyrg2 R1_pyrg1 TA RS THPg1 THPg2 THPr1 THPr2
%clear Td1 Td2 Tc1 Tc2 R1Pg1 R1Pg2 R2Pg1 R2Pg2