1 /***************************************
  2  * Copyright 2011, 2012 GlobWeb contributors.
  3  *
  4  * This file is part of GlobWeb.
  5  *
  6  * GlobWeb is free software: you can redistribute it and/or modify
  7  * it under the terms of the GNU Lesser General Public License as published by
  8  * the Free Software Foundation, version 3 of the License, or
  9  * (at your option) any later version.
 10  *
 11  * GlobWeb is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14  * GNU Lesser General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU General Public License
 17  * along with GlobWeb. If not, see <http://www.gnu.org/licenses/>.
 18  ***************************************/
 19 /***************************************
 20  * Copyright 2011, 2012 GlobWeb contributors.
 21  *
 22  * This file is part of GlobWeb.
 23  *  Ported from HEALPix Java code supported by the Gaia project.
 24  * 	Copyright (C) 2006-2011 Gaia Data Processing and Analysis Consortium
 25  *
 26  * GlobWeb is free software: you can redistribute it and/or modify
 27  * it under the terms of the GNU Lesser General Public License as published by
 28  * the Free Software Foundation, version 3 of the License, or
 29  * (at your option) any later version.
 30  *
 31  * GlobWeb is distributed in the hope that it will be useful,
 32  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 33  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 34  * GNU Lesser General Public License for more details.
 35  *
 36  * You should have received a copy of the GNU General Public License
 37  * along with GlobWeb. If not, see <http://www.gnu.org/licenses/>.
 38  ***************************************/
 39 
 40 define(function() {
 41 
 42 /**************************************************************************************************************/
 43 
 44 /** The Constant twoPi. */
 45 var twoPi=2.*Math.PI;
 46 
 47 /** The Constant fourPi. */
 48 var fourPi=4.*Math.PI;
 49 
 50 /** The Constant degToRad. */
 51 var degToRad=180.0/Math.PI;
 52 
 53 /** The Constant psi. */
 54 var psi = [
 55 	[0.57595865315,4.92619181360,0.00000000000,0.00000000000,0.11129056012,4.70053728340],
 56 	[0.57477043300,4.93682924650,0.00000000000,0.00000000000,0.11142137093,4.71279419371]
 57 ];
 58 
 59 /** The Constant stheta. */
 60 var stheta = [
 61 	[0.88781538514,-0.88781538514, 0.39788119938,-0.39788119938, 0.86766174755,-0.86766174755],
 62 	[0.88998808748,-0.88998808748, 0.39777715593,-0.39777715593, 0.86766622025,-0.86766622025]
 63 ];
 64 
 65 /** The Constant ctheta. */
 66 var ctheta = [
 67 	[0.46019978478,0.46019978478,0.91743694670,0.91743694670,0.49715499774,0.49715499774],
 68 	[0.45598377618,0.45598377618,0.91748206207,0.91748206207,0.49714719172,0.49714719172]
 69 ];
 70 
 71 /** The Constant phi. */
 72 var phi = [
 73 	[4.92619181360,0.57595865315,0.00000000000,0.00000000000,4.70053728340,0.11129056012],
 74 	[4.93682924650,0.57477043300,0.00000000000,0.00000000000,4.71279419371,0.11142137093]
 75 ];
 76 
 77 var AstroCoordTransform = {
 78 
 79 	/**Transforms an angular position in radians in a given coordinate system to a position
 80 	   in an other coordinate system, also in radians. RA-Dec position are intended in 
 81 	   Equinox J2000
 82 	   
 83 	   @param {Float[]} pos Angular position [phi, theta]
 84 	   @param trType Transform type
 85 	 */
 86     transform: function(pos, trType) 
 87 	{
 88 		var ao,bo,a,b,sb,cb,cbsa;
 89 		var J2000 = 1;
 90 		//by setting J2000 = 0, RA-Dec are intended in Equinox 1950.
 91 
 92 		a= pos[0] - phi[J2000][trType];
 93 		b= pos[1];
 94 		sb=Math.sin(b);
 95 		cb=Math.cos(b);
 96 		cbsa=cb*Math.sin(a);
 97 		b=-stheta[J2000][trType] * cbsa + ctheta[J2000][trType]*sb;
 98 		b=Math.max(-1.0,Math.min(b,1.0));
 99 		bo=Math.asin(b);
100 		
101 		a=Math.atan2(ctheta[J2000][trType] * cbsa+ stheta[J2000][trType]*sb,cb*Math.cos(a));
102 		ao=(a+psi[J2000][trType]+fourPi)%twoPi;
103 
104 		return [ao, bo]; // phi, theta
105     },
106 
107     /**Transforms an angular position in degrees in a given coordinate system to a position
108        in an other coordinate systems, also in degrees. RA-Dec position are intended in 
109        Equinox J2000
110 
111        @param {Float[]} pos Angular position [phi, theta]
112        @param trType Transform type
113        */
114     transformInDeg: function(pos, trType) 
115 	{
116 		var ao,bo,a,b,sb,cb,cbsa;
117 		var J2000 = 1;
118 		//by setting J2000 = 0, RA-Dec are intended in Equinox 1950.
119 
120 		a= pos[0]/degToRad - phi[J2000][trType];
121 		b= pos[1]/degToRad;
122 		sb=Math.sin(b);
123 		cb=Math.cos(b);
124 		cbsa=cb*Math.sin(a);
125 		b=-stheta[J2000][trType] * cbsa + ctheta[J2000][trType]*sb;
126 		b=Math.max(-1.0,Math.min(b,1.0));
127 		bo=Math.asin(b)*degToRad;
128 		
129 		a=Math.atan2(ctheta[J2000][trType] * cbsa+ stheta[J2000][trType]*sb,cb*Math.cos(a));
130 		ao= ((a+psi[J2000][trType]+fourPi)%twoPi)*degToRad;
131 
132 		return [ao, bo];		      
133     }
134 };
135 
136 /**
137  *	Transform type enumerations
138  */
139 AstroCoordTransform.Type = 
140 {
141 	EQ2GAL: 0,		//RA-Dec (2000) -> Galactic
142 	GAL2EQ: 1,		//Galactic      -> RA-Dec
143 	EQ2ECL: 2,		//RA-Dec        -> Ecliptic
144 	ECL2EQ: 3,		//Ecliptic      -> RA-Dec
145 	ECL2GAL: 4,		//Ecliptic      -> Galactic
146 	GAL2ECL: 5 		//Galactic      -> Ecliptic
147 };
148 
149 /**************************************************************************************************************/
150 
151 return AstroCoordTransform;
152 
153 });
154