From 036b1a3efe5be9ad0e0ffe754f0efeac5f7a0571 Mon Sep 17 00:00:00 2001
From: Jacques Grelet <jacques.grelet@ird.fr>
Date: Tue, 26 Aug 2014 07:40:57 +0000
Subject: [PATCH] =?UTF-8?q?G.=20Alory=20Adapt=C3=A9=20aux=20navires=20de?=
 =?UTF-8?q?=20recherche=20en=20station:=20l'alerte=20n'est=20maintenant=20?=
 =?UTF-8?q?=C3=A9mise=20que=20si=20la=20position=20du=20navire=20=C3=A0=20?=
 =?UTF-8?q?la=20date=20de=20la=20donn=C3=A9e=20externe=20est=20=C3=A0=20pl?=
 =?UTF-8?q?us=20de=205=20km=20de=20la=20position=20de=20la=20donn=C3=A9e?=
 =?UTF-8?q?=20externe?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 tsg_util/compDateLocBtlShip.m | 272 ++++++++++++++++++----------------
 1 file changed, 142 insertions(+), 130 deletions(-)

diff --git a/tsg_util/compDateLocBtlShip.m b/tsg_util/compDateLocBtlShip.m
index 91dae46..5e70aac 100644
--- a/tsg_util/compDateLocBtlShip.m
+++ b/tsg_util/compDateLocBtlShip.m
@@ -1,130 +1,142 @@
-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=15;
-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))...
-    & 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
-        
-        % 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
-        
-        timeDiff=abs(tsg.DAYD(dateTsgAround(locNearest))-data.DAYD_EXT(ibtl));
-        
-        %warning for lag > 30 minutes
-        %----------------------------
-        if timeDiff>WS_SHIPTRAJ_TIMEDIFF %&  dist(nearest)<=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 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('%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
+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
+
-- 
GitLab