From 423ba3c3862fb6ea30115ebbbbe02079250f1039 Mon Sep 17 00:00:00 2001
From: David Robert Verelst <dave@dtu.dk>
Date: Tue, 28 Nov 2017 19:45:01 +0100
Subject: [PATCH] add gtsdf support to prepost.windIO.LoadResults and
 Hawc2io.ReadHawc2

---
 wetb/hawc2/Hawc2io.py  | 30 ++++++++++++++++++++++++++----
 wetb/prepost/windIO.py | 36 +++++++++++++++++++++---------------
 2 files changed, 47 insertions(+), 19 deletions(-)

diff --git a/wetb/hawc2/Hawc2io.py b/wetb/hawc2/Hawc2io.py
index ac8d28c..5bd39ca 100644
--- a/wetb/hawc2/Hawc2io.py
+++ b/wetb/hawc2/Hawc2io.py
@@ -41,6 +41,8 @@ from builtins import object
 import numpy as np
 import os
 
+from wetb import gtsdf
+
 # FIXME: numpy doesn't like io.open binary fid in PY27, why is that? As a hack
 # workaround, use opent for PY23 compatibility when handling text files,
 # and default open for binary
@@ -82,9 +84,11 @@ class ReadHawc2(object):
         # findes general result info (number of scans, number of channels,
         # simulation time and file format)
         temp = Lines[8].split()
-        self.NrSc = int(temp[0]); self.NrCh = int(temp[1])
-        self.Time = float(temp[2]); self.Freq = self.NrSc / self.Time;
-        self.t = np.linspace(0, self.Time, self.NrSc + 1)[1:];
+        self.NrSc = int(temp[0])
+        self.NrCh = int(temp[1])
+        self.Time = float(temp[2])
+        self.Freq = self.NrSc / self.Time
+        self.t = np.linspace(0, self.Time, self.NrSc + 1)[1:]
         Format = temp[3]
         # reads channel info (name, unit and description)
         Name = []; Unit = []; Description = [];
@@ -151,6 +155,8 @@ class ReadHawc2(object):
         elif os.path.isfile(self.FileName + ".int"):
              self.FileFormat = 'FLEX'
              self._ReadSensorFile()
+        elif os.path.isfile(self.FileName + ".hdf5"):
+            self.FileFormat = 'GTSDF'
         else:
             print ("unknown file: " + FileName)
 ################################################################################
@@ -184,14 +190,30 @@ class ReadHawc2(object):
         fid.close()
         return np.dot(temp[:, ChVec], np.diag(self.ScaleFactor[ChVec]))
 ################################################################################
+# Read results in GTSD format
+    def ReadGtsdf(self):
+        self.t, data, info = gtsdf.load(self.FileName + '.hdf5')
+        self.Time = self.t[-1]
+        self.ChInfo = [info['attribute_names'],
+                       info['attribute_units'],
+                       info['attribute_descriptions']]
+        self.NrCh = data.shape[1]
+        self.NrSc = data.shape[0]
+        self.Freq = self.NrSc / self.Time
+        self.FileFormat = 'GTSDF'
+        self.gtsdf_description = info['description']
+        return data
+################################################################################
 # One stop call for reading all data formats
     def ReadAll(self, ChVec=[]):
-        if not ChVec:
+        if not ChVec and not self.FileFormat == 'GTSDF':
             ChVec = range(0, self.NrCh)
         if self.FileFormat == 'HAWC2_BINARY':
             return self.ReadBinary(ChVec)
         elif self.FileFormat == 'HAWC2_ASCII':
             return self.ReadAscii(ChVec)
+        elif self.FileFormat == 'GTSDF':
+            return self.ReadGtsdf()
         else:
             return self.ReadFLEX(ChVec)
 ################################################################################
diff --git a/wetb/prepost/windIO.py b/wetb/prepost/windIO.py
index 2e4c71b..429320d 100755
--- a/wetb/prepost/windIO.py
+++ b/wetb/prepost/windIO.py
@@ -550,29 +550,35 @@ class LoadResults(ReadHawc2):
 
         self.file_path = file_path
         # remove .log, .dat, .sel extensions who might be accedental left
-        if file_name[-4:] in ['.htc', '.sel', '.dat', '.log']:
-            file_name = file_name[:-4]
+        ext = file_name.split('.')[-1]
+        if ext in ['htc', 'sel', 'dat', 'log', 'hdf5']:
+            file_name = file_name.replace('.' + ext, '')
         # FIXME: since HAWC2 will always have lower case output files, convert
         # any wrongly used upper case letters to lower case here
         self.file_name = file_name
         FileName = os.path.join(self.file_path, self.file_name)
 
-        ReadOnly = 0 if readdata else 1
-        super(LoadResults, self).__init__(FileName, ReadOnly=ReadOnly)
-        self.FileType = self.FileFormat[6:]
-        self.N = int(self.NrSc)
-        self.Nch = int(self.NrCh)
-        self.ch_details = np.ndarray(shape=(self.Nch, 3), dtype='<U100')
-        for ic in range(self.Nch):
-            self.ch_details[ic, 0] = self.ChInfo[0][ic]
-            self.ch_details[ic, 1] = self.ChInfo[1][ic]
-            self.ch_details[ic, 2] = self.ChInfo[2][ic]
+        super(LoadResults, self).__init__(FileName, ReadOnly=readdata)
+        self.FileType = self.FileFormat
+        if self.FileType.find('HAWC2_'):
+            self.FileType = self.FileType[6:]
 
-        ChVec = [] if usecols is None else usecols
+        if readdata:
+            ChVec = [] if usecols is None else usecols
+            self.sig = self.ReadAll(ChVec=ChVec)
+
+        # info in sel file is not available when not reading gtsdf
+        # so this is only skipped when readdata is false and FileType is gtsdf
+        if not (not readdata and (self.FileType == 'GTSDF')):
+            self.N = int(self.NrSc)
+            self.Nch = int(self.NrCh)
+            self.ch_details = np.ndarray(shape=(self.Nch, 3), dtype='<U100')
+            for ic in range(self.Nch):
+                self.ch_details[ic, 0] = self.ChInfo[0][ic]
+                self.ch_details[ic, 1] = self.ChInfo[1][ic]
+                self.ch_details[ic, 2] = self.ChInfo[2][ic]
 
         self._unified_channel_names()
-        if readdata:
-            self.sig = super(LoadResults, self).__call__(ChVec=ChVec)
 
         if self.debug:
             stop = time() - start
-- 
GitLab