function compDateLocBtlShip( hMainFig, data )

% Function that compares noted date of bottle sampling with
% date when the ship was closest to sampling location
%
%
% input
%  hMainFig .... handle to the tsgqc main program
%  data ........ structure of _EXT data whose dates are compared with ship location
%
%

% Get the data from the application GUI
% -------------------------------------
tsg = getappdata( hMainFig, 'tsg_data');

% time/space limits for "good" bottle/ship colocation
%----------------------------------------------------
%A PASSER dans tsg.cst?
WS_SHIPTRAJ_TIMEDIFF=datenum(0,0,0,0,30,0);
WS_SHIPTRAJ_DISTDIFF=5;
WS_SHIPTRAJ_COLOC_TIMEMAX=1.5;

% find bottles within the ship trip
%----------------------------------
btlTrip=find((data.DAYD_EXT>=(tsg.DAYD(1)-WS_SHIPTRAJ_COLOC_TIMEMAX))...
    & (data.DAYD_EXT<=(tsg.DAYD(end)+WS_SHIPTRAJ_COLOC_TIMEMAX)));
%    & (data.DAYD_EXT<=(tsg.DAYD(end)+WS_SHIPTRAJ_COLOC_TIMEMAX))...
%    & strcmp(data.SSPS_EXT_TYPE,'WS'));

if isempty(btlTrip)
    message = { 'WARNING: No Water Sample within ship trip is detected'};
    
    % Display the message box and blocks execution until the msgbox is deleted.
    % -------------------------------------------------------------------------
    hMsg1 = msgbox(message,'No useful Water Sample','warn', 'modal');
    uiwait(hMsg1);
    
else
    
    % indices with colocation problem
    %--------------------------------
    nprob=0;
    indBtlProb=[];
    indTsgBestLoc=[];
    indTsgAtSampling=[];
    
    for ibtl=min(btlTrip):max(btlTrip)
        dateTsgAround=find((tsg.DAYD>=(data.DAYD_EXT(ibtl)-WS_SHIPTRAJ_COLOC_TIMEMAX))...
            & (tsg.DAYD<=(data.DAYD_EXT(ibtl)+WS_SHIPTRAJ_COLOC_TIMEMAX)));
        
        % geographical distance between sampling location and ship position
        % within colocation window
        %------------------------------------------------------------------
        dist=nan*ones(1,length(dateTsgAround));
        for it=1:length(dateTsgAround)
            dist(it)=m_lldist([data.LONX_EXT(ibtl) tsg.LONX(dateTsgAround(it))],[data.LATX_EXT(ibtl) tsg.LATX(dateTsgAround(it))]);
        end
        
        % look for ship location nearest to sampling location
        %----------------------------------------------------
        locNearest=find(dist==min(dist));
        if ~isempty(locNearest)
            locNearest=locNearest(1);
        end
        % time lag between time of sampling and 
        % time when ship is nearest to sampling location 
        %-----------------------------------------------
        timeDiff=abs(tsg.DAYD(dateTsgAround(locNearest))-data.DAYD_EXT(ibtl));
        
        % look for ship location at time of sampling
        %-------------------------------------------
        timeDiffTsgBtl=abs(tsg.DAYD-data.DAYD_EXT(ibtl));
        timeNearest=find(timeDiffTsgBtl==min(timeDiffTsgBtl));
        if ~isempty(timeNearest)
            timeNearest=timeNearest(1);
        end        
        % distance between ship location nearest to sampling location
        % and ship location at time of sampling
        %------------------------------------------------------------
        distDiff=m_lldist([tsg.LONX(timeNearest) tsg.LONX(dateTsgAround(locNearest))],...
            [tsg.LATX(timeNearest) tsg.LATX(dateTsgAround(locNearest))]);
        
        %warning for lag > 30 minutes (and distance > 5km between 
        %ship location nearest to sampling location & at time of sampling)
        %-----------------------------------------------------------------
        if timeDiff>WS_SHIPTRAJ_TIMEDIFF & distDiff>=WS_SHIPTRAJ_DISTDIFF
            nprob=nprob+1;
            indBtlProb=[indBtlProb,ibtl];
            indTsgBestLoc=[indTsgBestLoc,dateTsgAround(locNearest)];
            indTsgAtSampling=[indTsgAtSampling,timeNearest];
        end
        
        
    end
    
    if nprob>0
        
        % Create the message text to display
        % ----------------------------------
        MDhm_TsgAtSampling=datevec(tsg.DAYD(indTsgAtSampling,:));
        MDhm_TsgAtSampling=MDhm_TsgAtSampling(:,2:5);
        MDhm_TsgBestLoc=datevec(tsg.DAYD(indTsgBestLoc,:));
        MDhm_TsgBestLoc=MDhm_TsgBestLoc(:,2:5);
        message = { 'WARNING: bottle/ship colocation problems are detected'; ' ';...
            [num2str(nprob),' bottles location disagree with ship trajectory']; ' ';...
%            'BTL date/lat/lon TSG date1/lat1/lon1 TSG date2/lat2/lon2 '; ' ';...
            'BTL date/lat/lon TSG date/lat/lon '; ' ';...
            [reshape(sprintf('%02d/',data.MNTH(indBtlProb)),3,nprob)',...
            reshape(sprintf('%02d ',data.DAYX(indBtlProb,:)),3,nprob)',...
            reshape(sprintf('%2d:',data.hh(indBtlProb,:)),3,nprob)',...
            reshape(sprintf('%02d ',data.mi(indBtlProb,:)),3,nprob)',...
            reshape(sprintf('%7.2f',data.LATX_EXT(indBtlProb,:)),7,nprob)',...
            reshape(sprintf('%8.2f ',data.LONX_EXT(indBtlProb,:)),9,nprob)',...
            reshape(sprintf('%02d/',MDhm_TsgBestLoc(:,1)),3,nprob)',...
            reshape(sprintf('%02d ',MDhm_TsgBestLoc(:,2)),3,nprob)',...
            reshape(sprintf('%2d:',MDhm_TsgBestLoc(:,3)),3,nprob)',...
            reshape(sprintf('%02d ',MDhm_TsgBestLoc(:,4)),3,nprob)',...
            reshape(sprintf('%7.2f',tsg.LATX(indTsgBestLoc,:)),7,nprob)',...
            reshape(sprintf('%8.2f',mod(tsg.LONX(indTsgBestLoc,:)+180,360)-180),8,nprob)'] };
%             reshape(sprintf('%8.2f ',mod(tsg.LONX(indTsgBestLoc,:)+180,360)-180),9,nprob)',...
%             reshape(sprintf('%02d/',MDhm_TsgAtSampling(:,1)),3,nprob)',...
%             reshape(sprintf('%02d ',MDhm_TsgAtSampling(:,2)),3,nprob)',...
%             reshape(sprintf('%2d:',MDhm_TsgAtSampling(:,3)),3,nprob)',...
%             reshape(sprintf('%02d ',MDhm_TsgAtSampling(:,4)),3,nprob)',...
%             reshape(sprintf('%7.2f',tsg.LATX(indTsgAtSampling,:)),7,nprob)',...
%             reshape(sprintf('%8.2f ',mod(tsg.LONX(indTsgAtSampling,:)+180,360)-180),9,nprob)'] };
        
        % Display the message box and blocks execution until the msgbox is deleted.
        % -------------------------------------------------------------------------
        hMsg1 = msgbox(message,'Test bottle/ship colocation, ','warn', 'modal');
        uiwait(hMsg1);
        
    end
end

% Save tsg structure
% ------------------
setappdata( hMainFig, 'tsg_data', tsg);

end

