% Read Crescent GPS data (differential GPS)
%  Note:  hhh=hour

disp(['read_head_pitch_', cruise]);

% gps data is 10 Hz
nsampl=3600*10;
ncols=6;
jd_hed=NaN+zeros(1,nsampl*1.01); % preallocate with tiny 1% buffer
st2=NaN+zeros(ncols,nsampl*1.01);

jax1=1;jd1=[];

% for jam=0:23
    
    hr=sprintf('%02i',hhh);   
    jd=sprintf('%03i',ddd);  
    
    nmang='pitch';
    dfl=[way_raw_data_flux, year(3:4),jd,'\hed0' year(3:4) jd hr '_raw.txt'];
    disp(['Reading head file  ',dfl])
    flist=fopen(dfl);
    if flist>0
        
    %     temp =textscan(flist,'%f %6c %6c %c %f %c %f %c %f %f %f %f %4c %*[^\n]','delimiter', ',', 'headerlines', 1,  'emptyvalue', NaN);
        temp =textscan(flist,'%2f%2f%3f %5c %3c %f %f %f %f %4c %*[^\n]','delimiter', ', ', 'headerlines', 1,  'emptyvalue', NaN);
         %%%%  check for pitch or roll
         xy=temp{1,8};
        if ~isnan(xy(1));
            try
                stx=[temp{1,1}';temp{1,2}';temp{1,3}';(temp{1,6}');(temp{1,7}');(temp{1,8}')];
            catch
                [l10,c10]=size(temp{1,10});
%                 if l10==0 || c10==0; continue; end % read next batch of lines
                for ll=1:10
                    if length(temp{1,ll})~=l10
                        temp{1,ll}(l10+1)=[];
                    end;
                end;
                if isempty(temp{1,1});
                    temp{1,10}=temp{1,10}';
                end;
                stx=[temp{1,1}';temp{1,2}';temp{1,3}';(temp{1,6}');(temp{1,7}');(temp{1,8}')];
            end;    
        else
              stx=[temp{1,1}';temp{1,2}';temp{1,3}';(temp{1,6}');(temp{1,7}');(temp{1,9}')];
              nmang='roll';
        end;
        stx(:,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 + (hhh+(st2(1,jax1:jax1+nl1-1)+(st2(2,jax1:jax1+nl1-1)+st2(3,jax1:jax1+nl1-1)/1000)/60)/60)/24;
        jd_hed(:,jax1:jax1+nl1-1) = jd1;
        jax1=jax1+nl1;
    
    else
        disp(sprintf('\b<--File not found.'))
    end;
% end;

% trim arrays to their filled size
st2=st2(:,1:jax1-1);
jd_hed_pitch=jd_hed(:,1:jax1-1);

[b,m,n]=unique(jd_hed_pitch);
jd_hed_pitch=jd_hed_pitch(m);st2=st2(:,m);

% time_hed_pitch=st2(1,:);
time_hed_pitch=st2(1,:)*60+st2(2,:)+st2(3,:)/1000;  %time in s
headx_pitch=st2(5,:)-0.65;   %12022011 - LB heading off by ~0.65deg comapre to SCS
pitchx_pitch=st2(6,:); 

jdx=jd_hed_pitch(:);jd_hed_pitch=[];jd_hed_pitch=jdx;
% jd_gps=ddd+((st2(1,:)-st2(1,1))/1000/60/60)/24;

if length(st2)< 36000
        disp('<<<<<<<<<<<<<<<<<<  achtung >>>>>>>>>>>>>>>>>>>>>>>>')
    if ~isempty(headx_pitch) && ~isempty(pitchx_pitch)
        rdcon=pi/180;
        headx_pitchc=cos(headx_pitch'*rdcon);        %convert to N
        headx_pitchs=sin(headx_pitch'*rdcon);        %convert to E

        jd_ref=ddd+hhh/24+(0:1/10:35999/10)/60/60/24;

        headxi_pitchc=interp1(jd_hed_pitch(~isnan(jd_hed_pitch)),headx_pitchc(~isnan(jd_hed_pitch)),jd_ref); %deg
        headxi_pitchs=interp1(jd_hed_pitch(~isnan(jd_hed_pitch)),headx_pitchs(~isnan(jd_hed_pitch)),jd_ref); %deg
        pitchx_pitch=interp1(jd_hed_pitch(~isnan(jd_hed_pitch)),pitchx_pitch(~isnan(jd_hed_pitch)),jd_ref); %deg

        headx_pitch=mod(atan2(headxi_pitchs,headxi_pitchc)/rdcon+360,360);  
 
        jd_hed_pitch=jd_ref';
    end;
% elseif length(st2)< 36000
% % if length(st2)< 36000
%     headx_pitch=[headx_pitch (1:36000-length(st2)).*0+headx_pitch(length(st2))];
%     pitchx_pitch=[pitchx_pitch (1:36000-length(st2)).*0+pitchx_pitch(length(st2))]; 
elseif length(st2)> 36000
% if length(st2)> 36000
    headx_pitch=headx_pitch(1:36000);
    pitchx_pitch=pitchx_pitch(1:36000);     
end;

if clearit
    clear headx pitchx jd_hed jdx time_hed st2 yxp jaz jazz jd1 stx xy
%clear also big size GPS variables
%     clear gpslon1 gpslat1 gpsspeed1 gpshead1 glt gln gpslat_sign gpslon_sign 
%     clear gpsspeedx gpsspeedy jd_gps
end
