Debian Codegraph @

gov/fnal/eag/healpix/PixTools.java

raw
1 package gov.fnal.eag.healpix;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Vector;
6 import java.io.FileNotFoundException;
7 import java.io.PrintStream;
8 import java.lang.Number;
9
10 import javax.vecmath.Vector3d;
11
12 /**
13 *
14 * contains methods translated from HEALPix Fortran90
15 * with increased map resolution in comparison to original Fortran code.
16 *
17 * @author N Kuropatkin
18 *
19 * Created on Mar 10, 2005
20 * Modified on December 18 2007
21 * Corrected arithmetic and mistyping April 20 2008
22 *
23 * @author Mark Taylor made modifications to make the class thread safe 11-Jan-2012
24 *
25 * <p>
26 * All methods are thread safe. This class can be used as a singleton,
27 * the singleton instance being available from the {@link #getInstance} method.
28 * For compatibility with previous versions however it is possible to
29 * construct new instances using the default constructor.
30 *</p>
31 *
32 */
33 public class PixTools {
34
35 private static final double twothird = 2. / 3.;
36
37 private static final double PI = Math.PI;
38
39 private static final double TWOPI = 2. * PI;
40
41
42
43 private static final double HALFPI = PI / 2.0;
44
45 private static final int ns_max = 1048576; // 2^20
46
47 //
48 private static final int xmax = 4096;
49 //
50 private static final int pixmax = 262144;
51
52 //
53 private static final int xmid = 512;
54
55 private static final long[] x2pix = new long[xmax+1];
56
57 private static final long[] y2pix = new long[xmax+1];
58
59 private static final long[] pix2x = new long[pixmax+1];
60
61 private static final long[] pix2y = new long[pixmax+1];
62
63
64
65 static {
66 mk_xy2pix();
67 }
68
69 /** Singleton instance. */
70 private static final PixTools pixTools = new PixTools();
71
72 /**
73 * default constructor
74 *
75 *
76 */
77 public PixTools() {
78 }
79
80 /**
81 * finds pixels having a colatitude (measured from North pole) :
82 * theta1 < colatitude < theta2 with 0 <= theta1 < theta2 <= Pi
83 * if theta2 < theta1
84 * then pixels with 0 <= colatitude < theta2 or theta1 < colatitude < Pi are
85 * returned
86 *
87 * @param nside
88 * long the map resolution parameter
89 * @param theta1
90 * lower edge of the colatitude
91 * @param theta2
92 * upper edge of the colatitude
93 * @param nest
94 * long if = 1 result is in NESTED scheme
95 * @return ArrayList of pixel numbers (long)
96 * @throws Exception
97 * @throws IllegalArgumentException
98 */
99 public ArrayList query_strip(long nside, double theta1, double theta2,
100 long nest) throws Exception {
101 ArrayList res = new ArrayList();
102 ArrayList listir = new ArrayList();
103 long npix, nstrip;
104 long iz, irmin, irmax;
105 int is;
106 double phi0, dphi;
107 double[] colrange = new double[4];
108 boolean nest_flag = false;
109 String SID = " QUERY_STRIP";
110 /* ---------------------------------------- */
111 npix = Nside2Npix(nside);
112 if (nest == 1)
113 nest_flag = true;
114 if (npix < 0) {
115 throw new IllegalArgumentException(SID + " Nside should be power of 2");
116 }
117 if ((theta1 < 0.0 || theta1 > PI) || (theta2 < 0.0 || theta2 > PI)) {
118 throw new IllegalArgumentException(SID + " Illegal value of theta1, theta2");
119 }
120 if (theta1 <= theta2) {
121 nstrip = 1;
122 colrange[0] = theta1;
123 colrange[1] = theta2;
124 } else {
125 nstrip = 2;
126 colrange[0] = 0.0;
127 colrange[1] = theta2;
128 colrange[2] = theta1;
129 colrange[3] = PI;
130 }
131 /* loops on strips */
132 for (is = 0; is < nstrip; is++) {
133 irmin = RingNum(nside, Math.cos(colrange[2 * is]));
134 irmax = RingNum(nside, Math.cos(colrange[2 * is + 1]));
135 /* loop on ring number */
136 for (iz = irmin; iz <= irmax; iz++) {
137 phi0 = 0.;
138 dphi = PI;
139 listir = InRing(nside, iz, phi0, dphi, nest_flag);
140 res.addAll(listir);
141 }
142 }
143 return res;
144 }
145
146 /**
147 * finds pixels that lay within a CONVEX polygon defined by its vertex on
148 * sphere
149 *
150 * @param nside
151 * the map resolution
152 * @param vlist
153 * ArrayList of vectors defining the polygon vertices
154 * @param nest
155 * if set to 1 use NESTED scheme
156 * @param inclusive
157 * if set 1 returns all pixels crossed by polygon boundaries
158 * @return ArrayList of pixels
159 *
160 * algorithm: the polygon is divided into triangles vertex 0 belongs to all
161 * triangles
162 * @throws Exception
163 * @throws IllegalArgumentException
164 */
165 public ArrayList query_polygon(long nside, ArrayList vlist, long nest,
166 long inclusive) throws Exception {
167 ArrayList res = new ArrayList();
168 int nv = vlist.size();
169 Vector3d vp0, vp1, vp2;
170 Vector3d vo;
171 ArrayList vvlist = new ArrayList();
172 // double surface, fsky;
173 double hand;
174 double[] ss = new double[nv];
175 // int n_in_trg, ilist, ntl;
176 long npix;
177 int ix = 0;
178
179 int n_remain, np, nm, nlow;
180 String SID = "QUERY_POLYGON";
181
182 // System.out.println("Start polygon");
183 for (int k = 0; k < nv; k++)
184 ss[k] = 0.;
185 /* -------------------------------------- */
186 n_remain = nv;
187 if (n_remain < 3) {
188 throw new IllegalArgumentException(SID + " Number of vertices should be >= 3");
189 }
190 /*---------------------------------------------------------------- */
191 /* Check that the poligon is convex or has only one concave vertex */
192 /*---------------------------------------------------------------- */
193 int i0 = 0;
194 int i2 = 0;
195 if (n_remain > 3) { // a triangle is always convex
196 for (int i1 = 1; i1 <= n_remain - 1; i1++) { // in [0,n_remain-1]
197 i0 = (int) BitManipulation.MODULO(i1 - 1, n_remain);
198 i2 = (int) BitManipulation.MODULO(i1 + 1, n_remain);
199 vp0 = (Vector3d) vlist.get(i0); // select vertices by 3
200 // neighbour
201 vp1 = (Vector3d) vlist.get(i1);
202 vp2 = (Vector3d) vlist.get(i2);
203 // computes handedness (v0 x v2) . v1 for each vertex v1
204 vo = new Vector3d(crossProduct(vp0, vp2));
205 hand = dotProduct(vo, vp1);
206 if (hand >= 0.) {
207 ss[i1] = 1.0;
208 } else {
209 ss[i1] = -1.0;
210 }
211
212 }
213 np = 0; // number of vert. with positive handedness
214 for (int i = 0; i < nv; i++) {
215 if (ss[i] > 0.)
216 np++;
217 }
218 nm = n_remain - np;
219
220 nlow = Math.min(np, nm);
221
222 if (nlow != 0) {
223 if (nlow == 1) { // only one concave vertex
224 if (np == 1) { // ix index of the vertex in the list
225 for (int k = 0; k < nv - 1; k++) {
226 if (Math.abs(ss[k] - 1.0) <= 1.e-12) {
227 ix = k;
228 break;
229 }
230 }
231 } else {
232 for (int k = 0; k < nv - 1; k++) {
233 if (Math.abs(ss[k] + 1.0) <= 1.e-12) {
234 ix = k;
235 break;
236 }
237 }
238 }
239
240 // rotate pixel list to put that vertex in #0
241 int n_rot = vlist.size() - ix;
242 int ilast = vlist.size() - 1;
243 for (int k = 0; k < n_rot; k++) {
244 Vector3d temp = new Vector3d((Vector3d) vlist
245 .get(ilast));
246 vlist.remove(ilast);
247 vlist.add(0, temp);
248 }
249 }
250 if (nlow > 1) { // more than 1concave vertex
251 System.out
252 .println(" The polygon has more than one concave vertex");
253 System.out.println(" The result is unpredictable");
254 }
255 }
256 }
257 /* fill the polygon, one triangle at a time */
258 npix = (long) Nside2Npix(nside);
259 while (n_remain >= 3) {
260 vp0 = (Vector3d) vlist.get(0);
261 vp1 = (Vector3d) vlist.get(n_remain - 2);
262 vp2 = (Vector3d) vlist.get(n_remain - 1);
263
264 /* find pixels within the triangle */
265 ArrayList templist = new ArrayList();
266 templist = query_triangle(nside, vp0, vp1, vp2, nest, inclusive);
267
268 vvlist.addAll(templist);
269 n_remain--;
270 }
271 /* make final pixel list */
272 npix = vvlist.size();
273 long[] pixels = new long[(int)npix];
274 for (int i = 0; i < npix; i++) {
275 pixels[i] = ((Long) vvlist.get(i)).longValue();
276 }
277 Arrays.sort(pixels);
278 int k = 0;
279 res.add(k, new Long(pixels[0]));
280 for (int i = 1; i < pixels.length; i++) {
281 if (pixels[i] > pixels[i - 1]) {
282 k++;
283 res.add(k, new Long(pixels[i]));
284 }
285 }
286
287 return res;
288 }
289
290 /**
291 * generates a list of pixels that lay inside a triangle defined by
292 * the three vertex vectors
293 *
294 * @param nside
295 * long map resolution parameter
296 * @param v1
297 * Vector3d defines one vertex of the triangle
298 * @param v2
299 * Vector3d another vertex
300 * @param v3
301 * Vector3d yet another one
302 * @param nest
303 * long 0 (default) RING numbering scheme, if set to 1 the NESTED
304 * scheme will be used.
305 * @param inclusive
306 * long 0 (default) only pixels whose centers are inside the
307 * triangle will be listed, if set to 1 all pixels overlaping the
308 * triangle will be listed
309 * @return ArrayList with pixel numbers
310 * @throws Exception
311 * @throws IllegalArgumentException
312 */
313 public ArrayList query_triangle(long nside, Vector3d v1, Vector3d v2,
314 Vector3d v3, long nest, long inclusive) throws Exception {
315 ArrayList res;
316 res = new ArrayList();
317 ArrayList listir;
318 long npix, iz, irmin, irmax, n12, n123a, n123b, ndom = 0;
319 boolean test1, test2, test3;
320 boolean test1a, test1b, test2a, test2b, test3a, test3b;
321 double dth1, dth2, determ, sdet;
322 double zmax, zmin, z1max, z1min, z2max, z2min, z3max, z3min;
323 double z, tgth, st, offset, sin_off;
324 double phi_pos, phi_neg;
325 Vector3d[] vv = new Vector3d[3];
326 Vector3d[] vo = new Vector3d[3];
327 double[] sprod = new double[3];
328 double[] sto = new double[3];
329 double[] phi0i = new double[3];
330 double[] tgthi = new double[3];
331 double[] dc = new double[3];
332 double[][] dom = new double[3][2];
333 double[] dom12 = new double[4];
334 double[] dom123a = new double[4];
335 double[] dom123b = new double[4];
336 double[] alldom = new double[6];
337 double a_i, b_i, phi0, dphiring;
338 long idom;
339 // long nir, ip, status;
340 boolean do_inclusive = false;
341 boolean do_nest = false;
342 String SID = "QUERY_TRIANGLE";
343 long nsidesq = nside * nside;
344 /* */
345
346 // System.out.println("in query_triangle");
347 npix = Nside2Npix(nside);
348 if (npix < 0) {
349 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
350 }
351 if (inclusive == 1)
352 do_inclusive = true;
353 if (nest == 1)
354 do_nest = true;
355 vv[0] = new Vector3d(v1);
356 vv[0].normalize();
357 vv[1] = new Vector3d(v2);
358 vv[1].normalize();
359 vv[2] = new Vector3d(v3);
360 vv[2].normalize();
361
362 /* */
363 dth1 = 1.0 / (3.0 * nsidesq);
364 dth2 = 2.0 / (3.0 * nside);
365 /*
366 * determ = (v1 X v2) . v3 determines the left ( <0) or right (>0)
367 * handedness of the triangle
368 */
369 Vector3d vt = new Vector3d(0., 0., 0.);
370 vt = crossProduct(vv[0], vv[1]);
371 determ = dotProduct(vt, vv[2]);
372
373 if (Math.abs(determ) < 1.0e-20) {
374 throw new HealpixException(
375 SID
376 + ": the triangle is degenerated - query cannot be performed");
377 }
378 if (determ >= 0.) { // The sign of determinant
379 sdet = 1.0;
380 } else {
381 sdet = -1.0;
382 }
383
384 sprod[0] = dotProduct(vv[1], vv[2]);
385 sprod[1] = dotProduct(vv[2], vv[0]);
386 sprod[2] = dotProduct(vv[0], vv[1]);
387 /* vector orthogonal to the great circle containing the vertex doublet */
388
389 vo[0] = crossProduct(vv[1], vv[2]);
390 vo[1] = crossProduct(vv[2], vv[0]);
391 vo[2] = crossProduct(vv[0], vv[1]);
392 vo[0].normalize();
393 vo[1].normalize();
394 vo[2].normalize();
395
396 /* test presence of poles in the triangle */
397 zmax = -1.0;
398 zmin = 1.0;
399 test1 = (vo[0].z * sdet >= 0.0); // north pole in hemisphere defined by
400 // 2-3
401 test2 = (vo[1].z * sdet >= 0.0); // north pole in the hemisphere defined
402 // by 1-2
403 test3 = (vo[2].z * sdet >= 0.0); // north pole in hemisphere defined by
404 // 1-3
405 if (test1 && test2 && test3)
406 zmax = 1.0; // north pole in the triangle
407 if ((!test1) && (!test2) && (!test3))
408 zmin = -1.0; // south pole in the triangle
409 /* look for northenest and southernest points in the triangle */
410 test1a = ((vv[2].z - sprod[0] * vv[1].z) >= 0.0); // segment 2-3
411 test1b = ((vv[1].z - sprod[0] * vv[2].z) >= 0.0);
412 test2a = ((vv[2].z - sprod[1] * vv[0].z) >= 0.0); // segment 1-3
413 test2b = ((vv[0].z - sprod[1] * vv[2].z) >= 0.0);
414 test3a = ((vv[1].z - sprod[2] * vv[0].z) >= 0.0); // segment 1-2
415 test3b = ((vv[0].z - sprod[2] * vv[1].z) >= 0.0);
416
417 /* sin of theta for orthogonal vector */
418 for (int i = 0; i < 3; i++) {
419 sto[i] = Math.sqrt((1.0 - vo[i].z) * (1.0 + vo[i].z));
420 }
421 /*
422 * for each segment ( side of the triangle ) the extrema are either -
423 * -the 2 vertices - one of the vertices and a point within the segment
424 */
425 // segment 2-3
426 z1max = vv[1].z;
427 z1min = vv[2].z;
428 // if (test1a == test1b) {
429 // zz = sto[0];
430 // if (vv[1].z + vv[2].z >= 0.0) {
431 // z1max = zz;
432 // } else {
433 // z1min = -zz;
434 // }
435 // }
436 // segment 1-3
437 z2max = vv[2].z;
438 z2min = vv[0].z;
439 // if (test2a == test2b) {
440 // zz = sto[1];
441 // if (vv[0].z + vv[2].z >= 0.0) {
442 // z2max = zz;
443 // } else {
444 // z2min = -zz;
445 // }
446 // }
447 // segment 1-2
448 z3max = vv[0].z;
449 z3min = vv[1].z;
450 // if (test3a == test3b) {
451 // zz = sto[2];
452 // if (vv[0].z + vv[1].z >= 0.0) {
453 // z3max = zz;
454 // } else {
455 // z3min = -zz;
456 // }
457 // }
458
459 zmax = Math.max(Math.max(z1max, z2max), Math.max(z3max, zmax));
460 zmin = Math.min(Math.min(z1min, z2min), Math.min(z3min, zmin));
461 /*
462 * if we are inclusive, move upper point up, and lower point down, by a
463 * half pixel size
464 */
465 offset = 0.0;
466 sin_off = 0.0;
467 if (do_inclusive) {
468 offset = PI / (4.0 * nside); // half pixel size
469 sin_off = Math.sin(offset);
470 zmax = Math.min(1.0, Math.cos(Math.acos(zmax) - offset));
471 zmin = Math.max(-1.0, Math.cos(Math.acos(zmin) + offset));
472 }
473
474 irmin = RingNum(nside, zmax);
475 irmax = RingNum(nside, zmin);
476
477 // System.out.println("irmin = " + irmin + " irmax =" + irmax);
478
479 /* loop on the rings */
480 for (int i = 0; i < 3; i++) {
481 tgthi[i] = -1.0e30 * vo[i].z;
482 phi0i[i] = 0.0;
483 }
484 for (int j = 0; j < 3; j++) {
485 if (sto[j] > 1.0e-10) {
486 tgthi[j] = -vo[j].z / sto[j]; // - cotan(theta_orth)
487
488 phi0i[j] = Math.atan2(vo[j].y, vo[j].x); // Should make it 0-2pi
489 // ?
490 /* Bring the phi0i to the [0,2pi] domain if need */
491
492 if (phi0i[j] < 0.) {
493 phi0i[j] = BitManipulation.MODULO(
494 (Math.atan2(vo[j].y, vo[j].x) + TWOPI), TWOPI); // [0-2pi]
495 }
496
497 }
498 }
499 /*
500 * the triangle boundaries are geodesics: intersection of the sphere
501 * with plans going through (0,0,0) if we are inclusive, the boundaries
502 * are the intersection of the sphere with plains pushed outward by
503 * sin(offset)
504 */
505 // double temp = 0.;
506 boolean found = false;
507 for (iz = irmin; iz <= irmax; iz++) {
508 found = false;
509 if (iz <= nside - 1) { // North polar cap
510 z = 1.0 - iz * iz * dth1;
511 } else if (iz <= 3 * nside) { // tropical band + equator
512 z = (2.0 * nside - iz) * dth2;
513 } else {
514 z = -1.0 + (4.0 * nside - iz) * (4.0 * nside - iz) * dth1;
515 }
516
517 /* computes the 3 intervals described by the 3 great circles */
518 st = Math.sqrt((1.0 - z) * (1.0 + z));
519 tgth = z / st; // cotan(theta_ring)
520 for (int j = 0; j < 3; j++) {
521 dc[j] = tgthi[j] * tgth - sdet * sin_off
522 / ((sto[j] + 1.0e-30) * st);
523
524 }
525 for (int k = 0; k < 3; k++) {
526 if (dc[k] * sdet <= -1.0) { // the whole iso-latitude ring is on
527 // right side of the great circle
528 dom[k][0] = 0.0;
529 dom[k][1] = TWOPI;
530 } else if (dc[k] * sdet >= 1.0) { // all on the wrong side
531 dom[k][0] = -1.000001 * (k + 1);
532 dom[k][1] = -1.0 * (k + 1);
533 } else { // some is good some is bad
534 phi_neg = phi0i[k] - (Math.acos(dc[k]) * sdet);
535 phi_pos = phi0i[k] + (Math.acos(dc[k]) * sdet);
536 //
537 if (phi_pos < 0.)
538 phi_pos += TWOPI;
539 if (phi_neg < 0.)
540 phi_neg += TWOPI;
541
542 //
543
544 dom[k][0] = BitManipulation.MODULO(phi_neg, TWOPI);
545 dom[k][1] = BitManipulation.MODULO(phi_pos, TWOPI);
546
547 }
548 //
549
550 }
551 /* identify the intersections (0,1,2 or 3) of the 3 intervals */
552
553 dom12 = intrs_intrv(dom[0], dom[1]);
554 n12 = dom12.length / 2;
555 if (n12 != 0) {
556 if (n12 == 1) {
557 dom123a = intrs_intrv(dom[2], dom12);
558 n123a = dom123a.length / 2;
559
560 if (n123a == 0)
561 found = true;
562 if (!found) {
563 for (int l = 0; l < dom123a.length; l++) {
564 alldom[l] = dom123a[l];
565 }
566
567 ndom = n123a; // 1 or 2
568 }
569 }
570 if (!found) {
571 if (n12 == 2) {
572 double[] tmp = { dom12[0], dom12[1] };
573 dom123a = intrs_intrv(dom[2], tmp);
574 double[] tmp1 = { dom12[2], dom12[3] };
575 dom123b = intrs_intrv(dom[2], tmp1);
576 n123a = dom123a.length / 2;
577 n123b = dom123b.length / 2;
578 ndom = n123a + n123b; // 0, 1, 2 or 3
579
580 if (ndom == 0)
581 found = true;
582 if (!found) {
583 if (n123a != 0) {
584 for (int l = 0; l < 2 * n123a; l++) {
585 alldom[l] = dom123a[l];
586 }
587 }
588 if (n123b != 0) {
589 for (int l = 0; l < 2 * n123b; l++) {
590 alldom[(int) (l + 2 * n123a)] = dom123b[l];
591 }
592 }
593 if (ndom > 3) {
594 throw new HealpixException(SID
595 + ": too many intervals found");
596 }
597 }
598 }
599 }
600 if (!found) {
601 for (idom = 0; idom < ndom; idom++) {
602 a_i = alldom[(int) (2 * idom)];
603 b_i = alldom[(int) (2 * idom + 1)];
604 phi0 = (a_i + b_i) / 2.0;
605 dphiring = Math.abs(b_i - a_i) / 2.0;
606
607 if (dphiring < 0.0) {
608 phi0 += PI;
609 dphiring += PI;
610 }
611
612 /* finds pixels in the triangle on that ring */
613 listir = InRing(nside, iz, phi0, dphiring, do_nest);
614 res.addAll(listir);
615
616 }
617 }
618 }
619
620 }
621 return res;
622 }
623
624 /**
625 * computes the intersection di of 2 intervals d1 (= [a1,b1])
626 * and d2 (= [a2,b2]) on the periodic domain (=[A,B] where A and B
627 * arbitrary) ni is the resulting number of intervals (0,1, or 2) if a1 <b1
628 * then d1 = {x |a1 <= x <= b1} if a1>b1 then d1 = {x | a1 <=x <= B U A <=x
629 * <=b1}
630 *
631 * @param d1 double[] first interval
632 * @param d2 double[] second interval
633 * @return double[] one or two intervals intersections
634 */
635 public double[] intrs_intrv(double[] d1, double[] d2) {
636 double[] res;
637 double epsilon = 1.0e-10;
638 // double temp = 0.;
639 // int ni;
640 double[] dk;
641 double[] di = { 0. };
642 int ik = 0;
643 boolean tr12, tr21, tr34, tr43, tr13, tr31, tr24, tr42, tr14, tr32;
644 /* */
645
646 tr12 = (d1[0] < d1[1] + epsilon);
647 tr21 = !tr12; // d1[0] >= d1[1]
648 tr34 = (d2[0] < d2[1] + epsilon);
649 tr43 = !tr34; // d2[0]>d2[1]
650 tr13 = (d1[0] < d2[0] + epsilon); // d2[0] can be in interval
651 tr31 = !tr13; // d1[0] in longerval
652 tr24 = (d1[1] < d2[1] + epsilon); // d1[1] upper limit
653 tr42 = !tr24; // d2[1] upper limit
654 tr14 = (d1[0] < d2[1] + epsilon); // d1[0] in interval
655 tr32 = (d2[0] < d1[1] + epsilon); // d2[0] in interval
656
657 ik = 0;
658 dk = new double[] { -1.0e9, -1.0e9, -1.0e9, -1.0e9 };
659 /* d1[0] lower limit case 1 */
660 if ((tr34 && tr31 && tr14) || (tr43 && (tr31 || tr14))) {
661 ik++; // ik = 1;
662 dk[ik - 1] = d1[0]; // a1
663
664 }
665 /* d2[0] lower limit case 1 */
666 if ((tr12 && tr13 && tr32) || (tr21 && (tr13 || tr32))) {
667 ik++; // ik = 1
668 dk[ik - 1] = d2[0]; // a2
669
670 }
671 /* d1[1] upper limit case 2 */
672 if ((tr34 && tr32 && tr24) || (tr43 && (tr32 || tr24))) {
673 ik++; // ik = 2
674 dk[ik - 1] = d1[1]; // b1
675
676 }
677 /* d2[1] upper limit case 2 */
678 if ((tr12 && tr14 && tr42) || (tr21 && (tr14 || tr42))) {
679 ik++; // ik = 2
680 dk[ik - 1] = d2[1]; // b2
681
682 }
683 di = new double[1];
684 di[0] = 0.;
685 switch (ik) {
686
687 case 2:
688 di = new double[2];
689
690 di[0] = dk[0] - epsilon;
691 di[1] = dk[1] + epsilon;
692 break;
693 case 4:
694
695 di = new double[4];
696 di[0] = dk[0] - epsilon;
697 di[1] = dk[3] + epsilon;
698 di[2] = dk[1] - epsilon;
699 di[3] = dk[2] + epsilon;
700 break;
701 }
702 res = di;
703
704 return res;
705 }
706
707 /**
708 * an obsolete method. Use query_disc instead.
709 *
710 * @param nside
711 * @param vector0
712 * @param radius
713 * @return - ArrayList of long
714 */
715 public ArrayList getDisc_ring(long nside, Vector3d vector0, double radius) {
716 ArrayList res;
717 int nest = 0;
718 int inclusive = 0;
719 res = query_disc(nside, vector0, radius, nest, inclusive);
720 return res;
721 }
722
723 /**
724 * generates in the RING or NESTED scheme all pixels that lays within an
725 * angular distance Radius of the center.
726 *
727 * @param nside
728 * long map resolution
729 * @param vector
730 * Vector3d pointing to the disc center
731 * @param radius
732 * double angular radius of the disc (in RADIAN )
733 * @param nest
734 * int 0 (default) if output is in RING scheme, if set to 1
735 * output is in NESTED
736 * @param inclusive
737 * int 0 (default) only pixels whose centers lay in the disc
738 * are listed, if set to 1, all pixels overlapping the disc
739 * are listed. In the inclusive mode the radius is increased by half the pixel size.
740 * In this case most probably all neighbor pixels will be listed even with very small
741 * radius.
742 * In case of exclusive search and very small radius when the disc lays completely
743 * inside a pixel the pixel number is returned using vector2pix method.
744 * @return ArrayList of pixel numbers
745 *
746 * calls: RingNum(nside, ir) InRing(nside, iz, phi0, dphi,nest) vector2pix(nside,ipix)
747 */
748 public ArrayList query_disc(long nside, Vector3d vector, double radius,
749 int nest, int inclusive) {
750 ArrayList res = new ArrayList();
751 long irmin, irmax, iz, ip, nir, npix, ilist;
752
753 // double norm_vect0;
754 double x0, y0, z0, radius_eff;
755 double a, b, c, cosang;
756 double dth1, dth2;
757 double phi0, cosphi0, cosdphi, dphi;
758 double rlat0, rlat1, rlat2, zmin, zmax, z;
759 // long status, list_size, nlost;
760 boolean do_inclusive = false;
761 boolean do_nest = false;
762 String SID = "QUERY_DISC";
763 /* */
764 long nsidesq = nside * nside;
765 npix = 12 * nsidesq;
766 double pixres = PixRes(nside); // in arc seconds
767 // double halfPix = 0.5*Math.toRadians(pixres/3600.); // in radians
768 double halfPix = PI / (4.0 * nside);
769 // System.out.println("0.5 pixel size ="+halfPix);
770
771
772 if (radius < 0.0 || radius > PI) {
773 throw new IllegalArgumentException(SID
774 + ": angular radius is in RADIAN and should be in [0,pi]");
775 }
776
777 if (inclusive == 1)
778 do_inclusive = true;
779 if (nest == 1)
780 do_nest = true;
781
782 dth1 = 1.0 / (3.0 * nside * nside);
783 dth2 = 2.0 / (3.0 * nside);
784
785 radius_eff = radius;
786 // if (radius_eff <= halfPix) radius_eff = halfPix;
787
788 /* disc center */
789 vector.normalize();
790 x0 = vector.x; // norm_vect0;
791 y0 = vector.y; // norm_vect0;
792 z0 = vector.z; // norm_vect0;
793 // System.out.println("x0="+x0+" y0="+y0+" z0="+z0);
794 //
795 // NK make radius increase a function of altitude
796 //
797 if (do_inclusive) { radius_eff += (halfPix + Math.abs(z0)*halfPix);} // increase radius for incluzive search
798 // System.out.println("effective radius="+radius_eff);
799 // if (do_inclusive) { radius_eff += halfPix;}
800 cosang = Math.cos(radius_eff);
801 phi0 = 0.0;
802 dphi = 0.0;
803 if (x0 != 0. || y0 != 0.)
804 phi0 = BitManipulation.MODULO(Math.atan2(y0, x0) + TWOPI, TWOPI); // in [0, 2pi]
805 cosphi0 = Math.cos(phi0);
806 // System.out.println("phi0="+phi0+" cosphi0="+cosphi0);
807 a = x0 * x0 + y0 * y0;
808 /* coordinate z of highest and lowest points in the disc */
809 rlat0 = Math.asin(z0); // latitude in RAD of the center
810 rlat1 = rlat0 + radius_eff;
811 rlat2 = rlat0 - radius_eff;
812 // System.out.println("rlat0="+rlat0+" rlat1="+rlat1+" rlat2="+rlat2);
813 //
814 if (rlat1 >= HALFPI) {
815 zmax = 1.0;
816 } else {
817 zmax = Math.sin(rlat1);
818 }
819 irmin = RingNum(nside, zmax);
820 irmin = Math.max(1, irmin - 1); // start from a higher point to be safe
821 if (rlat2 <= -HALFPI) {
822 zmin = -1.0;
823 } else {
824 zmin = Math.sin(rlat2);
825 }
826 irmax = RingNum(nside, zmin);
827 irmax = Math.min(4 * nside - 1, irmax + 1); // go down to a lower point
828 // System.out.println(" irmax="+irmax+" irmin="+irmin);
829 ilist = -1;
830
831
832
833 //
834 /* loop on ring number */
835 for (iz = irmin; iz <= irmax; iz++) {
836 if (iz <= nside - 1) { // north polar cap
837 z = 1.0 - iz * iz * dth1;
838 } else if (iz <= 3 * nside) { // tropical band + equator
839 z = (2.0 * nside - iz) * dth2;
840 } else {
841 z = -1.0 + (4.0 * nside - iz) * (4.0 * nside - iz) * dth1;
842 }
843 /* find phi range in the disc for each z */
844 b = cosang - z * z0;
845 c = 1.0 - z * z;
846 cosdphi = b / Math.sqrt(a * c);
847 long done = 0;
848
849 if (Math.abs(x0) <= 1.0e-12 && Math.abs(y0) <= 1.0e-12) {
850 cosdphi = -1.0;
851 dphi = PI;
852 done = 1;
853 }
854 if (done == 0) {
855 if (Math.abs(cosdphi) <= 1.0) {
856 dphi = Math.acos(cosdphi); // in [0,pi]
857 } else {
858 if (cosphi0 >= cosdphi) {
859 dphi = PI; // all the pixels at this elevation are in
860 // the disc
861 } else {
862 done = 2; // out of the disc
863 }
864 }
865
866 }
867 if (done < 2) { // pixels in disc
868 /* find pixels in the disc */
869 // System.out.println("iz="+iz+" phi="+phi0+" dphi="+dphi);
870 ArrayList listir = InRing(nside, iz, phi0, dphi, do_nest);
871 // System.out.println("ir"+iz);
872
873 res.addAll(listir);
874 }
875
876 }
877 //
878 // if radius less than pixel size check that the pixel number is in the list
879 // and add one if it is missing.
880 //
881
882 long pixel = 0;
883 if ( pixres > Math.toDegrees(radius)/3600. ) {
884 // System.out.println("case with r < pix. size");
885 if (do_nest) {
886 pixel = vect2pix_nest(nside,vector);
887 } else {
888 pixel = vect2pix_ring(nside,vector);
889 }
890 if (!res.contains(new Long(pixel)))
891 res.add(new Long(pixel));
892 }
893 return res;
894 }
895
896 /**
897 * generates in the RING or NESTED scheme all pixels that lays at an
898 * angular distance Radius of the center.
899 *
900 * @param nside
901 * long map resolution
902 * @param vector
903 * Vector3d pointing to the ring center
904 * @param radius
905 * double angular radius of the ring (in RADIAN )
906 * @param nest
907 * int 0 (default) if output is in RING scheme, if set to 1
908 * output is in NESTED
909 * @param inclusive
910 * int 0 (default) only pixels whose centers are crossed by the ring
911 * are listed, if set to 1, all pixels crossed or touched by the ring
912 * are listed. In the inclusive mode the radius is increased by half the pixel size.
913 * In this case most probably all neighbor pixels will be listed even with very small
914 * radius.
915 * In case of exclusive search and very small radius when the ring lays completely
916 * inside a pixel the pixel number is returned using vector2pix method.
917 * @return ArrayList of pixel numbers
918 *
919 * calls: RingNum(nside, ir) InRing(nside, iz, phi0, dphi,nest) vector2pix(nside,ipix)
920 */
921 public ArrayList query_ring(long nside, Vector3d vector, double radius,
922 int nest, int inclusive) {
923 ArrayList res = new ArrayList();
924 long irmin, irmax, iz, ip, nir, npix, ilist;
925
926 // double norm_vect0;
927 double x0, y0, z0, radius_eff;
928 double a, b, c, cosang;
929 double dth1, dth2;
930 double phi0, cosphi0, cosdphi, dphi;
931 double rlat0, rlat1, rlat2, zmin, zmax, z;
932 // long status, list_size, nlost;
933 boolean do_inclusive = false;
934 boolean do_nest = false;
935 String SID = "QUERY_RING";
936 /* */
937 long nsidesq = nside * nside;
938 npix = 12 * nsidesq;
939 double pixres = PixRes(nside); // in arc seconds
940 //
941 double halfPix = 0.5*Math.sqrt(PI / (3.0*npix)); // in radians
942 double dr = 2.0*halfPix; // interval in phi
943 // System.out.println("0.5 pixel size ="+halfPix);
944
945
946 if (radius < 0.0 || radius > PI) {
947 throw new IllegalArgumentException(SID
948 + ": angular radius is in RADIAN and should be in [0,pi]");
949 }
950
951 if (inclusive == 1)
952 do_inclusive = true;
953 if (nest == 1)
954 do_nest = true;
955
956 dth1 = 1.0 / (3.0 * nside * nside);
957 dth2 = 2.0 / (3.0 * nside);
958
959 radius_eff = radius;
960 // if (radius_eff <= halfPix) radius_eff = halfPix;
961
962 /* disc center */
963 vector.normalize();
964 x0 = vector.x; // norm_vect0;
965 y0 = vector.y; // norm_vect0;
966 z0 = vector.z; // norm_vect0;
967 // System.out.println("x0="+x0+" y0="+y0+" z0="+z0);
968 //
969 // NK make radius increase a function of altitude
970 //
971 if (do_inclusive) { radius_eff += (halfPix + Math.abs(z0)*halfPix);} // increase radius for incluzive search
972 // System.out.println("effective radius="+radius_eff);
973 // if (do_inclusive) { radius_eff += halfPix;}
974 cosang = Math.cos(radius_eff);
975 phi0 = 0.0;
976 dphi = 0.0;
977 if (x0 != 0. || y0 != 0.)
978 phi0 = BitManipulation.MODULO(Math.atan2(y0, x0) + TWOPI, TWOPI); // in [0, 2pi]
979 cosphi0 = Math.cos(phi0);
980 // System.out.println("phi0="+phi0+" cosphi0="+cosphi0);
981 a = x0 * x0 + y0 * y0;
982 /* coordinate z of highest and lowest points in the disc */
983 rlat0 = Math.asin(z0); // latitude in RAD of the center
984 rlat1 = rlat0 + radius_eff;
985 rlat2 = rlat0 - radius_eff;
986 // System.out.println("rlat0="+rlat0+" rlat1="+rlat1+" rlat2="+rlat2);
987 //
988 if (rlat1 >= HALFPI) {
989 zmax = 1.0;
990 } else {
991 zmax = Math.sin(rlat1);
992 }
993 irmin = RingNum(nside, zmax);
994 irmin = Math.max(1, irmin - 1); // start from a higher point to be safe
995 if (rlat2 <= -HALFPI) {
996 zmin = -1.0;
997 } else {
998 zmin = Math.sin(rlat2);
999 }
1000 irmax = RingNum(nside, zmin);
1001 irmax = Math.min(4 * nside - 1, irmax + 1); // go down to a lower point
1002 System.out.println(" irmax="+irmax+" irmin="+irmin);
1003 ilist = -1;
1004
1005
1006
1007 //
1008 /* loop on ring number */
1009 for (iz = irmin; iz <= irmax; iz++) {
1010 if (iz <= nside - 1) { // north polar cap
1011 z = 1.0 - iz * iz * dth1;
1012 } else if (iz <= 3 * nside) { // tropical band + equator
1013 z = (2.0 * nside - iz) * dth2;
1014 } else {
1015 z = -1.0 + (4.0 * nside - iz) * (4.0 * nside - iz) * dth1;
1016 }
1017 /* find phi range in the disc for each z */
1018 b = cosang - z * z0;
1019 c = 1.0 - z * z;
1020 cosdphi = b / Math.sqrt(a * c);
1021 int done = 0;
1022
1023 if (Math.abs(x0) <= 1.0e-12 && Math.abs(y0) <= 1.0e-12) {
1024 cosdphi = -1.0;
1025 dphi = PI;
1026 done = 1;
1027 }
1028
1029 if (done == 0) {
1030 if (Math.abs(cosdphi) <= 1.0) {
1031 dphi = Math.acos(cosdphi); // in [0,pi]
1032 } else {
1033 if (cosphi0 >= cosdphi) {
1034 dphi = PI; //
1035 } else {
1036 done = 2; // out of the ring
1037 }
1038 }
1039
1040 }
1041 System.out.println(" done="+done);
1042 if (done < 2) { // pixels in ring
1043 /* find pixels in the ring */
1044 System.out.println("iz="+iz+" phi="+phi0+" dphi="+dphi);
1045 double phi01 = phi0 - 0.5*dphi;
1046 double phi02 = phi0 + 0.5*dphi;
1047 ArrayList listir = null;
1048
1049 // System.out.println("iz="+iz +" phi01="+phi01+" phi02="+phi02+" dr="+dr);
1050 listir = InRing(nside, iz, phi0, dphi, do_nest);
1051 if (listir.size()<=1) {
1052 res.addAll(listir);
1053 } else {
1054 res.add(listir.get(0));
1055 res.add(listir.get(listir.size()-1));
1056 }
1057 // System.out.println("ir"+iz);
1058 // listir = InRing(nside, iz, phi02, dr, do_nest);
1059 // res.addAll(listir);
1060
1061 }
1062
1063 }
1064 //
1065
1066 return res;
1067 }
1068
1069 /**
1070 * renders theta and phi coordinates of the nominal pixel center for the
1071 * pixel number ipix (RING scheme) given the map resolution parameter nside
1072 *
1073 * @param nside
1074 * long map resolution
1075 * @param ipix
1076 * long pixel number
1077 * @return double[] theta,phi
1078 */
1079 public double[] pix2ang_ring(long nside, long ipix) {
1080 double[] res = { 0., 0. };
1081 long nl2, nl4, npix, ncap, iring, iphi, ip, ipix1;
1082 double fodd, hip, fihip, theta, phi;
1083 String SID = "pix2ang_ring:";
1084 /* */
1085 if (nside < 1 || nside > ns_max) {
1086 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
1087 }
1088 long nsidesq = nside * nside;
1089 npix = 12 * nsidesq; // total number of pixels
1090 if (ipix < 0 || ipix > npix - 1) {
1091 throw new IllegalArgumentException(SID + " ipix out of range calculated from nside");
1092 }
1093 ipix1 = ipix + 1; // in [1, npix]
1094 nl2 = 2 * nside;
1095 nl4 = 4 * nside;
1096 ncap = 2 * nside * (nside - 1); // points in each polar cap, =0 for
1097 // nside =1
1098
1099 if (ipix1 <= ncap) { // North polar cap
1100 hip = ipix1 / 2.0;
1101 fihip = (long) hip; // get integer part of hip
1102 iring = (long) (Math.sqrt(hip - Math.sqrt(fihip))) + 1; // counted from north
1103 // pole
1104 iphi = ipix1 - 2 * iring * (iring - 1);
1105 theta = Math.acos(1.0 - iring * iring / (3.0 * nsidesq));
1106 phi = ((double)iphi - 0.5) * PI / (2.0 * iring);
1107
1108
1109 } else if (ipix1 <= nl2 * (5 * nside + 1)) { // equatorial region
1110 ip = ipix1 - ncap - 1;
1111 iring = (long) (ip / nl4) + nside; // counted from North pole
1112 iphi = (long) BitManipulation.MODULO(ip, nl4) + 1;
1113 fodd = 0.5 * (1. + BitManipulation.MODULO(iring + nside, 2)); // 1 if iring+nside
1114 // is odd, 1/2 otherwise
1115 theta = Math.acos((nl2 - iring) / (1.5 * nside));
1116 phi = ((double)iphi - fodd) * PI / (2.0 * nside);
1117
1118 } else { // South pole cap
1119 ip = npix - ipix1 + 1;
1120 hip = ip / 2.0;
1121 fihip = (long) hip;
1122 iring = (long) (Math.sqrt(hip - Math.sqrt(fihip))) + 1; // counted from South
1123 // pole
1124 iphi = 4 * iring + 1 - (ip - 2 * iring * (iring - 1));
1125 theta = Math.acos(-1.0 + iring * iring / (3.0 * nsidesq));
1126 phi = ((double)iphi - 0.5) * PI / (2.0 * iring);
1127
1128 }
1129 res[0] = theta;
1130 res[1] = phi;
1131 return res;
1132 }
1133
1134 /**
1135 * returns the vector pointing in the center of the pixel ipix. The vector
1136 * is calculated by makePix2Vect_ring method
1137 *
1138 * @param nside map resolution
1139 * @param ipix pixel number
1140 * @return Vector3d
1141 */
1142 public Vector3d pix2vect_ring(long nside, long ipix) {
1143
1144 PixInfo pixInfo = makePix2Vect_ring(nside, ipix);
1145 Vector3d res = new Vector3d(pixInfo.pixVect);
1146 return res;
1147 }
1148
1149 /**
1150 * returns double [][] with coordinates of the pixel corners. The array is
1151 * calculated by makePix2Vect_ring method
1152 *
1153 * @param nside map resolution
1154 * @param ipix pixel number
1155 * @return double[][] list of vertex coordinates
1156 */
1157 public double[][] pix2vertex_ring(long nside, long ipix) {
1158 double[][] res;
1159 PixInfo pixinfo = makePix2Vect_ring(nside, ipix);
1160 res = pixinfo.pixVertex;
1161 return res;
1162 }
1163
1164 /**
1165 * renders vector (x,y,z) coordinates of the nominal pixel center for pixel
1166 * ipix (RING scheme) given the map resolution parameter nside. It also
1167 * calculates (x,y,z) positions of the four vertices in order N,W,S,E.
1168 * These results are returned in a PixInfo object.
1169 * Those can be used using pix2Vect_ring and pix2vert_ring methods
1170 *
1171 * @param nside
1172 * long map resolution
1173 * @param ipix
1174 * pixel number
1175 * @return result object
1176 */
1177 private PixInfo makePix2Vect_ring(long nside, long ipix) {
1178 long nl2;
1179 Vector3d pixVect = new Vector3d(0., 0., 0.);
1180 double[][] pixVertex = new double[3][4];
1181
1182 long nl4;
1183 long iring, iphi, ip, ipix1;
1184 long npix,ncap;
1185 double phi_nv, phi_wv, phi_sv, phi_ev;
1186 double z_nv, z_sv, sth_nv, sth_sv, hdelta_phi;
1187 double fact1, fact2, fodd, hip, fihip, z, sth, phi;
1188 long iphi_mod;
1189 long iphi_rat;
1190 // boolean do_vertex = true;
1191 long nsidesq = nside * nside;
1192 String SID = " Pix2Vect_ring:";
1193 /* */
1194 if (nside < 1 || nside > ns_max) {
1195 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
1196 }
1197
1198 npix = 12 * nsidesq;
1199 if (ipix < 0 || ipix > npix - 1) {
1200 throw new IllegalArgumentException(SID + " ipix out of range calculated from nside");
1201 }
1202
1203 ipix1 = ipix + 1; // in [1, npix]
1204 nl2 = 2 * nside;
1205 nl4 = 4 * nside;
1206 ncap = 2 * nside * (nside - 1); // points in each polar cap
1207 fact1 = 1.5 * nside;
1208 fact2 = 3.0 * nsidesq;
1209 phi_nv = 0.0;
1210 phi_sv = 0.0;
1211 if (ipix1 <= ncap) { // north polar cap
1212 hip = ipix1 / 2.0;
1213 fihip = (long) hip;
1214 iring = (long) (Math.sqrt(hip - Math.sqrt(fihip))) + 1; // counted from north
1215 // pole
1216 iphi = ipix1 - 2 * iring * (iring - 1);
1217 z = 1.0 - iring * iring / fact2;
1218 phi = (iphi - 0.5) * PI / (2.0 * iring);
1219
1220 hdelta_phi = PI / (4.0 * iring); // half pixel width
1221 z_nv = 1.0 - (iring - 1) * (iring - 1) / fact2;
1222 z_sv = 1.0 - (iring + 1) * (iring + 1) / fact2;
1223 iphi_mod = (long) BitManipulation.MODULO(iphi - 1, iring); // in [0,1,...,iring-1]
1224 iphi_rat = (iphi - 1) / iring; // in [0,1,2,3]
1225 if (iring > 1)
1226 phi_nv = HALFPI * (iphi_rat + iphi_mod / (iring - 1.0));
1227 phi_sv = HALFPI * (iphi_rat + (iphi_mod + 1.0) / (iring + 1.0));
1228 } else if (ipix1 <= nl2 * (5 * nside + 1)) { // equatorial region
1229 ip = (ipix1 - ncap - 1);
1230 iring = (long) (ip / nl4) + nside; // counted from North pole
1231 iphi = (long) BitManipulation.MODULO(ip, nl4) + 1;
1232 fodd = 0.5 * (1. + BitManipulation.MODULO(iring + nside, 2)); // 1 if iring+nside
1233 // is odd or 1/2
1234 z = (nl2 - iring) / fact1;
1235 phi = (iphi - fodd) * PI / (2.0 * nside);
1236 hdelta_phi = PI / (4.0 * nside); // half pixel width
1237 phi_nv = phi;
1238 phi_sv = phi;
1239 z_nv = (nl2 - iring + 1) / fact1;
1240 z_sv = (nl2 - iring - 1) / fact1;
1241 if (iring == nside) { // nothern transition
1242 z_nv = 1.0 - (nside - 1) * (nside - 1) / fact2;
1243 iphi_mod = (long) BitManipulation.MODULO(iphi - 1, nside); // in [0,1,...,nside-1]
1244 iphi_rat = (iphi - 1) / nside; // in [0,1,2,3]
1245 if (nside > 1)
1246 phi_nv = HALFPI * (iphi_rat + iphi_mod / (nside - 1.));
1247 } else if (iring == 3 * nside) { // southern transition
1248 z_sv = -1.0 + (nside - 1) * (nside - 1) / fact2;
1249 iphi_mod = (long) BitManipulation.MODULO(iphi - 1, nside); // in [0,1,... iring-1]
1250 iphi_rat = (iphi - 1) / nside; // in [0,1,2,3]
1251 if (nside > 1)
1252 phi_sv = HALFPI * (iphi_rat + iphi_mod / (nside - 1.0));
1253 }
1254
1255 } else { // South polar cap
1256 ip = npix - ipix1 + 1;
1257 hip = ip / 2.0;
1258 fihip = (long) hip;
1259 iring = (long) (Math.sqrt(hip - Math.sqrt(fihip))) + 1; // counted from South
1260 // pole
1261 iphi = 4 * iring + 1 - (ip - 2 * iring * (iring - 1));
1262 z = -1.0 + iring * iring / fact2;
1263 phi = (iphi - 0.5) * PI / (2.0 * iring);
1264 hdelta_phi = PI / (4.0 * iring); // half pixel width
1265 z_nv = -1.0 + (iring + 1) * (iring + 1) / fact2;
1266 z_sv = -1.0 + (iring - 1) * (iring - 1) / fact2;
1267 iphi_mod = (long) BitManipulation.MODULO(iphi - 1, iring); // in [0,1,...,iring-1]
1268 iphi_rat = (iphi - 1) / iring; // in [0,1,2,3]
1269 phi_nv = HALFPI * (iphi_rat + (iphi_mod + 1) / (iring + 1.0));
1270 if (iring > 1)
1271 phi_sv = HALFPI * (iphi_rat + iphi_mod / (iring - 1.0));
1272
1273 }
1274 /* pixel center */
1275 sth = Math.sqrt((1.0 - z) * (1.0 + z));
1276 pixVect.x = sth * Math.cos(phi);
1277 pixVect.y = sth * Math.sin(phi);
1278 pixVect.z = z;
1279 pixVect = new Vector3d(sth * Math.cos(phi), sth * Math.sin(phi), z);
1280 /* west vertex */
1281 phi_wv = phi - hdelta_phi;
1282 pixVertex[0][1] = sth * Math.cos(phi_wv);
1283 pixVertex[1][1] = sth * Math.sin(phi_wv);
1284 pixVertex[2][1] = z;
1285 /* east vertex */
1286 phi_ev = phi + hdelta_phi;
1287 pixVertex[0][3] = sth * Math.cos(phi_ev);
1288 pixVertex[1][3] = sth * Math.sin(phi_ev);
1289 pixVertex[2][3] = z;
1290 /* north vertex */
1291 sth_nv = Math.sqrt((1.0 - z_nv) * (1.0 + z_nv));
1292 pixVertex[0][0] = sth_nv * Math.cos(phi_nv);
1293 pixVertex[1][0] = sth_nv * Math.sin(phi_nv);
1294 pixVertex[2][0] = z_nv;
1295 /* south vertex */
1296 sth_sv = Math.sqrt((1.0 - z_sv) * (1.0 + z_sv));
1297 pixVertex[0][2] = sth_sv * Math.cos(phi_sv);
1298 pixVertex[1][2] = sth_sv * Math.sin(phi_sv);
1299 pixVertex[2][2] = z_sv;
1300 return new PixInfo(pixVect, pixVertex);
1301 }
1302
1303 /**
1304 * renders the pixel number ipix (RING scheme) for a pixel which contains a
1305 * point with coordinates theta and phi, given the map resolution parameter
1306 * nside.
1307 *
1308 * @param nside
1309 * long map resolution parameter
1310 * @param theta
1311 * double theta
1312 * @param phi -
1313 * double phi
1314 * @return long ipix
1315 */
1316 public long ang2pix_ring(long nside, double theta, double phi) {
1317 long nl4;
1318 long jp, jm, kshift;
1319 long ip;
1320 long ir;
1321 double z, za, tt, tp, tmp;
1322 long pix = 0;
1323 long ipix1;
1324 long nl2, ncap, npix;
1325 String SID = "ang2pix_ring:";
1326 /* */
1327 if (nside < 1 || nside > ns_max) {
1328 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
1329 }
1330 if (theta < 0.0 || theta > PI) {
1331 throw new IllegalArgumentException(SID + " Theta out of range [0,pi]");
1332 }
1333
1334 z = Math.cos(theta);
1335 za = Math.abs(z);
1336
1337
1338
1339 if (phi >= TWOPI) phi = phi -TWOPI ;
1340
1341 if (phi < 0.)
1342 phi =phi + TWOPI; // phi in [0, 2pi]
1343 tt = phi / HALFPI; // in [0,4]
1344 // tt = BitManipulation.MODULO(phi, TWOPI) / HALFPI; // in [0,4]
1345 nl2 = 2 * nside;
1346 nl4 = 4 * nside;
1347 ncap = nl2 * (nside - 1); // number of pixels in the north polar cap
1348 npix = 12 * nside * nside;
1349 if (za < twothird) { // equatorial region
1350 jp = (long) (nside * (0.5 + tt - 0.75 * z)); // index of ascending
1351 // edge line
1352 jm = (long) (nside * (0.5 + tt + 0.75 * z)); // index of descending
1353 // edge line
1354
1355 ir = nside + 1 + jp - jm; // in [1,2n+1]
1356 kshift = 0;
1357 if ((long) BitManipulation.MODULO(ir, 2) == 0)
1358 kshift = 1; // 1 if ir even, 0 otherwise
1359 ip = (long) ((jp + jm - nside + kshift + 1) / 2) + 1; // in [1,4n]
1360 if (ip > nl4) ip = ip - nl4;
1361 ipix1 = ncap + nl4 * (ir - 1) + ip;
1362
1363 } else { // North and South polar caps
1364 tp = tt - (long) tt;
1365 tmp = Math.sqrt(3.0 * (1.0 - za));
1366 jp = (long) (nside * tp * tmp); // increasing edge line index
1367 jm = (long) (nside * (1.0 - tp) * tmp); // decreasing edge index
1368
1369 ir = jp + jm + 1; // ring number counted from closest pole
1370 ip = (long) (tt * ir) + 1; // in [1,4*ir]
1371 if (ip > 4 * ir)
1372 ip = ip - 4 * ir;
1373
1374 ipix1 = 2 * ir * (ir - 1) + ip;
1375 if (z <= 0.0)
1376 ipix1 = npix - 2 * ir * (ir + 1) + ip;
1377
1378 }
1379 pix = ipix1 - 1; // in [0, npix-1]
1380
1381
1382 return pix;
1383 }
1384
1385 /**
1386 * renders the pixel number ipix (RING scheme) for a pixel which contains a
1387 * point on a sphere at coordinate vector (x,y,z), given the map resolution
1388 * parameter nside
1389 *
1390 * @param nside
1391 * long map resolution
1392 * @param vector
1393 * Vector3d of the point coordinates
1394 * @return long pixel number
1395 * @throws IllegalArgumentException
1396 */
1397 public long vect2pix_ring(long nside, Vector3d vector) {
1398 long res = 0;
1399 long nl2, nl4, ncap, npix, jp, jm, ipix1;
1400 double z, za, tt, tp, tmp, dnorm, phi;
1401 long ir, ip, kshift;
1402 String SID = " vect2pix_ring:";
1403 /* */
1404 if (nside < 1 || nside > ns_max) {
1405 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
1406 }
1407 dnorm = vector.length();
1408 z = vector.z / dnorm;
1409 phi = 0.;
1410 if (vector.x != 0. || vector.y != 0.)
1411 phi = Math.atan2(vector.y, vector.x); // phi in [-pi,pi]
1412 za = Math.abs(z);
1413 if (phi < 0.)
1414 phi += TWOPI; // phi in [0, 2pi]
1415 tt = phi / HALFPI; // in [0,4]
1416
1417 nl2 = 2 * nside;
1418 nl4 = 4 * nside;
1419 ncap = nl2 * (nside - 1); // number of pixels in the north polar cap
1420 npix = 12 * nside * nside;
1421 if (za < twothird) { // equatorial region
1422 jp = (long) (nside * (0.5 + tt - 0.75 * z)); // index of ascending
1423 // edge line
1424 jm = (long) (nside * (0.5 + tt + 0.75 * z)); // index of descending
1425 // edge line
1426
1427 ir = nside + 1 + jp - jm; // in [1,2n+1]
1428 kshift = 0;
1429 if ((long) BitManipulation.MODULO(ir, 2) == 0)
1430 kshift = 1; // 1 if ir even, 0 otherwise
1431 ip = (long) ((jp + jm - nside + kshift + 1) / 2) + 1; // in [1,4n]
1432 if (ip > nl4) ip = ip - nl4;
1433 ipix1 = ncap + nl4 * (ir - 1) + ip;
1434 } else { // North and South polar caps
1435 tp = tt - (long) tt;
1436 tmp = Math.sqrt(3.0 * (1.0 - za));
1437 jp = (long) (nside * tp * tmp); // increasing edge line index
1438 jm = (long) (nside * (1.0 - tp) * tmp); // decreasing edge index
1439
1440 ir = jp + jm + 1; // ring number counted from closest pole
1441 ip = (long) (tt * ir) + 1; // in [1,4*ir]
1442 if (ip > 4 * ir)
1443 ip = ip - 4 * ir;
1444
1445 ipix1 = 2 * ir * (ir - 1) + ip;
1446 if (z <= 0.0)
1447 ipix1 = npix - 2 * ir * (ir + 1) + ip;
1448 }
1449 res = ipix1 - 1; // in [0, npix-1]
1450 return res;
1451 }
1452
1453 /**
1454 *
1455 * Renders theta and phi coordinates of the normal pixel center for the
1456 * pixel number ipix (NESTED scheme) given the map resolution parameter
1457 * nside.
1458 *
1459 * @param nside
1460 * map resolution parameter - long
1461 * @param ipix
1462 * long pixel number
1463 * @return double[] (theta, phi)
1464 * @throws IllegalArgumentException
1465 */
1466 public double[] pix2ang_nest(long nside, long ipix) {
1467 double[] res = new double[2];
1468 double theta = 0.;
1469 double phi = 0.;
1470 long npix, npface, ipf, ip_low, ip_trunc, ip_med, ip_hi;
1471 long jrt, jr, nr, jpt, jp, kshift, nl4, ix, iy, face_num;
1472 double z, fn, fact1, fact2;
1473 String SID = "pix2ang_nest:";
1474 // coordinate of the lowest corner of each face
1475 long[] jrll = { 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4 }; // in units of
1476 // nside
1477 long[] jpll = { 0, 1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7 }; // in units of
1478 // nside/2
1479 /* */
1480 if (nside < 1 || nside > ns_max) {
1481 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
1482 }
1483 long nsidesq = nside * nside;
1484 npix = 12 * nsidesq;
1485 if (ipix < 0 || ipix > npix - 1) {
1486 throw new IllegalArgumentException(SID + " ipix out of range calculated from nside");
1487 }
1488 if (pix2x[xmax-1] <= 0)
1489 mk_pix2xy();
1490 fn = 1.*nside;
1491 fact1 = 1.0 / (3.0 * fn * fn);
1492 fact2 = 2.0 / (3.0 * fn);
1493 nl4 = 4 * nside;
1494 /* findes the face, and the number in the face */
1495 npface = nside * nside;
1496 face_num = ipix / npface; // face number [0,11]
1497 ipf = (long) BitManipulation.MODULO(ipix, npface); // pixel in the face [0, npface-1]
1498 /*
1499 * finds x,y on the face (starting from the lowest corner) from pixel
1500 * number
1501 */
1502 ip_low = (long) BitManipulation.MODULO(ipf, pixmax); // content of the last 18 bits
1503 ip_trunc = ipf / pixmax; // trancation of the last 18 bits
1504 ip_med = (long) BitManipulation.MODULO(ip_trunc, pixmax); // content of the next 18 bits
1505 ip_hi = ip_trunc / pixmax; // content of the high wait 18 bits
1506
1507 ix = pixmax * pix2x[(int)ip_hi] + xmid * pix2x[(int)ip_med] + pix2x[(int) ip_low];
1508 iy = pixmax * pix2y[(int)ip_hi] + xmid * pix2y[(int)ip_med] + pix2y[(int)ip_low];
1509 /* transform these in (horizontal, vertical) coordinates */
1510 jrt = ix + iy; // [0,2*(nside-1)]
1511 jpt = ix - iy; // [ -nside+1, nside -1]
1512 /* computes the z coordinate on the sphere */
1513 jr = jrll[(int) (face_num + 1)] * nside - jrt - 1; // ring number in [1,
1514 // 4*nside-1]
1515
1516 nr = nside; // equatorial region (the most frequent )
1517 z = (2 * nside - jr) * fact2;
1518 kshift = (long) BitManipulation.MODULO(jr - nside, 2);
1519 if (jr < nside) { // north pole region
1520 nr = jr;
1521 z = 1.0 - nr * nr * fact1;
1522 kshift = 0;
1523 } else if (jr > 3 * nside) { // south pole region
1524 nr = nl4 - jr;
1525 z = -1.0 + nr * nr * fact1;
1526 kshift = 0;
1527 }
1528 theta = Math.acos(z);
1529 /* computes phi coordinate on the sphere, in [0,2pi] */
1530 jp = (jpll[(int) (face_num + 1)] * nr + jpt + 1 + kshift) / 2;
1531 if (jp > nl4)
1532 jp = jp - nl4;
1533 if (jp < 1)
1534 jp = jp + nl4;
1535
1536 phi = (jp - (kshift + 1) * 0.5) * (HALFPI / nr);
1537 res[0] = theta;
1538 res[1] = phi;
1539 return res;
1540 }
1541
1542 /**
1543 * renders vector (x,y,z) coordinates of the nominal pixel center for the
1544 * pixel ipix (NESTED scheme ) given the map resolution parameter nside.
1545 * Also calculates the (x,y,z) positions of 4 pixel vertices (corners) in
1546 * the order N,W,S,E. These can be get using method pix2vertex_nest.
1547 *
1548 * @param nside the map resolution
1549 * @param ipix long pixel number
1550 * @return Vector3d
1551 * @throws IllegalArgumentException
1552 */
1553 public Vector3d pix2vect_nest(long nside, long ipix) {
1554
1555 PixInfo pixinfo = makePix2Vect_Nest(nside, ipix);
1556 return pixinfo.pixVect;
1557 }
1558
1559 /**
1560 * renders vector (x,y,z) coordinates of the nominal pixel center for the
1561 * pixel ipix (NESTED scheme ) given the map resolution parameter nside.
1562 * Also calculates the (x,y,z) positions of 4 pixel vertices (corners) in
1563 * the order N,W,S,E.
1564 *
1565 * @param nside the map resolution
1566 * @param ipix long pixel number
1567 * @return double[3][4] 4 sets of vector components
1568 * @throws IllegalArgumentException
1569 */
1570 public double[][] pix2vertex_nest(long nside, long ipix) {
1571 double[][] res = new double[3][4];
1572 PixInfo pixinfo = makePix2Vect_Nest(nside, ipix);
1573 double[][] pixVertex = pixinfo.pixVertex;
1574 for (int i = 0; i < 3; i++) {
1575 for (int j = 0; j < 4; j++) {
1576 res[i][j] = pixVertex[i][j];
1577 }
1578 }
1579 return res;
1580 }
1581
1582 /**
1583 * renders vector (x,y,z) coordinates of the nominal pixel center for the
1584 * pixel ipix (NESTED scheme ) given the map resolution parameter nside. Tis
1585 * can be get using method pix2vect_nest Also calculates the (x,y,z)
1586 * positions of 4 pixel vertices (corners) in the order N,W,S,E.
1587 * The result can be used using method pix2vertex_nest.
1588 * @param nside long the map resolution
1589 * @param ipix long pixel number
1590 * @return result
1591 */
1592 private PixInfo makePix2Vect_Nest(long nside, long ipix) {
1593 Vector3d pixVect = new Vector3d(0., 0., 0.);
1594 double[][] pixVertex = new double[3][4];
1595 long npix, npface, ipf, ip_low, ip_trunc, ip_med, ip_hi;
1596 long jrt, jr, nr, jpt, jp, kshift, nl4;
1597 double z, fn, fact1, fact2, sth, phi;
1598 long ix, iy, face_num;
1599 int[] jrll = { 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4 };
1600 int[] jpll = { 0, 1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7 };
1601 double phi_nv, phi_wv, phi_sv, phi_ev, phi_up, phi_dn;
1602 double z_nv, z_sv, sth_nv, sth_sv;
1603 double hdelta_phi;
1604 long iphi_mod, iphi_rat;
1605 // boolean do_vertex = true;
1606 String SID = "Pix2Vect_Nest:";
1607 z_nv = 0.;
1608 z_sv = 0.;
1609 /* */
1610 if (nside < 1 || nside > ns_max) {
1611 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
1612 }
1613 long nsidesq = nside * nside;
1614 npix = 12 * nsidesq;
1615 if (ipix < 0 || ipix > npix - 1) {
1616 throw new IllegalArgumentException(SID + " ipix out of range calculated from nside");
1617 }
1618 /* initiates the array for the pixel number -> (x,y) mapping */
1619 if (pix2x[pixmax-1] <= 0)
1620 mk_pix2xy();
1621 fn = nside;
1622 fact1 = 1.0 / (3.0 * fn * fn);
1623 fact2 = 2.0 / (3.0 * fn);
1624 nl4 = 4 * nside;
1625
1626 /* finds the face and the number in the face */
1627 npface = nsidesq;
1628 // System.out.println("ipix="+ipix+" npface="+npface);
1629 face_num = ipix / npface; // face number in [0, 11]
1630 ipf = (long) BitManipulation.MODULO(ipix, npface); // pixel number in the face [0,npface-1]
1631 /*
1632 * finds the x,y on the face (starting from the lowest corner) from the
1633 * pixel number
1634 */
1635 ip_low = (long) BitManipulation.MODULO(ipf, pixmax); // last 18 bits
1636 ip_trunc = ipf / pixmax; // trancateion of the last 18 bits
1637 ip_med = (long) BitManipulation.MODULO(ip_trunc, pixmax); // next 18 bits
1638 ip_hi = ip_trunc / pixmax; // high 18 bits
1639
1640 ix = pixmax * pix2x[(int)ip_hi] + xmid * pix2x[(int)ip_med] + pix2x[(int)ip_low];
1641 iy = pixmax * pix2y[(int)ip_hi] + xmid * pix2y[(int)ip_med] + pix2y[(int)ip_low];
1642
1643 /* transform this in (vertical, horizontal) coordinates */
1644 jrt = ix + iy; // vertical in [0,2*(nside-1)]
1645 jpt = ix - iy; // horizontal in [ -nside+1, nside-1]
1646 /* computes the z coordinate on the sphere */
1647 jr = jrll[(int) (face_num + 1)] * nside - jrt - 1; // ring number in
1648 // [1,4*nside-1]
1649
1650 nr = nside; // equatorial region (the most frequent )
1651 z = (2.0 * nside - jr) * fact2;
1652
1653 kshift = (long) BitManipulation.MODULO(jr - nside, 2);
1654 z_nv = (2.0 * nside - jr + 1.0) * fact2;
1655 z_sv = (2.0 * nside - jr - 1.0) * fact2;
1656 if (jr == nside) { // northen transition
1657 z_nv = 1.0 - (nside - 1.0) * (nside - 1.0) * fact1;
1658 } else if (jr == 3 * nside) { // southern transition
1659 z_sv = -1.0 + (nside - 1.0) * (nside - 1.0) * fact1;
1660 }
1661
1662 if (jr < nside) { // north pole region
1663 nr = jr;
1664 z = 1.0 - nr * nr * fact1;
1665 kshift = 0;
1666
1667 z_nv = 1.0 - (nr - 1) * (nr - 1) * fact1;
1668 z_sv = 1.0 - (nr + 1) * (nr + 1) * fact1;
1669
1670 } else if (jr > 3 * nside) { // south pole region
1671 nr = nl4 - jr;
1672 z = -1.0 + nr * nr * fact1;
1673 kshift = 0;
1674
1675 z_nv = -1.0 + (nr + 1) * (nr + 1) * fact1;
1676 z_sv = -1.0 + (nr - 1) * (nr - 1) * fact1;
1677
1678 }
1679 /* computes the phi coordinate on the sphere, in [0,2pi] */
1680 jp = (jpll[(int) (face_num + 1)] * nr + jpt + 1 + kshift) / 2; // phi in the ring in
1681 // [1,4*nr]
1682 if (jp > nl4)
1683 jp = jp - nl4;
1684 if (jp < 1)
1685 jp = jp + nl4;
1686
1687 phi = (jp - (kshift + 1) / 2.) * (HALFPI / nr);
1688
1689 sth = Math.sqrt((1.0 - z) * (1.0 + z));
1690 pixVect.x = sth * Math.cos(phi);
1691 pixVect.y = sth * Math.sin(phi);
1692 pixVect.z = z;
1693
1694 phi_nv = phi;
1695 phi_sv = phi;
1696
1697 phi_up = 0.0;
1698 iphi_mod = (long) BitManipulation.MODULO(jp - 1, nr); // in [0,1,...,nr-1]
1699 iphi_rat = (jp - 1) / nr; // in [0,1,2,3]
1700 if (nr > 1)
1701 phi_up = HALFPI * (iphi_rat + iphi_mod / (nr - 1.));
1702 phi_dn = HALFPI * (iphi_rat + (iphi_mod + 1) / (nr + 1.));
1703
1704 if (jr < nside) { // north polar cap
1705 phi_nv = phi_up;
1706 phi_sv = phi_dn;
1707 } else if (jr > 3 * nside) { // south polar cap
1708 phi_nv = phi_dn;
1709 phi_sv = phi_up;
1710 } else if (jr == nside) { // north transition
1711 phi_nv = phi_up;
1712 } else if (jr == 3 * nside) { // south transition
1713 phi_sv = phi_up;
1714 }
1715 hdelta_phi = PI / (4.0 * nr);
1716 /* west vertex */
1717 // phi_wv = phi = hdelta_phi;
1718 phi_wv = phi - hdelta_phi;
1719 pixVertex[0][1] = sth * Math.cos(phi_wv);
1720 pixVertex[1][1] = sth * Math.sin(phi_wv);
1721 pixVertex[2][1] = z;
1722 /* east vertex */
1723 phi_ev = phi + hdelta_phi;
1724 pixVertex[0][3] = sth * Math.cos(phi_ev);
1725 pixVertex[1][3] = sth * Math.sin(phi_ev);
1726 pixVertex[2][3] = z;
1727 /* north vertex */
1728 sth_nv = Math.sqrt((1.0 - z_nv) * (1.0 + z_nv));
1729 pixVertex[0][0] = sth_nv * Math.cos(phi_nv);
1730 pixVertex[1][0] = sth_nv * Math.sin(phi_nv);
1731 pixVertex[2][0] = z_nv;
1732 /* south vertex */
1733 sth_sv = Math.sqrt((1.0 - z_sv) * (1.0 + z_sv));
1734 pixVertex[0][2] = sth_sv * Math.cos(phi_sv);
1735 pixVertex[1][2] = sth_sv * Math.sin(phi_sv);
1736 pixVertex[2][2] = z_sv;
1737 return new PixInfo(pixVect, pixVertex);
1738 }
1739
1740 /**
1741 * renders the pixel number pix (NESTED scheme) for a pixel which contains a
1742 * point on a sphere at coordinates theta and phi, given map resolution
1743 * parameter nside.
1744 *
1745 * The computation is made to the highest resolution available and then
1746 * degraded to required resolution by integer division. It makes sure that
1747 * the treatment of round-off will be consistent for every resolution.
1748 *
1749 * @param nside the map resolution parameter
1750 * @param theta double theta coordinate in radians
1751 * @param phi double phi coordinate in radians
1752 * @return pixel number long
1753 * @throws IllegalArgumentException
1754 */
1755 public long ang2pix_nest(long nside, double theta, double phi) {
1756 long pix = 0;
1757 // long ipix1;
1758 double z, za, tt, tp, tmp;
1759 long jp, jm, ifp, ifm, face_num, ix, iy, ix_low, ix_hi;
1760 long iy_low, iy_hi, ipf, ntt;
1761 // long nl2, nl4, ncap, npix, kshift, ir, ip;
1762 String SID = "ang2pix_nest:";
1763 /* */
1764 if (nside < 1 || nside > ns_max) {
1765 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
1766 }
1767 if (theta < 0.0 || theta > PI) {
1768 throw new IllegalArgumentException(SID + " theta is out of range [0.,PI]");
1769 }
1770 if (x2pix[xmax-1] <= 0)
1771 mk_xy2pix();
1772
1773 z = Math.cos(theta);
1774 za = Math.abs(z);
1775
1776
1777 if (phi < 0.)
1778 phi += TWOPI; // phi in [0,2pi]
1779 if (phi >= TWOPI ) phi -= TWOPI;
1780 tt = BitManipulation.MODULO(phi, TWOPI) / HALFPI; // in [0,4]
1781 // tt = 2. * phi / PI; // in [0,4]
1782 if (za <= twothird) { // Equatorial region
1783 /*
1784 * the index of edge lines increases when the longitude = phi goes
1785 * up
1786 */
1787 jp = (long) (ns_max * (0.5 + tt - z * 0.75)); // ascending edge line
1788 // index
1789 jm = (long) (ns_max * (0.5 + tt + z * 0.75)); // descending edge line
1790 // index
1791 /* find the face */
1792 ifp = jp / ns_max; // in [0,4]
1793 ifm = jm / ns_max;
1794 if (ifp == ifm) { // faces 4 to 7
1795 face_num = (long) BitManipulation.MODULO(ifp, 4) + 4;
1796 } else if (ifp < ifm) { // (half-) faces 0 to 3
1797 face_num = (long) BitManipulation.MODULO(ifp, 4);
1798 } else { // (half-) faces 8 to 11
1799 face_num = (long) BitManipulation.MODULO(ifm, 4) + 8;
1800 }
1801
1802 ix = (long) BitManipulation.MODULO(jm, ns_max);
1803 iy = ns_max - (long) BitManipulation.MODULO(jp, ns_max) - 1;
1804 } else { // polar region, za > 2/3
1805 ntt = (long) tt;
1806 if (ntt >= 4)
1807 ntt = 3;
1808 tp = tt - ntt;
1809 tmp = Math.sqrt(3.0 * (1.0 - za)); // in [0,1]
1810 /*
1811 * the index of edge lines increases when distance from the closest
1812 * pole goes up
1813 */
1814 jp = (long) (ns_max * tp * tmp); // line going toward the pole has
1815 // phi increases
1816 jm = (long) (ns_max * (1.0 - tp) * tmp); // that one goes away of the
1817 // closest pole
1818 jp = (long) Math.min(ns_max - 1, jp); // for pointss too close to the
1819 // boundary
1820 jm = (long) Math.min(ns_max - 1, jm);
1821 /* finds the face and pixel's (x,y) */
1822 if (z >= 0) {
1823 face_num = ntt; // in [0,3]
1824 ix = ns_max - jm - 1;
1825 iy = ns_max - jp - 1;
1826 } else {
1827 face_num = ntt + 8; // in [8,11]
1828 ix = jp;
1829 iy = jm;
1830 }
1831 }
1832 ix_low = (long) BitManipulation.MODULO(ix, xmax);
1833 ix_hi = ix / xmax;
1834 iy_low = (long) BitManipulation.MODULO(iy, xmax);
1835 iy_hi = iy / xmax;
1836
1837 ipf = (x2pix[(int) (ix_hi + 1)] + y2pix[(int) (iy_hi + 1)]) * (xmax * xmax)
1838 + (x2pix[(int) (ix_low + 1)] + y2pix[(int) (iy_low + 1)]);
1839 ipf = ipf / ((ns_max / nside) * (ns_max / nside)); // in [0, nside**2
1840 // -1]
1841 pix = ipf + face_num * nside * nside; // in [0, 12*nside**2 -1]
1842 return pix;
1843 }
1844
1845 /**
1846 * make the conversion NEST to RING
1847 *
1848 * @param nside the map resolution parameter
1849 * @param map Object[] the map in NESTED scheme
1850 * @return - Object[] a map in RING scheme
1851 * @throws IllegalArgumentException
1852 */
1853 public Object[] convert_nest2ring(long nside, Object[] map) {
1854 Object[] res;
1855 long npix, ipn;
1856 int ipr;
1857 npix = 12 * nside * nside;
1858 res = new Object[(int) npix];
1859 for (ipn = 0; ipn < npix; ipn++) {
1860 ipr = (int) nest2ring(nside, ipn);
1861 res[ipr] = map[(int) ipn];
1862 }
1863 return res;
1864 }
1865
1866 /**
1867 * makes the conversion RING to NEST
1868 *
1869 * @param nside
1870 * long resolution
1871 * @param map
1872 * map in RING
1873 * @return map in NEST
1874 * @throws IllegalArgumentException
1875 */
1876 public Object[] convert_ring2nest(long nside, Object[] map) {
1877 Object[] res;
1878 long npix, ipn, ipr;
1879 npix = 12 * nside * nside;
1880 res = new Object[(int) npix];
1881 for (ipr = 0; ipr < npix; ipr++) {
1882 ipn = ring2nest(nside, ipr);
1883 res[(int) ipn] = map[(int)ipr];
1884 }
1885 return res;
1886 }
1887
1888 /**
1889 * converts a 8 byte Object map from RING to NESTED and vice versa in place,
1890 * ie without allocation a temporary map (Has no reason for Java). This
1891 * method is more general but slower than convert_nest2ring.
1892 *
1893 * This method is a wrapper for functions ring2nest and nest2ring. Their
1894 * names are supplied in the subcall argument.
1895 *
1896 * @param subcall
1897 * String name of the method to use.
1898 * @param map
1899 * Object[] map
1900 * @return resulting Object[] map.
1901 * @throws IllegalArgumentException
1902 */
1903 public Object[] convert_inplace_long(String subcall, Object[] map) {
1904 Object[] res;
1905 long npix, nside;
1906 boolean[] check;
1907 long ilast, i1, i2;
1908 String SID = "convert_in_place:";
1909 Object pixbuf1, pixbuf2;
1910 npix = map.length;
1911 nside = (long) Math.sqrt(npix / 12.);
1912 if (nside > ns_max) {
1913 throw new IllegalArgumentException(SID + " Map is too big");
1914 }
1915 check = new boolean[(int) npix];
1916 for (int i = 0; i < npix; i++)
1917 check[i] = false;
1918 ilast = 0; // start from first pixel
1919 for (int i = 0; i < npix; i++) {
1920 pixbuf2 = map[(int) ilast];
1921 i1 = ilast;
1922 if (subcall.equalsIgnoreCase("ring2nest")) {
1923 i2 = ring2nest(nside, i1);
1924 } else {
1925 i2 = nest2ring(nside, i1);
1926 }
1927 while (!check[(int) i2]) {
1928 pixbuf1 = map[(int) i2];
1929 map[(int) i2] = pixbuf2;
1930 pixbuf2 = pixbuf1;
1931 i1 = i2;
1932 if (subcall.equalsIgnoreCase("ring2nest")) {
1933 i2 = ring2nest(nside, i1);
1934 } else {
1935 i2 = nest2ring(nside, i1);
1936 }
1937 }
1938 while (!(check[(int) ilast] && (ilast < npix - 1))) {
1939 ilast++;
1940 }
1941 }
1942 res = map;
1943 return res;
1944 }
1945
1946 /**
1947 * returns 7 or 8 neighbours of any pixel in the nested scheme The neighbours
1948 * are ordered in the following way: First pixel is the one to the south (
1949 * the one west of the south direction is taken for pixels that don't have a
1950 * southern neighbour). From then on the neighbors are ordered in the
1951 * clockwise direction.
1952 *
1953 * @param nside the map resolution
1954 * @param ipix long pixel number
1955 * @return ArrayList
1956 * @throws IllegalArgumentException
1957 */
1958 public ArrayList neighbours_nest(long nside, long ipix) {
1959 ArrayList res = new ArrayList();
1960 long npix, ipf, ipo, ix, ixm, ixp, iy, iym, iyp, ixo, iyo;
1961 long face_num, other_face;
1962 long ia, ib, ibp, ibm, ib2, nsidesq;
1963 int icase;
1964 long local_magic1, local_magic2;
1965 long arb_const = 0;
1966 long[] ixiy = new long[2];
1967 long[] ixoiyo = new long[2];
1968 String SID = "neighbours_nest:";
1969 /* fill the pixel list with 0 */
1970 res.add(0, new Long(0));
1971 res.add(1, new Long(0));
1972 res.add(2, new Long(0));
1973 res.add(3, new Long(0));
1974 res.add(4, new Long(0));
1975 res.add(5, new Long(0));
1976 res.add(6, new Long(0));
1977 res.add(7, new Long(0));
1978 icase = 0;
1979 /* */
1980 if ((nside < 1) || (nside > ns_max)) {
1981 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
1982 }
1983 nsidesq = nside * nside;
1984 npix = 12 * nsidesq; // total number of pixels
1985 if ((ipix < 0) || (ipix > npix - 1)) {
1986 throw new IllegalArgumentException(SID + " ipix out of range ");
1987 }
1988 if (x2pix[xmax-1] <= 0)
1989 mk_xy2pix();
1990
1991 local_magic1 = (nsidesq - 1) / 3;
1992 local_magic2 = 2 * local_magic1;
1993 face_num = ipix / nsidesq;
1994 ipf = (long) BitManipulation.MODULO(ipix, nsidesq); // Pixel number in face
1995 ixiy = pix2xy_nest(nside, ipf);
1996 ix = ixiy[0];
1997 iy = ixiy[1];
1998 //
1999 ixm = ixiy[0] - 1;
2000 ixp = ixiy[0] + 1;
2001 iym = ixiy[1] - 1;
2002 iyp = ixiy[1] + 1;
2003
2004 icase = 0; // inside the face
2005
2006 /* exclude corners */
2007 if (ipf == local_magic2 && icase == 0)
2008 icase = 5; // West corner
2009 if (ipf == (nsidesq - 1) && icase == 0)
2010 icase = 6; // North corner
2011 if (ipf == 0 && icase == 0)
2012 icase = 7; // South corner
2013 if (ipf == local_magic1 && icase == 0)
2014 icase = 8; // East corner
2015
2016 /* detect edges */
2017 if ((ipf & local_magic1) == local_magic1 && icase == 0)
2018 icase = 1; // NorthEast
2019 if ((ipf & local_magic1) == 0 && icase == 0)
2020 icase = 2; // SouthWest
2021 if ((ipf & local_magic2) == local_magic2 && icase == 0)
2022 icase = 3; // NorthWest
2023 if ((ipf & local_magic2) == 0 && icase == 0)
2024 icase = 4; // SouthEast
2025
2026 /* iside a face */
2027 if (icase == 0) {
2028 res.add(0, new Long( xy2pix_nest(nside, ixm, iym, face_num)));
2029 res.add(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
2030 res.add(2, new Long( xy2pix_nest(nside, ixm, iyp, face_num)));
2031 res.add(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
2032 res.add(4, new Long( xy2pix_nest(nside, ixp, iyp, face_num)));
2033 res.add(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
2034 res.add(6, new Long( xy2pix_nest(nside, ixp, iym, face_num)));
2035 res.add(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
2036 }
2037 /* */
2038 ia = face_num / 4; // in [0,2]
2039 ib = (long) BitManipulation.MODULO(face_num, 4); // in [0,3]
2040 ibp = (long) BitManipulation.MODULO(ib + 1, 4);
2041 ibm = (long) BitManipulation.MODULO(ib + 4 - 1, 4);
2042 ib2 = (long) BitManipulation.MODULO(ib + 2, 4);
2043
2044 if (ia == 0) { // North pole region
2045 switch (icase) {
2046 case 1: // north-east edge
2047 other_face = 0 + ibp;
2048 res.set(0, new Long( xy2pix_nest(nside, ixm, iym, face_num)));
2049 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
2050 res.set(2, new Long( xy2pix_nest(nside, ixm, iyp, face_num)));
2051 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
2052 res.set(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
2053 ipo = (long) BitManipulation.MODULO(BitManipulation.swapLSBMSB( ipf), nsidesq);
2054 ixoiyo = pix2xy_nest(nside, ipo);
2055 ixo = ixoiyo[0];
2056 iyo = ixoiyo[1];
2057 res.set(4, new Long( xy2pix_nest(nside, ixo + 1, iyo,
2058 other_face)));
2059 res.set(5, new Long( (other_face * nsidesq + ipo)));
2060 res.set(6, new Long( xy2pix_nest(nside, ixo - 1, iyo,
2061 other_face)));
2062 break;
2063 case 2: // SouthWest edge
2064 other_face = 4 + ib;
2065 ipo = (long) BitManipulation.MODULO(BitManipulation.invLSB( ipf), nsidesq); // SW-NE flip
2066 ixoiyo = pix2xy_nest(nside, ipo);
2067 ixo = ixoiyo[0];
2068 iyo = ixoiyo[1];
2069 res.set(0, new Long( xy2pix_nest(nside, ixo, iyo - 1,
2070 other_face)));
2071 res.set(1, new Long( (other_face * nsidesq + ipo)));
2072 res.set(2, new Long( xy2pix_nest(nside, ixo, iyo + 1,
2073 other_face)));
2074 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
2075 res.set(4, new Long( xy2pix_nest(nside, ixp, iyp, face_num)));
2076 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
2077 res.set(6, new Long( xy2pix_nest(nside, ixp, iym, face_num)));
2078 res.set(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
2079 break;
2080 case 3: // NorthWest edge
2081 other_face = 0 + ibm;
2082 ipo = (long) BitManipulation.MODULO(BitManipulation.swapLSBMSB( ipf), nsidesq); // E-W flip
2083 ixoiyo = pix2xy_nest(nside, ipo);
2084 ixo = ixoiyo[0];
2085 iyo = ixoiyo[1];
2086 res.set(0, new Long( xy2pix_nest(nside, ixm, iym, face_num)));
2087 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
2088 res.set(2, new Long( xy2pix_nest(nside, ixo, iyo - 1,
2089 other_face)));
2090 res.set(3, new Long( (other_face * nsidesq + ipo)));
2091 res.set(4, new Long( xy2pix_nest(nside, ixo, iyo + 1,
2092 other_face)));
2093 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
2094 res.set(6, new Long( xy2pix_nest(nside, ixp, iym, face_num)));
2095 res.set(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
2096 break;
2097 case 4: // SouthEast edge
2098 other_face = 4 + ibp;
2099 ipo = (long) BitManipulation.MODULO(BitManipulation.invMSB( ipf), nsidesq); // SE-NW flip
2100 ixoiyo = pix2xy_nest(nside, ipo);
2101 ixo = ixoiyo[0];
2102 iyo = ixoiyo[1];
2103 res.set(0, new Long( xy2pix_nest(nside, ixo - 1, iyo,
2104 other_face)));
2105 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
2106 res.set(2, new Long( xy2pix_nest(nside, ixm, iyp, face_num)));
2107 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
2108 res.set(4, new Long( xy2pix_nest(nside, ixp, iyp, face_num)));
2109 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
2110 res.set(6, new Long( xy2pix_nest(nside, ixo + 1, iyo,
2111 other_face)));
2112 res.set(7, new Long( (other_face * nsidesq + ipo)));
2113 break;
2114 case 5: // West corner
2115 other_face = 4 + ib;
2116 arb_const = other_face * nsidesq + nsidesq - 1;
2117 res.set(0, new Long( (arb_const - 2)));
2118 res.set(1, new Long( arb_const));
2119 other_face = 0 + ibm;
2120 arb_const = other_face * nsidesq + local_magic1;
2121 res.set(2, new Long( arb_const));
2122 res.set(3, new Long( (arb_const + 2)));
2123 res.set(4, new Long( (ipix + 1)));
2124 res.set(5, new Long( (ipix - 1)));
2125 res.set(6, new Long( (ipix - 2)));
2126 res.remove(7);
2127 break;
2128 case 6: // North corner
2129 other_face = 0 + ibm;
2130 res.set(0, new Long( (ipix - 3)));
2131 res.set(1, new Long( (ipix - 1)));
2132 arb_const = other_face * nsidesq + nsidesq - 1;
2133 res.set(2, new Long( (arb_const - 2)));
2134 res.set(3, new Long( arb_const));
2135 other_face = 0 + ib2;
2136 res.set(4, new Long( (other_face * nsidesq + nsidesq - 1)));
2137 other_face = 0 + ibp;
2138 arb_const = other_face * nsidesq + nsidesq - 1;
2139 res.set(5, new Long( arb_const));
2140 res.set(6, new Long( (arb_const - 1)));
2141 res.set(7, new Long( (ipix - 2)));
2142 break;
2143 case 7: // South corner
2144 other_face = 8 + ib;
2145 res.set(0, new Long( (other_face * nsidesq + nsidesq - 1)));
2146 other_face = 4 + ib;
2147 arb_const = other_face * nsidesq + local_magic1;
2148 res.set(1, new Long( arb_const));
2149 res.set(2, new Long( (arb_const + 2)));
2150 res.set(3, new Long( (ipix + 2)));
2151 res.set(4, new Long( (ipix + 3)));
2152 res.set(5, new Long( (ipix + 1)));
2153 other_face = 4 + ibp;
2154 arb_const = other_face * nsidesq + local_magic2;
2155 res.set(6, new Long( (arb_const + 1)));
2156 res.set(7, new Long( arb_const));
2157 break;
2158 case 8: // East corner
2159 other_face = 0 + ibp;
2160 res.set(1, new Long( (ipix - 1)));
2161 res.set(2, new Long( (ipix + 1)));
2162 res.set(3, new Long( (ipix + 2)));
2163 arb_const = other_face * nsidesq + local_magic2;
2164 res.set(4, new Long( (arb_const + 1)));
2165 res.set(5, new Long(( arb_const)));
2166 other_face = 4 + ibp;
2167 arb_const = other_face * nsidesq + nsidesq - 1;
2168 res.set(0, new Long( (arb_const - 1)));
2169 res.set(6, new Long( arb_const));
2170 res.remove(7);
2171 break;
2172 }
2173 } else if (ia == 1) { // Equatorial region
2174 switch (icase) {
2175 case 1: // north-east edge
2176 other_face = 0 + ibp;
2177 res.set(0, new Long( xy2pix_nest(nside, ixm, iym, face_num)));
2178 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
2179 res.set(2, new Long( xy2pix_nest(nside, ixm, iyp, face_num)));
2180 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
2181 res.set(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
2182 ipo = (long) BitManipulation.MODULO(BitManipulation.invLSB( ipf), nsidesq); // NE-SW flip
2183 ixoiyo = pix2xy_nest(nside, ipo);
2184 ixo = ixoiyo[0];
2185 iyo = ixoiyo[1];
2186 res.set(4, new Long( xy2pix_nest(nside, ixo, iyo + 1,
2187 other_face)));
2188 res.set(5, new Long( (other_face * nsidesq + ipo)));
2189 res.set(6, new Long( xy2pix_nest(nside, ixo, iyo - 1,
2190 other_face)));
2191 break;
2192 case 2: // SouthWest edge
2193 other_face = 8 + ibm;
2194 ipo = (long) BitManipulation.MODULO(BitManipulation.invLSB( ipf), nsidesq); // SW-NE flip
2195 ixoiyo = pix2xy_nest(nside, ipo);
2196 ixo = ixoiyo[0];
2197 iyo = ixoiyo[1];
2198 res.set(0, new Long( xy2pix_nest(nside, ixo, iyo - 1,
2199 other_face)));
2200 res.set(1, new Long((other_face * nsidesq + ipo)));
2201 res.set(2, new Long( xy2pix_nest(nside, ixo, iyo + 1,
2202 other_face)));
2203 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
2204 res.set(4, new Long( xy2pix_nest(nside, ixp, iyp, face_num)));
2205 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
2206 res.set(6, new Long( xy2pix_nest(nside, ixp, iym, face_num)));
2207 res.set(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
2208 break;
2209 case 3: // NortWest edge
2210 other_face = 0 + ibm;
2211 ipo = (long) BitManipulation.MODULO(BitManipulation.invMSB( ipf), nsidesq); // NW-SE flip
2212 ixoiyo = pix2xy_nest(nside, ipo);
2213 ixo = ixoiyo[0];
2214 iyo = ixoiyo[1];
2215 res.set(2, new Long( xy2pix_nest(nside, ixo - 1, iyo,
2216 other_face)));
2217 res.set(3, new Long( (other_face * nsidesq + ipo)));
2218 res.set(4, new Long( xy2pix_nest(nside, ixo + 1, iyo,
2219 other_face)));
2220 res.set(0, new Long( xy2pix_nest(nside, ixm, iym, face_num)));
2221 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
2222 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
2223 res.set(6, new Long( xy2pix_nest(nside, ixp, iym, face_num)));
2224 res.set(7, new Long(xy2pix_nest(nside, ix, iym, face_num)));
2225 break;
2226 case 4: // SouthEast edge
2227 other_face = 8 + ib;
2228 ipo = (long) BitManipulation.MODULO(BitManipulation.invMSB( ipf), nsidesq); // SE-NW flip
2229 ixoiyo = pix2xy_nest(nside, ipo);
2230 ixo = ixoiyo[0];
2231 iyo = ixoiyo[1];
2232 res.set(0, new Long( xy2pix_nest(nside, ixo - 1, iyo,
2233 other_face)));
2234 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
2235 res.set(2, new Long( xy2pix_nest(nside, ixm, iyp, face_num)));
2236 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
2237 res.set(4, new Long( xy2pix_nest(nside, ixp, iyp, face_num)));
2238 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
2239 res.set(6, new Long( xy2pix_nest(nside, ixo + 1, iyo,
2240 other_face)));
2241 res.set(7, new Long( (other_face * nsidesq + ipo)));
2242 break;
2243 case 5: // West corner
2244 other_face = 8 + ibm;
2245 arb_const = other_face * nsidesq + nsidesq - 1;
2246 res.set(0, new Long( (arb_const - 2)));
2247 res.set(1, new Long( arb_const));
2248 other_face = 4 + ibm;
2249 res.set(2, new Long( (other_face * nsidesq + local_magic1)));
2250 other_face = 0 + ibm;
2251 arb_const = other_face * nsidesq;
2252 res.set(3, new Long( arb_const));
2253 res.set(4, new Long( (arb_const + 1)));
2254 res.set(5, new Long( (ipix + 1)));
2255 res.set(6, new Long( (ipix - 1)));
2256 res.set(7, new Long( (ipix - 2)));
2257 break;
2258 case 6: // North corner
2259 other_face = 0 + ibm;
2260 res.set(0, new Long( (ipix - 3)));
2261 res.set(1, new Long( (ipix - 1)));
2262 arb_const = other_face * nsidesq + local_magic1;
2263 res.set(2, new Long( (arb_const - 1)));
2264 res.set(3, new Long( arb_const));
2265 other_face = 0 + ib;
2266 arb_const = other_face * nsidesq + local_magic2;
2267 res.set(4, new Long( arb_const));
2268 res.set(5, new Long( (arb_const - 2)));
2269 res.set(6, new Long( (ipix - 2)));
2270 res.remove(7);
2271 break;
2272 case 7: // South corner
2273 other_face = 8 + ibm;
2274 arb_const = other_face * nsidesq + local_magic1;
2275 res.set(0, new Long( arb_const));
2276 res.set(1, new Long( (arb_const + 2)));
2277 res.set(2, new Long( (ipix + 2)));
2278 res.set(3, new Long( (ipix + 3)));
2279 res.set(4, new Long( (ipix + 1)));
2280 other_face = 8 + ib;
2281 arb_const = other_face * nsidesq + local_magic2;
2282 res.set(5, new Long( (arb_const + 1)));
2283 res.set(6, new Long( arb_const));
2284 res.remove(7);
2285 break;
2286 case 8: // East corner
2287 other_face = 8 + ib;
2288 arb_const = other_face * nsidesq + nsidesq - 1;
2289 res.set(0, new Long( (arb_const - 1)));
2290 res.set(1, new Long( (ipix - 1)));
2291 res.set(2, new Long( (ipix + 1)));
2292 res.set(3, new Long( (ipix + 2)));
2293 res.set(7, new Long( arb_const));
2294 other_face = 0 + ib;
2295 arb_const = other_face * nsidesq;
2296 res.set(4, new Long( (arb_const + 2)));
2297 res.set(5, new Long( arb_const));
2298 other_face = 4 + ibp;
2299 res.set(6, new Long( (other_face * nsidesq + local_magic2)));
2300 break;
2301 }
2302 } else { // South pole region
2303 switch (icase) {
2304 case 1: // North-East edge
2305 other_face = 4 + ibp;
2306 res.set(0, new Long( xy2pix_nest(nside, ixm, iym, face_num)));
2307 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
2308 res.set(2, new Long( xy2pix_nest(nside, ixm, iyp, face_num)));
2309 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
2310 res.set(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
2311 ipo = (long) BitManipulation.MODULO(BitManipulation.invLSB( ipf), nsidesq); // NE-SW flip
2312 ixoiyo = pix2xy_nest(nside, ipo);
2313 ixo = ixoiyo[0];
2314 iyo = ixoiyo[1];
2315 res.set(4, new Long( xy2pix_nest(nside, ixo, iyo + 1,
2316 other_face)));
2317 res.set(5, new Long( (other_face * nsidesq + ipo)));
2318 res.set(6, new Long( xy2pix_nest(nside, ixo, iyo - 1,
2319 other_face)));
2320 break;
2321 case 2: // SouthWest edge
2322 other_face = 8 + ibm;
2323 ipo = (long) BitManipulation.MODULO(BitManipulation.swapLSBMSB( ipf), nsidesq); // W-E flip
2324 ixoiyo = pix2xy_nest(nside, ipo);
2325 ixo = ixoiyo[0];
2326 iyo = ixoiyo[1];
2327 res.set(0, new Long( xy2pix_nest(nside, ixo - 1, iyo,
2328 other_face)));
2329 res.set(1, new Long( (other_face * nsidesq + ipo)));
2330 res.set(2, new Long( xy2pix_nest(nside, ixo + 1, iyo,
2331 other_face)));
2332 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
2333 res.set(4, new Long( xy2pix_nest(nside, ixp, iyp, face_num)));
2334 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
2335 res.set(6, new Long(xy2pix_nest(nside, ixp, iym, face_num)));
2336 res.set(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
2337 break;
2338 case 3: // NorthWest edge
2339 other_face = 4 + ib;
2340 ipo = (long) BitManipulation.MODULO(BitManipulation.invMSB( ipf), nsidesq);
2341 ixoiyo = pix2xy_nest(nside, ipo);
2342 ixo = ixoiyo[0];
2343 iyo = ixoiyo[1];
2344 res.set(0, new Long( xy2pix_nest(nside, ixm, iym, face_num)));
2345 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
2346 res.set(2, new Long( xy2pix_nest(nside, ixo - 1, iyo,
2347 other_face)));
2348 res.set(3, new Long( (other_face * nsidesq + ipo)));
2349 res.set(4, new Long( xy2pix_nest(nside, ixo + 1, iyo,
2350 other_face)));
2351 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
2352 res.set(6, new Long( xy2pix_nest(nside, ixp, iym, face_num)));
2353 res.set(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
2354 break;
2355 case 4: // SouthEast edge
2356 other_face = 8 + ibp;
2357 ipo = (long) BitManipulation.MODULO(BitManipulation.swapLSBMSB( ipf), nsidesq); // SE-NW
2358 // flip
2359 ixoiyo = pix2xy_nest(nside, ipo);
2360 ixo = ixoiyo[0];
2361 iyo = ixoiyo[1];
2362 res.set(0, new Long( xy2pix_nest(nside, ixo, iyo - 1,
2363 other_face)));
2364 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
2365 res.set(2, new Long( xy2pix_nest(nside, ixm, iyp, face_num)));
2366 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
2367 res.set(4, new Long( xy2pix_nest(nside, ixp, iyp, face_num)));
2368 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
2369 res.set(6, new Long( xy2pix_nest(nside, ixo, iyo + 1,
2370 other_face)));
2371 res.set(7, new Long( (other_face * nsidesq + ipo)));
2372 break;
2373 case 5: // West corner
2374 other_face = 8 + ibm;
2375 arb_const = other_face * nsidesq + local_magic1;
2376 res.set(0, new Long( (arb_const - 2)));
2377 res.set(1, new Long( arb_const));
2378 other_face = 4 + ib;
2379 res.set(2, new Long( (other_face * nsidesq)));
2380 res.set(3, new Long( (other_face * nsidesq + 1)));
2381 res.set(4, new Long( (ipix + 1)));
2382 res.set(5, new Long( (ipix - 1)));
2383 res.set(6, new Long( (ipix - 2)));
2384 res.remove(7);
2385 break;
2386 case 6: // North corner
2387 other_face = 4 + ib;
2388 res.set(0, new Long( (ipix - 3)));
2389 res.set(1, new Long((ipix - 1)));
2390 arb_const = other_face * nsidesq + local_magic1;
2391 res.set(2, new Long( (arb_const - 1)));
2392 res.set(3, new Long( arb_const));
2393 other_face = 0 + ib;
2394 res.set(4, new Long( (other_face * nsidesq)));
2395 other_face = 4 + ibp;
2396 arb_const = other_face * nsidesq + local_magic2;
2397 res.set(5, new Long( arb_const));
2398 res.set(6, new Long( (arb_const - 2)));
2399 res.set(7, new Long( (ipix - 2)));
2400 break;
2401 case 7: // South corner
2402 other_face = 8 + ib2;
2403 res.set(0, new Long( (other_face * nsidesq)));
2404 other_face = 8 + ibm;
2405 arb_const = other_face * nsidesq;
2406 res.set(1, new Long( arb_const));
2407 res.set(2, new Long( (arb_const + 1)));
2408 res.set(3, new Long( (ipix + 2)));
2409 res.set(4, new Long( (ipix + 3)));
2410 res.set(5, new Long( (ipix + 1)));
2411 other_face = 8 + ibp;
2412 arb_const = other_face * nsidesq;
2413 res.set(6, new Long( (arb_const + 2)));
2414 res.set(7, new Long( arb_const));
2415 break;
2416 case 8: // East corner
2417 other_face = 8 + ibp;
2418 res.set(1, new Long( (ipix - 1)));
2419 res.set(2, new Long( (ipix + 1)));
2420 res.set(3, new Long( (ipix + 2)));
2421 arb_const = other_face * nsidesq + local_magic2;
2422 res.set(6, new Long( arb_const));
2423 res.set(0, new Long( (arb_const - 2)));
2424 other_face = 4 + ibp;
2425 arb_const = other_face * nsidesq;
2426 res.set(4, new Long( (arb_const + 2)));
2427 res.set(5, new Long( arb_const));
2428 res.remove(7);
2429 break;
2430 }
2431 }
2432 return res;
2433 }
2434
2435 /**
2436 * returns the list of pixels in RING or NEST scheme with latitude in [phi0 -
2437 * dpi, phi0 + dphi] on the ring iz in [1, 4*nside -1 ] The pixel id numbers
2438 * are in [0, 12*nside^2 - 1] the indexing is in RING, unless nest is set to
2439 * 1
2440 *
2441 * @param nside
2442 * long the map resolution
2443 * @param iz
2444 * long ring number
2445 * @param phi0
2446 * double
2447 * @param dphi
2448 * double
2449 * @param nest
2450 * boolean format flag
2451 * @return ArrayList of pixels
2452 * @throws IllegalArgumentException
2453 *
2454 * Modified by N. Kuropatkin 07/09/2008 Corrected several bugs and make test of all cases.
2455 *
2456 */
2457 public ArrayList InRing(long nside, long iz, double phi0, double dphi,
2458 boolean nest) {
2459 boolean take_all = false;
2460 boolean to_top = false;
2461 boolean do_ring = true;
2462 boolean conservative = false;
2463 // String SID = "InRing:";
2464 double epsilon = Double.MIN_VALUE; // the constant to eliminate
2465 // java calculation jitter
2466 if (nest)
2467 do_ring = false;
2468 double shift = 0.;
2469 long ir = 0;
2470 long kshift, nr, ipix1, ipix2, nir1, nir2, ncap, npix;
2471 long ip_low = 0, ip_hi = 0, in, inext, nir;
2472 ArrayList res = new ArrayList();
2473 npix = 12 * nside * nside; // total number of pixels
2474 ncap = 2 * nside * (nside - 1); // number of pixels in the north polar
2475 // cap
2476 double phi_low = BitManipulation.MODULO(phi0 - dphi, TWOPI) - epsilon; // phi min,
2477 // excluding
2478 // 2pi period
2479 double phi_hi = BitManipulation.MODULO(phi0 + dphi, TWOPI) + epsilon;
2480
2481 //
2482 if (Math.abs(dphi - PI) < epsilon) take_all = true;
2483
2484 /* identifies ring number */
2485 if ((iz >= nside) && (iz <= 3 * nside)) { // equatorial region
2486 ir = iz - nside + 1; // in [1, 2*nside + 1]
2487 ipix1 = ncap + 4 * nside * (ir - 1); // lowest pixel number in the
2488 // ring
2489 ipix2 = ipix1 + 4 * nside - 1; // highest pixel number in the ring
2490 kshift = (long) BitManipulation.MODULO(ir, 2.);
2491
2492 nr = nside * 4;
2493 } else {
2494 if (iz < nside) { // north pole
2495 ir = iz;
2496 ipix1 = 2 * ir * (ir - 1); // lowest pixel number
2497 ipix2 = ipix1 + 4 * ir - 1; // highest pixel number
2498 } else { // south pole
2499 ir = 4 * nside - iz;
2500
2501 ipix1 = npix - 2 * ir * (ir + 1); // lowest pixel number
2502 ipix2 = ipix1 + 4 * ir - 1; // highest pixel number
2503 }
2504 nr = ir * 4;
2505 kshift = 1;
2506 }
2507
2508 // Construct the pixel list
2509 if (take_all) {
2510 nir = ipix2 - ipix1 + 1;
2511 if (do_ring) {
2512 long ind = 0;
2513 for (long i = ipix1; i <= ipix2; i++) {
2514 res.add((int) ind, new Long(i));
2515 ind++;
2516 }
2517 } else {
2518 // in = ring2nest(nside, ipix1);
2519 // res.add(0, new Long( in));
2520 int ind = 0;
2521 for (int i = 0; i < nir; i++) {
2522 // inext = next_in_line_nest(nside, in);
2523 // in = inext;
2524 in = ring2nest(nside, ipix1 + i);
2525 res.add( i, new Long(in));
2526
2527 }
2528 }
2529
2530 return res;
2531 }
2532
2533 shift = kshift / 2.0;
2534
2535 // conservative : include every intersected pixel, even if the
2536 // pixel center is out of the [phi_low, phi_hi] region
2537 if (conservative) {
2538 ip_low = (long) Math.round((nr * phi_low) / TWOPI - shift);
2539 ip_hi = (long) Math.round((nr * phi_hi) / TWOPI - shift);
2540
2541 ip_low = (long) BitManipulation.MODULO(ip_low, nr); // in [0, nr - 1]
2542 ip_hi = (long) BitManipulation.MODULO(ip_hi, nr); // in [0, nr - 1]
2543 // System.out.println("ip_low="+ip_low+" ip_hi="+ip_hi);
2544 } else { // strict: includes only pixels whose center is in
2545 // [phi_low,phi_hi]
2546
2547 ip_low = (long) Math.ceil((nr * phi_low) / TWOPI - shift);
2548 ip_hi = (long) Math.floor((nr * phi_hi) / TWOPI - shift);
2549 if (ip_low == ip_hi + 1)
2550 ip_low = ip_hi;
2551
2552 if ((ip_low - ip_hi == 1) && (dphi * nr < PI)) {
2553 // the interval is too small ( and away from pixel center)
2554 // so no pixels is included in the list
2555 System.out
2556 .println("the interval is too small and avay from center");
2557 return res; // return empty list
2558 }
2559
2560 ip_low = Math.min(ip_low, nr - 1);
2561 ip_hi = Math.max(ip_hi, 0);
2562 }
2563
2564 //
2565 if (ip_low > ip_hi)
2566 to_top = true;
2567
2568 if (to_top) {
2569 ip_low += ipix1;
2570 ip_hi += ipix1;
2571 nir1 = ipix2 - ip_low + 1;
2572
2573 nir2 = ip_hi + 1;
2574 nir = nir1 + nir2;
2575 if (do_ring) {
2576 int ind = 0;
2577 for (long i = ip_low; i <= ipix2; i++) {
2578 res.add(ind, new Long(i));
2579 ind++;
2580 }
2581 // ind = nir1;
2582 for (long i = ipix1; i <= ip_hi; i++) {
2583 res.add(ind, new Long(i));
2584 ind++;
2585 }
2586 } else {
2587 int ind = 0;
2588 for (long i = ip_low; i <= ipix2; i++) {
2589 in = ring2nest(nside, i);
2590 res.add(ind, new Long(in));
2591 ind++;
2592 }
2593 for (long i = ipix1; i <= ip_hi; i++) {
2594 in = ring2nest(nside, i);
2595 res.add(ind, new Long(in));
2596 ind++;
2597 }
2598
2599 }
2600 } else {
2601 nir = ip_hi - ip_low + 1;
2602 // System.out.println("nir="+nir+" ip_low="+ip_low+" ip_hi="+ip_hi+" ipix1="+
2603 // ipix1+" ipix2="+ipix2);
2604 //
2605 // Special case when ip_low < 0
2606 //
2607 if (ip_low < 0 ){
2608 ip_low = Math.abs(ip_low) ;
2609 nir1 = ip_low;
2610 nir2 = ip_hi + 1;
2611 nir = nir1 + nir2;
2612 if (do_ring) {
2613 int ind = 0;
2614 for (long i = 0; i < ip_low; i++) {
2615 res.add(ind, new Long(ipix2 - i));
2616 ind++;
2617 }
2618 for (long i = 0; i <= ip_hi; i++) {
2619 res.add(ind, new Long(ipix1 + i));
2620 ind++;
2621 }
2622
2623 } else {
2624
2625 int ind = 0;
2626 for (int i = 0; i < ip_low; i++) {
2627 in = ring2nest(nside, ipix2 - i);
2628 res.add(ind, new Long(in));
2629 ind++;
2630 }
2631 for (long i = 0; i <= ip_hi; i++) {
2632 in = ring2nest(nside, ipix1 + i);
2633 res.add(ind, new Long(in));
2634 ind++;
2635 }
2636
2637 }
2638 // System.out.println("nir="+nir+" ip_low="+ip_low+" ip_hi="+ip_hi+" ipix1="+
2639 // ipix1+" ipix2="+ipix2);
2640 // for (int i=0; i< res.size(); i++) {
2641 // System.out.print(" "+ res.get(i));
2642 // }
2643 // System.out.println();
2644 return res;
2645
2646 }
2647 ip_low += ipix1;
2648 ip_hi += ipix1;
2649 if (do_ring) {
2650 int ind = 0;
2651 for (long i = ip_low; i <= ip_hi; i++) {
2652 res.add(ind, new Long(i));
2653 ind++;
2654 }
2655 } else {
2656 // in = ring2nest(nside, ip_low);
2657 // res.add(0, new Long(in));
2658 int ind = 0;
2659 for (long i = ip_low; i <= ip_hi; i++) {
2660 // inext = next_in_line_nest(nside, in);
2661 // in = inext;
2662 in = ring2nest(nside, i);
2663 res.add(ind, new Long(in));
2664 ind++;
2665 }
2666 }
2667
2668 }
2669
2670 return res;
2671 }
2672
2673 /**
2674 * calculates the pixel that lies on the East side (and the same
2675 * latitude) as the given NESTED pixel number - ipix
2676 *
2677 * @param nside
2678 * long resolution
2679 * @param ipix
2680 * long pixel number
2681 * @return long next pixel in line
2682 * @throws IllegalArgumentException
2683 */
2684 public long next_in_line_nest(long nside, long ipix) {
2685 long npix, ipf, ipo, ix, ixp, iy, iym, ixo, iyo, face_num, other_face;
2686 long ia, ib, ibp, nsidesq;
2687 long ibm, ib2;
2688 int icase;
2689 long local_magic1, local_magic2;
2690 long[] ixiy = new long[2];
2691 long inext = 0; // next in line pixel in Nest scheme
2692 String SID = "next_in_line:";
2693 if ((nside < 1) || (nside > ns_max)) {
2694 throw new IllegalArgumentException(SID + " nside should be power of 2 >0 and < "+ns_max);
2695 }
2696 nsidesq = nside * nside;
2697 npix = 12 * nsidesq; // total number of pixels
2698 if ((ipix < 0) || (ipix > npix - 1)) {
2699 throw new IllegalArgumentException(SID + " ipix out of range defined by nside");
2700 }
2701 // initiates array for (x,y) -> pixel number -> (x,y) mapping
2702 if (x2pix[xmax-1] <= 0)
2703 mk_xy2pix();
2704 local_magic1 = (nsidesq - 1) / 3;
2705 local_magic2 = 2 * local_magic1;
2706 face_num = ipix / nsidesq;
2707 ipf = (long) BitManipulation.MODULO(ipix, nsidesq); // Pixel number in face
2708 ixiy = pix2xy_nest(nside, ipf);
2709 ix = ixiy[0];
2710 iy = ixiy[1];
2711 ixp = ix + 1;
2712 iym = iy - 1;
2713 boolean sel = false;
2714 icase = -1; // iside the nest flag
2715 // Exclude corners
2716 if (ipf == local_magic2) { // west coirner
2717 inext = ipix - 1;
2718 return inext;
2719 }
2720 if ((ipf == nsidesq - 1) && !sel) { // North corner
2721 icase = 6;
2722 sel = true;
2723 }
2724 if ((ipf == 0) && !sel) { // Siuth corner
2725 icase = 7;
2726 sel = true;
2727 }
2728 if ((ipf == local_magic1) && !sel) { // East corner
2729 icase = 8;
2730 sel = true;
2731 }
2732 // Detect edges
2733 if (((ipf & local_magic1) == local_magic1) && !sel) { // North-East
2734 icase = 1;
2735 sel = true;
2736 }
2737 if (((ipf & local_magic2) == 0) && !sel) { // South-East
2738 icase = 4;
2739 sel = true;
2740 }
2741 if (!sel) { // iside a face
2742 inext = xy2pix_nest(nside, ixp, iym, face_num);
2743 return inext;
2744 }
2745 //
2746 ia = face_num / 4; // in [0,2]
2747 ib = (long) BitManipulation.MODULO(face_num, 4); // in [0,3]
2748 ibp = (long) BitManipulation.MODULO(ib + 1, 4);
2749 ibm = (long) BitManipulation.MODULO(ib + 4 - 1, 4);
2750 ib2 = (long) BitManipulation.MODULO(ib + 2, 4);
2751
2752 if (ia == 0) { // North pole region
2753 switch (icase) {
2754 case 1:
2755 other_face = 0 + ibp;
2756 ipo = (long) BitManipulation.MODULO(BitManipulation.swapLSBMSB( ipf), nsidesq);
2757 inext = other_face * nsidesq + ipo;
2758 break;
2759 case 4:
2760 other_face = 4 + ibp;
2761 ipo = (long) BitManipulation.MODULO(BitManipulation.invMSB( ipf), nsidesq); // SE-NW flip
2762
2763 ixiy = pix2xy_nest(nside, ipo);
2764 ixo = ixiy[0];
2765 iyo = ixiy[1];
2766
2767 inext = xy2pix_nest(nside, ixo + 1, iyo, other_face);
2768
2769 break;
2770 case 6: // North corner
2771 other_face = 0 + ibp;
2772 inext = other_face * nsidesq + nsidesq - 1;
2773 break;
2774 case 7:
2775 other_face = 4 + ibp;
2776 inext = other_face * nsidesq + local_magic2 + 1;
2777 break;
2778 case 8:
2779 other_face = 0 + ibp;
2780 inext = other_face * nsidesq + local_magic2;
2781 break;
2782 }
2783
2784 } else if (ia == 1) { // Equatorial region
2785 switch (icase) {
2786 case 1: // NorthEast edge
2787 other_face = 0 + ib;
2788 // System.out.println("ipf="+ipf+" nsidesq="+nsidesq+" invLSB="+BitManipulation.invLSB(ipf));
2789 ipo = (long) BitManipulation.MODULO((double)BitManipulation.invLSB( ipf), (double)nsidesq); // NE-SW flip
2790 // System.out.println(" ipo="+ipo);
2791
2792 ixiy = pix2xy_nest(nside, ipo);
2793 ixo = ixiy[0];
2794 iyo = ixiy[1];
2795 inext = xy2pix_nest(nside, ixo, iyo - 1, other_face);
2796 break;
2797 case 4: // SouthEast edge
2798 other_face = 8 + ib;
2799 ipo = (long) BitManipulation.MODULO(BitManipulation.invMSB( ipf), nsidesq);
2800 ixiy = pix2xy_nest(nside, ipo);
2801 inext = xy2pix_nest(nside, ixiy[0] + 1, ixiy[1], other_face);
2802 break;
2803 case 6: // Northy corner
2804 other_face = 0 + ib;
2805 inext = other_face * nsidesq + local_magic2 - 2;
2806 break;
2807 case 7: // South corner
2808 other_face = 8 + ib;
2809 inext = other_face * nsidesq + local_magic2 + 1;
2810 break;
2811 case 8: // East corner
2812 other_face = 4 + ibp;
2813 inext = other_face * nsidesq + local_magic2;
2814 break;
2815
2816 }
2817 } else { // South pole region
2818 switch (icase) {
2819 case 1: // NorthEast edge
2820 other_face = 4 + ibp;
2821 ipo = (long) BitManipulation.MODULO(BitManipulation.invLSB( ipf), nsidesq); // NE-SW flip
2822 ixiy = pix2xy_nest(nside, ipo);
2823 inext = xy2pix_nest(nside, ixiy[0], ixiy[1] - 1, other_face);
2824 break;
2825 case 4: // SouthEast edge
2826 other_face = 8 + ibp;
2827 ipo = (long) BitManipulation.MODULO(BitManipulation.swapLSBMSB( ipf), nsidesq); // E-W flip
2828 inext = other_face * nsidesq + ipo; // (8)
2829 break;
2830 case 6: // North corner
2831 other_face = 4 + ibp;
2832 inext = other_face * nsidesq + local_magic2 - 2;
2833 break;
2834 case 7: // South corner
2835 other_face = 8 + ibp;
2836 inext = other_face * nsidesq;
2837 break;
2838 case 8: // East corner
2839 other_face = 8 + ibp;
2840 inext = other_face * nsidesq + local_magic2;
2841 break;
2842 }
2843 }
2844 return inext;
2845 }
2846
2847 /**
2848 * renders the pixel number pix (NESTED scheme) for a pixel which contains a
2849 * point on a sphere at coordinate vector (x,y,z), given the map resolution
2850 * parameter nside.
2851 *
2852 * The computation is made to the highest resolution available (nside=ns_max)
2853 * and then degraded to that requared (by Integer division) this doesn't
2854 * cost much, and it makes sure that the treatment of round-off will be
2855 * consistent for every resolution
2856 *
2857 * @param nside
2858 * long the map resolution
2859 * @param vector
2860 * Vewctor3d the input vector
2861 * @return pixel long
2862 * @throws IllegalArgumentException
2863 */
2864 public long vect2pix_nest(long nside, Vector3d vector) {
2865 long pix = 0;
2866 double z, za, tt, tp, tmp, dnorm, phi;
2867 long jp, jm, ifp, ifm, face_num, ix, iy, ix_low, ix_hi;
2868 long iy_low, iy_hi, ipf, ntt;
2869 String SID = " vect2pix_nest:";
2870 /* */
2871 if (nside < 1 || nside > ns_max) {
2872 throw new IllegalArgumentException(SID + " nside should be power of 2 >0 and < ns_max");
2873 }
2874 if (x2pix[xmax-1] <= 0)
2875 mk_xy2pix();
2876
2877 dnorm = vector.length();
2878 z = vector.z / dnorm;
2879 phi = 0.; // phi in [-pi,pi]
2880 if (vector.x != 0. || vector.y != 0.)
2881 phi = Math.atan2(vector.y, vector.x);
2882 za = Math.abs(z);
2883 if (phi < 0.)
2884 phi += TWOPI; // phi in [0,2pi]
2885 tt = 2. * phi / PI; // in [0,4]
2886 if (za <= twothird) { // Equatorial region
2887 /*
2888 * the index of edge lines increases when the longitude = phi goes
2889 * up
2890 */
2891 jp = (long) (ns_max * (0.5 + tt - z * 0.75)); // ascending edge line
2892 // index
2893 jm = (long) (ns_max * (0.5 + tt + z * 0.75)); // descending edge line
2894 // index
2895 /* find the face */
2896 ifp = jp / ns_max; // in [0,4]
2897 ifm = jm / ns_max;
2898 if (ifp == ifm) { // faces 4 to 7
2899 face_num = (long) BitManipulation.MODULO(ifp, 4) + 4;
2900 } else if (ifp < ifm) { // (half-) faces 0 to 3
2901 face_num = (long) BitManipulation.MODULO(ifp, 4);
2902 } else { // (half-) faces 8 to 11
2903 face_num = (long) BitManipulation.MODULO(ifm, 4) + 8;
2904 }
2905
2906 ix = (long) BitManipulation.MODULO(jm, ns_max);
2907 iy = ns_max - (long) BitManipulation.MODULO(jp, ns_max) - 1;
2908 } else { // polar region, za > 2/3
2909 ntt = (long) tt;
2910 if (ntt >= 4)
2911 ntt = 3;
2912 tp = tt - ntt;
2913 tmp = Math.sqrt(3.0 * (1.0 - za)); // in [0,1]
2914 /*
2915 * the index of edge lines increases when distance from the closest
2916 * pole goes up
2917 */
2918 jp = (long) (ns_max * tp * tmp); // line going toward the pole has
2919 // phi increases
2920 jm = (long) (ns_max * (1.0 - tp) * tmp); // that one goes away of the
2921 // closest pole
2922 jp = Math.min(ns_max - 1, jp); // for points too close to the
2923 // boundary
2924 jm = Math.min(ns_max - 1, jm);
2925 /* finds the face and pixel's (x,y) */
2926 if (z >= 0) {
2927 face_num = ntt; // in [0,3]
2928 ix = ns_max - jm - 1;
2929 iy = ns_max - jp - 1;
2930 } else {
2931 face_num = ntt + 8; // in [8,11]
2932 ix = jp;
2933 iy = jm;
2934 }
2935 }
2936 ix_low = (long) BitManipulation.MODULO(ix, xmax);
2937 ix_hi = ix / xmax;
2938 iy_low = (long) BitManipulation.MODULO(iy, xmax);
2939 iy_hi = iy / xmax;
2940
2941 ipf = (x2pix[(int) (ix_hi + 1)] + y2pix[(int) (iy_hi + 1)]) * (xmax * xmax)
2942 + (x2pix[(int) (ix_low + 1)] + y2pix[(int) (iy_low + 1)]);
2943 ipf = ipf / ((ns_max / nside) * (ns_max / nside)); // in [0, nside**2
2944 // -1]
2945 pix = ipf + face_num * nside * nside; // in [0, 12*nside**2 -1]
2946 return pix;
2947 }
2948
2949 /**
2950 * gives the pixel number ipix (NESTED) corresponding to ix, iy and face_num
2951 *
2952 * @param nside
2953 * the map resolution parameter
2954 * @param ix
2955 * Integer x coordinate
2956 * @param iy
2957 * Integer y coordinate
2958 * @param face_num
2959 * long face number
2960 * @return long pixel number ipix
2961 * @throws IllegalArgumentException
2962 */
2963 private long xy2pix_nest(long nside, long ix, long iy, long face_num){
2964 long res = 0;
2965 long ix_low, ix_hi, iy_low, iy_hi, ipf;
2966 String SID = "xy2pix_nest:";
2967 //
2968 if ((nside < 1) || (nside > ns_max)) {
2969 throw new IllegalArgumentException(SID + " nside should be power of 2 >0 and < "+ns_max);
2970 }
2971 // if ((ix < 0) || (ix > nside - 1)) {
2972 // throw new IllegalArgumentException(SID + " ix out of range [0, nside-1]");
2973 // }
2974 // if ((iy < 0) || (iy > nside - 1)) {
2975 // throw new IllegalArgumentException(SID + " iy out of range [0, nside-1]");
2976 // }
2977 if (x2pix[xmax-1] <= 0)
2978 mk_xy2pix();
2979 ix_low = (long) BitManipulation.MODULO(ix, xmax);
2980 ix_hi = ix / xmax;
2981 iy_low = (long) BitManipulation.MODULO(iy, xmax);
2982 iy_hi = iy / xmax;
2983
2984 ipf = (x2pix[(int) (ix_hi + 1)] + y2pix[(int) (iy_hi + 1)]) * xmax * xmax
2985 + (x2pix[(int) (ix_low + 1)] + y2pix[(int) (iy_low + 1)]);
2986 res = ipf + face_num * nside * nside; // in [0, 12*nside^2 - 1]
2987
2988 return res;
2989 }
2990
2991 /**
2992 * gives the x,y coordinates in a face from pixel number within the face
2993 * (NESTED) schema.
2994 *
2995 * @param nside
2996 * long resolution parameter
2997 * @param ipf
2998 * long pixel number
2999 * @return ixiy long[] contains x and y coordinates
3000 * @throws IllegalArgumentException
3001 */
3002 private long[] pix2xy_nest(long nside, long ipf) {
3003 long[] ixiy = { 0, 0 };
3004 long ip_low, ip_trunc, ip_med, ip_hi;
3005 String SID = "pix2xy_nest:";
3006 // System.out.println(" ipf="+ipf+" nside="+nside);
3007 if ((nside < 1) || (nside > ns_max)) {
3008 throw new IllegalArgumentException(SID + " nside should be power of 2 >0 and < "+ns_max);
3009 }
3010 if ((ipf < 0) || (ipf > nside * nside - 1)) {
3011 throw new IllegalArgumentException(SID + " ipix out of range defined by nside");
3012 }
3013 if (pix2x[pixmax] <= 0)
3014 mk_pix2xy();
3015 ip_low = (long) BitManipulation.MODULO(ipf, pixmax); // contents of last 15 bits
3016 ip_trunc = ipf / pixmax; // truncation of the last 15 bits
3017 ip_med = (long) BitManipulation.MODULO(ip_trunc, pixmax); // next 15 bits
3018 ip_hi = ip_trunc / pixmax; // select high 15 bits
3019
3020 long ix = pixmax * pix2x[(int) ip_hi] + xmid * pix2x[(int) ip_med] + pix2x[(int) ip_low];
3021 long iy = pixmax * pix2y[(int) ip_hi] + xmid * pix2y[(int) ip_med] + pix2y[(int) ip_low];
3022 ixiy[0] = ix;
3023 ixiy[1] = iy;
3024 return ixiy;
3025 }
3026
3027 /**
3028 * creates an array of pixel numbers pix2x from x and y coordinates in the
3029 * face. Suppose NESTED scheme of pixel ordering Bits corresponding to x and
3030 * y are interleaved in the pixel number in even and odd bits.
3031 */
3032 private void mk_pix2xy() {
3033 long kpix, jpix, ix, iy, ip, id;
3034 boolean flag = true;
3035 for (kpix = 0; kpix <= pixmax; kpix++) { // loop on pixel numbers
3036 jpix = kpix;
3037 ix = 0;
3038 iy = 0;
3039 ip = 1; // bit position in x and y
3040
3041 while (jpix != 0) { // go through all the bits
3042
3043 id = (long) BitManipulation.MODULO(jpix, 2); // bit value (in kpix), goes in x
3044 jpix /= 2;
3045 ix += id * ip;
3046
3047 id = (long) BitManipulation.MODULO(jpix, 2); // bit value, goes in iy
3048 jpix /= 2;
3049 iy += id * ip;
3050
3051 ip *= 2; // next bit (in x and y )
3052 }
3053
3054 pix2x[(int) kpix] = ix; // in [0,pixmax]
3055 pix2y[(int) kpix] = iy; // in [0,pixmax]
3056
3057
3058 }
3059 }
3060
3061 /**
3062 * converts pixel number from ring numbering schema to the nested one
3063 *
3064 * @param nside
3065 * long resolution
3066 * @param ipring long pixel number in ring schema
3067 * @return long pixel number in nest schema
3068 * @throws IllegalArgumentException
3069 */
3070 public long ring2nest(long nside, long ipring) {
3071 long ipnest = 0;
3072 double fihip;
3073 double hip;
3074 long npix, nl2, nl4, ncap, ip, iphi, ipt, ipring1, kshift, face_num;
3075 long nr, irn, ire, irm, irs, irt, ifm, ifp, ix, iy, ix_low, ix_hi, iy_low;
3076 long iy_hi, ipf;
3077 long[] jrll = { 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4 }; // in units of
3078 // nside
3079 long[] jpll = { 0, 1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7 }; // in units of
3080 // nside/2
3081 String SID = "ring2nest:";
3082 //
3083 face_num = 0;
3084 if ((nside < 1) || (nside > ns_max)) {
3085 throw new IllegalArgumentException(SID + " nside should be power of 2 >0 and < "+ns_max);
3086 }
3087 npix = 12 * nside * nside; // total number of points
3088
3089 if ((ipring < 0) || (ipring > npix - 1)) {
3090 throw new IllegalArgumentException(SID + " ipring out of range [0,npix-1]");
3091 }
3092 if (x2pix[xmax-1] <= 0)
3093 mk_xy2pix();
3094
3095 nl2 = 2 * nside;
3096 nl4 = 4 * nside;
3097 ncap = nl2 * (nside - 1); // points in each polar cap, =0 for nside = 1
3098 ipring1 = ipring + 1;
3099 // finds the ring number, the position of the ring and the face number
3100 if (ipring1 <= ncap) { // north polar cap
3101 hip = ipring1 / 2.0;
3102 fihip = Math.floor(hip);
3103 irn = (long)Math.floor( Math.sqrt(hip - Math.sqrt(fihip))) + 1; // counted from
3104 // north pole
3105 iphi = ipring1 - 2 * irn * (irn - 1);
3106
3107 kshift = 0;
3108 nr = irn; // 1/4 of the number of points on the current ring
3109 face_num = (iphi - 1) / irn; // in [0,3 ]
3110
3111 } else if (ipring1 <= nl2 * (5 * nside + 1)) { // equatorial region
3112 ip = ipring1 - ncap - 1;
3113 irn = (long)Math.floor(ip / nl4) + nside; // counted from north pole
3114 iphi = (long) BitManipulation.MODULO(ip, nl4) + 1;
3115
3116 kshift = (long) BitManipulation.MODULO(irn + nside, 2); // 1 if odd 0
3117 // otherwise
3118 nr = nside;
3119 ire = irn - nside + 1; // in [1, 2*nside+1]
3120 irm = nl2 + 2 - ire;
3121 ifm = (iphi - ire / 2 + nside - 1) / nside; // face boundary
3122 ifp = (iphi - irm / 2 + nside - 1) / nside;
3123 if (ifp == ifm) {
3124 face_num = (long) BitManipulation.MODULO(ifp, 4.) + 4;
3125 } else if (ifp + 1 == ifm) { // (half-) faces 0 to 3
3126 face_num = ifp;
3127 } else if (ifp - 1 == ifm) { // (half-) faces 8 to 11
3128 face_num = ifp + 7;
3129 }
3130
3131
3132 } else { // south polar cap
3133
3134 ip = npix - ipring1 + 1;
3135 hip = ip / 2.0;
3136 fihip = Math.floor(hip);
3137 irs = (long)Math.floor( Math.sqrt(hip - Math.sqrt(fihip))) + 1;
3138 iphi = 4 * irs + 1 - (ip - 2 * irs * (irs - 1));
3139 kshift = 0;
3140 nr = irs;
3141 irn = nl4 - irs;
3142 face_num = (iphi - 1) / irs + 8; // in [8,11]
3143
3144
3145 }
3146 // finds the (x,y) on the face
3147
3148
3149 //
3150 irt = irn - jrll[(int) (face_num + 1)] * nside + 1; // in [-nside+1,0]
3151 ipt = 2 * iphi - jpll[(int) (face_num + 1)] * nr - kshift - 1; // in [-nside+1,
3152 // nside-1]
3153 //
3154 if (ipt >= nl2){
3155 ipt = ipt - 8*nside; // for the face #4
3156 }
3157 ix = (ipt - irt) / 2;
3158 iy = -(ipt + irt) / 2;
3159
3160 ix_low = (long) BitManipulation.MODULO(ix, xmax);
3161 ix_hi = ix / xmax;
3162 iy_low = (long) BitManipulation.MODULO(iy, xmax);
3163 iy_hi = iy / xmax;
3164
3165 //
3166
3167 ipf = (x2pix[(int) (ix_hi + 1)] + y2pix[(int) (iy_hi + 1)]) * xmax * xmax
3168 + (x2pix[(int) (ix_low + 1)] + y2pix[(int) (iy_low + 1)]); // in [0, nside**2 -1]
3169 ipnest = ipf + face_num * nside * nside; // in [0, 12*nside**2 -1]
3170
3171 return ipnest;
3172
3173 }
3174
3175 /**
3176 * converts from NESTED to RING pixel numbering
3177 *
3178 * @param nside
3179 * long resolution
3180 * @param ipnest
3181 * long NEST pixel number
3182 * @return ipring long RING pixel number
3183 * @throws IllegalArgumentException
3184 */
3185 public long nest2ring(long nside, long ipnest) {
3186 long res = 0;
3187 long npix, npface, face_num, ncap, n_before, ipf, ip_low, ip_trunc;
3188 long ip_med, ip_hi, ix, iy, jrt, jr, nr, jpt, jp, kshift, nl4;
3189 // long[] ixiy = { 0, 0 };
3190 // coordinates of lowest corner of each face
3191 long[] jrll = { 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4 }; // in units of
3192 // nside
3193 long[] jpll = { 0, 1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7 }; // in units of
3194 // nside/2
3195 String SID = "nest2ring:";
3196 //
3197 if ((nside < 1) || (nside > ns_max)) {
3198 throw new IllegalArgumentException(SID + " nside should be power of 2 >0 and < ns_max");
3199 }
3200 npix = 12 * nside * nside;
3201 if ((ipnest < 0) || (ipnest > npix - 1)) {
3202 throw new IllegalArgumentException(SID + " ipnest out of range [0,npix-1]");
3203 }
3204 if (pix2x[pixmax-1] <= 0)
3205 mk_pix2xy();
3206 ncap = 2 * nside * (nside - 1); // number of points in the North polar
3207 // cap
3208 nl4 = 4 * nside;
3209 // finds the face and the number in the face
3210 npface = nside * nside;
3211
3212 face_num = ipnest / npface; // face number in [0,11]
3213 if (ipnest >= npface) {
3214 ipf = (long) BitManipulation.MODULO(ipnest, npface); // pixel number in the face
3215 } else {
3216 ipf = ipnest;
3217 }
3218
3219 // finds the x,y on the face
3220 // from the pixel number
3221 ip_low = (long) BitManipulation.MODULO(ipf, pixmax); // last 15 bits
3222 if (ip_low < 0)
3223 ip_low = -ip_low;
3224
3225 ip_trunc = ipf / pixmax; // truncate last 15 bits
3226 ip_med = (long) BitManipulation.MODULO(ip_trunc, pixmax); // next 15 bits
3227 if (ip_med < 0)
3228 ip_med = -ip_med;
3229 ip_hi = ip_trunc / pixmax; // high 15 bits
3230
3231 ix = pixmax * pix2x[(int) ip_hi] + xmid * pix2x[(int) ip_med] + pix2x[(int) ip_low];
3232 iy = pixmax * pix2y[(int) ip_hi] + xmid * pix2y[(int) ip_med] + pix2y[(int) ip_low];
3233
3234 // transform this in (horizontal, vertical) coordinates
3235 jrt = ix + iy; // vertical in [0,2*(nside -1)]
3236 jpt = ix - iy; // horizontal in [-nside+1, nside - 1]
3237 // calculate the z coordinate on the sphere
3238 jr = jrll[(int) (face_num + 1)] * nside - jrt - 1; // ring number in [1,4*nside
3239 // -1]
3240 nr = nside; // equatorial region (the most frequent)
3241 n_before = ncap + nl4 * (jr - nside);
3242 kshift = (long) BitManipulation.MODULO(jr - nside, 2);
3243 if (jr < nside) { // north pole region
3244 nr = jr;
3245 n_before = 2 * nr * (nr - 1);
3246 kshift = 0;
3247 } else if (jr > 3 * nside) { // south pole region
3248 nr = nl4 - jr;
3249 n_before = npix - 2 * (nr + 1) * nr;
3250 kshift = 0;
3251 }
3252 // computes the phi coordinate on the sphere in [0,2pi]
3253 jp = (jpll[(int) (face_num + 1)] * nr + jpt + 1 + kshift) / 2; // 'phi' number
3254 // in ring
3255 // [1,4*nr]
3256 if (jp > nl4)
3257 jp -= nl4;
3258 if (jp < 1)
3259 jp += nl4;
3260 res = n_before + jp - 1; // in [0, npix-1]
3261 return res;
3262 }
3263
3264 /**
3265 * fills arrays x2pix and y2pix giving the number of the pixel laying in
3266 * (x,y). x and y are in [1,512] the pixel number is in [0, 512**2 -1]
3267 *
3268 * if i-1 = sum_p=0 b_p*2^p then ix = sum+p=0 b_p*4^p iy = 2*ix ix + iy in
3269 * [0,512**2 -1]
3270 *
3271 */
3272 private static void mk_xy2pix() {
3273 long k, ip, id;
3274
3275 for (int i = 1; i <= xmax; i++) {
3276 long j = i - 1;
3277 k = 0;
3278 ip = 1;
3279 while (j != 0) {
3280 id = (long) BitManipulation.MODULO(j, 2);
3281 j /= 2;
3282 k += ip * id;
3283 ip *= 4;
3284 }
3285 x2pix[i] = k;
3286 y2pix[i] = 2 * k;
3287
3288 }
3289
3290 }
3291
3292 /**
3293 * returns the ring number in {1, 4*nside - 1} calculated from z coordinate
3294 *
3295 * @param nside
3296 * long resolution
3297 * @param z
3298 * double z coordinate
3299 * @return long ring number
3300 */
3301 public long RingNum(long nside, double z) {
3302 long iring = 0;
3303 /* equatorial region */
3304
3305 iring = (long) Math.round(nside * (2.0 - 1.5 * z));
3306 /* north cap */
3307 if (z > twothird) {
3308 iring = (long) Math.round(nside * Math.sqrt(3.0 * (1.0 - z)));
3309 if (iring == 0)
3310 iring = 1;
3311 }
3312 /* south cap */
3313 if (z < -twothird) {
3314 iring = (long) Math.round(nside * Math.sqrt(3.0 * (1.0 + z)));
3315 if (iring == 0)
3316 iring = 1;
3317 iring = 4 * nside - iring;
3318 }
3319 return iring;
3320 }
3321
3322 /**
3323 * calculates vector corresponding to angles theta (co-latitude
3324 * measured from North pole, in [0,pi] radians) phi (longitude measured
3325 * eastward in [0,2pi] radians) North pole is (x,y,z) = (0, 0, 1)
3326 *
3327 * @param theta double
3328 * @param phi double
3329 * @return Vector3d
3330 * @throws IllegalArgumentException
3331 */
3332 public Vector3d Ang2Vec(double theta, double phi) {
3333 double PI = Math.PI;
3334 String SID = "Ang2Vec:";
3335 Vector3d v;
3336 if ((theta < 0.0) || (theta > PI)) {
3337 throw new IllegalArgumentException(SID + " theta out of range [0.,PI]");
3338 }
3339 double stheta = Math.sin(theta);
3340 double x = stheta * Math.cos(phi);
3341 double y = stheta * Math.sin(phi);
3342 double z = Math.cos(theta);
3343 v = new Vector3d(x, y, z);
3344 return v;
3345 }
3346
3347 /**
3348 * converts a Vector3d in a tuple of angles tup[0] = theta
3349 * co-latitude measured from North pole, in [0,PI] radians, tup[1] = phi
3350 * longitude measured eastward, in [0,2PI] radians
3351 *
3352 * @param v
3353 * Vector3d
3354 * @return double[] out_tup out_tup[0] = theta out_tup[1] = phi
3355 */
3356 public double[] Vect2Ang(Vector3d v) {
3357 double[] out_tup = new double[2];
3358 double norm = v.length();
3359 double z = v.z / norm;
3360 double theta = Math.acos(z);
3361 double phi = 0.;
3362 if ((v.x != 0.) || (v.y != 0)) {
3363 phi = Math.atan2(v.y, v.x); // phi in [-pi,pi]
3364 }
3365 if (phi < 0)
3366 phi += 2.0 * Math.PI; // phi in [0, 2pi]
3367 // phi += Math.PI;
3368 out_tup[0] = theta;
3369 out_tup[1] = phi;
3370 return out_tup;
3371 }
3372
3373 /**
3374 * returns nside such that npix = 12*nside^2, nside should be
3375 * power of 2 and smaller than ns_max if not return -1
3376 *
3377 * @param npix
3378 * long the number of pixels in the map
3379 * @return long nside the map resolution parameter
3380 */
3381 public long Npix2Nside(long npix) {
3382 long nside = 0;
3383 long npixmax = 12 *(long) ns_max *(long) ns_max;
3384
3385 String SID = "Npix2Nside:";
3386 nside = (long) Math.rint(Math.sqrt(npix / 12));
3387 if (npix < 12) {
3388 throw new IllegalArgumentException(SID + " npix is too small should be > 12");
3389 }
3390 if (npix > npixmax) {
3391 throw new IllegalArgumentException(SID + " npix is too large > 12 * ns_max^2");
3392 }
3393 double fnpix = 12.0 * nside * nside;
3394 if (Math.abs(fnpix - npix) > 1.0e-2) {
3395 throw new IllegalArgumentException(SID + " npix is not 12*nside*nside");
3396 }
3397 double flog = Math.log((double) nside) / Math.log(2.0);
3398 double ilog = Math.rint(flog);
3399 if (Math.abs(flog - ilog) > 1.0e-6) {
3400 throw new IllegalArgumentException(SID + " nside is not power of 2");
3401 }
3402 return nside;
3403 }
3404
3405 /**
3406 * calculates npix such that npix = 12*nside^2 ,nside should be
3407 * a power of 2, and smaller than ns_max otherwise return -1
3408 *
3409 * @param nside
3410 * long the map resolution
3411 * @return npix long the number of pixels in the map
3412 */
3413 public long Nside2Npix(long nside) {
3414
3415 long[] nsidelist = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
3416 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576,
3417 2097152, 4194304};
3418
3419 long res = 0;
3420 String SID = "Nside2Npix:";
3421 if (Arrays.binarySearch(nsidelist, nside) < 0) {
3422 throw new IllegalArgumentException(SID + " nside should be >0, power of 2, <"+ns_max);
3423 }
3424 res = 12 * nside * nside;
3425 return res;
3426 }
3427
3428 /**
3429 * calculates the surface of spherical triangle defined by
3430 * vertices v1,v2,v3 Algorithm: finds triangle sides and uses l'Huilier
3431 * formula to compute "spherical excess" = surface area of triangle on a
3432 * sphere of radius one see, eg Bronshtein, Semendyayev Eq 2.86 half
3433 * perimeter hp = 0.5*(side1+side2+side3) l'Huilier formula x0 = tan( hp/2.)
3434 * x1 = tan((hp - side1)/2.) x2 = tan((hp - side2)/2.) x3 = tan((hp -
3435 * side3)/2.)
3436 *
3437 * @param v1 Vector3d
3438 * @param v2 Vector3d
3439 * @param v3 Vector3d vertices of the triangle
3440 * @return double the triangle surface
3441 * @throws Exception
3442 *
3443 */
3444 public double SurfaceTriangle(Vector3d v1, Vector3d v2, Vector3d v3)
3445 throws Exception {
3446 double res = 0.;
3447 double side1 = AngDist(v2, v3) / 4.0;
3448 double side2 = AngDist(v3, v1) / 4.0;
3449 double side3 = AngDist(v1, v2) / 4.0;
3450 double x0 = Math.tan(side1 + side2 + side3);
3451 double x1 = Math.tan(side2 + side3 - side1);
3452 double x2 = Math.tan(side1 + side3 - side2);
3453 double x3 = Math.tan(side1 + side2 - side3);
3454 res = 4.0 * Math.atan(Math.sqrt(x0 * x1 * x2 * x3));
3455
3456 return res;
3457 }
3458
3459 /**
3460 * calculates angular distance (in radians) between 2 Vectors
3461 * v1 and v2 In general dist = acos(v1.v2) except if the vectors are almost
3462 * aligned
3463 *
3464 * @param v1 Vector3d
3465 * @param v2 Vector3d
3466 * @return double dist in radians
3467 * @throws Exception
3468 */
3469 public double AngDist(Vector3d v1, Vector3d v2) throws Exception {
3470 double dist = 0.;
3471 double aligned = 0.999;
3472 /* Normalize both vectors */
3473 Vector3d r1 = new Vector3d(v1.x,v1.y,v1.z);
3474 Vector3d r2 = new Vector3d(v2.x,v2.y,v2.z);
3475 r1.normalize();
3476 r2.normalize();
3477 double sprod = r1.dot(r2);
3478 /* This takes care about the bug in vecmath method from java3d project */
3479 if (sprod > aligned) { // almost aligned
3480 r1.sub(r2);
3481 double diff = r1.length();
3482 dist = 2.0 * Math.asin(diff / 2.0);
3483
3484 } else if (sprod < -aligned) {
3485 r1.add(r2);
3486 double diff = r1.length();
3487 dist = Math.PI - 2.0 * Math.asin(diff / 2.0);
3488 } else {
3489 dist = r1.angle(r2);
3490 }
3491 return dist;
3492 }
3493
3494 /**
3495 * calculates a vector production of two vectors.
3496 *
3497 * @param v1
3498 * Vectror containing 3 elements of Number type
3499 * @param v2
3500 * Vector containing 3 elements of Number type
3501 * @return Vector of 3 Objects of Double type
3502 * @throws Exception
3503 */
3504 public Vector VectProd(Vector v1, Vector v2) throws Exception {
3505 Vector res = new Vector();
3506 //
3507 double[] v1_element = new double[3];
3508 double[] v2_element = new double[3];
3509 for (int i = 0; i < 3; i++) {
3510 if (v1.get(i).getClass().isInstance(Number.class)) {
3511 v1_element[i] = ((Number) v1.get(i)).doubleValue();
3512 } else {
3513 throw new Exception();
3514 }
3515 if (v2.get(i).getClass().isInstance(Number.class)) {
3516 v2_element[i] = ((Number) v2.get(i)).doubleValue();
3517 } else {
3518 throw new Exception();
3519 }
3520
3521 }
3522
3523 Double value = new Double(v1_element[1] * v2_element[2] - v1_element[2]
3524 * v2_element[1]);
3525 res.add( value);
3526 value = new Double(v1_element[1] * v2_element[2] - v1_element[2]
3527 * v2_element[1]);
3528 res.add( value);
3529 value = new Double(v1_element[1] * v2_element[2] - v1_element[2]
3530 * v2_element[1]);
3531 res.add( value);
3532 return res;
3533 }
3534
3535 /**
3536 * calculates a dot product (inner product) of two 3D vectors
3537 * the result is double
3538 *
3539 * @param v1
3540 * 3d Vector of Number Objects (Double, long .. )
3541 * @param v2
3542 * 3d Vector
3543 * @return double
3544 * @throws Exception
3545 */
3546 public double dotProduct(Vector3d v1, Vector3d v2) throws Exception {
3547
3548 double prod = v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
3549
3550 return prod;
3551 }
3552
3553 /**
3554 * calculate cross product of two vectors
3555 *
3556 * @param v1
3557 * Vector3d
3558 * @param v2
3559 * Vector3d
3560 * @return Vector3d result of the product
3561 */
3562 public Vector3d crossProduct(Vector3d v1, Vector3d v2) {
3563 Vector3d res = new Vector3d(0., 0., 0.);
3564 double x = v1.y * v2.z - v1.z * v2.y;
3565 double y = v1.z * v2.x - v1.x * v2.z;
3566 double z = v1.x * v2.y - v1.y * v2.x;
3567 res.x = x;
3568 res.y = y;
3569 res.z = z;
3570 return res;
3571 }
3572 /**
3573 * calculates angular resolution of the pixel map
3574 * in arc seconds.
3575 * @param nside
3576 * @return double resolution in arcsec
3577 */
3578 public double PixRes(long nside) {
3579 double res = 0.;
3580 double degrad = Math.toDegrees(1.0);
3581 double skyArea = 4.*PI*degrad*degrad; // 4PI steredian in deg^2
3582 double arcSecArea = skyArea*3600.*3600.; // 4PI steredian in (arcSec^2)
3583 long npixels = 12*nside*nside;
3584 res = arcSecArea/npixels; // area per pixel
3585 res = Math.sqrt(res); // angular size of the pixel arcsec
3586 return res;
3587 }
3588 /**
3589 * calculate requared nside given pixel size in arcsec
3590 * @param pixsize in arcsec
3591 * @return long nside parameter
3592 */
3593 public long GetNSide(double pixsize) {
3594 long[] nsidelist = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
3595 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576 };
3596 long res = 0;
3597 double pixelArea = pixsize*pixsize;
3598 double degrad = Math.toDegrees(1.);
3599 double skyArea = 4.*PI*degrad*degrad*3600.*3600.;
3600 long npixels = (long) (skyArea/pixelArea);
3601 long nsidesq = npixels/12;
3602 long nside_req = (long) Math.sqrt(nsidesq);
3603 long mindiff = ns_max;
3604 int indmin = 0;
3605 for (int i=0; i<nsidelist.length; i++) {
3606 if (Math.abs(nside_req - nsidelist[i]) <= mindiff) {
3607 mindiff = Math.abs(nside_req - nsidelist[i]);
3608 res = nsidelist[i];
3609 indmin = i;
3610 }
3611 if ((nside_req > res) && (nside_req < ns_max)) res = nsidelist[indmin+1];
3612 if (nside_req > ns_max ) {
3613 System.out.println("nside cannot be bigger than "+ns_max);
3614 return ns_max;
3615 }
3616
3617 }
3618 return res;
3619 }
3620 /**
3621 * returns polar coordinates in radians given ra, dec in degrees
3622 * @param radec double array containing ra,dec in degrees
3623 * @return res double array containing theta and phi in radians
3624 * res[0] = theta res[1] = phi
3625 */
3626 public double[] RaDecToPolar(double[] radec) {
3627 double[] res = {0.0,0.0};
3628
3629 double ra = radec[0];
3630 double dec = radec[1];
3631 double theta = PI/2. - Math.toRadians(dec);
3632 double phi = Math.toRadians(ra);
3633 res[0] = theta;
3634 res[1] = phi;
3635
3636 return res;
3637 }
3638 /**
3639 * returns ra, dec in degrees given polar coordinates in radians
3640 * @param polar double array polar[0] = phi in radians
3641 * polar[1] = theta in radians
3642 * @return double array radec radec[0] = ra in degrees
3643 * radec[1] = dec in degrees
3644 */
3645 public double[] PolarToRaDec(double[] polar) {
3646 double[] radec = {0.0,0.0};
3647 double phi = polar[1];
3648 double theta = polar[0];
3649 double dec = Math.toDegrees(PI/2. - theta);
3650 double ra = Math.toDegrees(phi);
3651 radec[0] = ra;
3652 radec[1] = dec;
3653
3654 return radec;
3655 }
3656
3657 /**
3658 * returns polar coordinates of a point on unit sphere given Cartesian coordinates
3659 * @param x - Cartesian coordinate x of a point on unit sphere
3660 * @param y - y coordinate
3661 * @param z - z coordinate
3662 * @return double [] theta,phi
3663 */
3664 public double[] xyzToPolar(double x, double y, double z) {
3665 double[] res;
3666 Vector3d vv = new Vector3d(x,y,z);
3667 res = Vect2Ang(vv);
3668 return res;
3669 }
3670
3671 /**
3672 * Returns singleton instance.
3673 */
3674 public static PixTools getInstance() {
3675 return pixTools;
3676 }
3677 }