/* File: convolvemodule.c
 * This file is auto-generated with f2py (version:2).
 * f2py is a Fortran to Python Interface Generator (FPIG), Second Edition,
 * written by Pearu Peterson <pearu@cens.ioc.ee>.
 * See http://cens.ioc.ee/projects/f2py2e/
 * Generation date: Thu Feb 14 14:04:38 2013
 * $Revision:$
 * $Date:$
 * Do not edit this file directly unless you know what you are doing!!!
 */
#ifdef __cplusplus
extern "C" {
#endif

/*********************** See f2py2e/cfuncs.py: includes ***********************/
#include "Python.h"
#include <stdarg.h>
#include "fortranobject.h"
#include <math.h>

/**************** See f2py2e/rules.py: mod_rules['modulebody'] ****************/
static PyObject *convolve_error;
static PyObject *convolve_module;

/*********************** See f2py2e/cfuncs.py: typedefs ***********************/
/*need_typedefs*/

/****************** See f2py2e/cfuncs.py: typedefs_generated ******************/
/*need_typedefs_generated*/

/********************** See f2py2e/cfuncs.py: cppmacros **********************/
#define rank(var) var ## _Rank
#define shape(var,dim) var ## _Dims[dim]
#define old_rank(var) (((PyArrayObject *)(capi_ ## var ## _tmp))->nd)
#define old_shape(var,dim) (((PyArrayObject *)(capi_ ## var ## _tmp))->dimensions[dim])
#define fshape(var,dim) shape(var,rank(var)-dim-1)
#define len(var) shape(var,0)
#define flen(var) fshape(var,0)
#define old_size(var) PyArray_SIZE((PyArrayObject *)(capi_ ## var ## _tmp))
/* #define index(i) capi_i ## i */
#define slen(var) capi_ ## var ## _len
#define size(var, ...) f2py_size((PyArrayObject *)(capi_ ## var ## _tmp), ## __VA_ARGS__, -1)

#ifdef DEBUGCFUNCS
#define CFUNCSMESS(mess) fprintf(stderr,"debug-capi:"mess);
#define CFUNCSMESSPY(mess,obj) CFUNCSMESS(mess) \
  PyObject_Print((PyObject *)obj,stderr,Py_PRINT_RAW);\
  fprintf(stderr,"\n");
#else
#define CFUNCSMESS(mess)
#define CFUNCSMESSPY(mess,obj)
#endif

#ifndef max
#define max(a,b) ((a > b) ? (a) : (b))
#endif
#ifndef min
#define min(a,b) ((a < b) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a,b) ((a > b) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a,b) ((a < b) ? (a) : (b))
#endif

#if defined(PREPEND_FORTRAN)
#if defined(NO_APPEND_FORTRAN)
#if defined(UPPERCASE_FORTRAN)
#define F_FUNC(f,F) _##F
#else
#define F_FUNC(f,F) _##f
#endif
#else
#if defined(UPPERCASE_FORTRAN)
#define F_FUNC(f,F) _##F##_
#else
#define F_FUNC(f,F) _##f##_
#endif
#endif
#else
#if defined(NO_APPEND_FORTRAN)
#if defined(UPPERCASE_FORTRAN)
#define F_FUNC(f,F) F
#else
#define F_FUNC(f,F) f
#endif
#else
#if defined(UPPERCASE_FORTRAN)
#define F_FUNC(f,F) F##_
#else
#define F_FUNC(f,F) f##_
#endif
#endif
#endif
#if defined(UNDERSCORE_G77)
#define F_FUNC_US(f,F) F_FUNC(f##_,F##_)
#else
#define F_FUNC_US(f,F) F_FUNC(f,F)
#endif


/************************ See f2py2e/cfuncs.py: cfuncs ************************/
int f2py_size(PyArrayObject* var, ...)
{
  npy_int sz = 0;
  npy_int dim;
  npy_int rank;
  va_list argp;
  va_start(argp, var);
  dim = va_arg(argp, npy_int);
  if (dim==-1)
    {
      sz = PyArray_SIZE(var);
    }
  else
    {
      rank = PyArray_NDIM(var);
      if (dim>=1 && dim<=rank)
        sz = PyArray_DIM(var, dim-1);
      else
        fprintf(stderr, "f2py_size: 2nd argument value=%d fails to satisfy 1<=value<=%d. Result will be 0.\n", dim, rank);
    }
  va_end(argp);
  return sz;
}

static int double_from_pyobj(double* v,PyObject *obj,const char *errmess) {
  PyObject* tmp = NULL;
  if (PyFloat_Check(obj)) {
#ifdef __sgi
    *v = PyFloat_AsDouble(obj);
#else
    *v = PyFloat_AS_DOUBLE(obj);
#endif
    return 1;
  }
  tmp = PyNumber_Float(obj);
  if (tmp) {
#ifdef __sgi
    *v = PyFloat_AsDouble(tmp);
#else
    *v = PyFloat_AS_DOUBLE(tmp);
#endif
    Py_DECREF(tmp);
    return 1;
  }
  if (PyComplex_Check(obj))
    tmp = PyObject_GetAttrString(obj,"real");
  else if (PyString_Check(obj) || PyUnicode_Check(obj))
    /*pass*/;
  else if (PySequence_Check(obj))
    tmp = PySequence_GetItem(obj,0);
  if (tmp) {
    PyErr_Clear();
    if (double_from_pyobj(v,tmp,errmess)) {Py_DECREF(tmp); return 1;}
    Py_DECREF(tmp);
  }
  {
    PyObject* err = PyErr_Occurred();
    if (err==NULL) err = convolve_error;
    PyErr_SetString(err,errmess);
  }
  return 0;
}


/********************* See f2py2e/cfuncs.py: userincludes *********************/
/*need_userincludes*/

/********************* See f2py2e/capi_rules.py: usercode *********************/


/* See f2py2e/rules.py */
extern void discreteconv(double*,int,double*,int,double,double,double,double,double*);
/*eof externroutines*/

/******************** See f2py2e/capi_rules.py: usercode1 ********************/


/******************* See f2py2e/cb_rules.py: buildcallback *******************/
/*need_callbacks*/

/*********************** See f2py2e/rules.py: buildapi ***********************/

/******************************** discreteconv ********************************/
static char doc_f2py_rout_convolve_discreteconv[] = "\
Function signature:\n\
  out = discreteconv(times,kern,kdt,onset,offset,odt)\n\
Required arguments:\n"
"  times : input rank-1 array('d') with bounds (ntimes)\n"
"  kern : input rank-1 array('d') with bounds (nkern)\n"
"  kdt : input float\n"
"  onset : input float\n"
"  offset : input float\n"
"  odt : input float\n"
"Return objects:\n"
"  out : rank-1 array('d') with bounds (ceil((offset-onset)/odt))";
/* extern void discreteconv(double*,int,double*,int,double,double,double,double,double*); */
static PyObject *f2py_rout_convolve_discreteconv(const PyObject *capi_self,
                           PyObject *capi_args,
                           PyObject *capi_keywds,
                           void (*f2py_func)(double*,int,double*,int,double,double,double,double,double*)) {
  PyObject * volatile capi_buildvalue = NULL;
  volatile int f2py_success = 1;
/*decl*/

  double *times = NULL;
  npy_intp times_Dims[1] = {-1};
  const int times_Rank = 1;
  PyArrayObject *capi_times_tmp = NULL;
  int capi_times_intent = 0;
  PyObject *times_capi = Py_None;
  int ntimes = 0;
  double *kern = NULL;
  npy_intp kern_Dims[1] = {-1};
  const int kern_Rank = 1;
  PyArrayObject *capi_kern_tmp = NULL;
  int capi_kern_intent = 0;
  PyObject *kern_capi = Py_None;
  int nkern = 0;
  double kdt = 0;
  PyObject *kdt_capi = Py_None;
  double onset = 0;
  PyObject *onset_capi = Py_None;
  double offset = 0;
  PyObject *offset_capi = Py_None;
  double odt = 0;
  PyObject *odt_capi = Py_None;
  double *out = NULL;
  npy_intp out_Dims[1] = {-1};
  const int out_Rank = 1;
  PyArrayObject *capi_out_tmp = NULL;
  int capi_out_intent = 0;
  static char *capi_kwlist[] = {"times","kern","kdt","onset","offset","odt",NULL};

/*routdebugenter*/
#ifdef F2PY_REPORT_ATEXIT
f2py_start_clock();
#endif
  if (!PyArg_ParseTupleAndKeywords(capi_args,capi_keywds,\
    "OOOOOO:convolve.discreteconv",\
    capi_kwlist,&times_capi,&kern_capi,&kdt_capi,&onset_capi,&offset_capi,&odt_capi))
    return NULL;
/*frompyobj*/
  /* Processing variable kern */
  ;
  capi_kern_intent |= F2PY_INTENT_IN|F2PY_INTENT_C;
  capi_kern_tmp = array_from_pyobj(PyArray_DOUBLE,kern_Dims,kern_Rank,capi_kern_intent,kern_capi);
  if (capi_kern_tmp == NULL) {
    if (!PyErr_Occurred())
      PyErr_SetString(convolve_error,"failed in converting 2nd argument `kern' of convolve.discreteconv to C/Fortran array" );
  } else {
    kern = (double *)(capi_kern_tmp->data);

  /* Processing variable odt */
    f2py_success = double_from_pyobj(&odt,odt_capi,"convolve.discreteconv() 6th argument (odt) can't be converted to double");
  if (f2py_success) {
  /* Processing variable times */
  ;
  capi_times_intent |= F2PY_INTENT_IN|F2PY_INTENT_C;
  capi_times_tmp = array_from_pyobj(PyArray_DOUBLE,times_Dims,times_Rank,capi_times_intent,times_capi);
  if (capi_times_tmp == NULL) {
    if (!PyErr_Occurred())
      PyErr_SetString(convolve_error,"failed in converting 1st argument `times' of convolve.discreteconv to C/Fortran array" );
  } else {
    times = (double *)(capi_times_tmp->data);

  /* Processing variable onset */
    f2py_success = double_from_pyobj(&onset,onset_capi,"convolve.discreteconv() 4th argument (onset) can't be converted to double");
  if (f2py_success) {
  /* Processing variable offset */
    f2py_success = double_from_pyobj(&offset,offset_capi,"convolve.discreteconv() 5th argument (offset) can't be converted to double");
  if (f2py_success) {
  /* Processing variable kdt */
    f2py_success = double_from_pyobj(&kdt,kdt_capi,"convolve.discreteconv() 3rd argument (kdt) can't be converted to double");
  if (f2py_success) {
  /* Processing variable nkern */
  nkern = len(kern);
  /* Processing variable ntimes */
  ntimes = len(times);
  /* Processing variable out */
  out_Dims[0]=ceil((offset-onset)/odt);
  capi_out_intent |= F2PY_INTENT_OUT|F2PY_INTENT_HIDE|F2PY_INTENT_C;
  capi_out_tmp = array_from_pyobj(PyArray_DOUBLE,out_Dims,out_Rank,capi_out_intent,Py_None);
  if (capi_out_tmp == NULL) {
    if (!PyErr_Occurred())
      PyErr_SetString(convolve_error,"failed in converting hidden `out' of convolve.discreteconv to C/Fortran array" );
  } else {
    out = (double *)(capi_out_tmp->data);

/*end of frompyobj*/
#ifdef F2PY_REPORT_ATEXIT
f2py_start_call_clock();
#endif
/*callfortranroutine*/
        (*f2py_func)(times,ntimes,kern,nkern,kdt,onset,offset,odt,out);
if (PyErr_Occurred())
  f2py_success = 0;
#ifdef F2PY_REPORT_ATEXIT
f2py_stop_call_clock();
#endif
/*end of callfortranroutine*/
    if (f2py_success) {
/*pyobjfrom*/
/*end of pyobjfrom*/
    CFUNCSMESS("Building return value.\n");
    capi_buildvalue = Py_BuildValue("N",capi_out_tmp);
/*closepyobjfrom*/
/*end of closepyobjfrom*/
    } /*if (f2py_success) after callfortranroutine*/
/*cleanupfrompyobj*/
  }  /*if (capi_out_tmp == NULL) ... else of out*/
  /* End of cleaning variable out */
  /* End of cleaning variable ntimes */
  /* End of cleaning variable nkern */
  } /*if (f2py_success) of kdt*/
  /* End of cleaning variable kdt */
  } /*if (f2py_success) of offset*/
  /* End of cleaning variable offset */
  } /*if (f2py_success) of onset*/
  /* End of cleaning variable onset */
  if((PyObject *)capi_times_tmp!=times_capi) {
    Py_XDECREF(capi_times_tmp); }
  }  /*if (capi_times_tmp == NULL) ... else of times*/
  /* End of cleaning variable times */
  } /*if (f2py_success) of odt*/
  /* End of cleaning variable odt */
  if((PyObject *)capi_kern_tmp!=kern_capi) {
    Py_XDECREF(capi_kern_tmp); }
  }  /*if (capi_kern_tmp == NULL) ... else of kern*/
  /* End of cleaning variable kern */
/*end of cleanupfrompyobj*/
  if (capi_buildvalue == NULL) {
/*routdebugfailure*/
  } else {
/*routdebugleave*/
  }
  CFUNCSMESS("Freeing memory.\n");
/*freemem*/
#ifdef F2PY_REPORT_ATEXIT
f2py_stop_clock();
#endif
  return capi_buildvalue;
}
/**************************** end of discreteconv ****************************/
/*eof body*/

/******************* See f2py2e/f90mod_rules.py: buildhooks *******************/
/*need_f90modhooks*/

/************** See f2py2e/rules.py: module_rules['modulebody'] **************/

/******************* See f2py2e/common_rules.py: buildhooks *******************/

/*need_commonhooks*/

/**************************** See f2py2e/rules.py ****************************/

static FortranDataDef f2py_routine_defs[] = {
  {"discreteconv",-1,{{-1}},0,(char *)discreteconv,(f2py_init_func)f2py_rout_convolve_discreteconv,doc_f2py_rout_convolve_discreteconv},

/*eof routine_defs*/
  {NULL}
};

static PyMethodDef f2py_module_methods[] = {

  {NULL,NULL}
};

#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef moduledef = {
  PyModuleDef_HEAD_INIT,
  "convolve",
  NULL,
  -1,
  f2py_module_methods,
  NULL,
  NULL,
  NULL,
  NULL
};
#endif

#if PY_VERSION_HEX >= 0x03000000
#define RETVAL m
PyObject *PyInit_convolve(void) {
#else
#define RETVAL
PyMODINIT_FUNC initconvolve(void) {
#endif
  int i;
  PyObject *m,*d, *s;
#if PY_VERSION_HEX >= 0x03000000
  m = convolve_module = PyModule_Create(&moduledef);
#else
  m = convolve_module = Py_InitModule("convolve", f2py_module_methods);
#endif
  Py_TYPE(&PyFortran_Type) = &PyType_Type;
  import_array();
  if (PyErr_Occurred())
    {PyErr_SetString(PyExc_ImportError, "can't initialize module convolve (failed to import numpy)"); return RETVAL;}
  d = PyModule_GetDict(m);
  s = PyString_FromString("$Revision: $");
  PyDict_SetItemString(d, "__version__", s);
#if PY_VERSION_HEX >= 0x03000000
  s = PyUnicode_FromString(
#else
  s = PyString_FromString(
#endif
    "This module 'convolve' is auto-generated with f2py (version:2).\nFunctions:\n"
"  out = discreteconv(times,kern,kdt,onset,offset,odt)\n"
".");
  PyDict_SetItemString(d, "__doc__", s);
  convolve_error = PyErr_NewException ("convolve.error", NULL, NULL);
  Py_DECREF(s);
  for(i=0;f2py_routine_defs[i].name!=NULL;i++)
    PyDict_SetItemString(d, f2py_routine_defs[i].name,PyFortranObject_NewAsAttr(&f2py_routine_defs[i]));

/*eof initf2pywraphooks*/
/*eof initf90modhooks*/

/*eof initcommonhooks*/


#ifdef F2PY_REPORT_ATEXIT
  if (! PyErr_Occurred())
    on_exit(f2py_report_on_exit,(void*)"convolve");
#endif

  return RETVAL;
}
#ifdef __cplusplus
}
#endif
