diff --git a/gwinc/__init__.py b/gwinc/__init__.py index 12cd527da86e18725e3b5aaebaca716a4957e400..b8d7d0293ec1a1143979458be6cb85e4f58c9449 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 568378e8988874618975de937475266e59d810a1..32eac20ac83fcd8162c9eb086c285499b2ada58f 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']