forked from marti-vidal-i/PolConvert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_getAntInfo.cpp
More file actions
275 lines (198 loc) · 6.82 KB
/
_getAntInfo.cpp
File metadata and controls
275 lines (198 loc) · 6.82 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* getAntInfo -
Copyright (C) 2017-2022 Ivan Marti-Vidal
Centro Astronomico de Yebes (Spain)
Universitat de Valencia (Spain)
This program 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.
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 General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include <Python.h>
// compiler warning that we use a deprecated NumPy API
// #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
// #define NO_IMPORT_ARRAY
#if PY_MAJOR_VERSION >= 3
#define NPY_NO_DEPRECATED_API 0x0
#endif
#include <numpy/npy_common.h>
#include <numpy/arrayobject.h>
#include <sys/types.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include "fitsio.h"
#include <math.h>
// cribbed from SWIG machinery
#if PY_MAJOR_VERSION >= 3
#define PyClass_Check(obj) PyObject_IsInstance(obj, (PyObject *)&PyType_Type)
#define PyInt_Check(x) PyLong_Check(x)
#define PyInt_AsLong(x) PyLong_AsLong(x)
#define PyInt_FromLong(x) PyLong_FromLong(x)
#define PyInt_FromSize_t(x) PyLong_FromSize_t(x)
#define PyString_Check(name) PyBytes_Check(name)
#define PyString_FromString(x) PyUnicode_FromString(x)
#define PyString_Format(fmt, args) PyUnicode_Format(fmt, args)
//#define PyString_AsString(str) PyBytes_AsString(str)
#define PyString_Size(str) PyBytes_Size(str)
#define PyString_InternFromString(key) PyUnicode_InternFromString(key)
#define Py_TPFLAGS_HAVE_CLASS Py_TPFLAGS_BASETYPE
#define PyString_AS_STRING(x) PyUnicode_AS_STRING(x)
#define _PyLong_FromSsize_t(x) PyLong_FromSsize_t(x)
#endif
// and after some hacking
#if PY_MAJOR_VERSION >= 3
#define PyString_AsString(obj) PyUnicode_AsUTF8(obj)
#endif
/* Docstrings */
static char module_docstring[] =
"Get antenna information (coordinates and mount types).";
static char getAntInfo_docstring[] =
"Returns antenna information";
static char getCoords_docstring[] =
"Returns antenna coordinates";
static char getMounts_docstring[] =
"Returns the mount types";
/* Available functions */
static PyObject *getAntInfo(PyObject *self, PyObject *args);
static PyObject *getCoords(PyObject *self, PyObject *args);
static PyObject *getMounts(PyObject *self, PyObject *args);
/* Module specification */
static PyMethodDef module_methods[] = {
{"getAntInfo", getAntInfo, METH_VARARGS, getAntInfo_docstring},
{"getCoords", getCoords, METH_VARARGS, getCoords_docstring},
{"getMounts", getMounts, METH_VARARGS, getMounts_docstring}, //,
{NULL, NULL, 0, NULL} /* terminated by list of NULLs, apparently */
};
/* Initialize the module */
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef pc_module_def = {
PyModuleDef_HEAD_INIT,
"_getAntInfo", /* m_name */
module_docstring, /* m_doc */
-1, /* m_size */
module_methods, /* m_methods */
NULL,NULL,NULL,NULL /* m_reload, m_traverse, m_clear, m_free */
};
PyMODINIT_FUNC PyInit__getAntInfo(void)
{
PyObject *m = PyModule_Create(&pc_module_def);
import_array();
return(m);
}
#else
PyMODINIT_FUNC init_getAntInfo(void)
{
PyObject *m = Py_InitModule3("_getAntInfo", module_methods, module_docstring);import_array();
if (m == NULL)
return;
}
#endif
///////////////////////
char message[512];
int Nants = 1;
double *Coords = new double[3];
int *Mounts = new int[1];
static PyObject *getAntInfo(PyObject *self, PyObject *args){
PyObject *IDI;
int status, ist, imt, iAux;
long lAux, jj;
long Iret = 0;
status = 0;
Nants = -1;
char ARRAY[] = "ARRAY_GEOMETRY";
char STABXYZ[] = "STABXYZ";
char MNTSTA[] = "MNTSTA";
fitsfile *ifile;
ifile = nullptr;
std::string fname;
if (!PyArg_ParseTuple(args, "O",&IDI)){
printf("Failed initialization of getAntInfo! Check inputs!\n");
Iret = -1; goto abort1;
};
fname = PyString_AsString(IDI);
fits_open_file(&ifile, fname.c_str(), READONLY, &status);
if (status){
printf("\n\nPROBLEM OPENING FILE! ERR: %i\n\n",status);
Iret = status; goto abort1;
};
fits_movnam_hdu(ifile, BINARY_TBL, ARRAY,1, &status);
if (status){
printf("\n\nPROBLEM ACCESSING ARRAY INFO! ERR: %i\n\n",status);
Iret = status; goto abort1;
};
fits_get_num_rows(ifile, &lAux, &status);
if(status){
printf("\n\nPROBLEM ACCESSING ARRAY INFO! ERR: %i\n\n",status);
Iret = status; goto abort1;
};
Nants = (int) lAux;
// Allocate memory:
delete[] Coords;
delete[] Mounts;
Coords = new double[3*Nants];
Mounts = new int[Nants];
// Read coordinates:
fits_get_colnum(ifile, CASEINSEN, STABXYZ, &ist, &status);
if(status){
printf("\n\nPROBLEM ACCESSING ARRAY INFO! ERR: %i\n\n",status);
Iret = status; goto abort1;
};
fits_get_colnum(ifile, CASEINSEN, MNTSTA, &imt, &status);
if(status){
printf("\n\nPROBLEM ACCESSING ARRAY INFO! ERR: %i\n\n",status);
Iret = status; goto abort1;
};
for (jj=0;jj<Nants;jj++){
fits_read_col(ifile, TDOUBLE, ist, jj+1, 1, 3, NULL, &Coords[3*jj], &iAux, &status);
if(status){
printf("\n\nPROBLEM ACCESSING ARRAY COORDINATES DATA! ERR: %i\n\n",status);
Iret = status; goto abort1;
};
fits_read_col(ifile, TINT, imt, jj+1, 1, 1, NULL, &Mounts[jj], &iAux, &status);
if(status){
printf("\n\nPROBLEM ACCESSING ARRAY MOUNTS DATA! ERR: %i\n\n",status);
Iret = status; goto abort1;
};
printf("ANTENNA %2li: X=%-9.1f Y=%-9.1f Z=%-9.1f | MOUNT: %i\n",
jj,Coords[3*jj],Coords[3*jj+1],Coords[3*jj+2],Mounts[jj]);
};
abort1:
if (Iret!=0 && Nants!=-1){ // back to defaults.
delete[] Coords; Coords = new double[3];
delete[] Mounts; Mounts = new int[1];
};
if(ifile){
fits_close_file(ifile, &status);
if(status){
printf("\n\nPROBLEM CLOSING FITS-IDI! ERR: %i (Previous Error: %li)\n\n",status,Iret);
Iret = status;
};
};
return PyLong_FromLong(Iret);
};
static PyObject *getCoords(PyObject *self, PyObject *args){
// Build numpy array:
PyObject *CoordArr;
int nd = 2;
npy_intp* CD = new npy_intp[nd];
CD[0] = Nants; CD[1] = 3;
CoordArr = PyArray_SimpleNewFromData(nd, CD, NPY_DOUBLE, (void*) Coords);
return CoordArr;
};
static PyObject *getMounts(PyObject *self, PyObject *args){
// Build numpy array:
PyObject *MountArr;
int nd = 1;
npy_intp* MD = new npy_intp[nd];
MD[0] = Nants;
MountArr = PyArray_SimpleNewFromData(nd, MD, NPY_INT, (void*) Mounts);
return MountArr;
};
// eof