LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
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 #include <string>
24 
25 #include "lsst/utils/RaDecStr.h"
26 
27 using namespace std;
28 namespace except = lsst::pex::exceptions;
29 
59 using namespace std;
60 namespace ut = lsst::utils;
61 
62 double radToDeg(long double angleInRadians) {
63  const long double pi = 3.141592653589793115997963468544;
64  return angleInRadians * 180./pi;
65 }
66 
67 
68 double degToRad(long double angleInDegrees) {
69  const long double pi = 3.141592653589793115997963468544;
70  return angleInDegrees * pi / 180.;
71 }
72 
73 
76 string ut::raRadToStr(double raRad ) {
77  return raDegToStr( radToDeg(raRad) );
78 }
79 
80 string ut::raDegToStr(double raDeg){
81 
82  double ra = raDeg; //Shorthand
83 
84  //Convert to seconds of arc
85  ra = round(ra*1e6)/1e6;
86  ra *= 86400/360;
87 
88  int hr = (int) floor(ra/3600.);
89  ra -= hr*3600;
90 
91  int mn = (int) floor(ra/60.);
92  ra -= mn*60; //Only seconds remain
93 
94  return str( boost::format("%02i:%02i:%05.2f") % hr % mn % ra);
95 }
96 
97 
98 string ut::decRadToStr(double decRad) {
99  return decDegToStr( radToDeg(decRad) );
100 }
101 
102 
103 string ut::decDegToStr(double decDeg) {
104 
105  double dec = decDeg; //Shorthand
106 
107  string sgn = (dec<0) ? "-" : "+";
108 
109  //Rounding the declination prevents 14.999999999 being represented
110  //as 14.:59:60.00
111  dec = fabs(round(dec*1e6)/1e6);
112 
113  int degrees = (int) floor(dec);
114  dec -= degrees;
115 
116  int min = (int) floor(dec*60);
117  dec -= min/60.;
118 
119  double sec = dec*3600;
120  sec = floor(sec*100)/100.;
121 
122  string str = sgn;
123  return boost::str(boost::format("%s%02i:%02i:%05.2f") % sgn % degrees % min % sec);
124 
125 }
126 
127 
128 string ut::raDecRadToStr(double raRad, double decRad) {
129  string val = raRadToStr(raRad);
130  val = val + " "+decRadToStr(decRad);
131  return val;
132 }
133 
134 
135 string ut::raDecDegToStr(double raDeg, double decDeg) {
136  string val = raDegToStr(raDeg);
137  val = val + " "+decDegToStr(decDeg);
138  return val;
139 }
140 
141 
142 
143 
144 
145 // ////////////////////////////////////////////////////////////////
146 
147 //
148 // Converting strings to numbers
149 //
150 
151 double ut::raStrToRad(std::string raStr, std::string delimiter) {
152  return degToRad( raStrToDeg(raStr) );
153 }
154 
155 
156 double ut::raStrToDeg(std::string raStr, std::string delimiter) {
157 
158  //Regex the hours, minutes and seconds
159  string regexStr = "(\\d+)";
160  regexStr.append(delimiter);
161  regexStr.append("(\\d+)");
162  regexStr.append(delimiter);
163  regexStr.append("([\\d\\.]+)");
164 
165  static const boost::regex re(regexStr);
166  boost::cmatch what;
167  //This throws an exception of failure. I could catch it,
168  //but I'd only throw it again
169  if(! boost::regex_match(raStr.c_str(), what, re)) {
170  string msg= boost::str(boost::format("Failed to parse %s as a declination") % raStr);
171  throw LSST_EXCEPT(except::RuntimeError, msg);
172  }
173 
174 
175  //Convert strings to doubles. Again, errors thrown on failure
176  double hours = std::stod(string(what[1].first, what[1].second));
177  double mins = std::stod(string(what[2].first, what[2].second));
178  double secs = std::stod(string(what[3].first, what[3].second));
179 
180  double raDeg = secs/3600.;
181  raDeg += mins/60.;
182  raDeg += hours;
183  raDeg *= 360./24.;
184 
185  //printf("%g %g %g --> %g\n", hours, mins, secs, raDeg);
186  return raDeg;
187 
188 }
189 
190 
191 double ut::decStrToRad(std::string decStr, std::string delimiter) {
192  return degToRad( decStrToDeg(decStr) );
193 }
194 
195 
196 double ut::decStrToDeg(std::string decStr, std::string delimiter) {
197 
198  //Regex the degrees, minutes and seconds
199  string regexStr = "([\\d]+)";
200  regexStr.append(delimiter);
201  regexStr.append("(\\d+)");
202  regexStr.append(delimiter);
203  regexStr.append("([\\d\\.]+)");
204 
205  //http://www.boost.org/doc/libs/1_40_0/libs/regex/doc/html/boost_regex/captures.html
206  static const boost::regex re(regexStr);
207  boost::cmatch what;
208 
209  if(! boost::regex_search(decStr.c_str(), what, re)) {
210  string msg= boost::str(boost::format("Failed to parse %s as a declination") % decStr);
211  throw LSST_EXCEPT(except::RuntimeError, msg);
212  }
213 
214  //Convert strings to doubles. Automatically pass the exception up the stack
215  double degrees = std::stod(string(what[1].first, what[1].second));
216  double mins = std::stod(string(what[2].first, what[2].second));
217  double secs = std::stod(string(what[3].first, what[3].second));
218 
219  degrees += ((secs/60.) +mins)/60.;
220 
221  //Search for the presence of a minus sign. This approach catches the case of -0 degrees
222  string pmStr = "^-";
223  static const boost::regex pm(pmStr);
224  if(boost::regex_search(decStr, pm)) {
225  degrees *= -1;
226  }
227 
228  return degrees;
229 }
230 
double decStrToDeg(std::string decStr, std::string delimiter=":")
Definition: RaDecStr.cc:196
AngleUnit const hours
Definition: Angle.h:92
std::string decRadToStr(double decRad)
Definition: RaDecStr.cc:98
double decStrToRad(std::string decStr, std::string delimiter=":")
Definition: RaDecStr.cc:191
double raStrToDeg(std::string raStr, std::string delimiter=":")
Definition: RaDecStr.cc:156
double raStrToRad(std::string raStr, std::string delimiter=":")
Definition: RaDecStr.cc:151
bool val
double radToDeg(long double angleInRadians)
Definition: RaDecStr.cc:62
std::string raRadToStr(double raRad)
Definition: RaDecStr.cc:76
double degToRad(long double angleInDegrees)
Definition: RaDecStr.cc:68
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
std::string raDecDegToStr(double raDeg, double decDeg)
Definition: RaDecStr.cc:135
Extent< int, N > floor(Extent< double, N > const &input)
std::string raDegToStr(double raDeg)
Definition: RaDecStr.cc:80
AngleUnit const degrees
Definition: Angle.h:91
std::string decDegToStr(double decDeg)
Definition: RaDecStr.cc:103
std::string raDecRadToStr(double raRad, double decRad)
Definition: RaDecStr.cc:128