Skip to content
Snippets Groups Projects
pyswiftsim_tools.c 1.67 KiB
/*******************************************************************************
 * This file is part of PYSWIFTSIM.
 * Copyright (c) 2018 loic hausammann (loic.hausammann@epfl.ch)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 ******************************************************************************/
#include "pyswiftsim_tools.h"


/**
 * @brief Check if object is the expected PyArray type
 *
 * @param obj PyArray to check
 * @param dim required dimension
 * @param int required type
 * @param name Name to print for errors
 * @return #error_code result of the test
 */
int pytools_check_array(PyArrayObject *obj, int dim, int type, const char* name)
{
  IMPORT_ARRAY1(1);

  /* check if array */
  if (!PyArray_Check(obj))
    {
      error("Expecting a numpy array for %s", name);
    }

  /* check if required dim */
  if (PyArray_NDIM(obj) != dim)
    {
      error("Array should be a %i dimensional object for %s", dim, name);
    }

  /* check data type */
  if (PyArray_TYPE(obj) != type)
    {
      error("Wrong array type for %s", name);
    }

  return SWIFT_SUCCESS;

}