-
Notifications
You must be signed in to change notification settings - Fork 27
/
q3c.c
1316 lines (1131 loc) · 33 KB
/
q3c.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2004-2023 Sergey Koposov
Email: skoposov AT ed DOT ac DOT uk
This file is part of Q3C.
Q3C 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.
Q3C 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 Q3C; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>
/* Postgres stuff */
#include "postgres.h"
#include "executor/spi.h"
#include "utils/lsyscache.h"
/* I included that just to remove the gcc warning
* q3c.c:128: warning: implicit declaration of function `get_typlenbyvalalign'
*/
#include "utils/array.h"
#include "utils/geo_decls.h"
#include "catalog/pg_type.h"
#include "fmgr.h"
#if PG_VERSION_NUM >= 90300
#include "access/tupmacs.h"
#endif
//#include "nodes/relation.h"
#include "utils/selfuncs.h"
/* For PostgreSQL versions >= 8.2 */
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
/* End of Postgres stuff */
#include "common.h"
extern Node *estimate_expression_value(PlannerInfo *root, Node *node);
/* Postgres functions */
Datum pgq3c_ang2ipix(PG_FUNCTION_ARGS);
Datum pgq3c_ang2ipix_real(PG_FUNCTION_ARGS);
Datum pgq3c_ipix2ang(PG_FUNCTION_ARGS);
Datum pgq3c_pixarea(PG_FUNCTION_ARGS);
Datum pgq3c_dist(PG_FUNCTION_ARGS);
Datum pgq3c_dist_pm(PG_FUNCTION_ARGS);
Datum pgq3c_sindist(PG_FUNCTION_ARGS);
Datum pgq3c_sindist_pm(PG_FUNCTION_ARGS);
Datum q3c_strquery(PG_FUNCTION_ARGS);
Datum pgq3c_nearby_it(PG_FUNCTION_ARGS);
Datum pgq3c_nearby_pm_it(PG_FUNCTION_ARGS);
Datum pgq3c_ellipse_nearby_it(PG_FUNCTION_ARGS);
Datum pgq3c_radial_array(PG_FUNCTION_ARGS);
Datum pgq3c_radial_query_it(PG_FUNCTION_ARGS);
Datum pgq3c_ellipse_query_it(PG_FUNCTION_ARGS);
Datum pgq3c_poly_query_it(PG_FUNCTION_ARGS);
Datum pgq3c_poly_query1_it(PG_FUNCTION_ARGS);
Datum pgq3c_in_ellipse(PG_FUNCTION_ARGS);
Datum pgq3c_in_poly(PG_FUNCTION_ARGS);
Datum pgq3c_in_poly1(PG_FUNCTION_ARGS);
Datum pgq3c_get_version(PG_FUNCTION_ARGS);
Datum pgq3c_sel(PG_FUNCTION_ARGS);
Datum pgq3c_seljoin(PG_FUNCTION_ARGS);
Datum pgq3c_seloper(PG_FUNCTION_ARGS);
/* Dummy function that implements the selectivity operator */
PG_FUNCTION_INFO_V1(pgq3c_seloper);
Datum pgq3c_seloper(PG_FUNCTION_ARGS)
{
PG_RETURN_BOOL(true);
}
/* The actual selectivity function, it returns the ratio of the
* search circle to the whole sky area
*/
PG_FUNCTION_INFO_V1(pgq3c_sel);
Datum pgq3c_sel(PG_FUNCTION_ARGS)
{
PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
List *args = (List *) PG_GETARG_POINTER(2);
int varRelid = PG_GETARG_INT32(3);
Node *left;
Node *other;
VariableStatData vardata;
Datum radDatum;
bool isnull;
double rad;
double ratio;
/* this needs more protections against crazy inputs */
if (list_length(args) != 2) { elog(ERROR, "Wrong inputs to selectivity function");}
left = (Node *) linitial(args);
examine_variable(root, left, varRelid, &vardata);
other = estimate_expression_value(root, vardata.var);
radDatum = ((Const *) other)->constvalue;
isnull = ((Const *) other)->constisnull;
/* We shouldn't be really getting null inputs here */
if (!isnull)
{
rad = DatumGetFloat8(radDatum);
}
else
{
rad = 0;
}
ratio = 3.14 * rad * rad / 41252.; /* pi*r^2/whole_sky_area */
/* clamp at 0, 1*/
CLAMP_PROBABILITY(ratio);
//elog(WARNING, "HERE0.... %e", ratio);
PG_RETURN_FLOAT8(ratio);
}
PG_FUNCTION_INFO_V1(pgq3c_seljoin);
Datum pgq3c_seljoin(PG_FUNCTION_ARGS)
{
PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
List *args = (List *) PG_GETARG_POINTER(2);
int varRelid = 0;
/* Because there is no varrelid in the join selectivity call
* I just set it to zero */
Node *left;
Node *other;
VariableStatData vardata;
Datum radDatum;
bool isnull;
double rad;
double ratio;
/* this needs more protections against crazy inputs */
if (list_length(args) != 2) { elog(ERROR, "Wrong inputs to selectivity function");}
left = (Node *) linitial(args);
examine_variable(root, left, varRelid, &vardata);
other = estimate_expression_value(root, vardata.var);
radDatum = ((Const *) other)->constvalue;
isnull = ((Const *) other)->constisnull;
/* We shouldn't be really getting null inputs here */
if (!isnull)
{
rad = DatumGetFloat8(radDatum);
}
else
{
rad = 0;
}
ratio = 3.14 * rad * rad / 41252.; /* pi*r^2/whole_sky_area */
/* clamp at 0, 1*/
CLAMP_PROBABILITY(ratio);
PG_RETURN_FLOAT8(ratio);
}
static int convert_pgarray2poly(ArrayType *poly_arr, q3c_coord_t *in_ra, q3c_coord_t *in_dec, int *nvert);
PG_FUNCTION_INFO_V1(pgq3c_get_version);
Datum pgq3c_get_version(PG_FUNCTION_ARGS)
{
char VERSION_MAX_BYTES = 100;
char *buf = palloc(VERSION_MAX_BYTES);
q3c_get_version(buf, VERSION_MAX_BYTES);
PG_RETURN_CSTRING(buf);
}
PG_FUNCTION_INFO_V1(pgq3c_ang2ipix);
Datum pgq3c_ang2ipix(PG_FUNCTION_ARGS)
{
extern struct q3c_prm hprm;
q3c_coord_t ra = PG_GETARG_FLOAT8(0);
q3c_coord_t dec = PG_GETARG_FLOAT8(1);
q3c_ipix_t ipix;
static int invocation;
static q3c_coord_t ra_buf, dec_buf;
static q3c_ipix_t ipix_buf;
if (invocation == 0)
{
}
else
{
if ((ra == ra_buf) && (dec == dec_buf))
{
PG_RETURN_INT64(ipix_buf);
}
}
if ((!isfinite(ra)) || (!isfinite(dec)))
{
PG_RETURN_NULL();
}
q3c_ang2ipix(&hprm, ra, dec, &ipix);
ra_buf = ra;
dec_buf = dec;
ipix_buf = ipix;
invocation = 1;
PG_RETURN_INT64(ipix);
}
PG_FUNCTION_INFO_V1(pgq3c_ang2ipix_real);
Datum pgq3c_ang2ipix_real(PG_FUNCTION_ARGS)
{
extern struct q3c_prm hprm;
q3c_coord_t ra = PG_GETARG_FLOAT4(0);
q3c_coord_t dec = PG_GETARG_FLOAT4(1);
q3c_ipix_t ipix;
static int invocation;
static q3c_coord_t ra_buf, dec_buf;
static q3c_ipix_t ipix_buf;
if (invocation == 0)
{
}
else
{
if ((ra == ra_buf) && (dec == dec_buf))
{
PG_RETURN_INT64(ipix_buf);
}
}
if ((!isfinite(ra)) || (!isfinite(dec)))
{
PG_RETURN_NULL();
}
q3c_ang2ipix(&hprm, ra, dec, &ipix);
ra_buf = ra;
dec_buf = dec;
ipix_buf = ipix;
invocation = 1;
PG_RETURN_INT64(ipix);
}
PG_FUNCTION_INFO_V1(pgq3c_ipix2ang);
Datum pgq3c_ipix2ang(PG_FUNCTION_ARGS)
{
extern struct q3c_prm hprm;
q3c_ipix_t ipix;
q3c_coord_t ra, dec;
Datum *data;
int16 typlen;
bool typbyval;
char typalign;
ArrayType *result;
ipix = PG_GETARG_INT64(0);
if ((ipix < 0) || (ipix > Q3C_MAX_IPIX))
{
elog(ERROR, "Invalid ipix value");
}
q3c_ipix2ang(&hprm, ipix, &ra, &dec);
data = ( Datum *) palloc(sizeof(Datum) * 2);
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, 2, FLOAT8OID, typlen, typbyval, typalign);
PG_RETURN_ARRAYTYPE_P(result);
}
PG_FUNCTION_INFO_V1(pgq3c_pixarea);
Datum pgq3c_pixarea(PG_FUNCTION_ARGS)
{
extern struct q3c_prm hprm;
q3c_ipix_t ipix;
q3c_coord_t res;
int depth;
ipix = PG_GETARG_INT64(0);
depth = PG_GETARG_INT32(1);
if (depth <= 0)
{
elog(ERROR, "Invalid depth. It should be greater than 0.");
}
if (depth > 30)
{
elog(ERROR, "Invalid depth. It should be less than 31.");
}
if (ipix < 0)
{
elog(ERROR, "Invalid ipix value");
}
if (ipix > Q3C_MAX_IPIX)
{
elog(ERROR, "Invalid ipix value");
}
res = q3c_pixarea(&hprm, ipix, depth);
PG_RETURN_FLOAT8(res);
}
PG_FUNCTION_INFO_V1(pgq3c_dist);
Datum pgq3c_dist(PG_FUNCTION_ARGS)
{
q3c_coord_t ra1 = PG_GETARG_FLOAT8(0);
q3c_coord_t dec1 = PG_GETARG_FLOAT8(1);
q3c_coord_t ra2 = PG_GETARG_FLOAT8(2);
q3c_coord_t dec2 = PG_GETARG_FLOAT8(3);
q3c_coord_t res;
res = q3c_dist(ra1, dec1, ra2, dec2);
PG_RETURN_FLOAT8(res);
}
PG_FUNCTION_INFO_V1(pgq3c_sindist);
Datum pgq3c_sindist(PG_FUNCTION_ARGS)
{
q3c_coord_t ra1 = PG_GETARG_FLOAT8(0);
q3c_coord_t dec1 = PG_GETARG_FLOAT8(1);
q3c_coord_t ra2 = PG_GETARG_FLOAT8(2);
q3c_coord_t dec2 = PG_GETARG_FLOAT8(3);
q3c_coord_t res;
res = q3c_sindist(ra1, dec1, ra2, dec2);
PG_RETURN_FLOAT8(res);
}
PG_FUNCTION_INFO_V1(pgq3c_sindist_pm);
Datum pgq3c_sindist_pm(PG_FUNCTION_ARGS)
{
q3c_coord_t pmra1, pmdec1, epoch1, epoch2;
q3c_coord_t ra1, dec1, ra2, dec2, ra1_shift, dec1_shift, cdec;
bool pm_enabled = true, cosdec;
q3c_coord_t res;
const int ra_arg_pos = 0, dec_arg_pos = 1, pmra_arg_pos = 2, pmdec_arg_pos = 3,
cosdec_arg_pos = 4, epoch_arg_pos = 5, ra2_arg_pos = 6, dec2_arg_pos = 7,
epoch2_arg_pos = 8;
if (PG_ARGISNULL(ra_arg_pos) || PG_ARGISNULL(dec_arg_pos) ||
PG_ARGISNULL(ra2_arg_pos) || PG_ARGISNULL(dec2_arg_pos))
{
PG_RETURN_NULL();
}
ra1 = PG_GETARG_FLOAT8(ra_arg_pos);
dec1 = PG_GETARG_FLOAT8(dec_arg_pos);
if ((!PG_ARGISNULL(pmra_arg_pos)) &&
(!PG_ARGISNULL(pmdec_arg_pos)) &&
(!PG_ARGISNULL(epoch_arg_pos)) && (!PG_ARGISNULL(epoch2_arg_pos)))
{
pmra1 = PG_GETARG_FLOAT8(pmra_arg_pos);
pmdec1 = PG_GETARG_FLOAT8(pmdec_arg_pos);
epoch1 = PG_GETARG_FLOAT8(epoch_arg_pos);
epoch2 = PG_GETARG_FLOAT8(epoch2_arg_pos);
}
else
{
pm_enabled = false;
pmra1 = 0;
pmdec1 = 0;
epoch1 = 0;
epoch2 = 0;
}
cosdec = PG_GETARG_INT32(cosdec_arg_pos) != 0;
ra2 = PG_GETARG_FLOAT8(ra2_arg_pos);
dec2 = PG_GETARG_FLOAT8(dec2_arg_pos);
if (pm_enabled)
{
if (cosdec)
{
cdec = cos(dec1 * Q3C_DEGRA);
}
else
{
cdec = 1;
}
ra1_shift = ra1 + pmra1 * (epoch2 - epoch1) / cdec / 3600000;
dec1_shift = dec1 + pmdec1 * (epoch2 - epoch1) / 3600000;
}
else
{
ra1_shift = ra1;
dec1_shift = dec1;
}
res = q3c_sindist(ra1_shift, dec1_shift, ra2, dec2);
PG_RETURN_FLOAT8(res);
}
PG_FUNCTION_INFO_V1(pgq3c_dist_pm);
Datum pgq3c_dist_pm(PG_FUNCTION_ARGS)
{
q3c_coord_t pmra1, pmdec1, epoch1, epoch2;
q3c_coord_t ra1, dec1, ra2, dec2, ra1_shift, dec1_shift, cdec = 1;
bool pm_enabled = true, cosdec;
q3c_coord_t res;
const int ra_arg_pos = 0, dec_arg_pos = 1, pmra_arg_pos = 2, pmdec_arg_pos = 3,
cosdec_arg_pos = 4, epoch_arg_pos = 5, ra2_arg_pos = 6, dec2_arg_pos = 7,
epoch2_arg_pos = 8;
if (PG_ARGISNULL(ra_arg_pos) || PG_ARGISNULL(dec_arg_pos) ||
PG_ARGISNULL(ra2_arg_pos) || PG_ARGISNULL(dec2_arg_pos))
{
PG_RETURN_NULL();
}
ra1 = PG_GETARG_FLOAT8(ra_arg_pos);
dec1 = PG_GETARG_FLOAT8(dec_arg_pos);
if ((!PG_ARGISNULL(pmra_arg_pos)) && (!PG_ARGISNULL(pmdec_arg_pos)) &&
(!PG_ARGISNULL(epoch_arg_pos)) && (!PG_ARGISNULL(epoch2_arg_pos)))
{
pmra1 = PG_GETARG_FLOAT8(pmra_arg_pos);
pmdec1 = PG_GETARG_FLOAT8(pmdec_arg_pos);
epoch1 = PG_GETARG_FLOAT8(epoch_arg_pos);
epoch2 = PG_GETARG_FLOAT8(epoch2_arg_pos);
}
else
{
pm_enabled = false;
pmra1 = 0;
pmdec1 = 0;
epoch1 = 0;
epoch2 = 0;
}
cosdec = PG_GETARG_INT32(cosdec_arg_pos) != 0;
ra2 = PG_GETARG_FLOAT8(ra2_arg_pos);
dec2 = PG_GETARG_FLOAT8(dec2_arg_pos);
if (pm_enabled)
{
if (cosdec)
{
cdec = cos(dec1 * Q3C_DEGRA);
}
else
{
cdec = 1;
}
ra1_shift = ra1 + pmra1 * (epoch2 - epoch1) / cdec / 3600000;
dec1_shift = dec1 + pmdec1 * (epoch2 - epoch1) / 3600000;
}
else
{
ra1_shift = ra1;
dec1_shift = dec1;
}
res = q3c_dist(ra1_shift, dec1_shift, ra2, dec2);
PG_RETURN_FLOAT8(res);
}
PG_FUNCTION_INFO_V1(pgq3c_nearby_it);
Datum pgq3c_nearby_it(PG_FUNCTION_ARGS)
{
q3c_ipix_t ipix_array[8];
static q3c_ipix_t ipix_array_buf[8];
static q3c_coord_t ra_cen_buf, dec_cen_buf, radius_buf;
static int invocation;
int i;
extern struct q3c_prm hprm;
q3c_circle_region circle;
q3c_coord_t ra_cen = PG_GETARG_FLOAT8(0); // ra_cen
q3c_coord_t dec_cen = PG_GETARG_FLOAT8(1); // dec_cen
q3c_coord_t radius = PG_GETARG_FLOAT8(2); // error radius
int iteration = PG_GETARG_INT32(3); // iteration
if ( (!isfinite(ra_cen)) || (!isfinite(dec_cen)) )
{
elog(ERROR, "The values of ra,dec are infinites or NaNs");
}
if (invocation == 0)
/* If this is the first invocation of the function */
{
/* I should set invocation=1 ONLY!!! after setting ra_cen_buf, dec_cen_buf and
* ipix_buf. Because if the program will be canceled or crashed
* for some reason the invocation should be == 0
*/
}
else
{
if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) && (radius == radius_buf))
{
PG_RETURN_INT64(ipix_array_buf[iteration]);
}
}
ra_cen = UNWRAP_RA(ra_cen);
if (q3c_fabs(dec_cen) > 90) {dec_cen = q3c_fmod(dec_cen,90);}
circle.ra = ra_cen;
circle.dec = dec_cen;
circle.rad = radius;
q3c_get_nearby(&hprm, Q3C_CIRCLE, &circle, ipix_array);
for(i = 0; i < 8; i++)
{
ipix_array_buf[i] = ipix_array[i];
}
ra_cen_buf = ra_cen;
dec_cen_buf = dec_cen;
radius_buf = radius;
invocation = 1;
PG_RETURN_INT64(ipix_array_buf[iteration]);
}
PG_FUNCTION_INFO_V1(pgq3c_nearby_pm_it);
Datum pgq3c_nearby_pm_it(PG_FUNCTION_ARGS)
{
q3c_ipix_t ipix_array[8];
static q3c_ipix_t ipix_array_buf[8];
static q3c_coord_t ra_cen_buf, dec_cen_buf, radius_buf;
static q3c_coord_t pmra_buf, pmdec_buf, max_epoch_delta_buf;
static int invocation;
int i;
extern struct q3c_prm hprm;
q3c_circle_region circle;
q3c_coord_t new_radius;
q3c_coord_t ra_cen, dec_cen, pmra, pmdec;
q3c_coord_t max_epoch_delta = 0, radius = 0;
bool pm_enabled = true, cosdec;
int iteration;
const int ra_arg_pos = 0, dec_arg_pos = 1, pmra_arg_pos = 2, pmdec_arg_pos = 3,
cosdec_arg_pos = 4, maxepochdelta_arg_pos = 5, radius_arg_pos = 6, iteration_arg_pos = 7;
if (PG_ARGISNULL(ra_arg_pos) || PG_ARGISNULL(dec_arg_pos) || PG_ARGISNULL(radius_arg_pos))
{
elog(ERROR, "Right Ascensions and raddii must be not null");
}
ra_cen = PG_GETARG_FLOAT8(ra_arg_pos); // ra_cen
dec_cen = PG_GETARG_FLOAT8(dec_arg_pos); // dec_cen
if ((!PG_ARGISNULL(pmra_arg_pos)) && (!PG_ARGISNULL(pmdec_arg_pos)) &&
(!PG_ARGISNULL(maxepochdelta_arg_pos)))
{
pmra = PG_GETARG_FLOAT8(pmra_arg_pos);
pmdec = PG_GETARG_FLOAT8(pmdec_arg_pos);
max_epoch_delta = PG_GETARG_FLOAT8(maxepochdelta_arg_pos);
}
else
{
pm_enabled = false;
pmra = 0;
pmdec = 0;
max_epoch_delta = 0;
}
cosdec = PG_GETARG_INT32(cosdec_arg_pos) != 0;
radius = PG_GETARG_FLOAT8(radius_arg_pos); // error radius
iteration = PG_GETARG_INT32(iteration_arg_pos); // iteration
if ( (!isfinite(ra_cen)) || (!isfinite(dec_cen)) )
{
elog(ERROR, "The values of ra,dec are infinites or NaNs");
}
if ( (!isfinite(pmra)) || (!isfinite(pmdec)) ||
(!isfinite(max_epoch_delta)) )
{
pmra = 0;
pmdec = 0;
max_epoch_delta = 0;
}
if (max_epoch_delta < 0)
{
elog(ERROR, "The maximum epoch difference must be >=0 ");
}
if (invocation == 0)
/* If this is the first invocation of the function */
{
/* I should set invocation=1 ONLY!!! after setting ra_cen_buf, dec_cen_buf and
* ipix_buf. Because if the program will be canceled or crashed
* for some reason the invocation should be == 0
*/
}
else
{
if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) &&
(radius == radius_buf) && (pmra == pmra_buf) &&
(pmdec == pmdec_buf) && (max_epoch_delta == max_epoch_delta_buf))
{
PG_RETURN_INT64(ipix_array_buf[iteration]);
}
}
if (pm_enabled)
{
q3c_coord_t pmra1;
if (cosdec) { pmra1 = pmra; } else { pmra1 = pmra * cos(Q3C_DEGRA * dec_cen);}
new_radius = q3c_sqrt(pmra1 * pmra1 + pmdec * pmdec) / 3600000 * max_epoch_delta + radius;
}
else
{
new_radius = radius;
}
ra_cen = UNWRAP_RA(ra_cen);
if (q3c_fabs(dec_cen) > 90) {dec_cen = q3c_fmod(dec_cen,90);}
circle.ra = ra_cen;
circle.dec = dec_cen;
circle.rad = new_radius;
q3c_get_nearby(&hprm, Q3C_CIRCLE, &circle, ipix_array);
for(i = 0; i < 8; i++)
{
ipix_array_buf[i] = ipix_array[i];
}
ra_cen_buf = ra_cen;
dec_cen_buf = dec_cen;
radius_buf = radius;
max_epoch_delta_buf = max_epoch_delta;
pmra_buf = pmra;
pmdec_buf = pmdec;
invocation = 1;
PG_RETURN_INT64(ipix_array_buf[iteration]);
}
PG_FUNCTION_INFO_V1(pgq3c_ellipse_nearby_it);
Datum pgq3c_ellipse_nearby_it(PG_FUNCTION_ARGS)
{
q3c_ipix_t ipix_array[8];
static q3c_ipix_t ipix_array_buf[8];
static q3c_coord_t ra_cen_buf, dec_cen_buf, radius_buf, axis_ratio_buf, PA_buf;
static int invocation;
int i;
q3c_ellipse_region ellipse;
extern struct q3c_prm hprm;
q3c_coord_t ra_cen = PG_GETARG_FLOAT8(0); /* ra_cen */
q3c_coord_t dec_cen = PG_GETARG_FLOAT8(1); /* dec_cen */
q3c_coord_t radius = PG_GETARG_FLOAT8(2); /* error radius */
q3c_coord_t axis_ratio = PG_GETARG_FLOAT8(3); /* axis_ratio */
q3c_coord_t PA = PG_GETARG_FLOAT8(4); /* PA */
int iteration = PG_GETARG_INT32(5); /* iteration */
if ( (!isfinite(ra_cen)) || (!isfinite(dec_cen)) || (!isfinite(radius)) )
{
elog(ERROR, "The values of ra,dec,radius are infinites or NaNs");
}
if (invocation == 0)
/* If this is the first invocation of the function */
{
/* I should set invocation=1 ONLY!!! after setting ra_cen_buf, dec_cen_buf and
* ipix_buf. Because if the program will be canceled or crashed
* for some reason the invocation should be == 0
*/
}
else
{
if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) &&
(radius == radius_buf) && (PA == PA_buf) &&
(axis_ratio == axis_ratio_buf))
{
PG_RETURN_INT64(ipix_array_buf[iteration]);
}
}
ra_cen = UNWRAP_RA(ra_cen);
if (q3c_fabs(dec_cen) > 90) {dec_cen = q3c_fmod(dec_cen,90);}
ellipse.ra = ra_cen;
ellipse.dec = dec_cen;
ellipse.rad = radius;
ellipse.e = q3c_sqrt ( 1 - axis_ratio * axis_ratio );
ellipse.PA = PA;
q3c_get_nearby(&hprm, Q3C_ELLIPSE, &ellipse, ipix_array);
for(i = 0; i < 8; i++)
{
ipix_array_buf[i] = ipix_array[i];
}
ra_cen_buf = ra_cen;
dec_cen_buf = dec_cen;
radius_buf = radius;
axis_ratio_buf = axis_ratio;
PA_buf = PA;
invocation = 1;
PG_RETURN_INT64(ipix_array_buf[iteration]);
}
PG_FUNCTION_INFO_V1(pgq3c_radial_query_it);
Datum pgq3c_radial_query_it(PG_FUNCTION_ARGS)
{
extern struct q3c_prm hprm;
q3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
q3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
q3c_coord_t radius = PG_GETARG_FLOAT8(2); /* error radius */
int iteration = PG_GETARG_INT32(3); /* iteration */
int full_flag = PG_GETARG_INT32(4); /* full_flag */
/* 1 means full, 0 means partial */
static q3c_coord_t ra_cen_buf, dec_cen_buf, radius_buf;
static q3c_ipix_t partials[2 * Q3C_NPARTIALS];
static q3c_ipix_t fulls[2 * Q3C_NFULLS];
/* !!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!
* Here the Q3C_NPARTIALS and Q3C_NFULLS is the number of pairs !!! of ranges
* So we should have the array with the size twice bigger
*/
static int invocation;
ra_cen = UNWRAP_RA(ra_cen);
if (q3c_fabs(dec_cen) > 90)
{
elog(ERROR, "The absolute value of declination > 90!");
}
if (invocation == 0)
/* If this is the first invocation of the function */
{
/* I should set invocation=1 ONLY!!! after setting ra_cen_buf, dec_cen_buf and
* ipix_buf. Because if the program will be canceled or crashed
* for some reason the invocation should be == 0
*/
}
else
{
if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) && (radius == radius_buf))
{
if (full_flag)
{
PG_RETURN_INT64(fulls[iteration]);
}
else
{
PG_RETURN_INT64(partials[iteration]);
}
}
}
q3c_radial_query(&hprm, ra_cen, dec_cen, radius, fulls, partials);
ra_cen_buf = ra_cen;
dec_cen_buf = dec_cen;
radius_buf = radius;
invocation = 1;
if (full_flag)
{
PG_RETURN_INT64(fulls[iteration]);
}
else
{
PG_RETURN_INT64(partials[iteration]);
}
}
PG_FUNCTION_INFO_V1(pgq3c_ellipse_query_it);
Datum pgq3c_ellipse_query_it(PG_FUNCTION_ARGS)
{
extern struct q3c_prm hprm;
q3c_coord_t ra_cen = PG_GETARG_FLOAT8(0);
q3c_coord_t dec_cen = PG_GETARG_FLOAT8(1);
q3c_coord_t radius = PG_GETARG_FLOAT8(2); /* Major axis */
q3c_coord_t axis_ratio = PG_GETARG_FLOAT8(3); /* Axis ratio */
q3c_coord_t PA = PG_GETARG_FLOAT8(4); /* PA */
int iteration = PG_GETARG_INT32(5); /* iteration */
int full_flag = PG_GETARG_INT32(6); /* full_flag */
q3c_coord_t ell = q3c_sqrt ( 1 - axis_ratio * axis_ratio );
/* 1 means full, 0 means partial */
static q3c_coord_t ra_cen_buf, dec_cen_buf, radius_buf;
static q3c_ipix_t partials[2 * Q3C_NPARTIALS];
static q3c_ipix_t fulls[2 * Q3C_NFULLS];
/* !!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!
* Here the Q3C_NPARTIALS and Q3C_NFULLS is the number of pairs !!! of ranges
* So we should have the array with the size twice bigger
*/
static int invocation;
ra_cen = UNWRAP_RA(ra_cen);
if (q3c_fabs(dec_cen) > 90)
{
elog(ERROR, "The absolute value of declination > 90!");
}
if (invocation == 0)
/* If this is the first invocation of the function */
{
/* I should set invocation=1 ONLY!!! after setting ra_cen_buf, dec_cen_buf and
* ipix_buf. Because if the program will be canceled or crashed
* for some reason the invocation should be == 0
*/
}
else
{
if ((ra_cen == ra_cen_buf) && (dec_cen == dec_cen_buf) && (radius == radius_buf))
{
if (full_flag)
{
PG_RETURN_INT64(fulls[iteration]);
}
else
{
PG_RETURN_INT64(partials[iteration]);
}
}
}
q3c_ellipse_query(&hprm, ra_cen, dec_cen, radius, ell, PA, fulls,
partials);
ra_cen_buf = ra_cen;
dec_cen_buf = dec_cen;
radius_buf = radius;
invocation = 1;
if (full_flag)
{
PG_RETURN_INT64(fulls[iteration]);
}
else
{
PG_RETURN_INT64(partials[iteration]);
}
}
static q3c_coord_t read_from_array(char **p, bits8 *bitmap, int *bitmask, bool typbyval,
char typalign, int16 typlen)
{
q3c_coord_t val;
/* Taken from /pgsql/src/backend/utils/adt/arrayfuncs.c
* function deconstruct_array
*/
if (bitmap && (*bitmap & *bitmask) == 0)
{
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("null array element not allowed in this context")));
}
val = DatumGetFloat8(fetch_att(*p, typbyval, typlen));
*p = att_addlength_pointer(*p, typlen, PointerGetDatum(p));
*p = (char *) att_align_nominal(*p, typalign);
if (bitmap)
{
*bitmask <<= 1;
if (*bitmask == 0x100)
{
bitmap++;
*bitmask = 1;
}
}
return val;
}
/* Convert the PG array in two c arrays of ra,dec */
static int convert_pgarray2poly(ArrayType *poly_arr, q3c_coord_t *in_ra, q3c_coord_t *in_dec, int *nvert)
{
int poly_nitems = ArrayGetNItems(ARR_NDIM(poly_arr), ARR_DIMS(poly_arr));
Oid element_type = FLOAT8OID;
int identical = 1;
int16 typlen;
bool typbyval;
char typalign;
int i;
q3c_coord_t ra_cur, dec_cur;
char *p;
bits8 *bitmap;
int bitmask;
get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
/* Taken from /pgsql/src/backend/utils/adt/arrayfuncs.c
* function deconstruct_array
*/
if ((poly_nitems % 2) != 0)
{
elog(ERROR, "Invalid array argument!\nThe array should contain even number of elements");
}
else if (poly_nitems <= 4)
{
elog(ERROR, "Invalid polygon! The polygon must have more than two vertices");
}
else if (poly_nitems > (2 * Q3C_MAX_N_POLY_VERTEX))
{
elog(ERROR,"Polygons with more than 100 vertices are not supported");
}
p = ARR_DATA_PTR(poly_arr);
poly_nitems /= 2;
*nvert = poly_nitems;
identical = 1;
bitmap = ARR_NULLBITMAP(poly_arr);
bitmask = 1;
for (i = 0; i < poly_nitems; i++)
{
ra_cur = read_from_array(&p, bitmap, &bitmask, typbyval, typalign, typlen);
dec_cur = read_from_array(&p, bitmap, &bitmask, typbyval, typalign, typlen);
if ((in_ra[i] != ra_cur) || (in_dec[i] != dec_cur))
{
identical = 0;
}
in_ra[i] = ra_cur;
in_dec[i] = dec_cur;
}
return identical;
}
/* Convert Postgresql polygon in two c arrays */
static int convert_pgpoly2poly(POLYGON *poly, q3c_coord_t *ra, q3c_coord_t *dec, int *n)
{
int i, npts = poly->npts;
q3c_coord_t newx, newy;
int identical = 1;
*n = npts;
if (npts < 3)
{
elog(ERROR, "Invalid polygon! The polygon must have more than two vertices");
}
else if (npts > Q3C_MAX_N_POLY_VERTEX)
{
elog(ERROR,"Polygons with more than 100 vertices are not supported");
}
for(i = 0; i < npts; i++)
{
newx = poly->p[i].x;
newy = poly->p[i].y;
if ((newx != ra[i]) || (newy != dec[i])) {identical = 0;}
ra[i] = newx;
dec[i] = newy;
}
return identical;
}
typedef struct q3c_poly_info_type {
/* !!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!
* Here the Q3C_NPARTIALS and Q3C_NFULLS is the number of pairs !!! of ranges
* So we should have the array with the size twice bigger
*/
int ready;
q3c_ipix_t partials[2 * Q3C_NPARTIALS]; /* array of ipixes partially covered */
q3c_ipix_t fulls[2 * Q3C_NFULLS]; /* array of ipixes fully covered */
q3c_coord_t ra[Q3C_MAX_N_POLY_VERTEX],
dec[Q3C_MAX_N_POLY_VERTEX], x[Q3C_MAX_N_POLY_VERTEX],y[Q3C_MAX_N_POLY_VERTEX],
ax[Q3C_MAX_N_POLY_VERTEX], ay[Q3C_MAX_N_POLY_VERTEX];
q3c_coord_t xpj[3][Q3C_MAX_N_POLY_VERTEX], ypj[3][Q3C_MAX_N_POLY_VERTEX],
axpj[3][Q3C_MAX_N_POLY_VERTEX], aypj[3][Q3C_MAX_N_POLY_VERTEX];
// arrays storing the ra,dec ,projected x,y
char faces[6];