From a35358c006b6be828e62c3eb34c4fd857e7abbb2 Mon Sep 17 00:00:00 2001
From: Lewin Probst <info@emirror.de>
Date: Tue, 18 Apr 2023 11:21:36 +0200
Subject: [PATCH] feat: IBCSO dataset can now be used to plot topographical
 lines

---
 VERSION                                       |  2 +-
 artoa.ini.example                             |  6 ++-
 lib/+artoa/+controller/+file/loadArtoaIni.m   | 11 +++-
 .../plotTopographicalLines.m                  | 51 +++++++++++++++----
 .../tableTopographicalLinesEdit.m             |  4 +-
 .../+track/+trajectoryOutput/updateGui.m      |  4 +-
 6 files changed, 61 insertions(+), 17 deletions(-)

diff --git a/VERSION b/VERSION
index b99877a..8db9866 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-331
\ No newline at end of file
+332
\ No newline at end of file
diff --git a/artoa.ini.example b/artoa.ini.example
index c4c0f9d..0ab4eb6 100644
--- a/artoa.ini.example
+++ b/artoa.ini.example
@@ -5,6 +5,7 @@
 -floatfile          ..\HAFOS.flo  % not used in artoa4 yet
 -bathyfile            % not used in artoa4 yet
 -etopo              /your/filepath/here/data/etopo1_bed_c.flt
+-ibcso              /your/filepath/here/data/contours_Grid5000_Step50.mat
 
 [directory]
 -interim        /your/filepath/here/
@@ -56,7 +57,10 @@
 -pressure		9999
 -temperature		9999
 -toa			9999
--topographical_lines -4000 -3000 -2000
+
+[topographical_lines]
+-depth -4000 -3000 -2000
+-use_ibcso 1 % set this to 0 if you want to use etopo
 
 [offsetvariations]
 1   1
diff --git a/lib/+artoa/+controller/+file/loadArtoaIni.m b/lib/+artoa/+controller/+file/loadArtoaIni.m
index 69ac60d..4769fc0 100644
--- a/lib/+artoa/+controller/+file/loadArtoaIni.m
+++ b/lib/+artoa/+controller/+file/loadArtoaIni.m
@@ -2,7 +2,7 @@ function [] = loadArtoaIni(~, ~)
 %RELOADARTOAINI Loads the artoa.ini file and copies the required values to workspace.
 %   
 
-global artoaConfig artoaWorkspace;
+global artoaConfig artoaWorkspace artoaDataInput;
 
 try
     artoaConfig = artoa.load.tomlini('artoa.ini');
@@ -16,6 +16,15 @@ if isempty(artoaConfig)
     error([mfilename ': artoa.ini has been found but is empty. Please use a valid artoa.ini!']);
 end
 
+% load ibcso if required
+if artoaConfig.topographical_lines.use_ibcso == 1
+    try
+        artoaDataInput.ibcso = load(artoaConfig.files.ibcso);
+    catch ex
+        error([mfilename ': Loading IBCSO file failed. Please make sure that file "' artoaConfig.files.ibcso '" exists!']);
+    end
+end
+
 artoa.controller.addDefaults();
 
 artoa.controller.edit.updateAvailablePlots();
diff --git a/lib/+artoa/+controller/+track/+trajectoryOutput/plotTopographicalLines.m b/lib/+artoa/+controller/+track/+trajectoryOutput/plotTopographicalLines.m
index 5143680..551b44b 100644
--- a/lib/+artoa/+controller/+track/+trajectoryOutput/plotTopographicalLines.m
+++ b/lib/+artoa/+controller/+track/+trajectoryOutput/plotTopographicalLines.m
@@ -14,7 +14,8 @@ if ~artoa.controller.file.loadEtopoFile( ...
 end
 
 %% Get required variables
-topographical_line_values = artoaConfig.defaults.topographical_lines;
+topographical_line_values = artoaConfig.topographical_lines.depth;
+use_ibcso = artoaConfig.topographical_lines.use_ibcso;
 
 %% Plot topographical lines
 
@@ -22,9 +23,10 @@ topographical_line_values = artoaConfig.defaults.topographical_lines;
 axes(pAxesHandle);
 
 hold(pAxesHandle, 'on');
-topographicalLineHandles = cell(length(topographical_line_values), 1);
+topographicalLineHandles = {};
+topoColors = {'r', 'g', 'b', 'm', 'y'};
 
-for o = 1:length(topographicalLineHandles)
+for o = 1:length(topographical_line_values)
     if isnan(topographical_line_values(o)) ...
         | (ischar(topographical_line_values(o)) && strcmp(strip(topographical_line_values(o)), ''))
         continue;
@@ -35,13 +37,42 @@ for o = 1:length(topographicalLineHandles)
         else
             lineValue = topographical_line_values(o);
         end
-        [~, topographicalLineHandles{o}] = contourm( ...
-            artoaDataInput.etopo.Z, ...
-            artoaDataInput.etopo.refvec, ...
-            [lineValue, lineValue], ...
-            'ShowText', 'on', ...
-            'LabelSpacing', 400 ...
-            );
+        
+        if use_ibcso
+            depth_index = find(artoaDataInput.ibcso.contourLevels == lineValue, 1);
+            if isempty(depth_index)
+                warning(['No data has been found for contour line at ' num2str(lineValue) ' in IBCSO dataset. Please double check that the dataset contains the specified contour level.']);
+                continue;
+            end
+            c_data = artoaDataInput.ibcso.cArray{depth_index};
+            lineValueBool = c_data(1, :) == lineValue;
+            idxFilter = c_data(1, :) ~= lineValue;
+            filteredCData = mat2cell(c_data(:, idxFilter), 2, c_data(2, lineValueBool));
+            for cIdx = 1:length(filteredCData)
+                currentData = filteredCData{cIdx};
+                [topographicalLineHandles{end + 1}] = plot( ...
+                    currentData(1, :), ...
+                    currentData(2, :), ...
+                    topoColors{o} ...
+                    );
+            end
+            [~, maxIdx] = max(c_data(2, lineValueBool));
+            textCoordinatesIdx = round(size(filteredCData{maxIdx}, 2)/2);
+            topographicalLineHandles{end + 1} = text( ...
+                filteredCData{maxIdx}(1, textCoordinatesIdx), ...
+                filteredCData{maxIdx}(2, textCoordinatesIdx), ...
+                num2str(lineValue), ...
+                'BackgroundColor', 'white' ...
+                );
+        else
+            [~, topographicalLineHandles{o}] = contourm( ...
+                artoaDataInput.etopo.Z, ...
+                artoaDataInput.etopo.refvec, ...
+                [lineValue, lineValue], ...
+                'ShowText', 'on', ...
+                'LabelSpacing', 400 ...
+                );
+        end
     catch
         warning('There was an error on plotting topographical lines. Kindly ignore this warning if your zoomed area does not contain any topograpical lines.');
     end
diff --git a/lib/+artoa/+controller/+track/+trajectoryOutput/tableTopographicalLinesEdit.m b/lib/+artoa/+controller/+track/+trajectoryOutput/tableTopographicalLinesEdit.m
index 4a69734..d49721e 100644
--- a/lib/+artoa/+controller/+track/+trajectoryOutput/tableTopographicalLinesEdit.m
+++ b/lib/+artoa/+controller/+track/+trajectoryOutput/tableTopographicalLinesEdit.m
@@ -2,10 +2,10 @@ function [] = tableTopographicalLinesEdit(~, ~)
 %UPDATETABLEGENERATEDTRACKS Summary of this function goes here
 %   Detailed explanation goes here
 
-global artoaGui artoaWorkspace;
+global artoaGui artoaWorkspace artoaConfig;
 
 %% Save to workspace
-artoaWorkspace.trajectoryOutput.topographicalLines = artoaGui.trajectoryOutput.tableTopographicalLines.Data;
+artoaConfig.topographical_lines.depth = artoaGui.trajectoryOutput.tableTopographicalLines.Data';
 
 %% Plot if required
 if ~artoaWorkspace.trajectoryOutput.showTopographicalLines
diff --git a/lib/+artoa/+controller/+track/+trajectoryOutput/updateGui.m b/lib/+artoa/+controller/+track/+trajectoryOutput/updateGui.m
index 402917c..f25f9f7 100644
--- a/lib/+artoa/+controller/+track/+trajectoryOutput/updateGui.m
+++ b/lib/+artoa/+controller/+track/+trajectoryOutput/updateGui.m
@@ -2,7 +2,7 @@ function [] = updateGui()
 %UPDATEGUI Summary of this function goes here
 %   Detailed explanation goes here
 
-global artoaWorkspace artoaGui;
+global artoaWorkspace artoaGui artoaConfig;
 
 if artoa.data.hasMember(artoaWorkspace, {'trajectoryOutput', 'updateTrackParameterWindow'})
     artoaGui.trajectoryOutput.checkboxUpdateTrackParameterWindow.Value = ...
@@ -42,7 +42,7 @@ else
 end
 if artoa.data.hasMember(artoaWorkspace, {'trajectoryOutput', 'topographicalLines'})
     artoaGui.trajectoryOutput.tableTopographicalLines.Data = ...
-        artoaWorkspace.trajectoryOutput.topographicalLines;
+        artoaConfig.topographical_lines.depth';
 else
     artoaGui.trajectoryOutput.checkboxShowTopographicalLines.Value = false;
 end
-- 
GitLab