File: python.c

package info (click to toggle)
gnuastro 0.24-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,360 kB
  • sloc: ansic: 185,444; sh: 15,785; makefile: 1,303; cpp: 9
file content (108 lines) | stat: -rw-r--r-- 3,851 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*********************************************************************
python -- Functions to assist Python wrappers using Gnuastro's library.
This is part of GNU Astronomy Utilities (Gnuastro) package.

Original author:
     Jash Shah <jash28582@gmail.com>
Contributing author(s):
     Mohammad Akhlaghi <mohammad@akhlaghi.org>
Copyright (C) 2022-2025 Free Software Foundation, Inc.

Gnuastro is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.

Gnuastro 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 General Public License
along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
#include <config.h>

#include <errno.h>
#include <error.h>

/* This macro needs to be defined before including any NumPy headers to
   avoid the compiler from raising a warning message. */
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION

/* This macro needs to be defined before including any NumPy headers so
   that the NumPY C-API can be used without having to call the
   'import_arra' function as mentioned here:
   https://numpy.org/doc/1.17/reference/c-api.array.html#c.NO_IMPORT_ARRAY
   This also helps to avoid any "defined but not used" while compiling this
   file. */
#define NO_IMPORT_ARRAY

/* Import Numpy's necessary header(s). */
#include <numpy/arrayobject.h>

/* Gnuastro's Python headers. */
#include <gnuastro/python.h>





/*************************************************************
 **************           Type codes           ***************
 *************************************************************/
/* Convert Gnuastro type to NumPy datatype. Currently only converting types
   directly compatible between the two. */
int
gal_python_type_to_numpy(uint8_t type)
{
  switch (type)
    {
    case GAL_TYPE_INT8:      return NPY_INT8;
    case GAL_TYPE_INT16:     return NPY_INT16;
    case GAL_TYPE_INT32:     return NPY_INT32;
    case GAL_TYPE_INT64:     return NPY_INT64;
    case GAL_TYPE_UINT8:     return NPY_UINT8;
    case GAL_TYPE_UINT16:    return NPY_UINT16;
    case GAL_TYPE_UINT32:    return NPY_UINT32;
    case GAL_TYPE_UINT64:    return NPY_UINT64;
    case GAL_TYPE_FLOAT32:   return NPY_FLOAT32;
    case GAL_TYPE_FLOAT64:   return NPY_FLOAT64;
    case GAL_TYPE_STRING:    return NPY_STRING;
    default:
      error(EXIT_FAILURE, 0, "%s: type code %d is not convertible"
            "to NumPy.", __func__, type);
    }

  return GAL_TYPE_INVALID;
}





/* Convert Numpy datatype to Gnuastro type. Currently only converting types
   directly compatible between the two. */
uint8_t
gal_python_type_from_numpy(int type)
{
  switch (type)
    {
    case NPY_INT8:           return GAL_TYPE_INT8;
    case NPY_INT16:          return GAL_TYPE_INT16;
    case NPY_INT32:          return GAL_TYPE_INT32;
    case NPY_INT64:          return GAL_TYPE_INT64;
    case NPY_UINT8:          return GAL_TYPE_UINT8;
    case NPY_UINT16:         return GAL_TYPE_UINT16;
    case NPY_UINT32:         return GAL_TYPE_UINT32;
    case NPY_UINT64:         return GAL_TYPE_UINT64;
    case NPY_FLOAT32:        return GAL_TYPE_FLOAT32;
    case NPY_FLOAT64:        return GAL_TYPE_FLOAT64;
    case NPY_COMPLEX64:      return GAL_TYPE_COMPLEX64;
    case NPY_STRING:         return GAL_TYPE_STRING;
    default:
      error(EXIT_FAILURE, 0, "%s: type code %d is not convertible"
            "to Gnuastro.", __func__, type);
    }
  return GAL_TYPE_INVALID;
}