/* -*- C -*- */
static PyArrayObject *
PyGSL_New_Array(int nd, int *dimensions, int type)
{
     maybelong tmp[2];
     if (nd > 2){
	  gsl_error("This function must not be called to create an array with more than two dimensions!",
		    filename, __LINE__, GSL_ESANITY);
	  return NULL;
     }
     if(nd <= 0){
	  gsl_error("Dimesions must be 1 or 2!",
		    filename, __LINE__, GSL_ESANITY);
	  return NULL;
     }
     tmp[0] = dimensions[0];
     if(nd == 2)
	  tmp[1] = dimensions[1];
     return NA_Empty(nd, tmp, type);

}

static PyArrayObject *
PyGSL_Copy_Array(PyArrayObject *ob, int type)
{
     int nd;
     nd = ob->nd;
     if(!PyArray_Check((PyObject *) ob)){
	  gsl_error("This function only copies arrays!", filename, __LINE__, GSL_ESANITY);
	  return NULL;
     }
     return (PyArrayObject *) NA_copy(ob);
}

static PyArrayObject * 
PyGSL_nummarray_convert(PyObject *src, int array_type, int flag)
{
     PyArrayObject * a_array = NULL;
     int contiguous = 1;
     int line = -1;

     FUNC_MESS_BEGIN();
     contiguous = flag & PyGSL_CONTIGUOUS;
     if (flag & PyGSL_INPUT_ARRAY){
	  a_array = NA_InputArray(src, array_type, NUM_NOTSWAPPED | NUM_ALIGNED | contiguous);
	  line = __LINE__ - 1;
     } else if (flag & PyGSL_OUTPUT_ARRAY){
	  a_array = NA_OutputArray(src, array_type, NUM_NOTSWAPPED | NUM_ALIGNED | contiguous);
	  line = __LINE__ - 1;
     } else if(flag & PyGSL_IO_ARRAY){
	  a_array = NA_IoArray(src, array_type, NUM_NOTSWAPPED | NUM_ALIGNED | contiguous);
	  line = __LINE__ - 1;
     } else {
	  line =  __LINE__;
	  gsl_error("Either Input of Output or InOut array must be sepecified",
		    filename, line, GSL_ESANITY);
	  goto fail;
     }
     FUNC_MESS_END();

     if(a_array == NULL)
	  goto fail;
     return a_array;          

 fail:
     PyGSL_add_traceback(NULL, __FILE__, __FUNCTION__, line);     
     DEBUG_MESS(3, "contiguous = %d, array_type = %d", contiguous, array_type);
     DEBUG_MESS(4, "common array types: Double %d, CDouble %d", PyArray_DOUBLE, PyArray_CDOUBLE);
     return NULL;
}


static PyArrayObject * 
PyGSL_PyArray_prepare_gsl_vector_view(PyObject *src,
				      int array_type,
				      int flag, 
				      long size, int argnum, PyGSL_error_info * info)
{
     PyArrayObject * a_array = NULL;
     int line=-1;

     FUNC_MESS_BEGIN();
     if(!NA_NumArrayCheckExact(src))
	  PyGSL_INCREASE_vector_transform_counter();

     a_array = PyGSL_nummarray_convert(src, array_type, flag);
     if(NULL == a_array){
	  line = __LINE__ - 2;
	  /* Here one could put some more information */
	  goto fail;
     }
     if(PyGSL_PyArray_Check(a_array, array_type, flag,  1, &size, argnum, info) != GSL_SUCCESS){
	  line = __LINE__ - 1;
	  goto fail;
     }
     FUNC_MESS_END();
     return a_array;
 fail:
     PyGSL_add_traceback(NULL, __FILE__, __FUNCTION__, line);
     Py_XDECREF(a_array);
     return NULL;
}

static PyArrayObject *
PyGSL_PyArray_prepare_gsl_matrix_view(PyObject *src,
				      int array_type,
				      int flag, 
				      long size1, long size2, int argnum, 
				      PyGSL_error_info * info)
{
     PyArrayObject * a_array = NULL;
     long dimensions[2];

     FUNC_MESS_BEGIN();
     if(!NA_NumArrayCheckExact(src))
	  PyGSL_INCREASE_matrix_transform_counter();

     a_array = PyGSL_nummarray_convert(src, array_type, flag);
     /* Here one could put some more information */
     if(NULL == a_array)
	  goto fail;

     dimensions[0] = size1;
     dimensions[1] = size2;
     if(PyGSL_PyArray_Check(a_array, array_type, flag,  2, dimensions, argnum, info) != GSL_SUCCESS)
	  goto fail;
     
     FUNC_MESS_END();
     return a_array;
 fail:
     PyGSL_add_traceback(NULL, filename, __FUNCTION__, __LINE__);
     Py_XDECREF(a_array);
     return NULL;

}

/* EOF */
