diff --git a/qpms/qpms_c.pyx b/qpms/qpms_c.pyx index 893c203..96974ca 100644 --- a/qpms/qpms_c.pyx +++ b/qpms/qpms_c.pyx @@ -1134,6 +1134,8 @@ cdef class TMatrixInterpolator: def __cinit__(self, filename, BaseSpec bspec, *args, **kwargs): '''Creates a T-matrix interpolator object from a scuff-tmatrix output''' + global qpms_load_scuff_tmatrix_crash_on_failure + qpms_load_scuff_tmatrix_crash_on_failure = False self.spec = bspec cdef char * cpath = make_c_string(filename) if QPMS_SUCCESS != qpms_load_scuff_tmatrix(cpath, self.spec.rawpointer(), diff --git a/qpms/qpms_cdefs.pxd b/qpms/qpms_cdefs.pxd index d315cb3..c03c7dc 100644 --- a/qpms/qpms_cdefs.pxd +++ b/qpms/qpms_cdefs.pxd @@ -276,6 +276,7 @@ cdef extern from "gsl/gsl_interp.h": # ^^^ These are probably the only relevant ones. cdef extern from "tmatrices.h": + bint qpms_load_scuff_tmatrix_crash_on_failure struct qpms_tmatrix_interpolator_t: const qpms_vswf_set_spec_t *bspec struct qpms_permittivity_interpolator_t: diff --git a/qpms/tmatrices.c b/qpms/tmatrices.c index 567ffce..f5bbbb7 100644 --- a/qpms/tmatrices.c +++ b/qpms/tmatrices.c @@ -18,6 +18,7 @@ #include "tmatrices.h" #include "qpms_specfunc.h" #include "normalisation.h" +#include #define HBAR (1.05457162825e-34) #define ELECTRONVOLT (1.602176487e-19) @@ -443,6 +444,8 @@ qpms_errno_t qpms_read_scuff_tmatrix( return QPMS_SUCCESS; } +bool qpms_load_scuff_tmatrix_crash_on_failure = true; + qpms_errno_t qpms_load_scuff_tmatrix( const char *path, ///< file path const qpms_vswf_set_spec_t * bs, ///< VSWF set spec @@ -453,9 +456,11 @@ qpms_errno_t qpms_load_scuff_tmatrix( complex double ** const tmdata ) { FILE *f = fopen(path, "r"); - if (!f) - qpms_pr_error_at_line(__FILE__, __LINE__, __func__, - "Could not open T-matrix file %s", path); + if (!f) + if (qpms_load_scuff_tmatrix_crash_on_failure) + qpms_pr_error_at_line(__FILE__, __LINE__, __func__, + "Could not open T-matrix file %s", path); + else return errno; qpms_errno_t retval = qpms_read_scuff_tmatrix(f, bs, n, freqs, freqs_su, tmatrices_array, tmdata); diff --git a/qpms/tmatrices.h b/qpms/tmatrices.h index f77d4e4..b4aa876 100644 --- a/qpms/tmatrices.h +++ b/qpms/tmatrices.h @@ -139,6 +139,17 @@ qpms_errno_t qpms_load_scuff_tmatrix( complex double **tmdata ///< The T-matrices raw contents ); +/// Tells whether qpms_load_scuff_tmatrix should crash if fopen() fails. +/** If true (default), the function causes the program + * die e.g. when the tmatrix file + * does not exists. + * + * If false, it does nothing and returns an appropriate error value instead. + * This is desirable e.g. when used in Python (so that proper exception can + * be thrown). + */ +extern bool qpms_load_scuff_tmatrix_crash_on_failure; + /// Loads a scuff-tmatrix generated file. /** A simple wrapper over qpms_read_scuff_tmatrix() that needs a * path instead of open FILE.