disp('read_hp_hiwings2013.m')
fclose('all');

g=0.0098;						%adiabatic lapse rate
sig_sb=5.67e-8;				%Stefan Boltzmann constant
jax=1;
jd=sprintf('%03i',ddd);
hr=sprintf('%02i',hhh);  
nr1=65;
% e=[way_raw_data_flux, yyyy(3:4),jd,'\met0' yyyy(3:4) jd hr '_raw.txt'];
e=[way_raw_data_flux, yyyy(3:4),jd,'/met0' yyyy(3:4) jd hr '_raw.txt'];% Mac file system

disp(['Reading means file from hour ',hr]);
flist=fopen(e,'r');

% Declare Arrays
nr1=61;jax1=1;

st2=ones(nr1,60)*NaN;
jd_pc=ones(1,60)*NaN;

if flist>0,					    %if the file exists, 
        disp(['Reading means file from hour ',hr]);
        stx=[];
        while ~feof(flist) % end-of-file not reached         
            try
                                     %1                           %10                          %20                           %30                           %40                           %50                                                    
%               temp =textscan(flist,'%2f%2f%3f %2f/%2f/%4f %2f:%2f:%2f %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);
                % stx=[stx cell2mat(temp)'];
                %reads in as %s as potential NAN in the met files
                temp =textscan(flist,'%2f%2f%3f %2f/%2f/%4f %2f:%2f:%2f %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %*[^\n]', 'delimiter', ', ', 'headerlines', 1,  'emptyvalue', NaN);
                stx=[stx temp{1} temp{2} temp{3} temp{4} temp{5} temp{6} temp{7} temp{8} temp{9}];
                for ll=10:length(temp)
                        temp{ll}(ismember(temp{ll},'NAN'))=cellstr('NaN');
                        temp{ll}(ismember(temp{ll},''))=cellstr('NaN');
                        stx=[stx str2num(char(temp{ll}))];
                end;
%                 lud=find((ismember(temp{ll},''))); %sometimes a comma is
%                 missing thus offsetting the reading and empty value are
%                 found which crashes the code - LB - 10192013
%                 will edit manually the file for now...

            catch 
                [lnr1,cnr1]=size(temp{1,nr1});
                if lnr1==0 || cnr1==0; continue; end % read next batch of lines
                for ll=1:nr1
                    if length(temp{1,ll})~=lnr1
                        temp{1,ll}(lnr1+1)=[];
                    end;
                end;
                if isempty(temp{1,1});
                    temp{1,nr1}=temp{1,nr1}';
                end;
                stx=[stx cell2mat(temp)'];
            end;   %end try/catch
        end; %end ~feof(flist)
        stx=stx';
        [nr1,nl1]=size(stx);
        st2(:,jax1:jax1+nl1-1)=stx;
        jd1=ddd + (hhh+(stx(1,:)+(stx(2,:)+stx(3,:)/1000)/60)/60)/24;
        jd_pc(:,jax1:jax1+nl1-1)=jd1;
        jax1=jax1+nl1;
    fclose(flist);
    else
        disp(['Reading means file from hour ',hr]);
         disp(sprintf('\b<--File not found.'))
        stx=ones(nr1,60)*NaN;
        [nr1,nl1]=size(stx);
        jd1=ddd + (hhh+(0:59)/60)/24;
        jd_pc(:,jax1:jax1+nl1-1)=jd1;
        st2(:,jax1:jax1+nl1-1)=stx;
        jax1=jax1+nl1;
    end;                            %end if flist


% TO CHECK
% %adjust MET for 2minlag issue
% mean1=[mean1(:,3:end) ones(65,2)*NaN];

% Adjustments & postcalibrations for flux observations (done on ship)
%Comparison done with Buoys
tsnake_adjustment=0; %degC;
ta_etl_adjustment=0;
rh_etl_adjustment=0;
bp_etl_adjustment=0;  %our BP is 0.58mb too high compare to UConn system

% TRH=load('C:\data\CRUISES\HiWinGS_2013\Knorr\flux\Processed\cleanedTRH');

% t_case1_adjustment=Tcase1_adj_ref(ddd==doyref);
% t_dome1_adjustment=Tdome1_adj_ref(ddd==doyref);
% t_kz_adjustment=0;
t_case1_adjustment=0;
t_dome1_adjustment=0;
t_case2_adjustment=0;
t_dome2_adjustment=0;


press=st2(35,:)+bp_etl_adjustment;    %pressure (mbar)
%%%%%%%%%%%%%%  correct pressure offset at beginning (from epicz_read_CALNEX_2010_cf.m)
% ii=find(jd_mean1<137.111);press(ii)=press(ii)+17;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
psp1=-st2(25,:);  %Eppley PSP bridge
psp2=-st2(27,:);  %K&Z PSP bridge
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
org=st2(30,:);   %mm/hr
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Tsea=st2(34,:)+tsnake_adjustment;
Tc1=st2(13,:) +t_case1_adjustment;  %case temperature for PIR Eppley
Td1=st2(15,:) +t_dome1_adjustment;  %dome temperature for PIR Eppley
Tc2=st2(17,:) +t_case1_adjustment;  %case temperature for PIR Eppley
Td2=st2(19,:) +t_dome1_adjustment;  %dome temperature for PIR Eppley
% thermopile voltages
therm1=-st2(21,:);
therm2=-st2(23,:);

%%%%%%%%%%%%%%%%%%%%%%%%  reads in T/RH
read_tairh_HiWinGS_2013;
if length(jd1(isnan(jd1)))~=3600
    Rhvais=interp1(jd1(~isnan(jd1)),RH(~isnan(jd1)),jd_pc)+ta_etl_adjustment;          
    Tvais=interp1(jd1(~isnan(jd1)),T(~isnan(jd1)),jd_pc)+rh_etl_adjustment;
else
    Rhvais=RH;
    Tvais=T;
end

C2K=273.15;  
pir1=therm1+sig_sb*(Tc1+C2K).^4-3*sig_sb*((Td1+C2K).^4-(Tc1+C2K).^4); %adjust B coeff to 3 for leg1
pir2=therm2+sig_sb*(Tc2+C2K).^4-4*sig_sb*((Td2+C2K).^4-(Tc2+C2K).^4);

% pir=(pir1+pir2)/2;
psp=(psp1+psp2)/2;
%12/02/2011 - LB
% Eppley PIR1 had some solar contamination or overheating. Will use PIR2
% only
pir=pir2;
% psp=psp2;
clear psp1 psp2 pir2 pir1

if length(Rhvais)<60,
   Rhvais=[Rhvais (1:(60-length(Rhvais))).*0+NaN];
end;
if length(pir)<60,
   pir=[pir (1:(60-length(pir))).*0+NaN];
end;
if length(psp)<60,
   psp=[psp (1:(60-length(psp))).*0+NaN];
end;
if length(org)<60,
   org=[org (1:(60-length(org))).*0+NaN];
end;
if length(Tvais)<60,
   Tvais=[Tvais (1:(60-length(Tvais))).*0+NaN];
end;
if length(Tsea)<60,
   Tsea=[Tsea (1:(60-length(Tsea))).*0+NaN];
end;
if length(press)<60,
   press=[press (1:(60-length(press))).*0+NaN];
end;

if length(Rhvais)>60,
   Rhvais=Rhvais(1:60);
end;
if length(pir)>60,
   pir=pir(1:60);
end;
if length(psp)>60,
   psp=psp(1:60);
end;
if length(org)>60,
   org=org(1:60);
end;
if length(Tvais)>60,
   Tvais=Tvais(1:60);
end;
if length(Tsea)>60,
   Tsea=Tsea(1:60);
end;
if length(press)>60,
   press=press(1:60);
end;

if clearit
clear jax jad j_st j_en hul hll g flist f eps_w dfl dmp blk backchk ans
clear vul uul tzilch therm1 therm2 sul stz sty stx st2 st1 st0 sig_sb prtit prt_it plotit
clear Tc1 Tc2 Td1 Td2 Tll Tul jazz m0 n0 nl1 np npz nr1 Rlll Rlul Rsll Rsul Rtll Rtul
end;
% Rs_1=mean(psp(1:10));
% Rs_2=mean(psp(11:20));
% Rs_3=mean(psp(21:30));
% Rs_4=mean(psp(31:40));
% Rs_5=mean(psp(41:50));
% Rs_6=mean(psp(51:60));
% stdRs_1=std(psp(1:10));
% stdRs_2=std(psp(11:20));
% stdRs_3=std(psp(21:30));
% stdRs_4=std(psp(31:40));
% stdRs_5=std(psp(41:50));
% stdRs_6=std(psp(51:60));
% 
% A=[Rs_1 stdRs_1
%    Rs_2 stdRs_2
%    Rs_3 stdRs_3
%    Rs_4 stdRs_4
%    Rs_5 stdRs_5
%    Rs_6 stdRs_6];
%    
% sdg=['save ', drive_w, '\procturb\ss_',jd,hr,'.prn A -ascii'];
% 
% disp(sdg)
% eval(sdg)