-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpg_healpix.c
227 lines (194 loc) · 5.32 KB
/
pg_healpix.c
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
/*
* Copyright (C) 2004-2012 Sergey Koposov
*
* Author: Sergey Koposov, Institute of Astronomy, Cambridge
* Multiple authors of the HEALPIX code are given in the LICENSE.healpix file
* Email: [email protected]
* http://ast.cam.ac.uk/~koposov
*
* This file is part of pg_healpix
*
* pg_healpix 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 2 of the License, or
* (at your option) any later version.
*
* pg_healpix 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 pg_healpix; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <string.h>
#include <math.h>
/* Postgres stuff */
#include "postgres.h"
#include "executor/spi.h"
#include "utils/lsyscache.h"
#include "utils/array.h"
#include "catalog/pg_type.h"
#include "fmgr.h"
/* For PostgreSQL versions >= 8.2 */
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
/* End of Postgres stuff */
#include "chealpix.h"
#define RADIAN(x) ((M_PI / 180.) * (x))
#define DEGREE(x) ((180. / M_PI) * (x))
#define NSIDEMAX 8192 /* so they say */
typedef double q3c_coord_t;
typedef long q3c_ipix_t;
void nside_check(q3c_ipix_t);
void ipix_check(q3c_ipix_t, q3c_ipix_t);
void angle_check(q3c_coord_t);
void get_theta_phi(q3c_coord_t, q3c_coord_t, q3c_coord_t *,
q3c_coord_t *);
void get_ra_dec(q3c_coord_t, q3c_coord_t, q3c_coord_t *, q3c_coord_t *);
ArrayType *build_array(q3c_coord_t, q3c_coord_t);
inline void
nside_check(q3c_ipix_t nside)
{
if (
((nside & (nside - 1)) != 0) || /* test if power of two */
(nside <= 0) ||
(nside > NSIDEMAX)
)
{
elog(ERROR, "Invalid value of nside. Must be >0, <=8192 and power of two");
}
}
inline void
ipix_check(q3c_ipix_t nside, q3c_ipix_t ipix)
{
if ((ipix < 0) || (ipix >= (12 * nside * nside)))
{
elog(ERROR, "Invalid value of ipix. Must be >=0 and be < 12*NSIDE^2");
}
}
inline void
angle_check(q3c_coord_t theta)
{
if (theta < 0. || theta > M_PI) /* theta is like polar distance */
{
elog(ERROR, "Invalid angle");
}
}
inline void
get_theta_phi(q3c_coord_t ra, q3c_coord_t dec, q3c_coord_t *theta,
q3c_coord_t *phi)
{
*theta = M_PI_2 - RADIAN(dec); /* should be between 0 and M_PI */
*phi = RADIAN(ra); /* in theory between 0 and 2*M_PI but could
* wrap more than that */
}
inline void
get_ra_dec(q3c_coord_t theta, q3c_coord_t phi, q3c_coord_t *ra,
q3c_coord_t *dec)
{
*dec = DEGREE(M_PI_2 - theta);
*ra = DEGREE(phi); /* in theory between 0 and 2*M_PI but could
* wrap more than that */
}
ArrayType *
build_array(q3c_coord_t ra, q3c_coord_t dec)
{
Datum *data;
int16 typlen;
bool typbyval;
char typalign;
int nelems;
ArrayType *result;
nelems = 2;
data = (Datum *) palloc(sizeof(Datum) * nelems);
data[0] = Float8GetDatum(ra);
data[1] = Float8GetDatum(dec);
/* get required info about the element type */
get_typlenbyvalalign(FLOAT8OID, &typlen, &typbyval, &typalign);
/* now build the array */
result = construct_array(data, nelems, FLOAT8OID, typlen, typbyval, typalign);
return result;
}
/* Postgres functions */
Datum pgheal_ang2ipix_nest(PG_FUNCTION_ARGS);
Datum pgheal_ang2ipix_ring(PG_FUNCTION_ARGS);
Datum pgheal_ipix2ang_nest(PG_FUNCTION_ARGS);
Datum pgheal_ipix2ang_ring(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pgheal_ang2ipix_nest);
Datum
pgheal_ang2ipix_nest(PG_FUNCTION_ARGS)
{
q3c_ipix_t nside = PG_GETARG_INT64(0);
q3c_coord_t ra = PG_GETARG_FLOAT8(1);
q3c_coord_t dec = PG_GETARG_FLOAT8(2);
q3c_ipix_t ipix;
q3c_coord_t theta;
q3c_coord_t phi;
if ((!isfinite(ra)) || (!isfinite(dec)))
{
PG_RETURN_NULL();
}
get_theta_phi(ra, dec, &theta, &phi);
nside_check(nside);
angle_check(theta);
ang2pix_nest(nside, theta, phi, &ipix);
PG_RETURN_INT64(ipix);
}
PG_FUNCTION_INFO_V1(pgheal_ang2ipix_ring);
Datum
pgheal_ang2ipix_ring(PG_FUNCTION_ARGS)
{
q3c_ipix_t nside = PG_GETARG_INT64(0);
q3c_coord_t ra = PG_GETARG_FLOAT8(1);
q3c_coord_t dec = PG_GETARG_FLOAT8(2);
q3c_ipix_t ipix;
q3c_coord_t theta,
phi;
if ((!isfinite(ra)) || (!isfinite(dec)))
{
PG_RETURN_NULL();
}
get_theta_phi(ra, dec, &theta, &phi);
nside_check(nside);
angle_check(theta);
ang2pix_ring(nside, theta, phi, &ipix);
PG_RETURN_INT64(ipix);
}
PG_FUNCTION_INFO_V1(pgheal_ipix2ang_nest);
Datum
pgheal_ipix2ang_nest(PG_FUNCTION_ARGS)
{
q3c_ipix_t ipix;
q3c_coord_t ra,
dec,
theta,
phi;
q3c_ipix_t nside = PG_GETARG_INT64(0);
nside_check(nside);
ipix = PG_GETARG_INT64(1);
ipix_check(nside, ipix);
pix2ang_nest(nside, ipix, &theta, &phi);
get_ra_dec(theta, phi, &ra, &dec);
PG_RETURN_ARRAYTYPE_P(build_array(ra, dec));
}
PG_FUNCTION_INFO_V1(pgheal_ipix2ang_ring);
Datum
pgheal_ipix2ang_ring(PG_FUNCTION_ARGS)
{
q3c_ipix_t ipix;
q3c_coord_t ra,
dec,
theta,
phi;
q3c_ipix_t nside = PG_GETARG_INT64(0);
nside_check(nside);
ipix = PG_GETARG_INT64(1);
ipix_check(nside, ipix);
pix2ang_ring(nside, ipix, &theta, &phi);
get_ra_dec(theta, phi, &ra, &dec);
PG_RETURN_ARRAYTYPE_P(build_array(ra, dec));
}