~pconv-team/polconvertsd/trunk-1

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
/* getAntInfo -
             Copyright (C) 2017  Ivan Marti-Vidal
             Nordic Node of EU ALMA Regional Center (Onsala, Sweden)
             Max-Planck-Institut fuer Radioastronomie (Bonn, Germany)
  
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>
#include <numpy/arrayobject.h>
#include <sys/types.h>
#include <iostream> 
#include <fstream>
#include <stdlib.h>  
#include <string.h>
#include "fitsio.h"
#include <math.h>




/* 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}
};


/* Initialize the module */
PyMODINIT_FUNC init_getAntInfo(void)
{
    PyObject *m = Py_InitModule3("_getAntInfo", module_methods, module_docstring);import_array();
    if (m == NULL)
        return;

}

///////////////////////


   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;

  status = 0;

  if (!PyArg_ParseTuple(args, "O",&IDI)){
     printf("Failed initialization of getAntInfo! Check inputs!\n"); 
     PyObject *ret = Py_BuildValue("i",-1);
     return ret;
  };

  std::string fname;

  fname = PyString_AsString(IDI);


// TYPE == 1 -> IDI

  fitsfile *ifile;
  char ARRAY[] = "ARRAY_GEOMETRY";
  char STABXYZ[] = "STABXYZ";
  char MNTSTA[] = "MNTSTA";


  fits_open_file(&ifile, fname.c_str(), READONLY, &status);

  if (status){
     printf("\n\nPROBLEM OPENING FILE!  ERR: %i\n\n",status);
     return Py_BuildValue("i",1);
  };
  fits_movnam_hdu(ifile, BINARY_TBL, ARRAY,1, &status);
  if (status){
     printf("\n\nPROBLEM ACCESSING ARRAY INFO!  ERR: %i\n\n",status); 
     return Py_BuildValue("i",1);
  };

  fits_get_num_rows(ifile, &lAux, &status);

  if(status){
    printf("\n\nPROBLEM ACCESSING ARRAY INFO!  ERR: %i\n\n",status);
    return Py_BuildValue("i",1);
  };

  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);
    return Py_BuildValue("i",1);
  };
  fits_get_colnum(ifile, CASEINSEN, MNTSTA, &imt, &status);
  if(status){
    printf("\n\nPROBLEM ACCESSING ARRAY INFO!  ERR: %i\n\n",status);
    return Py_BuildValue("i",1);
  };

  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);
    return Py_BuildValue("i",2);
  };


  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);
    return Py_BuildValue("i",2);
  };


  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]);


  };



  fits_close_file(ifile, &status);

  if(status){
    printf("\n\nPROBLEM LOSING FITS-IDI!  ERR: %i\n\n",status);
    return Py_BuildValue("i",3);
  };


    return Py_BuildValue("i",0);

};


static PyObject *getCoords(PyObject *self, PyObject *args){

// Build numpy array:

PyObject *CoordArr;
long CD[2] = {Nants,3};

Py_INCREF(CoordArr);
CoordArr = PyArray_SimpleNewFromData(2, &CD[0], NPY_DOUBLE, (void*) Coords);

return CoordArr;

};


static PyObject *getMounts(PyObject *self, PyObject *args){

// Build numpy array:
PyObject *MountArr;

long MD[1] = {Nants};

Py_INCREF(MountArr);
MountArr = PyArray_SimpleNewFromData(1, &MD[0], NPY_INT, (void*) Mounts);

return MountArr;

};