From 7370169b8d2da27453cfd3c7bca5597e9bf04646 Mon Sep 17 00:00:00 2001
From: Jameson Graef Rollins <jrollins@finestructure.net>
Date: Mon, 11 Mar 2019 11:27:02 -0700
Subject: [PATCH] convenience load_struct() function to load a Struct from yaml
 or mat file

---
 gwinc/__init__.py |  1 +
 gwinc/struct.py   | 28 ++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/gwinc/__init__.py b/gwinc/__init__.py
index 12cd527d..b8d7d029 100644
--- a/gwinc/__init__.py
+++ b/gwinc/__init__.py
@@ -1,3 +1,4 @@
+from .struct import load_struct
 from .ifo import load_ifo
 from .precomp import precompIFO
 from .gwinc import noise_calc
diff --git a/gwinc/struct.py b/gwinc/struct.py
index 568378e8..32eac20a 100644
--- a/gwinc/struct.py
+++ b/gwinc/struct.py
@@ -324,3 +324,31 @@ class Struct(object):
                 return cls.from_matstruct(s)
             else:
                 raise IOError("Unknown file type: {}".format(ext))
+
+
+def load_struct(path):
+    """Load struct from YAML or MATLAB file.
+
+    Files may be either .yaml, .mat or .m.  For .m files, the file is
+    expected to include either an object or function that corresponds
+    to the basename of the file.  The MATLAB engine will be invoked to
+    execute the .m code and extract the resultant IFO data.
+
+    """
+    root, ext = os.path.splitext(path)
+
+    if ext == '.m':
+        from ..gwinc_matlab import Matlab
+        matlab = Matlab()
+        matlab.addpath(os.path.dirname(path))
+        func_name = os.path.basename(root)
+        matlab.eval("ifo = {};".format(func_name), nargout=0)
+        ifo = matlab.extract('ifo')
+        return Struct.from_matstruct(ifo)
+
+    else:
+        return Struct.from_file(path)
+
+
+# accepted extension types for struct files
+STRUCT_EXT = ['.yaml', '.yml', '.mat', '.m']
-- 
GitLab