From c04b0b7e7c91e22ea41389d9ae2efea1cb1f4910 Mon Sep 17 00:00:00 2001
From: Dmitri Naumov <dmitri.naumov@ufz.de>
Date: Wed, 19 May 2021 16:35:45 +0200
Subject: [PATCH] [App/IO] Surfer raster header returns an optional.

---
 Applications/FileIO/AsciiRasterInterface.cpp | 24 ++++++++------------
 Applications/FileIO/AsciiRasterInterface.h   |  8 ++++---
 2 files changed, 15 insertions(+), 17 deletions(-)

diff --git a/Applications/FileIO/AsciiRasterInterface.cpp b/Applications/FileIO/AsciiRasterInterface.cpp
index 82efa50e06a..ad5877b8d7a 100644
--- a/Applications/FileIO/AsciiRasterInterface.cpp
+++ b/Applications/FileIO/AsciiRasterInterface.cpp
@@ -168,13 +168,9 @@ GeoLib::Raster* AsciiRasterInterface::getRasterFromSurferFile(
         return nullptr;
     }
 
-    // header information
-    GeoLib::RasterHeader header;
-    double min(0.0);
-    double max(0.0);
-
-    if (readSurferHeader(in, header, min, max))
+    if (auto const optional_header = readSurferHeader(in))
     {
+        auto const [header, min, max] = *optional_header;
         const double no_data_val(min - 1);
         std::vector<double> values(header.n_cols * header.n_rows);
         std::string s;
@@ -191,17 +187,15 @@ GeoLib::Raster* AsciiRasterInterface::getRasterFromSurferFile(
             }
         }
         in.close();
-        return new GeoLib::Raster(std::move(header), values.begin(),
-                                  values.end());
+        return new GeoLib::Raster(header, values.begin(), values.end());
     }
     ERR("Raster::getRasterFromASCFile() - could not read header of file {:s}",
         fname);
     return nullptr;
 }
 
-bool AsciiRasterInterface::readSurferHeader(std::ifstream& in,
-                                            GeoLib::RasterHeader& header,
-                                            double& min, double& max)
+std::optional<std::tuple<GeoLib::RasterHeader, double, double>>
+AsciiRasterInterface::readSurferHeader(std::ifstream& in)
 {
     std::string tag;
 
@@ -210,10 +204,12 @@ bool AsciiRasterInterface::readSurferHeader(std::ifstream& in,
     if (tag != "DSAA")
     {
         ERR("Error in readSurferHeader() - No Surfer file.");
-        return false;
+        return {};
     }
 
+    GeoLib::RasterHeader header;
     in >> header.n_cols >> header.n_rows;
+    double min, max;
     in >> min >> max;
     header.origin[0] = min;
     header.cell_size = (max - min) / static_cast<double>(header.n_cols);
@@ -230,13 +226,13 @@ bool AsciiRasterInterface::readSurferHeader(std::ifstream& in,
     else
     {
         ERR("Error in readSurferHeader() - Anisotropic cellsize detected.");
-        return false;
+        return {};
     }
     header.n_depth = 1;
     header.no_data = min - 1;
     in >> min >> max;
 
-    return true;
+    return {{header, min, max}};
 }
 
 void AsciiRasterInterface::writeRasterAsASC(GeoLib::Raster const& raster,
diff --git a/Applications/FileIO/AsciiRasterInterface.h b/Applications/FileIO/AsciiRasterInterface.h
index cbaae86112d..b6d6dfd7497 100644
--- a/Applications/FileIO/AsciiRasterInterface.h
+++ b/Applications/FileIO/AsciiRasterInterface.h
@@ -16,6 +16,7 @@
 #include <fstream>
 #include <optional>
 #include <string>
+#include <tuple>
 #include <vector>
 
 #include "GeoLib/Raster.h"
@@ -47,9 +48,10 @@ private:
     /// If the return value is empty, reading was not successful.
     static std::optional<GeoLib::RasterHeader> readASCHeader(std::ifstream& in);
 
-    /// Reads the header of a Surfer grd-file.
-    static bool readSurferHeader(std::ifstream &in, GeoLib::RasterHeader &header,
-                                 double &min, double &max);
+    /// Reads the header of a Surfer grd-file with minimum and maximum values.
+    /// If the return value is empty, reading was not successful.
+    static std::optional<std::tuple<GeoLib::RasterHeader, double, double>>
+    readSurferHeader(std::ifstream& in);
 };
 
 /// Reads a vector of rasters given by file names. On error nothing is returned,
-- 
GitLab