% Read Ming's  motion pack files for HiWinGS 2013 data

function [jd_PML,AccX_PML,AccY_PML,AccZ_PML,RateX_PML,RateY_PML,RateZ_PML,U_PML,V_PML,W_PML,Tsonic_PML,Xlag]=read_PML_HiWinGS_2013(ddd,hhh,yyyy,accz)

% disp(['read_DMS: ', cruise]);
 jd=sprintf('%03i',ddd);
hr=sprintf('%02i',hhh);
fclose('all');
 [m,d]=yd2md(str2num(yyyy), ddd);
%declare arrays
jax=1;                              %line counter
nr2=16;
pmldata=ones(nr2+1,36000*1.01)*NaN;


% for jam=0:23,					    %cycle thru 24 hourly stats files
    
    e=['C:\data\CRUISES\HiWinGS_2013\Knorr\Ming\2013 Oct 9 to 11 WindplusMotion\' yyyy sprintf('%02i%02i',m,d) hr '_WindplusMotion.txt'];
    %e=[way_raw_data_flux, 'day',jd,'\met0' year(3:4) jd hr '_raw.txt'];
    disp(['Reading DMS 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/%2f %2f:%2f:%f %f %f %f %f %f %f %f %f %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)*hhh;
        pmldata(:,jax:jax+nl1-1)=[stx' hhrs']';
        jax=jax+nl1;
    
    else
        disp(sprintf('\b<--File not found.'))
        stx=ones(nr2,36000)*NaN;
%         stx(1,:)=ones(1,3600)*jam; stx(2,:)=ones(1,3600)*jam;
        [nr1,nl1]=size(stx);
        hhrs=ones(1,nl1)*jam;
        pmldata(:,jax:jax+nl1-1)=[stx' hhrs']';
        jax=jax+nl1;
    end;      %     end if list
  
pmldata(:,any(isnan(pmldata),1))=[]; %remove lines containing NaNs (bad lines, too long...)

% jd_PML= ddd+(pmldata(1,:)-pmldata(1,1))/3600/24; % time in decimal yearday
jd_PML= datenum(2000+pmldata(3,:),pmldata(1,:),pmldata(2,:),pmldata(4,:),pmldata(5,:),pmldata(6,:))-datenum(2013,1,0); % time in decimal yearday
U_PML=pmldata(7,:);     % scale wind to physical units
V_PML=pmldata(8,:);  
W_PML=pmldata(9,:); 
Tsonic_PML=pmldata(10,:);
AccX_PML=-pmldata(12,:).*9.80;  %m/s2
AccY_PML=pmldata(11,:).*9.80;  %m/s2
AccZ_PML=pmldata(13,:).*9.80;  %m/s2
RateX_PML=-pmldata(15,:)*pi/180; %rad/s
RateY_PML=pmldata(14,:)*pi/180; %rad/s
RateZ_PML=pmldata(16,:)*pi/180; %rad/s
% // motion polarity (looking at the connector, which is on the downside)
% // + Zaccel is upwards
% // + yaccel is to the back (towards the stern)
% // + x accel is to the left (towards the port)
% // + zrot is bow to port, or counterclockwise (when looking from up)
% // + yrot is roll to port, or counterclockwise (when looking from behind)
% // + xrot is pitch down to bow, or counterclockwise (when looking from the left)

if ~isnan(accz)
    try
        Xlag=findlag(accz,AccZ_PML(1:length(accz)),500,0);
    catch
        Xlag=findlag(accz(1:length(AccZ_PML)),AccZ_PML,500,0);
    end    
else
    Xlag=NaN;
end



