| 1 |
package gov.fnal.eag.healpix.test; |
| 2 |
import java.util.Collection; |
| 3 |
import java.util.Iterator; |
| 4 |
import java.util.Random; |
| 5 |
import java.util.Set; |
| 6 |
import java.util.TreeSet; |
| 7 |
|
| 8 |
import javax.vecmath.Vector3d; |
| 9 |
|
| 10 |
import gov.fnal.eag.healpix.PixTools; |
| 11 |
|
| 12 |
public class RingTest { |
| 13 |
private int nCheck; |
| 14 |
private int nFail; |
| 15 |
|
| 16 |
|
| 17 |
public Collection queryRing( int order, double ra, double dec, |
| 18 |
double radius ) { |
| 19 |
PixTools pixTools = PixTools.getInstance(); |
| 20 |
long nside = orderToNside( order ); |
| 21 |
double[] radec = new double[]{ra,dec}; |
| 22 |
double[] polar = pixTools.RaDecToPolar(radec); |
| 23 |
// double theta = Math.PI / 2. - Math.toRadians(dec); |
| 24 |
// double phi = Math.toRadians(ra); |
| 25 |
Vector3d vec = pixTools.Ang2Vec( polar[0], polar[1] ); |
| 26 |
return pixTools.query_ring( nside, vec, radius, 0, 0 ); |
| 27 |
} |
| 28 |
|
| 29 |
public String report() { |
| 30 |
return "Checks: " + nCheck + "; " |
| 31 |
+ "Failures: " + nFail; |
| 32 |
} |
| 33 |
|
| 34 |
static long orderToNside( int order ) { |
| 35 |
return 1L << order; |
| 36 |
} |
| 37 |
|
| 38 |
static Collection intersection( Collection c1, Collection c2 ) { |
| 39 |
// System.out.println("n pixels 1="+c1.size()+" 2="+c2.size()); |
| 40 |
Set s = new TreeSet( c1 ); |
| 41 |
s.retainAll( c2 ); |
| 42 |
return s; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Haversine formula for spherical trigonometry. |
| 47 |
* This does not have the numerical instabilities of the cosine formula |
| 48 |
* at small angles. |
| 49 |
* <p> |
| 50 |
* This implementation derives from Bob Chamberlain's contribution |
| 51 |
* to the comp.infosystems.gis FAQ; he cites |
| 52 |
* R.W.Sinnott, "Virtues of the Haversine", Sky and Telescope vol.68, |
| 53 |
* no.2, 1984, p159. |
| 54 |
* |
| 55 |
* @param ra1 right ascension of point 1 in radians |
| 56 |
* @param dec1 declination of point 1 in radians |
| 57 |
* @param ra2 right ascension of point 2 in radians |
| 58 |
* @param dec2 declination of point 2 in radians |
| 59 |
* @return angular separation of point 1 and point 2 in radians |
| 60 |
* @see <http://www.census.gov/geo/www/gis-faq.txt> |
| 61 |
*/ |
| 62 |
static double haversineSeparationFormula( double ra1, double dec1, |
| 63 |
double ra2, double dec2 ) { |
| 64 |
|
| 65 |
double sd2 = Math.sin( 0.5 * ( dec2 - dec1 ) ); |
| 66 |
double sr2 = Math.sin( 0.5 * ( ra2 - ra1 ) ); |
| 67 |
double a = sd2 * sd2 + |
| 68 |
sr2 * sr2 * Math.cos( dec1 ) * Math.cos( dec2 ); |
| 69 |
if ( Double.isNaN( a ) ) { |
| 70 |
return Double.NaN; |
| 71 |
} |
| 72 |
return a < 1.0 ? 2.0 * Math.asin( Math.sqrt( a ) ) |
| 73 |
: Math.PI; |
| 74 |
} |
| 75 |
/** |
| 76 |
* |
| 77 |
* @param order |
| 78 |
* @param raDeg1 in degrees |
| 79 |
* @param decDeg1 |
| 80 |
* @param raDeg2 |
| 81 |
* @param decDeg2 |
| 82 |
* @throws Exception |
| 83 |
*/ |
| 84 |
public void checkRing( int order, double raDeg1, double decDeg1, |
| 85 |
double raDeg2, double decDeg2 ) throws Exception { |
| 86 |
double ra1 = raDeg1; |
| 87 |
double dec1 = decDeg1 ; |
| 88 |
double ra2 = raDeg2; |
| 89 |
double dec2 = decDeg2; |
| 90 |
double[] radec1 = {raDeg1,decDeg1}; |
| 91 |
double[] polar1 = new double[2]; |
| 92 |
double[] radec2 = {raDeg2,decDeg2}; |
| 93 |
double[] polar2 = new double[2]; |
| 94 |
long nside = orderToNside(order); |
| 95 |
// double pixSize = Math.PI / (2.0 * nside); // in radians |
| 96 |
long npix = 12*nside*nside; |
| 97 |
double pixSize = Math.sqrt(4.0*Math.PI/npix); |
| 98 |
System.out.println("pixSize="+pixSize+" nside="+nside); |
| 99 |
//System.out.println("ra1="+ra1+" dec1="+dec1+" ra2="+ra2+" dec2="+dec2); |
| 100 |
PixTools pixTools = PixTools.getInstance(); |
| 101 |
polar1 = pixTools.RaDecToPolar(radec1); |
| 102 |
polar2 = pixTools.RaDecToPolar(radec2); |
| 103 |
|
| 104 |
//System.out.println("thet1="+polar1[0]+" phi1="+polar1[1]+" thet2="+polar2[0]+" phi2="+polar2[1]); |
| 105 |
Vector3d v1 = pixTools.Ang2Vec(polar1[0], polar1[1]); // center of 1 |
| 106 |
Vector3d v2 = pixTools.Ang2Vec(polar2[0], polar2[1]); // center of 2 |
| 107 |
|
| 108 |
// Distance between the two points. |
| 109 |
double separation = haversineSeparationFormula( ra1, dec1, ra2, dec2 ); |
| 110 |
double separation2 = pixTools.AngDist(v1, v2); |
| 111 |
System.out.println("separation1="+separation+" separation2="+separation2); |
| 112 |
// Pick disc radius to be slightly larger than half the distance. |
| 113 |
// The discs must therefore overlap, which means that the |
| 114 |
// returned pixel lists must have at least one pixel in common. |
| 115 |
double radius = separation * 0.5001; |
| 116 |
double radius2 = separation2 * 0.5001; |
| 117 |
radius = 20.0*pixSize; |
| 118 |
System.out.println("radius="+radius+" radius2="+radius2); |
| 119 |
Collection disc1 = queryRing( order, ra1, dec1, radius ); |
| 120 |
// Collection disc2 = queryRing( order, ra2, dec2, radius ); |
| 121 |
|
| 122 |
Vector3d vP = new Vector3d(0.,0.,0.); |
| 123 |
long cp = pixTools.vect2pix_ring(nside, v1); |
| 124 |
v1 = pixTools.pix2vect_ring(nside, cp); |
| 125 |
polar1 = pixTools.Vect2Ang(v1); |
| 126 |
radec1 = pixTools.PolarToRaDec(polar1); |
| 127 |
disc1 = queryRing( order, radec1[0], radec1[1], radius ); |
| 128 |
Iterator it = disc1.iterator(); |
| 129 |
long pix = 0; |
| 130 |
System.out.println("number of pixels in ring "+disc1.size()); |
| 131 |
while (it.hasNext()) { |
| 132 |
pix = ((Long)it.next()).longValue(); |
| 133 |
// polar1 = pixTools.pix2ang_ring(nside, pix); |
| 134 |
// vP = pixTools.Ang2Vec(polar1[0], polar1[1]); |
| 135 |
vP=pixTools.pix2vect_ring(nside, pix); |
| 136 |
separation = pixTools.AngDist(v1, vP); |
| 137 |
System.out.println("separation="+separation); |
| 138 |
} |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
public static void main( String[] args ) throws Exception { |
| 143 |
RingTest test = new RingTest(); |
| 144 |
Random rnd = new Random( 2301L ); |
| 145 |
double scale = 0.1; // degree |
| 146 |
// for ( int i = 0; i < 1; i++ ) { |
| 147 |
// double ra1 = rnd.nextDouble() * 360; |
| 148 |
// double dec1 = rnd.nextDouble() * 180 - 90; |
| 149 |
// double ra2 = Math.min( ra1 + rnd.nextDouble() * scale, 360 ); |
| 150 |
// double dec2 = Math.max( Math.min( dec1 + rnd.nextDouble() * scale, |
| 151 |
// 90 ), -90 ); |
| 152 |
// for ( int order = 5; order < 12; order++ ) { |
| 153 |
// test.checkRing( order, ra1, dec1, ra2, dec2 ); |
| 154 |
// } |
| 155 |
// } |
| 156 |
// test.checkRing( 8, 0., -89.983888, 180., -89.983888 ); |
| 157 |
test.checkRing( 10, 10.0, 0.0, 180., 0.1 ); |
| 158 |
|
| 159 |
System.out.println( test.report() ); |
| 160 |
} |
| 161 |
|
| 162 |
} |