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