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
Functions
Right Ascension and Declination Parsers

Routines for converting right ascension and declination from degrees or radians into strings and back again. More...

Functions

double radToDeg (long double angleInRadians)
 
double degToRad (long double angleInDegrees)
 
std::string lsst.utils::raRadToStr (double raRad)
 
std::string lsst.utils::raDegToStr (double raDeg)
 
std::string lsst.utils::decRadToStr (double decRad)
 
std::string lsst.utils::decDegToStr (double decDeg)
 
std::string lsst.utils::raDecRadToStr (double raRad, double decRad)
 
std::string lsst.utils::raDecDegToStr (double raDeg, double decDeg)
 
double lsst.utils::raStrToRad (std::string raStr, std::string delimiter=":")
 
double lsst.utils::raStrToDeg (std::string raStr, std::string delimiter=":")
 
double lsst.utils::decStrToRad (std::string decStr, std::string delimiter=":")
 
double lsst.utils::decStrToDeg (std::string decStr, std::string delimiter=":")
 

Detailed Description

Routines for converting right ascension and declination from degrees or radians into strings and back again.

Right ascensions and declinations (raDecs) are easiest read as strings in the form hh:mm:ss.ss +dd:mm::ss.s, but for calculations, they need to be in degrees or radians. These functions perform those calculations. The function names themselves use the following abbreviations

So, for example raStrToRad() converts a right ascension in the form of a string to radians.

The ouput strings are in fixed length Ra = hh:mm:ss.ss and Dec= +dd:mm::ss.s with all zeros present (not replaced with whitespace).

Input strings must be of a similar format, although some variation is allowed. The default delimiter (the colon) can be supplied as an optional argument

Function Documentation

string lsst::utils::decDegToStr ( double  decDeg)

Definition at line 102 of file RaDecStr.cc.

102  {
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 }
Extent< int, N > floor(Extent< double, N > const &input)
AngleUnit const degrees
Definition: Angle.h:92
string lsst::utils::decRadToStr ( double  decRad)

Definition at line 97 of file RaDecStr.cc.

97  {
98  return decDegToStr( radToDeg(decRad) );
99 }
double radToDeg(long double angleInRadians)
Definition: RaDecStr.cc:61
std::string decDegToStr(double decDeg)
Definition: RaDecStr.cc:102
double lsst::utils::decStrToDeg ( std::string  decStr,
std::string  delimiter = ":" 
)

Definition at line 195 of file RaDecStr.cc.

195  {
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 }
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
AngleUnit const degrees
Definition: Angle.h:92
double lsst::utils::decStrToRad ( std::string  decStr,
std::string  delimiter = ":" 
)

Definition at line 190 of file RaDecStr.cc.

190  {
191  return degToRad( decStrToDeg(decStr) );
192 }
double decStrToDeg(std::string decStr, std::string delimiter=":")
Definition: RaDecStr.cc:195
double degToRad(long double angleInDegrees)
Definition: RaDecStr.cc:67
double degToRad ( long double  angleInDegrees)

Definition at line 67 of file RaDecStr.cc.

67  {
68  const long double pi = 3.141592653589793115997963468544;
69  return angleInDegrees * pi / 180.;
70 }
string lsst::utils::raDecDegToStr ( double  raDeg,
double  decDeg 
)

Definition at line 134 of file RaDecStr.cc.

134  {
135  string val = raDegToStr(raDeg);
136  val = val + " "+decDegToStr(decDeg);
137  return val;
138 }
bool val
std::string raDegToStr(double raDeg)
Definition: RaDecStr.cc:79
std::string decDegToStr(double decDeg)
Definition: RaDecStr.cc:102
string lsst::utils::raDecRadToStr ( double  raRad,
double  decRad 
)

Definition at line 127 of file RaDecStr.cc.

127  {
128  string val = raRadToStr(raRad);
129  val = val + " "+decRadToStr(decRad);
130  return val;
131 }
std::string decRadToStr(double decRad)
Definition: RaDecStr.cc:97
bool val
std::string raRadToStr(double raRad)
Definition: RaDecStr.cc:75
string lsst::utils::raDegToStr ( double  raDeg)

Definition at line 79 of file RaDecStr.cc.

79  {
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 }
Extent< int, N > floor(Extent< double, N > const &input)
double radToDeg ( long double  angleInRadians)

Definition at line 61 of file RaDecStr.cc.

61  {
62  const long double pi = 3.141592653589793115997963468544;
63  return angleInRadians * 180./pi;
64 }
string lsst::utils::raRadToStr ( double  raRad)

Convert a right ascension in radians to a string format

Parameters
raRadRa in radians

Definition at line 75 of file RaDecStr.cc.

75  {
76  return raDegToStr( radToDeg(raRad) );
77 }
double radToDeg(long double angleInRadians)
Definition: RaDecStr.cc:61
std::string raDegToStr(double raDeg)
Definition: RaDecStr.cc:79
double lsst::utils::raStrToDeg ( std::string  raStr,
std::string  delimiter = ":" 
)

Definition at line 155 of file RaDecStr.cc.

155  {
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 }
AngleUnit const hours
Definition: Angle.h:93
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
double lsst::utils::raStrToRad ( std::string  raStr,
std::string  delimiter = ":" 
)

Definition at line 150 of file RaDecStr.cc.

150  {
151  return degToRad( raStrToDeg(raStr) );
152 }
double raStrToDeg(std::string raStr, std::string delimiter=":")
Definition: RaDecStr.cc:155
double degToRad(long double angleInDegrees)
Definition: RaDecStr.cc:67