disp('read_hp_day_ep')
fclose('all');

jd=num2str(ddd);
if ddd<100
jd=['0'num2str(ddd)];
end;
if ddd<10
jd=['00'num2str(ddd)];
end;

plotit=1;
prtit=1;
clear jad;
hr='00';							%input('gmt hr','s');
g=0.0098;						%adiabatic lapse rate
eps_w=0.97;						%emissivity of water
sig_sb=5.67e-8;				%Stefan Boltzmann constant
blk=1;							%10-min index 
jax=1;
backchk=0;
for jam=0:23,					%cycle thru 24 hourly stats files
   if jam<10,
      hr=['0'num2str(jam)];
   else
      hr=num2str(jam);
   end;
   
   e=['f:\day',jd,'\P2_' jd hr];
   disp(['Reading means file from hour ',num2str(jam)]);
   flist=fopen(e,'r');
   
   if flist>0,					%if the file exists, 
      if backchk==0,
         tzilch=jam;
         backchk=1;
      end;						%end if
      
      dmp=fgets(flist);		%read header lines and skip blanks
		
      while feof(flist)==0,
         clear st1;
          st1=fscanf(flist,'%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g',[73,1]);
         [n0 m0]=size(st1);if n0==0;break;end;         
      
	    if n0==73
      	    st2(:,jax)=st1; 
      	    jax=jax+1;
        end; 
      end;
   end;
end;
psp1=st2(29,:);
psp2=st2(30,:);
    ii=find(psp1<-50 | psp1>1500);jj=find(psp1>-50 & psp1<1500);psp1(ii)=median(psp1(jj));
    ii=find(psp2<-50 | psp2>1500);jj=find(psp2>-50 & psp2<1500);psp2(ii)=median(psp2(jj));
org1=st2(31,:);  
   ii=find(org1<0 | org1>400);jj=find(org1>=0 & org1<400);org1(ii)=median(org1(jj));
Tvais=st2(25,:);
Rhvais=st2(26,:);
   ii=find(Tvais<10 | Tvais>40);jj=find(Tvais>15 & Tvais<40);Tvais(ii)=median(Tvais(jj));
   ii=find(Rhvais<40 | Rhvais>120);jj=find(Rhvais>40 & Rhvais<120);Rhvais(ii)=median(Rhvais(jj));
Tsea=st2(24,:);
  ii=find(Tsea<10 | Tsea>40);jj=find(Tsea>15 & Tsea<40);Tsea(ii)=median(Tsea(jj));
Tc1=st2(20,:);
Td1=st2(21,:);
Tc2=st2(22,:);
Td2=st2(23,:);
therm1=st2(27,:);
therm2=st2(28,:);
pir1=therm1+sig_sb*(Tc1+273.16).^4-4*sig_sb*((Td1+273.16).^4-(Tc1+273.16).^4);
pir2=therm2+sig_sb*(Tc2+273.16).^4-4*sig_sb*((Td2+273.16).^4-(Tc2+273.16).^4);
    ii=find(pir1<250 | pir1>600);jj=find(pir1>250 & pir1<600);pir1(ii)=median(pir1(jj));
    ii=find(pir2<250 | pir2>600);jj=find(pir2>250 & pir2<600);pir2(ii)=median(pir2(jj));
org2=st2(31+6,:);                    %?
org3=st2(32+6,:);                    %?
org4=st2(33+6,:);                    %?
org1_car=st2(16,:);
org2_car=st2(27+6,:);
aspir=st2(73,:);
jad=str2num(num2str(jd))+(tzilch+(st2(1,:)-st2(1,1))/60/60)/24;
np=jax-1;
prtit=1;

if prtit==1;
  prt_epi_means_etl;
end;

if plotit==1;
   Tll=-25;
   hll=70;
   uul=10;
   vul=10;
   Tul=-0;
   hul=100;
   sul=12;
   Rlul=30;
   Rlll=-100;
   Rtul=300;
   Rtll=125;
   Rsul=1100;
   Rsll=0;
    y=num2str(24*mean([org1' org2' org3' org4']));
   %plot T profiles
   j_st=jad(1);
   j_en=jad(np);
   figure;plot(jad(1:np),Tvais(1:np),jad(1:np),Tsea(1:np),jad(1:np),Td1(1:np),jad(1:np),Td2(1:np),jad(1:np),Tc1(1:np),jad(1:np),Tc2(1:np));xlabel('Jday (2001)');ylabel('Temperature (C)');title('Tair_v=blue; Tsea_s_n_a_k_e=green;  T_d_o_m_e_1=red;  T_d_o_m_e_2=cyan;  T_c_a_s_e_1=magenta;  T_c_a_s_e_2=yellow');axis([j_st j_en 20 35]); 
   figure;plot(jad(1:np),Tvais(1:np),jad(1:np),Tsea(1:np));xlabel('Jday (2001)');ylabel('Temperature (C)');title('Tair_v=blue;     Tsea_s_n_a_k_e=green');axis([j_st j_en 20 26]);
   figure;plot(jad(1:np),pir1(1:np),jad(1:np),pir2(1:np));xlabel('Jday (2001)');ylabel('Pir_1 & Pir_2 (W/m^2)');title('Downward IR Flux:   PIR_1=blue;   PIR_2=green'); 
   figure;plot(jad(1:np),psp1(1:np),jad(1:np),psp2(1:np));xlabel('Jday (2001)');ylabel('Psp_1 & Psp_2 (W/m^2)');axis([j_st j_en Rsll Rsul]);title('Downward Solar Flux:   PSP_1=blue;    PSP_2=green'); 
   figure;plot(jad(1:np),org1(1:np),jad(1:np),org2(1:np),jad(1:np),org3(1:np),jad(1:np),org4(1:np));xlabel('Jday (2001)');ylabel('ORG Precipitation Rate (mm/hr)');axis([j_st j_en 0 50]);title(['STI ORG totals= ' y 'mm' ]);
   figure;plot(jad(1:np),org1_car(1:np),jad(1:np),org2_car(1:np));xlabel('Jday (2001)');ylabel('ORG Carrier (V)');axis([j_st j_en 4 6]);title('STI Optical Rain Gauge');
   figure;plot(jad(1:np),aspir(1:np));xlabel('Jday (2001)');ylabel('Backflow (V)');axis([j_st j_en -1 6]);title('Aspirator Backflow');
   
end; 

%clear jd jax jam jad j_st j_en hul hr hll g flist f eps_w e dmp blk backchk ans
%clear vul uul tzilch therm1 therm2 sul stz sty stx st2 st1 st0 sig_sb psp1 psp2 prtit prt_it plotit
%clear Tc1 Tc2 Td1 Td2 Tll Tsea Tul Tvais jazz m0 n0 nl1 np npz nr1 org pir1 pir2
%clear Rhvais Rlll Rlul Rsll Rsul Rtll Rtul
