From 32993abe526cd8e99b0f8f2d9e1715a46cba7e01 Mon Sep 17 00:00:00 2001
From: Jameson Graef Rollins <jrollins@finestructure.net>
Date: Sat, 12 May 2018 10:47:16 -0700
Subject: [PATCH] support loading ifo from IFOModel .m script

Assumes the IFOModel is given as function in .m script, and use Matlab
engine interface to execute and load.
---
 gwinc/__main__.py     |  4 ++--
 gwinc/ifo/__init__.py | 28 +++++++++++++++++++++++-----
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/gwinc/__main__.py b/gwinc/__main__.py
index 0ea5aa1..4856987 100644
--- a/gwinc/__main__.py
+++ b/gwinc/__main__.py
@@ -62,8 +62,8 @@ group.add_argument('--interactive', '-i', action='store_true',
                    help="open interactive shell when plotting")
 group.add_argument('--no-plot', '-np', action='store_false', dest='plot',
                    help="supress plotting")
-parser.add_argument('IFO', nargs='?', default=IFO,
-                    help="IFO name or description file path (.yaml or .mat)")
+parser.add_argument('IFO', default=IFO,
+                    help="IFO name or description file path (.yaml, .mat, .m)")
 
 
 def main():
diff --git a/gwinc/ifo/__init__.py b/gwinc/ifo/__init__.py
index 71e6ce1..7ea1c79 100644
--- a/gwinc/ifo/__init__.py
+++ b/gwinc/ifo/__init__.py
@@ -2,6 +2,8 @@ import os
 import fnmatch
 
 from ..struct import Struct
+from ..gwinc_matlab import Matlab
+
 
 def available_ifos():
     """List available included IFO files"""
@@ -15,10 +17,15 @@ def available_ifos():
 def load_ifo(name_or_path):
     """Load IFO by name or from file.
 
-    IFO names will correspond to basename of included .yaml IFO
-    definition file.
+    Named IFOs should correspond to the basename of .yaml IFO
+    definition files included with pygwinc (see available_ifos()
+    above).
 
-    When specifying path may be either .yaml or .mat.
+    When specifying by path 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.
 
     """
     if os.path.exists(name_or_path):
@@ -26,5 +33,16 @@ def load_ifo(name_or_path):
     else:
         path = os.path.join(os.path.dirname(__file__),
                             name_or_path+'.yaml')
-    s = Struct.from_file(path)
-    return s
+
+    (root, ext) = os.path.splitext(path)
+
+    if ext == '.m':
+        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)
-- 
GitLab