60 if (level == H3C_ERROR) {
65 vsprintf(str, fmt, l);
67 fprintf(stderr,
"(ERROR)H3C: %s\n", str);
74 static struct timeval tv;
75 static struct timezone tz;
82 vsprintf(str, fmt, l);
84 if (level < 0 || t0 == -1) {
86 fprintf(stderr,
"(debug)H3C: begin time stat\n");
88 elog(DEBUG1,
"H3C: begin time stat");
90 gettimeofday(&tv, &tz);
91 t0 = tv.tv_sec*1000000+tv.tv_usec;
92 if (level < 0)
return;
95 gettimeofday(&tv, &tz);
96 t = (tv.tv_sec*1000000+tv.tv_usec)-t0;
98 fprintf(stderr,
"(%s)H3C: %s [%.3f msec]\n",
99 (level==H3C_DEBUG)?
"debug":(level==H3C_INFO)?
"info":
"error",
101 if (level == H3C_ERROR) exit(1);
103 elog(level,
"H3C: %s [%.3f msec]",
127 return H3C_PI_2-theta;
138 if (dec>H3C_PI_2) dec=H3C_PI_2;
139 else if(dec<-1*H3C_PI_2) dec=-1*H3C_PI_2;
151 if (phi > H3C_PI)
return phi-H3C_2PI;
163 if (ra < 0)
return H3C_2PI+ra;
175 if (nside <= 0 || (nside & (nside-1))) {
176 h3c_log(H3C_ERROR,
"unvalid nside");
180 while (nside > 0x0000FFFF) { res+=16; nside>>=16; }
181 if (nside > 0x000000FF) { res|=8; nside>>=8; }
182 if (nside > 0x0000000F) { res|=4; nside>>=4; }
183 if (nside > 0x00000003) { res|=2; nside>>=2; }
184 if (nside > 0x00000001) { res|=1; }
195 return 12*nside*nside;
237 #define H3C_MAX_MEM_IPIX 6
240 void h3c_sorted_list_insert(h3c_ipix_t *list,
int count, h3c_ipix_t ipix) {
243 for (i = count-1; i > -1; i--) {
244 if (list[i] < ipix)
return;
255 h3c_ipix_t *h3c_disk_ipix_1(h3c_coord_t ra,
262 static h3c_ipix_t ipix[H3C_MAX_MEM_IPIX];
264 h3c_coord_t theta,phi, ra0, dec0;
268 h3c_ang2pix_nest(nside,
271 (h3c_ipix_t *) &ipix0);
276 for (i = 0; i < 8 ; i++) {
277 h3c_pix2ang_nest(nside,
283 if (
h3c_dist(ra, dec, ra0, dec0) < radius) {
284 h3c_log(H3C_DEBUG,
"ADD ipix %lld", near[i]);
285 if ((*count) > H3C_MAX_MEM_IPIX-1)
h3c_log(H3C_ERROR,
"too much ipix!");
286 h3c_sorted_list_insert(ipix, *count, near[i]);
291 *count = (*count > 4) ? 4: *count;
298 static int h3c_ipix_t_cmp(
const void *a,
const void *b) {
299 h3c_ipix_t t =(*(h3c_ipix_t*)a)-*((h3c_ipix_t*)b);
311 static int h3c_split_list(h3c_ipix_t *in,
319 for (i = 1; i < nin; i++) {
320 if (in[i]-in[i-1] > step) {
329 if (j+1 < nout) out[++j] = in[i-1];
350 #ifndef LIMIT_CIRCLE_ORDER_0
351 #define LIMIT_CIRCLE_ORDER_0 2/60.
353 #ifndef LIMIT_CIRCLE_ORDER_1
354 #define LIMIT_CIRCLE_ORDER_1 0.5
356 #ifndef LIMIT_CIRCLE_ORDER_2
357 #define LIMIT_CIRCLE_ORDER_2 1
359 #ifndef LIMIT_CIRCLE_ORDER_3
360 #define LIMIT_CIRCLE_ORDER_3 15
362 #ifndef LIMIT_CIRCLE_ORDER_4
363 #define LIMIT_CIRCLE_ORDER_4 40
366 int h3c_disk_ipix_it(h3c_coord_t ra,
373 int j, count, step, gap, nside0 = nside;
375 h3c_log(H3C_DEBUG,
"begin getting ipix %d", nside);
378 all = h3c_query_disc(ra, dec, radius, nside, &count);
382 if (radius >= LIMIT_CIRCLE_ORDER_4) nside0 = nside>>6;
383 else if (radius >= LIMIT_CIRCLE_ORDER_3) nside0 = nside>>5;
384 else if (radius >= LIMIT_CIRCLE_ORDER_2) nside0 = nside>>4;
385 else if (radius >= LIMIT_CIRCLE_ORDER_1) nside0 = nside>>3;
386 else nside0 = nside>>2;
388 if (nside0 < 512) nside0 = 512;
389 h3c_log(H3C_INFO,
"decrease nside to %d", nside0);
395 h3c_log(H3C_DEBUG,
"end getting ipix");
397 if (count > 0 )
h3c_log(H3C_ERROR,
"index H3C uses too much ipix !");
402 qsort(all, count,
sizeof(h3c_ipix_t), h3c_ipix_t_cmp);
403 h3c_log(H3C_DEBUG,
"end sorting");
406 if (radius >= 10) step = 256;
407 else if (radius >= 5) step = 64;
413 while ( (j = h3c_split_list(all, count, list, listsize, gap)) < 0 ) {
417 h3c_log(H3C_DEBUG,
"end split");
419 if (nside0 != nside) {
420 for (j=0; j<listsize; j++) {
432 static long long sum = 0;
435 for (i = 0; i < j/2; i++) e += list[2*i+1]-list[2*i]+1;
436 h3c_log(H3C_DEBUG,
"STAT (h3c_disk_ipix_it) #ipix=%d =split=> %d (gap=%d)",
441 max = (count>max) ? count : max;
442 if (npass % 10000 == 0)
h3c_log(H3C_INFO,
"npass=%d moy(#ipix)=%f max(#ipix)=%d", npass, (1.*sum/npass), max);
465 h3c_coord_t ra1Rad = ra1*H3C_DEGRA;
466 h3c_coord_t ra2Rad = ra2*H3C_DEGRA;
467 h3c_coord_t dec1Rad = dec1*H3C_DEGRA;
468 h3c_coord_t dec2Rad = dec2*H3C_DEGRA;
470 h3c_coord_t d = pow(sin((dec1Rad-dec2Rad)/2.),2);
471 d += pow(sin((ra1Rad-ra2Rad)/2.), 2)*cos(dec1Rad)*cos(dec2Rad);
472 return (2.*asin(sqrt(d)))*H3C_RADEG;
491 h3c_coord_t ra1Rad = ra1*H3C_DEGRA;
492 h3c_coord_t ra2Rad = ra2*H3C_DEGRA;
493 h3c_coord_t dec1Rad = dec1*H3C_DEGRA;
494 h3c_coord_t dec2Rad = dec2*H3C_DEGRA;
496 h3c_coord_t d = pow(sinl((dec1Rad-dec2Rad)/2.),2);
497 d += pow(sinl((ra1Rad-ra2Rad)/2.), 2)*cosl(dec1Rad)*cos(dec2Rad);
503 static h3c_coord_t h3c_polygon_radius(h3c_coord_t *in_ra,
508 h3c_coord_t center[2];
509 h3c_coord_t d, dist = 0;
512 for (i=0; i<n; i++) {
513 d =
h3c_dist(center[0], center[1], in_ra[i], in_dec[i]);
514 if (d > dist) dist = d;
532 #ifndef LIMIT_POLYGONE_ORDER_0
533 #define LIMIT_POLYGONE_ORDER_0 1/60.
535 #ifndef LIMIT_POLYGONE_ORDER_1
536 #define LIMIT_POLYGONE_ORDER_1 0.5
538 #ifndef LIMIT_POLYGONE_ORDER_2
539 #define LIMIT_POLYGONE_ORDER_2 1
542 int h3c_polygon_ipix_it(h3c_coord_t *in_ra,
549 int j, count, gap, step;
553 h3c_coord_t sz = h3c_polygon_radius(in_ra, in_dec, n);
554 h3c_log(H3C_INFO,
"size=%f",sz);
556 if (sz >= LIMIT_POLYGONE_ORDER_2) nside0 = nside>>4;
557 else if (sz >= LIMIT_POLYGONE_ORDER_1) nside0 = nside>>3;
558 else nside0 = nside>>2;
560 if (nside0 < 64) nside0 = 64;
561 h3c_log(H3C_INFO,
"decrease nside to %d (circumscribed circle r=%g)",
565 h3c_log(H3C_DEBUG,
"begin getting ipix");
569 h3c_log(H3C_INFO,
"no healpix found ?..");
574 qsort(all, count,
sizeof(h3c_ipix_t), h3c_ipix_t_cmp);
575 h3c_log(H3C_DEBUG,
"end sorting (%d items)", count);
577 if (count > 1000000) step = 4096;
578 else if (count > 100000) step = 1024;
579 else if (count > 10000) step = 32;
583 while ( (j = h3c_split_list(all, count, list, listsize, gap))<0 ) {
586 h3c_log(H3C_DEBUG,
"end split");
590 if (nside0 != nside) {
592 for (i=0; i<listsize/2; i++) {
601 for (i = 0; i < j/2; i++) e += (list[2*i+1]-list[2*i])+1;
602 h3c_log(H3C_DEBUG,
"STAT (h3c_polygon_ipix_it) #ipix=%d =split=> %d (gap=%d)",
616 h3c_coord_t alpha1, h3c_coord_t delta01, h3c_coord_t d0,
617 h3c_coord_t e, h3c_coord_t PA0)
619 h3c_coord_t d_alpha = (alpha1 - alpha) * H3C_DEGRA;
620 h3c_coord_t delta1 = delta01 * H3C_DEGRA;
621 h3c_coord_t delta = delta0 * H3C_DEGRA;
622 h3c_coord_t PA = PA0 * H3C_DEGRA;
623 h3c_coord_t d = d0 * H3C_DEGRA;
625 h3c_coord_t t1 = cos(d_alpha);
626 h3c_coord_t t22 = sin(d_alpha);
627 h3c_coord_t t3 = cos(delta1);
628 h3c_coord_t t32 = sin(delta1);
629 h3c_coord_t t6 = cos(delta);
630 h3c_coord_t t26 = sin(delta);
631 h3c_coord_t t9 = cos(d);
632 h3c_coord_t t55 = sin(d);
659 if ((t3 * t6 * t1 + t32 * t26) < 0)
684 t36 = 2.0*t31*t32*t26*t6;
691 t60 = -t8+t5*t11+2.0*t5*t15-t5*t19-2.0*t1*t4*t22*t10*t24*t13*t26-t36+2.0*t37*t38*t10-2.0*t37*t38*t15-t45*t14-t45*t2+2.0*t22*t3*t32*t6*t24*t10*t13-t56+t7-t11+t4-t57+t57*t10+t19-t18*t45;
693 t63 = t60*t61+t8+t57-t4-t7+t56+t36;
698 void h3c_pix2ang_nest(
long nside,
702 h3cpp_pix2ang_nest(nside, ipnest, theta, phi);
706 void h3c_ang2pix_nest(
long nside,
709 h3c_ipix_t *ipnest) {
710 h3cpp_ang2pix_nest(nside, theta, phi, ipnest);
714 void h3c_ring2nest(
long nside,
716 h3c_ipix_t *ipnest) {