Skip to content

Commit

Permalink
Fix RaDec stream operators
Browse files Browse the repository at this point in the history
Resolve indilib#908
  • Loading branch information
not7cd committed Apr 11, 2019
1 parent 4a88664 commit 2591ffc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 37 deletions.
9 changes: 6 additions & 3 deletions indi-starbook/command_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,13 @@ namespace starbook {

lnh_equ_posn equ_posn = {{0, 0, 0},
{0, 0, 0, 0}};
HMS ra{};
DMS dec(res.payload.at("DEC"));

std::stringstream ss{res.payload.at("RA")};
ss >> ra;

starbook::HMS ra(res.payload.at("RA"));
equ_posn.ra = ra;
starbook::DMS dec(res.payload.at("DEC"));
equ_posn.dec = dec;
result.equ = {0, 0};
ln_hequ_to_equ(&equ_posn, &result.equ);
Expand Down Expand Up @@ -240,7 +243,7 @@ namespace starbook {
ln_date CommandInterface::ParseTimeResponse(const CommandResponse &response) {
if (!response.status) throw std::runtime_error("can't parse time");
std::stringstream ss{response.payload.at("time")};
DateTime time(0, 0, 0, 0, 0, 0);
DateTime time{0, 0, 0, 0, 0, 0};
ss >> time;
return time;
}
Expand Down
57 changes: 30 additions & 27 deletions indi-starbook/starbook_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

using namespace std;

constexpr char sep = '+';

starbook::DMS::DMS(string dms) : ln_dms{0, 0, 0, 0} {
static regex pattern(R"((-?)(\d+)\+(\d+))");
smatch results;
Expand All @@ -39,54 +41,55 @@ starbook::DMS::DMS(string dms) : ln_dms{0, 0, 0, 0} {
}
}

ostream &starbook::operator<<(ostream &os, const starbook::DMS &dms) {
if (dms.neg != 0) os << "-";
ostream &starbook::operator<<(ostream &os, const starbook::DMS &obj) {
if (obj.neg != 0) os << "-";
os << fixed << setprecision(0) << setfill('0')
<< setw(3) << dms.degrees
<< setw(0) << "+"
<< setw(2) << dms.minutes
<< setw(3) << obj.degrees
<< setw(0) << sep
<< setw(2) << obj.minutes
<< setw(0);
return os;
}

starbook::HMS::HMS(string hms) : ln_hms{0, 0, 0} {
static regex pattern(R"((\d+)\+(\d+)\.(\d+))");
smatch results;
if (regex_search(hms, results, pattern)) {
hours = (unsigned short) stoi(results[1].str());
minutes = (unsigned short) stoi(results[2].str());
seconds = (double) stoi(results[3].str());
} else {
throw;
}
}

ostream &starbook::operator<<(ostream &os, const starbook::HMS &hms) {
ostream &starbook::operator<<(ostream &os, const starbook::HMS &obj) {
os << fixed << setprecision(0) << setfill('0')
<< setw(2) << hms.hours
<< setw(0) << "+"
<< setw(2) << hms.minutes
<< setw(0) << "." << floor(hms.seconds);
<< setw(2) << obj.hours
<< setw(0) << sep
<< setw(2) << obj.minutes
<< setw(0) << "."
<< setw(1) << floor(obj.seconds / 6);
return os;
}

std::istream &starbook::operator>>(std::istream &is, starbook::HMS &obj) {
unsigned short h, m, m_tenth;
std::array<char, 2> ch = {{'\0'}};
is >> h >> ch[0] >> m >> ch[1] >> m_tenth;

if (!is) return is;
if (ch[0] != sep && ch[1] != '.') {
is.clear(ios_base::failbit);
return is;
}
obj = HMS(h, m, static_cast<double>(m_tenth * 6));
return is;
}

starbook::Equ::Equ(double ra, double dec) : lnh_equ_posn{{0, 0, 0},
{0, 0, 0, 0}} {
ln_equ_posn target_d = {ra, dec};
ln_equ_to_hequ(&target_d, this);
}

ostream &starbook::operator<<(ostream &os, const starbook::Equ &equ) {
ostream &starbook::operator<<(ostream &os, const starbook::Equ &obj) {
os << "RA=";
os << static_cast<const HMS &> (equ.ra);
os << static_cast<const HMS &> (obj.ra);

os << "&DEC=";
os << static_cast<const DMS &> (equ.dec);
os << static_cast<const DMS &> (obj.dec);
return os;
}

constexpr char sep = '+';

ostream &starbook::operator<<(ostream &os, const starbook::DateTime &obj) {
os << setfill('0') << std::fixed << setprecision(0)
<< obj.years << sep
Expand Down
22 changes: 15 additions & 7 deletions indi-starbook/starbook_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,23 @@ namespace starbook {
struct DMS : ln_dms {
explicit DMS(std::string dms);

friend std::ostream &operator<<(std::ostream &os, const DMS &dms);
friend std::ostream &operator<<(std::ostream &os, const DMS &obj);

friend std::istream &operator>>(std::istream &is, DMS &obj);
};

struct HMS : ln_hms {
explicit HMS(std::string hms);
explicit HMS(unsigned short h = 0, unsigned short m = 0, double s = 0) : ln_hms{h, m, s} {}

friend std::ostream &operator<<(std::ostream &os, const HMS &obj);

friend std::ostream &operator<<(std::ostream &os, const HMS &hms);
friend std::istream &operator>>(std::istream &is, HMS &obj);
};

struct Equ : lnh_equ_posn {
Equ(double ra, double dec);

friend std::ostream &operator<<(std::ostream &os, const Equ &equ);
friend std::ostream &operator<<(std::ostream &os, const Equ &obj);
};

struct DateTime : ln_date {
Expand All @@ -66,11 +70,15 @@ namespace starbook {
friend std::istream &operator>>(std::istream &is, LnLat &obj);
};

std::ostream &operator<<(std::ostream &os, const DMS &dms);
std::ostream &operator<<(std::ostream &os, const DMS &obj);

std::istream &operator>>(std::istream &is, DMS &obj);

std::ostream &operator<<(std::ostream &os, const HMS &obj);

std::ostream &operator<<(std::ostream &os, const HMS &hms);
std::istream &operator>>(std::istream &is, HMS &obj);

std::ostream &operator<<(std::ostream &os, const Equ &equ);
std::ostream &operator<<(std::ostream &os, const Equ &obj);

std::ostream &operator<<(std::ostream &os, const DateTime &obj);

Expand Down

0 comments on commit 2591ffc

Please sign in to comment.