%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');
R1Pg1=[];R2Pg1=[];THPg1=[];R1Pg2=[];R2Pg2=[];THPg2=[];THPr1=[];THPr2=[];
RS=[];TA=[];RH=[];org=[];clear jad;
jax=1;

if flist>0,   %if the file exists, 
   dmp=fgets(flist);%read header lines and skip blanks
	clear st2;	
   while feof(flist)==0,%go until you run out of 10-min sections
      clear st1;clear stz;
      st0=fscanf(flist,'%g%*c%g%*c%g',[3,1]);
      [n0 m0]=size(st0);if n0==0;break;end;
      if st0(3)==1
         stx=fscanf(flist,'%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%*c%g%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%*c%g%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%*c%g%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%*c%g%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%*c%g%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%*c%g%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%*c%g%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%*c%g%*c%g%g',[127,1]);
			stz(1:3)=st0;stz(4:length(stx)+3)=stx;st1=stz';
      else
        	sty=fscanf(flist,'%g%*c%g%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%*c%g%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%*c%g%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%*c%g%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%*c%g%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%*c%g%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%*c%g%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%*c%g%*c%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%*c%g%*c%g%g',[130,1]);
      		           %1     2   3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 1    2    3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 1     2   3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8  1    2   3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 1     2    3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 1    2   3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8  1    2   3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 1    2    3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 1    2    3 4   
        if length(sty)>3
             st1=sty;sty(1:3) 
        else
             break
        end;%end if length
     end;%end if st
     [nr1,nl1]=size(st1);
	   if nr1==130
      	st2(:,jax)=st1; 
      	jax=jax+1;
		else
   		break
      end;%end if             
   end;%end while
end;%end flist
THPr1=st2(62,:);
THPr2=st2(64,:);
org=st2(66,:);
TA=st2(52,:);
RH=st2(54,:);
RS=st2(50,:);
Tc1=st2(42,:);
Td1=st2(44,:);
Tc2=st2(46,:);
Td2=st2(48,:);
THPg1=st2(58,:);
THPg2=st2(60,:);
tradiom=st2(1,:)-st2(1,1);
np=jax-1;
jad=floor(tradiom/60);


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+273.15).^4)+3.5*sigma*((Td1+273.15).^4-(Tc1+273.15).^4);
LongIR2=THPg2+sigma*((Tc2+273.15).^4)+3.5*sigma*((Td2+273.15).^4-(Tc2+273.15).^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