LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
RaDecStr.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008, 2009, 2010 LSST Corporation.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <http://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 
24 #include "lsst/utils/RaDecStr.h"
25 
26 using namespace std;
27 namespace except = lsst::pex::exceptions;
28 
58 using namespace std;
59 namespace ut = lsst::utils;
60 
61 double radToDeg(long double angleInRadians) {
62  const long double pi = 3.141592653589793115997963468544;
63  return angleInRadians * 180./pi;
64 }
65 
66 
67 double degToRad(long double angleInDegrees) {
68  const long double pi = 3.141592653589793115997963468544;
69  return angleInDegrees * pi / 180.;
70 }
71 
72 
75 string ut::raRadToStr(double raRad ) {
76  return raDegToStr( radToDeg(raRad) );
77 }
78 
79 string ut::raDegToStr(double raDeg){
80 
81  double ra = raDeg; //Shorthand
82 
83  //Convert to seconds of arc
84  ra = round(ra*1e6)/1e6;
85  ra *= 86400/360;
86 
87  int hr = (int) floor(ra/3600.);
88  ra -= hr*3600;
89 
90  int mn = (int) floor(ra/60.);
91  ra -= mn*60; //Only seconds remain
92 
93  return str( boost::format("%02i:%02i:%05.2f") % hr % mn % ra);
94 }
95 
96 
97 string ut::decRadToStr(double decRad) {
98  return decDegToStr( radToDeg(decRad) );
99 }
100 
101 
102 string ut::decDegToStr(double decDeg) {
103 
104  double dec = decDeg; //Shorthand
105 
106  string sgn = (dec<0) ? "-" : "+";
107 
108  //Rounding the declination prevents 14.999999999 being represented
109  //as 14.:59:60.00
110  dec = fabs(round(dec*1e6)/1e6);
111 
112  int degrees = (int) floor(dec);
113  dec -= degrees;
114 
115  int min = (int) floor(dec*60);
116  dec -= min/60.;
117 
118  double sec = dec*3600;
119  sec = floor(sec*100)/100.;
120 
121  string str = sgn;
122  return boost::str(boost::format("%s%02i:%02i:%05.2f") % sgn % degrees % min % sec);
123 
124 }
125 
126 
127 string ut::raDecRadToStr(double raRad, double decRad) {
128  string val = raRadToStr(raRad);
129  val = val + " "+decRadToStr(decRad);
130  return val;
131 }
132 
133 
134 string ut::raDecDegToStr(double raDeg, double decDeg) {
135  string val = raDegToStr(raDeg);
136  val = val + " "+decDegToStr(decDeg);
137  return val;
138 }
139 
140 
141 
142 
143 
144 // ////////////////////////////////////////////////////////////////
145 
146 //
147 // Converting strings to numbers
148 //
149 
150 double ut::raStrToRad(std::string raStr, std::string delimiter) {
151  return degToRad( raStrToDeg(raStr) );
152 }
153 
154 
155 double ut::raStrToDeg(std::string raStr, std::string delimiter) {
156 
157  //Regex the hours, minutes and seconds
158  string regexStr = "(\\d+)";
159  regexStr.append(delimiter);
160  regexStr.append("(\\d+)");
161  regexStr.append(delimiter);
162  regexStr.append("([\\d\\.]+)");
163 
164  static const boost::regex re(regexStr);
165  boost::cmatch what;
166  //This throws an exception of failure. I could catch it,
167  //but I'd only throw it again
168  if(! boost::regex_match(raStr.c_str(), what, re)) {
169  string msg= boost::str(boost::format("Failed to parse %s as a declination") % raStr);
170  throw LSST_EXCEPT(except::RuntimeError, msg);
171  }
172 
173 
174  //Convert strings to doubles. Again, errors thrown on failure
175  double hours = boost::lexical_cast<double>(string(what[1].first, what[1].second));
176  double mins = boost::lexical_cast<double>(string(what[2].first, what[2].second));
177  double secs = boost::lexical_cast<double>(string(what[3].first, what[3].second));
178 
179  double raDeg = secs/3600.;
180  raDeg += mins/60.;
181  raDeg += hours;
182  raDeg *= 360./24.;
183 
184  //printf("%g %g %g --> %g\n", hours, mins, secs, raDeg);
185  return raDeg;
186 
187 }
188 
189 
190 double ut::decStrToRad(std::string decStr, std::string delimiter) {
191  return degToRad( decStrToDeg(decStr) );
192 }
193 
194 
195 double ut::decStrToDeg(std::string decStr, std::string delimiter) {
196 
197  //Regex the degrees, minutes and seconds
198  string regexStr = "([\\d]+)";
199  regexStr.append(delimiter);
200  regexStr.append("(\\d+)");
201  regexStr.append(delimiter);
202  regexStr.append("([\\d\\.]+)");
203 
204  //http://www.boost.org/doc/libs/1_40_0/libs/regex/doc/html/boost_regex/captures.html
205  static const boost::regex re(regexStr);
206  boost::cmatch what;
207 
208  if(! boost::regex_search(decStr.c_str(), what, re)) {
209  string msg= boost::str(boost::format("Failed to parse %s as a declination") % decStr);
210  throw LSST_EXCEPT(except::RuntimeError, msg);
211  }
212 
213  //Convert strings to doubles. Automatically pass the exception up the stack
214  double degrees = boost::lexical_cast<double>(string(what[1].first, what[1].second));
215  double mins = boost::lexical_cast<double>(string(what[2].first, what[2].second));
216  double secs = boost::lexical_cast<double>(string(what[3].first, what[3].second));
217 
218  degrees += ((secs/60.) +mins)/60.;
219 
220  //Search for the presence of a minus sign. This approach catches the case of -0 degrees
221  string pmStr = "^-";
222  static const boost::regex pm(pmStr);
223  if(boost::regex_search(decStr, pm)) {
224  degrees *= -1;
225  }
226 
227  return degrees;
228 }
229 
double decStrToDeg(std::string decStr, std::string delimiter=":")
Definition: RaDecStr.cc:195
AngleUnit const hours
Definition: Angle.h:93
std::string decRadToStr(double decRad)
Definition: RaDecStr.cc:97
double decStrToRad(std::string decStr, std::string delimiter=":")
Definition: RaDecStr.cc:190
double raStrToDeg(std::string raStr, std::string delimiter=":")
Definition: RaDecStr.cc:155
double raStrToRad(std::string raStr, std::string delimiter=":")
Definition: RaDecStr.cc:150
bool val
double radToDeg(long double angleInRadians)
Definition: RaDecStr.cc:61
std::string raRadToStr(double raRad)
Definition: RaDecStr.cc:75
double degToRad(long double angleInDegrees)
Definition: RaDecStr.cc:67
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
std::string raDecDegToStr(double raDeg, double decDeg)
Definition: RaDecStr.cc:134
Extent< int, N > floor(Extent< double, N > const &input)
std::string raDegToStr(double raDeg)
Definition: RaDecStr.cc:79
AngleUnit const degrees
Definition: Angle.h:92
std::string decDegToStr(double decDeg)
Definition: RaDecStr.cc:102
std::string raDecRadToStr(double raRad, double decRad)
Definition: RaDecStr.cc:127