Codebase list eag-healpix / 4261a72
Import Upstream version 2012.01.11 Ole Streicher 7 years ago
17 changed file(s) with 8823 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 GNU GENERAL PUBLIC LICENSE
1 Version 2, June 1991
2
3 Copyright (C) 1989, 1991 Free Software Foundation, Inc.
4 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
5 Everyone is permitted to copy and distribute verbatim copies
6 of this license document, but changing it is not allowed.
7
8 Preamble
9
10 The licenses for most software are designed to take away your
11 freedom to share and change it. By contrast, the GNU General Public
12 License is intended to guarantee your freedom to share and change free
13 software--to make sure the software is free for all its users. This
14 General Public License applies to most of the Free Software
15 Foundation's software and to any other program whose authors commit to
16 using it. (Some other Free Software Foundation software is covered by
17 the GNU Library General Public License instead.) You can apply it to
18 your programs, too.
19
20 When we speak of free software, we are referring to freedom, not
21 price. Our General Public Licenses are designed to make sure that you
22 have the freedom to distribute copies of free software (and charge for
23 this service if you wish), that you receive source code or can get it
24 if you want it, that you can change the software or use pieces of it
25 in new free programs; and that you know you can do these things.
26
27 To protect your rights, we need to make restrictions that forbid
28 anyone to deny you these rights or to ask you to surrender the rights.
29 These restrictions translate to certain responsibilities for you if you
30 distribute copies of the software, or if you modify it.
31
32 For example, if you distribute copies of such a program, whether
33 gratis or for a fee, you must give the recipients all the rights that
34 you have. You must make sure that they, too, receive or can get the
35 source code. And you must show them these terms so they know their
36 rights.
37
38 We protect your rights with two steps: (1) copyright the software, and
39 (2) offer you this license which gives you legal permission to copy,
40 distribute and/or modify the software.
41
42 Also, for each author's protection and ours, we want to make certain
43 that everyone understands that there is no warranty for this free
44 software. If the software is modified by someone else and passed on, we
45 want its recipients to know that what they have is not the original, so
46 that any problems introduced by others will not reflect on the original
47 authors' reputations.
48
49 Finally, any free program is threatened constantly by software
50 patents. We wish to avoid the danger that redistributors of a free
51 program will individually obtain patent licenses, in effect making the
52 program proprietary. To prevent this, we have made it clear that any
53 patent must be licensed for everyone's free use or not licensed at all.
54
55 The precise terms and conditions for copying, distribution and
56 modification follow.
57
58
59
60 GNU GENERAL PUBLIC LICENSE
61 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
62
63 0. This License applies to any program or other work which contains
64 a notice placed by the copyright holder saying it may be distributed
65 under the terms of this General Public License. The "Program", below,
66 refers to any such program or work, and a "work based on the Program"
67 means either the Program or any derivative work under copyright law:
68 that is to say, a work containing the Program or a portion of it,
69 either verbatim or with modifications and/or translated into another
70 language. (Hereinafter, translation is included without limitation in
71 the term "modification".) Each licensee is addressed as "you".
72
73 Activities other than copying, distribution and modification are not
74 covered by this License; they are outside its scope. The act of
75 running the Program is not restricted, and the output from the Program
76 is covered only if its contents constitute a work based on the
77 Program (independent of having been made by running the Program).
78 Whether that is true depends on what the Program does.
79
80 1. You may copy and distribute verbatim copies of the Program's
81 source code as you receive it, in any medium, provided that you
82 conspicuously and appropriately publish on each copy an appropriate
83 copyright notice and disclaimer of warranty; keep intact all the
84 notices that refer to this License and to the absence of any warranty;
85 and give any other recipients of the Program a copy of this License
86 along with the Program.
87
88 You may charge a fee for the physical act of transferring a copy, and
89 you may at your option offer warranty protection in exchange for a fee.
90
91 2. You may modify your copy or copies of the Program or any portion
92 of it, thus forming a work based on the Program, and copy and
93 distribute such modifications or work under the terms of Section 1
94 above, provided that you also meet all of these conditions:
95
96 a) You must cause the modified files to carry prominent notices
97 stating that you changed the files and the date of any change.
98
99 b) You must cause any work that you distribute or publish, that in
100 whole or in part contains or is derived from the Program or any
101 part thereof, to be licensed as a whole at no charge to all third
102 parties under the terms of this License.
103
104 c) If the modified program normally reads commands interactively
105 when run, you must cause it, when started running for such
106 interactive use in the most ordinary way, to print or display an
107 announcement including an appropriate copyright notice and a
108 notice that there is no warranty (or else, saying that you provide
109 a warranty) and that users may redistribute the program under
110 these conditions, and telling the user how to view a copy of this
111 License. (Exception: if the Program itself is interactive but
112 does not normally print such an announcement, your work based on
113 the Program is not required to print an announcement.)
114
115
116
117 These requirements apply to the modified work as a whole. If
118 identifiable sections of that work are not derived from the Program,
119 and can be reasonably considered independent and separate works in
120 themselves, then this License, and its terms, do not apply to those
121 sections when you distribute them as separate works. But when you
122 distribute the same sections as part of a whole which is a work based
123 on the Program, the distribution of the whole must be on the terms of
124 this License, whose permissions for other licensees extend to the
125 entire whole, and thus to each and every part regardless of who wrote it.
126
127 Thus, it is not the intent of this section to claim rights or contest
128 your rights to work written entirely by you; rather, the intent is to
129 exercise the right to control the distribution of derivative or
130 collective works based on the Program.
131
132 In addition, mere aggregation of another work not based on the Program
133 with the Program (or with a work based on the Program) on a volume of
134 a storage or distribution medium does not bring the other work under
135 the scope of this License.
136
137 3. You may copy and distribute the Program (or a work based on it,
138 under Section 2) in object code or executable form under the terms of
139 Sections 1 and 2 above provided that you also do one of the following:
140
141 a) Accompany it with the complete corresponding machine-readable
142 source code, which must be distributed under the terms of Sections
143 1 and 2 above on a medium customarily used for software interchange; or,
144
145 b) Accompany it with a written offer, valid for at least three
146 years, to give any third party, for a charge no more than your
147 cost of physically performing source distribution, a complete
148 machine-readable copy of the corresponding source code, to be
149 distributed under the terms of Sections 1 and 2 above on a medium
150 customarily used for software interchange; or,
151
152 c) Accompany it with the information you received as to the offer
153 to distribute corresponding source code. (This alternative is
154 allowed only for noncommercial distribution and only if you
155 received the program in object code or executable form with such
156 an offer, in accord with Subsection b above.)
157
158 The source code for a work means the preferred form of the work for
159 making modifications to it. For an executable work, complete source
160 code means all the source code for all modules it contains, plus any
161 associated interface definition files, plus the scripts used to
162 control compilation and installation of the executable. However, as a
163 special exception, the source code distributed need not include
164 anything that is normally distributed (in either source or binary
165 form) with the major components (compiler, kernel, and so on) of the
166 operating system on which the executable runs, unless that component
167 itself accompanies the executable.
168
169 If distribution of executable or object code is made by offering
170 access to copy from a designated place, then offering equivalent
171 access to copy the source code from the same place counts as
172 distribution of the source code, even though third parties are not
173 compelled to copy the source along with the object code.
174
175
176
177 4. You may not copy, modify, sublicense, or distribute the Program
178 except as expressly provided under this License. Any attempt
179 otherwise to copy, modify, sublicense or distribute the Program is
180 void, and will automatically terminate your rights under this License.
181 However, parties who have received copies, or rights, from you under
182 this License will not have their licenses terminated so long as such
183 parties remain in full compliance.
184
185 5. You are not required to accept this License, since you have not
186 signed it. However, nothing else grants you permission to modify or
187 distribute the Program or its derivative works. These actions are
188 prohibited by law if you do not accept this License. Therefore, by
189 modifying or distributing the Program (or any work based on the
190 Program), you indicate your acceptance of this License to do so, and
191 all its terms and conditions for copying, distributing or modifying
192 the Program or works based on it.
193
194 6. Each time you redistribute the Program (or any work based on the
195 Program), the recipient automatically receives a license from the
196 original licensor to copy, distribute or modify the Program subject to
197 these terms and conditions. You may not impose any further
198 restrictions on the recipients' exercise of the rights granted herein.
199 You are not responsible for enforcing compliance by third parties to
200 this License.
201
202 7. If, as a consequence of a court judgment or allegation of patent
203 infringement or for any other reason (not limited to patent issues),
204 conditions are imposed on you (whether by court order, agreement or
205 otherwise) that contradict the conditions of this License, they do not
206 excuse you from the conditions of this License. If you cannot
207 distribute so as to satisfy simultaneously your obligations under this
208 License and any other pertinent obligations, then as a consequence you
209 may not distribute the Program at all. For example, if a patent
210 license would not permit royalty-free redistribution of the Program by
211 all those who receive copies directly or indirectly through you, then
212 the only way you could satisfy both it and this License would be to
213 refrain entirely from distribution of the Program.
214
215 If any portion of this section is held invalid or unenforceable under
216 any particular circumstance, the balance of the section is intended to
217 apply and the section as a whole is intended to apply in other
218 circumstances.
219
220 It is not the purpose of this section to induce you to infringe any
221 patents or other property right claims or to contest validity of any
222 such claims; this section has the sole purpose of protecting the
223 integrity of the free software distribution system, which is
224 implemented by public license practices. Many people have made
225 generous contributions to the wide range of software distributed
226 through that system in reliance on consistent application of that
227 system; it is up to the author/donor to decide if he or she is willing
228 to distribute software through any other system and a licensee cannot
229 impose that choice.
230
231 This section is intended to make thoroughly clear what is believed to
232 be a consequence of the rest of this License.
233
234
235
236 8. If the distribution and/or use of the Program is restricted in
237 certain countries either by patents or by copyrighted interfaces, the
238 original copyright holder who places the Program under this License
239 may add an explicit geographical distribution limitation excluding
240 those countries, so that distribution is permitted only in or among
241 countries not thus excluded. In such case, this License incorporates
242 the limitation as if written in the body of this License.
243
244 9. The Free Software Foundation may publish revised and/or new versions
245 of the General Public License from time to time. Such new versions will
246 be similar in spirit to the present version, but may differ in detail to
247 address new problems or concerns.
248
249 Each version is given a distinguishing version number. If the Program
250 specifies a version number of this License which applies to it and "any
251 later version", you have the option of following the terms and conditions
252 either of that version or of any later version published by the Free
253 Software Foundation. If the Program does not specify a version number of
254 this License, you may choose any version ever published by the Free Software
255 Foundation.
256
257 10. If you wish to incorporate parts of the Program into other free
258 programs whose distribution conditions are different, write to the author
259 to ask for permission. For software which is copyrighted by the Free
260 Software Foundation, write to the Free Software Foundation; we sometimes
261 make exceptions for this. Our decision will be guided by the two goals
262 of preserving the free status of all derivatives of our free software and
263 of promoting the sharing and reuse of software generally.
264
265 NO WARRANTY
266
267 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
268 FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
269 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
270 PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
271 OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
272 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
273 TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
274 PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
275 REPAIR OR CORRECTION.
276
277 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
278 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
279 REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
280 INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
281 OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
282 TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
283 YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
284 PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
285 POSSIBILITY OF SUCH DAMAGES.
286
287 END OF TERMS AND CONDITIONS
288
289
290
291 How to Apply These Terms to Your New Programs
292
293 If you develop a new program, and you want it to be of the greatest
294 possible use to the public, the best way to achieve this is to make it
295 free software which everyone can redistribute and change under these terms.
296
297 To do so, attach the following notices to the program. It is safest
298 to attach them to the start of each source file to most effectively
299 convey the exclusion of warranty; and each file should have at least
300 the "copyright" line and a pointer to where the full notice is found.
301
302 <one line to give the program's name and a brief idea of what it does.>
303 Copyright (C) <year> <name of author>
304
305 This program is free software; you can redistribute it and/or modify
306 it under the terms of the GNU General Public License as published by
307 the Free Software Foundation; either version 2 of the License, or
308 (at your option) any later version.
309
310 This program is distributed in the hope that it will be useful,
311 but WITHOUT ANY WARRANTY; without even the implied warranty of
312 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
313 GNU General Public License for more details.
314
315 You should have received a copy of the GNU General Public License
316 along with this program; if not, write to the Free Software
317 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
318
319
320 Also add information on how to contact you by electronic and paper mail.
321
322 If the program is interactive, make it output a short notice like this
323 when it starts in an interactive mode:
324
325 Gnomovision version 69, Copyright (C) year name of author
326 Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
327 This is free software, and you are welcome to redistribute it
328 under certain conditions; type `show c' for details.
329
330 The hypothetical commands `show w' and `show c' should show the appropriate
331 parts of the General Public License. Of course, the commands you use may
332 be called something other than `show w' and `show c'; they could even be
333 mouse-clicks or menu items--whatever suits your program.
334
335 You should also get your employer (if you work as a programmer) or your
336 school, if any, to sign a "copyright disclaimer" for the program, if
337 necessary. Here is a sample; alter the names:
338
339 Yoyodyne, Inc., hereby disclaims all copyright interest in the program
340 `Gnomovision' (which makes passes at compilers) written by James Hacker.
341
342 <signature of Ty Coon>, 1 April 1989
343 Ty Coon, President of Vice
344
345 This General Public License does not permit incorporating your program into
346 proprietary programs. If your program is a subroutine library, you may
347 consider it more useful to permit linking proprietary applications with the
348 library. If this is what you want to do, use the GNU Library General
349 Public License instead of this License.
350
0
1
2 provides methods to create and manipulate HEALPix maps.
3
4 AUTHORS:
5 Nikolay Kuropatkin kuropat@fnal.gov
6
7
8 For detailed description see URL
9 http://home.fnal.gov/~kuropat/HEALPIX/PixTools.html
10 The Java doc of API is in the "doc" subdirectory.
Binary diff not shown
0 /*
1 * Created on Mar 15, 2005
2 *
3 */
4 package gov.fnal.eag.healpix;
5
6 /**
7 *
8 *
9 * bit manipulation class derived from Healpix
10 * fortran90 program.
11 *
12 * @author N Kuropatkin
13 */
14 public final class BitManipulation {
15 /**
16 * magic1 odd bits set constant
17 */
18 private static long magic1 = 89478485; // 101010101010101010101010101 LSB
19 /**
20 * magic2 - even bits set constant
21 */
22 private static long magic2 = 178956970; //1010101010101010101010101010 MSB
23
24
25 /**
26 * swaps low and high bits in the word i
27 * @param i integer input word
28 * @return int a word with odd and even bits interchanged
29 */
30 public static long swapLSBMSB(long i) {
31 long res = 0;
32 long lsb = (i & magic1);
33 long msb = (i & magic2);
34 res = msb/2 + lsb*2;
35 return res;
36 }
37 /**
38 * returns NOT i with even and odd bit positions interchanged
39 * @param i int input word
40 * @return int NOT (i with LSBMSB)
41 */
42 public static long invswapLSBMSB(long i) {
43 long res = 0;
44 long lsb = (i & magic1);
45 long msb = (i & magic2);
46 res = ~(msb/2+lsb*2);
47 return res;
48 }
49 /**
50 * returns i with even bits inverted
51 * @param i int input word
52 * @return int word with modified bits
53 */
54 public static long invLSB(long i) {
55 long res = 0;
56 res = (i ^ magic1); // returns exclusive OR with odd bits
57 return res;
58 }
59 /**
60 * returns i with odd bits inverted
61 * @param i int input word
62 * @return int word with modified bits
63 */
64 public static long invMSB(long i) {
65 long res = 0;
66 res = (i ^ magic2);
67 return res;
68 }
69 /**
70 * simulates behaviour of fortran90 MODULO function
71 * @param a double
72 * @param b double
73 * @return double MODULO
74 */
75 public static double MODULO(double a, double b) {
76 double res = 0.;
77 double c = 0;
78 long k = 0;
79 if (a>0.) {
80 if (b>0.) {
81 k = (long) (a/b);
82 res = a - k*b;
83 return res;
84 }
85 if (b<0.) {
86 k = (long)Math.rint(a/b);
87 res = a - k*b;
88 return res;
89 }
90 }
91 if (a<=0.) {
92 if (b<=0.) {
93 k = (long)(a/b);
94 res = a - k*b;
95 return res;
96 }
97 if (b>0.) {
98 k = (long)Math.rint(a/b);
99 res = a - k*b;
100 return res;
101 }
102 }
103 return res;
104 }
105
106 /**
107 * the main program for simple test
108 * @param pars
109 */
110 public static void main(String[] pars) {
111 // BitManipulation bm = new BitManipulation();
112 int l=0;
113 int k = 1;
114 long lsb;
115 long msb;
116 long mag1=0;;
117 long mag2=0;;
118 for (int i = 0; i<32; i++) {
119 mag1 += Math.pow(2.,l);
120 mag2 += Math.pow(2.,k);
121 System.out.println("l="+l+" mag1="+ mag1+" mag2="+mag2);
122 l +=2;
123 k +=2;
124 }
125 l = 0;
126 k = 1;
127 for (int i=0; i < 21; i++) {
128 lsb = (long)Math.pow(2.,l);
129 msb = (long)Math.pow(2.,k);
130 System.out.println(" l="+l+" 2^l="+lsb+" l="+k+" 2^l="+msb);
131 l +=2;
132 k +=2;
133
134 }
135 // BitManipulation bm = new BitManipulation();
136 double a= 6004765143422312.;
137 double b = BitManipulation.MODULO(a, 16.);
138 System.out.println("a="+a+" MODULO(1024)="+b);
139 }
140 }
0 /*
1 * Created on Apr 14, 2005
2 *
3 */
4 package gov.fnal.eag.healpix;
5
6 /**
7 * handles exceptions from this package
8 *
9 *@author N. Kuropatkin
10 */
11 public class HealpixException extends Exception {
12 /**
13 *
14 */
15 private static final long serialVersionUID = 1L;
16
17 /**
18 * message - String to be printed in case of exception
19 * @param message
20 */
21 public HealpixException(String message) {
22 super(message);
23 }
24 }
0 package gov.fnal.eag.healpix;
1
2 import javax.vecmath.Vector3d;
3
4 /**
5 * Aggregates a vector and double[3][4] array which are the results of a
6 * pixel center calculation.
7 */
8 class PixInfo {
9 final Vector3d pixVect;
10 final double[][] pixVertex;
11
12 PixInfo(Vector3d pixVect, double[][] pixVertex) {
13 this.pixVect = pixVect;
14 this.pixVertex = pixVertex;
15 }
16 }
0 package gov.fnal.eag.healpix;
1
2 import java.util.ArrayList;
3 import java.util.Arrays;
4 import java.util.Vector;
5 import java.io.FileNotFoundException;
6 import java.io.PrintStream;
7 import java.lang.Number;
8 import javax.vecmath.Vector3d;
9
10 /**
11 *
12 * contains methods translated from HEALPix Fortran90
13 * with increased map resolution in comparison to original Fortran code.
14 *
15 * @author N Kuropatkin
16 *
17 * Created on Mar 10, 2005
18 * Modified on December 18 2007
19 * Corrected arithmetic and mistyping April 20 2008
20 *
21 * @author Mark Taylor made modifications to make the class thread safe 11-Jan-2012
22 *
23 * <p>
24 * All methods are thread safe. This class can be used as a singleton,
25 * the singleton instance being available from the {@link #getInstance} method.
26 * For compatibility with previous versions however it is possible to
27 * construct new instances using the default constructor.
28 *</p>
29 *
30 */
31 public class PixTools {
32
33 private static final double twothird = 2. / 3.;
34
35 private static final double PI = Math.PI;
36
37 private static final double TWOPI = 2. * PI;
38
39
40
41 private static final double HALFPI = PI / 2.0;
42
43 private static final int ns_max = 1048576; // 2^20
44
45 //
46 private static final int xmax = 4096;
47 //
48 private static final int pixmax = 262144;
49
50 //
51 private static final int xmid = 512;
52
53 private static final long[] x2pix = new long[xmax+1];
54
55 private static final long[] y2pix = new long[xmax+1];
56
57 private static final long[] pix2x = new long[pixmax+1];
58
59 private static final long[] pix2y = new long[pixmax+1];
60
61
62
63 static {
64 mk_xy2pix();
65 }
66
67 /** Singleton instance. */
68 private static final PixTools pixTools = new PixTools();
69
70 /**
71 * default constructor
72 *
73 *
74 */
75 public PixTools() {
76 }
77
78 /**
79 * finds pixels having a colatitude (measured from North pole) :
80 * theta1 < colatitude < theta2 with 0 <= theta1 < theta2 <= Pi
81 * if theta2 < theta1
82 * then pixels with 0 <= colatitude < theta2 or theta1 < colatitude < Pi are
83 * returned
84 *
85 * @param nside
86 * long the map resolution parameter
87 * @param theta1
88 * lower edge of the colatitude
89 * @param theta2
90 * upper edge of the colatitude
91 * @param nest
92 * long if = 1 result is in NESTED scheme
93 * @return ArrayList of pixel numbers (long)
94 * @throws Exception
95 * @throws IllegalArgumentException
96 */
97 public ArrayList query_strip(long nside, double theta1, double theta2,
98 long nest) throws Exception {
99 ArrayList res = new ArrayList();
100 ArrayList listir = new ArrayList();
101 long npix, nstrip;
102 long iz, irmin, irmax;
103 int is;
104 double phi0, dphi;
105 double[] colrange = new double[4];
106 boolean nest_flag = false;
107 String SID = " QUERY_STRIP";
108 /* ---------------------------------------- */
109 npix = Nside2Npix(nside);
110 if (nest == 1)
111 nest_flag = true;
112 if (npix < 0) {
113 throw new IllegalArgumentException(SID + " Nside should be power of 2");
114 }
115 if ((theta1 < 0.0 || theta1 > PI) || (theta2 < 0.0 || theta2 > PI)) {
116 throw new IllegalArgumentException(SID + " Illegal value of theta1, theta2");
117 }
118 if (theta1 <= theta2) {
119 nstrip = 1;
120 colrange[0] = theta1;
121 colrange[1] = theta2;
122 } else {
123 nstrip = 2;
124 colrange[0] = 0.0;
125 colrange[1] = theta2;
126 colrange[2] = theta1;
127 colrange[3] = PI;
128 }
129 /* loops on strips */
130 for (is = 0; is < nstrip; is++) {
131 irmin = RingNum(nside, Math.cos(colrange[2 * is]));
132 irmax = RingNum(nside, Math.cos(colrange[2 * is + 1]));
133 /* loop on ring number */
134 for (iz = irmin; iz <= irmax; iz++) {
135 phi0 = 0.;
136 dphi = PI;
137 listir = InRing(nside, iz, phi0, dphi, nest_flag);
138 res.addAll(listir);
139 }
140 }
141 return res;
142 }
143
144 /**
145 * finds pixels that lay within a CONVEX polygon defined by its vertex on
146 * sphere
147 *
148 * @param nside
149 * the map resolution
150 * @param vlist
151 * ArrayList of vectors defining the polygon vertices
152 * @param nest
153 * if set to 1 use NESTED scheme
154 * @param inclusive
155 * if set 1 returns all pixels crossed by polygon boundaries
156 * @return ArrayList of pixels
157 *
158 * algorithm: the polygon is divided into triangles vertex 0 belongs to all
159 * triangles
160 * @throws Exception
161 * @throws IllegalArgumentException
162 */
163 public ArrayList query_polygon(long nside, ArrayList vlist, long nest,
164 long inclusive) throws Exception {
165 ArrayList res = new ArrayList();
166 int nv = vlist.size();
167 Vector3d vp0, vp1, vp2;
168 Vector3d vo;
169 ArrayList vvlist = new ArrayList();
170 // double surface, fsky;
171 double hand;
172 double[] ss = new double[nv];
173 // int n_in_trg, ilist, ntl;
174 long npix;
175 int ix = 0;
176
177 int n_remain, np, nm, nlow;
178 String SID = "QUERY_POLYGON";
179
180 // System.out.println("Start polygon");
181 for (int k = 0; k < nv; k++)
182 ss[k] = 0.;
183 /* -------------------------------------- */
184 n_remain = nv;
185 if (n_remain < 3) {
186 throw new IllegalArgumentException(SID + " Number of vertices should be >= 3");
187 }
188 /*---------------------------------------------------------------- */
189 /* Check that the poligon is convex or has only one concave vertex */
190 /*---------------------------------------------------------------- */
191 int i0 = 0;
192 int i2 = 0;
193 if (n_remain > 3) { // a triangle is always convex
194 for (int i1 = 1; i1 <= n_remain - 1; i1++) { // in [0,n_remain-1]
195 i0 = (int) BitManipulation.MODULO(i1 - 1, n_remain);
196 i2 = (int) BitManipulation.MODULO(i1 + 1, n_remain);
197 vp0 = (Vector3d) vlist.get(i0); // select vertices by 3
198 // neighbour
199 vp1 = (Vector3d) vlist.get(i1);
200 vp2 = (Vector3d) vlist.get(i2);
201 // computes handedness (v0 x v2) . v1 for each vertex v1
202 vo = new Vector3d(crossProduct(vp0, vp2));
203 hand = dotProduct(vo, vp1);
204 if (hand >= 0.) {
205 ss[i1] = 1.0;
206 } else {
207 ss[i1] = -1.0;
208 }
209
210 }
211 np = 0; // number of vert. with positive handedness
212 for (int i = 0; i < nv; i++) {
213 if (ss[i] > 0.)
214 np++;
215 }
216 nm = n_remain - np;
217
218 nlow = Math.min(np, nm);
219
220 if (nlow != 0) {
221 if (nlow == 1) { // only one concave vertex
222 if (np == 1) { // ix index of the vertex in the list
223 for (int k = 0; k < nv - 1; k++) {
224 if (Math.abs(ss[k] - 1.0) <= 1.e-12) {
225 ix = k;
226 break;
227 }
228 }
229 } else {
230 for (int k = 0; k < nv - 1; k++) {
231 if (Math.abs(ss[k] + 1.0) <= 1.e-12) {
232 ix = k;
233 break;
234 }
235 }
236 }
237
238 // rotate pixel list to put that vertex in #0
239 int n_rot = vlist.size() - ix;
240 int ilast = vlist.size() - 1;
241 for (int k = 0; k < n_rot; k++) {
242 Vector3d temp = new Vector3d((Vector3d) vlist
243 .get(ilast));
244 vlist.remove(ilast);
245 vlist.add(0, temp);
246 }
247 }
248 if (nlow > 1) { // more than 1concave vertex
249 System.out
250 .println(" The polygon has more than one concave vertex");
251 System.out.println(" The result is unpredictable");
252 }
253 }
254 }
255 /* fill the polygon, one triangle at a time */
256 npix = (long) Nside2Npix(nside);
257 while (n_remain >= 3) {
258 vp0 = (Vector3d) vlist.get(0);
259 vp1 = (Vector3d) vlist.get(n_remain - 2);
260 vp2 = (Vector3d) vlist.get(n_remain - 1);
261
262 /* find pixels within the triangle */
263 ArrayList templist = new ArrayList();
264 templist = query_triangle(nside, vp0, vp1, vp2, nest, inclusive);
265
266 vvlist.addAll(templist);
267 n_remain--;
268 }
269 /* make final pixel list */
270 npix = vvlist.size();
271 long[] pixels = new long[(int)npix];
272 for (int i = 0; i < npix; i++) {
273 pixels[i] = ((Long) vvlist.get(i)).longValue();
274 }
275 Arrays.sort(pixels);
276 int k = 0;
277 res.add(k, new Long(pixels[0]));
278 for (int i = 1; i < pixels.length; i++) {
279 if (pixels[i] > pixels[i - 1]) {
280 k++;
281 res.add(k, new Long(pixels[i]));
282 }
283 }
284
285 return res;
286 }
287
288 /**
289 * generates a list of pixels that lay inside a triangle defined by
290 * the three vertex vectors
291 *
292 * @param nside
293 * long map resolution parameter
294 * @param v1
295 * Vector3d defines one vertex of the triangle
296 * @param v2
297 * Vector3d another vertex
298 * @param v3
299 * Vector3d yet another one
300 * @param nest
301 * long 0 (default) RING numbering scheme, if set to 1 the NESTED
302 * scheme will be used.
303 * @param inclusive
304 * long 0 (default) only pixels whose centers are inside the
305 * triangle will be listed, if set to 1 all pixels overlaping the
306 * triangle will be listed
307 * @return ArrayList with pixel numbers
308 * @throws Exception
309 * @throws IllegalArgumentException
310 */
311 public ArrayList query_triangle(long nside, Vector3d v1, Vector3d v2,
312 Vector3d v3, long nest, long inclusive) throws Exception {
313 ArrayList res;
314 res = new ArrayList();
315 ArrayList listir;
316 long npix, iz, irmin, irmax, n12, n123a, n123b, ndom = 0;
317 boolean test1, test2, test3;
318 boolean test1a, test1b, test2a, test2b, test3a, test3b;
319 double dth1, dth2, determ, sdet;
320 double zmax, zmin, z1max, z1min, z2max, z2min, z3max, z3min;
321 double z, tgth, st, offset, sin_off;
322 double phi_pos, phi_neg;
323 Vector3d[] vv = new Vector3d[3];
324 Vector3d[] vo = new Vector3d[3];
325 double[] sprod = new double[3];
326 double[] sto = new double[3];
327 double[] phi0i = new double[3];
328 double[] tgthi = new double[3];
329 double[] dc = new double[3];
330 double[][] dom = new double[3][2];
331 double[] dom12 = new double[4];
332 double[] dom123a = new double[4];
333 double[] dom123b = new double[4];
334 double[] alldom = new double[6];
335 double a_i, b_i, phi0, dphiring;
336 long idom;
337 // long nir, ip, status;
338 boolean do_inclusive = false;
339 boolean do_nest = false;
340 String SID = "QUERY_TRIANGLE";
341 long nsidesq = nside * nside;
342 /* */
343
344 // System.out.println("in query_triangle");
345 npix = Nside2Npix(nside);
346 if (npix < 0) {
347 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
348 }
349 if (inclusive == 1)
350 do_inclusive = true;
351 if (nest == 1)
352 do_nest = true;
353 vv[0] = new Vector3d(v1);
354 vv[0].normalize();
355 vv[1] = new Vector3d(v2);
356 vv[1].normalize();
357 vv[2] = new Vector3d(v3);
358 vv[2].normalize();
359
360 /* */
361 dth1 = 1.0 / (3.0 * nsidesq);
362 dth2 = 2.0 / (3.0 * nside);
363 /*
364 * determ = (v1 X v2) . v3 determines the left ( <0) or right (>0)
365 * handedness of the triangle
366 */
367 Vector3d vt = new Vector3d(0., 0., 0.);
368 vt = crossProduct(vv[0], vv[1]);
369 determ = dotProduct(vt, vv[2]);
370
371 if (Math.abs(determ) < 1.0e-20) {
372 throw new HealpixException(
373 SID
374 + ": the triangle is degenerated - query cannot be performed");
375 }
376 if (determ >= 0.) { // The sign of determinant
377 sdet = 1.0;
378 } else {
379 sdet = -1.0;
380 }
381
382 sprod[0] = dotProduct(vv[1], vv[2]);
383 sprod[1] = dotProduct(vv[2], vv[0]);
384 sprod[2] = dotProduct(vv[0], vv[1]);
385 /* vector orthogonal to the great circle containing the vertex doublet */
386
387 vo[0] = crossProduct(vv[1], vv[2]);
388 vo[1] = crossProduct(vv[2], vv[0]);
389 vo[2] = crossProduct(vv[0], vv[1]);
390 vo[0].normalize();
391 vo[1].normalize();
392 vo[2].normalize();
393
394 /* test presence of poles in the triangle */
395 zmax = -1.0;
396 zmin = 1.0;
397 test1 = (vo[0].z * sdet >= 0.0); // north pole in hemisphere defined by
398 // 2-3
399 test2 = (vo[1].z * sdet >= 0.0); // north pole in the hemisphere defined
400 // by 1-2
401 test3 = (vo[2].z * sdet >= 0.0); // north pole in hemisphere defined by
402 // 1-3
403 if (test1 && test2 && test3)
404 zmax = 1.0; // north pole in the triangle
405 if ((!test1) && (!test2) && (!test3))
406 zmin = -1.0; // south pole in the triangle
407 /* look for northenest and southernest points in the triangle */
408 test1a = ((vv[2].z - sprod[0] * vv[1].z) >= 0.0); // segment 2-3
409 test1b = ((vv[1].z - sprod[0] * vv[2].z) >= 0.0);
410 test2a = ((vv[2].z - sprod[1] * vv[0].z) >= 0.0); // segment 1-3
411 test2b = ((vv[0].z - sprod[1] * vv[2].z) >= 0.0);
412 test3a = ((vv[1].z - sprod[2] * vv[0].z) >= 0.0); // segment 1-2
413 test3b = ((vv[0].z - sprod[2] * vv[1].z) >= 0.0);
414
415 /* sin of theta for orthogonal vector */
416 for (int i = 0; i < 3; i++) {
417 sto[i] = Math.sqrt((1.0 - vo[i].z) * (1.0 + vo[i].z));
418 }
419 /*
420 * for each segment ( side of the triangle ) the extrema are either -
421 * -the 2 vertices - one of the vertices and a point within the segment
422 */
423 // segment 2-3
424 z1max = vv[1].z;
425 z1min = vv[2].z;
426 // if (test1a == test1b) {
427 // zz = sto[0];
428 // if (vv[1].z + vv[2].z >= 0.0) {
429 // z1max = zz;
430 // } else {
431 // z1min = -zz;
432 // }
433 // }
434 // segment 1-3
435 z2max = vv[2].z;
436 z2min = vv[0].z;
437 // if (test2a == test2b) {
438 // zz = sto[1];
439 // if (vv[0].z + vv[2].z >= 0.0) {
440 // z2max = zz;
441 // } else {
442 // z2min = -zz;
443 // }
444 // }
445 // segment 1-2
446 z3max = vv[0].z;
447 z3min = vv[1].z;
448 // if (test3a == test3b) {
449 // zz = sto[2];
450 // if (vv[0].z + vv[1].z >= 0.0) {
451 // z3max = zz;
452 // } else {
453 // z3min = -zz;
454 // }
455 // }
456
457 zmax = Math.max(Math.max(z1max, z2max), Math.max(z3max, zmax));
458 zmin = Math.min(Math.min(z1min, z2min), Math.min(z3min, zmin));
459 /*
460 * if we are inclusive, move upper point up, and lower point down, by a
461 * half pixel size
462 */
463 offset = 0.0;
464 sin_off = 0.0;
465 if (do_inclusive) {
466 offset = PI / (4.0 * nside); // half pixel size
467 sin_off = Math.sin(offset);
468 zmax = Math.min(1.0, Math.cos(Math.acos(zmax) - offset));
469 zmin = Math.max(-1.0, Math.cos(Math.acos(zmin) + offset));
470 }
471
472 irmin = RingNum(nside, zmax);
473 irmax = RingNum(nside, zmin);
474
475 // System.out.println("irmin = " + irmin + " irmax =" + irmax);
476
477 /* loop on the rings */
478 for (int i = 0; i < 3; i++) {
479 tgthi[i] = -1.0e30 * vo[i].z;
480 phi0i[i] = 0.0;
481 }
482 for (int j = 0; j < 3; j++) {
483 if (sto[j] > 1.0e-10) {
484 tgthi[j] = -vo[j].z / sto[j]; // - cotan(theta_orth)
485
486 phi0i[j] = Math.atan2(vo[j].y, vo[j].x); // Should make it 0-2pi
487 // ?
488 /* Bring the phi0i to the [0,2pi] domain if need */
489
490 if (phi0i[j] < 0.) {
491 phi0i[j] = BitManipulation.MODULO(
492 (Math.atan2(vo[j].y, vo[j].x) + TWOPI), TWOPI); // [0-2pi]
493 }
494
495 }
496 }
497 /*
498 * the triangle boundaries are geodesics: intersection of the sphere
499 * with plans going through (0,0,0) if we are inclusive, the boundaries
500 * are the intersection of the sphere with plains pushed outward by
501 * sin(offset)
502 */
503 // double temp = 0.;
504 boolean found = false;
505 for (iz = irmin; iz <= irmax; iz++) {
506 found = false;
507 if (iz <= nside - 1) { // North polar cap
508 z = 1.0 - iz * iz * dth1;
509 } else if (iz <= 3 * nside) { // tropical band + equator
510 z = (2.0 * nside - iz) * dth2;
511 } else {
512 z = -1.0 + (4.0 * nside - iz) * (4.0 * nside - iz) * dth1;
513 }
514
515 /* computes the 3 intervals described by the 3 great circles */
516 st = Math.sqrt((1.0 - z) * (1.0 + z));
517 tgth = z / st; // cotan(theta_ring)
518 for (int j = 0; j < 3; j++) {
519 dc[j] = tgthi[j] * tgth - sdet * sin_off
520 / ((sto[j] + 1.0e-30) * st);
521
522 }
523 for (int k = 0; k < 3; k++) {
524 if (dc[k] * sdet <= -1.0) { // the whole iso-latitude ring is on
525 // right side of the great circle
526 dom[k][0] = 0.0;
527 dom[k][1] = TWOPI;
528 } else if (dc[k] * sdet >= 1.0) { // all on the wrong side
529 dom[k][0] = -1.000001 * (k + 1);
530 dom[k][1] = -1.0 * (k + 1);
531 } else { // some is good some is bad
532 phi_neg = phi0i[k] - (Math.acos(dc[k]) * sdet);
533 phi_pos = phi0i[k] + (Math.acos(dc[k]) * sdet);
534 //
535 if (phi_pos < 0.)
536 phi_pos += TWOPI;
537 if (phi_neg < 0.)
538 phi_neg += TWOPI;
539
540 //
541
542 dom[k][0] = BitManipulation.MODULO(phi_neg, TWOPI);
543 dom[k][1] = BitManipulation.MODULO(phi_pos, TWOPI);
544
545 }
546 //
547
548 }
549 /* identify the intersections (0,1,2 or 3) of the 3 intervals */
550
551 dom12 = intrs_intrv(dom[0], dom[1]);
552 n12 = dom12.length / 2;
553 if (n12 != 0) {
554 if (n12 == 1) {
555 dom123a = intrs_intrv(dom[2], dom12);
556 n123a = dom123a.length / 2;
557
558 if (n123a == 0)
559 found = true;
560 if (!found) {
561 for (int l = 0; l < dom123a.length; l++) {
562 alldom[l] = dom123a[l];
563 }
564
565 ndom = n123a; // 1 or 2
566 }
567 }
568 if (!found) {
569 if (n12 == 2) {
570 double[] tmp = { dom12[0], dom12[1] };
571 dom123a = intrs_intrv(dom[2], tmp);
572 double[] tmp1 = { dom12[2], dom12[3] };
573 dom123b = intrs_intrv(dom[2], tmp1);
574 n123a = dom123a.length / 2;
575 n123b = dom123b.length / 2;
576 ndom = n123a + n123b; // 0, 1, 2 or 3
577
578 if (ndom == 0)
579 found = true;
580 if (!found) {
581 if (n123a != 0) {
582 for (int l = 0; l < 2 * n123a; l++) {
583 alldom[l] = dom123a[l];
584 }
585 }
586 if (n123b != 0) {
587 for (int l = 0; l < 2 * n123b; l++) {
588 alldom[(int) (l + 2 * n123a)] = dom123b[l];
589 }
590 }
591 if (ndom > 3) {
592 throw new HealpixException(SID
593 + ": too many intervals found");
594 }
595 }
596 }
597 }
598 if (!found) {
599 for (idom = 0; idom < ndom; idom++) {
600 a_i = alldom[(int) (2 * idom)];
601 b_i = alldom[(int) (2 * idom + 1)];
602 phi0 = (a_i + b_i) / 2.0;
603 dphiring = Math.abs(b_i - a_i) / 2.0;
604
605 if (dphiring < 0.0) {
606 phi0 += PI;
607 dphiring += PI;
608 }
609
610 /* finds pixels in the triangle on that ring */
611 listir = InRing(nside, iz, phi0, dphiring, do_nest);
612 res.addAll(listir);
613
614 }
615 }
616 }
617
618 }
619 return res;
620 }
621
622 /**
623 * computes the intersection di of 2 intervals d1 (= [a1,b1])
624 * and d2 (= [a2,b2]) on the periodic domain (=[A,B] where A and B
625 * arbitrary) ni is the resulting number of intervals (0,1, or 2) if a1 <b1
626 * then d1 = {x |a1 <= x <= b1} if a1>b1 then d1 = {x | a1 <=x <= B U A <=x
627 * <=b1}
628 *
629 * @param d1 double[] first interval
630 * @param d2 double[] second interval
631 * @return double[] one or two intervals intersections
632 */
633 public double[] intrs_intrv(double[] d1, double[] d2) {
634 double[] res;
635 double epsilon = 1.0e-10;
636 // double temp = 0.;
637 // int ni;
638 double[] dk;
639 double[] di = { 0. };
640 int ik = 0;
641 boolean tr12, tr21, tr34, tr43, tr13, tr31, tr24, tr42, tr14, tr32;
642 /* */
643
644 tr12 = (d1[0] < d1[1] + epsilon);
645 tr21 = !tr12; // d1[0] >= d1[1]
646 tr34 = (d2[0] < d2[1] + epsilon);
647 tr43 = !tr34; // d2[0]>d2[1]
648 tr13 = (d1[0] < d2[0] + epsilon); // d2[0] can be in interval
649 tr31 = !tr13; // d1[0] in longerval
650 tr24 = (d1[1] < d2[1] + epsilon); // d1[1] upper limit
651 tr42 = !tr24; // d2[1] upper limit
652 tr14 = (d1[0] < d2[1] + epsilon); // d1[0] in interval
653 tr32 = (d2[0] < d1[1] + epsilon); // d2[0] in interval
654
655 ik = 0;
656 dk = new double[] { -1.0e9, -1.0e9, -1.0e9, -1.0e9 };
657 /* d1[0] lower limit case 1 */
658 if ((tr34 && tr31 && tr14) || (tr43 && (tr31 || tr14))) {
659 ik++; // ik = 1;
660 dk[ik - 1] = d1[0]; // a1
661
662 }
663 /* d2[0] lower limit case 1 */
664 if ((tr12 && tr13 && tr32) || (tr21 && (tr13 || tr32))) {
665 ik++; // ik = 1
666 dk[ik - 1] = d2[0]; // a2
667
668 }
669 /* d1[1] upper limit case 2 */
670 if ((tr34 && tr32 && tr24) || (tr43 && (tr32 || tr24))) {
671 ik++; // ik = 2
672 dk[ik - 1] = d1[1]; // b1
673
674 }
675 /* d2[1] upper limit case 2 */
676 if ((tr12 && tr14 && tr42) || (tr21 && (tr14 || tr42))) {
677 ik++; // ik = 2
678 dk[ik - 1] = d2[1]; // b2
679
680 }
681 di = new double[1];
682 di[0] = 0.;
683 switch (ik) {
684
685 case 2:
686 di = new double[2];
687
688 di[0] = dk[0] - epsilon;
689 di[1] = dk[1] + epsilon;
690 break;
691 case 4:
692
693 di = new double[4];
694 di[0] = dk[0] - epsilon;
695 di[1] = dk[3] + epsilon;
696 di[2] = dk[1] - epsilon;
697 di[3] = dk[2] + epsilon;
698 break;
699 }
700 res = di;
701
702 return res;
703 }
704
705 /**
706 * an obsolete method. Use query_disc instead.
707 *
708 * @param nside
709 * @param vector0
710 * @param radius
711 * @return - ArrayList of long
712 */
713 public ArrayList getDisc_ring(long nside, Vector3d vector0, double radius) {
714 ArrayList res;
715 int nest = 0;
716 int inclusive = 0;
717 res = query_disc(nside, vector0, radius, nest, inclusive);
718 return res;
719 }
720
721 /**
722 * generates in the RING or NESTED scheme all pixels that lays within an
723 * angular distance Radius of the center.
724 *
725 * @param nside
726 * long map resolution
727 * @param vector
728 * Vector3d pointing to the disc center
729 * @param radius
730 * double angular radius of the disc (in RADIAN )
731 * @param nest
732 * int 0 (default) if output is in RING scheme, if set to 1
733 * output is in NESTED
734 * @param inclusive
735 * int 0 (default) only pixels whose centers lay in the disc
736 * are listed, if set to 1, all pixels overlapping the disc
737 * are listed. In the inclusive mode the radius is increased by half the pixel size.
738 * In this case most probably all neighbor pixels will be listed even with very small
739 * radius.
740 * In case of exclusive search and very small radius when the disc lays completely
741 * inside a pixel the pixel number is returned using vector2pix method.
742 * @return ArrayList of pixel numbers
743 *
744 * calls: RingNum(nside, ir) InRing(nside, iz, phi0, dphi,nest) vector2pix(nside,ipix)
745 */
746 public ArrayList query_disc(long nside, Vector3d vector, double radius,
747 int nest, int inclusive) {
748 ArrayList res = new ArrayList();
749 long irmin, irmax, iz, ip, nir, npix, ilist;
750
751 // double norm_vect0;
752 double x0, y0, z0, radius_eff;
753 double a, b, c, cosang;
754 double dth1, dth2;
755 double phi0, cosphi0, cosdphi, dphi;
756 double rlat0, rlat1, rlat2, zmin, zmax, z;
757 // long status, list_size, nlost;
758 boolean do_inclusive = false;
759 boolean do_nest = false;
760 String SID = "QUERY_DISC";
761 /* */
762 long nsidesq = nside * nside;
763 npix = 12 * nsidesq;
764 double pixres = PixRes(nside); // in arc seconds
765 // double halfPix = 0.5*Math.toRadians(pixres/3600.); // in radians
766 double halfPix = PI / (4.0 * nside);
767 // System.out.println("0.5 pixel size ="+halfPix);
768
769
770 if (radius < 0.0 || radius > PI) {
771 throw new IllegalArgumentException(SID
772 + ": angular radius is in RADIAN and should be in [0,pi]");
773 }
774
775 if (inclusive == 1)
776 do_inclusive = true;
777 if (nest == 1)
778 do_nest = true;
779
780 dth1 = 1.0 / (3.0 * nside * nside);
781 dth2 = 2.0 / (3.0 * nside);
782
783 radius_eff = radius;
784 // if (radius_eff <= halfPix) radius_eff = halfPix;
785
786 /* disc center */
787 vector.normalize();
788 x0 = vector.x; // norm_vect0;
789 y0 = vector.y; // norm_vect0;
790 z0 = vector.z; // norm_vect0;
791 // System.out.println("x0="+x0+" y0="+y0+" z0="+z0);
792 //
793 // NK make radius increase a function of altitude
794 //
795 if (do_inclusive) { radius_eff += (halfPix + Math.abs(z0)*halfPix);} // increase radius for incluzive search
796 // System.out.println("effective radius="+radius_eff);
797 // if (do_inclusive) { radius_eff += halfPix;}
798 cosang = Math.cos(radius_eff);
799 phi0 = 0.0;
800 dphi = 0.0;
801 if (x0 != 0. || y0 != 0.)
802 phi0 = BitManipulation.MODULO(Math.atan2(y0, x0) + TWOPI, TWOPI); // in [0, 2pi]
803 cosphi0 = Math.cos(phi0);
804 // System.out.println("phi0="+phi0+" cosphi0="+cosphi0);
805 a = x0 * x0 + y0 * y0;
806 /* coordinate z of highest and lowest points in the disc */
807 rlat0 = Math.asin(z0); // latitude in RAD of the center
808 rlat1 = rlat0 + radius_eff;
809 rlat2 = rlat0 - radius_eff;
810 // System.out.println("rlat0="+rlat0+" rlat1="+rlat1+" rlat2="+rlat2);
811 //
812 if (rlat1 >= HALFPI) {
813 zmax = 1.0;
814 } else {
815 zmax = Math.sin(rlat1);
816 }
817 irmin = RingNum(nside, zmax);
818 irmin = Math.max(1, irmin - 1); // start from a higher point to be safe
819 if (rlat2 <= -HALFPI) {
820 zmin = -1.0;
821 } else {
822 zmin = Math.sin(rlat2);
823 }
824 irmax = RingNum(nside, zmin);
825 irmax = Math.min(4 * nside - 1, irmax + 1); // go down to a lower point
826 // System.out.println(" irmax="+irmax+" irmin="+irmin);
827 ilist = -1;
828
829
830
831 //
832 /* loop on ring number */
833 for (iz = irmin; iz <= irmax; iz++) {
834 if (iz <= nside - 1) { // north polar cap
835 z = 1.0 - iz * iz * dth1;
836 } else if (iz <= 3 * nside) { // tropical band + equator
837 z = (2.0 * nside - iz) * dth2;
838 } else {
839 z = -1.0 + (4.0 * nside - iz) * (4.0 * nside - iz) * dth1;
840 }
841 /* find phi range in the disc for each z */
842 b = cosang - z * z0;
843 c = 1.0 - z * z;
844 cosdphi = b / Math.sqrt(a * c);
845 long done = 0;
846
847 if (Math.abs(x0) <= 1.0e-12 && Math.abs(y0) <= 1.0e-12) {
848 cosdphi = -1.0;
849 dphi = PI;
850 done = 1;
851 }
852 if (done == 0) {
853 if (Math.abs(cosdphi) <= 1.0) {
854 dphi = Math.acos(cosdphi); // in [0,pi]
855 } else {
856 if (cosphi0 >= cosdphi) {
857 dphi = PI; // all the pixels at this elevation are in
858 // the disc
859 } else {
860 done = 2; // out of the disc
861 }
862 }
863
864 }
865 if (done < 2) { // pixels in disc
866 /* find pixels in the disc */
867 // System.out.println("iz="+iz+" phi="+phi0+" dphi="+dphi);
868 ArrayList listir = InRing(nside, iz, phi0, dphi, do_nest);
869 // System.out.println("ir"+iz);
870
871 res.addAll(listir);
872 }
873
874 }
875 //
876 // if radius less than pixel size check that the pixel number is in the list
877 // and add one if it is missing.
878 //
879
880 long pixel = 0;
881 if ( pixres > Math.toDegrees(radius)/3600. ) {
882 // System.out.println("case with r < pix. size");
883 if (do_nest) {
884 pixel = vect2pix_nest(nside,vector);
885 } else {
886 pixel = vect2pix_ring(nside,vector);
887 }
888 if (!res.contains(new Long(pixel)))
889 res.add(new Long(pixel));
890 }
891 return res;
892 }
893
894 /**
895 * renders theta and phi coordinates of the nominal pixel center for the
896 * pixel number ipix (RING scheme) given the map resolution parameter nside
897 *
898 * @param nside
899 * long map resolution
900 * @param ipix
901 * long pixel number
902 * @return double[] theta,phi
903 */
904 public double[] pix2ang_ring(long nside, long ipix) {
905 double[] res = { 0., 0. };
906 long nl2, nl4, npix, ncap, iring, iphi, ip, ipix1;
907 double fodd, hip, fihip, theta, phi;
908 String SID = "pix2ang_ring:";
909 /* */
910 if (nside < 1 || nside > ns_max) {
911 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
912 }
913 long nsidesq = nside * nside;
914 npix = 12 * nsidesq; // total number of pixels
915 if (ipix < 0 || ipix > npix - 1) {
916 throw new IllegalArgumentException(SID + " ipix out of range calculated from nside");
917 }
918 ipix1 = ipix + 1; // in [1, npix]
919 nl2 = 2 * nside;
920 nl4 = 4 * nside;
921 ncap = 2 * nside * (nside - 1); // points in each polar cap, =0 for
922 // nside =1
923
924 if (ipix1 <= ncap) { // North polar cap
925 hip = ipix1 / 2.0;
926 fihip = (long) hip; // get integer part of hip
927 iring = (long) (Math.sqrt(hip - Math.sqrt(fihip))) + 1; // counted from north
928 // pole
929 iphi = ipix1 - 2 * iring * (iring - 1);
930 theta = Math.acos(1.0 - iring * iring / (3.0 * nsidesq));
931 phi = ((double)iphi - 0.5) * PI / (2.0 * iring);
932
933
934 } else if (ipix1 <= nl2 * (5 * nside + 1)) { // equatorial region
935 ip = ipix1 - ncap - 1;
936 iring = (long) (ip / nl4) + nside; // counted from North pole
937 iphi = (long) BitManipulation.MODULO(ip, nl4) + 1;
938 fodd = 0.5 * (1. + BitManipulation.MODULO(iring + nside, 2)); // 1 if iring+nside
939 // is odd, 1/2 otherwise
940 theta = Math.acos((nl2 - iring) / (1.5 * nside));
941 phi = ((double)iphi - fodd) * PI / (2.0 * nside);
942
943 } else { // South pole cap
944 ip = npix - ipix1 + 1;
945 hip = ip / 2.0;
946 fihip = (long) hip;
947 iring = (long) (Math.sqrt(hip - Math.sqrt(fihip))) + 1; // counted from South
948 // pole
949 iphi = 4 * iring + 1 - (ip - 2 * iring * (iring - 1));
950 theta = Math.acos(-1.0 + iring * iring / (3.0 * nsidesq));
951 phi = ((double)iphi - 0.5) * PI / (2.0 * iring);
952
953 }
954 res[0] = theta;
955 res[1] = phi;
956 return res;
957 }
958
959 /**
960 * returns the vector pointing in the center of the pixel ipix. The vector
961 * is calculated by makePix2Vect_ring method
962 *
963 * @param nside map resolution
964 * @param ipix pixel number
965 * @return Vector3d
966 */
967 public Vector3d pix2vect_ring(long nside, long ipix) {
968
969 PixInfo pixInfo = makePix2Vect_ring(nside, ipix);
970 Vector3d res = new Vector3d(pixInfo.pixVect);
971 return res;
972 }
973
974 /**
975 * returns double [][] with coordinates of the pixel corners. The array is
976 * calculated by makePix2Vect_ring method
977 *
978 * @param nside map resolution
979 * @param ipix pixel number
980 * @return double[][] list of vertex coordinates
981 */
982 public double[][] pix2vertex_ring(long nside, long ipix) {
983 double[][] res;
984 PixInfo pixinfo = makePix2Vect_ring(nside, ipix);
985 res = pixinfo.pixVertex;
986 return res;
987 }
988
989 /**
990 * renders vector (x,y,z) coordinates of the nominal pixel center for pixel
991 * ipix (RING scheme) given the map resolution parameter nside. It also
992 * calculates (x,y,z) positions of the four vertices in order N,W,S,E.
993 * These results are returned in a PixInfo object.
994 * Those can be used using pix2Vect_ring and pix2vert_ring methods
995 *
996 * @param nside
997 * long map resolution
998 * @param ipix
999 * pixel number
1000 * @return result object
1001 */
1002 private PixInfo makePix2Vect_ring(long nside, long ipix) {
1003 long nl2;
1004 Vector3d pixVect = new Vector3d(0., 0., 0.);
1005 double[][] pixVertex = new double[3][4];
1006
1007 long nl4;
1008 long iring, iphi, ip, ipix1;
1009 long npix,ncap;
1010 double phi_nv, phi_wv, phi_sv, phi_ev;
1011 double z_nv, z_sv, sth_nv, sth_sv, hdelta_phi;
1012 double fact1, fact2, fodd, hip, fihip, z, sth, phi;
1013 long iphi_mod;
1014 long iphi_rat;
1015 // boolean do_vertex = true;
1016 long nsidesq = nside * nside;
1017 String SID = " Pix2Vect_ring:";
1018 /* */
1019 if (nside < 1 || nside > ns_max) {
1020 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
1021 }
1022
1023 npix = 12 * nsidesq;
1024 if (ipix < 0 || ipix > npix - 1) {
1025 throw new IllegalArgumentException(SID + " ipix out of range calculated from nside");
1026 }
1027
1028 ipix1 = ipix + 1; // in [1, npix]
1029 nl2 = 2 * nside;
1030 nl4 = 4 * nside;
1031 ncap = 2 * nside * (nside - 1); // points in each polar cap
1032 fact1 = 1.5 * nside;
1033 fact2 = 3.0 * nsidesq;
1034 phi_nv = 0.0;
1035 phi_sv = 0.0;
1036 if (ipix1 <= ncap) { // north polar cap
1037 hip = ipix1 / 2.0;
1038 fihip = (long) hip;
1039 iring = (long) (Math.sqrt(hip - Math.sqrt(fihip))) + 1; // counted from north
1040 // pole
1041 iphi = ipix1 - 2 * iring * (iring - 1);
1042 z = 1.0 - iring * iring / fact2;
1043 phi = (iphi - 0.5) * PI / (2.0 * iring);
1044
1045 hdelta_phi = PI / (4.0 * iring); // half pixel width
1046 z_nv = 1.0 - (iring - 1) * (iring - 1) / fact2;
1047 z_sv = 1.0 - (iring + 1) * (iring + 1) / fact2;
1048 iphi_mod = (long) BitManipulation.MODULO(iphi - 1, iring); // in [0,1,...,iring-1]
1049 iphi_rat = (iphi - 1) / iring; // in [0,1,2,3]
1050 if (iring > 1)
1051 phi_nv = HALFPI * (iphi_rat + iphi_mod / (iring - 1.0));
1052 phi_sv = HALFPI * (iphi_rat + (iphi_mod + 1.0) / (iring + 1.0));
1053 } else if (ipix1 <= nl2 * (5 * nside + 1)) { // equatorial region
1054 ip = (ipix1 - ncap - 1);
1055 iring = (long) (ip / nl4) + nside; // counted from North pole
1056 iphi = (long) BitManipulation.MODULO(ip, nl4) + 1;
1057 fodd = 0.5 * (1. + BitManipulation.MODULO(iring + nside, 2)); // 1 if iring+nside
1058 // is odd or 1/2
1059 z = (nl2 - iring) / fact1;
1060 phi = (iphi - fodd) * PI / (2.0 * nside);
1061 hdelta_phi = PI / (4.0 * nside); // half pixel width
1062 phi_nv = phi;
1063 phi_sv = phi;
1064 z_nv = (nl2 - iring + 1) / fact1;
1065 z_sv = (nl2 - iring - 1) / fact1;
1066 if (iring == nside) { // nothern transition
1067 z_nv = 1.0 - (nside - 1) * (nside - 1) / fact2;
1068 iphi_mod = (long) BitManipulation.MODULO(iphi - 1, nside); // in [0,1,...,nside-1]
1069 iphi_rat = (iphi - 1) / nside; // in [0,1,2,3]
1070 if (nside > 1)
1071 phi_nv = HALFPI * (iphi_rat + iphi_mod / (nside - 1.));
1072 } else if (iring == 3 * nside) { // southern transition
1073 z_sv = -1.0 + (nside - 1) * (nside - 1) / fact2;
1074 iphi_mod = (long) BitManipulation.MODULO(iphi - 1, nside); // in [0,1,... iring-1]
1075 iphi_rat = (iphi - 1) / nside; // in [0,1,2,3]
1076 if (nside > 1)
1077 phi_sv = HALFPI * (iphi_rat + iphi_mod / (nside - 1.0));
1078 }
1079
1080 } else { // South polar cap
1081 ip = npix - ipix1 + 1;
1082 hip = ip / 2.0;
1083 fihip = (long) hip;
1084 iring = (long) (Math.sqrt(hip - Math.sqrt(fihip))) + 1; // counted from South
1085 // pole
1086 iphi = 4 * iring + 1 - (ip - 2 * iring * (iring - 1));
1087 z = -1.0 + iring * iring / fact2;
1088 phi = (iphi - 0.5) * PI / (2.0 * iring);
1089 hdelta_phi = PI / (4.0 * iring); // half pixel width
1090 z_nv = -1.0 + (iring + 1) * (iring + 1) / fact2;
1091 z_sv = -1.0 + (iring - 1) * (iring - 1) / fact2;
1092 iphi_mod = (long) BitManipulation.MODULO(iphi - 1, iring); // in [0,1,...,iring-1]
1093 iphi_rat = (iphi - 1) / iring; // in [0,1,2,3]
1094 phi_nv = HALFPI * (iphi_rat + (iphi_mod + 1) / (iring + 1.0));
1095 if (iring > 1)
1096 phi_sv = HALFPI * (iphi_rat + iphi_mod / (iring - 1.0));
1097
1098 }
1099 /* pixel center */
1100 sth = Math.sqrt((1.0 - z) * (1.0 + z));
1101 pixVect.x = sth * Math.cos(phi);
1102 pixVect.y = sth * Math.sin(phi);
1103 pixVect.z = z;
1104 pixVect = new Vector3d(sth * Math.cos(phi), sth * Math.sin(phi), z);
1105 /* west vertex */
1106 phi_wv = phi - hdelta_phi;
1107 pixVertex[0][1] = sth * Math.cos(phi_wv);
1108 pixVertex[1][1] = sth * Math.sin(phi_wv);
1109 pixVertex[2][1] = z;
1110 /* east vertex */
1111 phi_ev = phi + hdelta_phi;
1112 pixVertex[0][3] = sth * Math.cos(phi_ev);
1113 pixVertex[1][3] = sth * Math.sin(phi_ev);
1114 pixVertex[2][3] = z;
1115 /* north vertex */
1116 sth_nv = Math.sqrt((1.0 - z_nv) * (1.0 + z_nv));
1117 pixVertex[0][0] = sth_nv * Math.cos(phi_nv);
1118 pixVertex[1][0] = sth_nv * Math.sin(phi_nv);
1119 pixVertex[2][0] = z_nv;
1120 /* south vertex */
1121 sth_sv = Math.sqrt((1.0 - z_sv) * (1.0 + z_sv));
1122 pixVertex[0][2] = sth_sv * Math.cos(phi_sv);
1123 pixVertex[1][2] = sth_sv * Math.sin(phi_sv);
1124 pixVertex[2][2] = z_sv;
1125 return new PixInfo(pixVect, pixVertex);
1126 }
1127
1128 /**
1129 * renders the pixel number ipix (RING scheme) for a pixel which contains a
1130 * point with coordinates theta and phi, given the map resolution parameter
1131 * nside.
1132 *
1133 * @param nside
1134 * long map resolution parameter
1135 * @param theta
1136 * double theta
1137 * @param phi -
1138 * double phi
1139 * @return long ipix
1140 */
1141 public long ang2pix_ring(long nside, double theta, double phi) {
1142 long nl4;
1143 long jp, jm, kshift;
1144 long ip;
1145 long ir;
1146 double z, za, tt, tp, tmp;
1147 long pix = 0;
1148 long ipix1;
1149 long nl2, ncap, npix;
1150 String SID = "ang2pix_ring:";
1151 /* */
1152 if (nside < 1 || nside > ns_max) {
1153 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
1154 }
1155 if (theta < 0.0 || theta > PI) {
1156 throw new IllegalArgumentException(SID + " Theta out of range [0,pi]");
1157 }
1158
1159 z = Math.cos(theta);
1160 za = Math.abs(z);
1161
1162
1163
1164 if (phi >= TWOPI) phi = phi -TWOPI ;
1165
1166 if (phi < 0.)
1167 phi =phi + TWOPI; // phi in [0, 2pi]
1168 tt = phi / HALFPI; // in [0,4]
1169 // tt = BitManipulation.MODULO(phi, TWOPI) / HALFPI; // in [0,4]
1170 nl2 = 2 * nside;
1171 nl4 = 4 * nside;
1172 ncap = nl2 * (nside - 1); // number of pixels in the north polar cap
1173 npix = 12 * nside * nside;
1174 if (za < twothird) { // equatorial region
1175 jp = (long) (nside * (0.5 + tt - 0.75 * z)); // index of ascending
1176 // edge line
1177 jm = (long) (nside * (0.5 + tt + 0.75 * z)); // index of descending
1178 // edge line
1179
1180 ir = nside + 1 + jp - jm; // in [1,2n+1]
1181 kshift = 0;
1182 if ((long) BitManipulation.MODULO(ir, 2) == 0)
1183 kshift = 1; // 1 if ir even, 0 otherwise
1184 ip = (long) ((jp + jm - nside + kshift + 1) / 2) + 1; // in [1,4n]
1185 if (ip > nl4) ip = ip - nl4;
1186 ipix1 = ncap + nl4 * (ir - 1) + ip;
1187
1188 } else { // North and South polar caps
1189 tp = tt - (long) tt;
1190 tmp = Math.sqrt(3.0 * (1.0 - za));
1191 jp = (long) (nside * tp * tmp); // increasing edge line index
1192 jm = (long) (nside * (1.0 - tp) * tmp); // decreasing edge index
1193
1194 ir = jp + jm + 1; // ring number counted from closest pole
1195 ip = (long) (tt * ir) + 1; // in [1,4*ir]
1196 if (ip > 4 * ir)
1197 ip = ip - 4 * ir;
1198
1199 ipix1 = 2 * ir * (ir - 1) + ip;
1200 if (z <= 0.0)
1201 ipix1 = npix - 2 * ir * (ir + 1) + ip;
1202
1203 }
1204 pix = ipix1 - 1; // in [0, npix-1]
1205
1206
1207 return pix;
1208 }
1209
1210 /**
1211 * renders the pixel number ipix (RING scheme) for a pixel which contains a
1212 * point on a sphere at coordinate vector (x,y,z), given the map resolution
1213 * parameter nside
1214 *
1215 * @param nside
1216 * long map resolution
1217 * @param vector
1218 * Vector3d of the point coordinates
1219 * @return long pixel number
1220 * @throws IllegalArgumentException
1221 */
1222 public long vect2pix_ring(long nside, Vector3d vector) {
1223 long res = 0;
1224 long nl2, nl4, ncap, npix, jp, jm, ipix1;
1225 double z, za, tt, tp, tmp, dnorm, phi;
1226 long ir, ip, kshift;
1227 String SID = " vect2pix_ring:";
1228 /* */
1229 if (nside < 1 || nside > ns_max) {
1230 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
1231 }
1232 dnorm = vector.length();
1233 z = vector.z / dnorm;
1234 phi = 0.;
1235 if (vector.x != 0. || vector.y != 0.)
1236 phi = Math.atan2(vector.y, vector.x); // phi in [-pi,pi]
1237 za = Math.abs(z);
1238 if (phi < 0.)
1239 phi += TWOPI; // phi in [0, 2pi]
1240 tt = phi / HALFPI; // in [0,4]
1241
1242 nl2 = 2 * nside;
1243 nl4 = 4 * nside;
1244 ncap = nl2 * (nside - 1); // number of pixels in the north polar cap
1245 npix = 12 * nside * nside;
1246 if (za < twothird) { // equatorial region
1247 jp = (long) (nside * (0.5 + tt - 0.75 * z)); // index of ascending
1248 // edge line
1249 jm = (long) (nside * (0.5 + tt + 0.75 * z)); // index of descending
1250 // edge line
1251
1252 ir = nside + 1 + jp - jm; // in [1,2n+1]
1253 kshift = 0;
1254 if ((long) BitManipulation.MODULO(ir, 2) == 0)
1255 kshift = 1; // 1 if ir even, 0 otherwise
1256 ip = (long) ((jp + jm - nside + kshift + 1) / 2) + 1; // in [1,4n]
1257 ipix1 = ncap + nl4 * (ir - 1) + ip;
1258 } else { // North and South polar caps
1259 tp = tt - (long) tt;
1260 tmp = Math.sqrt(3.0 * (1.0 - za));
1261 jp = (long) (nside * tp * tmp); // increasing edge line index
1262 jm = (long) (nside * (1.0 - tp) * tmp); // decreasing edge index
1263
1264 ir = jp + jm + 1; // ring number counted from closest pole
1265 ip = (long) (tt * ir) + 1; // in [1,4*ir]
1266 if (ip > 4 * ir)
1267 ip = ip - 4 * ir;
1268
1269 ipix1 = 2 * ir * (ir - 1) + ip;
1270 if (z <= 0.0)
1271 ipix1 = npix - 2 * ir * (ir + 1) + ip;
1272 }
1273 res = ipix1 - 1; // in [0, npix-1]
1274 return res;
1275 }
1276
1277 /**
1278 *
1279 * Renders theta and phi coordinates of the normal pixel center for the
1280 * pixel number ipix (NESTED scheme) given the map resolution parameter
1281 * nside.
1282 *
1283 * @param nside
1284 * map resolution parameter - long
1285 * @param ipix
1286 * long pixel number
1287 * @return double[] (theta, phi)
1288 * @throws IllegalArgumentException
1289 */
1290 public double[] pix2ang_nest(long nside, long ipix) {
1291 double[] res = new double[2];
1292 double theta = 0.;
1293 double phi = 0.;
1294 long npix, npface, ipf, ip_low, ip_trunc, ip_med, ip_hi;
1295 long jrt, jr, nr, jpt, jp, kshift, nl4, ix, iy, face_num;
1296 double z, fn, fact1, fact2;
1297 String SID = "pix2ang_nest:";
1298 // coordinate of the lowest corner of each face
1299 long[] jrll = { 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4 }; // in units of
1300 // nside
1301 long[] jpll = { 0, 1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7 }; // in units of
1302 // nside/2
1303 /* */
1304 if (nside < 1 || nside > ns_max) {
1305 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
1306 }
1307 long nsidesq = nside * nside;
1308 npix = 12 * nsidesq;
1309 if (ipix < 0 || ipix > npix - 1) {
1310 throw new IllegalArgumentException(SID + " ipix out of range calculated from nside");
1311 }
1312 if (pix2x[xmax-1] <= 0)
1313 mk_pix2xy();
1314 fn = 1.*nside;
1315 fact1 = 1.0 / (3.0 * fn * fn);
1316 fact2 = 2.0 / (3.0 * fn);
1317 nl4 = 4 * nside;
1318 /* findes the face, and the number in the face */
1319 npface = nside * nside;
1320 face_num = ipix / npface; // face number [0,11]
1321 ipf = (long) BitManipulation.MODULO(ipix, npface); // pixel in the face [0, npface-1]
1322 /*
1323 * finds x,y on the face (starting from the lowest corner) from pixel
1324 * number
1325 */
1326 ip_low = (long) BitManipulation.MODULO(ipf, pixmax); // content of the last 18 bits
1327 ip_trunc = ipf / pixmax; // trancation of the last 18 bits
1328 ip_med = (long) BitManipulation.MODULO(ip_trunc, pixmax); // content of the next 18 bits
1329 ip_hi = ip_trunc / pixmax; // content of the high wait 18 bits
1330
1331 ix = pixmax * pix2x[(int)ip_hi] + xmid * pix2x[(int)ip_med] + pix2x[(int) ip_low];
1332 iy = pixmax * pix2y[(int)ip_hi] + xmid * pix2y[(int)ip_med] + pix2y[(int)ip_low];
1333 /* transform these in (horizontal, vertical) coordinates */
1334 jrt = ix + iy; // [0,2*(nside-1)]
1335 jpt = ix - iy; // [ -nside+1, nside -1]
1336 /* computes the z coordinate on the sphere */
1337 jr = jrll[(int) (face_num + 1)] * nside - jrt - 1; // ring number in [1,
1338 // 4*nside-1]
1339
1340 nr = nside; // equatorial region (the most frequent )
1341 z = (2 * nside - jr) * fact2;
1342 kshift = (long) BitManipulation.MODULO(jr - nside, 2);
1343 if (jr < nside) { // north pole region
1344 nr = jr;
1345 z = 1.0 - nr * nr * fact1;
1346 kshift = 0;
1347 } else if (jr > 3 * nside) { // south pole region
1348 nr = nl4 - jr;
1349 z = -1.0 + nr * nr * fact1;
1350 kshift = 0;
1351 }
1352 theta = Math.acos(z);
1353 /* computes phi coordinate on the sphere, in [0,2pi] */
1354 jp = (jpll[(int) (face_num + 1)] * nr + jpt + 1 + kshift) / 2;
1355 if (jp > nl4)
1356 jp = jp - nl4;
1357 if (jp < 1)
1358 jp = jp + nl4;
1359
1360 phi = (jp - (kshift + 1) * 0.5) * (HALFPI / nr);
1361 res[0] = theta;
1362 res[1] = phi;
1363 return res;
1364 }
1365
1366 /**
1367 * renders vector (x,y,z) coordinates of the nominal pixel center for the
1368 * pixel ipix (NESTED scheme ) given the map resolution parameter nside.
1369 * Also calculates the (x,y,z) positions of 4 pixel vertices (corners) in
1370 * the order N,W,S,E. These can be get using method pix2vertex_nest.
1371 *
1372 * @param nside the map resolution
1373 * @param ipix long pixel number
1374 * @return Vector3d
1375 * @throws IllegalArgumentException
1376 */
1377 public Vector3d pix2vect_nest(long nside, long ipix) {
1378
1379 PixInfo pixinfo = makePix2Vect_Nest(nside, ipix);
1380 return pixinfo.pixVect;
1381 }
1382
1383 /**
1384 * renders vector (x,y,z) coordinates of the nominal pixel center for the
1385 * pixel ipix (NESTED scheme ) given the map resolution parameter nside.
1386 * Also calculates the (x,y,z) positions of 4 pixel vertices (corners) in
1387 * the order N,W,S,E.
1388 *
1389 * @param nside the map resolution
1390 * @param ipix long pixel number
1391 * @return double[3][4] 4 sets of vector components
1392 * @throws IllegalArgumentException
1393 */
1394 public double[][] pix2vertex_nest(long nside, long ipix) {
1395 double[][] res = new double[3][4];
1396 PixInfo pixinfo = makePix2Vect_Nest(nside, ipix);
1397 double[][] pixVertex = pixinfo.pixVertex;
1398 for (int i = 0; i < 3; i++) {
1399 for (int j = 0; j < 4; j++) {
1400 res[i][j] = pixVertex[i][j];
1401 }
1402 }
1403 return res;
1404 }
1405
1406 /**
1407 * renders vector (x,y,z) coordinates of the nominal pixel center for the
1408 * pixel ipix (NESTED scheme ) given the map resolution parameter nside. Tis
1409 * can be get using method pix2vect_nest Also calculates the (x,y,z)
1410 * positions of 4 pixel vertices (corners) in the order N,W,S,E.
1411 * The result can be used using method pix2vertex_nest.
1412 * @param nside long the map resolution
1413 * @param ipix long pixel number
1414 * @return result
1415 */
1416 private PixInfo makePix2Vect_Nest(long nside, long ipix) {
1417 Vector3d pixVect = new Vector3d(0., 0., 0.);
1418 double[][] pixVertex = new double[3][4];
1419 long npix, npface, ipf, ip_low, ip_trunc, ip_med, ip_hi;
1420 long jrt, jr, nr, jpt, jp, kshift, nl4;
1421 double z, fn, fact1, fact2, sth, phi;
1422 long ix, iy, face_num;
1423 int[] jrll = { 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4 };
1424 int[] jpll = { 0, 1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7 };
1425 double phi_nv, phi_wv, phi_sv, phi_ev, phi_up, phi_dn;
1426 double z_nv, z_sv, sth_nv, sth_sv;
1427 double hdelta_phi;
1428 long iphi_mod, iphi_rat;
1429 // boolean do_vertex = true;
1430 String SID = "Pix2Vect_Nest:";
1431 z_nv = 0.;
1432 z_sv = 0.;
1433 /* */
1434 if (nside < 1 || nside > ns_max) {
1435 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
1436 }
1437 long nsidesq = nside * nside;
1438 npix = 12 * nsidesq;
1439 if (ipix < 0 || ipix > npix - 1) {
1440 throw new IllegalArgumentException(SID + " ipix out of range calculated from nside");
1441 }
1442 /* initiates the array for the pixel number -> (x,y) mapping */
1443 if (pix2x[pixmax-1] <= 0)
1444 mk_pix2xy();
1445 fn = nside;
1446 fact1 = 1.0 / (3.0 * fn * fn);
1447 fact2 = 2.0 / (3.0 * fn);
1448 nl4 = 4 * nside;
1449
1450 /* finds the face and the number in the face */
1451 npface = nsidesq;
1452 // System.out.println("ipix="+ipix+" npface="+npface);
1453 face_num = ipix / npface; // face number in [0, 11]
1454 ipf = (long) BitManipulation.MODULO(ipix, npface); // pixel number in the face [0,npface-1]
1455 /*
1456 * finds the x,y on the face (starting from the lowest corner) from the
1457 * pixel number
1458 */
1459 ip_low = (long) BitManipulation.MODULO(ipf, pixmax); // last 18 bits
1460 ip_trunc = ipf / pixmax; // trancateion of the last 18 bits
1461 ip_med = (long) BitManipulation.MODULO(ip_trunc, pixmax); // next 18 bits
1462 ip_hi = ip_trunc / pixmax; // high 18 bits
1463
1464 ix = pixmax * pix2x[(int)ip_hi] + xmid * pix2x[(int)ip_med] + pix2x[(int)ip_low];
1465 iy = pixmax * pix2y[(int)ip_hi] + xmid * pix2y[(int)ip_med] + pix2y[(int)ip_low];
1466
1467 /* transform this in (vertical, horizontal) coordinates */
1468 jrt = ix + iy; // vertical in [0,2*(nside-1)]
1469 jpt = ix - iy; // horizontal in [ -nside+1, nside-1]
1470 /* computes the z coordinate on the sphere */
1471 jr = jrll[(int) (face_num + 1)] * nside - jrt - 1; // ring number in
1472 // [1,4*nside-1]
1473
1474 nr = nside; // equatorial region (the most frequent )
1475 z = (2.0 * nside - jr) * fact2;
1476
1477 kshift = (long) BitManipulation.MODULO(jr - nside, 2);
1478 z_nv = (2.0 * nside - jr + 1.0) * fact2;
1479 z_sv = (2.0 * nside - jr - 1.0) * fact2;
1480 if (jr == nside) { // northen transition
1481 z_nv = 1.0 - (nside - 1.0) * (nside - 1.0) * fact1;
1482 } else if (jr == 3 * nside) { // southern transition
1483 z_sv = -1.0 + (nside - 1.0) * (nside - 1.0) * fact1;
1484 }
1485
1486 if (jr < nside) { // north pole region
1487 nr = jr;
1488 z = 1.0 - nr * nr * fact1;
1489 kshift = 0;
1490
1491 z_nv = 1.0 - (nr - 1) * (nr - 1) * fact1;
1492 z_sv = 1.0 - (nr + 1) * (nr + 1) * fact1;
1493
1494 } else if (jr > 3 * nside) { // south pole region
1495 nr = nl4 - jr;
1496 z = -1.0 + nr * nr * fact1;
1497 kshift = 0;
1498
1499 z_nv = -1.0 + (nr + 1) * (nr + 1) * fact1;
1500 z_sv = -1.0 + (nr - 1) * (nr - 1) * fact1;
1501
1502 }
1503 /* computes the phi coordinate on the sphere, in [0,2pi] */
1504 jp = (jpll[(int) (face_num + 1)] * nr + jpt + 1 + kshift) / 2; // phi in the ring in
1505 // [1,4*nr]
1506 if (jp > nl4)
1507 jp = jp - nl4;
1508 if (jp < 1)
1509 jp = jp + nl4;
1510
1511 phi = (jp - (kshift + 1) / 2.) * (HALFPI / nr);
1512
1513 sth = Math.sqrt((1.0 - z) * (1.0 + z));
1514 pixVect.x = sth * Math.cos(phi);
1515 pixVect.y = sth * Math.sin(phi);
1516 pixVect.z = z;
1517
1518 phi_nv = phi;
1519 phi_sv = phi;
1520
1521 phi_up = 0.0;
1522 iphi_mod = (long) BitManipulation.MODULO(jp - 1, nr); // in [0,1,...,nr-1]
1523 iphi_rat = (jp - 1) / nr; // in [0,1,2,3]
1524 if (nr > 1)
1525 phi_up = HALFPI * (iphi_rat + iphi_mod / (nr - 1.));
1526 phi_dn = HALFPI * (iphi_rat + (iphi_mod + 1) / (nr + 1.));
1527
1528 if (jr < nside) { // north polar cap
1529 phi_nv = phi_up;
1530 phi_sv = phi_dn;
1531 } else if (jr > 3 * nside) { // south polar cap
1532 phi_nv = phi_dn;
1533 phi_sv = phi_up;
1534 } else if (jr == nside) { // north transition
1535 phi_nv = phi_up;
1536 } else if (jr == 3 * nside) { // south transition
1537 phi_sv = phi_up;
1538 }
1539 hdelta_phi = PI / (4.0 * nr);
1540 /* west vertex */
1541 // phi_wv = phi = hdelta_phi;
1542 phi_wv = phi - hdelta_phi;
1543 pixVertex[0][1] = sth * Math.cos(phi_wv);
1544 pixVertex[1][1] = sth * Math.sin(phi_wv);
1545 pixVertex[2][1] = z;
1546 /* east vertex */
1547 phi_ev = phi + hdelta_phi;
1548 pixVertex[0][3] = sth * Math.cos(phi_ev);
1549 pixVertex[1][3] = sth * Math.sin(phi_ev);
1550 pixVertex[2][3] = z;
1551 /* north vertex */
1552 sth_nv = Math.sqrt((1.0 - z_nv) * (1.0 + z_nv));
1553 pixVertex[0][0] = sth_nv * Math.cos(phi_nv);
1554 pixVertex[1][0] = sth_nv * Math.sin(phi_nv);
1555 pixVertex[2][0] = z_nv;
1556 /* south vertex */
1557 sth_sv = Math.sqrt((1.0 - z_sv) * (1.0 + z_sv));
1558 pixVertex[0][2] = sth_sv * Math.cos(phi_sv);
1559 pixVertex[1][2] = sth_sv * Math.sin(phi_sv);
1560 pixVertex[2][2] = z_sv;
1561 return new PixInfo(pixVect, pixVertex);
1562 }
1563
1564 /**
1565 * renders the pixel number pix (NESTED scheme) for a pixel which contains a
1566 * point on a sphere at coordinates theta and phi, given map resolution
1567 * parameter nside.
1568 *
1569 * The computation is made to the highest resolution available and then
1570 * degraded to required resolution by integer division. It makes sure that
1571 * the treatment of round-off will be consistent for every resolution.
1572 *
1573 * @param nside the map resolution parameter
1574 * @param theta double theta coordinate in radians
1575 * @param phi double phi coordinate in radians
1576 * @return pixel number long
1577 * @throws IllegalArgumentException
1578 */
1579 public long ang2pix_nest(long nside, double theta, double phi) {
1580 long pix = 0;
1581 // long ipix1;
1582 double z, za, tt, tp, tmp;
1583 long jp, jm, ifp, ifm, face_num, ix, iy, ix_low, ix_hi;
1584 long iy_low, iy_hi, ipf, ntt;
1585 // long nl2, nl4, ncap, npix, kshift, ir, ip;
1586 String SID = "ang2pix_nest:";
1587 /* */
1588 if (nside < 1 || nside > ns_max) {
1589 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
1590 }
1591 if (theta < 0.0 || theta > PI) {
1592 throw new IllegalArgumentException(SID + " theta is out of range [0.,PI]");
1593 }
1594 if (x2pix[xmax-1] <= 0)
1595 mk_xy2pix();
1596
1597 z = Math.cos(theta);
1598 za = Math.abs(z);
1599
1600
1601 if (phi < 0.)
1602 phi += TWOPI; // phi in [0,2pi]
1603 if (phi >= TWOPI ) phi -= TWOPI;
1604 tt = BitManipulation.MODULO(phi, TWOPI) / HALFPI; // in [0,4]
1605 // tt = 2. * phi / PI; // in [0,4]
1606 if (za <= twothird) { // Equatorial region
1607 /*
1608 * the index of edge lines increases when the longitude = phi goes
1609 * up
1610 */
1611 jp = (long) (ns_max * (0.5 + tt - z * 0.75)); // ascending edge line
1612 // index
1613 jm = (long) (ns_max * (0.5 + tt + z * 0.75)); // descending edge line
1614 // index
1615 /* find the face */
1616 ifp = jp / ns_max; // in [0,4]
1617 ifm = jm / ns_max;
1618 if (ifp == ifm) { // faces 4 to 7
1619 face_num = (long) BitManipulation.MODULO(ifp, 4) + 4;
1620 } else if (ifp < ifm) { // (half-) faces 0 to 3
1621 face_num = (long) BitManipulation.MODULO(ifp, 4);
1622 } else { // (half-) faces 8 to 11
1623 face_num = (long) BitManipulation.MODULO(ifm, 4) + 8;
1624 }
1625
1626 ix = (long) BitManipulation.MODULO(jm, ns_max);
1627 iy = ns_max - (long) BitManipulation.MODULO(jp, ns_max) - 1;
1628 } else { // polar region, za > 2/3
1629 ntt = (long) tt;
1630 if (ntt >= 4)
1631 ntt = 3;
1632 tp = tt - ntt;
1633 tmp = Math.sqrt(3.0 * (1.0 - za)); // in [0,1]
1634 /*
1635 * the index of edge lines increases when distance from the closest
1636 * pole goes up
1637 */
1638 jp = (long) (ns_max * tp * tmp); // line going toward the pole has
1639 // phi increases
1640 jm = (long) (ns_max * (1.0 - tp) * tmp); // that one goes away of the
1641 // closest pole
1642 jp = (long) Math.min(ns_max - 1, jp); // for pointss too close to the
1643 // boundary
1644 jm = (long) Math.min(ns_max - 1, jm);
1645 /* finds the face and pixel's (x,y) */
1646 if (z >= 0) {
1647 face_num = ntt; // in [0,3]
1648 ix = ns_max - jm - 1;
1649 iy = ns_max - jp - 1;
1650 } else {
1651 face_num = ntt + 8; // in [8,11]
1652 ix = jp;
1653 iy = jm;
1654 }
1655 }
1656 ix_low = (long) BitManipulation.MODULO(ix, xmax);
1657 ix_hi = ix / xmax;
1658 iy_low = (long) BitManipulation.MODULO(iy, xmax);
1659 iy_hi = iy / xmax;
1660
1661 ipf = (x2pix[(int) (ix_hi + 1)] + y2pix[(int) (iy_hi + 1)]) * (xmax * xmax)
1662 + (x2pix[(int) (ix_low + 1)] + y2pix[(int) (iy_low + 1)]);
1663 ipf = ipf / ((ns_max / nside) * (ns_max / nside)); // in [0, nside**2
1664 // -1]
1665 pix = ipf + face_num * nside * nside; // in [0, 12*nside**2 -1]
1666 return pix;
1667 }
1668
1669 /**
1670 * make the conversion NEST to RING
1671 *
1672 * @param nside the map resolution parameter
1673 * @param map Object[] the map in NESTED scheme
1674 * @return - Object[] a map in RING scheme
1675 * @throws IllegalArgumentException
1676 */
1677 public Object[] convert_nest2ring(long nside, Object[] map) {
1678 Object[] res;
1679 long npix, ipn;
1680 int ipr;
1681 npix = 12 * nside * nside;
1682 res = new Object[(int) npix];
1683 for (ipn = 0; ipn < npix; ipn++) {
1684 ipr = (int) nest2ring(nside, ipn);
1685 res[ipr] = map[(int) ipn];
1686 }
1687 return res;
1688 }
1689
1690 /**
1691 * makes the conversion RING to NEST
1692 *
1693 * @param nside
1694 * long resolution
1695 * @param map
1696 * map in RING
1697 * @return map in NEST
1698 * @throws IllegalArgumentException
1699 */
1700 public Object[] convert_ring2nest(long nside, Object[] map) {
1701 Object[] res;
1702 long npix, ipn, ipr;
1703 npix = 12 * nside * nside;
1704 res = new Object[(int) npix];
1705 for (ipr = 0; ipr < npix; ipr++) {
1706 ipn = ring2nest(nside, ipr);
1707 res[(int) ipn] = map[(int)ipr];
1708 }
1709 return res;
1710 }
1711
1712 /**
1713 * converts a 8 byte Object map from RING to NESTED and vice versa in place,
1714 * ie without allocation a temporary map (Has no reason for Java). This
1715 * method is more general but slower than convert_nest2ring.
1716 *
1717 * This method is a wrapper for functions ring2nest and nest2ring. Their
1718 * names are supplied in the subcall argument.
1719 *
1720 * @param subcall
1721 * String name of the method to use.
1722 * @param map
1723 * Object[] map
1724 * @return resulting Object[] map.
1725 * @throws IllegalArgumentException
1726 */
1727 public Object[] convert_inplace_long(String subcall, Object[] map) {
1728 Object[] res;
1729 long npix, nside;
1730 boolean[] check;
1731 long ilast, i1, i2;
1732 String SID = "convert_in_place:";
1733 Object pixbuf1, pixbuf2;
1734 npix = map.length;
1735 nside = (long) Math.sqrt(npix / 12.);
1736 if (nside > ns_max) {
1737 throw new IllegalArgumentException(SID + " Map is too big");
1738 }
1739 check = new boolean[(int) npix];
1740 for (int i = 0; i < npix; i++)
1741 check[i] = false;
1742 ilast = 0; // start from first pixel
1743 for (int i = 0; i < npix; i++) {
1744 pixbuf2 = map[(int) ilast];
1745 i1 = ilast;
1746 if (subcall.equalsIgnoreCase("ring2nest")) {
1747 i2 = ring2nest(nside, i1);
1748 } else {
1749 i2 = nest2ring(nside, i1);
1750 }
1751 while (!check[(int) i2]) {
1752 pixbuf1 = map[(int) i2];
1753 map[(int) i2] = pixbuf2;
1754 pixbuf2 = pixbuf1;
1755 i1 = i2;
1756 if (subcall.equalsIgnoreCase("ring2nest")) {
1757 i2 = ring2nest(nside, i1);
1758 } else {
1759 i2 = nest2ring(nside, i1);
1760 }
1761 }
1762 while (!(check[(int) ilast] && (ilast < npix - 1))) {
1763 ilast++;
1764 }
1765 }
1766 res = map;
1767 return res;
1768 }
1769
1770 /**
1771 * returns 7 or 8 neighbours of any pixel in the nested scheme The neighbours
1772 * are ordered in the following way: First pixel is the one to the south (
1773 * the one west of the south direction is taken for pixels that don't have a
1774 * southern neighbour). From then on the neighbors are ordered in the
1775 * clockwise direction.
1776 *
1777 * @param nside the map resolution
1778 * @param ipix long pixel number
1779 * @return ArrayList
1780 * @throws IllegalArgumentException
1781 */
1782 public ArrayList neighbours_nest(long nside, long ipix) {
1783 ArrayList res = new ArrayList();
1784 long npix, ipf, ipo, ix, ixm, ixp, iy, iym, iyp, ixo, iyo;
1785 long face_num, other_face;
1786 long ia, ib, ibp, ibm, ib2, nsidesq;
1787 int icase;
1788 long local_magic1, local_magic2;
1789 long arb_const = 0;
1790 long[] ixiy = new long[2];
1791 long[] ixoiyo = new long[2];
1792 String SID = "neighbours_nest:";
1793 /* fill the pixel list with 0 */
1794 res.add(0, new Long(0));
1795 res.add(1, new Long(0));
1796 res.add(2, new Long(0));
1797 res.add(3, new Long(0));
1798 res.add(4, new Long(0));
1799 res.add(5, new Long(0));
1800 res.add(6, new Long(0));
1801 res.add(7, new Long(0));
1802 icase = 0;
1803 /* */
1804 if ((nside < 1) || (nside > ns_max)) {
1805 throw new IllegalArgumentException(SID + " Nside should be power of 2 >0 and < "+ns_max);
1806 }
1807 nsidesq = nside * nside;
1808 npix = 12 * nsidesq; // total number of pixels
1809 if ((ipix < 0) || (ipix > npix - 1)) {
1810 throw new IllegalArgumentException(SID + " ipix out of range ");
1811 }
1812 if (x2pix[xmax-1] <= 0)
1813 mk_xy2pix();
1814
1815 local_magic1 = (nsidesq - 1) / 3;
1816 local_magic2 = 2 * local_magic1;
1817 face_num = ipix / nsidesq;
1818 ipf = (long) BitManipulation.MODULO(ipix, nsidesq); // Pixel number in face
1819 ixiy = pix2xy_nest(nside, ipf);
1820 ix = ixiy[0];
1821 iy = ixiy[1];
1822 //
1823 ixm = ixiy[0] - 1;
1824 ixp = ixiy[0] + 1;
1825 iym = ixiy[1] - 1;
1826 iyp = ixiy[1] + 1;
1827
1828 icase = 0; // inside the face
1829
1830 /* exclude corners */
1831 if (ipf == local_magic2 && icase == 0)
1832 icase = 5; // West corner
1833 if (ipf == (nsidesq - 1) && icase == 0)
1834 icase = 6; // North corner
1835 if (ipf == 0 && icase == 0)
1836 icase = 7; // South corner
1837 if (ipf == local_magic1 && icase == 0)
1838 icase = 8; // East corner
1839
1840 /* detect edges */
1841 if ((ipf & local_magic1) == local_magic1 && icase == 0)
1842 icase = 1; // NorthEast
1843 if ((ipf & local_magic1) == 0 && icase == 0)
1844 icase = 2; // SouthWest
1845 if ((ipf & local_magic2) == local_magic2 && icase == 0)
1846 icase = 3; // NorthWest
1847 if ((ipf & local_magic2) == 0 && icase == 0)
1848 icase = 4; // SouthEast
1849
1850 /* iside a face */
1851 if (icase == 0) {
1852 res.add(0, new Long( xy2pix_nest(nside, ixm, iym, face_num)));
1853 res.add(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
1854 res.add(2, new Long( xy2pix_nest(nside, ixm, iyp, face_num)));
1855 res.add(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
1856 res.add(4, new Long( xy2pix_nest(nside, ixp, iyp, face_num)));
1857 res.add(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
1858 res.add(6, new Long( xy2pix_nest(nside, ixp, iym, face_num)));
1859 res.add(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
1860 }
1861 /* */
1862 ia = face_num / 4; // in [0,2]
1863 ib = (long) BitManipulation.MODULO(face_num, 4); // in [0,3]
1864 ibp = (long) BitManipulation.MODULO(ib + 1, 4);
1865 ibm = (long) BitManipulation.MODULO(ib + 4 - 1, 4);
1866 ib2 = (long) BitManipulation.MODULO(ib + 2, 4);
1867
1868 if (ia == 0) { // North pole region
1869 switch (icase) {
1870 case 1: // north-east edge
1871 other_face = 0 + ibp;
1872 res.set(0, new Long( xy2pix_nest(nside, ixm, iym, face_num)));
1873 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
1874 res.set(2, new Long( xy2pix_nest(nside, ixm, iyp, face_num)));
1875 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
1876 res.set(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
1877 ipo = (long) BitManipulation.MODULO(BitManipulation.swapLSBMSB( ipf), nsidesq);
1878 ixoiyo = pix2xy_nest(nside, ipo);
1879 ixo = ixoiyo[0];
1880 iyo = ixoiyo[1];
1881 res.set(4, new Long( xy2pix_nest(nside, ixo + 1, iyo,
1882 other_face)));
1883 res.set(5, new Long( (other_face * nsidesq + ipo)));
1884 res.set(6, new Long( xy2pix_nest(nside, ixo - 1, iyo,
1885 other_face)));
1886 break;
1887 case 2: // SouthWest edge
1888 other_face = 4 + ib;
1889 ipo = (long) BitManipulation.MODULO(BitManipulation.invLSB( ipf), nsidesq); // SW-NE flip
1890 ixoiyo = pix2xy_nest(nside, ipo);
1891 ixo = ixoiyo[0];
1892 iyo = ixoiyo[1];
1893 res.set(0, new Long( xy2pix_nest(nside, ixo, iyo - 1,
1894 other_face)));
1895 res.set(1, new Long( (other_face * nsidesq + ipo)));
1896 res.set(2, new Long( xy2pix_nest(nside, ixo, iyo + 1,
1897 other_face)));
1898 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
1899 res.set(4, new Long( xy2pix_nest(nside, ixp, iyp, face_num)));
1900 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
1901 res.set(6, new Long( xy2pix_nest(nside, ixp, iym, face_num)));
1902 res.set(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
1903 break;
1904 case 3: // NorthWest edge
1905 other_face = 0 + ibm;
1906 ipo = (long) BitManipulation.MODULO(BitManipulation.swapLSBMSB( ipf), nsidesq); // E-W flip
1907 ixoiyo = pix2xy_nest(nside, ipo);
1908 ixo = ixoiyo[0];
1909 iyo = ixoiyo[1];
1910 res.set(0, new Long( xy2pix_nest(nside, ixm, iym, face_num)));
1911 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
1912 res.set(2, new Long( xy2pix_nest(nside, ixo, iyo - 1,
1913 other_face)));
1914 res.set(3, new Long( (other_face * nsidesq + ipo)));
1915 res.set(4, new Long( xy2pix_nest(nside, ixo, iyo + 1,
1916 other_face)));
1917 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
1918 res.set(6, new Long( xy2pix_nest(nside, ixp, iym, face_num)));
1919 res.set(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
1920 break;
1921 case 4: // SouthEast edge
1922 other_face = 4 + ibp;
1923 ipo = (long) BitManipulation.MODULO(BitManipulation.invMSB( ipf), nsidesq); // SE-NW flip
1924 ixoiyo = pix2xy_nest(nside, ipo);
1925 ixo = ixoiyo[0];
1926 iyo = ixoiyo[1];
1927 res.set(0, new Long( xy2pix_nest(nside, ixo - 1, iyo,
1928 other_face)));
1929 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
1930 res.set(2, new Long( xy2pix_nest(nside, ixm, iyp, face_num)));
1931 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
1932 res.set(4, new Long( xy2pix_nest(nside, ixp, iyp, face_num)));
1933 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
1934 res.set(6, new Long( xy2pix_nest(nside, ixo + 1, iyo,
1935 other_face)));
1936 res.set(7, new Long( (other_face * nsidesq + ipo)));
1937 break;
1938 case 5: // West corner
1939 other_face = 4 + ib;
1940 arb_const = other_face * nsidesq + nsidesq - 1;
1941 res.set(0, new Long( (arb_const - 2)));
1942 res.set(1, new Long( arb_const));
1943 other_face = 0 + ibm;
1944 arb_const = other_face * nsidesq + local_magic1;
1945 res.set(2, new Long( arb_const));
1946 res.set(3, new Long( (arb_const + 2)));
1947 res.set(4, new Long( (ipix + 1)));
1948 res.set(5, new Long( (ipix - 1)));
1949 res.set(6, new Long( (ipix - 2)));
1950 res.remove(7);
1951 break;
1952 case 6: // North corner
1953 other_face = 0 + ibm;
1954 res.set(0, new Long( (ipix - 3)));
1955 res.set(1, new Long( (ipix - 1)));
1956 arb_const = other_face * nsidesq + nsidesq - 1;
1957 res.set(2, new Long( (arb_const - 2)));
1958 res.set(3, new Long( arb_const));
1959 other_face = 0 + ib2;
1960 res.set(4, new Long( (other_face * nsidesq + nsidesq - 1)));
1961 other_face = 0 + ibp;
1962 arb_const = other_face * nsidesq + nsidesq - 1;
1963 res.set(5, new Long( arb_const));
1964 res.set(6, new Long( (arb_const - 1)));
1965 res.set(7, new Long( (ipix - 2)));
1966 break;
1967 case 7: // South corner
1968 other_face = 8 + ib;
1969 res.set(0, new Long( (other_face * nsidesq + nsidesq - 1)));
1970 other_face = 4 + ib;
1971 arb_const = other_face * nsidesq + local_magic1;
1972 res.set(1, new Long( arb_const));
1973 res.set(2, new Long( (arb_const + 2)));
1974 res.set(3, new Long( (ipix + 2)));
1975 res.set(4, new Long( (ipix + 3)));
1976 res.set(5, new Long( (ipix + 1)));
1977 other_face = 4 + ibp;
1978 arb_const = other_face * nsidesq + local_magic2;
1979 res.set(6, new Long( (arb_const + 1)));
1980 res.set(7, new Long( arb_const));
1981 break;
1982 case 8: // East corner
1983 other_face = 0 + ibp;
1984 res.set(1, new Long( (ipix - 1)));
1985 res.set(2, new Long( (ipix + 1)));
1986 res.set(3, new Long( (ipix + 2)));
1987 arb_const = other_face * nsidesq + local_magic2;
1988 res.set(4, new Long( (arb_const + 1)));
1989 res.set(5, new Long(( arb_const)));
1990 other_face = 4 + ibp;
1991 arb_const = other_face * nsidesq + nsidesq - 1;
1992 res.set(0, new Long( (arb_const - 1)));
1993 res.set(6, new Long( arb_const));
1994 res.remove(7);
1995 break;
1996 }
1997 } else if (ia == 1) { // Equatorial region
1998 switch (icase) {
1999 case 1: // north-east edge
2000 other_face = 0 + ibp;
2001 res.set(0, new Long( xy2pix_nest(nside, ixm, iym, face_num)));
2002 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
2003 res.set(2, new Long( xy2pix_nest(nside, ixm, iyp, face_num)));
2004 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
2005 res.set(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
2006 ipo = (long) BitManipulation.MODULO(BitManipulation.invLSB( ipf), nsidesq); // NE-SW flip
2007 ixoiyo = pix2xy_nest(nside, ipo);
2008 ixo = ixoiyo[0];
2009 iyo = ixoiyo[1];
2010 res.set(4, new Long( xy2pix_nest(nside, ixo, iyo + 1,
2011 other_face)));
2012 res.set(5, new Long( (other_face * nsidesq + ipo)));
2013 res.set(6, new Long( xy2pix_nest(nside, ixo, iyo - 1,
2014 other_face)));
2015 break;
2016 case 2: // SouthWest edge
2017 other_face = 8 + ibm;
2018 ipo = (long) BitManipulation.MODULO(BitManipulation.invLSB( ipf), nsidesq); // SW-NE flip
2019 ixoiyo = pix2xy_nest(nside, ipo);
2020 ixo = ixoiyo[0];
2021 iyo = ixoiyo[1];
2022 res.set(0, new Long( xy2pix_nest(nside, ixo, iyo - 1,
2023 other_face)));
2024 res.set(1, new Long((other_face * nsidesq + ipo)));
2025 res.set(2, new Long( xy2pix_nest(nside, ixo, iyo + 1,
2026 other_face)));
2027 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
2028 res.set(4, new Long( xy2pix_nest(nside, ixp, iyp, face_num)));
2029 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
2030 res.set(6, new Long( xy2pix_nest(nside, ixp, iym, face_num)));
2031 res.set(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
2032 break;
2033 case 3: // NortWest edge
2034 other_face = 0 + ibm;
2035 ipo = (long) BitManipulation.MODULO(BitManipulation.invMSB( ipf), nsidesq); // NW-SE flip
2036 ixoiyo = pix2xy_nest(nside, ipo);
2037 ixo = ixoiyo[0];
2038 iyo = ixoiyo[1];
2039 res.set(2, new Long( xy2pix_nest(nside, ixo - 1, iyo,
2040 other_face)));
2041 res.set(3, new Long( (other_face * nsidesq + ipo)));
2042 res.set(4, new Long( xy2pix_nest(nside, ixo + 1, iyo,
2043 other_face)));
2044 res.set(0, new Long( xy2pix_nest(nside, ixm, iym, face_num)));
2045 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
2046 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
2047 res.set(6, new Long( xy2pix_nest(nside, ixp, iym, face_num)));
2048 res.set(7, new Long(xy2pix_nest(nside, ix, iym, face_num)));
2049 break;
2050 case 4: // SouthEast edge
2051 other_face = 8 + ib;
2052 ipo = (long) BitManipulation.MODULO(BitManipulation.invMSB( ipf), nsidesq); // SE-NW flip
2053 ixoiyo = pix2xy_nest(nside, ipo);
2054 ixo = ixoiyo[0];
2055 iyo = ixoiyo[1];
2056 res.set(0, new Long( xy2pix_nest(nside, ixo - 1, iyo,
2057 other_face)));
2058 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
2059 res.set(2, new Long( xy2pix_nest(nside, ixm, iyp, face_num)));
2060 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
2061 res.set(4, new Long( xy2pix_nest(nside, ixp, iyp, face_num)));
2062 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
2063 res.set(6, new Long( xy2pix_nest(nside, ixo + 1, iyo,
2064 other_face)));
2065 res.set(7, new Long( (other_face * nsidesq + ipo)));
2066 break;
2067 case 5: // West corner
2068 other_face = 8 + ibm;
2069 arb_const = other_face * nsidesq + nsidesq - 1;
2070 res.set(0, new Long( (arb_const - 2)));
2071 res.set(1, new Long( arb_const));
2072 other_face = 4 + ibm;
2073 res.set(2, new Long( (other_face * nsidesq + local_magic1)));
2074 other_face = 0 + ibm;
2075 arb_const = other_face * nsidesq;
2076 res.set(3, new Long( arb_const));
2077 res.set(4, new Long( (arb_const + 1)));
2078 res.set(5, new Long( (ipix + 1)));
2079 res.set(6, new Long( (ipix - 1)));
2080 res.set(7, new Long( (ipix - 2)));
2081 break;
2082 case 6: // North corner
2083 other_face = 0 + ibm;
2084 res.set(0, new Long( (ipix - 3)));
2085 res.set(1, new Long( (ipix - 1)));
2086 arb_const = other_face * nsidesq + local_magic1;
2087 res.set(2, new Long( (arb_const - 1)));
2088 res.set(3, new Long( arb_const));
2089 other_face = 0 + ib;
2090 arb_const = other_face * nsidesq + local_magic2;
2091 res.set(4, new Long( arb_const));
2092 res.set(5, new Long( (arb_const - 2)));
2093 res.set(6, new Long( (ipix - 2)));
2094 res.remove(7);
2095 break;
2096 case 7: // South corner
2097 other_face = 8 + ibm;
2098 arb_const = other_face * nsidesq + local_magic1;
2099 res.set(0, new Long( arb_const));
2100 res.set(1, new Long( (arb_const + 2)));
2101 res.set(2, new Long( (ipix + 2)));
2102 res.set(3, new Long( (ipix + 3)));
2103 res.set(4, new Long( (ipix + 1)));
2104 other_face = 8 + ib;
2105 arb_const = other_face * nsidesq + local_magic2;
2106 res.set(5, new Long( (arb_const + 1)));
2107 res.set(6, new Long( arb_const));
2108 res.remove(7);
2109 break;
2110 case 8: // East corner
2111 other_face = 8 + ib;
2112 arb_const = other_face * nsidesq + nsidesq - 1;
2113 res.set(0, new Long( (arb_const - 1)));
2114 res.set(1, new Long( (ipix - 1)));
2115 res.set(2, new Long( (ipix + 1)));
2116 res.set(3, new Long( (ipix + 2)));
2117 res.set(7, new Long( arb_const));
2118 other_face = 0 + ib;
2119 arb_const = other_face * nsidesq;
2120 res.set(4, new Long( (arb_const + 2)));
2121 res.set(5, new Long( arb_const));
2122 other_face = 4 + ibp;
2123 res.set(6, new Long( (other_face * nsidesq + local_magic2)));
2124 break;
2125 }
2126 } else { // South pole region
2127 switch (icase) {
2128 case 1: // North-East edge
2129 other_face = 4 + ibp;
2130 res.set(0, new Long( xy2pix_nest(nside, ixm, iym, face_num)));
2131 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
2132 res.set(2, new Long( xy2pix_nest(nside, ixm, iyp, face_num)));
2133 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
2134 res.set(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
2135 ipo = (long) BitManipulation.MODULO(BitManipulation.invLSB( ipf), nsidesq); // NE-SW flip
2136 ixoiyo = pix2xy_nest(nside, ipo);
2137 ixo = ixoiyo[0];
2138 iyo = ixoiyo[1];
2139 res.set(4, new Long( xy2pix_nest(nside, ixo, iyo + 1,
2140 other_face)));
2141 res.set(5, new Long( (other_face * nsidesq + ipo)));
2142 res.set(6, new Long( xy2pix_nest(nside, ixo, iyo - 1,
2143 other_face)));
2144 break;
2145 case 2: // SouthWest edge
2146 other_face = 8 + ibm;
2147 ipo = (long) BitManipulation.MODULO(BitManipulation.swapLSBMSB( ipf), nsidesq); // W-E flip
2148 ixoiyo = pix2xy_nest(nside, ipo);
2149 ixo = ixoiyo[0];
2150 iyo = ixoiyo[1];
2151 res.set(0, new Long( xy2pix_nest(nside, ixo - 1, iyo,
2152 other_face)));
2153 res.set(1, new Long( (other_face * nsidesq + ipo)));
2154 res.set(2, new Long( xy2pix_nest(nside, ixo + 1, iyo,
2155 other_face)));
2156 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
2157 res.set(4, new Long( xy2pix_nest(nside, ixp, iyp, face_num)));
2158 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
2159 res.set(6, new Long(xy2pix_nest(nside, ixp, iym, face_num)));
2160 res.set(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
2161 break;
2162 case 3: // NorthWest edge
2163 other_face = 4 + ib;
2164 ipo = (long) BitManipulation.MODULO(BitManipulation.invMSB( ipf), nsidesq);
2165 ixoiyo = pix2xy_nest(nside, ipo);
2166 ixo = ixoiyo[0];
2167 iyo = ixoiyo[1];
2168 res.set(0, new Long( xy2pix_nest(nside, ixm, iym, face_num)));
2169 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
2170 res.set(2, new Long( xy2pix_nest(nside, ixo - 1, iyo,
2171 other_face)));
2172 res.set(3, new Long( (other_face * nsidesq + ipo)));
2173 res.set(4, new Long( xy2pix_nest(nside, ixo + 1, iyo,
2174 other_face)));
2175 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
2176 res.set(6, new Long( xy2pix_nest(nside, ixp, iym, face_num)));
2177 res.set(7, new Long( xy2pix_nest(nside, ix, iym, face_num)));
2178 break;
2179 case 4: // SouthEast edge
2180 other_face = 8 + ibp;
2181 ipo = (long) BitManipulation.MODULO(BitManipulation.swapLSBMSB( ipf), nsidesq); // SE-NW
2182 // flip
2183 ixoiyo = pix2xy_nest(nside, ipo);
2184 ixo = ixoiyo[0];
2185 iyo = ixoiyo[1];
2186 res.set(0, new Long( xy2pix_nest(nside, ixo, iyo - 1,
2187 other_face)));
2188 res.set(1, new Long( xy2pix_nest(nside, ixm, iy, face_num)));
2189 res.set(2, new Long( xy2pix_nest(nside, ixm, iyp, face_num)));
2190 res.set(3, new Long( xy2pix_nest(nside, ix, iyp, face_num)));
2191 res.set(4, new Long( xy2pix_nest(nside, ixp, iyp, face_num)));
2192 res.set(5, new Long( xy2pix_nest(nside, ixp, iy, face_num)));
2193 res.set(6, new Long( xy2pix_nest(nside, ixo, iyo + 1,
2194 other_face)));
2195 res.set(7, new Long( (other_face * nsidesq + ipo)));
2196 break;
2197 case 5: // West corner
2198 other_face = 8 + ibm;
2199 arb_const = other_face * nsidesq + local_magic1;
2200 res.set(0, new Long( (arb_const - 2)));
2201 res.set(1, new Long( arb_const));
2202 other_face = 4 + ib;
2203 res.set(2, new Long( (other_face * nsidesq)));
2204 res.set(3, new Long( (other_face * nsidesq + 1)));
2205 res.set(4, new Long( (ipix + 1)));
2206 res.set(5, new Long( (ipix - 1)));
2207 res.set(6, new Long( (ipix - 2)));
2208 res.remove(7);
2209 break;
2210 case 6: // North corner
2211 other_face = 4 + ib;
2212 res.set(0, new Long( (ipix - 3)));
2213 res.set(1, new Long((ipix - 1)));
2214 arb_const = other_face * nsidesq + local_magic1;
2215 res.set(2, new Long( (arb_const - 1)));
2216 res.set(3, new Long( arb_const));
2217 other_face = 0 + ib;
2218 res.set(4, new Long( (other_face * nsidesq)));
2219 other_face = 4 + ibp;
2220 arb_const = other_face * nsidesq + local_magic2;
2221 res.set(5, new Long( arb_const));
2222 res.set(6, new Long( (arb_const - 2)));
2223 res.set(7, new Long( (ipix - 2)));
2224 break;
2225 case 7: // South corner
2226 other_face = 8 + ib2;
2227 res.set(0, new Long( (other_face * nsidesq)));
2228 other_face = 8 + ibm;
2229 arb_const = other_face * nsidesq;
2230 res.set(1, new Long( arb_const));
2231 res.set(2, new Long( (arb_const + 1)));
2232 res.set(3, new Long( (ipix + 2)));
2233 res.set(4, new Long( (ipix + 3)));
2234 res.set(5, new Long( (ipix + 1)));
2235 other_face = 8 + ibp;
2236 arb_const = other_face * nsidesq;
2237 res.set(6, new Long( (arb_const + 2)));
2238 res.set(7, new Long( arb_const));
2239 break;
2240 case 8: // East corner
2241 other_face = 8 + ibp;
2242 res.set(1, new Long( (ipix - 1)));
2243 res.set(2, new Long( (ipix + 1)));
2244 res.set(3, new Long( (ipix + 2)));
2245 arb_const = other_face * nsidesq + local_magic2;
2246 res.set(6, new Long( arb_const));
2247 res.set(0, new Long( (arb_const - 2)));
2248 other_face = 4 + ibp;
2249 arb_const = other_face * nsidesq;
2250 res.set(4, new Long( (arb_const + 2)));
2251 res.set(5, new Long( arb_const));
2252 res.remove(7);
2253 break;
2254 }
2255 }
2256 return res;
2257 }
2258
2259 /**
2260 * returns the list of pixels in RING or NEST scheme with latitude in [phi0 -
2261 * dpi, phi0 + dphi] on the ring iz in [1, 4*nside -1 ] The pixel id numbers
2262 * are in [0, 12*nside^2 - 1] the indexing is in RING, unless nest is set to
2263 * 1
2264 *
2265 * @param nside
2266 * long the map resolution
2267 * @param iz
2268 * long ring number
2269 * @param phi0
2270 * double
2271 * @param dphi
2272 * double
2273 * @param nest
2274 * boolean format flag
2275 * @return ArrayList of pixels
2276 * @throws IllegalArgumentException
2277 *
2278 * Modified by N. Kuropatkin 07/09/2008 Corrected several bugs and make test of all cases.
2279 *
2280 */
2281 public ArrayList InRing(long nside, long iz, double phi0, double dphi,
2282 boolean nest) {
2283 boolean take_all = false;
2284 boolean to_top = false;
2285 boolean do_ring = true;
2286 boolean conservative = false;
2287 // String SID = "InRing:";
2288 double epsilon = Double.MIN_VALUE; // the constant to eliminate
2289 // java calculation jitter
2290 if (nest)
2291 do_ring = false;
2292 double shift = 0.;
2293 long ir = 0;
2294 long kshift, nr, ipix1, ipix2, nir1, nir2, ncap, npix;
2295 long ip_low = 0, ip_hi = 0, in, inext, nir;
2296 ArrayList res = new ArrayList();
2297 npix = 12 * nside * nside; // total number of pixels
2298 ncap = 2 * nside * (nside - 1); // number of pixels in the north polar
2299 // cap
2300 double phi_low = BitManipulation.MODULO(phi0 - dphi, TWOPI) - epsilon; // phi min,
2301 // excluding
2302 // 2pi period
2303 double phi_hi = BitManipulation.MODULO(phi0 + dphi, TWOPI) + epsilon;
2304
2305 //
2306 if (Math.abs(dphi - PI) < epsilon) take_all = true;
2307
2308 /* identifies ring number */
2309 if ((iz >= nside) && (iz <= 3 * nside)) { // equatorial region
2310 ir = iz - nside + 1; // in [1, 2*nside + 1]
2311 ipix1 = ncap + 4 * nside * (ir - 1); // lowest pixel number in the
2312 // ring
2313 ipix2 = ipix1 + 4 * nside - 1; // highest pixel number in the ring
2314 kshift = (long) BitManipulation.MODULO(ir, 2.);
2315
2316 nr = nside * 4;
2317 } else {
2318 if (iz < nside) { // north pole
2319 ir = iz;
2320 ipix1 = 2 * ir * (ir - 1); // lowest pixel number
2321 ipix2 = ipix1 + 4 * ir - 1; // highest pixel number
2322 } else { // south pole
2323 ir = 4 * nside - iz;
2324
2325 ipix1 = npix - 2 * ir * (ir + 1); // lowest pixel number
2326 ipix2 = ipix1 + 4 * ir - 1; // highest pixel number
2327 }
2328 nr = ir * 4;
2329 kshift = 1;
2330 }
2331
2332 // Construct the pixel list
2333 if (take_all) {
2334 nir = ipix2 - ipix1 + 1;
2335 if (do_ring) {
2336 long ind = 0;
2337 for (long i = ipix1; i <= ipix2; i++) {
2338 res.add((int) ind, new Long(i));
2339 ind++;
2340 }
2341 } else {
2342 // in = ring2nest(nside, ipix1);
2343 // res.add(0, new Long( in));
2344 int ind = 0;
2345 for (int i = 0; i < nir; i++) {
2346 // inext = next_in_line_nest(nside, in);
2347 // in = inext;
2348 in = ring2nest(nside, ipix1 + i);
2349 res.add( i, new Long(in));
2350
2351 }
2352 }
2353
2354 return res;
2355 }
2356
2357 shift = kshift / 2.0;
2358
2359 // conservative : include every intersected pixel, even if the
2360 // pixel center is out of the [phi_low, phi_hi] region
2361 if (conservative) {
2362 ip_low = (long) Math.round((nr * phi_low) / TWOPI - shift);
2363 ip_hi = (long) Math.round((nr * phi_hi) / TWOPI - shift);
2364
2365 ip_low = (long) BitManipulation.MODULO(ip_low, nr); // in [0, nr - 1]
2366 ip_hi = (long) BitManipulation.MODULO(ip_hi, nr); // in [0, nr - 1]
2367 // System.out.println("ip_low="+ip_low+" ip_hi="+ip_hi);
2368 } else { // strict: includes only pixels whose center is in
2369 // [phi_low,phi_hi]
2370
2371 ip_low = (long) Math.ceil((nr * phi_low) / TWOPI - shift);
2372 ip_hi = (long) Math.floor((nr * phi_hi) / TWOPI - shift);
2373 if (ip_low == ip_hi + 1)
2374 ip_low = ip_hi;
2375
2376 if ((ip_low - ip_hi == 1) && (dphi * nr < PI)) {
2377 // the interval is too small ( and away from pixel center)
2378 // so no pixels is included in the list
2379 System.out
2380 .println("the interval is too small and avay from center");
2381 return res; // return empty list
2382 }
2383
2384 ip_low = Math.min(ip_low, nr - 1);
2385 ip_hi = Math.max(ip_hi, 0);
2386 }
2387
2388 //
2389 if (ip_low > ip_hi)
2390 to_top = true;
2391
2392 if (to_top) {
2393 ip_low += ipix1;
2394 ip_hi += ipix1;
2395 nir1 = ipix2 - ip_low + 1;
2396
2397 nir2 = ip_hi + 1;
2398 nir = nir1 + nir2;
2399 if (do_ring) {
2400 int ind = 0;
2401 for (long i = ip_low; i <= ipix2; i++) {
2402 res.add(ind, new Long(i));
2403 ind++;
2404 }
2405 // ind = nir1;
2406 for (long i = ipix1; i <= ip_hi; i++) {
2407 res.add(ind, new Long(i));
2408 ind++;
2409 }
2410 } else {
2411 int ind = 0;
2412 for (long i = ip_low; i <= ipix2; i++) {
2413 in = ring2nest(nside, i);
2414 res.add(ind, new Long(in));
2415 ind++;
2416 }
2417 for (long i = ipix1; i <= ip_hi; i++) {
2418 in = ring2nest(nside, i);
2419 res.add(ind, new Long(in));
2420 ind++;
2421 }
2422
2423 }
2424 } else {
2425 nir = ip_hi - ip_low + 1;
2426 // System.out.println("nir="+nir+" ip_low="+ip_low+" ip_hi="+ip_hi+" ipix1="+
2427 // ipix1+" ipix2="+ipix2);
2428 //
2429 // Special case when ip_low < 0
2430 //
2431 if (ip_low < 0 ){
2432 ip_low = Math.abs(ip_low) ;
2433 nir1 = ip_low;
2434 nir2 = ip_hi + 1;
2435 nir = nir1 + nir2;
2436 if (do_ring) {
2437 int ind = 0;
2438 for (long i = 0; i < ip_low; i++) {
2439 res.add(ind, new Long(ipix2 - i));
2440 ind++;
2441 }
2442 for (long i = 0; i <= ip_hi; i++) {
2443 res.add(ind, new Long(ipix1 + i));
2444 ind++;
2445 }
2446
2447 } else {
2448
2449 int ind = 0;
2450 for (int i = 0; i < ip_low; i++) {
2451 in = ring2nest(nside, ipix2 - i);
2452 res.add(ind, new Long(in));
2453 ind++;
2454 }
2455 for (long i = 0; i <= ip_hi; i++) {
2456 in = ring2nest(nside, ipix1 + i);
2457 res.add(ind, new Long(in));
2458 ind++;
2459 }
2460
2461 }
2462 // System.out.println("nir="+nir+" ip_low="+ip_low+" ip_hi="+ip_hi+" ipix1="+
2463 // ipix1+" ipix2="+ipix2);
2464 // for (int i=0; i< res.size(); i++) {
2465 // System.out.print(" "+ res.get(i));
2466 // }
2467 // System.out.println();
2468 return res;
2469
2470 }
2471 ip_low += ipix1;
2472 ip_hi += ipix1;
2473 if (do_ring) {
2474 int ind = 0;
2475 for (long i = ip_low; i <= ip_hi; i++) {
2476 res.add(ind, new Long(i));
2477 ind++;
2478 }
2479 } else {
2480 // in = ring2nest(nside, ip_low);
2481 // res.add(0, new Long(in));
2482 int ind = 0;
2483 for (long i = ip_low; i <= ip_hi; i++) {
2484 // inext = next_in_line_nest(nside, in);
2485 // in = inext;
2486 in = ring2nest(nside, i);
2487 res.add(ind, new Long(in));
2488 ind++;
2489 }
2490 }
2491
2492 }
2493
2494 return res;
2495 }
2496
2497 /**
2498 * calculates the pixel that lies on the East side (and the same
2499 * latitude) as the given NESTED pixel number - ipix
2500 *
2501 * @param nside
2502 * long resolution
2503 * @param ipix
2504 * long pixel number
2505 * @return long next pixel in line
2506 * @throws IllegalArgumentException
2507 */
2508 public long next_in_line_nest(long nside, long ipix) {
2509 long npix, ipf, ipo, ix, ixp, iy, iym, ixo, iyo, face_num, other_face;
2510 long ia, ib, ibp, nsidesq;
2511 long ibm, ib2;
2512 int icase;
2513 long local_magic1, local_magic2;
2514 long[] ixiy = new long[2];
2515 long inext = 0; // next in line pixel in Nest scheme
2516 String SID = "next_in_line:";
2517 if ((nside < 1) || (nside > ns_max)) {
2518 throw new IllegalArgumentException(SID + " nside should be power of 2 >0 and < "+ns_max);
2519 }
2520 nsidesq = nside * nside;
2521 npix = 12 * nsidesq; // total number of pixels
2522 if ((ipix < 0) || (ipix > npix - 1)) {
2523 throw new IllegalArgumentException(SID + " ipix out of range defined by nside");
2524 }
2525 // initiates array for (x,y) -> pixel number -> (x,y) mapping
2526 if (x2pix[xmax-1] <= 0)
2527 mk_xy2pix();
2528 local_magic1 = (nsidesq - 1) / 3;
2529 local_magic2 = 2 * local_magic1;
2530 face_num = ipix / nsidesq;
2531 ipf = (long) BitManipulation.MODULO(ipix, nsidesq); // Pixel number in face
2532 ixiy = pix2xy_nest(nside, ipf);
2533 ix = ixiy[0];
2534 iy = ixiy[1];
2535 ixp = ix + 1;
2536 iym = iy - 1;
2537 boolean sel = false;
2538 icase = -1; // iside the nest flag
2539 // Exclude corners
2540 if (ipf == local_magic2) { // west coirner
2541 inext = ipix - 1;
2542 return inext;
2543 }
2544 if ((ipf == nsidesq - 1) && !sel) { // North corner
2545 icase = 6;
2546 sel = true;
2547 }
2548 if ((ipf == 0) && !sel) { // Siuth corner
2549 icase = 7;
2550 sel = true;
2551 }
2552 if ((ipf == local_magic1) && !sel) { // East corner
2553 icase = 8;
2554 sel = true;
2555 }
2556 // Detect edges
2557 if (((ipf & local_magic1) == local_magic1) && !sel) { // North-East
2558 icase = 1;
2559 sel = true;
2560 }
2561 if (((ipf & local_magic2) == 0) && !sel) { // South-East
2562 icase = 4;
2563 sel = true;
2564 }
2565 if (!sel) { // iside a face
2566 inext = xy2pix_nest(nside, ixp, iym, face_num);
2567 return inext;
2568 }
2569 //
2570 ia = face_num / 4; // in [0,2]
2571 ib = (long) BitManipulation.MODULO(face_num, 4); // in [0,3]
2572 ibp = (long) BitManipulation.MODULO(ib + 1, 4);
2573 ibm = (long) BitManipulation.MODULO(ib + 4 - 1, 4);
2574 ib2 = (long) BitManipulation.MODULO(ib + 2, 4);
2575
2576 if (ia == 0) { // North pole region
2577 switch (icase) {
2578 case 1:
2579 other_face = 0 + ibp;
2580 ipo = (long) BitManipulation.MODULO(BitManipulation.swapLSBMSB( ipf), nsidesq);
2581 inext = other_face * nsidesq + ipo;
2582 break;
2583 case 4:
2584 other_face = 4 + ibp;
2585 ipo = (long) BitManipulation.MODULO(BitManipulation.invMSB( ipf), nsidesq); // SE-NW flip
2586
2587 ixiy = pix2xy_nest(nside, ipo);
2588 ixo = ixiy[0];
2589 iyo = ixiy[1];
2590
2591 inext = xy2pix_nest(nside, ixo + 1, iyo, other_face);
2592
2593 break;
2594 case 6: // North corner
2595 other_face = 0 + ibp;
2596 inext = other_face * nsidesq + nsidesq - 1;
2597 break;
2598 case 7:
2599 other_face = 4 + ibp;
2600 inext = other_face * nsidesq + local_magic2 + 1;
2601 break;
2602 case 8:
2603 other_face = 0 + ibp;
2604 inext = other_face * nsidesq + local_magic2;
2605 break;
2606 }
2607
2608 } else if (ia == 1) { // Equatorial region
2609 switch (icase) {
2610 case 1: // NorthEast edge
2611 other_face = 0 + ib;
2612 // System.out.println("ipf="+ipf+" nsidesq="+nsidesq+" invLSB="+BitManipulation.invLSB(ipf));
2613 ipo = (long) BitManipulation.MODULO((double)BitManipulation.invLSB( ipf), (double)nsidesq); // NE-SW flip
2614 // System.out.println(" ipo="+ipo);
2615
2616 ixiy = pix2xy_nest(nside, ipo);
2617 ixo = ixiy[0];
2618 iyo = ixiy[1];
2619 inext = xy2pix_nest(nside, ixo, iyo - 1, other_face);
2620 break;
2621 case 4: // SouthEast edge
2622 other_face = 8 + ib;
2623 ipo = (long) BitManipulation.MODULO(BitManipulation.invMSB( ipf), nsidesq);
2624 ixiy = pix2xy_nest(nside, ipo);
2625 inext = xy2pix_nest(nside, ixiy[0] + 1, ixiy[1], other_face);
2626 break;
2627 case 6: // Northy corner
2628 other_face = 0 + ib;
2629 inext = other_face * nsidesq + local_magic2 - 2;
2630 break;
2631 case 7: // South corner
2632 other_face = 8 + ib;
2633 inext = other_face * nsidesq + local_magic2 + 1;
2634 break;
2635 case 8: // East corner
2636 other_face = 4 + ibp;
2637 inext = other_face * nsidesq + local_magic2;
2638 break;
2639
2640 }
2641 } else { // South pole region
2642 switch (icase) {
2643 case 1: // NorthEast edge
2644 other_face = 4 + ibp;
2645 ipo = (long) BitManipulation.MODULO(BitManipulation.invLSB( ipf), nsidesq); // NE-SW flip
2646 ixiy = pix2xy_nest(nside, ipo);
2647 inext = xy2pix_nest(nside, ixiy[0], ixiy[1] - 1, other_face);
2648 break;
2649 case 4: // SouthEast edge
2650 other_face = 8 + ibp;
2651 ipo = (long) BitManipulation.MODULO(BitManipulation.swapLSBMSB( ipf), nsidesq); // E-W flip
2652 inext = other_face * nsidesq + ipo; // (8)
2653 break;
2654 case 6: // North corner
2655 other_face = 4 + ibp;
2656 inext = other_face * nsidesq + local_magic2 - 2;
2657 break;
2658 case 7: // South corner
2659 other_face = 8 + ibp;
2660 inext = other_face * nsidesq;
2661 break;
2662 case 8: // East corner
2663 other_face = 8 + ibp;
2664 inext = other_face * nsidesq + local_magic2;
2665 break;
2666 }
2667 }
2668 return inext;
2669 }
2670
2671 /**
2672 * renders the pixel number pix (NESTED scheme) for a pixel which contains a
2673 * point on a sphere at coordinate vector (x,y,z), given the map resolution
2674 * parameter nside.
2675 *
2676 * The computation is made to the highest resolution available (nside=ns_max)
2677 * and then degraded to that requared (by Integer division) this doesn't
2678 * cost much, and it makes sure that the treatment of round-off will be
2679 * consistent for every resolution
2680 *
2681 * @param nside
2682 * long the map resolution
2683 * @param vector
2684 * Vewctor3d the input vector
2685 * @return pixel long
2686 * @throws IllegalArgumentException
2687 */
2688 public long vect2pix_nest(long nside, Vector3d vector) {
2689 long pix = 0;
2690 double z, za, tt, tp, tmp, dnorm, phi;
2691 long jp, jm, ifp, ifm, face_num, ix, iy, ix_low, ix_hi;
2692 long iy_low, iy_hi, ipf, ntt;
2693 String SID = " vect2pix_nest:";
2694 /* */
2695 if (nside < 1 || nside > ns_max) {
2696 throw new IllegalArgumentException(SID + " nside should be power of 2 >0 and < ns_max");
2697 }
2698 if (x2pix[xmax-1] <= 0)
2699 mk_xy2pix();
2700
2701 dnorm = vector.length();
2702 z = vector.z / dnorm;
2703 phi = 0.; // phi in [-pi,pi]
2704 if (vector.x != 0. || vector.y != 0.)
2705 phi = Math.atan2(vector.y, vector.x);
2706 za = Math.abs(z);
2707 if (phi < 0.)
2708 phi += TWOPI; // phi in [0,2pi]
2709 tt = 2. * phi / PI; // in [0,4]
2710 if (za <= twothird) { // Equatorial region
2711 /*
2712 * the index of edge lines increases when the longitude = phi goes
2713 * up
2714 */
2715 jp = (long) (ns_max * (0.5 + tt - z * 0.75)); // ascending edge line
2716 // index
2717 jm = (long) (ns_max * (0.5 + tt + z * 0.75)); // descending edge line
2718 // index
2719 /* find the face */
2720 ifp = jp / ns_max; // in [0,4]
2721 ifm = jm / ns_max;
2722 if (ifp == ifm) { // faces 4 to 7
2723 face_num = (long) BitManipulation.MODULO(ifp, 4) + 4;
2724 } else if (ifp < ifm) { // (half-) faces 0 to 3
2725 face_num = (long) BitManipulation.MODULO(ifp, 4);
2726 } else { // (half-) faces 8 to 11
2727 face_num = (long) BitManipulation.MODULO(ifm, 4) + 8;
2728 }
2729
2730 ix = (long) BitManipulation.MODULO(jm, ns_max);
2731 iy = ns_max - (long) BitManipulation.MODULO(jp, ns_max) - 1;
2732 } else { // polar region, za > 2/3
2733 ntt = (long) tt;
2734 if (ntt >= 4)
2735 ntt = 3;
2736 tp = tt - ntt;
2737 tmp = Math.sqrt(3.0 * (1.0 - za)); // in [0,1]
2738 /*
2739 * the index of edge lines increases when distance from the closest
2740 * pole goes up
2741 */
2742 jp = (long) (ns_max * tp * tmp); // line going toward the pole has
2743 // phi increases
2744 jm = (long) (ns_max * (1.0 - tp) * tmp); // that one goes away of the
2745 // closest pole
2746 jp = Math.min(ns_max - 1, jp); // for points too close to the
2747 // boundary
2748 jm = Math.min(ns_max - 1, jm);
2749 /* finds the face and pixel's (x,y) */
2750 if (z >= 0) {
2751 face_num = ntt; // in [0,3]
2752 ix = ns_max - jm - 1;
2753 iy = ns_max - jp - 1;
2754 } else {
2755 face_num = ntt + 8; // in [8,11]
2756 ix = jp;
2757 iy = jm;
2758 }
2759 }
2760 ix_low = (long) BitManipulation.MODULO(ix, xmax);
2761 ix_hi = ix / xmax;
2762 iy_low = (long) BitManipulation.MODULO(iy, xmax);
2763 iy_hi = iy / xmax;
2764
2765 ipf = (x2pix[(int) (ix_hi + 1)] + y2pix[(int) (iy_hi + 1)]) * (xmax * xmax)
2766 + (x2pix[(int) (ix_low + 1)] + y2pix[(int) (iy_low + 1)]);
2767 ipf = ipf / ((ns_max / nside) * (ns_max / nside)); // in [0, nside**2
2768 // -1]
2769 pix = ipf + face_num * nside * nside; // in [0, 12*nside**2 -1]
2770 return pix;
2771 }
2772
2773 /**
2774 * gives the pixel number ipix (NESTED) corresponding to ix, iy and face_num
2775 *
2776 * @param nside
2777 * the map resolution parameter
2778 * @param ix
2779 * Integer x coordinate
2780 * @param iy
2781 * Integer y coordinate
2782 * @param face_num
2783 * long face number
2784 * @return long pixel number ipix
2785 * @throws IllegalArgumentException
2786 */
2787 private long xy2pix_nest(long nside, long ix, long iy, long face_num){
2788 long res = 0;
2789 long ix_low, ix_hi, iy_low, iy_hi, ipf;
2790 String SID = "xy2pix_nest:";
2791 //
2792 if ((nside < 1) || (nside > ns_max)) {
2793 throw new IllegalArgumentException(SID + " nside should be power of 2 >0 and < "+ns_max);
2794 }
2795 // if ((ix < 0) || (ix > nside - 1)) {
2796 // throw new IllegalArgumentException(SID + " ix out of range [0, nside-1]");
2797 // }
2798 // if ((iy < 0) || (iy > nside - 1)) {
2799 // throw new IllegalArgumentException(SID + " iy out of range [0, nside-1]");
2800 // }
2801 if (x2pix[xmax-1] <= 0)
2802 mk_xy2pix();
2803 ix_low = (long) BitManipulation.MODULO(ix, xmax);
2804 ix_hi = ix / xmax;
2805 iy_low = (long) BitManipulation.MODULO(iy, xmax);
2806 iy_hi = iy / xmax;
2807
2808 ipf = (x2pix[(int) (ix_hi + 1)] + y2pix[(int) (iy_hi + 1)]) * xmax * xmax
2809 + (x2pix[(int) (ix_low + 1)] + y2pix[(int) (iy_low + 1)]);
2810 res = ipf + face_num * nside * nside; // in [0, 12*nside^2 - 1]
2811
2812 return res;
2813 }
2814
2815 /**
2816 * gives the x,y coordinates in a face from pixel number within the face
2817 * (NESTED) schema.
2818 *
2819 * @param nside
2820 * long resolution parameter
2821 * @param ipf
2822 * long pixel number
2823 * @return ixiy long[] contains x and y coordinates
2824 * @throws IllegalArgumentException
2825 */
2826 private long[] pix2xy_nest(long nside, long ipf) {
2827 long[] ixiy = { 0, 0 };
2828 long ip_low, ip_trunc, ip_med, ip_hi;
2829 String SID = "pix2xy_nest:";
2830 // System.out.println(" ipf="+ipf+" nside="+nside);
2831 if ((nside < 1) || (nside > ns_max)) {
2832 throw new IllegalArgumentException(SID + " nside should be power of 2 >0 and < "+ns_max);
2833 }
2834 if ((ipf < 0) || (ipf > nside * nside - 1)) {
2835 throw new IllegalArgumentException(SID + " ipix out of range defined by nside");
2836 }
2837 if (pix2x[pixmax] <= 0)
2838 mk_pix2xy();
2839 ip_low = (long) BitManipulation.MODULO(ipf, pixmax); // contents of last 15 bits
2840 ip_trunc = ipf / pixmax; // truncation of the last 15 bits
2841 ip_med = (long) BitManipulation.MODULO(ip_trunc, pixmax); // next 15 bits
2842 ip_hi = ip_trunc / pixmax; // select high 15 bits
2843
2844 long ix = pixmax * pix2x[(int) ip_hi] + xmid * pix2x[(int) ip_med] + pix2x[(int) ip_low];
2845 long iy = pixmax * pix2y[(int) ip_hi] + xmid * pix2y[(int) ip_med] + pix2y[(int) ip_low];
2846 ixiy[0] = ix;
2847 ixiy[1] = iy;
2848 return ixiy;
2849 }
2850
2851 /**
2852 * creates an array of pixel numbers pix2x from x and y coordinates in the
2853 * face. Suppose NESTED scheme of pixel ordering Bits corresponding to x and
2854 * y are interleaved in the pixel number in even and odd bits.
2855 */
2856 private void mk_pix2xy() {
2857 long kpix, jpix, ix, iy, ip, id;
2858 boolean flag = true;
2859 for (kpix = 0; kpix <= pixmax; kpix++) { // loop on pixel numbers
2860 jpix = kpix;
2861 ix = 0;
2862 iy = 0;
2863 ip = 1; // bit position in x and y
2864
2865 while (jpix != 0) { // go through all the bits
2866
2867 id = (long) BitManipulation.MODULO(jpix, 2); // bit value (in kpix), goes in x
2868 jpix /= 2;
2869 ix += id * ip;
2870
2871 id = (long) BitManipulation.MODULO(jpix, 2); // bit value, goes in iy
2872 jpix /= 2;
2873 iy += id * ip;
2874
2875 ip *= 2; // next bit (in x and y )
2876 }
2877
2878 pix2x[(int) kpix] = ix; // in [0,pixmax]
2879 pix2y[(int) kpix] = iy; // in [0,pixmax]
2880
2881
2882 }
2883 }
2884
2885 /**
2886 * converts pixel number from ring numbering schema to the nested one
2887 *
2888 * @param nside
2889 * long resolution
2890 * @param ipring long pixel number in ring schema
2891 * @return long pixel number in nest schema
2892 * @throws IllegalArgumentException
2893 */
2894 public long ring2nest(long nside, long ipring) {
2895 long ipnest = 0;
2896 double fihip;
2897 double hip;
2898 long npix, nl2, nl4, ncap, ip, iphi, ipt, ipring1, kshift, face_num;
2899 long nr, irn, ire, irm, irs, irt, ifm, ifp, ix, iy, ix_low, ix_hi, iy_low;
2900 long iy_hi, ipf;
2901 long[] jrll = { 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4 }; // in units of
2902 // nside
2903 long[] jpll = { 0, 1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7 }; // in units of
2904 // nside/2
2905 String SID = "ring2nest:";
2906 //
2907 face_num = 0;
2908 if ((nside < 1) || (nside > ns_max)) {
2909 throw new IllegalArgumentException(SID + " nside should be power of 2 >0 and < "+ns_max);
2910 }
2911 npix = 12 * nside * nside; // total number of points
2912
2913 if ((ipring < 0) || (ipring > npix - 1)) {
2914 throw new IllegalArgumentException(SID + " ipring out of range [0,npix-1]");
2915 }
2916 if (x2pix[xmax-1] <= 0)
2917 mk_xy2pix();
2918
2919 nl2 = 2 * nside;
2920 nl4 = 4 * nside;
2921 ncap = nl2 * (nside - 1); // points in each polar cap, =0 for nside = 1
2922 ipring1 = ipring + 1;
2923 // finds the ring number, the position of the ring and the face number
2924 if (ipring1 <= ncap) { // north polar cap
2925 hip = ipring1 / 2.0;
2926 fihip = Math.floor(hip);
2927 irn = (long)Math.floor( Math.sqrt(hip - Math.sqrt(fihip))) + 1; // counted from
2928 // north pole
2929 iphi = ipring1 - 2 * irn * (irn - 1);
2930
2931 kshift = 0;
2932 nr = irn; // 1/4 of the number of points on the current ring
2933 face_num = (iphi - 1) / irn; // in [0,3 ]
2934
2935 } else if (ipring1 <= nl2 * (5 * nside + 1)) { // equatorial region
2936 ip = ipring1 - ncap - 1;
2937 irn = (long)Math.floor(ip / nl4) + nside; // counted from north pole
2938 iphi = (long) BitManipulation.MODULO(ip, nl4) + 1;
2939
2940 kshift = (long) BitManipulation.MODULO(irn + nside, 2); // 1 if odd 0
2941 // otherwise
2942 nr = nside;
2943 ire = irn - nside + 1; // in [1, 2*nside+1]
2944 irm = nl2 + 2 - ire;
2945 ifm = (iphi - ire / 2 + nside - 1) / nside; // face boundary
2946 ifp = (iphi - irm / 2 + nside - 1) / nside;
2947 if (ifp == ifm) {
2948 face_num = (long) BitManipulation.MODULO(ifp, 4.) + 4;
2949 } else if (ifp + 1 == ifm) { // (half-) faces 0 to 3
2950 face_num = ifp;
2951 } else if (ifp - 1 == ifm) { // (half-) faces 8 to 11
2952 face_num = ifp + 7;
2953 }
2954
2955
2956 } else { // south polar cap
2957
2958 ip = npix - ipring1 + 1;
2959 hip = ip / 2.0;
2960 fihip = Math.floor(hip);
2961 irs = (long)Math.floor( Math.sqrt(hip - Math.sqrt(fihip))) + 1;
2962 iphi = 4 * irs + 1 - (ip - 2 * irs * (irs - 1));
2963 kshift = 0;
2964 nr = irs;
2965 irn = nl4 - irs;
2966 face_num = (iphi - 1) / irs + 8; // in [8,11]
2967
2968
2969 }
2970 // finds the (x,y) on the face
2971
2972
2973 //
2974 irt = irn - jrll[(int) (face_num + 1)] * nside + 1; // in [-nside+1,0]
2975 ipt = 2 * iphi - jpll[(int) (face_num + 1)] * nr - kshift - 1; // in [-nside+1,
2976 // nside-1]
2977 //
2978 if (ipt >= nl2){
2979 ipt = ipt - 8*nside; // for the face #4
2980 }
2981 ix = (ipt - irt) / 2;
2982 iy = -(ipt + irt) / 2;
2983
2984 ix_low = (long) BitManipulation.MODULO(ix, xmax);
2985 ix_hi = ix / xmax;
2986 iy_low = (long) BitManipulation.MODULO(iy, xmax);
2987 iy_hi = iy / xmax;
2988
2989 //
2990
2991 ipf = (x2pix[(int) (ix_hi + 1)] + y2pix[(int) (iy_hi + 1)]) * xmax * xmax
2992 + (x2pix[(int) (ix_low + 1)] + y2pix[(int) (iy_low + 1)]); // in [0, nside**2 -1]
2993 ipnest = ipf + face_num * nside * nside; // in [0, 12*nside**2 -1]
2994
2995 return ipnest;
2996
2997 }
2998
2999 /**
3000 * converts from NESTED to RING pixel numbering
3001 *
3002 * @param nside
3003 * long resolution
3004 * @param ipnest
3005 * long NEST pixel number
3006 * @return ipring long RING pixel number
3007 * @throws IllegalArgumentException
3008 */
3009 public long nest2ring(long nside, long ipnest) {
3010 long res = 0;
3011 long npix, npface, face_num, ncap, n_before, ipf, ip_low, ip_trunc;
3012 long ip_med, ip_hi, ix, iy, jrt, jr, nr, jpt, jp, kshift, nl4;
3013 // long[] ixiy = { 0, 0 };
3014 // coordinates of lowest corner of each face
3015 long[] jrll = { 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4 }; // in units of
3016 // nside
3017 long[] jpll = { 0, 1, 3, 5, 7, 0, 2, 4, 6, 1, 3, 5, 7 }; // in units of
3018 // nside/2
3019 String SID = "nest2ring:";
3020 //
3021 if ((nside < 1) || (nside > ns_max)) {
3022 throw new IllegalArgumentException(SID + " nside should be power of 2 >0 and < ns_max");
3023 }
3024 npix = 12 * nside * nside;
3025 if ((ipnest < 0) || (ipnest > npix - 1)) {
3026 throw new IllegalArgumentException(SID + " ipnest out of range [0,npix-1]");
3027 }
3028 if (pix2x[pixmax-1] <= 0)
3029 mk_pix2xy();
3030 ncap = 2 * nside * (nside - 1); // number of points in the North polar
3031 // cap
3032 nl4 = 4 * nside;
3033 // finds the face and the number in the face
3034 npface = nside * nside;
3035
3036 face_num = ipnest / npface; // face number in [0,11]
3037 if (ipnest >= npface) {
3038 ipf = (long) BitManipulation.MODULO(ipnest, npface); // pixel number in the face
3039 } else {
3040 ipf = ipnest;
3041 }
3042
3043 // finds the x,y on the face
3044 // from the pixel number
3045 ip_low = (long) BitManipulation.MODULO(ipf, pixmax); // last 15 bits
3046 if (ip_low < 0)
3047 ip_low = -ip_low;
3048
3049 ip_trunc = ipf / pixmax; // truncate last 15 bits
3050 ip_med = (long) BitManipulation.MODULO(ip_trunc, pixmax); // next 15 bits
3051 if (ip_med < 0)
3052 ip_med = -ip_med;
3053 ip_hi = ip_trunc / pixmax; // high 15 bits
3054
3055 ix = pixmax * pix2x[(int) ip_hi] + xmid * pix2x[(int) ip_med] + pix2x[(int) ip_low];
3056 iy = pixmax * pix2y[(int) ip_hi] + xmid * pix2y[(int) ip_med] + pix2y[(int) ip_low];
3057
3058 // transform this in (horizontal, vertical) coordinates
3059 jrt = ix + iy; // vertical in [0,2*(nside -1)]
3060 jpt = ix - iy; // horizontal in [-nside+1, nside - 1]
3061 // calculate the z coordinate on the sphere
3062 jr = jrll[(int) (face_num + 1)] * nside - jrt - 1; // ring number in [1,4*nside
3063 // -1]
3064 nr = nside; // equatorial region (the most frequent)
3065 n_before = ncap + nl4 * (jr - nside);
3066 kshift = (long) BitManipulation.MODULO(jr - nside, 2);
3067 if (jr < nside) { // north pole region
3068 nr = jr;
3069 n_before = 2 * nr * (nr - 1);
3070 kshift = 0;
3071 } else if (jr > 3 * nside) { // south pole region
3072 nr = nl4 - jr;
3073 n_before = npix - 2 * (nr + 1) * nr;
3074 kshift = 0;
3075 }
3076 // computes the phi coordinate on the sphere in [0,2pi]
3077 jp = (jpll[(int) (face_num + 1)] * nr + jpt + 1 + kshift) / 2; // 'phi' number
3078 // in ring
3079 // [1,4*nr]
3080 if (jp > nl4)
3081 jp -= nl4;
3082 if (jp < 1)
3083 jp += nl4;
3084 res = n_before + jp - 1; // in [0, npix-1]
3085 return res;
3086 }
3087
3088 /**
3089 * fills arrays x2pix and y2pix giving the number of the pixel laying in
3090 * (x,y). x and y are in [1,512] the pixel number is in [0, 512**2 -1]
3091 *
3092 * 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
3093 * [0,512**2 -1]
3094 *
3095 */
3096 private static void mk_xy2pix() {
3097 long k, ip, id;
3098
3099 for (int i = 1; i <= xmax; i++) {
3100 long j = i - 1;
3101 k = 0;
3102 ip = 1;
3103 while (j != 0) {
3104 id = (long) BitManipulation.MODULO(j, 2);
3105 j /= 2;
3106 k += ip * id;
3107 ip *= 4;
3108 }
3109 x2pix[i] = k;
3110 y2pix[i] = 2 * k;
3111
3112 }
3113
3114 }
3115
3116 /**
3117 * returns the ring number in {1, 4*nside - 1} calculated from z coordinate
3118 *
3119 * @param nside
3120 * long resolution
3121 * @param z
3122 * double z coordinate
3123 * @return long ring number
3124 */
3125 public long RingNum(long nside, double z) {
3126 long iring = 0;
3127 /* equatorial region */
3128
3129 iring = (long) Math.round(nside * (2.0 - 1.5 * z));
3130 /* north cap */
3131 if (z > twothird) {
3132 iring = (long) Math.round(nside * Math.sqrt(3.0 * (1.0 - z)));
3133 if (iring == 0)
3134 iring = 1;
3135 }
3136 /* south cap */
3137 if (z < -twothird) {
3138 iring = (long) Math.round(nside * Math.sqrt(3.0 * (1.0 + z)));
3139 if (iring == 0)
3140 iring = 1;
3141 iring = 4 * nside - iring;
3142 }
3143 return iring;
3144 }
3145
3146 /**
3147 * calculates vector corresponding to angles theta (co-latitude
3148 * measured from North pole, in [0,pi] radians) phi (longitude measured
3149 * eastward in [0,2pi] radians) North pole is (x,y,z) = (0, 0, 1)
3150 *
3151 * @param theta double
3152 * @param phi double
3153 * @return Vector3d
3154 * @throws IllegalArgumentException
3155 */
3156 public Vector3d Ang2Vec(double theta, double phi) {
3157 double PI = Math.PI;
3158 String SID = "Ang2Vec:";
3159 Vector3d v;
3160 if ((theta < 0.0) || (theta > PI)) {
3161 throw new IllegalArgumentException(SID + " theta out of range [0.,PI]");
3162 }
3163 double stheta = Math.sin(theta);
3164 double x = stheta * Math.cos(phi);
3165 double y = stheta * Math.sin(phi);
3166 double z = Math.cos(theta);
3167 v = new Vector3d(x, y, z);
3168 return v;
3169 }
3170
3171 /**
3172 * converts a Vector3d in a tuple of angles tup[0] = theta
3173 * co-latitude measured from North pole, in [0,PI] radians, tup[1] = phi
3174 * longitude measured eastward, in [0,2PI] radians
3175 *
3176 * @param v
3177 * Vector3d
3178 * @return double[] out_tup out_tup[0] = theta out_tup[1] = phi
3179 */
3180 public double[] Vect2Ang(Vector3d v) {
3181 double[] out_tup = new double[2];
3182 double norm = v.length();
3183 double z = v.z / norm;
3184 double theta = Math.acos(z);
3185 double phi = 0.;
3186 if ((v.x != 0.) || (v.y != 0)) {
3187 phi = Math.atan2(v.y, v.x); // phi in [-pi,pi]
3188 }
3189 if (phi < 0)
3190 phi += 2.0 * Math.PI; // phi in [0, 2pi]
3191 // phi += Math.PI;
3192 out_tup[0] = theta;
3193 out_tup[1] = phi;
3194 return out_tup;
3195 }
3196
3197 /**
3198 * returns nside such that npix = 12*nside^2, nside should be
3199 * power of 2 and smaller than ns_max if not return -1
3200 *
3201 * @param npix
3202 * long the number of pixels in the map
3203 * @return long nside the map resolution parameter
3204 */
3205 public long Npix2Nside(long npix) {
3206 long nside = 0;
3207 long npixmax = 12 *(long) ns_max *(long) ns_max;
3208
3209 String SID = "Npix2Nside:";
3210 nside = (long) Math.rint(Math.sqrt(npix / 12));
3211 if (npix < 12) {
3212 throw new IllegalArgumentException(SID + " npix is too small should be > 12");
3213 }
3214 if (npix > npixmax) {
3215 throw new IllegalArgumentException(SID + " npix is too large > 12 * ns_max^2");
3216 }
3217 double fnpix = 12.0 * nside * nside;
3218 if (Math.abs(fnpix - npix) > 1.0e-2) {
3219 throw new IllegalArgumentException(SID + " npix is not 12*nside*nside");
3220 }
3221 double flog = Math.log((double) nside) / Math.log(2.0);
3222 double ilog = Math.rint(flog);
3223 if (Math.abs(flog - ilog) > 1.0e-6) {
3224 throw new IllegalArgumentException(SID + " nside is not power of 2");
3225 }
3226 return nside;
3227 }
3228
3229 /**
3230 * calculates npix such that npix = 12*nside^2 ,nside should be
3231 * a power of 2, and smaller than ns_max otherwise return -1
3232 *
3233 * @param nside
3234 * long the map resolution
3235 * @return npix long the number of pixels in the map
3236 */
3237 public long Nside2Npix(long nside) {
3238
3239 long[] nsidelist = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
3240 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576,
3241 2097152, 4194304};
3242
3243 long res = 0;
3244 String SID = "Nside2Npix:";
3245 if (Arrays.binarySearch(nsidelist, nside) < 0) {
3246 throw new IllegalArgumentException(SID + " nside should be >0, power of 2, <"+ns_max);
3247 }
3248 res = 12 * nside * nside;
3249 return res;
3250 }
3251
3252 /**
3253 * calculates the surface of spherical triangle defined by
3254 * vertices v1,v2,v3 Algorithm: finds triangle sides and uses l'Huilier
3255 * formula to compute "spherical excess" = surface area of triangle on a
3256 * sphere of radius one see, eg Bronshtein, Semendyayev Eq 2.86 half
3257 * perimeter hp = 0.5*(side1+side2+side3) l'Huilier formula x0 = tan( hp/2.)
3258 * x1 = tan((hp - side1)/2.) x2 = tan((hp - side2)/2.) x3 = tan((hp -
3259 * side3)/2.)
3260 *
3261 * @param v1 Vector3d
3262 * @param v2 Vector3d
3263 * @param v3 Vector3d vertices of the triangle
3264 * @return double the triangle surface
3265 * @throws Exception
3266 *
3267 */
3268 public double SurfaceTriangle(Vector3d v1, Vector3d v2, Vector3d v3)
3269 throws Exception {
3270 double res = 0.;
3271 double side1 = AngDist(v2, v3) / 4.0;
3272 double side2 = AngDist(v3, v1) / 4.0;
3273 double side3 = AngDist(v1, v2) / 4.0;
3274 double x0 = Math.tan(side1 + side2 + side3);
3275 double x1 = Math.tan(side2 + side3 - side1);
3276 double x2 = Math.tan(side1 + side3 - side2);
3277 double x3 = Math.tan(side1 + side2 - side3);
3278 res = 4.0 * Math.atan(Math.sqrt(x0 * x1 * x2 * x3));
3279
3280 return res;
3281 }
3282
3283 /**
3284 * calculates angular distance (in radians) between 2 Vectors
3285 * v1 and v2 In general dist = acos(v1.v2) except if the vectors are almost
3286 * aligned
3287 *
3288 * @param v1 Vector3d
3289 * @param v2 Vector3d
3290 * @return double dist
3291 * @throws Exception
3292 */
3293 public double AngDist(Vector3d v1, Vector3d v2) throws Exception {
3294 double dist = 0.;
3295 double aligned = 0.999;
3296 /* Normalize both vectors */
3297 Vector3d r1 = v1;
3298 Vector3d r2 = v2;
3299 r1.normalize();
3300 r2.normalize();
3301 double sprod = r1.dot(r2);
3302 /* This takes care about the bug in vecmath method from java3d project */
3303 if (sprod > aligned) { // almost aligned
3304 r1.sub(r2);
3305 double diff = r1.length();
3306 dist = 2.0 * Math.asin(diff / 2.0);
3307
3308 } else if (sprod < -aligned) {
3309 r1.add(r2);
3310 double diff = r1.length();
3311 dist = Math.PI - 2.0 * Math.asin(diff / 2.0);
3312 } else {
3313 dist = r1.angle(r2);
3314 }
3315 return dist;
3316 }
3317
3318 /**
3319 * calculates a vector production of two vectors.
3320 *
3321 * @param v1
3322 * Vectror containing 3 elements of Number type
3323 * @param v2
3324 * Vector containing 3 elements of Number type
3325 * @return Vector of 3 Objects of Double type
3326 * @throws Exception
3327 */
3328 public Vector VectProd(Vector v1, Vector v2) throws Exception {
3329 Vector res = new Vector();
3330 //
3331 double[] v1_element = new double[3];
3332 double[] v2_element = new double[3];
3333 for (int i = 0; i < 3; i++) {
3334 if (v1.get(i).getClass().isInstance(Number.class)) {
3335 v1_element[i] = ((Number) v1.get(i)).doubleValue();
3336 } else {
3337 throw new Exception();
3338 }
3339 if (v2.get(i).getClass().isInstance(Number.class)) {
3340 v2_element[i] = ((Number) v2.get(i)).doubleValue();
3341 } else {
3342 throw new Exception();
3343 }
3344
3345 }
3346
3347 Double value = new Double(v1_element[1] * v2_element[2] - v1_element[2]
3348 * v2_element[1]);
3349 res.add( value);
3350 value = new Double(v1_element[1] * v2_element[2] - v1_element[2]
3351 * v2_element[1]);
3352 res.add( value);
3353 value = new Double(v1_element[1] * v2_element[2] - v1_element[2]
3354 * v2_element[1]);
3355 res.add( value);
3356 return res;
3357 }
3358
3359 /**
3360 * calculates a dot product (inner product) of two 3D vectors
3361 * the result is double
3362 *
3363 * @param v1
3364 * 3d Vector of Number Objects (Double, long .. )
3365 * @param v2
3366 * 3d Vector
3367 * @return double
3368 * @throws Exception
3369 */
3370 public double dotProduct(Vector3d v1, Vector3d v2) throws Exception {
3371
3372 double prod = v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
3373
3374 return prod;
3375 }
3376
3377 /**
3378 * calculate cross product of two vectors
3379 *
3380 * @param v1
3381 * Vector3d
3382 * @param v2
3383 * Vector3d
3384 * @return Vector3d result of the product
3385 */
3386 public Vector3d crossProduct(Vector3d v1, Vector3d v2) {
3387 Vector3d res = new Vector3d(0., 0., 0.);
3388 double x = v1.y * v2.z - v1.z * v2.y;
3389 double y = v1.z * v2.x - v1.x * v2.z;
3390 double z = v1.x * v2.y - v1.y * v2.x;
3391 res.x = x;
3392 res.y = y;
3393 res.z = z;
3394 return res;
3395 }
3396 /**
3397 * calculates angular resolution of the pixel map
3398 * in arc seconds.
3399 * @param nside
3400 * @return double resolution in arcsec
3401 */
3402 public double PixRes(long nside) {
3403 double res = 0.;
3404 double degrad = Math.toDegrees(1.0);
3405 double skyArea = 4.*PI*degrad*degrad; // 4PI steredian in deg^2
3406 double arcSecArea = skyArea*3600.*3600.; // 4PI steredian in (arcSec^2)
3407 long npixels = 12*nside*nside;
3408 res = arcSecArea/npixels; // area per pixel
3409 res = Math.sqrt(res); // angular size of the pixel arcsec
3410 return res;
3411 }
3412 /**
3413 * calculate requared nside given pixel size in arcsec
3414 * @param pixsize in arcsec
3415 * @return long nside parameter
3416 */
3417 public long GetNSide(double pixsize) {
3418 long[] nsidelist = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
3419 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576 };
3420 long res = 0;
3421 double pixelArea = pixsize*pixsize;
3422 double degrad = Math.toDegrees(1.);
3423 double skyArea = 4.*PI*degrad*degrad*3600.*3600.;
3424 long npixels = (long) (skyArea/pixelArea);
3425 long nsidesq = npixels/12;
3426 long nside_req = (long) Math.sqrt(nsidesq);
3427 long mindiff = ns_max;
3428 int indmin = 0;
3429 for (int i=0; i<nsidelist.length; i++) {
3430 if (Math.abs(nside_req - nsidelist[i]) <= mindiff) {
3431 mindiff = Math.abs(nside_req - nsidelist[i]);
3432 res = nsidelist[i];
3433 indmin = i;
3434 }
3435 if ((nside_req > res) && (nside_req < ns_max)) res = nsidelist[indmin+1];
3436 if (nside_req > ns_max ) {
3437 System.out.println("nside cannot be bigger than "+ns_max);
3438 return ns_max;
3439 }
3440
3441 }
3442 return res;
3443 }
3444 /**
3445 * returns polar coordinates in radians given ra, dec in degrees
3446 * @param radec double array containing ra,dec in degrees
3447 * @return res double array containing theta and phi in radians
3448 * res[0] = theta res[1] = phi
3449 */
3450 public double[] RaDecToPolar(double[] radec) {
3451 double[] res = {0.0,0.0};
3452
3453 double ra = radec[0];
3454 double dec = radec[1];
3455 double theta = PI/2. - Math.toRadians(dec);
3456 double phi = Math.toRadians(ra);
3457 res[0] = theta;
3458 res[1] = phi;
3459
3460 return res;
3461 }
3462 /**
3463 * returns ra, dec in degrees given polar coordinates in radians
3464 * @param polar double array polar[0] = phi in radians
3465 * polar[1] = theta in radians
3466 * @return double array radec radec[0] = ra in degrees
3467 * radec[1] = dec in degrees
3468 */
3469 public double[] PolarToRaDec(double[] polar) {
3470 double[] radec = {0.0,0.0};
3471 double phi = polar[1];
3472 double theta = polar[0];
3473 double dec = Math.toDegrees(PI/2. - theta);
3474 double ra = Math.toDegrees(phi);
3475 radec[0] = ra;
3476 radec[1] = dec;
3477
3478 return radec;
3479 }
3480
3481 /**
3482 * returns polar coordinates of a point on unit sphere given Cartesian coordinates
3483 * @param x - Cartesian coordinate x of a point on unit sphere
3484 * @param y - y coordinate
3485 * @param z - z coordinate
3486 * @return double [] theta,phi
3487 */
3488 public double[] xyzToPolar(double x, double y, double z) {
3489 double[] res;
3490 Vector3d vv = new Vector3d(x,y,z);
3491 res = Vect2Ang(vv);
3492 return res;
3493 }
3494
3495 /**
3496 * Returns singleton instance.
3497 */
3498 public static PixTools getInstance() {
3499 return pixTools;
3500 }
3501 }
0 package gov.fnal.eag.healpix;
1
2 import java.util.ArrayList;
3
4 import javax.vecmath.Vector3d;
5
6
7 /**
8 * @author kuropat
9 * The class represents square region on a sphere defined by raMin, raMax
10 * decMin,decMax in degrees. The dec varies from Pi/2 to -Pi/2.
11 * The ra varies from 0. inclusive to 2Pi exclusive. Negative value of ra
12 * will be corrected by adding 2Pi
13 *
14 */
15 public class Region {
16 private static final double TWOPI = 2.0*Math.PI;
17 private double raMin;
18 private double raMax;
19 private double decMin;
20 private double decMax;
21 private double tetMin;
22 private double tetMax;
23 private double phiMin;
24 private double phiMax;
25 private static final double epsilon = 1.0e-10;
26 // private boolean normalized = false;
27 private ArrayList vertices;
28 private double PI = Math.PI;
29 private BitManipulation bm;
30 /**
31 * default constructor
32 * @param xMin ra min in degrees
33 * @param xMax ra max in degrees
34 * @param yMin dec min in degrees
35 * @param yMax dec max in degrees
36 */
37 public Region(double xMin, double xMax, double yMin, double yMax) {
38 super();
39 bm = new BitManipulation();
40 this.raMin = xMin;
41 this.raMax = xMax;
42 this.phiMin = Math.toRadians(raMin);
43 this.phiMax = Math.toRadians(raMax);
44 if (phiMin < 0.) phiMin += TWOPI;
45 if (phiMax < 0. ) phiMax += TWOPI;
46 if (this.phiMax < this.phiMin) this.phiMax += TWOPI;
47 this.phiMin = bm.MODULO(this.phiMin, TWOPI) - epsilon; // phi min, excluding 2pi period
48 this.phiMax = bm.MODULO(this.phiMax, TWOPI) + epsilon;
49 this.raMin = Math.toDegrees(this.phiMin);
50 this.raMax = Math.toDegrees(this.phiMax);
51 this.decMin = yMin;
52 this.decMax = yMax;
53
54
55 this.tetMax = PI/2. - Math.toRadians(decMin);
56 this.tetMin = PI/2. - Math.toRadians(decMax);
57
58 // create list of vertex vectors
59 vertices = new ArrayList();
60 PixTools pt = new PixTools();
61 Vector3d vv = pt.Ang2Vec(tetMin,phiMin);
62 vertices.add(vv);
63 vv = pt.Ang2Vec(tetMin,phiMax);
64 vertices.add(vv);
65 vv = pt.Ang2Vec(tetMax,phiMin);
66 vertices.add(vv);
67 vv = pt.Ang2Vec(tetMax,phiMax);
68 vertices.add(vv);
69
70 }
71 /**
72 * return true if the point ra,dec is inside region
73 * @param ra in degrees
74 * @param dec in degrees
75 * @return boolean true if inside the region
76 */
77 public boolean inReg(double ra, double dec) {
78 boolean res = false;
79 double racomp = ra;
80 double phiComp = Math.toRadians(ra);
81 if (phiComp < 0.) phiComp += TWOPI;
82 phiComp = bm.MODULO(phiComp, TWOPI) - epsilon;
83
84 racomp = Math.toDegrees(phiComp);
85
86
87 if ((phiComp >= phiMin - epsilon) && (phiComp <= phiMax + epsilon) && (dec>= decMin - epsilon) && (dec <= decMax + epsilon)) res = true;
88 return res;
89 }
90 /**
91 * return true if point phi, theta is inside region
92 * @param phi in radians
93 * @param theta in radians
94 * @return boolean true if in region
95 */
96 public boolean inRegPol(double phi, double theta) {
97 boolean res = false;
98 double phicomp = phi;
99 if (phicomp < 0.) phicomp += TWOPI;
100 phicomp = bm.MODULO(this.phiMin, TWOPI) - epsilon;
101 if ((phicomp >= phiMin - epsilon) && (phicomp <= phiMax + epsilon) && (theta>= tetMin - epsilon) && (theta <= tetMax + epsilon)) res = true;
102 return res;
103 }
104 /**
105 * @return ArrayList of 3d vectors of vertices of the region
106 */
107 public ArrayList getVertices() {
108 return vertices;
109 }
110
111 /**
112 * divides region on HealPix pixels of given size including
113 * pixels that only partly inside the region
114 * @param precision - angular size of the division element in arcsec
115 * @return ArrayList of pixel numbers in ring schema for specifyed resolution
116 * @throws Exception
117 */
118 public ArrayList pixelize(double precision) throws Exception {
119 ArrayList res = new ArrayList();
120 PixTools pt = new PixTools();
121 long nside = pt.GetNSide(precision);
122 long rnmin = pt.RingNum(nside,Math.cos(tetMin));
123 long rnmax = pt.RingNum(nside,Math.cos(tetMax));
124 for (long ir = rnmin; ir < rnmax; ir++) {
125 ArrayList strip = new ArrayList();
126 double phi = (phiMin + phiMax)/2.;
127 double dphi = (phiMax - phiMin)/2.;
128 strip = pt.InRing(nside, ir, phi, dphi, false);
129 res.addAll(strip);
130 }
131
132 return res;
133 }
134 /**
135 * provides polar coordinates of the region vertices
136 * @return array of corner coordinates in form of polar thete,phi angles.
137 */
138 public double[][] getPolReg() {
139 double[][] res = new double[4][2];
140 res[0][1] = phiMin;
141 res[0][0] = tetMin;
142 res[1][1] = phiMax;
143 res[1][0] = tetMin;
144 res[2][1] = phiMin;
145 res[2][0] = tetMax;
146 res[3][1] = phiMax;
147 res[3][0] = tetMax;
148 return res;
149 }
150 }
0 /*
1 * Created on Mar 15, 2005
2 *
3 */
4 package gov.fnal.eag.healpix.test;
5
6 import gov.fnal.eag.healpix.BitManipulation;
7 import junit.framework.TestCase;
8
9 /**
10 * @author N Kuropatkin
11 *
12 */
13 public class BitManipulationTest extends TestCase {
14 /**
15 * tests bit manipulation methods
16 */
17 public void testBitManipulation() {
18 //
19 long mag1 = 178956970;
20 //
21 long a = 3;
22 long b = BitManipulation.swapLSBMSB(a);
23 assertEquals("swapLSBMSB=" + b, 1, a/b, 1e-10);
24 a = 8;
25 b = BitManipulation.swapLSBMSB(a);
26 assertEquals("swapLSBMSB=" + b, 2, a/b, 1e-10);
27 a = 3;
28 b = BitManipulation.invswapLSBMSB(a);
29 assertEquals("invswapLSBMSB=" + b, -4, b, 1e-10);
30 a = 8;
31 b = BitManipulation.invswapLSBMSB(a);
32 assertEquals("invswapLSBMSB=" + b, -5, b, 1e-10);
33
34 a = 3;
35 b = BitManipulation.invMSB(a);
36 assertEquals("invMSB=" + b, mag1-1, b, 1e-10);
37 a = 8;
38 b = BitManipulation.invMSB(a);
39 assertEquals("invMSB=" + b, mag1-8, b, 1e-10);
40 }
41 /**
42 * test Modulo
43 */
44 public void testMODULO() {
45 //
46 double a = 5.;
47 double b = 3.;
48 double mod = BitManipulation.MODULO(a,b);
49 System.out.println("a="+a+" b="+b+" mod="+mod);
50 a = -5.0;
51 b = 3.0;
52 mod = BitManipulation.MODULO(a,b);
53 System.out.println("a="+a+" b="+b+" mod="+mod);
54 a = 5.0;
55 b = -3.0;
56 mod = BitManipulation.MODULO(a,b);
57 System.out.println("a="+a+" b="+b+" mod="+mod);
58 a = -5.0;
59 b = -3.0;
60 mod = BitManipulation.MODULO(a,b);
61 System.out.println("a="+a+" b="+b+" mod="+mod);
62 a = 8.0;
63 b = 5.0;
64 mod = BitManipulation.MODULO(a,b);
65 System.out.println("a="+a+" b="+b+" mod="+mod);
66 a = -8.0;
67 b = 5.0;
68 mod = BitManipulation.MODULO(a,b);
69 System.out.println("a="+a+" b="+b+" mod="+mod);
70 a = 1.0;
71 b = 4.0;
72 mod = BitManipulation.MODULO(a,b);
73 System.out.println("a="+a+" b="+b+" mod="+mod);
74 }
75 }
0 package gov.fnal.eag.healpix.test;
1 import java.util.Collection;
2 import java.util.Iterator;
3 import java.util.Random;
4 import java.util.Set;
5 import java.util.TreeSet;
6 import javax.vecmath.Vector3d;
7 import gov.fnal.eag.healpix.PixTools;
8
9 public class DiscTest {
10
11 private int nCheck;
12 private int nFail;
13
14 public void checkOverlap( int order, double raDeg1, double decDeg1,
15 double raDeg2, double decDeg2 ) throws Exception {
16 double ra1 = Math.toRadians( raDeg1 );
17 double dec1 = Math.toRadians( decDeg1 );
18 double ra2 = Math.toRadians( raDeg2 );
19 double dec2 = Math.toRadians( decDeg2 );
20 double[] radec1 = {raDeg1,decDeg1};
21 double[] polar1 = new double[2];
22 double[] radec2 = {raDeg2,decDeg2};
23 double[] polar2 = new double[2];
24 // System.out.println("ra1="+ra1+" dec1="+dec1+" ra2="+ra2+" dec2="+dec2);
25 PixTools pixTools = PixTools.getInstance();
26 polar1 = pixTools.RaDecToPolar(radec1);
27 polar2 = pixTools.RaDecToPolar(radec2);
28 // System.out.println("thet1="+polar1[0]+" phi1="+polar1[1]+" thet2="+polar2[0]+" phi2="+polar2[1]);
29 Vector3d v1 = pixTools.Ang2Vec(polar1[0], polar1[1]);
30 Vector3d v2 = pixTools.Ang2Vec(polar2[0], polar2[1]);
31
32 // Distance between the two points.
33 double separation = haversineSeparationFormula( ra1, dec1, ra2, dec2 );
34 double separation2 = pixTools.AngDist(v1, v2);
35 // System.out.println("separation1="+separation+" separation2="+separation2);
36 // Pick disc radius to be slightly larger than half the distance.
37 // The discs must therefore overlap, which means that the
38 // returned pixel lists must have at least one pixel in common.
39 double radius = separation * 0.5001;
40 double radius2 = separation2 * 0.5001;
41 // System.out.println("radius="+radius+" radius2="+radius2);
42 Collection disc1 = queryDisc( order, ra1, dec1, radius );
43 Collection disc2 = queryDisc( order, ra2, dec2, radius );
44
45 nCheck++;
46 if ( intersection( disc1, disc2 ).isEmpty() ) {
47 System.out.println( "** ERROR: no overlap detected **: "
48 + "(" + raDeg1 + "," + decDeg1 + ") - "
49 + "(" + raDeg2 + "," + decDeg2 + ")"
50 + ", order=" + order
51 + ", radius=" + radius );
52 nFail++;
53 }
54 }
55
56 public Collection queryDisc( int order, double ra, double dec,
57 double radius ) {
58 PixTools pixTools = PixTools.getInstance();
59 long nside = orderToNside( order );
60 double theta = Math.PI / 2 - dec;
61 double phi = ra;
62 Vector3d vec = pixTools.Ang2Vec( theta, phi );
63 return pixTools.query_disc( nside, vec, radius, 1, 1 );
64 }
65
66 public String report() {
67 return "Checks: " + nCheck + "; "
68 + "Failures: " + nFail;
69 }
70
71 static long orderToNside( int order ) {
72 return 1L << order;
73 }
74
75 static Collection intersection( Collection c1, Collection c2 ) {
76 // System.out.println("n pixels 1="+c1.size()+" 2="+c2.size());
77 Set s = new TreeSet( c1 );
78 s.retainAll( c2 );
79 return s;
80 }
81
82 /**
83 * Haversine formula for spherical trigonometry.
84 * This does not have the numerical instabilities of the cosine formula
85 * at small angles.
86 * <p>
87 * This implementation derives from Bob Chamberlain's contribution
88 * to the comp.infosystems.gis FAQ; he cites
89 * R.W.Sinnott, "Virtues of the Haversine", Sky and Telescope vol.68,
90 * no.2, 1984, p159.
91 *
92 * @param ra1 right ascension of point 1 in radians
93 * @param dec1 declination of point 1 in radians
94 * @param ra2 right ascension of point 2 in radians
95 * @param dec2 declination of point 2 in radians
96 * @return angular separation of point 1 and point 2 in radians
97 * @see <http://www.census.gov/geo/www/gis-faq.txt>
98 */
99 static double haversineSeparationFormula( double ra1, double dec1,
100 double ra2, double dec2 ) {
101 double sd2 = Math.sin( 0.5 * ( dec2 - dec1 ) );
102 double sr2 = Math.sin( 0.5 * ( ra2 - ra1 ) );
103 double a = sd2 * sd2 +
104 sr2 * sr2 * Math.cos( dec1 ) * Math.cos( dec2 );
105 if ( Double.isNaN( a ) ) {
106 return Double.NaN;
107 }
108 return a < 1.0 ? 2.0 * Math.asin( Math.sqrt( a ) )
109 : Math.PI;
110 }
111
112 public static void main( String[] args ) throws Exception {
113 DiscTest test = new DiscTest();
114 Random rnd = new Random( 2301L );
115 double scale = 0.1; // degree
116 for ( int i = 0; i < 1000000; i++ ) {
117 double ra1 = rnd.nextDouble() * 360;
118 double dec1 = rnd.nextDouble() * 180 - 90;
119 double ra2 = Math.min( ra1 + rnd.nextDouble() * scale, 360 );
120 double dec2 = Math.max( Math.min( dec1 + rnd.nextDouble() * scale,
121 90 ), -90 );
122 for ( int order = 5; order < 12; order++ ) {
123 test.checkOverlap( order, ra1, dec1, ra2, dec2 );
124 }
125 }
126 test.checkOverlap( 8, 0., -89.983888, 180., -89.983888 );
127
128 System.out.println( test.report() );
129 }
130 }
131
132
0 package gov.fnal.eag.healpix.test;
1
2 import javax.vecmath.Vector3d;
3
4 import gov.fnal.eag.healpix.PixTools;
5 import junit.framework.TestCase;
6
7
8 public class PixToolTest1 extends TestCase {
9
10 static PixTools tools = new PixTools();
11
12 long NSIDE1 = 64;
13
14 long NSIDE2 = 512;
15
16 public void testCircle1(){
17 Vector3d vect = tools.Ang2Vec(0.5178634297507729, 0.06421357206295804);
18 System.out.println("vx="+vect.x+" vy="+vect.y+" vz="+vect.z);
19 tools.query_disc(NSIDE1, tools.Ang2Vec(0.5178634297507729, 0.06421357206295804), 5.817764173314432E-4, 1, 1);
20 }
21
22 public void testCircle2(){
23 tools.query_disc(NSIDE1, tools.Ang2Vec(0.3127581538205727, 0.1050979097909252), 0.01454441043328608, 1, 1);
24 }
25
26 public void testCircle3(){
27 Vector3d vect = tools.Ang2Vec(0.011620983936195673, 0.44456444930382233);
28 System.out.println("vx="+vect.x+" vy="+vect.y+" vz="+vect.z);
29 tools.query_disc(NSIDE1, vect, 5.526875964648709E-4, 1, 1);
30 }
31
32 public void testCircle4(){
33 tools.query_disc(NSIDE1, tools.Ang2Vec(0.3127581538205727, 0.1050979097909252), 0.01454441043328608, 1, 1);
34 }
35
36
37 /*** with NSIDE 512 **/
38 public void testCircle5(){
39 tools.query_disc(NSIDE2, tools.Ang2Vec(1.0486568403767373, 0.036411931519731704), 6.399540590645875E-4, 1, 1);
40 }
41 /*** with NSIDE 512 **/
42 public void testVertexes(){
43 double ra = 30.0;
44 double dec=30.0;
45 double rad2deg = 180.0/Math.PI;
46 double[] radec = new double[2];
47 radec[0] = ra;
48 radec[1] = dec;
49 double[] thetphi = new double[2];
50 thetphi = tools.RaDecToPolar(radec);
51 double theta = thetphi[0];
52 double phi = thetphi[1];
53 long ipix = tools.ang2pix_nest(NSIDE2, theta, phi);
54 double[][] vertexes = tools.pix2vertex_nest(NSIDE2, ipix);
55 long ipixR = tools.ang2pix_ring(NSIDE2, theta, phi);
56 double[][] vertexesr = tools.pix2vertex_ring(NSIDE2, ipixR);
57 for (int i=0; i< 4; i++) {
58 Vector3d vect = new Vector3d(vertexes[0][i],vertexes[1][i],vertexes[2][i]);
59 double[] angs =tools.Vect2Ang(vect);
60 Vector3d vectR = new Vector3d(vertexesr[0][i],vertexesr[1][i],vertexesr[2][i]);
61 double[] angsR =tools.Vect2Ang(vectR);
62 assertEquals("theta="+angs[0],angs[0],angsR[0], 1e-10);
63 assertEquals("phi="+angs[1],angs[1],angsR[1], 1e-10);
64 }
65
66 }
67 /*** with high res.**/
68 public void testVertexesHR(){
69 long nside = 1 << 20 ;
70 double ra = 30.0;
71 double dec=30.0;
72
73 double[] radec = new double[2];
74 radec[0] = ra;
75 radec[1] = dec;
76 double[] thetphi = new double[2];
77 thetphi = tools.RaDecToPolar(radec);
78 double theta = thetphi[0];
79 double phi = thetphi[1];
80 long ipix = tools.ang2pix_nest(nside, theta, phi);
81 double[][] vertexes = tools.pix2vertex_nest(nside, ipix);
82 long ipixR = tools.ang2pix_ring(nside, theta, phi);
83 double[][] vertexesr = tools.pix2vertex_ring(nside, ipixR);
84 for (int i=0; i< 4; i++) {
85 Vector3d vect = new Vector3d(vertexes[0][i],vertexes[1][i],vertexes[2][i]);
86 double[] angs =tools.Vect2Ang(vect);
87 Vector3d vectR = new Vector3d(vertexesr[0][i],vertexesr[1][i],vertexesr[2][i]);
88 double[] angsR =tools.Vect2Ang(vectR);
89 assertEquals("theta="+angs[0],angs[0],angsR[0], 1e-10);
90 assertEquals("phi="+angs[1],angs[1],angsR[1], 1e-10);
91 }
92 }
93 public void testInverse(){
94 for(double ra = 0.0 ; ra < 360.0; ra+=10) {
95 for(double de = -85 ; de <= 85; de+=10)
96 {
97
98 long area =getHealId(ra,de,NSIDE2);
99 RaDePoint p = getRaDe(area,NSIDE2);
100
101 //compare with tolerance
102 assertTrue(ra+"!="+p.ra,Math.abs(ra- p.ra)<1);
103 assertTrue(de+"!="+p.de,Math.abs(de- p.de)<1);
104 }
105 }
106 }
107
108 /**
109 * get position ID for ra,de
110 * @param ra right ascencion in degrees
111 * @param de declination in degrees
112 * @param nsides - non default number of nsides
113 */
114
115 public long getHealId(double ra, double de, long nsides) {
116 double[] radec = new double[2];
117 radec[0] = ra;
118 radec[1] = de;
119 double[] polar = tools.RaDecToPolar(radec);
120 long ip = tools.ang2pix_ring(nsides, polar[0], polar[1]);
121
122
123 return ip;
124 }
125
126 /** inverse method, convert area ID to RaDePoint */
127 public static RaDePoint getRaDe(long area,long nsides) {
128 double [] polar = tools.pix2ang_ring(nsides , area);
129 double[]radec = tools.PolarToRaDec(polar);
130 return new RaDePoint(radec[0],radec[1]);
131 }
132
133
134 /**
135 * Simple point on sky. Used as return value of functions
136 * <p>
137 * @See CoeliObject for data definition
138 */
139 static public class RaDePoint {
140
141 /** coordinates in degrees*/
142 public double ra,de;
143
144
145 public RaDePoint() {}
146
147 public RaDePoint(double ra, double de){
148 this.ra = ra;
149 this.de = de;
150 }
151
152
153 }
154 }
0 package gov.fnal.eag.healpix.test;
1 import java.io.File;
2 import java.io.FileNotFoundException;
3 import java.io.OutputStream;
4 import java.io.PrintStream;
5 import java.util.ArrayList;
6 import gov.fnal.eag.healpix.BitManipulation;
7 import gov.fnal.eag.healpix.PixTools;
8
9 import javax.vecmath.Vector3d;
10
11 import junit.framework.TestCase;
12
13
14 public class PixToolsSingletonTest extends TestCase {
15
16
17 /**
18 * test MODULO function
19 */
20 public void testMODULO() {
21 //
22 double A = 8.;
23 double B = 5.;
24 double res = BitManipulation.MODULO(A, B);
25 assertEquals("modulo = " + res, 3., res, 1e-10);
26 A = -8.;
27 B = 5.;
28 res = BitManipulation.MODULO(A, B);
29 assertEquals("modulo = " + res, 2., res, 1e-10);
30 A = 8.;
31 B = -5.;
32 res = BitManipulation.MODULO(A, B);
33 assertEquals("modulo = " + res, -2., res, 1e-10);
34 A = -8.;
35 B = -5.;
36 res = BitManipulation.MODULO(A, B);
37 assertEquals("modulo = " + res, -3., res, 1e-10);
38 System.out.println(" test MODULO is done");
39 }
40
41 /**
42 * tests angular distance calculation
43 * @throws Exception
44 */
45 public void testAngDist() throws Exception {
46 Vector3d v1 = new Vector3d(1.5, 1.6, 2.0);
47 Vector3d v2 = new Vector3d(1.2, 1.0, 1.5);
48
49 double res1 = PixTools.getInstance().AngDist(v1, v2);
50 double res2 = v1.angle(v2);
51 System.out.println("res1 = " + res1 + " res2=" + res2);
52 assertEquals("angular Distance=" + res2, 1.0, res1 / res2, 1e-10);
53 /* Check known problem with vecmath for small vector differences */
54
55 Vector3d v3 = new Vector3d(1.5, 1.6, 0.);
56 Vector3d v4 = new Vector3d(1.5, 1.601, 0.);
57 double res3 = PixTools.getInstance().AngDist(v3, v4);
58 double res4 = v3.angle(v4);
59 //
60 System.out.println("res3 = " + res3 + " res4=" + res4);
61 assertEquals("angular Distance=" + res4, 1., (Math.PI / 2. + res3)
62 / res4, 1e-3);
63 Vector3d v5 = new Vector3d(1.5, 1.6, 0.);
64 Vector3d v6 = new Vector3d(-1.5, -1.75, 0.);
65 double res5 = PixTools.getInstance().AngDist(v5, v6);
66 double res6 = v5.angle(v6);
67 System.out.println("res5 = " + res5 + " res6=" + res6);
68 assertEquals("angular Distance=" + res6, 2.0, res5 / res6, 1e-10);
69 System.out.println(" test of AngDist is done");
70 }
71
72 /**
73 * @throws Exception
74 */
75 public void testSurfaceTriangle() throws Exception {
76 Vector3d v1 = new Vector3d(1.0, 0.0, 0.0);
77 Vector3d v2 = new Vector3d(0.0, 1.0, 0.0);
78 Vector3d v3 = new Vector3d(0.0, 0.0, 1.0);
79
80 double res = PixTools.getInstance().SurfaceTriangle(v1, v2, v3);
81 System.out.println("Triangle surface is=" + res / Math.PI
82 + " steredians");
83 assertEquals("Triangle surface=" + res, 0.5, res / Math.PI, 1e-10);
84 System.out.println(" test of SurfaceTriangle is done");
85 }
86
87 /**
88 * tests calculation of npixels from nside
89 */
90 public void testNside2Npix() {
91 int nside = 1;
92 int npix = 0;
93
94 npix = (int) PixTools.getInstance().Nside2Npix(nside);
95 assertEquals("Npix=" + npix, 12, npix, 1e-10);
96 nside = 2;
97 npix = (int) PixTools.getInstance().Nside2Npix(nside);
98 assertEquals("Npix=" + npix, 48, npix, 1e-10);
99 }
100
101 /**
102 * tests calculation of nsides from npixels
103 */
104 public void testNpix2Nside() {
105 int npix = 12;
106 int nside = 0;
107
108 nside = (int) PixTools.getInstance().Npix2Nside(npix);
109
110 double pixSize1 = PixTools.getInstance().PixRes(65536);
111 long nside1 = PixTools.getInstance().GetNSide(pixSize1);
112 assertEquals("Nside=" + nside1, 65536, nside1, 1e-10);
113
114 assertEquals("Nside=" + nside, 1, nside, 1e-10);
115
116 }
117
118 /**
119 * test of directional angles calculation
120 */
121 public void testVec2Ang() {
122 double PI = Math.PI;
123 Vector3d v = new Vector3d(0.0, 1.0, 0.0);
124
125 double[] ang_tup = { 0., 0. };
126 ang_tup = PixTools.getInstance().Vect2Ang(v);
127 System.out.println(" Theta=" + ang_tup[0] / PI + " Phi=" + ang_tup[1]
128 / PI);
129 assertEquals("Theta=" + ang_tup[0], 0.5, ang_tup[0] / PI, 1e-10);
130 assertEquals("Phi=" + ang_tup[1], 0.5, ang_tup[1] / PI, 1e-10);
131 v = new Vector3d(1.0, 0.0, 0.0);
132 ang_tup = PixTools.getInstance().Vect2Ang(v);
133 assertEquals("phi=" + ang_tup[1], 0., ang_tup[1] / PI, 1e-10);
134 System.out.println(" test Vect2Ang is done");
135 }
136
137 /**
138 * tests calculation of pixel from polar angles
139 * in ring schema of pixalization
140 * @throws Exception
141 */
142 public void testAng2Pix() throws Exception {
143 System.out.println(" Test ang2pix_ring ___________________");
144 double PI = Math.PI;
145 long pix = -1;
146 double theta = PI / 2. - 0.2;
147 double phi = PI / 2. ;
148 long nside = 4;
149
150 try {
151 pix = PixTools.getInstance().ang2pix_ring(nside,theta, phi);
152 } catch (Exception e) {
153 e.printStackTrace();
154 }
155 Vector3d v = PixTools.getInstance().Ang2Vec(theta,phi);
156 long pix1 = PixTools.getInstance().vect2pix_ring(nside,v);
157 assertEquals("pix=" + pix, pix1, pix, 1e-10);
158 assertEquals("pix=" + pix, 76, pix, 1e-10);
159
160 long pix2 = PixTools.getInstance().ang2pix_nest(nside,theta,phi);
161 long pix3 = PixTools.getInstance().vect2pix_nest(nside,v);
162 assertEquals("pix2=" + pix2, pix3, pix2, 1e-10);
163 assertEquals("pix2=" + pix2, 92, pix2, 1e-10);
164
165
166 double[] radec = PixTools.getInstance().pix2ang_ring(nside,76);
167 assertEquals("theta=" + theta, theta, radec[0], 4e-2);
168 assertEquals("phi=" + phi, radec[1], phi, 1e-2);
169 double[] radec1 = PixTools.getInstance().pix2ang_nest(nside,92);
170 System.out.println("theta="+radec1[0]+" phi="+radec1[1]);
171 assertEquals("theta=" + theta, theta, radec1[0], 4e-2);
172 assertEquals("phi=" + phi, radec1[1], phi, 1e-2);
173 System.out.println(" test Ang2Pix is done");
174 }
175
176 /**
177 * tests calculation of unit vector from polar angles
178 * @throws Exception
179 */
180 public void testAng2Vect() throws Exception {
181 System.out.println(" Start test Ang2Vect----------------");
182 double PI = Math.PI;
183 double theta = PI / 2.;
184 double phi = PI / 2;
185
186 Vector3d v = PixTools.getInstance().Ang2Vec(theta, phi);
187 System.out.println("Vector x=" + v.x + " y=" + v.y + " z=" + v.z);
188 assertEquals("x=" + v.x, 0., v.x, 1e-10);
189 assertEquals("y=" + v.y, 1., v.y, 1e-10);
190 assertEquals("z=" + v.z, 0., v.z, 1e-10);
191 System.out.println(" test Ang2Vect is done");
192 }
193
194 /**
195 * tests calculation of ring number from z coordinate
196 * @throws Exception
197 */
198 public void testRingNum() throws Exception {
199 double z = 0.25;
200 int nside = 1;
201 System.out.println("Start test RingNum !!!!!!!!!!!!!!!!!!!!");
202
203 int nring = (int) PixTools.getInstance().RingNum(nside, z);
204 System.out.println("z=" + z + " ring number =" + nring);
205 assertEquals("z=" + z, 2, nring, 1e-10);
206 z = -0.25;
207 nring = (int) PixTools.getInstance().RingNum(nside, z);
208 assertEquals("z=" + z, 2, nring, 1e-10);
209 z = 0.8;
210 nring = (int) PixTools.getInstance().RingNum(nside, z);
211 assertEquals("z=" + z, 1, nring, 1e-10);
212 z = -0.8;
213 nring = (int) PixTools.getInstance().RingNum(nside, z);
214 assertEquals("z=" + z, 3, nring, 1e-10);
215 System.out.println(" test RingNum is done");
216 nside = 4;
217 int pix = 3;
218 Vector3d v = PixTools.getInstance().pix2vect_ring(nside, pix);
219 z = v.z;
220 nring = (int) PixTools.getInstance().RingNum(nside, z);
221 assertEquals("z=" + z, 1, nring, 1e-10);
222 pix = 11;
223 v = PixTools.getInstance().pix2vect_ring(nside, pix);
224 z = v.z;
225 nring = (int) PixTools.getInstance().RingNum(nside, z);
226 assertEquals("z=" + z, 2, nring, 1e-10);
227 pix = 23;
228 v = PixTools.getInstance().pix2vect_ring(nside, pix);
229 z = v.z;
230 nring = (int) PixTools.getInstance().RingNum(nside, z);
231 assertEquals("z=" + z, 3, nring, 1e-10);
232 pix = 39;
233 v = PixTools.getInstance().pix2vect_ring(nside, pix);
234 z = v.z;
235 nring = (int) PixTools.getInstance().RingNum(nside, z);
236 assertEquals("z=" + z, 4, nring, 1e-10);
237 pix = 55;
238 v = PixTools.getInstance().pix2vect_ring(nside, pix);
239 z = v.z;
240 nring = (int) PixTools.getInstance().RingNum(nside, z);
241 assertEquals("z=" + z, 5, nring, 1e-10);
242 pix = 71;
243 v = PixTools.getInstance().pix2vect_ring(nside, pix);
244 z = v.z;
245 nring = (int) PixTools.getInstance().RingNum(nside, z);
246 assertEquals("z=" + z, 6, nring, 1e-10);
247 pix = 87;
248 v = PixTools.getInstance().pix2vect_ring(nside, pix);
249 z = v.z;
250 nring = (int) PixTools.getInstance().RingNum(nside, z);
251 assertEquals("z=" + z, 7, nring, 1e-10);
252 pix = 103;
253 v = PixTools.getInstance().pix2vect_ring(nside, pix);
254 z = v.z;
255 nring = (int) PixTools.getInstance().RingNum(nside, z);
256 assertEquals("z=" + z, 8, nring, 1e-10);
257 pix = 119;
258 v = PixTools.getInstance().pix2vect_ring(nside, pix);
259 z = v.z;
260 nring = (int) PixTools.getInstance().RingNum(nside, z);
261 assertEquals("z=" + z, 9, nring, 1e-10);
262 pix = 135;
263 v = PixTools.getInstance().pix2vect_ring(nside, pix);
264 z = v.z;
265 nring = (int) PixTools.getInstance().RingNum(nside, z);
266 assertEquals("z=" + z, 10, nring, 1e-10);
267 pix = 151;
268 v = PixTools.getInstance().pix2vect_ring(nside, pix);
269 z = v.z;
270 nring = (int) PixTools.getInstance().RingNum(nside, z);
271 assertEquals("z=" + z, 11, nring, 1e-10);
272 pix = 167;
273 v = PixTools.getInstance().pix2vect_ring(nside, pix);
274 z = v.z;
275 nring = (int) PixTools.getInstance().RingNum(nside, z);
276 assertEquals("z=" + z, 12, nring, 1e-10);
277 pix = 169;
278 v = PixTools.getInstance().pix2vect_ring(nside, pix);
279 z = v.z;
280 nring = (int) PixTools.getInstance().RingNum(nside, z);
281 assertEquals("z=" + z, 13, nring, 1e-10);
282 pix = 180;
283 v = PixTools.getInstance().pix2vect_ring(nside, pix);
284 z = v.z;
285 nring = (int) PixTools.getInstance().RingNum(nside, z);
286 assertEquals("z=" + z, 14, nring, 1e-10);
287 System.out.println("End test RingNum");
288 }
289
290 /**
291 * tests conversion from nest schema pixel to ring schema pixel
292 * @throws Exception
293 */
294 public void testNest2Ring() throws Exception {
295
296 int ipnest = 3;
297 int nside = 2;
298 int ipring = (int) PixTools.getInstance().nest2ring(nside, ipnest);
299 assertEquals("ipring=" + ipring, 0, ipring, 1e-10);
300 ipnest = 0;
301 nside = 2;
302 ipring = (int) PixTools.getInstance().nest2ring(nside, ipnest);
303 assertEquals("ipring=" + ipring, 13, ipring, 1e-10);
304 ipnest = 18;
305 nside = 2;
306 ipring = (int) PixTools.getInstance().nest2ring(nside, ipnest);
307 assertEquals("ipring=" + ipring, 27, ipring, 1e-10);
308 ipnest = 23;
309 nside = 2;
310 ipring = (int) PixTools.getInstance().nest2ring(nside, ipnest);
311 assertEquals("ipring=" + ipring, 14, ipring, 1e-10);
312 ipnest = 5;
313 nside = 4;
314 ipring = (int) PixTools.getInstance().nest2ring(nside, ipnest);
315 assertEquals("ipring = " + ipring, 27, ipring, 1e-10);
316 System.out.println(" test Nest2Ring is done");
317 }
318
319 /**
320 * tests conversion from ring schema pixel to nest schema pixel
321 * @throws Exception
322 */
323 public void testRing2Nest() throws Exception {
324
325 System.out.println(" start test Ring2Nest !!!!!!!!!!!!!!!!!!!!!!");
326 int ipring = 0;
327 int nside = 2;
328
329 int ipnest = (int) PixTools.getInstance().ring2nest(nside, ipring);
330 assertEquals("ipnest=" + ipnest, 3, ipnest, 1e-10);
331 ipring = 13;
332 nside = 2;
333 ipnest = (int) PixTools.getInstance().ring2nest(nside, ipring);
334 assertEquals("ipnest=" + ipnest, 0, ipnest, 1e-10);
335 ipring = 27;
336 nside = 2;
337 ipnest = (int) PixTools.getInstance().ring2nest(nside, ipring);
338 assertEquals("ipnest=" + ipnest, 18, ipnest, 1e-10);
339 ipring = 14;
340 nside = 2;
341 ipnest = (int) PixTools.getInstance().ring2nest(nside, ipring);
342 assertEquals("ipnest=" + ipnest, 23, ipnest, 1e-10);
343 ipring = 27;
344 nside = 4;
345 ipnest = (int) PixTools.getInstance().ring2nest(nside, ipring);
346 assertEquals("ipnest = " + ipnest, 5, ipnest, 1e-10);
347 ipring = 83;
348 nside = 4;
349 ipnest = (int) PixTools.getInstance().ring2nest(nside, ipring);
350 assertEquals("ipnest = " + ipnest, 123, ipnest, 1e-10);
351 System.out.println(" test Ring2Nest is done");
352 }
353
354 /**
355 * tests Next_In_Line method for the nest schema
356 * @throws Exception
357 */
358 public void testNext_In_Line_Nest() throws Exception {
359
360 int ipix = 0;
361 int nside = 2;
362 int ipnext = (int) PixTools.getInstance().next_in_line_nest(nside, ipix);
363 assertEquals("ipnext=" + ipnext, 23, ipnext, 1e-10);
364 ipix = 1;
365 nside = 2;
366 ipnext = (int) PixTools.getInstance().next_in_line_nest(nside, ipix);
367 assertEquals("ipnext=" + ipnext, 6, ipnext, 1e-10);
368 ipix = 4;
369 nside = 2;
370 ipnext = (int) PixTools.getInstance().next_in_line_nest(nside, ipix);
371 assertEquals("ipnext=" + ipnext, 27, ipnext, 1e-10);
372 ipix = 27;
373 nside = 2;
374 ipnext = (int) PixTools.getInstance().next_in_line_nest(nside, ipix);
375 assertEquals("ipnext=" + ipnext, 8, ipnext, 1e-10);
376 ipix = 12;
377 nside = 2;
378 ipnext = (int) PixTools.getInstance().next_in_line_nest(nside, ipix);
379 assertEquals("ipnext=" + ipnext, 19, ipnext, 1e-10);
380 ipix = 118;
381 nside = 4;
382 ipnext = (int) PixTools.getInstance().next_in_line_nest(nside, ipix);
383 assertEquals("ipnext = " + ipnext, 117, ipnext, 1e-10);
384 System.out.println(" test next_in_line_nest is done");
385 }
386
387 /**
388 * tests InRing method
389 * @throws Exception
390 */
391 public void testInRing() throws Exception {
392 System.out.println(" Start test InRing !!!!!!!!!!!!!!!!!!!!!!!!!");
393
394 int[] nestComp = { 19, 0, 23, 4, 27, 8, 31, 12 };
395 long[] ringHi = {17, 18, 19, 12, 13 };
396 long[] nestHi = { 8, 31, 12, 19, 0 };
397 long[] ringLow = {19, 12, 13, 14, 15 };
398 long[] nestLow = { 12, 19, 0, 23, 4 };
399 double PI = Math.PI;
400 int nside = 2;
401 boolean nest = false;
402 int iz = 3;
403 double phi = PI;
404 double dphi = PI;
405 ArrayList ring = PixTools.getInstance().InRing(nside, iz, phi, dphi, nest);
406 for (int i = 0; i < ring.size(); i++) {
407 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
408 i + 12, ((Long) ring.get(i)).longValue(), 1e-10);
409 }
410 Vector3d v = new Vector3d(1., 0., 0.);
411 double[] ang_tup = { 0., 0. };
412 ang_tup = PixTools.getInstance().Vect2Ang(v);
413 phi = ang_tup[1]/PI;
414 ring = PixTools.getInstance().InRing(nside, iz, phi, dphi, nest);
415 for (int i = 0; i < ring.size(); i++) {
416 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
417 i + 12, ((Long) ring.get(i)).longValue(), 1e-10);
418 }
419 Vector3d v1 = new Vector3d(-1., 0., 0.);
420
421 ang_tup = PixTools.getInstance().Vect2Ang(v1);
422 phi = ang_tup[1]/PI;
423 ring = PixTools.getInstance().InRing(nside, iz, phi, dphi, nest);
424 for (int i = 0; i < ring.size(); i++) {
425 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
426 i + 12, ((Long) ring.get(i)).longValue(), 1e-10);
427 }
428 phi = 1.75*PI;
429 dphi = 0.5*PI;
430 ring = PixTools.getInstance().InRing(nside, iz, phi, dphi, nest);
431 for (int i = 0; i < ring.size(); i++) {
432 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
433 ringHi[i], ((Long) ring.get(i)).longValue(), 1e-10);
434
435 }
436
437 phi = 1.75*PI;
438 dphi = 0.5*PI;
439 nest = true;
440 ring = PixTools.getInstance().InRing(nside, iz, phi, dphi, nest);
441 for (int i = 0; i < ring.size(); i++) {
442 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
443 nestHi[i], ((Long) ring.get(i)).longValue(), 1e-10);
444
445 }
446 phi = 0.25*PI;
447 dphi = 0.5*PI;
448 nest = false;
449 ring = PixTools.getInstance().InRing(nside, iz, phi, dphi, nest);
450 for (int i = 0; i < ring.size(); i++) {
451 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
452 ringLow[i], ((Long) ring.get(i)).longValue(), 1e-10);
453
454 }
455 phi = 0.25*PI;
456 dphi = 0.5*PI;
457 nest = true;
458 ring = PixTools.getInstance().InRing(nside, iz, phi, dphi, nest);
459 for (int i = 0; i < ring.size(); i++) {
460 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
461 nestLow[i], ((Long) ring.get(i)).longValue(), 1e-10);
462
463 }
464
465 nest = true;
466 dphi = PI;
467 ring = PixTools.getInstance().InRing(nside, iz, phi, dphi, nest);
468 for (int i = 0; i < ring.size(); i++) {
469 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
470 nestComp[i], ((Long) ring.get(i)).longValue(), 1e-10);
471 }
472 nest = false;
473 nside = 4;
474 phi = 2.1598449493429825;
475 iz = 8;
476 dphi = 0.5890486225480867;
477 // System.out.println(" iz="+iz+" phi="+phi+" dphi="+dphi);
478 ring = PixTools.getInstance().InRing(nside, iz, phi, dphi, nest);
479 // for (int i = 0; i<ring.size(); i++) {
480 // System.out.println("ipnext = "+ ((Integer)ring.get(i)).intValue());
481 // }
482 nest = false;
483 nside = 4;
484 dphi = 0. * PI;
485 iz = 8;
486 phi = 2.1598449493429825;
487 // System.out.println(" iz="+iz+" phi="+phi+" dphi="+dphi);
488 ring = PixTools.getInstance().InRing(nside, iz, phi, dphi, nest);
489 // for (int i = 0; i<ring.size(); i++) {
490 // System.out.println("ipnext = "+ ((Integer)ring.get(i)).intValue());
491 // }
492 System.out.println(" test InRing is done");
493 }
494
495 /**
496 * tests Neighbour's method for nest schema of the pixelization
497 * @throws Exception
498 */
499 public void testNeighbours_Nest() throws Exception {
500 System.out.println(" Start test Neighbours_Nest !!!!!!!!!!!!!!!!!");
501
502 long nside = 2;
503 long ipix = 25;
504 long[] test17 = { 34, 16, 18, 19, 2, 0, 22, 35 };
505 long[] test32 = { 40, 44, 45, 34, 35, 33, 38, 36 };
506 long[] test3 = { 0, 2, 13, 15, 11, 7, 6, 1 };
507 long[] test25 = { 42, 24, 26, 27, 10, 8, 30, 43 };
508 //
509 ArrayList npixList = new ArrayList();
510 npixList = PixTools.getInstance().neighbours_nest(nside, ipix);
511 for (int i = 0; i < npixList.size(); i++) {
512 assertEquals("ip = " + ((Long) npixList.get( i)).longValue(),
513 test25[ i], ((Long) npixList.get( i)).longValue(), 1e-10);
514 }
515 ipix = 17;
516
517 npixList = PixTools.getInstance().neighbours_nest(nside, ipix);
518 for (int i = 0; i < npixList.size(); i++) {
519 assertEquals("ip = " + ((Long) npixList.get(i)).longValue(),
520 test17[i], ((Long) npixList.get(i)).longValue(), 1e-10);
521 }
522 ipix = 32;
523
524 npixList = PixTools.getInstance().neighbours_nest(nside, ipix);
525 for (int i = 0; i < npixList.size(); i++) {
526 assertEquals("ip = " + ((Long) npixList.get(i)).longValue(),
527 test32[i], ((Long) npixList.get(i)).longValue(), 1e-10);
528 }
529 ipix = 3;
530
531 npixList = PixTools.getInstance().neighbours_nest(nside, ipix);
532 for (int i = 0; i < npixList.size(); i++) {
533 assertEquals("ip = " + ((Long) npixList.get(i)).longValue(),
534 test3[i], ((Long) npixList.get(i)).longValue(), 1e-10);
535 }
536 System.out.println(" test NeighboursNest is done");
537 }
538
539 /**
540 * tests intrs_intrv method
541 * @throws Exception
542 */
543 public void testIntrs_Intrv() throws Exception {
544 System.out.println(" test intrs_intrv !!!!!!!!!!!!!!!!!!!!!!!!!!!");
545
546 double[] d1 = { 1.0, 9.0 };
547 double[] d2 = { 3.0, 16.0 };
548 double[] di;
549 // System.out.println("Case "+d1[0]+" "+d1[1]+" | "+d2[0]+" "+d2[1]);
550 di = PixTools.getInstance().intrs_intrv(d1, d2);
551 // System.out.println("Result "+di[0]+" - "+di[1]);
552 int n12 = di.length / 2;
553 assertEquals("n12 = " + n12, 1, n12, 1e-6);
554 assertEquals("di[0] = " + di[0], 3.0, di[0], 1e-6);
555 assertEquals("di[1] = " + di[1], 9.0, di[1], 1e-6);
556 d1 = new double[] { 0.537, 4.356 };
557 d2 = new double[] { 3.356, 0.8 };
558 // System.out.println("Case "+d1[0]+" "+d1[1]+" | "+d2[0]+" "+d2[1]);
559 di = PixTools.getInstance().intrs_intrv(d1, d2);
560 n12 = di.length / 2;
561 assertEquals("n12 = " + n12, 2, n12, 1e-6);
562 assertEquals("di[0] = " + di[0], 0.537, di[0], 1e-6);
563 assertEquals("di[1] = " + di[1], 0.8, di[1], 1e-6);
564 assertEquals("di[2] = " + di[2], 3.356, di[2], 1e-6);
565 assertEquals("di[1] = " + di[3], 4.356, di[3], 1e-6);
566
567 d1 = new double[] { 2.356194490092345, 2.356194490292345 };
568 d2 = new double[] { 1.251567, 4.17 };
569 // System.out.println("Case "+d1[0]+" "+d1[1]+" | "+d2[0]+" "+d2[1]);
570 di = PixTools.getInstance().intrs_intrv(d1, d2);
571 n12 = di.length / 2;
572 assertEquals("n12 = " + n12, 1, n12, 1e-6);
573 assertEquals("di[0] = " + di[0], 2.35619449009, di[0], 1e-6);
574 assertEquals("di[1] = " + di[1], 2.35619449029, di[1], 1e-6);
575
576 System.out.println(" test intrs_intrv is done");
577 }
578
579 /**
580 * tests conversion from pixel number to vector
581 * @throws Exception
582 */
583 public void testPix2Vect_ring() throws Exception {
584 System.out.println("Start test Pix2Vect_ring !!!!!!!!!!!!!!!!!!!");
585 double TWOPI = 2.0 * Math.PI;
586 int nside = 2;
587 int ipix = 0;
588
589 Vector3d v1 = new Vector3d(0., 0., 0.);
590 v1 = PixTools.getInstance().pix2vect_ring(nside, ipix);
591 assertEquals("v1.z = " + v1.z, 1.0, v1.z, 1e-1);
592
593 ipix = 20;
594 Vector3d v2 = new Vector3d(0., 0., 0.);
595 v2 = PixTools.getInstance().pix2vect_ring(nside, ipix);
596 assertEquals("v2.x = " + v2.x, 1.0, v2.x, 1e-1);
597 assertEquals("v2.z = " + v2.z, 0.0, v2.z, 1e-1);
598 ipix = 22;
599 Vector3d v3 = new Vector3d();
600 v3 = PixTools.getInstance().pix2vect_ring(nside, ipix);
601 assertEquals("v3.y = " + v3.y, 1.0, v3.y, 1e-1);
602 assertEquals("v3.z = " + v3.z, 0.0, v3.z, 1e-1);
603 // System.out.println("Vector3 x="+v3.x+" y="+v3.y+" z="+v3.z);
604 ipix = 95;
605 nside = 4;
606 v1 = PixTools.getInstance().pix2vect_ring(nside, ipix);
607 v1.normalize();
608 double phi1 = Math.atan2(v1.y, v1.x);
609 double[] tetphi = new double[2];
610 tetphi = PixTools.getInstance().pix2ang_ring(nside, ipix);
611 assertEquals("phi = " + phi1, 0.0, Math.abs(phi1 - tetphi[1]), 1e-10);
612 ipix = 26;
613 nside = 4;
614 v1 = PixTools.getInstance().pix2vect_ring(nside, ipix);
615 v1.normalize();
616 phi1 = Math.atan2(v1.y, v1.x);
617 if (phi1 < 0.)
618 phi1 += TWOPI;
619 tetphi = new double[2];
620 tetphi = PixTools.getInstance().pix2ang_ring(nside, ipix);
621 assertEquals("phi = " + phi1, 0.0, Math.abs(phi1 - tetphi[1]), 1e-10);
622 System.out.println("------------------------------------------");
623 System.out.println(" test pix2vect_ring is done");
624 }
625
626 /**
627 * tests conversion from pixel number to vector
628 * @throws Exception
629 */
630 public void testPix2Vect_nest() throws Exception {
631 double TWOPI = 2.0 * Math.PI;
632 int nside = 2;
633 int ipix = 3;
634 System.out.println(" Start test Pix2Vect_nest !!!!!!!!!!!!!!");
635 Vector3d v1 = new Vector3d(0., 0., 0.);
636 v1 = PixTools.getInstance().pix2vect_nest(nside, ipix);
637 assertEquals("v1.z = " + v1.z, 1.0, v1.z, 1e-1);
638 ipix = 17;
639 Vector3d v2 = new Vector3d(0., 0., 0.);
640 v2 = PixTools.getInstance().pix2vect_nest(nside, ipix);
641 assertEquals("v2.x = " + v2.x, 1.0, v2.x, 1e-1);
642 assertEquals("v2.z = " + v2.z, 0.0, v2.z, 1e-1);
643
644 ipix = 21;
645 Vector3d v3 = new Vector3d();
646 v3 = PixTools.getInstance().pix2vect_nest(nside, ipix);
647 assertEquals("v3.y = " + v3.y, 1.0, v3.y, 1e-1);
648 assertEquals("v3.z = " + v3.z, 0.0, v3.z, 1e-1);
649 nside = 4;
650 ipix = 105;
651 v1 = PixTools.getInstance().pix2vect_nest(nside, ipix);
652 v1.normalize();
653 double phi1 = Math.atan2(v1.y, v1.x);
654 if (phi1 < 0.)
655 phi1 += TWOPI;
656 double[] tetphi = new double[2];
657 tetphi = PixTools.getInstance().pix2ang_nest(nside, ipix);
658 assertEquals("phi = " + phi1, 0.0, Math.abs(phi1 - tetphi[1]), 1e-10);
659 nside = 4;
660 ipix = 74;
661 v1 = PixTools.getInstance().pix2vect_nest(nside, ipix);
662 v1.normalize();
663 phi1 = Math.atan2(v1.y, v1.x);
664 if (phi1 < 0.)
665 phi1 += TWOPI;
666 tetphi = new double[2];
667 tetphi = PixTools.getInstance().pix2ang_nest(nside, ipix);
668 assertEquals("phi = " + phi1, 0.0, Math.abs(phi1 - tetphi[1]), 1e-10);
669
670 System.out.println(" test pix2vect_nest is done");
671 System.out.println("-------------------------------------------");
672 }
673
674 /**
675 * tests conversion from vector to pixel number
676 * @throws Exception
677 */
678 public void testVect2Pix_ring() throws Exception {
679 System.out.println("Start test Vect2Pix_ring !!!!!!!!!!!!!!!!!!!");
680
681 long nside = 4;
682 long ipix = 83;
683 long respix = 0;
684
685 Vector3d v1 = new Vector3d(0., 0., 0.);
686 v1 = PixTools.getInstance().pix2vect_ring(nside, ipix);
687 respix = (int) PixTools.getInstance().vect2pix_ring(nside, v1);
688 assertEquals("respix = " + respix, 83, respix, 1e-10);
689 // Hi resolution test
690 long nside1 = 1 << 20;
691 long maxpix= PixTools.getInstance().Nside2Npix(nside1);
692 System.out.println("nside="+nside1+" maxpix="+maxpix);
693 Vector3d v2 = new Vector3d( -0.704, 0.580, 0.408);
694 respix = PixTools.getInstance().vect2pix_ring(nside1, v2); // ring pixel
695 long respixN = PixTools.getInstance().ring2nest(nside1, respix); // convert to nest
696 long respixNC = PixTools.getInstance().vect2pix_nest(nside1,v2); // nest pixel from the same vector
697 long respixR = PixTools.getInstance().nest2ring(nside1, respixN); // convert pixel
698 System.out.println(" orig="+respix+" doubleT="+respixR+" nest="+respixN+" correct nest="+respixNC);
699 assertEquals("ringpix = " + respix, respix, respixR);
700 assertEquals("nestpix = " + respixNC, respixNC, respixN);
701 System.out.println("------------------------------------------");
702 System.out.println(" test vect2pix_ring is done");
703 }
704
705 /**
706 * tests conversion from vector to pixel number
707 * @throws Exception
708 */
709 public void testVect2Pix_nest() throws Exception {
710 System.out.println("Start test Vect2Pix_nest !!!!!!!!!!!!!!!!!!!");
711 long nside = 4;
712 long ipix = 83;
713 long respix = 0;
714
715 Vector3d v1 = new Vector3d(0., 0., 0.);
716 v1 = PixTools.getInstance().pix2vect_ring(nside, ipix);
717 respix = (int) PixTools.getInstance().vect2pix_nest(nside, v1);
718 assertEquals("respix = " + respix, 123, respix, 1e-10);
719 //
720 long nside1 = 1 << 20;
721 long maxpix= PixTools.getInstance().Nside2Npix(nside1);
722 System.out.println("nside="+nside1+" maxpix="+maxpix);
723 Vector3d v2 = new Vector3d( -0.704, 0.580, 0.408);
724 respix = PixTools.getInstance().vect2pix_nest(nside1, v2);
725 long respixRC = PixTools.getInstance().vect2pix_ring(nside1, v2);
726 long respixR = PixTools.getInstance().nest2ring(nside1, respix);
727 long respixN = PixTools.getInstance().ring2nest(nside1, respixRC);
728 System.out.println(" orig="+respix+" doubleT="+respixN+" ring="+respixR+" correct ring="+respixRC);
729 assertEquals("ringpix = " + respixRC, respixRC, respixR);
730 assertEquals("nestpix = " + respix, respix, respixN);
731 System.out.println("------------------------------------------");
732 System.out.println(" test vect2pix_nest is done");
733 }
734
735 /**
736 * tests Query_Strip method
737 * @throws Exception
738 */
739 public void testQuery_Strip() throws Exception {
740 System.out.println(" Start test query Strip !!!!!!!!!!!!!!!!");
741
742 int[] pixel1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
743 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
744 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
745 48, 49, 50, 51, 52, 53, 54, 55 };
746 int nside = 4;
747 int nest = 0;
748 double theta1 = 0.0;
749 double theta2 = Math.PI / 4.0 + 0.2;
750 ArrayList pixlist;
751 pixlist = PixTools.getInstance().query_strip(nside, theta1, theta2, nest);
752 int nlist = pixlist.size();
753 for (int i = 0; i < nlist; i++) {
754 long ipix = ((Long) pixlist.get(i)).longValue();
755 assertEquals("pixel = " + ipix, pixel1[i], ipix, 1e-10);
756 }
757
758 System.out.println(" test query_strip is done");
759
760 }
761
762 /**
763 * tests Query_Disc method
764 * @throws Exception
765 */
766 public void testQuery_Disc() throws Exception {
767 System.out.println(" Start test query_disc !!!!!!!!!!!!!!!!!!!!!");
768 long nside = 4;
769 int nest = 0;
770 long ipix = 0;
771
772
773 int[] pixel1 = {45, 46, 60, 61, 62, 77, 78, 92, 93, 94,
774 109, 110, 124, 125, 126, 141, 142
775 };
776
777
778
779 int[] pixel2 = {24, 19, 93, 18, 17, 87, 16, 86, 85,
780 106, 84, 159, 81, 158, 157, 155, 156
781 };
782
783
784 int[] pixel3 = { 52, 79, 49, 78, 77, 75, 76, 74, 73, 70,
785 72, 67, 189, 66, 65, 183, 64
786 };
787 int inclusive = 1;
788 double radius = Math.PI / 8.0;
789 Vector3d v = PixTools.getInstance().pix2vect_ring(nside, 93);
790 ArrayList pixlist;
791 pixlist = PixTools.getInstance().query_disc(nside, v, radius, nest, inclusive);
792
793 int nlist = pixlist.size();
794
795 for (int i = 0; i < nlist; i++) {
796 ipix = ((Long) pixlist.get(i)).longValue();
797 // System.out.println("i="+i+"pixel="+ipix+" pixel1="+pixel1[i]);
798 assertEquals("pixel = " + ipix, pixel1[i], ipix, 1e-10);
799 }
800 radius = Math.PI/2.0;
801 nest = 0;
802 v = new Vector3d(1.0,0.0,0.0);
803 ArrayList pixlistT = PixTools.getInstance().query_disc(2, v, radius, nest, inclusive);
804
805 radius = Math.PI / 8.0;
806 v = PixTools.getInstance().pix2vect_ring(nside, 93);
807 nest = 1;
808 ArrayList pixlist2 = PixTools.getInstance().query_disc(nside, v, radius, nest, inclusive);
809
810 int nlist2 = pixlist2.size();
811
812 assertEquals("npix="+nlist, nlist2, pixel2.length, 1e-10);
813 for (int i = 0; i < nlist2; i++) {
814 ipix = ((Long) pixlist2.get(i)).longValue();
815 // System.out.println("i="+i+"pixel="+ipix+" pixel2="+pixel2[i]);
816 assertEquals("pixel = " + ipix, pixel2[i], ipix, 1e-10);
817
818 }
819
820 v = PixTools.getInstance().pix2vect_ring(nside, 103);
821 nest = 1;
822 inclusive = 1;
823 ArrayList pixlist3 = PixTools.getInstance().query_disc(nside, v, radius, nest, inclusive);
824 nlist = pixlist3.size();
825 assertEquals("npix="+nlist, nlist, pixel3.length, 1e-10);
826
827 for (int i = 0; i < pixlist3.size(); i++) {
828 ipix = ((Long) pixlist3.get(i)).longValue();
829 // System.out.println("i="+i+"pixel="+ipix+" pixel3="+pixel3[i]);
830 assertEquals("pixel = " + ipix, pixel3[i], ipix, 1e-10);
831 }
832
833
834 for (int i=0; i<pixel1.length; i++) {
835 long ipixR = pixel1[i];
836 long ipixT = PixTools.getInstance().ring2nest(nside, ipixR);
837 assertEquals("pixel="+ipixT, ipixT, pixel2[i], 1e-10);
838 long ipixN = PixTools.getInstance().nest2ring(nside, ipixT);
839 assertEquals("pixel="+ipixN, ipixN, pixel1[i], 1e-10);
840 }
841 System.out.println(" End query_disk test ______________________________");
842 }
843
844 /**
845 * tests Query_Triangle method
846 * @throws Exception
847 */
848 public void testQuery_Triangle() throws Exception {
849
850 long nside = 4;
851 int nest = 0;
852 long ipix = 0;
853 int[] pixel1 = { 57, 58, 59, 60, 61, 62, 74, 75, 76, 77, 78, 90, 91,
854 92, 93, 107, 108, 109, 123, 124, 140 };
855 int[] pixel2 = { 88, 89, 90, 91, 105, 106, 107, 108, 121, 122, 123,
856 138, 139, 154 };
857 int[] pixel3 = { 49, 64, 80, 81, 95, 96, 112, 113,
858 127, 128, 142, 143, 144, 145 };
859 int[] pixel4 = { 36, 52, 53, 67, 68, 69, 83, 84, 85, 86, 98, 99, 100,
860 101, 102, 114, 115, 116, 117, 118, 119, 129, 130, 131, 132,
861 133, 134, 135 };
862 int[] pixel5 = { 58, 127, 56, 126, 125, 50, 123, 124, 119, 48, 122,
863 121, 118, 117, 74, 175, 120, 115, 116, 191, 72, 174, 173, 114,
864 113, 190, 189, 66 };
865 int[] pixel6 = { 110, 123, 124, 125, 140, 141, 156 };
866 int[] pixel7 = { 53, 68, 69 };
867 long pix1 = 62;
868 long pix2 = 57;
869 long pix3 = 140;
870 System.out.println("Start test Query Triangle !!!!!!!!!!!!!!!!!!!!");
871 Vector3d v11 = PixTools.getInstance().pix2vect_ring(nside, pix1);
872
873 Vector3d v22 = PixTools.getInstance().pix2vect_ring(nside, pix2);
874
875 Vector3d v33 = PixTools.getInstance().pix2vect_ring(nside, pix3);
876
877 // System.out.println("nside="+nside+" triangle pixels "+pix1+" "+pix2+"
878 // "+pix3);
879 int inclusive = 0;
880
881 ArrayList pixlist;
882 pixlist = PixTools.getInstance().query_triangle(nside, v11, v22, v33, nest, inclusive);
883
884 int nlist = pixlist.size();
885
886 for (int i = 0; i < nlist; i++) {
887 ipix = ((Long) pixlist.get(i)).longValue();
888
889 assertEquals("pixel = " + ipix, pixel1[i], ipix, 1e-10);
890 }
891 pix1 = 92;
892 pix2 = 88;
893 pix3 = 154;
894 v11 = PixTools.getInstance().pix2vect_ring(nside, pix1);
895 v22 = PixTools.getInstance().pix2vect_ring(nside, pix2);
896 v33 = PixTools.getInstance().pix2vect_ring(nside, pix3);
897
898 inclusive = 0;
899 ArrayList pixlist1;
900 pixlist1 = PixTools.getInstance().query_triangle(nside, v11, v22, v33, nest, inclusive);
901
902 nlist = pixlist1.size();
903
904 for (int i = 0; i < nlist; i++) {
905 ipix = ((Long) pixlist1.get(i)).longValue();
906 // System.out.println(ipix);
907 assertEquals("pixel = " + ipix, pixel2[i], ipix, 1e-10);
908 }
909 pix1 = 49;
910 pix2 = 142;
911 pix3 = 145;
912 v11 = PixTools.getInstance().pix2vect_ring(nside, pix1);
913 v22 = PixTools.getInstance().pix2vect_ring(nside, pix2);
914 v33 = PixTools.getInstance().pix2vect_ring(nside, pix3);
915
916 inclusive = 0;
917 ArrayList pixlist2;
918 pixlist2 = PixTools.getInstance().query_triangle(nside, v11, v22, v33, nest, inclusive);
919
920 nlist = pixlist2.size();
921
922 for (int i = 0; i < nlist; i++) {
923 ipix = ((Long) pixlist2.get(i)).longValue();
924 // System.out.println(ipix);
925 assertEquals("pixel = " + ipix, pixel3[i], ipix, 1e-10);
926 }
927 pix1 = 36;
928 pix2 = 129;
929 pix3 = 135;
930 v11 = PixTools.getInstance().pix2vect_ring(nside, pix1);
931 v22 = PixTools.getInstance().pix2vect_ring(nside, pix2);
932 v33 = PixTools.getInstance().pix2vect_ring(nside, pix3);
933
934 inclusive = 0;
935
936 pixlist2 = PixTools.getInstance().query_triangle(nside, v11, v22, v33, nest, inclusive);
937
938 nlist = pixlist2.size();
939
940 for (int i = 0; i < nlist; i++) {
941 ipix = ((Long) pixlist2.get(i)).longValue();
942 assertEquals("pixel = " + ipix, pixel4[i], ipix, 1e-10);
943 }
944 pix1 = 36;
945 pix2 = 129;
946 pix3 = 135;
947 nest = 1;
948 v11 = PixTools.getInstance().pix2vect_ring(nside, pix1);
949 v22 = PixTools.getInstance().pix2vect_ring(nside, pix2);
950 v33 = PixTools.getInstance().pix2vect_ring(nside, pix3);
951 inclusive = 0;
952
953 pixlist2 = PixTools.getInstance().query_triangle(nside, v11, v22, v33, nest, inclusive);
954
955 nlist = pixlist2.size();
956
957 for (int i = 0; i < nlist; i++) {
958 ipix = ((Long) pixlist2.get(i)).longValue();
959 // System.out.println("ipix="+ipix);
960 assertEquals("pixel = " + ipix, pixel5[i], ipix, 1e-10);
961 }
962 pix1 = 123;
963 pix2 = 156;
964 pix3 = 110;
965 nest = 0;
966 v11 = PixTools.getInstance().pix2vect_ring(nside, pix1);
967 v22 = PixTools.getInstance().pix2vect_ring(nside, pix2);
968 v33 = PixTools.getInstance().pix2vect_ring(nside, pix3);
969 inclusive = 0;
970
971 pixlist2 = PixTools.getInstance().query_triangle(nside, v11, v22, v33, nest, inclusive);
972
973 nlist = pixlist2.size();
974
975 for (int i = 0; i < nlist; i++) {
976 ipix = ((Long) pixlist2.get(i)).longValue();
977 assertEquals("pixel = " + ipix, pixel6[i], ipix, 1e-10);
978 // System.out.println("i="+i+" pixel#="+ipix);
979 }
980 pix1 = 69;
981 pix2 = 53;
982 pix3 = 68;
983 nest = 0;
984 v11 = PixTools.getInstance().pix2vect_ring(nside, pix1);
985 v22 = PixTools.getInstance().pix2vect_ring(nside, pix2);
986 v33 = PixTools.getInstance().pix2vect_ring(nside, pix3);
987 inclusive = 0;
988
989 pixlist2 = PixTools.getInstance().query_triangle(nside, v11, v22, v33, nest, inclusive);
990
991 nlist = pixlist2.size();
992
993 for (int i = 0; i < nlist; i++) {
994 ipix = ((Long) pixlist2.get(i)).longValue();
995 assertEquals("pixel = " + ipix, pixel7[i], ipix, 1e-10);
996 // System.out.println("i="+i+" pixel#="+ipix);
997 }
998 System.out.println(" test query_triangle is done");
999
1000 }
1001
1002 /**
1003 * tests Query_Poligon method
1004 * @throws Exception
1005 */
1006 public void testQuery_Polygon() throws Exception {
1007
1008 long nside = 4;
1009 int nest = 0;
1010 long ipix = 0;
1011 int inclusive = 0;
1012 int[] result = { 51, 52, 53, 66, 67, 68, 69, 82, 83, 84, 85, 86, 98,
1013 99, 100, 101, 115, 116, 117 };
1014 int[] result1 = { 55, 70, 71, 87 };
1015 int[] result2 = { 137, 152, 153, 168 };
1016 int[] result3 = { 27, 43, 44, 58, 59, 60, 74, 75, 76, 77, 89, 90, 91,
1017 92, 93, 105, 106, 107, 108, 109, 110, 121, 122, 123, 124, 125,
1018 138, 139, 140, 141, 154, 156 };
1019
1020
1021 System.out.println("Start test query_polygon !!!!!!!!!!!!!!!!!!!!!!");
1022 ArrayList vlist = new ArrayList();
1023 Vector3d v = PixTools.getInstance().pix2vect_ring(nside, 53);
1024 vlist.add( v);
1025 v = PixTools.getInstance().pix2vect_ring(nside, 51);
1026 vlist.add( v);
1027 v = PixTools.getInstance().pix2vect_ring(nside, 82);
1028 vlist.add( v);
1029 v = PixTools.getInstance().pix2vect_ring(nside, 115);
1030 vlist.add( v);
1031 v = PixTools.getInstance().pix2vect_ring(nside, 117);
1032 vlist.add( v);
1033 v = PixTools.getInstance().pix2vect_ring(nside, 86);
1034 vlist.add( v);
1035
1036 ArrayList pixlist;
1037 pixlist = PixTools.getInstance().query_polygon(nside, vlist, nest, inclusive);
1038 // System.out.println(" List size="+pixlist.size());
1039 int nlist = pixlist.size();
1040 // System.out.println(" Pixel list:");
1041 for (int i = 0; i < nlist; i++) {
1042 ipix = ((Long) pixlist.get(i)).longValue();
1043 assertEquals("pixel = " + ipix, result[i], ipix, 1e-10);
1044 // System.out.println("i="+i+" pixel # "+ipix);
1045 }
1046
1047 /* Yet another test */
1048
1049 ArrayList vlist1 = new ArrayList();
1050 v = PixTools.getInstance().pix2vect_ring(nside, 71);
1051 vlist1.add( v);
1052 v = PixTools.getInstance().pix2vect_ring(nside, 55);
1053 vlist1.add( v);
1054 v = PixTools.getInstance().pix2vect_ring(nside, 70);
1055 vlist1.add( v);
1056 v = PixTools.getInstance().pix2vect_ring(nside, 87);
1057 vlist1.add( v);
1058 pixlist = PixTools.getInstance().query_polygon(nside, vlist1, nest, inclusive);
1059
1060 nlist = pixlist.size();
1061
1062 for (int i = 0; i < nlist; i++) {
1063 ipix = ((Long) pixlist.get(i)).longValue();
1064 // System.out.println("i="+i+" pixel # "+ipix);
1065 assertEquals("pixel = " + ipix, result1[i], ipix, 1e-10);
1066 }
1067
1068 /* Yet another test */
1069 ArrayList vlist2 = new ArrayList();
1070 v = PixTools.getInstance().pix2vect_ring(nside, 153);
1071 vlist2.add( v);
1072 v = PixTools.getInstance().pix2vect_ring(nside, 137);
1073 vlist2.add( v);
1074 v = PixTools.getInstance().pix2vect_ring(nside, 152);
1075 vlist2.add( v);
1076 v = PixTools.getInstance().pix2vect_ring(nside, 168);
1077 vlist2.add( v);
1078 pixlist = PixTools.getInstance().query_polygon(nside, vlist2, nest, inclusive);
1079
1080 nlist = pixlist.size();
1081
1082 for (int i = 0; i < nlist; i++) {
1083 ipix = ((Long) pixlist.get(i)).longValue();
1084 assertEquals("pixel = " + ipix, result2[i], ipix, 1e-10);
1085 // System.out.println("i="+i+" pixel # "+ipix);
1086 }
1087 /* Yet another test */
1088
1089 ArrayList vlist3 = new ArrayList();
1090 v = PixTools.getInstance().pix2vect_ring(nside, 110);
1091 vlist3.add( v);
1092 v = PixTools.getInstance().pix2vect_ring(nside, 27);
1093 vlist3.add( v);
1094 v = PixTools.getInstance().pix2vect_ring(nside, 105);
1095 vlist3.add( v);
1096 v = PixTools.getInstance().pix2vect_ring(nside, 154);
1097 vlist3.add( v);
1098 v = PixTools.getInstance().pix2vect_ring(nside, 123);
1099 vlist3.add( v);
1100 v = PixTools.getInstance().pix2vect_ring(nside, 156);
1101 vlist3.add( v);
1102 pixlist = PixTools.getInstance().query_polygon(nside, vlist3, nest, inclusive);
1103
1104 nlist = pixlist.size();
1105
1106 for (int i = 0; i < nlist; i++) {
1107 ipix = ((Long) pixlist.get(i)).longValue();
1108 assertEquals("pixel = " + ipix, result3[i], ipix, 1e-10);
1109 // System.out.println("i="+i+" pixel # "+ipix);
1110 }
1111 System.out.println(" test query_polygon is done");
1112
1113 }
1114 /**
1115 * tests MaxResolution method
1116 */
1117 public void testMaxResolution() {
1118 System.out.println(" Start test MaxRes !!!!!!!!!!!!!!!!!!!!!");
1119
1120 long nside = 1048576;
1121
1122 double res = PixTools.getInstance().PixRes(nside);
1123 System.out.println("Minimum size of the pixel side is "+res+" arcsec.");
1124 assertEquals("res = " + res, 0.2, res, 1e-1);
1125 long nsideR = PixTools.getInstance().GetNSide(res);
1126 assertEquals("nside = " + nside, nside, nsideR, 1e-1);
1127 System.out.println(" End of MaxRes test _______________________");
1128 }
1129 /**
1130 * tests QueryDiscResolution method
1131 * @throws Exception
1132 */
1133 public void testQueryDiscRes() throws Exception {
1134 System.out.println(" Start test DiscRes !!!!!!!!!!!!!!!!!!!!!");
1135
1136
1137 int nest = 0;
1138 long ipix = 0;
1139
1140 int inclusive = 0;
1141 double theta= Math.PI;
1142 double phi = Math.PI;
1143 double radius = Math.toRadians(0.2/3600.); // One arcse
1144 long nside = PixTools.getInstance().GetNSide(radius);
1145 System.out.println(" calculated nside="+nside);
1146 long cpix = PixTools.getInstance().ang2pix_ring(nside,theta,phi);
1147 Vector3d vc = PixTools.getInstance().pix2vect_ring(nside, cpix);
1148 ArrayList pixlist;
1149 pixlist = PixTools.getInstance().query_disc(nside, vc, radius, nest, inclusive);
1150
1151 int nlist = pixlist.size();
1152 for (int i = 0; i < nlist; i++) {
1153 ipix = ((Long) pixlist.get(i)).longValue();
1154 Vector3d v = PixTools.getInstance().pix2vect_ring(nside,ipix);
1155 double dist = PixTools.getInstance().AngDist(v,vc);
1156 assertTrue(dist<=2.*radius);
1157 }
1158 cpix = PixTools.getInstance().ang2pix_nest(nside,theta,phi);
1159 Vector3d vc1 = PixTools.getInstance().pix2vect_nest(nside, cpix);
1160 ArrayList pixlist1;
1161 nest = 1;
1162 radius *=4;
1163 pixlist1 = PixTools.getInstance().query_disc(nside, vc1, radius, nest, inclusive);
1164 int nlist1 = pixlist1.size();
1165 for (int i = 0; i < nlist1; i++) {
1166 ipix = ((Long) pixlist1.get(i)).longValue();
1167 Vector3d v = PixTools.getInstance().pix2vect_nest(nside,ipix);
1168 double dist = PixTools.getInstance().AngDist(v,vc1);
1169 assertTrue(dist<=2.*radius);
1170 }
1171 System.out.println(" test query disk is done -------------------");
1172 }
1173 /**
1174 * test Query_disk check for consistency in the query for RING/NESTED
1175 */
1176 public void testQuery_disk2() {
1177 System.out.println(" Start test query_disk HiRes!!!!!!!!!!!!!!!!!!!!!!!!");
1178
1179 long nside = 1 << 20 ;
1180 double res = PixTools.getInstance().PixRes(nside);
1181 System.out.println("nside="+nside+" sresolution="+res);
1182 double radius = Math.toRadians(res/3600.)/2.;
1183 System.out.println("radius="+radius);
1184 Vector3d v1 = new Vector3d(-0.704, 0.580, 0.408);
1185 System.out.println("!!!!!!!!!!!!! NESTED !!!!!!!!!!!");
1186 ArrayList diskQ = PixTools.getInstance().query_disc(nside,
1187 v1,
1188 radius, 1,1); // inclusive query at vector point
1189 assertEquals("npixels = " + diskQ.size(), 8, diskQ.size() , 1e-1);
1190 long pix1 = PixTools.getInstance().vect2pix_nest(nside, v1);
1191 Vector3d v2 = PixTools.getInstance().pix2vect_nest(nside, pix1); // vector to pix center
1192 //
1193 ArrayList diskQ2 = PixTools.getInstance().query_disc(nside,
1194 v2,
1195 radius, 1,1); // inclusive with point at pixel center
1196 assertEquals("npixels = " + diskQ2.size(), 9, diskQ2.size() , 1e-1);
1197
1198 //
1199 ArrayList diskQ3 = PixTools.getInstance().query_disc(nside,
1200 v2,
1201 radius, 1,0); // exclusive with point at pixel center
1202 assertEquals("npixels = " + diskQ3.size(), 1, diskQ3.size() , 1e-1);
1203
1204 // RING schema
1205 System.out.println("!!!!!!!!!!!!! RING !!!!!!!!!!!");
1206 ArrayList diskQ4 = PixTools.getInstance().query_disc(nside,
1207 v1,
1208 radius, 0,1); // inclusiv at vector point
1209 assertEquals("npixels = " + diskQ4.size(), 8, diskQ4.size() , 1e-1);
1210 //
1211
1212 ArrayList diskQ5 = PixTools.getInstance().query_disc(nside,
1213 v2,
1214 radius, 0, 1); // inclusive at pixel center
1215 assertEquals("npixels = " + diskQ5.size(), 9, diskQ5.size() , 1e-1);
1216
1217 // System.out.println("n pixels in disk5 ="+diskQ5.size());
1218 ArrayList diskQ6 = PixTools.getInstance().query_disc(nside,
1219 v2,
1220 radius, 0,0); // exclusive at pixel center
1221 assertEquals("npixels = " + diskQ6.size(), 1, diskQ6.size() , 1e-1);
1222 //
1223 // test HiRes conversions
1224 //
1225 Vector3d pos = new Vector3d( -0.704, 0.580, 0.408 );
1226
1227 nside = 1 << 20;
1228 System.out.println("HiRes transformation tests: nside="+nside);
1229 ArrayList nestPixels = PixTools.getInstance().query_disc(nside, pos, radius, 1, 1);
1230 ArrayList ringPixels = PixTools.getInstance().query_disc(nside, pos, radius, 0, 1);
1231 assertEquals(nestPixels.size(), ringPixels.size());
1232 for(int i=0; i< ringPixels.size(); i++) {
1233 long iring = ((Number)ringPixels.get(i)).longValue();
1234 Vector3d cv = PixTools.getInstance().pix2vect_ring(nside, iring);
1235 long inest = PixTools.getInstance().ring2nest(nside, iring);
1236 long inestC = ((Number)nestPixels.get(i)).longValue();
1237 Vector3d cvN = PixTools.getInstance().pix2vect_nest(nside, inestC);
1238 long iringT = PixTools.getInstance().nest2ring(nside, inestC);
1239 assertEquals(iring,iringT);
1240 assertEquals(inest,inestC);
1241 assertEquals(" Xv="+cv.x,cv.x,cvN.x,1.e-10);
1242 assertEquals(" Yv="+cv.y,cv.y,cvN.y,1.e-10);
1243 assertEquals(" Zv="+cv.z,cv.z,cvN.z,1.e-10);
1244 // System.out.println(" inest orig="+inestC+" transformed="+inest+" iring orig="+iring+" transf="+iringT);
1245 // System.out.println("Vector cv vs cvN x="+cv.x+" cvN.x="+cvN.x);
1246 // System.out.println("Vector cv vs cvN y="+cv.y+" cvN.y="+cvN.y);
1247 // System.out.println("Vector cv vs cvN z="+cv.z+" cvN.z="+cvN.z);
1248 double[] tetphiR = PixTools.getInstance().pix2ang_ring(nside, iring);
1249 double[] tetphiN= PixTools.getInstance().pix2ang_nest(nside, inestC);
1250 assertEquals(" theta="+tetphiR[0],tetphiR[0],tetphiN[0],1.e-10);
1251 assertEquals(" phi="+tetphiR[1],tetphiR[1],tetphiN[1],1.e-10);
1252 // System.out.println("theta R vs N "+tetphiR[0]+" "+tetphiN[0]);
1253 // System.out.println("phi R vs N "+tetphiR[1]+" "+tetphiN[1]);
1254 }
1255
1256 System.out.println(" End test of query_disc2____________________________");
1257
1258 }
1259 /**
1260 * tests GetNside method
1261 */
1262 public void testGetNside() {
1263 System.out.println(" Start test GetNside !!!!!!!!!!!!!!!!!!!!!");
1264
1265 double pixsize = 0.3;
1266 long nside = PixTools.getInstance().GetNSide(pixsize);
1267 System.out.println("Requared nside is "+nside);
1268 assertEquals("nside = " + nside, 1048576, nside, 1e-1);
1269 System.out.println(" End of GetNSide test _______________________");
1270 }
1271
1272 /**
1273 * test conversion of Ra Dec to polar coordinates
1274 */
1275 public void testRaDecToPolar() {
1276 System.out.println(" Start test RaDecToPolar !!!!!!!!!!!!!!!!!!!!!");
1277
1278 double [] radec = new double[2];
1279 radec[0] = 312.115456;
1280 radec[1] = -1.153759;
1281 double[] polar = PixTools.getInstance().RaDecToPolar(radec);
1282 assertEquals("theta = " + polar[0], 1.5909332201194137, polar[0], 1e-10);
1283 assertEquals("phi = " + polar[1], 5.447442353563491, polar[1], 1e-10);
1284 System.out.println("End test RaDecToPolar__________________________");
1285
1286 }
1287 }
0 package gov.fnal.eag.healpix.test;
1
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.OutputStream;
6 import java.io.PrintStream;
7 import java.util.ArrayList;
8 import gov.fnal.eag.healpix.BitManipulation;
9 import gov.fnal.eag.healpix.PixTools;
10
11 import javax.vecmath.Vector3d;
12
13 import junit.framework.TestCase;
14
15 /**
16 * test suit for PixTools using static constructor
17 *
18 * @author N. Kuropatkin
19 *
20 */
21
22 public class PixToolsStaticTest extends TestCase {
23 private static PixTools pixTools = new PixTools();
24
25 /**
26 * test MODULO function
27 */
28 public void testMODULO() {
29 //
30 double A = 8.;
31 double B = 5.;
32 double res = BitManipulation.MODULO(A, B);
33 assertEquals("modulo = " + res, 3., res, 1e-10);
34 A = -8.;
35 B = 5.;
36 res = BitManipulation.MODULO(A, B);
37 assertEquals("modulo = " + res, 2., res, 1e-10);
38 A = 8.;
39 B = -5.;
40 res = BitManipulation.MODULO(A, B);
41 assertEquals("modulo = " + res, -2., res, 1e-10);
42 A = -8.;
43 B = -5.;
44 res = BitManipulation.MODULO(A, B);
45 assertEquals("modulo = " + res, -3., res, 1e-10);
46 System.out.println(" test MODULO is done");
47 }
48
49 /**
50 * tests angular distance calculation
51 * @throws Exception
52 */
53 public void testAngDist() throws Exception {
54 Vector3d v1 = new Vector3d(1.5, 1.6, 2.0);
55 Vector3d v2 = new Vector3d(1.2, 1.0, 1.5);
56
57 double res1 = pixTools.AngDist(v1, v2);
58 double res2 = v1.angle(v2);
59 System.out.println("res1 = " + res1 + " res2=" + res2);
60 assertEquals("angular Distance=" + res2, 1.0, res1 / res2, 1e-10);
61 /* Check known problem with vecmath for small vector differences */
62
63 Vector3d v3 = new Vector3d(1.5, 1.6, 0.);
64 Vector3d v4 = new Vector3d(1.5, 1.601, 0.);
65 double res3 = pixTools.AngDist(v3, v4);
66 double res4 = v3.angle(v4);
67 //
68 System.out.println("res3 = " + res3 + " res4=" + res4);
69 assertEquals("angular Distance=" + res4, 1., (Math.PI / 2. + res3)
70 / res4, 1e-3);
71 Vector3d v5 = new Vector3d(1.5, 1.6, 0.);
72 Vector3d v6 = new Vector3d(-1.5, -1.75, 0.);
73 double res5 = pixTools.AngDist(v5, v6);
74 double res6 = v5.angle(v6);
75 System.out.println("res5 = " + res5 + " res6=" + res6);
76 assertEquals("angular Distance=" + res6, 2.0, res5 / res6, 1e-10);
77 System.out.println(" test of AngDist is done");
78 }
79
80 /**
81 * @throws Exception
82 */
83 public void testSurfaceTriangle() throws Exception {
84 Vector3d v1 = new Vector3d(1.0, 0.0, 0.0);
85 Vector3d v2 = new Vector3d(0.0, 1.0, 0.0);
86 Vector3d v3 = new Vector3d(0.0, 0.0, 1.0);
87 PixTools pt = new PixTools();
88 double res = pixTools.SurfaceTriangle(v1, v2, v3);
89 System.out.println("Triangle surface is=" + res / Math.PI
90 + " steredians");
91 assertEquals("Triangle surface=" + res, 0.5, res / Math.PI, 1e-10);
92 System.out.println(" test of SurfaceTriangle is done");
93 }
94
95 /**
96 * tests calculation of npixels from nside
97 */
98 public void testNside2Npix() {
99 int nside = 1;
100 int npix = 0;
101 npix = (int) pixTools.Nside2Npix(nside);
102 assertEquals("Npix=" + npix, 12, npix, 1e-10);
103 nside = 2;
104 npix = (int) pixTools.Nside2Npix(nside);
105 assertEquals("Npix=" + npix, 48, npix, 1e-10);
106 }
107
108 /**
109 * tests calculation of nsides from npixels
110 */
111 public void testNpix2Nside() {
112 int npix = 12;
113 int nside = 0;
114 nside = (int) pixTools.Npix2Nside(npix);
115
116 double pixSize1 = pixTools.PixRes(65536);
117 long nside1 = pixTools.GetNSide(pixSize1);
118 assertEquals("Nside=" + nside1, 65536, nside1, 1e-10);
119
120 assertEquals("Nside=" + nside, 1, nside, 1e-10);
121
122 }
123
124 /**
125 * test of directional angles calculation
126 */
127 public void testVec2Ang() {
128 double PI = Math.PI;
129 Vector3d v = new Vector3d(0.0, 1.0, 0.0);
130 double[] ang_tup = { 0., 0. };
131 ang_tup = pixTools.Vect2Ang(v);
132 System.out.println(" Theta=" + ang_tup[0] / PI + " Phi=" + ang_tup[1]
133 / PI);
134 assertEquals("Theta=" + ang_tup[0], 0.5, ang_tup[0] / PI, 1e-10);
135 assertEquals("Phi=" + ang_tup[1], 0.5, ang_tup[1] / PI, 1e-10);
136 v = new Vector3d(1.0, 0.0, 0.0);
137 ang_tup = pixTools.Vect2Ang(v);
138 assertEquals("phi=" + ang_tup[1], 0., ang_tup[1] / PI, 1e-10);
139 System.out.println(" test Vect2Ang is done");
140 }
141
142 /**
143 * tests calculation of pixel from polar angles
144 * in ring schema of pixalization
145 * @throws Exception
146 */
147 public void testAng2Pix() throws Exception {
148 System.out.println(" Test ang2pix_ring ___________________");
149 double PI = Math.PI;
150 long pix = -1;
151 double theta = PI / 2. - 0.2;
152 double phi = PI / 2. ;
153 long nside = 4;
154 try {
155 pix = pixTools.ang2pix_ring(nside,theta, phi);
156 } catch (Exception e) {
157 e.printStackTrace();
158 }
159 Vector3d v = pixTools.Ang2Vec(theta,phi);
160 long pix1 = pixTools.vect2pix_ring(nside,v);
161 assertEquals("pix=" + pix, pix1, pix, 1e-10);
162 assertEquals("pix=" + pix, 76, pix, 1e-10);
163
164 long pix2 = pixTools.ang2pix_nest(nside,theta,phi);
165 long pix3 = pixTools.vect2pix_nest(nside,v);
166 assertEquals("pix2=" + pix2, pix3, pix2, 1e-10);
167 assertEquals("pix2=" + pix2, 92, pix2, 1e-10);
168
169
170 double[] radec = pixTools.pix2ang_ring(nside,76);
171 assertEquals("theta=" + theta, theta, radec[0], 4e-2);
172 assertEquals("phi=" + phi, radec[1], phi, 1e-2);
173 double[] radec1 = pixTools.pix2ang_nest(nside,92);
174 System.out.println("theta="+radec1[0]+" phi="+radec1[1]);
175 assertEquals("theta=" + theta, theta, radec1[0], 4e-2);
176 assertEquals("phi=" + phi, radec1[1], phi, 1e-2);
177 System.out.println(" test Ang2Pix is done");
178 }
179
180 /**
181 * tests calculation of unit vector from polar angles
182 * @throws Exception
183 */
184 public void testAng2Vect() throws Exception {
185 System.out.println(" Start test Ang2Vect----------------");
186 double PI = Math.PI;
187 double theta = PI / 2.;
188 double phi = PI / 2;
189 Vector3d v = pixTools.Ang2Vec(theta, phi);
190 System.out.println("Vector x=" + v.x + " y=" + v.y + " z=" + v.z);
191 assertEquals("x=" + v.x, 0., v.x, 1e-10);
192 assertEquals("y=" + v.y, 1., v.y, 1e-10);
193 assertEquals("z=" + v.z, 0., v.z, 1e-10);
194 System.out.println(" test Ang2Vect is done");
195 }
196
197 /**
198 * tests calculation of ring number from z coordinate
199 * @throws Exception
200 */
201 public void testRingNum() throws Exception {
202 double z = 0.25;
203 int nside = 1;
204 System.out.println("Start test RingNum !!!!!!!!!!!!!!!!!!!!");
205 int nring = (int) pixTools.RingNum(nside, z);
206 System.out.println("z=" + z + " ring number =" + nring);
207 assertEquals("z=" + z, 2, nring, 1e-10);
208 z = -0.25;
209 nring = (int) pixTools.RingNum(nside, z);
210 assertEquals("z=" + z, 2, nring, 1e-10);
211 z = 0.8;
212 nring = (int) pixTools.RingNum(nside, z);
213 assertEquals("z=" + z, 1, nring, 1e-10);
214 z = -0.8;
215 nring = (int) pixTools.RingNum(nside, z);
216 assertEquals("z=" + z, 3, nring, 1e-10);
217 System.out.println(" test RingNum is done");
218 nside = 4;
219 int pix = 3;
220 Vector3d v = pixTools.pix2vect_ring(nside, pix);
221 z = v.z;
222 nring = (int) pixTools.RingNum(nside, z);
223 assertEquals("z=" + z, 1, nring, 1e-10);
224 pix = 11;
225 v = pixTools.pix2vect_ring(nside, pix);
226 z = v.z;
227 nring = (int) pixTools.RingNum(nside, z);
228 assertEquals("z=" + z, 2, nring, 1e-10);
229 pix = 23;
230 v = pixTools.pix2vect_ring(nside, pix);
231 z = v.z;
232 nring = (int) pixTools.RingNum(nside, z);
233 assertEquals("z=" + z, 3, nring, 1e-10);
234 pix = 39;
235 v = pixTools.pix2vect_ring(nside, pix);
236 z = v.z;
237 nring = (int) pixTools.RingNum(nside, z);
238 assertEquals("z=" + z, 4, nring, 1e-10);
239 pix = 55;
240 v = pixTools.pix2vect_ring(nside, pix);
241 z = v.z;
242 nring = (int) pixTools.RingNum(nside, z);
243 assertEquals("z=" + z, 5, nring, 1e-10);
244 pix = 71;
245 v = pixTools.pix2vect_ring(nside, pix);
246 z = v.z;
247 nring = (int) pixTools.RingNum(nside, z);
248 assertEquals("z=" + z, 6, nring, 1e-10);
249 pix = 87;
250 v = pixTools.pix2vect_ring(nside, pix);
251 z = v.z;
252 nring = (int) pixTools.RingNum(nside, z);
253 assertEquals("z=" + z, 7, nring, 1e-10);
254 pix = 103;
255 v = pixTools.pix2vect_ring(nside, pix);
256 z = v.z;
257 nring = (int) pixTools.RingNum(nside, z);
258 assertEquals("z=" + z, 8, nring, 1e-10);
259 pix = 119;
260 v = pixTools.pix2vect_ring(nside, pix);
261 z = v.z;
262 nring = (int) pixTools.RingNum(nside, z);
263 assertEquals("z=" + z, 9, nring, 1e-10);
264 pix = 135;
265 v = pixTools.pix2vect_ring(nside, pix);
266 z = v.z;
267 nring = (int) pixTools.RingNum(nside, z);
268 assertEquals("z=" + z, 10, nring, 1e-10);
269 pix = 151;
270 v = pixTools.pix2vect_ring(nside, pix);
271 z = v.z;
272 nring = (int) pixTools.RingNum(nside, z);
273 assertEquals("z=" + z, 11, nring, 1e-10);
274 pix = 167;
275 v = pixTools.pix2vect_ring(nside, pix);
276 z = v.z;
277 nring = (int) pixTools.RingNum(nside, z);
278 assertEquals("z=" + z, 12, nring, 1e-10);
279 pix = 169;
280 v = pixTools.pix2vect_ring(nside, pix);
281 z = v.z;
282 nring = (int) pixTools.RingNum(nside, z);
283 assertEquals("z=" + z, 13, nring, 1e-10);
284 pix = 180;
285 v = pixTools.pix2vect_ring(nside, pix);
286 z = v.z;
287 nring = (int) pixTools.RingNum(nside, z);
288 assertEquals("z=" + z, 14, nring, 1e-10);
289 System.out.println("End test RingNum");
290 }
291
292 /**
293 * tests conversion from nest schema pixel to ring schema pixel
294 * @throws Exception
295 */
296 public void testNest2Ring() throws Exception {
297 int ipnest = 3;
298 int nside = 2;
299 int ipring = (int) pixTools.nest2ring(nside, ipnest);
300 assertEquals("ipring=" + ipring, 0, ipring, 1e-10);
301 ipnest = 0;
302 nside = 2;
303 ipring = (int) pixTools.nest2ring(nside, ipnest);
304 assertEquals("ipring=" + ipring, 13, ipring, 1e-10);
305 ipnest = 18;
306 nside = 2;
307 ipring = (int) pixTools.nest2ring(nside, ipnest);
308 assertEquals("ipring=" + ipring, 27, ipring, 1e-10);
309 ipnest = 23;
310 nside = 2;
311 ipring = (int) pixTools.nest2ring(nside, ipnest);
312 assertEquals("ipring=" + ipring, 14, ipring, 1e-10);
313 ipnest = 5;
314 nside = 4;
315 ipring = (int) pixTools.nest2ring(nside, ipnest);
316 assertEquals("ipring = " + ipring, 27, ipring, 1e-10);
317 System.out.println(" test Nest2Ring is done");
318 }
319
320 /**
321 * tests conversion from ring schema pixel to nest schema pixel
322 * @throws Exception
323 */
324 public void testRing2Nest() throws Exception {
325 System.out.println(" start test Ring2Nest !!!!!!!!!!!!!!!!!!!!!!");
326 int ipring = 0;
327 int nside = 2;
328
329 int ipnest = (int) pixTools.ring2nest(nside, ipring);
330 assertEquals("ipnest=" + ipnest, 3, ipnest, 1e-10);
331 ipring = 13;
332 nside = 2;
333 ipnest = (int) pixTools.ring2nest(nside, ipring);
334 assertEquals("ipnest=" + ipnest, 0, ipnest, 1e-10);
335 ipring = 27;
336 nside = 2;
337 ipnest = (int) pixTools.ring2nest(nside, ipring);
338 assertEquals("ipnest=" + ipnest, 18, ipnest, 1e-10);
339 ipring = 14;
340 nside = 2;
341 ipnest = (int) pixTools.ring2nest(nside, ipring);
342 assertEquals("ipnest=" + ipnest, 23, ipnest, 1e-10);
343 ipring = 27;
344 nside = 4;
345 ipnest = (int) pixTools.ring2nest(nside, ipring);
346 assertEquals("ipnest = " + ipnest, 5, ipnest, 1e-10);
347 ipring = 83;
348 nside = 4;
349 ipnest = (int) pixTools.ring2nest(nside, ipring);
350 assertEquals("ipnest = " + ipnest, 123, ipnest, 1e-10);
351 System.out.println(" test Ring2Nest is done");
352 }
353
354 /**
355 * tests Next_In_Line method for the nest schema
356 * @throws Exception
357 */
358 public void testNext_In_Line_Nest() throws Exception {
359 int ipix = 0;
360 int nside = 2;
361 int ipnext = (int) pixTools.next_in_line_nest(nside, ipix);
362 assertEquals("ipnext=" + ipnext, 23, ipnext, 1e-10);
363 ipix = 1;
364 nside = 2;
365 ipnext = (int) pixTools.next_in_line_nest(nside, ipix);
366 assertEquals("ipnext=" + ipnext, 6, ipnext, 1e-10);
367 ipix = 4;
368 nside = 2;
369 ipnext = (int) pixTools.next_in_line_nest(nside, ipix);
370 assertEquals("ipnext=" + ipnext, 27, ipnext, 1e-10);
371 ipix = 27;
372 nside = 2;
373 ipnext = (int) pixTools.next_in_line_nest(nside, ipix);
374 assertEquals("ipnext=" + ipnext, 8, ipnext, 1e-10);
375 ipix = 12;
376 nside = 2;
377 ipnext = (int) pixTools.next_in_line_nest(nside, ipix);
378 assertEquals("ipnext=" + ipnext, 19, ipnext, 1e-10);
379 ipix = 118;
380 nside = 4;
381 ipnext = (int) pixTools.next_in_line_nest(nside, ipix);
382 assertEquals("ipnext = " + ipnext, 117, ipnext, 1e-10);
383 System.out.println(" test next_in_line_nest is done");
384 }
385
386 /**
387 * tests InRing method
388 * @throws Exception
389 */
390 public void testInRing() throws Exception {
391 System.out.println(" Start test InRing !!!!!!!!!!!!!!!!!!!!!!!!!");
392 int[] nestComp = { 19, 0, 23, 4, 27, 8, 31, 12 };
393 long[] ringHi = {17, 18, 19, 12, 13 };
394 long[] nestHi = { 8, 31, 12, 19, 0 };
395 long[] ringLow = {19, 12, 13, 14, 15 };
396 long[] nestLow = { 12, 19, 0, 23, 4 };
397 double PI = Math.PI;
398 int nside = 2;
399 boolean nest = false;
400 int iz = 3;
401 double phi = PI;
402 double dphi = PI;
403 ArrayList ring = pixTools.InRing(nside, iz, phi, dphi, nest);
404 for (int i = 0; i < ring.size(); i++) {
405 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
406 i + 12, ((Long) ring.get(i)).longValue(), 1e-10);
407 }
408 Vector3d v = new Vector3d(1., 0., 0.);
409 double[] ang_tup = { 0., 0. };
410 ang_tup = pixTools.Vect2Ang(v);
411 phi = ang_tup[1]/PI;
412 ring = pixTools.InRing(nside, iz, phi, dphi, nest);
413 for (int i = 0; i < ring.size(); i++) {
414 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
415 i + 12, ((Long) ring.get(i)).longValue(), 1e-10);
416 }
417 Vector3d v1 = new Vector3d(-1., 0., 0.);
418
419 ang_tup = pixTools.Vect2Ang(v1);
420 phi = ang_tup[1]/PI;
421 ring = pixTools.InRing(nside, iz, phi, dphi, nest);
422 for (int i = 0; i < ring.size(); i++) {
423 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
424 i + 12, ((Long) ring.get(i)).longValue(), 1e-10);
425 }
426 phi = 1.75*PI;
427 dphi = 0.5*PI;
428 ring = pixTools.InRing(nside, iz, phi, dphi, nest);
429 for (int i = 0; i < ring.size(); i++) {
430 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
431 ringHi[i], ((Long) ring.get(i)).longValue(), 1e-10);
432
433 }
434
435 phi = 1.75*PI;
436 dphi = 0.5*PI;
437 nest = true;
438 ring = pixTools.InRing(nside, iz, phi, dphi, nest);
439 for (int i = 0; i < ring.size(); i++) {
440 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
441 nestHi[i], ((Long) ring.get(i)).longValue(), 1e-10);
442
443 }
444 phi = 0.25*PI;
445 dphi = 0.5*PI;
446 nest = false;
447 ring = pixTools.InRing(nside, iz, phi, dphi, nest);
448 for (int i = 0; i < ring.size(); i++) {
449 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
450 ringLow[i], ((Long) ring.get(i)).longValue(), 1e-10);
451
452 }
453 phi = 0.25*PI;
454 dphi = 0.5*PI;
455 nest = true;
456 ring = pixTools.InRing(nside, iz, phi, dphi, nest);
457 for (int i = 0; i < ring.size(); i++) {
458 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
459 nestLow[i], ((Long) ring.get(i)).longValue(), 1e-10);
460
461 }
462
463 nest = true;
464 dphi = PI;
465 ring = pixTools.InRing(nside, iz, phi, dphi, nest);
466 for (int i = 0; i < ring.size(); i++) {
467 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
468 nestComp[i], ((Long) ring.get(i)).longValue(), 1e-10);
469 }
470 nest = false;
471 nside = 4;
472 phi = 2.1598449493429825;
473 iz = 8;
474 dphi = 0.5890486225480867;
475 // System.out.println(" iz="+iz+" phi="+phi+" dphi="+dphi);
476 ring = pixTools.InRing(nside, iz, phi, dphi, nest);
477 // for (int i = 0; i<ring.size(); i++) {
478 // System.out.println("ipnext = "+ ((Integer)ring.get(i)).intValue());
479 // }
480 nest = false;
481 nside = 4;
482 dphi = 0. * PI;
483 iz = 8;
484 phi = 2.1598449493429825;
485 // System.out.println(" iz="+iz+" phi="+phi+" dphi="+dphi);
486 ring = pixTools.InRing(nside, iz, phi, dphi, nest);
487 // for (int i = 0; i<ring.size(); i++) {
488 // System.out.println("ipnext = "+ ((Integer)ring.get(i)).intValue());
489 // }
490 System.out.println(" test InRing is done");
491 }
492
493 /**
494 * tests Neighbour's method for nest schema of the pixelization
495 * @throws Exception
496 */
497 public void testNeighbours_Nest() throws Exception {
498 System.out.println(" Start test Neighbours_Nest !!!!!!!!!!!!!!!!!");
499 long nside = 2;
500 long ipix = 25;
501 long[] test17 = { 34, 16, 18, 19, 2, 0, 22, 35 };
502 long[] test32 = { 40, 44, 45, 34, 35, 33, 38, 36 };
503 long[] test3 = { 0, 2, 13, 15, 11, 7, 6, 1 };
504 long[] test25 = { 42, 24, 26, 27, 10, 8, 30, 43 };
505 //
506 ArrayList npixList = new ArrayList();
507 npixList = pixTools.neighbours_nest(nside, ipix);
508 for (int i = 0; i < npixList.size(); i++) {
509 assertEquals("ip = " + ((Long) npixList.get( i)).longValue(),
510 test25[ i], ((Long) npixList.get( i)).longValue(), 1e-10);
511 }
512 ipix = 17;
513
514 npixList = pixTools.neighbours_nest(nside, ipix);
515 for (int i = 0; i < npixList.size(); i++) {
516 assertEquals("ip = " + ((Long) npixList.get(i)).longValue(),
517 test17[i], ((Long) npixList.get(i)).longValue(), 1e-10);
518 }
519 ipix = 32;
520
521 npixList = pixTools.neighbours_nest(nside, ipix);
522 for (int i = 0; i < npixList.size(); i++) {
523 assertEquals("ip = " + ((Long) npixList.get(i)).longValue(),
524 test32[i], ((Long) npixList.get(i)).longValue(), 1e-10);
525 }
526 ipix = 3;
527
528 npixList = pixTools.neighbours_nest(nside, ipix);
529 for (int i = 0; i < npixList.size(); i++) {
530 assertEquals("ip = " + ((Long) npixList.get(i)).longValue(),
531 test3[i], ((Long) npixList.get(i)).longValue(), 1e-10);
532 }
533 System.out.println(" test NeighboursNest is done");
534 }
535
536 /**
537 * tests intrs_intrv method
538 * @throws Exception
539 */
540 public void testIntrs_Intrv() throws Exception {
541 System.out.println(" test intrs_intrv !!!!!!!!!!!!!!!!!!!!!!!!!!!");
542 double[] d1 = { 1.0, 9.0 };
543 double[] d2 = { 3.0, 16.0 };
544 double[] di;
545 // System.out.println("Case "+d1[0]+" "+d1[1]+" | "+d2[0]+" "+d2[1]);
546 di = pixTools.intrs_intrv(d1, d2);
547 // System.out.println("Result "+di[0]+" - "+di[1]);
548 int n12 = di.length / 2;
549 assertEquals("n12 = " + n12, 1, n12, 1e-6);
550 assertEquals("di[0] = " + di[0], 3.0, di[0], 1e-6);
551 assertEquals("di[1] = " + di[1], 9.0, di[1], 1e-6);
552 d1 = new double[] { 0.537, 4.356 };
553 d2 = new double[] { 3.356, 0.8 };
554 // System.out.println("Case "+d1[0]+" "+d1[1]+" | "+d2[0]+" "+d2[1]);
555 di = pixTools.intrs_intrv(d1, d2);
556 n12 = di.length / 2;
557 assertEquals("n12 = " + n12, 2, n12, 1e-6);
558 assertEquals("di[0] = " + di[0], 0.537, di[0], 1e-6);
559 assertEquals("di[1] = " + di[1], 0.8, di[1], 1e-6);
560 assertEquals("di[2] = " + di[2], 3.356, di[2], 1e-6);
561 assertEquals("di[1] = " + di[3], 4.356, di[3], 1e-6);
562
563 d1 = new double[] { 2.356194490092345, 2.356194490292345 };
564 d2 = new double[] { 1.251567, 4.17 };
565 // System.out.println("Case "+d1[0]+" "+d1[1]+" | "+d2[0]+" "+d2[1]);
566 di = pixTools.intrs_intrv(d1, d2);
567 n12 = di.length / 2;
568 assertEquals("n12 = " + n12, 1, n12, 1e-6);
569 assertEquals("di[0] = " + di[0], 2.35619449009, di[0], 1e-6);
570 assertEquals("di[1] = " + di[1], 2.35619449029, di[1], 1e-6);
571
572 System.out.println(" test intrs_intrv is done");
573 }
574
575 /**
576 * tests conversion from pixel number to vector
577 * @throws Exception
578 */
579 public void testPix2Vect_ring() throws Exception {
580 System.out.println("Start test Pix2Vect_ring !!!!!!!!!!!!!!!!!!!");
581 double TWOPI = 2.0 * Math.PI;
582 int nside = 2;
583 int ipix = 0;
584 Vector3d v1 = new Vector3d(0., 0., 0.);
585 v1 = pixTools.pix2vect_ring(nside, ipix);
586 assertEquals("v1.z = " + v1.z, 1.0, v1.z, 1e-1);
587
588 ipix = 20;
589 Vector3d v2 = new Vector3d(0., 0., 0.);
590 v2 = pixTools.pix2vect_ring(nside, ipix);
591 assertEquals("v2.x = " + v2.x, 1.0, v2.x, 1e-1);
592 assertEquals("v2.z = " + v2.z, 0.0, v2.z, 1e-1);
593 ipix = 22;
594 Vector3d v3 = new Vector3d();
595 v3 = pixTools.pix2vect_ring(nside, ipix);
596 assertEquals("v3.y = " + v3.y, 1.0, v3.y, 1e-1);
597 assertEquals("v3.z = " + v3.z, 0.0, v3.z, 1e-1);
598 // System.out.println("Vector3 x="+v3.x+" y="+v3.y+" z="+v3.z);
599 ipix = 95;
600 nside = 4;
601 v1 = pixTools.pix2vect_ring(nside, ipix);
602 v1.normalize();
603 double phi1 = Math.atan2(v1.y, v1.x);
604 double[] tetphi = new double[2];
605 tetphi = pixTools.pix2ang_ring(nside, ipix);
606 assertEquals("phi = " + phi1, 0.0, Math.abs(phi1 - tetphi[1]), 1e-10);
607 ipix = 26;
608 nside = 4;
609 v1 = pixTools.pix2vect_ring(nside, ipix);
610 v1.normalize();
611 phi1 = Math.atan2(v1.y, v1.x);
612 if (phi1 < 0.)
613 phi1 += TWOPI;
614 tetphi = new double[2];
615 tetphi = pixTools.pix2ang_ring(nside, ipix);
616 assertEquals("phi = " + phi1, 0.0, Math.abs(phi1 - tetphi[1]), 1e-10);
617 System.out.println("------------------------------------------");
618 System.out.println(" test pix2vect_ring is done");
619 }
620
621 /**
622 * tests conversion from pixel number to vector
623 * @throws Exception
624 */
625 public void testPix2Vect_nest() throws Exception {
626 double TWOPI = 2.0 * Math.PI;
627 int nside = 2;
628 int ipix = 3;
629 System.out.println(" Start test Pix2Vect_nest !!!!!!!!!!!!!!");
630 Vector3d v1 = new Vector3d(0., 0., 0.);
631 v1 = pixTools.pix2vect_nest(nside, ipix);
632 assertEquals("v1.z = " + v1.z, 1.0, v1.z, 1e-1);
633 ipix = 17;
634 Vector3d v2 = new Vector3d(0., 0., 0.);
635 v2 = pixTools.pix2vect_nest(nside, ipix);
636 assertEquals("v2.x = " + v2.x, 1.0, v2.x, 1e-1);
637 assertEquals("v2.z = " + v2.z, 0.0, v2.z, 1e-1);
638
639 ipix = 21;
640 Vector3d v3 = new Vector3d();
641 v3 = pixTools.pix2vect_nest(nside, ipix);
642 assertEquals("v3.y = " + v3.y, 1.0, v3.y, 1e-1);
643 assertEquals("v3.z = " + v3.z, 0.0, v3.z, 1e-1);
644 nside = 4;
645 ipix = 105;
646 v1 = pixTools.pix2vect_nest(nside, ipix);
647 v1.normalize();
648 double phi1 = Math.atan2(v1.y, v1.x);
649 if (phi1 < 0.)
650 phi1 += TWOPI;
651 double[] tetphi = new double[2];
652 tetphi = pixTools.pix2ang_nest(nside, ipix);
653 assertEquals("phi = " + phi1, 0.0, Math.abs(phi1 - tetphi[1]), 1e-10);
654 nside = 4;
655 ipix = 74;
656 v1 = pixTools.pix2vect_nest(nside, ipix);
657 v1.normalize();
658 phi1 = Math.atan2(v1.y, v1.x);
659 if (phi1 < 0.)
660 phi1 += TWOPI;
661 tetphi = new double[2];
662 tetphi = pixTools.pix2ang_nest(nside, ipix);
663 assertEquals("phi = " + phi1, 0.0, Math.abs(phi1 - tetphi[1]), 1e-10);
664
665 System.out.println(" test pix2vect_nest is done");
666 System.out.println("-------------------------------------------");
667 }
668
669 /**
670 * tests conversion from vector to pixel number
671 * @throws Exception
672 */
673 public void testVect2Pix_ring() throws Exception {
674 System.out.println("Start test Vect2Pix_ring !!!!!!!!!!!!!!!!!!!");
675
676 long nside = 4;
677 long ipix = 83;
678 long respix = 0;
679 Vector3d v1 = new Vector3d(0., 0., 0.);
680 v1 = pixTools.pix2vect_ring(nside, ipix);
681 respix = (int) pixTools.vect2pix_ring(nside, v1);
682 assertEquals("respix = " + respix, 83, respix, 1e-10);
683 // Hi resolution test
684 long nside1 = 1 << 20;
685 long maxpix= pixTools.Nside2Npix(nside1);
686 System.out.println("nside="+nside1+" maxpix="+maxpix);
687 Vector3d v2 = new Vector3d( -0.704, 0.580, 0.408);
688 respix = pixTools.vect2pix_ring(nside1, v2); // ring pixel
689 long respixN = pixTools.ring2nest(nside1, respix); // convert to nest
690 long respixNC = pixTools.vect2pix_nest(nside1,v2); // nest pixel from the same vector
691 long respixR = pixTools.nest2ring(nside1, respixN); // convert pixel
692 System.out.println(" orig="+respix+" doubleT="+respixR+" nest="+respixN+" correct nest="+respixNC);
693 assertEquals("ringpix = " + respix, respix, respixR);
694 assertEquals("nestpix = " + respixNC, respixNC, respixN);
695 System.out.println("------------------------------------------");
696 System.out.println(" test vect2pix_ring is done");
697 }
698
699 /**
700 * tests conversion from vector to pixel number
701 * @throws Exception
702 */
703 public void testVect2Pix_nest() throws Exception {
704 System.out.println("Start test Vect2Pix_nest !!!!!!!!!!!!!!!!!!!");
705 long nside = 4;
706 long ipix = 83;
707 long respix = 0;
708 Vector3d v1 = new Vector3d(0., 0., 0.);
709 v1 = pixTools.pix2vect_ring(nside, ipix);
710 respix = (int) pixTools.vect2pix_nest(nside, v1);
711 assertEquals("respix = " + respix, 123, respix, 1e-10);
712 //
713 long nside1 = 1 << 20;
714 long maxpix= pixTools.Nside2Npix(nside1);
715 System.out.println("nside="+nside1+" maxpix="+maxpix);
716 Vector3d v2 = new Vector3d( -0.704, 0.580, 0.408);
717 respix = pixTools.vect2pix_nest(nside1, v2);
718 long respixRC = pixTools.vect2pix_ring(nside1, v2);
719 long respixR = pixTools.nest2ring(nside1, respix);
720 long respixN = pixTools.ring2nest(nside1, respixRC);
721 System.out.println(" orig="+respix+" doubleT="+respixN+" ring="+respixR+" correct ring="+respixRC);
722 assertEquals("ringpix = " + respixRC, respixRC, respixR);
723 assertEquals("nestpix = " + respix, respix, respixN);
724 System.out.println("------------------------------------------");
725 System.out.println(" test vect2pix_nest is done");
726 }
727
728 /**
729 * tests Query_Strip method
730 * @throws Exception
731 */
732 public void testQuery_Strip() throws Exception {
733 System.out.println(" Start test query Strip !!!!!!!!!!!!!!!!");
734 int[] pixel1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
735 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
736 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
737 48, 49, 50, 51, 52, 53, 54, 55 };
738 int nside = 4;
739 int nest = 0;
740 double theta1 = 0.0;
741 double theta2 = Math.PI / 4.0 + 0.2;
742 ArrayList pixlist;
743 pixlist = pixTools.query_strip(nside, theta1, theta2, nest);
744 int nlist = pixlist.size();
745 for (int i = 0; i < nlist; i++) {
746 long ipix = ((Long) pixlist.get(i)).longValue();
747 assertEquals("pixel = " + ipix, pixel1[i], ipix, 1e-10);
748 }
749
750 System.out.println(" test query_strip is done");
751
752 }
753
754 /**
755 * tests Query_Disc method
756 * @throws Exception
757 */
758 public void testQuery_Disc() throws Exception {
759 System.out.println(" Start test query_disc !!!!!!!!!!!!!!!!!!!!!");
760 long nside = 4;
761 int nest = 0;
762 long ipix = 0;
763
764
765 int[] pixel1 = { 45, 46, 60, 61, 62, 77, 78, 92, 93, 94,
766 109, 110, 124, 125, 126, 141, 142
767 };
768
769
770
771 int[] pixel2 = { 24, 19, 93, 18, 17, 87, 16,86, 85,
772 106, 84, 159, 81, 158, 157, 155, 156
773 };
774
775
776 int[] pixel3 = {52, 79, 49, 78, 77, 75, 76, 74, 73, 70,
777 72, 67, 189, 66, 65, 183, 64
778 };
779 int inclusive = 1;
780 double radius = Math.PI / 8.0;
781 Vector3d v = pixTools.pix2vect_ring(nside, 93);
782 ArrayList pixlist;
783 pixlist = pixTools.query_disc(nside, v, radius, nest, inclusive);
784
785 int nlist = pixlist.size();
786
787 for (int i = 0; i < nlist; i++) {
788 ipix = ((Long) pixlist.get(i)).longValue();
789 // System.out.println("i="+i+"pixel="+ipix+" pixel1="+pixel1[i]);
790 assertEquals("pixel = " + ipix, pixel1[i], ipix, 1e-10);
791 }
792 radius = Math.PI/2.0;
793 nest = 0;
794 v = new Vector3d(1.0,0.0,0.0);
795 ArrayList pixlistT = pixTools.query_disc(2, v, radius, nest, inclusive);
796
797 radius = Math.PI / 8.0;
798 v = pixTools.pix2vect_ring(nside, 93);
799 nest = 1;
800 ArrayList pixlist2 = pixTools.query_disc(nside, v, radius, nest, inclusive);
801
802 int nlist2 = pixlist2.size();
803 assertEquals("npix="+nlist, nlist, nlist2, 1e-10);
804 for (int i = 0; i < nlist2; i++) {
805 ipix = ((Long) pixlist2.get(i)).longValue();
806
807 assertEquals("pixel = " + ipix, pixel2[i], ipix, 1e-10);
808
809 }
810
811 v = pixTools.pix2vect_ring(nside, 103);
812 nest = 1;
813 inclusive = 1;
814 ArrayList pixlist3 = pixTools.query_disc(nside, v, radius, nest, inclusive);
815 nlist = pixlist3.size();
816 assertEquals("npix="+nlist, nlist, pixel3.length, 1e-10);
817
818 for (int i = 0; i < pixlist3.size(); i++) {
819 ipix = ((Long) pixlist3.get(i)).longValue();
820
821 assertEquals("pixel = " + ipix, pixel3[i], ipix, 1e-10);
822 }
823
824
825 for (int i=0; i<pixel1.length; i++) {
826 long ipixR = pixel1[i];
827 long ipixT = pixTools.ring2nest(nside, ipixR);
828 assertEquals("pixel="+ipixT, ipixT, pixel2[i], 1e-10);
829 long ipixN = pixTools.nest2ring(nside, ipixT);
830 assertEquals("pixel="+ipixN, ipixN, pixel1[i], 1e-10);
831 }
832 System.out.println(" End query_disk test ______________________________");
833 }
834
835 /**
836 * tests Query_Triangle method
837 * @throws Exception
838 */
839 public void testQuery_Triangle() throws Exception {
840 long nside = 4;
841 int nest = 0;
842 long ipix = 0;
843 int[] pixel1 = { 57, 58, 59, 60, 61, 62, 74, 75, 76, 77, 78, 90, 91,
844 92, 93, 107, 108, 109, 123, 124, 140 };
845 int[] pixel2 = { 88, 89, 90, 91, 105, 106, 107, 108, 121, 122, 123,
846 138, 139, 154 };
847 int[] pixel3 = { 49, 64, 80, 81, 95, 96, 112, 113,
848 127, 128, 142, 143, 144, 145 };
849 int[] pixel4 = { 36, 52, 53, 67, 68, 69, 83, 84, 85, 86, 98, 99, 100,
850 101, 102, 114, 115, 116, 117, 118, 119, 129, 130, 131, 132,
851 133, 134, 135 };
852 int[] pixel5 = { 58, 127, 56, 126, 125, 50, 123, 124, 119, 48, 122,
853 121, 118, 117, 74, 175, 120, 115, 116, 191, 72, 174, 173, 114,
854 113, 190, 189, 66 };
855 int[] pixel6 = { 110, 123, 124, 125, 140, 141, 156 };
856 int[] pixel7 = { 53, 68, 69 };
857 long pix1 = 62;
858 long pix2 = 57;
859 long pix3 = 140;
860 System.out.println("Start test Query Triangle !!!!!!!!!!!!!!!!!!!!");
861 Vector3d v11 = pixTools.pix2vect_ring(nside, pix1);
862
863 Vector3d v22 = pixTools.pix2vect_ring(nside, pix2);
864
865 Vector3d v33 = pixTools.pix2vect_ring(nside, pix3);
866
867 // System.out.println("nside="+nside+" triangle pixels "+pix1+" "+pix2+"
868 // "+pix3);
869 int inclusive = 0;
870
871 ArrayList pixlist;
872 pixlist = pixTools.query_triangle(nside, v11, v22, v33, nest, inclusive);
873
874 int nlist = pixlist.size();
875
876 for (int i = 0; i < nlist; i++) {
877 ipix = ((Long) pixlist.get(i)).longValue();
878
879 assertEquals("pixel = " + ipix, pixel1[i], ipix, 1e-10);
880 }
881 pix1 = 92;
882 pix2 = 88;
883 pix3 = 154;
884 v11 = pixTools.pix2vect_ring(nside, pix1);
885 v22 = pixTools.pix2vect_ring(nside, pix2);
886 v33 = pixTools.pix2vect_ring(nside, pix3);
887
888 inclusive = 0;
889 ArrayList pixlist1;
890 pixlist1 = pixTools.query_triangle(nside, v11, v22, v33, nest, inclusive);
891
892 nlist = pixlist1.size();
893
894 for (int i = 0; i < nlist; i++) {
895 ipix = ((Long) pixlist1.get(i)).longValue();
896 // System.out.println(ipix);
897 assertEquals("pixel = " + ipix, pixel2[i], ipix, 1e-10);
898 }
899 pix1 = 49;
900 pix2 = 142;
901 pix3 = 145;
902 v11 = pixTools.pix2vect_ring(nside, pix1);
903 v22 = pixTools.pix2vect_ring(nside, pix2);
904 v33 = pixTools.pix2vect_ring(nside, pix3);
905
906 inclusive = 0;
907 ArrayList pixlist2;
908 pixlist2 = pixTools.query_triangle(nside, v11, v22, v33, nest, inclusive);
909
910 nlist = pixlist2.size();
911
912 for (int i = 0; i < nlist; i++) {
913 ipix = ((Long) pixlist2.get(i)).longValue();
914 // System.out.println(ipix);
915 assertEquals("pixel = " + ipix, pixel3[i], ipix, 1e-10);
916 }
917 pix1 = 36;
918 pix2 = 129;
919 pix3 = 135;
920 v11 = pixTools.pix2vect_ring(nside, pix1);
921 v22 = pixTools.pix2vect_ring(nside, pix2);
922 v33 = pixTools.pix2vect_ring(nside, pix3);
923
924 inclusive = 0;
925
926 pixlist2 = pixTools.query_triangle(nside, v11, v22, v33, nest, inclusive);
927
928 nlist = pixlist2.size();
929
930 for (int i = 0; i < nlist; i++) {
931 ipix = ((Long) pixlist2.get(i)).longValue();
932 assertEquals("pixel = " + ipix, pixel4[i], ipix, 1e-10);
933 }
934 pix1 = 36;
935 pix2 = 129;
936 pix3 = 135;
937 nest = 1;
938 v11 = pixTools.pix2vect_ring(nside, pix1);
939 v22 = pixTools.pix2vect_ring(nside, pix2);
940 v33 = pixTools.pix2vect_ring(nside, pix3);
941 inclusive = 0;
942
943 pixlist2 = pixTools.query_triangle(nside, v11, v22, v33, nest, inclusive);
944
945 nlist = pixlist2.size();
946
947 for (int i = 0; i < nlist; i++) {
948 ipix = ((Long) pixlist2.get(i)).longValue();
949 // System.out.println("ipix="+ipix);
950 assertEquals("pixel = " + ipix, pixel5[i], ipix, 1e-10);
951 }
952 pix1 = 123;
953 pix2 = 156;
954 pix3 = 110;
955 nest = 0;
956 v11 = pixTools.pix2vect_ring(nside, pix1);
957 v22 = pixTools.pix2vect_ring(nside, pix2);
958 v33 = pixTools.pix2vect_ring(nside, pix3);
959 inclusive = 0;
960
961 pixlist2 = pixTools.query_triangle(nside, v11, v22, v33, nest, inclusive);
962
963 nlist = pixlist2.size();
964
965 for (int i = 0; i < nlist; i++) {
966 ipix = ((Long) pixlist2.get(i)).longValue();
967 assertEquals("pixel = " + ipix, pixel6[i], ipix, 1e-10);
968 // System.out.println("i="+i+" pixel#="+ipix);
969 }
970 pix1 = 69;
971 pix2 = 53;
972 pix3 = 68;
973 nest = 0;
974 v11 = pixTools.pix2vect_ring(nside, pix1);
975 v22 = pixTools.pix2vect_ring(nside, pix2);
976 v33 = pixTools.pix2vect_ring(nside, pix3);
977 inclusive = 0;
978
979 pixlist2 = pixTools.query_triangle(nside, v11, v22, v33, nest, inclusive);
980
981 nlist = pixlist2.size();
982
983 for (int i = 0; i < nlist; i++) {
984 ipix = ((Long) pixlist2.get(i)).longValue();
985 assertEquals("pixel = " + ipix, pixel7[i], ipix, 1e-10);
986 // System.out.println("i="+i+" pixel#="+ipix);
987 }
988 System.out.println(" test query_triangle is done");
989
990 }
991
992 /**
993 * tests Query_Poligon method
994 * @throws Exception
995 */
996 public void testQuery_Polygon() throws Exception {
997 long nside = 4;
998 int nest = 0;
999 long ipix = 0;
1000 int inclusive = 0;
1001 int[] result = { 51, 52, 53, 66, 67, 68, 69, 82, 83, 84, 85, 86, 98,
1002 99, 100, 101, 115, 116, 117 };
1003 int[] result1 = { 55, 70, 71, 87 };
1004 int[] result2 = { 137, 152, 153, 168 };
1005 int[] result3 = { 27, 43, 44, 58, 59, 60, 74, 75, 76, 77, 89, 90, 91,
1006 92, 93, 105, 106, 107, 108, 109, 110, 121, 122, 123, 124, 125,
1007 138, 139, 140, 141, 154, 156 };
1008
1009
1010 System.out.println("Start test query_polygon !!!!!!!!!!!!!!!!!!!!!!");
1011 ArrayList vlist = new ArrayList();
1012 Vector3d v = pixTools.pix2vect_ring(nside, 53);
1013 vlist.add( v);
1014 v = pixTools.pix2vect_ring(nside, 51);
1015 vlist.add( v);
1016 v = pixTools.pix2vect_ring(nside, 82);
1017 vlist.add( v);
1018 v = pixTools.pix2vect_ring(nside, 115);
1019 vlist.add( v);
1020 v = pixTools.pix2vect_ring(nside, 117);
1021 vlist.add( v);
1022 v = pixTools.pix2vect_ring(nside, 86);
1023 vlist.add( v);
1024
1025 ArrayList pixlist;
1026 pixlist = pixTools.query_polygon(nside, vlist, nest, inclusive);
1027 // System.out.println(" List size="+pixlist.size());
1028 int nlist = pixlist.size();
1029 // System.out.println(" Pixel list:");
1030 for (int i = 0; i < nlist; i++) {
1031 ipix = ((Long) pixlist.get(i)).longValue();
1032 assertEquals("pixel = " + ipix, result[i], ipix, 1e-10);
1033 // System.out.println("i="+i+" pixel # "+ipix);
1034 }
1035
1036 /* Yet another test */
1037
1038 ArrayList vlist1 = new ArrayList();
1039 v = pixTools.pix2vect_ring(nside, 71);
1040 vlist1.add( v);
1041 v = pixTools.pix2vect_ring(nside, 55);
1042 vlist1.add( v);
1043 v = pixTools.pix2vect_ring(nside, 70);
1044 vlist1.add( v);
1045 v = pixTools.pix2vect_ring(nside, 87);
1046 vlist1.add( v);
1047 pixlist = pixTools.query_polygon(nside, vlist1, nest, inclusive);
1048
1049 nlist = pixlist.size();
1050
1051 for (int i = 0; i < nlist; i++) {
1052 ipix = ((Long) pixlist.get(i)).longValue();
1053 // System.out.println("i="+i+" pixel # "+ipix);
1054 assertEquals("pixel = " + ipix, result1[i], ipix, 1e-10);
1055 }
1056
1057 /* Yet another test */
1058 ArrayList vlist2 = new ArrayList();
1059 v = pixTools.pix2vect_ring(nside, 153);
1060 vlist2.add( v);
1061 v = pixTools.pix2vect_ring(nside, 137);
1062 vlist2.add( v);
1063 v = pixTools.pix2vect_ring(nside, 152);
1064 vlist2.add( v);
1065 v = pixTools.pix2vect_ring(nside, 168);
1066 vlist2.add( v);
1067 pixlist = pixTools.query_polygon(nside, vlist2, nest, inclusive);
1068
1069 nlist = pixlist.size();
1070
1071 for (int i = 0; i < nlist; i++) {
1072 ipix = ((Long) pixlist.get(i)).longValue();
1073 assertEquals("pixel = " + ipix, result2[i], ipix, 1e-10);
1074 // System.out.println("i="+i+" pixel # "+ipix);
1075 }
1076 /* Yet another test */
1077
1078 ArrayList vlist3 = new ArrayList();
1079 v = pixTools.pix2vect_ring(nside, 110);
1080 vlist3.add( v);
1081 v = pixTools.pix2vect_ring(nside, 27);
1082 vlist3.add( v);
1083 v = pixTools.pix2vect_ring(nside, 105);
1084 vlist3.add( v);
1085 v = pixTools.pix2vect_ring(nside, 154);
1086 vlist3.add( v);
1087 v = pixTools.pix2vect_ring(nside, 123);
1088 vlist3.add( v);
1089 v = pixTools.pix2vect_ring(nside, 156);
1090 vlist3.add( v);
1091 pixlist = pixTools.query_polygon(nside, vlist3, nest, inclusive);
1092
1093 nlist = pixlist.size();
1094
1095 for (int i = 0; i < nlist; i++) {
1096 ipix = ((Long) pixlist.get(i)).longValue();
1097 assertEquals("pixel = " + ipix, result3[i], ipix, 1e-10);
1098 // System.out.println("i="+i+" pixel # "+ipix);
1099 }
1100 System.out.println(" test query_polygon is done");
1101
1102 }
1103 /**
1104 * tests MaxResolution method
1105 */
1106 public void testMaxResolution() {
1107 System.out.println(" Start test MaxRes !!!!!!!!!!!!!!!!!!!!!");
1108
1109 long nside = 1048576;
1110 double res = pixTools.PixRes(nside);
1111 System.out.println("Minimum size of the pixel side is "+res+" arcsec.");
1112 assertEquals("res = " + res, 0.2, res, 1e-1);
1113 long nsideR = pixTools.GetNSide(res);
1114 assertEquals("nside = " + nside, nside, nsideR, 1e-1);
1115 System.out.println(" End of MaxRes test _______________________");
1116 }
1117 /**
1118 * tests QueryDiscResolution method
1119 * @throws Exception
1120 */
1121 public void testQueryDiscRes() throws Exception {
1122 System.out.println(" Start test DiscRes !!!!!!!!!!!!!!!!!!!!!");
1123
1124 int nest = 0;
1125 long ipix = 0;
1126
1127 int inclusive = 0;
1128 double theta= Math.PI;
1129 double phi = Math.PI;
1130 double radius = Math.toRadians(0.2/3600.); // One arcse
1131 long nside = pixTools.GetNSide(radius);
1132 System.out.println(" calculated nside="+nside);
1133 long cpix = pixTools.ang2pix_ring(nside,theta,phi);
1134 Vector3d vc = pixTools.pix2vect_ring(nside, cpix);
1135 ArrayList pixlist;
1136 pixlist = pixTools.query_disc(nside, vc, radius, nest, inclusive);
1137
1138 int nlist = pixlist.size();
1139 for (int i = 0; i < nlist; i++) {
1140 ipix = ((Long) pixlist.get(i)).longValue();
1141 Vector3d v = pixTools.pix2vect_ring(nside,ipix);
1142 double dist = pixTools.AngDist(v,vc);
1143 assertTrue(dist<=2.*radius);
1144 }
1145 cpix = pixTools.ang2pix_nest(nside,theta,phi);
1146 Vector3d vc1 = pixTools.pix2vect_nest(nside, cpix);
1147 ArrayList pixlist1;
1148 nest = 1;
1149 radius *=4;
1150 pixlist1 = pixTools.query_disc(nside, vc1, radius, nest, inclusive);
1151 int nlist1 = pixlist1.size();
1152 for (int i = 0; i < nlist1; i++) {
1153 ipix = ((Long) pixlist1.get(i)).longValue();
1154 Vector3d v = pixTools.pix2vect_nest(nside,ipix);
1155 double dist = pixTools.AngDist(v,vc1);
1156 assertTrue(dist<=2.*radius);
1157 }
1158 System.out.println(" test query disk is done -------------------");
1159 }
1160 /**
1161 * test Query_disk check for consistency in the query for RING/NESTED
1162 */
1163 public void testQuery_disk2() {
1164 System.out.println(" Start test query_disk HiRes!!!!!!!!!!!!!!!!!!!!!!!!");
1165 long nside = 1 << 20 ;
1166 double res = pixTools.PixRes(nside);
1167 System.out.println("nside="+nside+" sresolution="+res);
1168 double radius = Math.toRadians(res/3600.)/2.;
1169 System.out.println("radius="+radius);
1170 Vector3d v1 = new Vector3d(-0.704, 0.580, 0.408);
1171 System.out.println("!!!!!!!!!!!!! NESTED !!!!!!!!!!!");
1172 ArrayList diskQ = pixTools.query_disc(nside,
1173 v1,
1174 radius, 1,1); // inclusive query at vector point
1175 assertEquals("npixels = " + diskQ.size(), 8, diskQ.size() , 1e-1);
1176 long pix1 = pixTools.vect2pix_nest(nside, v1);
1177 Vector3d v2 = pixTools.pix2vect_nest(nside, pix1); // vector to pix center
1178 //
1179 ArrayList diskQ2 = pixTools.query_disc(nside,
1180 v2,
1181 radius, 1,1); // inclusive with point at pixel center
1182 assertEquals("npixels = " + diskQ2.size(), 9, diskQ2.size() , 1e-1);
1183
1184 //
1185 ArrayList diskQ3 = pixTools.query_disc(nside,
1186 v2,
1187 radius, 1,0); // exclusive with point at pixel center
1188 assertEquals("npixels = " + diskQ3.size(), 1, diskQ3.size() , 1e-1);
1189
1190 // RING schema
1191 System.out.println("!!!!!!!!!!!!! RING !!!!!!!!!!!");
1192 ArrayList diskQ4 = pixTools.query_disc(nside,
1193 v1,
1194 radius, 0,1); // inclusiv at vector point
1195 assertEquals("npixels = " + diskQ4.size(), 8, diskQ4.size() , 1e-1);
1196 //
1197
1198 ArrayList diskQ5 = pixTools.query_disc(nside,
1199 v2,
1200 radius, 0, 1); // inclusive at pixel center
1201 assertEquals("npixels = " + diskQ5.size(), 9, diskQ5.size() , 1e-1);
1202
1203 // System.out.println("n pixels in disk5 ="+diskQ5.size());
1204 ArrayList diskQ6 = pixTools.query_disc(nside,
1205 v2,
1206 radius, 0,0); // exclusive at pixel center
1207 assertEquals("npixels = " + diskQ6.size(), 1, diskQ6.size() , 1e-1);
1208 //
1209 // test HiRes conversions
1210 //
1211 Vector3d pos = new Vector3d( -0.704, 0.580, 0.408 );
1212
1213 nside = 1 << 20;
1214 System.out.println("HiRes transformation tests: nside="+nside);
1215 ArrayList nestPixels = pixTools.query_disc(nside, pos, radius, 1, 1);
1216 ArrayList ringPixels = pixTools.query_disc(nside, pos, radius, 0, 1);
1217 assertEquals(nestPixels.size(), ringPixels.size());
1218 for(int i=0; i< ringPixels.size(); i++) {
1219 long iring = ((Number)ringPixels.get(i)).longValue();
1220 Vector3d cv = pixTools.pix2vect_ring(nside, iring);
1221 long inest = pixTools.ring2nest(nside, iring);
1222 long inestC = ((Number)nestPixels.get(i)).longValue();
1223 Vector3d cvN = pixTools.pix2vect_nest(nside, inestC);
1224 long iringT = pixTools.nest2ring(nside, inestC);
1225 assertEquals(iring,iringT);
1226 assertEquals(inest,inestC);
1227 assertEquals(" Xv="+cv.x,cv.x,cvN.x,1.e-10);
1228 assertEquals(" Yv="+cv.y,cv.y,cvN.y,1.e-10);
1229 assertEquals(" Zv="+cv.z,cv.z,cvN.z,1.e-10);
1230 // System.out.println(" inest orig="+inestC+" transformed="+inest+" iring orig="+iring+" transf="+iringT);
1231 // System.out.println("Vector cv vs cvN x="+cv.x+" cvN.x="+cvN.x);
1232 // System.out.println("Vector cv vs cvN y="+cv.y+" cvN.y="+cvN.y);
1233 // System.out.println("Vector cv vs cvN z="+cv.z+" cvN.z="+cvN.z);
1234 double[] tetphiR = pixTools.pix2ang_ring(nside, iring);
1235 double[] tetphiN= pixTools.pix2ang_nest(nside, inestC);
1236 assertEquals(" theta="+tetphiR[0],tetphiR[0],tetphiN[0],1.e-10);
1237 assertEquals(" phi="+tetphiR[1],tetphiR[1],tetphiN[1],1.e-10);
1238 // System.out.println("theta R vs N "+tetphiR[0]+" "+tetphiN[0]);
1239 // System.out.println("phi R vs N "+tetphiR[1]+" "+tetphiN[1]);
1240 }
1241
1242 System.out.println(" End test of query_disc2____________________________");
1243
1244 }
1245 /**
1246 * tests GetNside method
1247 */
1248 public void testGetNside() {
1249 System.out.println(" Start test GetNside !!!!!!!!!!!!!!!!!!!!!");
1250
1251 double pixsize = 0.3;
1252 long nside = pixTools.GetNSide(pixsize);
1253 System.out.println("Requared nside is "+nside);
1254 assertEquals("nside = " + nside, 1048576, nside, 1e-1);
1255 System.out.println(" End of GetNSide test _______________________");
1256 }
1257
1258 /**
1259 * test conversion of Ra Dec to polar coordinates
1260 */
1261 public void testRaDecToPolar() {
1262 System.out.println(" Start test RaDecToPolar !!!!!!!!!!!!!!!!!!!!!");
1263 double [] radec = new double[2];
1264 radec[0] = 312.115456;
1265 radec[1] = -1.153759;
1266 double[] polar = pixTools.RaDecToPolar(radec);
1267 assertEquals("theta = " + polar[0], 1.5909332201194137, polar[0], 1e-10);
1268 assertEquals("phi = " + polar[1], 5.447442353563491, polar[1], 1e-10);
1269 System.out.println("End test RaDecToPolar__________________________");
1270
1271 }
1272 }
1273
0 /*
1 * Created on Mar 10, 2005
2 * Modified on December 2007
3 *
4 */
5 package gov.fnal.eag.healpix.test;
6
7 import java.io.File;
8 import java.io.FileNotFoundException;
9 import java.io.OutputStream;
10 import java.io.PrintStream;
11 import java.util.ArrayList;
12 import gov.fnal.eag.healpix.BitManipulation;
13 import gov.fnal.eag.healpix.PixTools;
14
15 import javax.vecmath.Vector3d;
16
17 import junit.framework.TestCase;
18
19 /**
20 * test suit for PixTools
21 *
22 * @author N. Kuropatkin
23 *
24 */
25
26 public class PixToolsTest extends TestCase {
27
28 /**
29 * test MODULO function
30 */
31 public void testMODULO() {
32 double A = 8.;
33 double B = 5.;
34 double res = BitManipulation.MODULO(A, B);
35 assertEquals("modulo = " + res, 3., res, 1e-10);
36 A = -8.;
37 B = 5.;
38 res = BitManipulation.MODULO(A, B);
39 assertEquals("modulo = " + res, 2., res, 1e-10);
40 A = 8.;
41 B = -5.;
42 res = BitManipulation.MODULO(A, B);
43 assertEquals("modulo = " + res, -2., res, 1e-10);
44 A = -8.;
45 B = -5.;
46 res = BitManipulation.MODULO(A, B);
47 assertEquals("modulo = " + res, -3., res, 1e-10);
48 System.out.println(" test MODULO is done");
49 }
50
51 /**
52 * tests angular distance calculation
53 * @throws Exception
54 */
55 public void testAngDist() throws Exception {
56 Vector3d v1 = new Vector3d(1.5, 1.6, 2.0);
57 Vector3d v2 = new Vector3d(1.2, 1.0, 1.5);
58 PixTools pt = new PixTools();
59 double res1 = pt.AngDist(v1, v2);
60 double res2 = v1.angle(v2);
61 System.out.println("res1 = " + res1 + " res2=" + res2);
62 assertEquals("angular Distance=" + res2, 1.0, res1 / res2, 1e-10);
63 /* Check known problem with vecmath for small vector differences */
64
65 Vector3d v3 = new Vector3d(1.5, 1.6, 0.);
66 Vector3d v4 = new Vector3d(1.5, 1.601, 0.);
67 double res3 = pt.AngDist(v3, v4);
68 double res4 = v3.angle(v4);
69 //
70 System.out.println("res3 = " + res3 + " res4=" + res4);
71 assertEquals("angular Distance=" + res4, 1., (Math.PI / 2. + res3)
72 / res4, 1e-3);
73 Vector3d v5 = new Vector3d(1.5, 1.6, 0.);
74 Vector3d v6 = new Vector3d(-1.5, -1.75, 0.);
75 double res5 = pt.AngDist(v5, v6);
76 double res6 = v5.angle(v6);
77 System.out.println("res5 = " + res5 + " res6=" + res6);
78 assertEquals("angular Distance=" + res6, 2.0, res5 / res6, 1e-10);
79 System.out.println(" test of AngDist is done");
80 }
81
82 /**
83 * @throws Exception
84 */
85 public void testSurfaceTriangle() throws Exception {
86 Vector3d v1 = new Vector3d(1.0, 0.0, 0.0);
87 Vector3d v2 = new Vector3d(0.0, 1.0, 0.0);
88 Vector3d v3 = new Vector3d(0.0, 0.0, 1.0);
89 PixTools pt = new PixTools();
90 double res = pt.SurfaceTriangle(v1, v2, v3);
91 System.out.println("Triangle surface is=" + res / Math.PI
92 + " steredians");
93 assertEquals("Triangle surface=" + res, 0.5, res / Math.PI, 1e-10);
94 System.out.println(" test of SurfaceTriangle is done");
95 }
96
97 /**
98 * tests calculation of npixels from nside
99 */
100 public void testNside2Npix() {
101 int nside = 1;
102 int npix = 0;
103 PixTools pt = new PixTools();
104 npix = (int) pt.Nside2Npix(nside);
105 assertEquals("Npix=" + npix, 12, npix, 1e-10);
106 nside = 2;
107 npix = (int) pt.Nside2Npix(nside);
108 assertEquals("Npix=" + npix, 48, npix, 1e-10);
109 }
110
111 /**
112 * tests calculation of nsides from npixels
113 */
114 public void testNpix2Nside() {
115 int npix = 12;
116 int nside = 0;
117 PixTools pt = new PixTools();
118 nside = (int) pt.Npix2Nside(npix);
119
120 double pixSize1 = pt.PixRes(65536);
121 long nside1 = pt.GetNSide(pixSize1);
122 assertEquals("Nside=" + nside1, 65536, nside1, 1e-10);
123
124 assertEquals("Nside=" + nside, 1, nside, 1e-10);
125
126 }
127
128 /**
129 * test of directional angles calculation
130 */
131 public void testVec2Ang() {
132 double PI = Math.PI;
133 Vector3d v = new Vector3d(0.0, 1.0, 0.0);
134 PixTools pt = new PixTools();
135 double[] ang_tup = { 0., 0. };
136 ang_tup = pt.Vect2Ang(v);
137 System.out.println(" Theta=" + ang_tup[0] / PI + " Phi=" + ang_tup[1]
138 / PI);
139 assertEquals("Theta=" + ang_tup[0], 0.5, ang_tup[0] / PI, 1e-10);
140 assertEquals("Phi=" + ang_tup[1], 0.5, ang_tup[1] / PI, 1e-10);
141 v = new Vector3d(1.0, 0.0, 0.0);
142 ang_tup = pt.Vect2Ang(v);
143 assertEquals("phi=" + ang_tup[1], 0., ang_tup[1] / PI, 1e-10);
144 System.out.println(" test Vect2Ang is done");
145 }
146
147 /**
148 * tests calculation of pixel from polar angles
149 * in ring schema of pixalization
150 * @throws Exception
151 */
152 public void testAng2Pix() throws Exception {
153 System.out.println(" Test ang2pix_ring ___________________");
154 double PI = Math.PI;
155 long pix = -1;
156 double theta = PI / 2. - 0.2;
157 double phi = PI / 2. ;
158 long nside = 4;
159 PixTools pt = new PixTools();
160 try {
161 pix = pt.ang2pix_ring(nside,theta, phi);
162 } catch (Exception e) {
163 e.printStackTrace();
164 }
165 Vector3d v = pt.Ang2Vec(theta,phi);
166 long pix1 = pt.vect2pix_ring(nside,v);
167 assertEquals("pix=" + pix, pix1, pix, 1e-10);
168 assertEquals("pix=" + pix, 76, pix, 1e-10);
169
170 long pix2 = pt.ang2pix_nest(nside,theta,phi);
171 long pix3 = pt.vect2pix_nest(nside,v);
172 assertEquals("pix2=" + pix2, pix3, pix2, 1e-10);
173 assertEquals("pix2=" + pix2, 92, pix2, 1e-10);
174
175
176 double[] radec = pt.pix2ang_ring(nside,76);
177 assertEquals("theta=" + theta, theta, radec[0], 4e-2);
178 assertEquals("phi=" + phi, radec[1], phi, 1e-2);
179 double[] radec1 = pt.pix2ang_nest(nside,92);
180 System.out.println("theta="+radec1[0]+" phi="+radec1[1]);
181 assertEquals("theta=" + theta, theta, radec1[0], 4e-2);
182 assertEquals("phi=" + phi, radec1[1], phi, 1e-2);
183 System.out.println(" test Ang2Pix is done");
184 }
185
186 /**
187 * tests calculation of unit vector from polar angles
188 * @throws Exception
189 */
190 public void testAng2Vect() throws Exception {
191 System.out.println(" Start test Ang2Vect----------------");
192 double PI = Math.PI;
193 double theta = PI / 2.;
194 double phi = PI / 2;
195 PixTools pt = new PixTools();
196 Vector3d v = pt.Ang2Vec(theta, phi);
197 System.out.println("Vector x=" + v.x + " y=" + v.y + " z=" + v.z);
198 assertEquals("x=" + v.x, 0., v.x, 1e-10);
199 assertEquals("y=" + v.y, 1., v.y, 1e-10);
200 assertEquals("z=" + v.z, 0., v.z, 1e-10);
201 System.out.println(" test Ang2Vect is done");
202 }
203
204 /**
205 * tests calculation of ring number from z coordinate
206 * @throws Exception
207 */
208 public void testRingNum() throws Exception {
209 double z = 0.25;
210 int nside = 1;
211 System.out.println("Start test RingNum !!!!!!!!!!!!!!!!!!!!");
212 PixTools pt = new PixTools();
213 int nring = (int) pt.RingNum(nside, z);
214 System.out.println("z=" + z + " ring number =" + nring);
215 assertEquals("z=" + z, 2, nring, 1e-10);
216 z = -0.25;
217 nring = (int) pt.RingNum(nside, z);
218 assertEquals("z=" + z, 2, nring, 1e-10);
219 z = 0.8;
220 nring = (int) pt.RingNum(nside, z);
221 assertEquals("z=" + z, 1, nring, 1e-10);
222 z = -0.8;
223 nring = (int) pt.RingNum(nside, z);
224 assertEquals("z=" + z, 3, nring, 1e-10);
225 System.out.println(" test RingNum is done");
226 nside = 4;
227 int pix = 3;
228 Vector3d v = pt.pix2vect_ring(nside, pix);
229 z = v.z;
230 nring = (int) pt.RingNum(nside, z);
231 assertEquals("z=" + z, 1, nring, 1e-10);
232 pix = 11;
233 v = pt.pix2vect_ring(nside, pix);
234 z = v.z;
235 nring = (int) pt.RingNum(nside, z);
236 assertEquals("z=" + z, 2, nring, 1e-10);
237 pix = 23;
238 v = pt.pix2vect_ring(nside, pix);
239 z = v.z;
240 nring = (int) pt.RingNum(nside, z);
241 assertEquals("z=" + z, 3, nring, 1e-10);
242 pix = 39;
243 v = pt.pix2vect_ring(nside, pix);
244 z = v.z;
245 nring = (int) pt.RingNum(nside, z);
246 assertEquals("z=" + z, 4, nring, 1e-10);
247 pix = 55;
248 v = pt.pix2vect_ring(nside, pix);
249 z = v.z;
250 nring = (int) pt.RingNum(nside, z);
251 assertEquals("z=" + z, 5, nring, 1e-10);
252 pix = 71;
253 v = pt.pix2vect_ring(nside, pix);
254 z = v.z;
255 nring = (int) pt.RingNum(nside, z);
256 assertEquals("z=" + z, 6, nring, 1e-10);
257 pix = 87;
258 v = pt.pix2vect_ring(nside, pix);
259 z = v.z;
260 nring = (int) pt.RingNum(nside, z);
261 assertEquals("z=" + z, 7, nring, 1e-10);
262 pix = 103;
263 v = pt.pix2vect_ring(nside, pix);
264 z = v.z;
265 nring = (int) pt.RingNum(nside, z);
266 assertEquals("z=" + z, 8, nring, 1e-10);
267 pix = 119;
268 v = pt.pix2vect_ring(nside, pix);
269 z = v.z;
270 nring = (int) pt.RingNum(nside, z);
271 assertEquals("z=" + z, 9, nring, 1e-10);
272 pix = 135;
273 v = pt.pix2vect_ring(nside, pix);
274 z = v.z;
275 nring = (int) pt.RingNum(nside, z);
276 assertEquals("z=" + z, 10, nring, 1e-10);
277 pix = 151;
278 v = pt.pix2vect_ring(nside, pix);
279 z = v.z;
280 nring = (int) pt.RingNum(nside, z);
281 assertEquals("z=" + z, 11, nring, 1e-10);
282 pix = 167;
283 v = pt.pix2vect_ring(nside, pix);
284 z = v.z;
285 nring = (int) pt.RingNum(nside, z);
286 assertEquals("z=" + z, 12, nring, 1e-10);
287 pix = 169;
288 v = pt.pix2vect_ring(nside, pix);
289 z = v.z;
290 nring = (int) pt.RingNum(nside, z);
291 assertEquals("z=" + z, 13, nring, 1e-10);
292 pix = 180;
293 v = pt.pix2vect_ring(nside, pix);
294 z = v.z;
295 nring = (int) pt.RingNum(nside, z);
296 assertEquals("z=" + z, 14, nring, 1e-10);
297 System.out.println("End test RingNum");
298 }
299
300 /**
301 * tests conversion from nest schema pixel to ring schema pixel
302 * @throws Exception
303 */
304 public void testNest2Ring() throws Exception {
305 PixTools pt = new PixTools();
306 int ipnest = 3;
307 int nside = 2;
308 int ipring = (int) pt.nest2ring(nside, ipnest);
309 assertEquals("ipring=" + ipring, 0, ipring, 1e-10);
310 ipnest = 0;
311 nside = 2;
312 ipring = (int) pt.nest2ring(nside, ipnest);
313 assertEquals("ipring=" + ipring, 13, ipring, 1e-10);
314 ipnest = 18;
315 nside = 2;
316 ipring = (int) pt.nest2ring(nside, ipnest);
317 assertEquals("ipring=" + ipring, 27, ipring, 1e-10);
318 ipnest = 23;
319 nside = 2;
320 ipring = (int) pt.nest2ring(nside, ipnest);
321 assertEquals("ipring=" + ipring, 14, ipring, 1e-10);
322 ipnest = 5;
323 nside = 4;
324 ipring = (int) pt.nest2ring(nside, ipnest);
325 assertEquals("ipring = " + ipring, 27, ipring, 1e-10);
326 System.out.println(" test Nest2Ring is done");
327 }
328
329 /**
330 * tests conversion from ring schema pixel to nest schema pixel
331 * @throws Exception
332 */
333 public void testRing2Nest() throws Exception {
334 PixTools pt = new PixTools();
335 System.out.println(" start test Ring2Nest !!!!!!!!!!!!!!!!!!!!!!");
336 int ipring = 0;
337 int nside = 2;
338
339 int ipnest = (int) pt.ring2nest(nside, ipring);
340 assertEquals("ipnest=" + ipnest, 3, ipnest, 1e-10);
341 ipring = 13;
342 nside = 2;
343 ipnest = (int) pt.ring2nest(nside, ipring);
344 assertEquals("ipnest=" + ipnest, 0, ipnest, 1e-10);
345 ipring = 27;
346 nside = 2;
347 ipnest = (int) pt.ring2nest(nside, ipring);
348 assertEquals("ipnest=" + ipnest, 18, ipnest, 1e-10);
349 ipring = 14;
350 nside = 2;
351 ipnest = (int) pt.ring2nest(nside, ipring);
352 assertEquals("ipnest=" + ipnest, 23, ipnest, 1e-10);
353 ipring = 27;
354 nside = 4;
355 ipnest = (int) pt.ring2nest(nside, ipring);
356 assertEquals("ipnest = " + ipnest, 5, ipnest, 1e-10);
357 ipring = 83;
358 nside = 4;
359 ipnest = (int) pt.ring2nest(nside, ipring);
360 assertEquals("ipnest = " + ipnest, 123, ipnest, 1e-10);
361 System.out.println(" test Ring2Nest is done");
362 }
363
364 /**
365 * tests Next_In_Line method for the nest schema
366 * @throws Exception
367 */
368 public void testNext_In_Line_Nest() throws Exception {
369 PixTools pt = new PixTools();
370 int ipix = 0;
371 int nside = 2;
372 int ipnext = (int) pt.next_in_line_nest(nside, ipix);
373 assertEquals("ipnext=" + ipnext, 23, ipnext, 1e-10);
374 ipix = 1;
375 nside = 2;
376 ipnext = (int) pt.next_in_line_nest(nside, ipix);
377 assertEquals("ipnext=" + ipnext, 6, ipnext, 1e-10);
378 ipix = 4;
379 nside = 2;
380 ipnext = (int) pt.next_in_line_nest(nside, ipix);
381 assertEquals("ipnext=" + ipnext, 27, ipnext, 1e-10);
382 ipix = 27;
383 nside = 2;
384 ipnext = (int) pt.next_in_line_nest(nside, ipix);
385 assertEquals("ipnext=" + ipnext, 8, ipnext, 1e-10);
386 ipix = 12;
387 nside = 2;
388 ipnext = (int) pt.next_in_line_nest(nside, ipix);
389 assertEquals("ipnext=" + ipnext, 19, ipnext, 1e-10);
390 ipix = 118;
391 nside = 4;
392 ipnext = (int) pt.next_in_line_nest(nside, ipix);
393 assertEquals("ipnext = " + ipnext, 117, ipnext, 1e-10);
394 System.out.println(" test next_in_line_nest is done");
395 }
396
397 /**
398 * tests InRing method
399 * @throws Exception
400 */
401 public void testInRing() throws Exception {
402 System.out.println(" Start test InRing !!!!!!!!!!!!!!!!!!!!!!!!!");
403 PixTools pt = new PixTools();
404 int[] nestComp = { 19, 0, 23, 4, 27, 8, 31, 12 };
405 long[] ringHi = {17, 18, 19, 12, 13 };
406 long[] nestHi = { 8, 31, 12, 19, 0 };
407 long[] ringLow = {19, 12, 13, 14, 15 };
408 long[] nestLow = { 12, 19, 0, 23, 4 };
409 double PI = Math.PI;
410 int nside = 2;
411 boolean nest = false;
412 int iz = 3;
413 double phi = PI;
414 double dphi = PI;
415 ArrayList ring = pt.InRing(nside, iz, phi, dphi, nest);
416 for (int i = 0; i < ring.size(); i++) {
417 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
418 i + 12, ((Long) ring.get(i)).longValue(), 1e-10);
419 }
420 Vector3d v = new Vector3d(1., 0., 0.);
421 double[] ang_tup = { 0., 0. };
422 ang_tup = pt.Vect2Ang(v);
423 phi = ang_tup[1]/PI;
424 ring = pt.InRing(nside, iz, phi, dphi, nest);
425 for (int i = 0; i < ring.size(); i++) {
426 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
427 i + 12, ((Long) ring.get(i)).longValue(), 1e-10);
428 }
429 Vector3d v1 = new Vector3d(-1., 0., 0.);
430
431 ang_tup = pt.Vect2Ang(v1);
432 phi = ang_tup[1]/PI;
433 ring = pt.InRing(nside, iz, phi, dphi, nest);
434 for (int i = 0; i < ring.size(); i++) {
435 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
436 i + 12, ((Long) ring.get(i)).longValue(), 1e-10);
437 }
438 phi = 1.75*PI;
439 dphi = 0.5*PI;
440 ring = pt.InRing(nside, iz, phi, dphi, nest);
441 for (int i = 0; i < ring.size(); i++) {
442 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
443 ringHi[i], ((Long) ring.get(i)).longValue(), 1e-10);
444
445 }
446
447 phi = 1.75*PI;
448 dphi = 0.5*PI;
449 nest = true;
450 ring = pt.InRing(nside, iz, phi, dphi, nest);
451 for (int i = 0; i < ring.size(); i++) {
452 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
453 nestHi[i], ((Long) ring.get(i)).longValue(), 1e-10);
454
455 }
456 phi = 0.25*PI;
457 dphi = 0.5*PI;
458 nest = false;
459 ring = pt.InRing(nside, iz, phi, dphi, nest);
460 for (int i = 0; i < ring.size(); i++) {
461 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
462 ringLow[i], ((Long) ring.get(i)).longValue(), 1e-10);
463
464 }
465 phi = 0.25*PI;
466 dphi = 0.5*PI;
467 nest = true;
468 ring = pt.InRing(nside, iz, phi, dphi, nest);
469 for (int i = 0; i < ring.size(); i++) {
470 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
471 nestLow[i], ((Long) ring.get(i)).longValue(), 1e-10);
472
473 }
474
475 nest = true;
476 dphi = PI;
477 ring = pt.InRing(nside, iz, phi, dphi, nest);
478 for (int i = 0; i < ring.size(); i++) {
479 assertEquals("ipnext = " + ((Long) ring.get(i)).longValue(),
480 nestComp[i], ((Long) ring.get(i)).longValue(), 1e-10);
481 }
482 nest = false;
483 nside = 4;
484 phi = 2.1598449493429825;
485 iz = 8;
486 dphi = 0.5890486225480867;
487 // System.out.println(" iz="+iz+" phi="+phi+" dphi="+dphi);
488 ring = pt.InRing(nside, iz, phi, dphi, nest);
489 // for (int i = 0; i<ring.size(); i++) {
490 // System.out.println("ipnext = "+ ((Integer)ring.get(i)).intValue());
491 // }
492 nest = false;
493 nside = 4;
494 dphi = 0. * PI;
495 iz = 8;
496 phi = 2.1598449493429825;
497 // System.out.println(" iz="+iz+" phi="+phi+" dphi="+dphi);
498 ring = pt.InRing(nside, iz, phi, dphi, nest);
499 // for (int i = 0; i<ring.size(); i++) {
500 // System.out.println("ipnext = "+ ((Integer)ring.get(i)).intValue());
501 // }
502 System.out.println(" test InRing is done");
503 }
504
505 /**
506 * tests Neighbour's method for nest schema of the pixelization
507 * @throws Exception
508 */
509 public void testNeighbours_Nest() throws Exception {
510 System.out.println(" Start test Neighbours_Nest !!!!!!!!!!!!!!!!!");
511 PixTools pt = new PixTools();
512 long nside = 2;
513 long ipix = 25;
514 long[] test17 = { 34, 16, 18, 19, 2, 0, 22, 35 };
515 long[] test32 = { 40, 44, 45, 34, 35, 33, 38, 36 };
516 long[] test3 = { 0, 2, 13, 15, 11, 7, 6, 1 };
517 long[] test25 = { 42, 24, 26, 27, 10, 8, 30, 43 };
518 //
519 ArrayList npixList = new ArrayList();
520 npixList = pt.neighbours_nest(nside, ipix);
521 for (int i = 0; i < npixList.size(); i++) {
522 assertEquals("ip = " + ((Long) npixList.get( i)).longValue(),
523 test25[ i], ((Long) npixList.get( i)).longValue(), 1e-10);
524 }
525 ipix = 17;
526
527 npixList = pt.neighbours_nest(nside, ipix);
528 for (int i = 0; i < npixList.size(); i++) {
529 assertEquals("ip = " + ((Long) npixList.get(i)).longValue(),
530 test17[i], ((Long) npixList.get(i)).longValue(), 1e-10);
531 }
532 ipix = 32;
533
534 npixList = pt.neighbours_nest(nside, ipix);
535 for (int i = 0; i < npixList.size(); i++) {
536 assertEquals("ip = " + ((Long) npixList.get(i)).longValue(),
537 test32[i], ((Long) npixList.get(i)).longValue(), 1e-10);
538 }
539 ipix = 3;
540
541 npixList = pt.neighbours_nest(nside, ipix);
542 for (int i = 0; i < npixList.size(); i++) {
543 assertEquals("ip = " + ((Long) npixList.get(i)).longValue(),
544 test3[i], ((Long) npixList.get(i)).longValue(), 1e-10);
545 }
546 System.out.println(" test NeighboursNest is done");
547 }
548
549 /**
550 * tests intrs_intrv method
551 * @throws Exception
552 */
553 public void testIntrs_Intrv() throws Exception {
554 System.out.println(" test intrs_intrv !!!!!!!!!!!!!!!!!!!!!!!!!!!");
555 PixTools pt = new PixTools();
556 double[] d1 = { 1.0, 9.0 };
557 double[] d2 = { 3.0, 16.0 };
558 double[] di;
559 // System.out.println("Case "+d1[0]+" "+d1[1]+" | "+d2[0]+" "+d2[1]);
560 di = pt.intrs_intrv(d1, d2);
561 // System.out.println("Result "+di[0]+" - "+di[1]);
562 int n12 = di.length / 2;
563 assertEquals("n12 = " + n12, 1, n12, 1e-6);
564 assertEquals("di[0] = " + di[0], 3.0, di[0], 1e-6);
565 assertEquals("di[1] = " + di[1], 9.0, di[1], 1e-6);
566 d1 = new double[] { 0.537, 4.356 };
567 d2 = new double[] { 3.356, 0.8 };
568 // System.out.println("Case "+d1[0]+" "+d1[1]+" | "+d2[0]+" "+d2[1]);
569 di = pt.intrs_intrv(d1, d2);
570 n12 = di.length / 2;
571 assertEquals("n12 = " + n12, 2, n12, 1e-6);
572 assertEquals("di[0] = " + di[0], 0.537, di[0], 1e-6);
573 assertEquals("di[1] = " + di[1], 0.8, di[1], 1e-6);
574 assertEquals("di[2] = " + di[2], 3.356, di[2], 1e-6);
575 assertEquals("di[1] = " + di[3], 4.356, di[3], 1e-6);
576
577 d1 = new double[] { 2.356194490092345, 2.356194490292345 };
578 d2 = new double[] { 1.251567, 4.17 };
579 // System.out.println("Case "+d1[0]+" "+d1[1]+" | "+d2[0]+" "+d2[1]);
580 di = pt.intrs_intrv(d1, d2);
581 n12 = di.length / 2;
582 assertEquals("n12 = " + n12, 1, n12, 1e-6);
583 assertEquals("di[0] = " + di[0], 2.35619449009, di[0], 1e-6);
584 assertEquals("di[1] = " + di[1], 2.35619449029, di[1], 1e-6);
585
586 System.out.println(" test intrs_intrv is done");
587 }
588
589 /**
590 * tests conversion from pixel number to vector
591 * @throws Exception
592 */
593 public void testPix2Vect_ring() throws Exception {
594 System.out.println("Start test Pix2Vect_ring !!!!!!!!!!!!!!!!!!!");
595 double TWOPI = 2.0 * Math.PI;
596 int nside = 2;
597 int ipix = 0;
598 PixTools pt = new PixTools();
599 Vector3d v1 = new Vector3d(0., 0., 0.);
600 v1 = pt.pix2vect_ring(nside, ipix);
601 assertEquals("v1.z = " + v1.z, 1.0, v1.z, 1e-1);
602
603 ipix = 20;
604 Vector3d v2 = new Vector3d(0., 0., 0.);
605 v2 = pt.pix2vect_ring(nside, ipix);
606 assertEquals("v2.x = " + v2.x, 1.0, v2.x, 1e-1);
607 assertEquals("v2.z = " + v2.z, 0.0, v2.z, 1e-1);
608 ipix = 22;
609 Vector3d v3 = new Vector3d();
610 v3 = pt.pix2vect_ring(nside, ipix);
611 assertEquals("v3.y = " + v3.y, 1.0, v3.y, 1e-1);
612 assertEquals("v3.z = " + v3.z, 0.0, v3.z, 1e-1);
613 // System.out.println("Vector3 x="+v3.x+" y="+v3.y+" z="+v3.z);
614 ipix = 95;
615 nside = 4;
616 v1 = pt.pix2vect_ring(nside, ipix);
617 v1.normalize();
618 double phi1 = Math.atan2(v1.y, v1.x);
619 double[] tetphi = new double[2];
620 tetphi = pt.pix2ang_ring(nside, ipix);
621 assertEquals("phi = " + phi1, 0.0, Math.abs(phi1 - tetphi[1]), 1e-10);
622 ipix = 26;
623 nside = 4;
624 v1 = pt.pix2vect_ring(nside, ipix);
625 v1.normalize();
626 phi1 = Math.atan2(v1.y, v1.x);
627 if (phi1 < 0.)
628 phi1 += TWOPI;
629 tetphi = new double[2];
630 tetphi = pt.pix2ang_ring(nside, ipix);
631 assertEquals("phi = " + phi1, 0.0, Math.abs(phi1 - tetphi[1]), 1e-10);
632 System.out.println("------------------------------------------");
633 System.out.println(" test pix2vect_ring is done");
634 }
635
636 /**
637 * tests conversion from pixel number to vector
638 * @throws Exception
639 */
640 public void testPix2Vect_nest() throws Exception {
641 double TWOPI = 2.0 * Math.PI;
642 int nside = 2;
643 int ipix = 3;
644 System.out.println(" Start test Pix2Vect_nest !!!!!!!!!!!!!!");
645 PixTools pt = new PixTools();
646 Vector3d v1 = new Vector3d(0., 0., 0.);
647 v1 = pt.pix2vect_nest(nside, ipix);
648 assertEquals("v1.z = " + v1.z, 1.0, v1.z, 1e-1);
649 ipix = 17;
650 Vector3d v2 = new Vector3d(0., 0., 0.);
651 v2 = pt.pix2vect_nest(nside, ipix);
652 assertEquals("v2.x = " + v2.x, 1.0, v2.x, 1e-1);
653 assertEquals("v2.z = " + v2.z, 0.0, v2.z, 1e-1);
654
655 ipix = 21;
656 Vector3d v3 = new Vector3d();
657 v3 = pt.pix2vect_nest(nside, ipix);
658 assertEquals("v3.y = " + v3.y, 1.0, v3.y, 1e-1);
659 assertEquals("v3.z = " + v3.z, 0.0, v3.z, 1e-1);
660 nside = 4;
661 ipix = 105;
662 v1 = pt.pix2vect_nest(nside, ipix);
663 v1.normalize();
664 double phi1 = Math.atan2(v1.y, v1.x);
665 if (phi1 < 0.)
666 phi1 += TWOPI;
667 double[] tetphi = new double[2];
668 tetphi = pt.pix2ang_nest(nside, ipix);
669 assertEquals("phi = " + phi1, 0.0, Math.abs(phi1 - tetphi[1]), 1e-10);
670 nside = 4;
671 ipix = 74;
672 v1 = pt.pix2vect_nest(nside, ipix);
673 v1.normalize();
674 phi1 = Math.atan2(v1.y, v1.x);
675 if (phi1 < 0.)
676 phi1 += TWOPI;
677 tetphi = new double[2];
678 tetphi = pt.pix2ang_nest(nside, ipix);
679 assertEquals("phi = " + phi1, 0.0, Math.abs(phi1 - tetphi[1]), 1e-10);
680
681 System.out.println(" test pix2vect_nest is done");
682 System.out.println("-------------------------------------------");
683 }
684
685 /**
686 * tests conversion from vector to pixel number
687 * @throws Exception
688 */
689 public void testVect2Pix_ring() throws Exception {
690 System.out.println("Start test Vect2Pix_ring !!!!!!!!!!!!!!!!!!!");
691
692 long nside = 4;
693 long ipix = 83;
694 long respix = 0;
695 PixTools pt = new PixTools();
696 Vector3d v1 = new Vector3d(0., 0., 0.);
697 v1 = pt.pix2vect_ring(nside, ipix);
698 respix = (int) pt.vect2pix_ring(nside, v1);
699 assertEquals("respix = " + respix, 83, respix, 1e-10);
700 // Hi resolution test
701 long nside1 = 1 << 20;
702 long maxpix= pt.Nside2Npix(nside1);
703 System.out.println("nside="+nside1+" maxpix="+maxpix);
704 Vector3d v2 = new Vector3d( -0.704, 0.580, 0.408);
705 respix = pt.vect2pix_ring(nside1, v2); // ring pixel
706 long respixN = pt.ring2nest(nside1, respix); // convert to nest
707 long respixNC = pt.vect2pix_nest(nside1,v2); // nest pixel from the same vector
708 long respixR = pt.nest2ring(nside1, respixN); // convert pixel
709 System.out.println(" orig="+respix+" doubleT="+respixR+" nest="+respixN+" correct nest="+respixNC);
710 assertEquals("ringpix = " + respix, respix, respixR);
711 assertEquals("nestpix = " + respixNC, respixNC, respixN);
712 System.out.println("------------------------------------------");
713 System.out.println(" test vect2pix_ring is done");
714 }
715
716 /**
717 * tests conversion from vector to pixel number
718 * @throws Exception
719 */
720 public void testVect2Pix_nest() throws Exception {
721 System.out.println("Start test Vect2Pix_nest !!!!!!!!!!!!!!!!!!!");
722 long nside = 4;
723 long ipix = 83;
724 long respix = 0;
725 PixTools pt = new PixTools();
726 Vector3d v1 = new Vector3d(0., 0., 0.);
727 v1 = pt.pix2vect_ring(nside, ipix);
728 respix = (int) pt.vect2pix_nest(nside, v1);
729 assertEquals("respix = " + respix, 123, respix, 1e-10);
730 //
731 long nside1 = 1 << 20;
732 long maxpix= pt.Nside2Npix(nside1);
733 System.out.println("nside="+nside1+" maxpix="+maxpix);
734 Vector3d v2 = new Vector3d( -0.704, 0.580, 0.408);
735 respix = pt.vect2pix_nest(nside1, v2);
736 long respixRC = pt.vect2pix_ring(nside1, v2);
737 long respixR = pt.nest2ring(nside1, respix);
738 long respixN = pt.ring2nest(nside1, respixRC);
739 System.out.println(" orig="+respix+" doubleT="+respixN+" ring="+respixR+" correct ring="+respixRC);
740 assertEquals("ringpix = " + respixRC, respixRC, respixR);
741 assertEquals("nestpix = " + respix, respix, respixN);
742 System.out.println("------------------------------------------");
743 System.out.println(" test vect2pix_nest is done");
744 }
745
746 /**
747 * tests Query_Strip method
748 * @throws Exception
749 */
750 public void testQuery_Strip() throws Exception {
751 System.out.println(" Start test query Strip !!!!!!!!!!!!!!!!");
752 PixTools pt = new PixTools();
753 int[] pixel1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
754 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
755 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
756 48, 49, 50, 51, 52, 53, 54, 55 };
757 int nside = 4;
758 int nest = 0;
759 double theta1 = 0.0;
760 double theta2 = Math.PI / 4.0 + 0.2;
761 ArrayList pixlist;
762 pixlist = pt.query_strip(nside, theta1, theta2, nest);
763 int nlist = pixlist.size();
764 for (int i = 0; i < nlist; i++) {
765 long ipix = ((Long) pixlist.get(i)).longValue();
766 assertEquals("pixel = " + ipix, pixel1[i], ipix, 1e-10);
767 }
768
769 System.out.println(" test query_strip is done");
770
771 }
772
773 /**
774 * tests Query_Disc method
775 * @throws Exception
776 */
777 public void testQuery_Disc() throws Exception {
778 System.out.println(" Start test query_disc !!!!!!!!!!!!!!!!!!!!!");
779 PixTools pt = new PixTools();
780 long nside = 4;
781 int nest = 0;
782 long ipix = 0;
783
784
785 int[] pixel1 = { 45, 46, 60, 61, 62, 77, 78, 92, 93, 94,
786 109, 110, 124, 125, 126, 141, 142
787 };
788
789
790
791 int[] pixel2 = { 24, 19, 93, 18, 17, 87, 16,86, 85,
792 106, 84, 159, 81, 158, 157, 155, 156
793 };
794
795
796 int[] pixel3 = {52, 79, 49, 78, 77, 75, 76, 74, 73, 70,
797 72, 67, 189, 66, 65, 183, 64
798 };
799 int inclusive = 1;
800 double radius = Math.PI / 8.0;
801 Vector3d v = pt.pix2vect_ring(nside, 93);
802 ArrayList pixlist;
803 pixlist = pt.query_disc(nside, v, radius, nest, inclusive);
804
805 int nlist = pixlist.size();
806
807 for (int i = 0; i < nlist; i++) {
808 ipix = ((Long) pixlist.get(i)).longValue();
809 // System.out.println("i="+i+"pixel="+ipix+" pixel1="+pixel1[i]);
810 assertEquals("pixel = " + ipix, pixel1[i], ipix, 1e-10);
811 }
812 radius = Math.PI/2.0;
813 nest = 0;
814 v = new Vector3d(1.0,0.0,0.0);
815 ArrayList pixlistT = pt.query_disc(2, v, radius, nest, inclusive);
816
817 radius = Math.PI / 8.0;
818 v = pt.pix2vect_ring(nside, 93);
819 nest = 1;
820 ArrayList pixlist2 = pt.query_disc(nside, v, radius, nest, inclusive);
821
822 int nlist2 = pixlist2.size();
823 assertEquals("npix="+nlist, nlist, nlist2, 1e-10);
824 for (int i = 0; i < nlist2; i++) {
825 ipix = ((Long) pixlist2.get(i)).longValue();
826
827 assertEquals("pixel = " + ipix, pixel2[i], ipix, 1e-10);
828
829 }
830
831 v = pt.pix2vect_ring(nside, 103);
832 nest = 1;
833 inclusive = 1;
834 ArrayList pixlist3 = pt.query_disc(nside, v, radius, nest, inclusive);
835 nlist = pixlist3.size();
836 assertEquals("npix="+nlist, nlist, pixel3.length, 1e-10);
837
838 for (int i = 0; i < pixlist3.size(); i++) {
839 ipix = ((Long) pixlist3.get(i)).longValue();
840
841 assertEquals("pixel = " + ipix, pixel3[i], ipix, 1e-10);
842 }
843
844
845 for (int i=0; i<pixel1.length; i++) {
846 long ipixR = pixel1[i];
847 long ipixT = pt.ring2nest(nside, ipixR);
848 assertEquals("pixel="+ipixT, ipixT, pixel2[i], 1e-10);
849 long ipixN = pt.nest2ring(nside, ipixT);
850 assertEquals("pixel="+ipixN, ipixN, pixel1[i], 1e-10);
851 }
852 System.out.println(" End query_disk test ______________________________");
853 }
854
855 /**
856 * tests Query_Triangle method
857 * @throws Exception
858 */
859 public void testQuery_Triangle() throws Exception {
860 PixTools pt = new PixTools();
861 long nside = 4;
862 int nest = 0;
863 long ipix = 0;
864 int[] pixel1 = { 57, 58, 59, 60, 61, 62, 74, 75, 76, 77, 78, 90, 91,
865 92, 93, 107, 108, 109, 123, 124, 140 };
866 int[] pixel2 = { 88, 89, 90, 91, 105, 106, 107, 108, 121, 122, 123,
867 138, 139, 154 };
868 int[] pixel3 = { 49, 64, 80, 81, 95, 96, 112, 113,
869 127, 128, 142, 143, 144, 145 };
870 int[] pixel4 = { 36, 52, 53, 67, 68, 69, 83, 84, 85, 86, 98, 99, 100,
871 101, 102, 114, 115, 116, 117, 118, 119, 129, 130, 131, 132,
872 133, 134, 135 };
873 int[] pixel5 = { 58, 127, 56, 126, 125, 50, 123, 124, 119, 48, 122,
874 121, 118, 117, 74, 175, 120, 115, 116, 191, 72, 174, 173, 114,
875 113, 190, 189, 66 };
876 int[] pixel6 = { 110, 123, 124, 125, 140, 141, 156 };
877 int[] pixel7 = { 53, 68, 69 };
878 long pix1 = 62;
879 long pix2 = 57;
880 long pix3 = 140;
881 System.out.println("Start test Query Triangle !!!!!!!!!!!!!!!!!!!!");
882 Vector3d v11 = pt.pix2vect_ring(nside, pix1);
883
884 Vector3d v22 = pt.pix2vect_ring(nside, pix2);
885
886 Vector3d v33 = pt.pix2vect_ring(nside, pix3);
887
888 // System.out.println("nside="+nside+" triangle pixels "+pix1+" "+pix2+"
889 // "+pix3);
890 int inclusive = 0;
891
892 ArrayList pixlist;
893 pixlist = pt.query_triangle(nside, v11, v22, v33, nest, inclusive);
894
895 int nlist = pixlist.size();
896
897 for (int i = 0; i < nlist; i++) {
898 ipix = ((Long) pixlist.get(i)).longValue();
899
900 assertEquals("pixel = " + ipix, pixel1[i], ipix, 1e-10);
901 }
902 pix1 = 92;
903 pix2 = 88;
904 pix3 = 154;
905 v11 = pt.pix2vect_ring(nside, pix1);
906 v22 = pt.pix2vect_ring(nside, pix2);
907 v33 = pt.pix2vect_ring(nside, pix3);
908
909 inclusive = 0;
910 ArrayList pixlist1;
911 pixlist1 = pt.query_triangle(nside, v11, v22, v33, nest, inclusive);
912
913 nlist = pixlist1.size();
914
915 for (int i = 0; i < nlist; i++) {
916 ipix = ((Long) pixlist1.get(i)).longValue();
917 // System.out.println(ipix);
918 assertEquals("pixel = " + ipix, pixel2[i], ipix, 1e-10);
919 }
920 pix1 = 49;
921 pix2 = 142;
922 pix3 = 145;
923 v11 = pt.pix2vect_ring(nside, pix1);
924 v22 = pt.pix2vect_ring(nside, pix2);
925 v33 = pt.pix2vect_ring(nside, pix3);
926
927 inclusive = 0;
928 ArrayList pixlist2;
929 pixlist2 = pt.query_triangle(nside, v11, v22, v33, nest, inclusive);
930
931 nlist = pixlist2.size();
932
933 for (int i = 0; i < nlist; i++) {
934 ipix = ((Long) pixlist2.get(i)).longValue();
935 // System.out.println(ipix);
936 assertEquals("pixel = " + ipix, pixel3[i], ipix, 1e-10);
937 }
938 pix1 = 36;
939 pix2 = 129;
940 pix3 = 135;
941 v11 = pt.pix2vect_ring(nside, pix1);
942 v22 = pt.pix2vect_ring(nside, pix2);
943 v33 = pt.pix2vect_ring(nside, pix3);
944
945 inclusive = 0;
946
947 pixlist2 = pt.query_triangle(nside, v11, v22, v33, nest, inclusive);
948
949 nlist = pixlist2.size();
950
951 for (int i = 0; i < nlist; i++) {
952 ipix = ((Long) pixlist2.get(i)).longValue();
953 assertEquals("pixel = " + ipix, pixel4[i], ipix, 1e-10);
954 }
955 pix1 = 36;
956 pix2 = 129;
957 pix3 = 135;
958 nest = 1;
959 v11 = pt.pix2vect_ring(nside, pix1);
960 v22 = pt.pix2vect_ring(nside, pix2);
961 v33 = pt.pix2vect_ring(nside, pix3);
962 inclusive = 0;
963
964 pixlist2 = pt.query_triangle(nside, v11, v22, v33, nest, inclusive);
965
966 nlist = pixlist2.size();
967
968 for (int i = 0; i < nlist; i++) {
969 ipix = ((Long) pixlist2.get(i)).longValue();
970 // System.out.println("ipix="+ipix);
971 assertEquals("pixel = " + ipix, pixel5[i], ipix, 1e-10);
972 }
973 pix1 = 123;
974 pix2 = 156;
975 pix3 = 110;
976 nest = 0;
977 v11 = pt.pix2vect_ring(nside, pix1);
978 v22 = pt.pix2vect_ring(nside, pix2);
979 v33 = pt.pix2vect_ring(nside, pix3);
980 inclusive = 0;
981
982 pixlist2 = pt.query_triangle(nside, v11, v22, v33, nest, inclusive);
983
984 nlist = pixlist2.size();
985
986 for (int i = 0; i < nlist; i++) {
987 ipix = ((Long) pixlist2.get(i)).longValue();
988 assertEquals("pixel = " + ipix, pixel6[i], ipix, 1e-10);
989 // System.out.println("i="+i+" pixel#="+ipix);
990 }
991 pix1 = 69;
992 pix2 = 53;
993 pix3 = 68;
994 nest = 0;
995 v11 = pt.pix2vect_ring(nside, pix1);
996 v22 = pt.pix2vect_ring(nside, pix2);
997 v33 = pt.pix2vect_ring(nside, pix3);
998 inclusive = 0;
999
1000 pixlist2 = pt.query_triangle(nside, v11, v22, v33, nest, inclusive);
1001
1002 nlist = pixlist2.size();
1003
1004 for (int i = 0; i < nlist; i++) {
1005 ipix = ((Long) pixlist2.get(i)).longValue();
1006 assertEquals("pixel = " + ipix, pixel7[i], ipix, 1e-10);
1007 // System.out.println("i="+i+" pixel#="+ipix);
1008 }
1009 System.out.println(" test query_triangle is done");
1010
1011 }
1012
1013 /**
1014 * tests Query_Poligon method
1015 * @throws Exception
1016 */
1017 public void testQuery_Polygon() throws Exception {
1018 PixTools pt = new PixTools();
1019 long nside = 4;
1020 int nest = 0;
1021 long ipix = 0;
1022 int inclusive = 0;
1023 int[] result = { 51, 52, 53, 66, 67, 68, 69, 82, 83, 84, 85, 86, 98,
1024 99, 100, 101, 115, 116, 117 };
1025 int[] result1 = { 55, 70, 71, 87 };
1026 int[] result2 = { 137, 152, 153, 168 };
1027 int[] result3 = { 27, 43, 44, 58, 59, 60, 74, 75, 76, 77, 89, 90, 91,
1028 92, 93, 105, 106, 107, 108, 109, 110, 121, 122, 123, 124, 125,
1029 138, 139, 140, 141, 154, 156 };
1030
1031
1032 System.out.println("Start test query_polygon !!!!!!!!!!!!!!!!!!!!!!");
1033 ArrayList vlist = new ArrayList();
1034 Vector3d v = pt.pix2vect_ring(nside, 53);
1035 vlist.add( v);
1036 v = pt.pix2vect_ring(nside, 51);
1037 vlist.add( v);
1038 v = pt.pix2vect_ring(nside, 82);
1039 vlist.add( v);
1040 v = pt.pix2vect_ring(nside, 115);
1041 vlist.add( v);
1042 v = pt.pix2vect_ring(nside, 117);
1043 vlist.add( v);
1044 v = pt.pix2vect_ring(nside, 86);
1045 vlist.add( v);
1046
1047 ArrayList pixlist;
1048 pixlist = pt.query_polygon(nside, vlist, nest, inclusive);
1049 // System.out.println(" List size="+pixlist.size());
1050 int nlist = pixlist.size();
1051 // System.out.println(" Pixel list:");
1052 for (int i = 0; i < nlist; i++) {
1053 ipix = ((Long) pixlist.get(i)).longValue();
1054 assertEquals("pixel = " + ipix, result[i], ipix, 1e-10);
1055 // System.out.println("i="+i+" pixel # "+ipix);
1056 }
1057
1058 /* Yet another test */
1059
1060 ArrayList vlist1 = new ArrayList();
1061 v = pt.pix2vect_ring(nside, 71);
1062 vlist1.add( v);
1063 v = pt.pix2vect_ring(nside, 55);
1064 vlist1.add( v);
1065 v = pt.pix2vect_ring(nside, 70);
1066 vlist1.add( v);
1067 v = pt.pix2vect_ring(nside, 87);
1068 vlist1.add( v);
1069 pixlist = pt.query_polygon(nside, vlist1, nest, inclusive);
1070
1071 nlist = pixlist.size();
1072
1073 for (int i = 0; i < nlist; i++) {
1074 ipix = ((Long) pixlist.get(i)).longValue();
1075 // System.out.println("i="+i+" pixel # "+ipix);
1076 assertEquals("pixel = " + ipix, result1[i], ipix, 1e-10);
1077 }
1078
1079 /* Yet another test */
1080 ArrayList vlist2 = new ArrayList();
1081 v = pt.pix2vect_ring(nside, 153);
1082 vlist2.add( v);
1083 v = pt.pix2vect_ring(nside, 137);
1084 vlist2.add( v);
1085 v = pt.pix2vect_ring(nside, 152);
1086 vlist2.add( v);
1087 v = pt.pix2vect_ring(nside, 168);
1088 vlist2.add( v);
1089 pixlist = pt.query_polygon(nside, vlist2, nest, inclusive);
1090
1091 nlist = pixlist.size();
1092
1093 for (int i = 0; i < nlist; i++) {
1094 ipix = ((Long) pixlist.get(i)).longValue();
1095 assertEquals("pixel = " + ipix, result2[i], ipix, 1e-10);
1096 // System.out.println("i="+i+" pixel # "+ipix);
1097 }
1098 /* Yet another test */
1099
1100 ArrayList vlist3 = new ArrayList();
1101 v = pt.pix2vect_ring(nside, 110);
1102 vlist3.add( v);
1103 v = pt.pix2vect_ring(nside, 27);
1104 vlist3.add( v);
1105 v = pt.pix2vect_ring(nside, 105);
1106 vlist3.add( v);
1107 v = pt.pix2vect_ring(nside, 154);
1108 vlist3.add( v);
1109 v = pt.pix2vect_ring(nside, 123);
1110 vlist3.add( v);
1111 v = pt.pix2vect_ring(nside, 156);
1112 vlist3.add( v);
1113 pixlist = pt.query_polygon(nside, vlist3, nest, inclusive);
1114
1115 nlist = pixlist.size();
1116
1117 for (int i = 0; i < nlist; i++) {
1118 ipix = ((Long) pixlist.get(i)).longValue();
1119 assertEquals("pixel = " + ipix, result3[i], ipix, 1e-10);
1120 // System.out.println("i="+i+" pixel # "+ipix);
1121 }
1122 System.out.println(" test query_polygon is done");
1123
1124 }
1125 /**
1126 * tests MaxResolution method
1127 */
1128 public void testMaxResolution() {
1129 System.out.println(" Start test MaxRes !!!!!!!!!!!!!!!!!!!!!");
1130
1131 long nside = 1048576;
1132 PixTools pt = new PixTools();
1133 double res = pt.PixRes(nside);
1134 System.out.println("Minimum size of the pixel side is "+res+" arcsec.");
1135 assertEquals("res = " + res, 0.2, res, 1e-1);
1136 long nsideR = pt.GetNSide(res);
1137 assertEquals("nside = " + nside, nside, nsideR, 1e-1);
1138 System.out.println(" End of MaxRes test _______________________");
1139 }
1140 /**
1141 * tests QueryDiscResolution method
1142 * @throws Exception
1143 */
1144 public void testQueryDiscRes() throws Exception {
1145 System.out.println(" Start test DiscRes !!!!!!!!!!!!!!!!!!!!!");
1146 PixTools pt = new PixTools();
1147
1148 int nest = 0;
1149 long ipix = 0;
1150
1151 int inclusive = 0;
1152 double theta= Math.PI;
1153 double phi = Math.PI;
1154 double radius = Math.toRadians(0.2/3600.); // One arcse
1155 long nside = pt.GetNSide(radius);
1156 System.out.println(" calculated nside="+nside);
1157 long cpix = pt.ang2pix_ring(nside,theta,phi);
1158 Vector3d vc = pt.pix2vect_ring(nside, cpix);
1159 ArrayList pixlist;
1160 pixlist = pt.query_disc(nside, vc, radius, nest, inclusive);
1161
1162 int nlist = pixlist.size();
1163 for (int i = 0; i < nlist; i++) {
1164 ipix = ((Long) pixlist.get(i)).longValue();
1165 Vector3d v = pt.pix2vect_ring(nside,ipix);
1166 double dist = pt.AngDist(v,vc);
1167 assertTrue(dist<=2.*radius);
1168 }
1169 cpix = pt.ang2pix_nest(nside,theta,phi);
1170 Vector3d vc1 = pt.pix2vect_nest(nside, cpix);
1171 ArrayList pixlist1;
1172 nest = 1;
1173 radius *=4;
1174 pixlist1 = pt.query_disc(nside, vc1, radius, nest, inclusive);
1175 int nlist1 = pixlist1.size();
1176 for (int i = 0; i < nlist1; i++) {
1177 ipix = ((Long) pixlist1.get(i)).longValue();
1178 Vector3d v = pt.pix2vect_nest(nside,ipix);
1179 double dist = pt.AngDist(v,vc1);
1180 assertTrue(dist<=2.*radius);
1181 }
1182 System.out.println(" test query disk is done -------------------");
1183 }
1184 /**
1185 * test Query_disk check for consistency in the query for RING/NESTED
1186 */
1187 public void testQuery_disk2() {
1188 System.out.println(" Start test query_disk HiRes!!!!!!!!!!!!!!!!!!!!!!!!");
1189 PixTools pt = new PixTools();
1190 long nside = 1 << 20 ;
1191 double res = pt.PixRes(nside);
1192 System.out.println("nside="+nside+" sresolution="+res);
1193 double radius = Math.toRadians(res/3600.)/2.;
1194 System.out.println("radius="+radius);
1195 Vector3d v1 = new Vector3d(-0.704, 0.580, 0.408);
1196 System.out.println("!!!!!!!!!!!!! NESTED !!!!!!!!!!!");
1197 ArrayList diskQ = pt.query_disc(nside,
1198 v1,
1199 radius, 1,1); // inclusive query at vector point
1200 assertEquals("npixels = " + diskQ.size(), 8, diskQ.size() , 1e-1);
1201 long pix1 = pt.vect2pix_nest(nside, v1);
1202 Vector3d v2 = pt.pix2vect_nest(nside, pix1); // vector to pix center
1203 //
1204 ArrayList diskQ2 = pt.query_disc(nside,
1205 v2,
1206 radius, 1,1); // inclusive with point at pixel center
1207 assertEquals("npixels = " + diskQ2.size(), 9, diskQ2.size() , 1e-1);
1208
1209 //
1210 ArrayList diskQ3 = pt.query_disc(nside,
1211 v2,
1212 radius, 1,0); // exclusive with point at pixel center
1213 assertEquals("npixels = " + diskQ3.size(), 1, diskQ3.size() , 1e-1);
1214
1215 // RING schema
1216 System.out.println("!!!!!!!!!!!!! RING !!!!!!!!!!!");
1217 ArrayList diskQ4 = pt.query_disc(nside,
1218 v1,
1219 radius, 0,1); // inclusiv at vector point
1220 assertEquals("npixels = " + diskQ4.size(), 8, diskQ4.size() , 1e-1);
1221 //
1222
1223 ArrayList diskQ5 = pt.query_disc(nside,
1224 v2,
1225 radius, 0, 1); // inclusive at pixel center
1226 assertEquals("npixels = " + diskQ5.size(), 9, diskQ5.size() , 1e-1);
1227
1228 // System.out.println("n pixels in disk5 ="+diskQ5.size());
1229 ArrayList diskQ6 = pt.query_disc(nside,
1230 v2,
1231 radius, 0,0); // exclusive at pixel center
1232 assertEquals("npixels = " + diskQ6.size(), 1, diskQ6.size() , 1e-1);
1233 //
1234 // test HiRes conversions
1235 //
1236 Vector3d pos = new Vector3d( -0.704, 0.580, 0.408 );
1237
1238 nside = 1 << 20;
1239 System.out.println("HiRes transformation tests: nside="+nside);
1240 ArrayList nestPixels = pt.query_disc(nside, pos, radius, 1, 1);
1241 ArrayList ringPixels = pt.query_disc(nside, pos, radius, 0, 1);
1242 assertEquals(nestPixels.size(), ringPixels.size());
1243 for(int i=0; i< ringPixels.size(); i++) {
1244 long iring = ((Number)ringPixels.get(i)).longValue();
1245 Vector3d cv = pt.pix2vect_ring(nside, iring);
1246 long inest = pt.ring2nest(nside, iring);
1247 long inestC = ((Number)nestPixels.get(i)).longValue();
1248 Vector3d cvN = pt.pix2vect_nest(nside, inestC);
1249 long iringT = pt.nest2ring(nside, inestC);
1250 assertEquals(iring,iringT);
1251 assertEquals(inest,inestC);
1252 assertEquals(" Xv="+cv.x,cv.x,cvN.x,1.e-10);
1253 assertEquals(" Yv="+cv.y,cv.y,cvN.y,1.e-10);
1254 assertEquals(" Zv="+cv.z,cv.z,cvN.z,1.e-10);
1255 // System.out.println(" inest orig="+inestC+" transformed="+inest+" iring orig="+iring+" transf="+iringT);
1256 // System.out.println("Vector cv vs cvN x="+cv.x+" cvN.x="+cvN.x);
1257 // System.out.println("Vector cv vs cvN y="+cv.y+" cvN.y="+cvN.y);
1258 // System.out.println("Vector cv vs cvN z="+cv.z+" cvN.z="+cvN.z);
1259 double[] tetphiR = pt.pix2ang_ring(nside, iring);
1260 double[] tetphiN= pt.pix2ang_nest(nside, inestC);
1261 assertEquals(" theta="+tetphiR[0],tetphiR[0],tetphiN[0],1.e-10);
1262 assertEquals(" phi="+tetphiR[1],tetphiR[1],tetphiN[1],1.e-10);
1263 // System.out.println("theta R vs N "+tetphiR[0]+" "+tetphiN[0]);
1264 // System.out.println("phi R vs N "+tetphiR[1]+" "+tetphiN[1]);
1265 }
1266
1267 System.out.println(" End test of query_disc2____________________________");
1268
1269 }
1270 /**
1271 * tests GetNside method
1272 */
1273 public void testGetNside() {
1274 System.out.println(" Start test GetNside !!!!!!!!!!!!!!!!!!!!!");
1275
1276 double pixsize = 0.3;
1277 PixTools pt = new PixTools();
1278 long nside = pt.GetNSide(pixsize);
1279 System.out.println("Requared nside is "+nside);
1280 assertEquals("nside = " + nside, 1048576, nside, 1e-1);
1281 System.out.println(" End of GetNSide test _______________________");
1282 }
1283
1284
1285 /**
1286 * test conversion of Ra Dec to polar coordinates
1287 */
1288 public void testRaDecToPolar() {
1289 System.out.println(" Start test RaDecToPolar !!!!!!!!!!!!!!!!!!!!!");
1290 PixTools pt = new PixTools();
1291 double [] radec = new double[2];
1292 radec[0] = 312.115456;
1293 radec[1] = -1.153759;
1294 double[] polar = pt.RaDecToPolar(radec);
1295 assertEquals("theta = " + polar[0], 1.5909332201194137, polar[0], 1e-10);
1296 assertEquals("phi = " + polar[1], 5.447442353563491, polar[1], 1e-10);
1297 System.out.println("End test RaDecToPolar__________________________");
1298
1299 }
1300 }
1301
0 package gov.fnal.eag.healpix.test;
1 import java.io.File;
2 import java.io.FileNotFoundException;
3 import java.io.OutputStream;
4 import java.io.PrintStream;
5 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.HashSet;
8
9 import gov.fnal.eag.healpix.BitManipulation;
10 import gov.fnal.eag.healpix.PixTools;
11 import javax.vecmath.Vector3d;
12 import junit.framework.*;
13
14 public class QueryDiscTest extends TestCase {
15 public void testQueryDisc () {
16 long nside = 32;
17
18 int nest = 0;
19 int inclusive = 0;
20 double radius = Math.PI;
21 double radius1 = Math.PI/2.;
22 PixTools pt = new PixTools();
23 int npix = (int) pt.Nside2Npix(nside);
24 double res = pt.PixRes(nside); // pixel size in radians
25 System.out.println("res="+res);
26 double pixSize = Math.toRadians(res/3600.0); // pixel size in radians
27 System.out.println("pixSize="+pixSize+" rad");
28
29
30 ArrayList fullSky = pt.query_disc(nside, new Vector3d(0., 0., 1.), radius, nest, inclusive);
31 ArrayList firstHalfSky = pt.query_disc(nside, new Vector3d(0., 0., 1.), radius1, nest, inclusive);
32 ArrayList secondHalfSky = pt.query_disc(nside, new Vector3d(0., 0., -1.), radius1, nest, inclusive);
33 firstHalfSky.addAll(secondHalfSky);
34 HashSet pixHalfsUnique = new HashSet(firstHalfSky);
35 ArrayList pixHalfsList = new ArrayList(pixHalfsUnique);
36 Collections.sort(pixHalfsList);
37 Collections.sort(fullSky);
38
39 long listL = Math.min(fullSky.size(),pixHalfsList.size() );
40 assertEquals(npix,fullSky.size());
41 assertEquals(npix,listL);
42 for ( int i=0; i< listL; i++) {
43
44 assertEquals(fullSky.get(i),pixHalfsList.get(i));
45 }
46
47 double[] ang_tup = { 0., 0. };
48 Vector3d v = new Vector3d(1., 0., 0.);
49 Vector3d v1 = new Vector3d(-1., 0., 0.);
50 ang_tup = pt.Vect2Ang(v);
51
52 firstHalfSky = pt.query_disc(nside, new Vector3d(1., 0., 0.), radius1, nest, inclusive);
53 secondHalfSky = pt.query_disc(nside, new Vector3d(-1., 0., 0.),radius1, nest, inclusive);
54 firstHalfSky.addAll(secondHalfSky);
55 pixHalfsUnique = new HashSet(firstHalfSky);
56 pixHalfsList = new ArrayList(pixHalfsUnique);
57
58 Collections.sort(pixHalfsList);
59 // System.out.println("full size="+fullSky.size()+" half size="+pixHalfsList.size());
60 listL = Math.min(fullSky.size(),pixHalfsList.size() );
61 assertEquals(npix,fullSky.size());
62 assertEquals(npix,listL);
63 for ( int i=0; i< listL; i++) {
64 // System.out.println( "i="+i+" "+fullSky.get(i)+" "+pixHalfsList.get(i));
65 assertEquals(fullSky.get(i),pixHalfsList.get(i));
66 }
67
68
69 firstHalfSky = pt.query_disc(nside, new Vector3d(0., 1., 0.), radius1, nest, inclusive);
70 secondHalfSky = pt.query_disc(nside, new Vector3d(0., -1., 0.), radius1, nest, inclusive);
71 firstHalfSky.addAll(secondHalfSky);
72 pixHalfsUnique = new HashSet(firstHalfSky);
73 pixHalfsList = new ArrayList(pixHalfsUnique);
74 Collections.sort(pixHalfsList);
75 // System.out.println("full size="+fullSky.size()+" half size="+pixHalfsList.size());
76 listL = Math.min(fullSky.size(),pixHalfsList.size() );
77 assertEquals(npix,fullSky.size());
78
79 for ( int i=0; i< listL; i++) {
80
81 assertEquals(fullSky.get(i),pixHalfsList.get(i));
82 }
83
84 }
85
86 public void testQueryCircle(){
87 PixTools pt = new PixTools();
88
89 long nside = 512;
90
91 double angle = Math.toRadians(0.011451621372724687);
92 // double angle = Math.toRadians(0.11451621372724687);
93 Vector3d v = new Vector3d(0.8956388362603873,
94 -1.838600645782914E-4, 0.44478201534866);
95
96 //convert vector to IPIX
97 long ipix = pt.vect2pix_ring(nside, v);
98 //and query circle
99 ArrayList r = pt.query_disc(nside,v,angle,0,1);
100
101 //now make test that IPIX is in Circle, this will fail
102 System.out.println("ipix = "+ipix);
103 System.out.println("disc: "+r);
104 //ipix = 875520
105 //disc: [873471, 871424, 875519, 873472]
106
107 assertTrue("pixel not found in disc",r.contains(new Long(ipix)));
108 ArrayList r1 = pt.query_disc(nside,v,angle,0,0);
109 assertTrue("pixel not found in disc",r1.contains(new Long(ipix)));
110 }
111 /**
112 * additional tests of Query_Disc method
113 * @throws Exception
114 */
115 public void testQuery_Disc_1() throws Exception {
116 System.out.println(" Start test query_disc_1 !!!!!!!!!!!!!!!!!!!!!");
117 long[] case1 = {282, 271, 280, 269};
118 long[] case2 = { 295, 293, 292};
119 long[] case3 = {312, 301, 306, 305, 300, 295, 304, 294, 293, 282, 291, 292, 271, 289, 270, 269, 267};
120 long[] case4 = { 63, 127, 191, 255, 62, 61, 126, 125, 190, 189, 254, 253};
121
122 PixTools pt = new PixTools();
123 long nside = 8;
124 int nest = 1;
125 long ipix = 0;
126 System.out.println("case 1");
127 double pixres= Math.toRadians(pt.PixRes(nside)/3600.);
128 System.out.println("pix res="+pixres+" rad");
129
130 int inclusive = 1;
131 double radius = Math.toRadians(1.932);
132
133 double[] radec = new double[]{5.64001,-4.57566};
134 double[] polar = pt.RaDecToPolar(radec);
135 Vector3d v = pt.Ang2Vec(polar[0], polar[1]);
136 ArrayList pixlist;
137 pixlist = pt.query_disc(nside, v, radius, nest, inclusive);
138
139 int nlist = pixlist.size();
140 long pixel =pt.vect2pix_nest(nside,v);
141
142 System.out.println(" radius="+radius+" center pixel="+pixel);
143 for (int i = 0; i < nlist; i++) {
144 ipix = ((Long) pixlist.get(i)).longValue();
145 System.out.println("i="+i+" pixel="+ipix);
146 assertEquals("pixel = " + ipix, case1[i], ipix, 1e-10);
147 }
148 System.out.println("case 2");
149 radec = new double[]{351.04536,1.74210};
150 polar = pt.RaDecToPolar(radec);
151 v = pt.Ang2Vec(polar[0], polar[1]);
152 radius = Math.toRadians(1.932);
153
154 pixlist = pt.query_disc(nside, v, radius, nest, inclusive);
155
156 nlist = pixlist.size();
157 pixel =pt.vect2pix_nest(nside,v);
158 System.out.println(" radius="+radius+" center pixel="+pixel);
159 for (int i = 0; i < nlist; i++) {
160 ipix = ((Long) pixlist.get(i)).longValue();
161 System.out.println("i="+i+" pixel="+ipix);
162 assertEquals("pixel = " + ipix, case2[i], ipix, 1e-10);
163 }
164 System.out.println("case 3");
165 radius = Math.toRadians(10.68);
166 radec = new double[]{348.86174 ,-0.07390};
167 polar = pt.RaDecToPolar(radec);
168 v = pt.Ang2Vec(polar[0], polar[1]);
169 pixlist = pt.query_disc(nside, v, radius, nest, inclusive);
170
171 nlist = pixlist.size();
172 pixel =pt.vect2pix_nest(nside,v);
173 System.out.println(" radius="+radius+" center pixel="+pixel);
174 for (int i = 0; i < nlist; i++) {
175 ipix = ((Long) pixlist.get(i)).longValue();
176 System.out.println("i="+i+" pixel="+ipix);
177 assertEquals("pixel = " + ipix, case3[i], ipix, 1e-10);
178 }
179 //
180 System.out.println(" case 4");
181 radius = Math.toRadians(4.134);
182
183 radec = new double[]{182.95228, 89.43585};
184 polar = pt.RaDecToPolar(radec);
185 v = pt.Ang2Vec(polar[0], polar[1]);
186 pixlist = pt.query_disc(nside, v, radius, nest, inclusive);
187
188 nlist = pixlist.size();
189 pixel =pt.vect2pix_nest(nside,v);
190 System.out.println(" radius="+radius+" center pixel="+pixel);
191 for (int i = 0; i < nlist; i++) {
192 ipix = ((Long) pixlist.get(i)).longValue();
193 System.out.println("i="+i+" pixel="+ipix);
194 assertEquals("pixel = " + ipix, case4[i], ipix, 1e-10);
195 }
196
197
198 System.out.println(" End query_disk_1 test ______________________________");
199 }
200 }
0 package gov.fnal.eag.healpix.test;
1
2 import java.util.ArrayList;
3 import javax.vecmath.Vector3d;
4 import gov.fnal.eag.healpix.PixTools;
5 import gov.fnal.eag.healpix.Region;
6 import gov.fnal.eag.healpix.BitManipulation;
7 import junit.framework.TestCase;
8
9 /**
10 * @author kuropat
11 * Junit methods to test Region class
12 */
13 public class RegionTest extends TestCase {
14 private final static double TWOPI = 2.0 * Math.PI;
15 private final static double epsilon = 1.0e-10;
16 private BitManipulation bm = new BitManipulation();
17 /**
18 * test default constructor
19 */
20 public void testRegion() {
21 double xMin = 0.;
22 double xMax = 30.;
23 double yMin = 0.;
24 double yMax = 10.;
25 PixTools pt = new PixTools();
26 Region rg = new Region(xMin,xMax,yMin,yMax);
27 assertNotNull(rg);
28 double x = -10.;
29 double y = 15.;
30 assertFalse(rg.inReg(x,y));
31 x = 10.;
32 y = 5.;
33 assertTrue(rg.inReg(x,y));
34 xMax = -10.;
35 ArrayList vert = rg.getVertices();
36 double[][] vertPol = rg.getPolReg();
37 for ( int ind=0; ind<vert.size(); ind++) {
38 Vector3d vv = (Vector3d) vert.get(ind);
39 double [] vvAng = pt.Vect2Ang(vv);
40 if (vertPol[ind][1] < 0) vertPol[ind][1] += TWOPI;
41 double comp = bm.MODULO(vvAng[1], TWOPI) - epsilon;
42 assertEquals("theta ",vertPol[ind][0],vvAng[0], 1.0e-5);
43 assertEquals("phi ="+vertPol[ind][1],vertPol[ind][1],vvAng[1], 1.0e-5);
44 }
45 xMin = 20.;
46 xMax = 95.;
47 Region rg1 = new Region(xMin,xMax,yMin,yMax);
48 assertNotNull(rg1);
49 x = 45.;
50 y = 5.;
51 assertTrue(rg1.inReg(x,y));
52 }
53 /**
54 * test pixelization
55 *
56 */
57 public void testPixelize() {
58 System.out.println("test pixelize");
59 ArrayList pixels = new ArrayList();
60 double xMin = 10.;
61 double xMax = 60.;
62 double yMin = -20.0;
63 double yMax = 0.;
64 PixTools pt = new PixTools();
65 Region rg = new Region(xMin,xMax,yMin,yMax);
66 double[][] regCoord = rg.getPolReg();
67 for (int i = 0; i<regCoord.length; i++ ) {
68 System.out.println("thet="+regCoord[i][0]+" phi="+regCoord[i][1]);
69 }
70 double resolution = 10.*60.*60.; // resolution in arcsec (= 10 degrees)
71
72 try {
73 pixels = rg.pixelize(resolution);
74 long nside = pt.GetNSide(resolution);
75 int npix = pixels.size();
76 assertFalse(npix == 0);
77 System.out.println("npix="+npix);
78 for (int i=0; i<npix; i++) {
79 long pix = ((Long) pixels.get(i)).longValue();
80 System.out.println("pixel="+pix);
81 double[] pixpos = pt.pix2ang_ring(nside,pix);
82 System.out.println("theta="+pixpos[0]+" phi="+pixpos[1]);
83 double[] radec = pt.PolarToRaDec(pixpos);
84 double[][] pixvert = pt.pix2vertex_ring(nside,pix);
85 System.out.println("corners");
86 for (int j=0; j<pixvert[0].length; j++) {
87 double x = pixvert[0][j];
88 double y = pixvert[1][j];
89 double z = pixvert[2][j];
90 double[] pol = pt.xyzToPolar(x,y,z);
91 double[] radec1 = pt.PolarToRaDec(pol);
92 System.out.println("ra= "+radec1[0]+" dec="+radec1[1]);
93 }
94 System.out.println();
95
96 }
97
98 } catch (Exception e) {
99 System.err.println("Exception in pixelize");
100 e.printStackTrace();
101 }
102
103 }
104 }
0 package gov.fnal.eag.healpix.test;
1
2
3 import gov.fnal.eag.healpix.PixTools;
4 import java.util.List;
5 import java.util.Random;
6 import javax.vecmath.Vector3d;
7 /**
8 * The thread proof test by Mark Teylor
9 * @author Mark Taylor
10 *
11 */
12 public class ThreadTest {
13 private static PixTools pixTools = new PixTools();
14 public void testPixtools() throws InterruptedException {
15 workPixtools( false );
16 workPixtools( true );
17 }
18
19 private void workPixtools( boolean useThreads )
20 throws InterruptedException {
21 int nw = 40;
22 Worker[] workers = new Worker[ nw ];
23 Random rnd = new Random( 23001L );
24 int nside = 6 + rnd.nextInt( 3 );
25 Vector3d vector = new Vector3d( rnd.nextDouble(),
26 rnd.nextDouble(),
27 rnd.nextDouble() );
28 double radius = 0.1 * rnd.nextDouble();
29 int nest = 1;
30 int inclusive = 1;
31 for ( int iw = 0; iw < nw; iw++ ) {
32 workers[ iw ] =
33 new Worker( nside, vector, radius, nest, inclusive );
34 }
35 if ( useThreads ) {
36 for ( int iw = 0; iw < nw; iw++ ) {
37 workers[ iw ].start();
38 }
39 for ( int iw = 0; iw < nw; iw++ ) {
40 workers[ iw ].join();
41 }
42 }
43 else {
44 for ( int iw = 0; iw < nw; iw++ ) {
45 workers[ iw ].run();
46 }
47 }
48 List result0 = workers[ 0 ].result_;
49 for ( int iw = 1; iw < nw; iw++ ) {
50 assertEquals( "Fail at " + iw + " for useThreads=" + useThreads,
51 result0, workers[ iw ].result_ );
52 }
53 }
54
55 private static void assertEquals( String msg, List l1, List l2 ) {
56 if ( ! l1.equals( l2 ) ) {
57 throw new AssertionError( msg + "(" + l1 + " != " + l2 + ")" );
58 }
59 }
60
61 private static class Worker extends Thread {
62 private final long nside_;
63 private final Vector3d vector_;
64 private final double radius_;
65 private final int nest_;
66 private final int inclusive_;
67 private List result_;
68
69 Worker( long nside, Vector3d vector, double radius,
70 int nest, int inclusive ) {
71 nside_ = nside;
72 vector_ = vector;
73 radius_ = radius;
74 nest_ = nest;
75 inclusive_ = inclusive;
76 }
77
78 public void run() {
79
80 result_ = pixTools.query_disc( nside_, vector_, radius_, nest_, inclusive_ );
81 }
82 }
83
84 public static void main( String[] args ) throws InterruptedException {
85 new ThreadTest().testPixtools();
86 System.out.println(" if no interrupts the test is complete successfully");
87 }
88 }
89
90