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
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 103 of file RaDecStr.cc.

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

Definition at line 98 of file RaDecStr.cc.

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

Definition at line 196 of file RaDecStr.cc.

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

Definition at line 191 of file RaDecStr.cc.

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

Definition at line 68 of file RaDecStr.cc.

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

Definition at line 135 of file RaDecStr.cc.

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

Definition at line 128 of file RaDecStr.cc.

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

Definition at line 80 of file RaDecStr.cc.

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

Definition at line 62 of file RaDecStr.cc.

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

Convert a right ascension in radians to a string format

Parameters
raRadRa in radians

Definition at line 76 of file RaDecStr.cc.

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

Definition at line 156 of file RaDecStr.cc.

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

Definition at line 151 of file RaDecStr.cc.

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