Ejemplo n.º 1
0
static PyObject *MatrixMaker_ice_to_interp(PyMatrixMaker *self, PyObject *args, PyObject *kwds)
{
printf("BEGIN MatrixMaker_ice_to_interp()\n");
    PyObject *ret_py = NULL;
    try {

        // Get arguments
        const char *ice_sheet_name_py;
        PyObject *f2_py;        // Should be [(1 : [...]), (2 : [...])]
        static char const *keyword_list[] = {"sheetname", "f2", NULL};

        if (!PyArg_ParseTupleAndKeywords(
            args, kwds, "sO",
            const_cast<char **>(keyword_list),
            &ice_sheet_name_py, &f2_py))
        {
            // Throw an exception...
            PyErr_SetString(PyExc_ValueError,
                "Bad arguments for ice_to_interp().");
            return 0;
        }

        glint2::MatrixMaker *maker = self->maker.get();
        std::string const ice_sheet_name(ice_sheet_name_py);
        IceSheet *sheet = (*maker)[ice_sheet_name];

        int dims[1] = {sheet->n2()};
        auto f2(giss::py_to_blitz<double,1>(f2_py, "f2", 1, dims));

        auto ret(sheet->ice_to_interp(f2));

        ret_py = giss::blitz_to_py(ret);
        return ret_py;
    } catch(...) {
        if (ret_py) Py_DECREF(ret_py);
        PyErr_SetString(PyExc_ValueError, "Error in MatrixMaker_ice_to_hp()");
        return 0;
    }
}
Ejemplo n.º 2
0
static PyObject *MatrixMaker_iceinterp_to_hp(PyMatrixMaker *self, PyObject *args, PyObject *kwds)
{
printf("BEGIN MatrixMaker_ice_to_hp()\n");
    PyObject *ret_py = NULL;
    try {

        // Get arguments
        PyObject *f2s_py;       // Should be [(1 : [...]), (2 : [...])]
        PyObject *initial_py = NULL;        // array[n3]
        const char *src_py = "ICE";
        const char *qp_algorithm_py = "SINGLE_QP";
        static char const *keyword_list[] = {"f2s", "initial3", "src", "qp_algorithm", NULL};

        if (!PyArg_ParseTupleAndKeywords(
            args, kwds, "O|Oss",
            const_cast<char **>(keyword_list),
            &f2s_py,
            &initial_py, &src_py, &qp_algorithm_py)) {
            // Throw an exception...
            PyErr_SetString(PyExc_ValueError,
                "Bad arguments for ice_to_hp().");
            return 0;
        }

        auto src(giss::parse_enum<IceInterp>(src_py));
        auto qp_algorithm(giss::parse_enum<QPAlgorithm>(qp_algorithm_py));

        if (!PyList_Check(f2s_py)) {
            PyErr_SetString(PyExc_ValueError,
                "Argument must be a list.");
            return 0;
        }


        // Convert the Python dict to a C++ dict
        std::map<int, blitz::Array<double,1>> f2s;
        Py_ssize_t len = PyList_Size(f2s_py);
        for (int i=0; i < len; ++i) {
            PyObject *ii = PyList_GetItem(f2s_py, i);
            if (!PyTuple_Check(ii)) {
                PyErr_SetString(PyExc_ValueError,
                    "List must contain tuples");
                return 0;
            }
            char *sheetname_py;
            PyObject *f2_py;
            PyArg_ParseTuple(ii, "sO", &sheetname_py, &f2_py);
printf("MatrixMaker_ice_to_hp(): Adding %s\n", sheetname_py);
            IceSheet *sheet = (*self->maker)[std::string(sheetname_py)];

            int dims[1] = {src == IceInterp::ICE ? sheet->n2() : sheet->n4()};
            auto f2(giss::py_to_blitz<double,1>(f2_py, "f2", 1, dims));

            f2s.insert(std::make_pair(sheet->index, f2));

        }

        // Get the blitz array from python
        int dims[1] = {self->maker->n3()};
        blitz::Array<double,1> initial;
        if (initial_py) {
            initial.reference(giss::py_to_blitz<double,1>(initial_py, "initial", 1, dims));
        }

        // Call!
        giss::VectorSparseVector<int, double> f3(
            self->maker->iceinterp_to_hp(f2s, initial, src, qp_algorithm));

        // Copy output for return
        blitz::Array<double,1> ret(self->maker->n3());
        giss::to_blitz(f3, ret);
#if 0
        ret = 0;
        for (auto ii = f3.begin(); ii != f3.end(); ++ii) {
            int i3 = ii->first;
            double val = ii->second;
            ret(i3) = val;
        }
#endif

        ret_py = giss::blitz_to_py(ret);
        return ret_py;
    } catch(...) {
        if (ret_py) Py_DECREF(ret_py);
        PyErr_SetString(PyExc_ValueError, "Error in MatrixMaker_ice_to_hp()");
        return 0;
    }
}