Skip to content

Commit 8f8d992

Browse files
committed
Initial import
0 parents  commit 8f8d992

File tree

10 files changed

+457
-0
lines changed

10 files changed

+457
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exopl
2+
src/*.o

Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
CXX := g++
3+
CXX_FLAGS := -Wall -std=c++11
4+
5+
SRC := src
6+
LIB := -lcurl -ljsoncpp
7+
BIN := exopl
8+
9+
OBJ = $(SRC)/exoplanet.o $(SRC)/ysapi.o $(SRC)/exopl.o $(SRC)/main.o
10+
11+
all: $(BIN)
12+
13+
$(BIN): $(OBJ)
14+
$(CXX) $(CXX_FLAGS) $(LIB) $^ -o $@ $(LIBRARIES)
15+
16+
$(SRC)/%.o: $(SRC)/%.cpp
17+
$(CXX) $(CXX_FLAGS) -c $< -o $@
18+
19+
clean:
20+
-rm -f $(BIN) $(SRC)/*.o

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
# exopl
3+
4+
**A work in progress..**
5+
6+
A minimalistic tool to illustrate the use of the
7+
[Y-Space](http://y-space.pw/) API in a C++ setting.
8+
9+
The `exopl` tool can be used to query the API and perform different
10+
operations, specified using a command.
11+
The goal is to build sequence of commands to perform more complex
12+
operations, combined with some popular command line tools,
13+
where the output of an operation is passed on as input to the
14+
next using pipes.
15+
16+
To build the tool just clone the repository and:
17+
18+
$ make
19+
20+
[libcurl](https://curl.haxx.se/libcurl/) and
21+
[jsoncpp](https://github.com/open-source-parsers/jsoncpp) are
22+
required. The `exopl` binary should be available in the local
23+
directory:
24+
25+
$ ./exopl
26+
27+
Usage: exopl <command> [expr]
28+
29+
Commands:
30+
list list all exoplanets
31+
filter filter exoplanets based on a given expreesion
32+
select select parameters when exporting based on a given expression
33+
as_txt print as text
34+
as_csv print as CSV
35+
36+
For example to get a count of exoplanets by discovery method using a
37+
combination of `exopl` and typical bash commands:
38+
39+
$ exopl list | exopl select discoverymethod | exopl as_txt | grep -v '^$' | sort | uniq -c
40+
1 discoverymethod: Astrometry
41+
1 discoverymethod: Disk Kinematics
42+
16 discoverymethod: Eclipse Timing Variations
43+
50 discoverymethod: Imaging
44+
96 discoverymethod: Microlensing
45+
6 discoverymethod: Orbital Brightness Modulation
46+
7 discoverymethod: Pulsar Timing
47+
2 discoverymethod: Pulsation Timing Variations
48+
810 discoverymethod: Radial Velocity
49+
3187 discoverymethod: Transit
50+
21 discoverymethod: Transit Timing Variations
51+
52+
Or for example to get the count of exoplanets discovered by year with an orbital period
53+
greater that 10 days:
54+
55+
$ exopl list | exopl filter 'pl_orbper>10' | exopl select disc_year | exopl as_txt | grep -v '^$' | sort | uniq -c
56+
1 disc_year: 1989
57+
2 disc_year: 1992
58+
1 disc_year: 1994
59+
4 disc_year: 1996
60+
1 disc_year: 1997
61+
4 disc_year: 1998
62+
11 disc_year: 1999
63+
13 disc_year: 2000
64+
11 disc_year: 2001
65+
(...)
66+
67+
It is also useful to create CVS files that can later be imported in other programs.
68+
For example to create a CSV file with a list of planets discovered after 2010 that contains
69+
the planet name and the orbital period:
70+
71+
$ exopl list | exopl filter 'disc_year>2000' | exopl select pl_name,pl_orbper | exopl as_csv > data.csv
72+
$ cat data.csv
73+
11_com_b,11 Com b,326.02999999999997
74+
11_umi_b,11 UMi b,516.21996999999999
75+
14_and_b,14 And b,185.84
76+
14_her_b,14 Her b,1773.40002
77+
18_del_b,18 Del b,993.29999999999995
78+
(...)

src/exopl.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
#include <stdio.h>
3+
#include <iostream>
4+
#include <string>
5+
#include <regex>
6+
#include <sstream>
7+
8+
#include <curl/curl.h>
9+
#include <json/json.h>
10+
11+
#include "exoplanet.hpp"
12+
#include "exopl.hpp"
13+
14+
#define FMT_TXT 0
15+
#define FMT_CSV 1
16+
17+
void exopl::op_list(ysapi api) {
18+
Json::Value root = api.list();
19+
20+
for (unsigned int i=0; i < root.size(); ++i )
21+
std::cout << root[i].asString() << std::endl;
22+
}
23+
24+
void exopl::op_filter(ysapi api, std::string expr) {
25+
std::string pl_id;
26+
Json::Value root;
27+
std::regex regexp("^([a-z_]+)(.)(.*?)$");
28+
std::smatch m;
29+
30+
for (std::string pl_id; std::getline(std::cin, pl_id); ) {
31+
root = api.id(pl_id);
32+
exoplanet pl(pl_id, root);
33+
34+
if (expr.empty()) {
35+
std::cout << pl_id << std::endl;
36+
}
37+
else {
38+
regex_search(expr, m, regexp);
39+
40+
if (pl.check(m[1], m[2], m[3]))
41+
std::cout << pl_id << std::endl;
42+
}
43+
}
44+
}
45+
46+
void exopl::op_select(ysapi api, std::string expr) {
47+
std::string pl_id;
48+
49+
std::cout << "##SELECT:" << expr << std::endl;
50+
51+
for (std::string pl_id; std::getline(std::cin, pl_id); ) {
52+
std::cout << pl_id << std::endl;
53+
}
54+
}
55+
56+
std::vector<std::string> _pre_proc_select(std::string line) {
57+
std::vector<std::string> select;
58+
59+
line.replace(0, 9, "");
60+
std::stringstream s_stream(line);
61+
while(s_stream.good()) {
62+
std::string substr;
63+
getline(s_stream, substr, ',');
64+
select.push_back(substr);
65+
}
66+
67+
return select;
68+
}
69+
70+
void _pp(ysapi api, int format) {
71+
std::string line, pl_id;
72+
Json::Value root;
73+
std::vector<std::string> select;
74+
75+
for (std::string line; std::getline(std::cin, line); ) {
76+
if (line.find("##SELECT:") == 0) {
77+
select = _pre_proc_select(line);
78+
}
79+
else {
80+
pl_id = line;
81+
82+
root = api.id(pl_id);
83+
exoplanet pl(pl_id, root);
84+
85+
86+
if (format == FMT_TXT)
87+
std::cout << pl.as_txt(select);
88+
if (format == FMT_CSV)
89+
std::cout << pl.as_csv(select);
90+
}
91+
}
92+
}
93+
94+
void exopl::op_as_txt(ysapi api) {
95+
_pp(api, FMT_TXT);
96+
}
97+
98+
void exopl::op_as_csv(ysapi api) {
99+
_pp(api, FMT_CSV);
100+
}

src/exopl.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
#include <string>
3+
#include "ysapi.hpp"
4+
5+
namespace exopl {
6+
void op_list(ysapi api);
7+
void op_filter(ysapi api, std::string expr);
8+
void op_select(ysapi api, std::string expr);
9+
void op_as_txt(ysapi api);
10+
void op_as_csv(ysapi api);
11+
}

src/exoplanet.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
#include <iostream>
3+
4+
#include <json/json.h>
5+
#include "exoplanet.hpp"
6+
7+
exoplanet::exoplanet(std::string id, Json::Value root) {
8+
pl_id = id;
9+
10+
for (std::string p : PARAMS)
11+
params[p] = root[p].asString();
12+
}
13+
14+
bool _select(std::vector<std::string> select, std::string param) {
15+
if (select.empty())
16+
return true;
17+
18+
for(auto s : select) {
19+
if (s.compare(param) == 0)
20+
return true;
21+
}
22+
23+
return false;
24+
}
25+
26+
std::string exoplanet::as_txt(std::vector<std::string> select) {
27+
std::string txt = "";
28+
29+
for (std::string p : PARAMS)
30+
if (_select(select, p))
31+
txt = txt + p + ": " + params[p] + "\n";
32+
33+
txt = txt + "\n";
34+
35+
return txt;
36+
}
37+
38+
std::string exoplanet::as_csv(std::vector<std::string> select) {
39+
std::string csv = pl_id;
40+
41+
for (std::string p : PARAMS)
42+
if (_select(select, p))
43+
csv = csv + "," + params[p];
44+
45+
csv = csv + "\n";
46+
47+
return csv;
48+
}
49+
50+
bool exoplanet::check(std::string param, std::string op, std::string val) {
51+
if (params[param].empty())
52+
return false;
53+
54+
float p = std::stof(params[param]), v = std::stof(val);
55+
56+
if (op.compare(">") == 0) {
57+
if (p > v)
58+
return true;
59+
else
60+
return false;
61+
}
62+
if (op.compare("<") == 0) {
63+
if (p < v)
64+
return true;
65+
else
66+
return false;
67+
}
68+
if (op.compare("=") == 0) {
69+
if (p == v)
70+
return true;
71+
else
72+
return false;
73+
}
74+
75+
std::cerr << "Failed to compare expression." << std::endl;
76+
return false;
77+
}

src/exoplanet.hpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
#ifndef EXOPLANET_H
3+
#define EXOPLANET_H
4+
5+
#include <string>
6+
#include <unordered_map>
7+
8+
#include <json/json.h>
9+
10+
const std::vector<std::string> PARAMS = {
11+
"pl_name", "hostname", "default_flag", "sy_snum", "sy_pnum",
12+
"discoverymethod", "disc_year", "disc_facility", "soltype",
13+
"pl_controv_flag", "pl_refname", "pl_orbper", "pl_orbpererr1",
14+
"pl_orbpererr2", "pl_orbperlim", "pl_orbsmax", "pl_orbsmaxerr1",
15+
"pl_orbsmaxerr2", "pl_orbsmaxlim", "pl_rade", "pl_radeerr1",
16+
"pl_radeerr2", "pl_radelim", "pl_radj", "pl_radjerr1", "pl_radjerr2",
17+
"pl_radjlim", "pl_bmasse", "pl_bmasseerr1", "pl_bmasseerr2",
18+
"pl_bmasselim", "pl_bmassj", "pl_bmassjerr1", "pl_bmassjerr2",
19+
"pl_bmassjlim", "pl_bmassprov", "pl_orbeccen", "pl_orbeccenerr1",
20+
"pl_orbeccenerr2", "pl_orbeccenlim", "pl_insol", "pl_insolerr1",
21+
"pl_insolerr2", "pl_insollim", "pl_eqt", "pl_eqterr1",
22+
"pl_eqterr2", "pl_eqtlim", "ttv_flag", "st_refname", "st_teff",
23+
"st_tefferr1", "st_tefferr2", "st_tefflim", "st_rad",
24+
"st_raderr1", "st_raderr2", "st_radlim", "st_mass", "st_masserr1",
25+
"st_masserr2", "st_masslim", "st_met", "st_meterr1", "st_meterr2",
26+
"st_metlim", "st_metratio", "st_logg", "st_loggerr1", "st_loggerr2",
27+
"st_logglim", "sy_refname", "rastr", "ra", "decstr", "dec", "sy_dist",
28+
"sy_disterr1", "sy_disterr2", "sy_vmag", "sy_vmagerr1", "sy_vmagerr2",
29+
"sy_kmag", "sy_kmagerr1", "sy_kmagerr2", "sy_gaiamag", "sy_gaiamagerr1",
30+
"sy_gaiamagerr2", "rowupdate", "pl_pubdate", "releasedate"
31+
};
32+
33+
class exoplanet {
34+
std::string pl_id;
35+
std::unordered_map<std::string, std::string> params;
36+
37+
public:
38+
exoplanet(std::string id, Json::Value root);
39+
std::string as_txt(std::vector<std::string> select);
40+
std::string as_csv(std::vector<std::string> select);
41+
bool check(std::string param, std::string op, std::string val);
42+
};
43+
44+
#endif

src/main.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
#include <stdio.h>
3+
#include <iostream>
4+
#include <string>
5+
#include <curl/curl.h>
6+
#include <json/json.h>
7+
8+
#include "ysapi.hpp"
9+
#include "exopl.hpp"
10+
11+
static void show_usage() {
12+
std::cerr << "\nUsage: exopl <command> [expr]\n\n"
13+
<< "Commands:\n"
14+
<< " list\t list all exoplanets\n"
15+
<< " filter\t filter exoplanets based on a given expreesion\n"
16+
<< " select\t select parameters when exporting based on a given expression\n"
17+
<< " as_txt\t print as text\n"
18+
<< " as_csv\t print as CSV\n\n";
19+
}
20+
21+
int main(int argc, char *argv[]) {
22+
ysapi api("http://api.y-space.pw");
23+
std::string op = "", expr = "";
24+
25+
if (argc < 2) {
26+
show_usage();
27+
return 1;
28+
}
29+
30+
op = argv[1];
31+
32+
if (argc > 2)
33+
expr = argv[2];
34+
35+
if (op.compare("list") == 0) {
36+
exopl::op_list(api);
37+
}
38+
else if (op.compare("filter") == 0) {
39+
exopl::op_filter(api, expr);
40+
}
41+
else if (op.compare("select") == 0) {
42+
exopl::op_select(api, expr);
43+
}
44+
else if (op.compare("as_txt") == 0) {
45+
exopl::op_as_txt(api);
46+
}
47+
else if (op.compare("as_csv") == 0) {
48+
exopl::op_as_csv(api);
49+
}
50+
else {
51+
show_usage();
52+
return 1;
53+
}
54+
55+
return 0;
56+
}

0 commit comments

Comments
 (0)