LSSTApplications  16.0-1-gce273f5+12,16.0-1-gf77f410+3,16.0-10-g230e10e+5,16.0-10-g90ce0e4+3,16.0-10-gc1446dd+3,16.0-12-g726f8f3+1,16.0-13-g4c33ca5+3,16.0-13-g5f6c0b1+3,16.0-13-gd9b1b71+3,16.0-13-gde155d7+11,16.0-16-g925333c+3,16.0-17-g6a7bfb3b+3,16.0-17-gd75be2c9,16.0-2-g0febb12+11,16.0-2-g839ba83+41,16.0-2-g9d5294e+30,16.0-3-g404ea43+3,16.0-3-gbc759ec+1,16.0-3-gcfd6c53+28,16.0-4-g03cf288+19,16.0-4-g13a27c5+5,16.0-4-g2cc461c+2,16.0-4-g5f3a788+10,16.0-4-g8a0f11a+25,16.0-4-ga3eb747,16.0-42-gaa8aebfeb+1,16.0-5-g1991253+3,16.0-5-g865efd9+3,16.0-5-gb3f8a4b+35,16.0-5-gd0f1235,16.0-6-g0a6c279+4,16.0-6-g316b399+3,16.0-6-gf0acd13+22,16.0-6-gf9cb114+4,16.0-7-g88875c5,16.0-7-gc370964+2,16.0-8-g4dec96c+16,16.0-8-gb99f628,16.0-9-gcc4efb7+15,w.2018.38
LSSTDataManagementBasePackage
Coord.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 /*
26  * Provide functions to handle coordinates
27  *
28  * Most (nearly all) algorithms adapted from Astronomical Algorithms, 2nd ed. (J. Meeus)
29  *
30  */
31 #include <cmath>
32 #include <cstdio>
33 
34 #include "lsst/pex/exceptions.h"
35 #include "boost/algorithm/string.hpp"
36 #include "boost/format.hpp"
37 
38 #include "lsst/afw/coord/Coord.h"
39 
40 namespace ex = lsst::pex::exceptions;
41 
42 namespace lsst {
43 namespace afw {
44 namespace coord {
45 
46 namespace {
47 
48 /*
49  * A local class to handle dd:mm:ss coordinates
50  *
51  * This class allows a decimal or dd:mm:ss coordinate to be
52  * disassembed into d, m, and s components.
53  * It's in an anonymous namespace, but there are public functions
54  * which perform the transformations directly:
55  *
56  * --> std::string dmsStr = degreesToDmsString(double deg);
57  * --> double deg = dmsStringToDegrees(std::string dms);
58  */
59 class Dms {
60 public:
61  Dms(){};
62 
63  // note that isSouth is needed to specify coords between dec = 0, and dec = -1
64  // otherwise, d = -0 gets carried as d = 0 ... need a way to specify it explicitly
65  Dms(int const d, int const m, double const s, bool const isSouth = false) {
66  sign = (d < 0 || isSouth) ? -1 : 1;
67  deg = std::abs(d);
68  min = m;
69  sec = s;
70  };
71  // unit could be "degrees" or "hours"
72  Dms(lsst::geom::Angle const deg00, lsst::geom::AngleUnit const unit = geom::degrees) {
73  double deg0 = deg00.asAngularUnits(unit);
74  double const absVal = std::fabs(deg0);
75  sign = (deg0 >= 0) ? 1 : -1;
76  deg = static_cast<int>(std::floor(absVal));
77  min = static_cast<int>(std::floor((absVal - deg) * 60.0));
78  sec = ((absVal - deg) * 60.0 - min) * 60.0;
79  }
80 
81  int deg;
82  int min;
83  double sec;
84  int sign;
85 };
86 
87 } // end anonymous namespace
88 
89 /* ******************* Public functions ******************* */
90 
91 static std::string angleToXmsString(lsst::geom::Angle const a, lsst::geom::AngleUnit const unit) {
92  Dms dms(a, unit);
93 
94  // make sure rounding won't give 60.00 for sec or min
95  if ((60.00 - dms.sec) < 0.005) {
96  dms.sec = 0.0;
97  dms.min += 1;
98  if (dms.min == 60) {
99  dms.min = 0;
100  dms.deg += 1;
101  if (dms.deg == 360) {
102  dms.deg = 0;
103  }
104  }
105  }
106 
107  std::string fmt("%02d:%02d:%05.2f");
108  std::string s = (boost::format(fmt) % dms.deg % dms.min % dms.sec).str();
109  if (dms.sign < 0) {
110  s = "-" + s;
111  }
112  return s;
113 }
114 
115 std::string angleToDmsString(lsst::geom::Angle const a) { return angleToXmsString(a, geom::degrees); }
116 
117 std::string angleToHmsString(lsst::geom::Angle const a) { return angleToXmsString(a, geom::hours); }
118 
126 static lsst::geom::Angle xmsStringToAngle(std::string const dms, lsst::geom::AngleUnit unit) {
127  if (dms.find(":") == std::string::npos) {
129  (boost::format("String is not in xx:mm:ss format: %s") % dms).str());
130  }
131  std::vector<std::string> elements;
132  boost::split(elements, dms, boost::is_any_of(":"));
133  if (elements.size() != 3) {
135  (boost::format("Could not parse string as xx:mm:ss format: %s") % dms).str());
136  }
137  int const deg = abs(atoi(elements[0].c_str()));
138  int const min = atoi(elements[1].c_str());
139  double const sec = atof(elements[2].c_str());
140 
141  lsst::geom::Angle ang = (deg + min / 60.0 + sec / 3600.0) * unit;
142  if ((elements[0].c_str())[0] == '-') {
143  ang *= -1.0;
144  }
145  return ang;
146 }
147 
148 lsst::geom::Angle hmsStringToAngle(std::string const hms) { return xmsStringToAngle(hms, geom::hours); }
149 
150 lsst::geom::Angle dmsStringToAngle(std::string const dms) { return xmsStringToAngle(dms, geom::degrees); }
151 
152 } // namespace coord
153 } // namespace afw
154 } // namespace lsst
constexpr double asAngularUnits(AngleUnit const &units) const noexcept
Return an Angle&#39;s value in the specified units.
Definition: Angle.h:160
Angle abs(Angle const &a)
Definition: Angle.h:106
AngleUnit constexpr hours
constant with units of hours
Definition: Angle.h:107
std::string angleToHmsString(lsst::geom::Angle const deg)
a function to convert decimal degrees to a string with form hh:mm:ss.s
Definition: Coord.cc:117
lsst::geom::Angle hmsStringToAngle(std::string const hms)
Convert a hh:mm:ss string to lsst::geom::Angle.
Definition: Coord.cc:148
int min
Definition: Coord.cc:82
table::Key< int > a
T floor(T... args)
A class representing an angle.
Definition: Angle.h:124
STL class.
lsst::geom::Angle dmsStringToAngle(std::string const dms)
Convert a dd:mm:ss string to lsst::geom::Angle.
Definition: Coord.cc:150
AngleUnit constexpr degrees
constant with units of degrees
Definition: Angle.h:106
A base class for image defects.
Definition: cameraGeom.dox:3
double sec
Definition: Coord.cc:83
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:129
T fabs(T... args)
solver_t * s
T find(T... args)
T size(T... args)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
std::string angleToDmsString(lsst::geom::Angle const deg)
Convert an angle to a string with form dd:mm:ss.
Definition: Coord.cc:115
Reports invalid arguments.
Definition: Runtime.h:66
int deg
Definition: Coord.cc:81
int m
Definition: SpanSet.cc:49
int sign
Definition: Coord.cc:84
A class used to convert scalar POD types such as double to Angle.
Definition: Angle.h:70