% Read in trh for OCEANUS_2010 data
% DEW 8/4/2010

close all

disp(['read_HMP T rh q: ', cruise]);
fclose('all');
jd=sprintf('%03i',ddd);

%Sergio trh output
% 08/04/2010 07:00 HMP235 (SN: HMP235)
% 0000549 
% 07:00:01  44.344	 27.382	 11.692
% 0001362 
% 07:00:02  44.343	 27.383	 11.693
sig_sb=5.67e-8;				        %Stefan Boltzmann constant
jax1=1;
jd_pc=[];st2=[];
ct = 0;

%declare arrays
jax=1;                              %line counter
nr2=5;
trhdata=ones(nr2+1,3600*24)*NaN;

for jam=0:23,					    %cycle thru 24 hourly stats files
    hr=sprintf('%02i',jam);
    e=[way_raw_data_flux, yyyy(3:4),jd,'\trh1' yyyy(3:4) jd hr '_raw.txt'];
    %e=[way_raw_data_flux, 'day',jd,'\met0' year(3:4) jd hr '_raw.txt'];
    disp(['Reading trh file from hour ',e]);
    flist=fopen(e,'r');
    temp=[];stx=[];
    
    if flist>0,					    %if the file exists,
        while ~feof(flist)
            try
                temp =textscan(flist,'%2f%2f%3f %f %f %*[^\n]', 'delimiter', '', 'headerlines', 1,  'emptyvalue', NaN);
                stx=[stx cell2mat(temp)'];
            catch
                [lnr2,cnr2]=size(temp{1,nr2});
                for ll=1:nr2
                     if length(temp{1,ll})~=lnr2
                         temp{1,ll}(lnr2+1)=[];
                     end;
                end;
                if isempty(temp{1,1}); continue; end
                if isempty(temp{1,1});
                    disp('Should never do this!')
                    temp{1,nr2}=temp{1,nr2}';
                end;
             stx=[stx cell2mat(temp)'];
            end;
        end;  %end while
    
        [nr1,nl1]=size(stx);
        hhrs=ones(1,nl1)*jam;
        trhdata(:,jax:jax+nl1-1)=[stx' hhrs']';
        jax=jax+nl1;
    
    else
        disp(sprintf('\b<--File not found.'))
        stx=ones(nr2,3600)*NaN;
        [nr1,nl1]=size(stx);
        hhrs=ones(1,nl1)*jam;
        trhdata(:,jax:jax+nl1-1)=[stx' hhrs']';
        jax=jax+nl1;
    end;      %     end if list
  
end;                                %end for jam

% trim arrays to their filled size
trhdata=trhdata(:,1:jax-1);
% 
% RH = trhdata(4,:);
% T  = trhdata(5,:);
% q  = trhdata(6,:);
hh = trhdata(end,:);
mm = trhdata(1,:);
ss = trhdata(2,:) + trhdata(3,:)/1000;
jdtrh = ddd + (hh+((mm+(ss/60))/60))/24; % time in decimal yearday
%             
%    
% if plotit
%     figure
%     subplot(2,1,1),plot(jd1,T,'k')
%     title(sprintf('%s (%04i-%02i-%02i, DOY%03i).  PSD T/RH',cruise,Vdate(1),Vdate(2),Vdate(3),ddd),'FontWeight','Bold','Interpreter','none');
%     xlabel('Hour (UTC)');ylabel('Temperature (degC)');xlim([ddd ddd+1])
%     set(gca(gcf),'XTick',ddd:2/24:ddd+1);datetick('x','HH:MM','keepticks');
%     xax=get(gca(gcf),'XTickLabel');xax(end,:)='24:00';set(gca(gcf),'XTickLabel',xax(:,1:2))    
%     grid
%     subplot(2,1,2),plot(jd1,RH,'k')
%     xlabel('Hour (UTC)');ylabel('RH (%)');axis([ddd ddd+1 0 100])
%     set(gca(gcf),'XTick',ddd:2/24:ddd+1);datetick('x','HH:MM','keepticks');
%     xax=get(gca(gcf),'XTickLabel');xax(end,:)='24:00';set(gca(gcf),'XTickLabel',xax(:,1:2))    
%     grid
%         annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
%         if prtit; print(graphdevice,[way_images_flux cruise '_PSD_TRH_' sprintf('%04i_%02i_%02i_%03i',Vdate(1),Vdate(2),Vdate(3),ddd) graphformat]); end;
% 
%     figure
%     plot(jd1,q,'k')
%     title(sprintf('%s (%04i-%02i-%02i, DOY%03i).  PSD specific humidity',cruise,Vdate(1),Vdate(2),Vdate(3),ddd),'FontWeight','Bold','Interpreter','none');
%     xlabel('Hour (UTC)');ylabel('q (g/kg)');axis([ddd ddd+1 0 25])
%     set(gca(gcf),'XTick',ddd:2/24:ddd+1);datetick('x','HH:MM','keepticks');
%     xax=get(gca(gcf),'XTickLabel');xax(end,:)='24:00';set(gca(gcf),'XTickLabel',xax(:,1:2))    
%     grid
%         annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
%         if prtit; print(graphdevice,[way_images_flux cruise '_PSD_q_' sprintf('%04i_%02i_%02i_%03i',Vdate(1),Vdate(2),Vdate(3),ddd) graphformat]); end;
% 
% end  % plotit   
% 
% 
% if saveit;
%     if length(isnan(jd1)==1)==1440;   %means no data files detected during the 24H period
%         jd1=ddd+(0:1439)/60/24;       %creates a fake decimal DOY base so that timatch programs work properly
%     end;  
%   jd_pc=ddd+(0:1439)/60/24; 
%   RHm=intavg(jd1,RH,jd_pc);          
%   Tm=intavg(jd1,T,jd_pc);         
%   qm=intavg(jd1,q,jd_pc);     
%   np=length(RHm);  
%   eval([ 'prt_jas_trh_', currentMname])
% end;