예제 #1
0
파일: _cmormodule.c 프로젝트: PCMDI/cmor
static PyObject *PyCMOR_grid_mapping(PyObject * self, PyObject * args)
{
    signal(signal_to_catch, signal_handler);
    int ierr;
    PyObject *param_nm_obj, *param_val_obj, *param_un_obj, *tmp;
    PyArrayObject *param_val_arr = NULL;
    void *param_val;
    char *name;
    int gid, i, n;
    char nms[CMOR_MAX_GRID_ATTRIBUTES][CMOR_MAX_STRING];
    char units[CMOR_MAX_GRID_ATTRIBUTES][CMOR_MAX_STRING];

    /* HUGE assumtion here is that the data is contiguous! */
    if (!PyArg_ParseTuple
        (args, "isOOO", &gid, &name, &param_nm_obj, &param_val_obj,
         &param_un_obj))
        return NULL;

    param_val_arr =
      (PyArrayObject *) PyArray_ContiguousFromObject(param_val_obj,
                                                     NPY_NOTYPE, 1, 0);
    param_val = PyArray_DATA(param_val_arr);

    n = PyList_Size(param_nm_obj);
    for (i = 0; i < n; i++) {
        tmp = PyList_GetItem(param_nm_obj, i);
#if PY_MAJOR_VERSION >= 3
        strcpy(nms[i], PyBytes_AsString(tmp));
#else
        strcpy(nms[i], PyString_AsString(tmp));
#endif
        //Py_DECREF(tmp); //Not needed get_item does not increase ref
        tmp = PyList_GetItem(param_un_obj, i);
#if PY_MAJOR_VERSION >= 3
        strcpy(units[i], PyBytes_AsString(tmp));
#else
        strcpy(units[i], PyString_AsString(tmp));
#endif
        //Py_DECREF(tmp); // Not need get_item does not incref
    }

    ierr =
      cmor_set_grid_mapping(gid, name, n, (char *)nms,
                            CMOR_MAX_STRING, param_val,
                            (char *)units, CMOR_MAX_STRING);

    if (param_val_arr != NULL) {
        Py_DECREF(param_val_arr);
    }

    if (ierr != 0 || raise_exception) {
        raise_exception = 0;
        PyErr_Format(CMORError, exception_message, "grid_mapping");
        return NULL;
    }

    return (Py_BuildValue("i", ierr));
}
예제 #2
0
파일: _cmormodule.c 프로젝트: AZed/uvcdat
static PyObject *
  PyCMOR_grid_mapping(PyObject *self,PyObject *args)
{
  int ierr;
  PyObject *param_nm_obj,*param_val_obj,*param_un_obj,*tmp;
  PyArrayObject *param_val_arr=NULL;
  void *param_val;
  char *name;
  int gid,i,n;
  char nms[CMOR_MAX_GRID_ATTRIBUTES][CMOR_MAX_STRING];
  char units[CMOR_MAX_GRID_ATTRIBUTES][CMOR_MAX_STRING];
  /* HUGE assumtion here is that the data is contiguous! */
  if (!PyArg_ParseTuple(args,"isOOO",&gid,&name,&param_nm_obj,&param_val_obj,&param_un_obj))
    return NULL;

  param_val_arr = (PyArrayObject *) PyArray_ContiguousFromObject(param_val_obj,PyArray_NOTYPE,1,0);
  param_val = param_val_arr->data;
  
  n = PyList_Size(param_nm_obj);
  for(i=0;i<n;i++) {
    tmp  =PyList_GetItem(param_nm_obj,i);
    strcpy(nms[i],PyString_AsString(tmp));
    Py_DECREF(tmp);
    tmp  =PyList_GetItem(param_un_obj,i);
    strcpy(units[i],PyString_AsString(tmp));
    Py_DECREF(tmp);
  }
  
  ierr = cmor_set_grid_mapping(gid,name,n,(char **)nms,CMOR_MAX_STRING,param_val,(char **)units,CMOR_MAX_STRING);

  if (param_val_arr!=NULL) {Py_DECREF(param_val_arr);}

  if (ierr!=0) {
    return NULL;
  }
  else {
    /* Return NULL Python Object */
    Py_INCREF(Py_None);
    return Py_None;
  }
}