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 define( ['./CoordinateSystem', './Numeric'], function(CoordinateSystem, Numeric) {
 21 
 22 /**************************************************************************************************************/
 23 
 24 /**
 25 *	Convert a 3D position to equatorial coordinates
 26 */
 27 CoordinateSystem.from3DToEquatorial = function(position3d, dest){
 28 	
 29 	if (!dest) { dest = new Array(3); }
 30 	
 31 	var geo = [];
 32 
 33 	CoordinateSystem.from3DToGeo(position3d, geo);
 34 	CoordinateSystem.fromGeoToEquatorial(geo, dest);
 35 	
 36 	return dest;
 37 }
 38 
 39 /**************************************************************************************************************/
 40 
 41 /**
 42 *	Converts an equatorial position to 3D
 43 */
 44 CoordinateSystem.fromEquatorialTo3D = function(equatorial, dest){
 45 	
 46 	if (!dest) { dest = new Array(3); }
 47 
 48 	var geo = [];
 49 	
 50 	CoordinateSystem.fromEquatorialToGeo(equatorial, geo);
 51 	CoordinateSystem.fromGeoTo3D(geo,dest);
 52 	
 53 	return dest;	
 54 }
 55 
 56 /**************************************************************************************************************/
 57 
 58 /**
 59 *	Convert an equatorial position to geographic
 60 *	@param {String[]} equatorial Array of two strings corresponding to Right Ascension and Declination
 61 *					  specified by: "hours minuts seconds" and "degrees minuts seconds" respectively
 62 *	@param {Float[]} dest Destination array of two floats corresponding to Longitude and Latitude
 63 */
 64 CoordinateSystem.fromEquatorialToGeo = function(equatorial, dest){
 65 	
 66 	if(!dest) dest = [];
 67 	
 68 	// we use string because : parseFloat("-0") returns 0..
 69 	function sign(stringDegree){
 70 		return ((stringDegree[0] == "-") ? -1 : 1);
 71 	}
 72 
 73 	var RA = equatorial[0].split(" ");
 74 	// long
 75 	var deg = parseFloat(RA[0]);
 76 	var min = parseFloat(RA[1]);
 77 	var sec = parseFloat(RA[2]);
 78 	
 79 	dest[0] = (deg + min/60 + sec/3600) * 15.;
 80 	if(dest[0] > 180)
 81 		dest[0] -= 360;
 82 	
 83 	var Decl = equatorial[1].split(" ");
 84 	// lat
 85 	deg = parseFloat(Decl[0]);
 86 	min = parseFloat(Decl[1]);
 87 	sec = parseFloat(Decl[2]);
 88 	
 89 	dest[1] = sign(Decl[0]) * (Math.abs(deg) + min/60 + sec/3600);
 90 	
 91 	return dest;
 92 
 93 }
 94 
 95 /**************************************************************************************************************/
 96 
 97 /**
 98 *	Convert a geographic position to equatorial
 99 *	@param {Float[]} geo Array of two floats corresponding to Longitude and Latitude
100 *	@param {String[]} dest Destination array of two strings corresponding to Right Ascension and Declination
101 *					  specified by: 'hours+"h" minuts+"m" seconds+"s"' and 'degrees+"�" minuts+"\'" seconds+"""' respectively
102 * 	@see <CoordinateSystem.fromDegreesToDMS>
103 * * 	@see <CoordinateSystem.fromDegreesToHMS>
104 */
105 CoordinateSystem.fromGeoToEquatorial = function(geo, dest){
106 	
107 	if (!dest) dest = [];
108 	
109 	var deg = geo[0];
110 	// RA
111 	if(deg < 0){
112 		deg += 360;
113 	}
114 
115 	dest[0] = CoordinateSystem.fromDegreesToHMS( deg );
116 	dest[1] = CoordinateSystem.fromDegreesToDMS(geo[1]);
117 	
118 	return dest;
119 }
120 
121 /**************************************************************************************************************/
122 
123 /**
124  *	Function converting degrees to DMS("degrees minuts seconds")
125  * 
126  *	@param {Float} degree The degree
127  */
128 
129 CoordinateSystem.fromDegreesToDMS = function(degree)
130 {
131 	function stringSign(val)
132 	{
133 		return (val>=0 ? "": "-");
134 	}
135 	
136 	var absLat = Math.abs(degree);
137 	deg = Math.floor(absLat);
138 	decimal = (absLat - deg) * 60;
139 	min = Math.floor(decimal);
140 	sec = (decimal - min) * 60;
141 	
142 	return stringSign(degree) + deg + String.fromCharCode(176) +" "+ min +"' "+ Numeric.roundNumber(sec, 2)+"\"";
143 	
144 }
145 
146 /**
147  *	Function converting degrees to HMS("hours minuts seconds")
148  *
149  *	@param {Float} degree The degree > 0
150  */
151 
152 CoordinateSystem.fromDegreesToHMS = function(degree)
153 {
154 	var degree = degree/15;
155 	
156 	var absLon = Math.abs(degree);
157 	var hours = Math.floor(absLon);
158 	var decimal = (absLon - hours) * 60;
159 	var min = Math.floor(decimal);
160 	var sec = (decimal - min) * 60;
161 	
162 	return hours+"h "+min+"m "+ Numeric.roundNumber(sec, 2) +"s";
163 }
164 
165 });
166