disp(['read_pc_day_', cruise, '_', year]);
fclose('all');


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

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

sig_sb=5.67e-8;				        %Stefan Boltzmann constant
jax1=1;
jd_pc=[];

for jam=0:23,					    %cycle thru 24 hourly stats files
    if jam<10,
        hr=['0' num2str(jam)];
    else
        hr=num2str(jam);
    end;                            %end if jam
   
    e=[way_raw_data_flux, 'day',jd,'\met0' year(3:4) jd hr '_raw.txt'];
%     e=[way_raw_data_flux, 'day',jd,'\P2_' jd hr];
    disp(['Reading means file from hour ',hr]);
    flist=fopen(e,'r');
   
    if flist>0,					    %if the file exists, 
            clear stx;
                                     %1                        %10                            %20                           %30                           %40                           %50                           %60                           %70                 %77
%                 stx =textscan(flist,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %*[^\n]', 'delimiter', ',', 'headerlines', 1,  'emptyvalue', NaN,'treatAsEmpty', 'Radiometers & Mean Met');
              stx =textscan(flist,'%2f%2f%3f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %*[^\n]', 'delimiter', ', ', 'headerlines', 1,  'emptyvalue', NaN,'treatAsEmpty', 'Radiometers & Mean Met');
                for ii=1:79
                    if length(stx{1,ii})~=60
                        stx{1,ii}=[stx{1,ii};ones(60-length(stx{1,ii}),1)*NaN];
                    end;
                end;    
                stx=cell2mat(stx)';
                stx(4:79,any(isnan(stx),1))=NaN;      %put NaN into any rows containing NaNs (to remove bad lines, gap...), except JD

                [nr1,nl1]=size(stx);
                st2(:,jax1:jax1+nl1-1)=stx;
                jd1=ddd + (jam+(stx(1,:)+(stx(2,:)+stx(3,:)/1000)/60)/60)/24;
                jd_pc = [jd_pc jd1];
    	        jax1=jax1+nl1;
    end;                            %end if flist
end;                                %end for jam

if length(st2)<1440,
    st2=[st2 ones(79,1440-length(st2))*NaN];
    jd_pc=[jd_pc ones(1,1440-length(jd_pc))*NaN];
end


org_carrier=st2(17,:);         %sti carrier voltage
aspir_on=st2(34,:);            %aspirator operational (+5V) or backflow (0V)

press=st2(36,:)+1.0;    %pressure (mbar), the 1.0 corrects for diff in readout and AtoD
psp1=st2(30,:);
psp2=st2(31,:);
org=st2(32,:);
if [cruise year]=='STRATUS2007';
    ii=find(jd_pc<366.1); org(ii)=0;
end;%STI not working on stratus 2007 
Tvais=st2(26,:);
Rhvais=st2(27,:);
ij=find(Rhvais<0);Rhvais(ij)=Rhvais(max(1,ij-1));ij=find(Rhvais<50);Rhvais(ij)=median(Rhvais);

avgpsp=nanmean(psp1)
avgrain=nanmean(org);
avgTvais=nanmean(Tvais)
avgRHvais=nanmean(Rhvais);
avgQvais=avgRHvais/100*qsea(avgTvais)
qvais=qair_p([Tvais' Rhvais' press']);


Tsea=st2(25,:)+.01;   %correct for lead resistance 20C

Tc1=st2(21,:)-0.25;  %correct for bias, stratus 2007
Td1=st2(22,:)-0.25;
Tc2=st2(23,:)-.1;    %correct for bias stratus 2007
Td2=st2(24,:);%+.35;

therm1=st2(28,:);
therm2=st2(29,:);

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);


pspm=(psp1+psp2)/2;

%check for bad values in PIR
ij=find(pir1<50 | pir1>500);pir1(ij)=NaN;
ik=find(pir2<50 | pir2>500);pir2(ik)=NaN;
pirm=(pir1+pir2)/2;
ij=find(~isnan(pir1) & isnan(pirm));pirm(ij)=pir1(ij);
ij=find(~isnan(pir2) & isnan(pirm));pirm(ij)=pir2(ij);
pirm(isnan(pirm))=-999;

avgsnake=nanmean(Tsea)

% % Time in flux data files is the number of 100-nano second intervals since
% % Jan 1, 1601 divided by 10000. It was chosen to maintain compatibility
% % with an HP-UNIX version of the code form long ago
% %  0             'dd-mmm-yyyy HH:MM:SS'  
% %01-Mar-2000 15:45:17
% dates = datestr(datenum(1601,1,1) + datenum(st2(1,:)*10000*100e-9/86400));
% hh = str2num(dates(:,13:14));
% mm = str2num(dates(:,16:17));
% ss = str2num(dates(:,19:20));
% jd_pc= ddd + (hh+((mm+(ss/60))/60))/24;
% 
% % test to see if first point is from the previous day
% if str2num(dates(1,1:2)) ~= str2num(dates(2,1:2))
%     jd_pc(1) = jd_pc(1)-1;
% end    

% jd_pc2=str2num(jd)+((st2(1,:)-st2(1,1))/1000/60/60)/24;

figure;plot(jd_pc,press,'r');title(['Vaisala Pressure. ', cruise, year]);xlabel(['JD', year]);ylabel('Pressure (mb)');axis([ddd,ddd+1,1000,1020])
    print('-djpeg90 ',[way_images_flux '\Pressure_' num2str(ddd) '.jpg']);
figure;plot(jd_pc,Tc1,'r',jd_pc,Tc2,'m',jd_pc,Td1,'k',jd_pc,Td2,'c',jd_pc,Tsea,'b',jd_pc,Tvais,'g');title(['PSD Temperatures. ', cruise, year]);legend('case 1','case 2','dome 1', 'dome 2','snake', 'Tair');xlabel(['JD', year]);ylabel('Temperature (C)');xlim([ddd ddd+1])
    print('-djpeg90 ',[way_images_flux '\Temperatures_' num2str(ddd) '.jpg']);
figure;plot(jd_pc,psp1,jd_pc,psp2);title(['PSD psp. ', cruise, year]);xlabel(['JD', year]);ylabel('Solar FLux (W/m^2)');legend('PSP1','PSP2');xlim([ddd ddd+1])
    print('-djpeg90 ',[way_images_flux '\Solar_flux_' num2str(ddd) '.jpg']);
figure;plot(jd_pc,pir1,jd_pc,pir2);title(['PSD pir. ', cruise, year]);xlabel(['JD', year]);ylabel('IR Flux (W/m^2)');legend('PIR1','PIR2');xlim([ddd ddd+1])
    print('-djpeg90 ',[way_images_flux '\IR_flux_' num2str(ddd) '.jpg']);
figure;plot(jd_pc,Rhvais);title(['PSD RH. ', cruise, year]);xlabel(['JD', year]);ylabel('Relative Humidity (%)');xlim([ddd ddd+1])
    print('-djpeg90 ',[way_images_flux '\RH_' num2str(ddd) '.jpg']);
figure;plot(jd_pc,org);title(['PSD org rain rate. ', cruise, year]);xlabel(['JD', year]);ylabel('Rain Rate (mm/hr)');xlim([ddd ddd+1])
    print('-djpeg90 ',[way_images_flux '\rainrate_' num2str(ddd) '.jpg']);
figure;plot(jd_pc,aspir_on);title(['PSD aspirator. ', cruise, year]);xlabel(['JD', year]);ylabel('Backflow Indicator (Volt). If 0V, there is backflow');axis([ddd,ddd+1,0,6])
    print('-djpeg90 ',[way_images_flux '\backflow_indicator_' num2str(ddd) '.jpg']);
figure;plot(jd_pc,org_carrier);title(['PSD org carrier. ', cruise, year]);xlabel(['JD', year]);ylabel('Rain Gauge Function (V)');xlim([ddd ddd+1])
    print('-djpeg90 ',[way_images_flux '\rain_gauge_function_' num2str(ddd) '.jpg']);
figure;plot(jd_pc,Tsea);title(['PSD seasnake. ', cruise, year]);xlabel(['JD', year]);ylabel('Temperature (degC)');xlim([ddd ddd+1])

if prtit==1;
  np=length(st2);  
  prt_jas_means_Stratus_07a;   
  prt_jas_rads_Stratus_07;
end;