3FileInput::FileInput(
const std::string_view filename) : _filename(filename) {}
5std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
6FileInput::readFileFromDisk(FileInput::Filetype filetype) {
10 case FileInput::Filetype::ASCII:
12 return readASCIIFileFromDisk();
15 case FileInput::Filetype::YAAAR:
17 return readYAAARFileFromDisk();
20 case FileInput::Filetype::VOTABLE:
22 return readVOTableFileFromDisk();
25 case FileInput::Filetype::GUESS:
27 return readGuessedFileFromDisk();
32 throw(
Exception(
"Unable to resolve filetype or filetype not supported"));
36std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
37FileInput::readASCIIFileFromDisk() {
39 std::vector<double> xdata;
42 std::vector<double> ydata;
45 std::vector<double> yerrdata;
60 ifstr.exceptions(std::ifstream::failbit | std::ifstream::badbit);
64 ifstr.open(_filename, std::ios::in);
66 while (ifstr.get(c).good()) {
68 if (isdigit(c) || c ==
'.' || c ==
'-' || c ==
'+') {
71 }
else if (c ==
'#') {
73 while (ifstr.get(c).good()) {
79 }
else if (isspace(c)) {
83 d = strtod(value.c_str(), &endptr);
85 if (*endptr !=
'\0') {
87 throw Exception(
"Unable to convert string to double");
92 throw Exception(
"Extraneous number of columns");
93 }
else if (column == 1) {
95 xdata.push_back(1e4 / d);
96 }
else if (column == 2) {
99 }
else if (column == 3) {
101 yerrdata.push_back(d);
104 if (c ==
'\r' || c ==
'\n') {
118 }
catch (
const std::ifstream::failure &e) {
129 return std::make_tuple(xdata, ydata, yerrdata);
132std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
133FileInput::readYAAARFileFromDisk() {
135 std::vector<double> xdata;
138 std::vector<double> ydata;
141 std::vector<double> yerrdata;
142 yerrdata.reserve(64);
144 CCfits::FITS::setVerboseMode(
true);
146 std::unique_ptr<CCfits::FITS> fits;
150 fits = std::make_unique<CCfits::FITS>(_filename, CCfits::Read);
152 CCfits::ExtHDU &table = fits->extension(1);
154 table.column(1).read(xdata, 1, table.column(1).rows());
156 table.column(2).read(ydata, 1, table.column(2).rows());
158 table.column(3).read(yerrdata, 1, table.column(3).rows());
160 }
catch (CCfits::FitsException &e) {
165 for (
auto &x : xdata) {
170 return std::make_tuple(xdata, ydata, yerrdata);
173std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
174FileInput::readVOTableFileFromDisk() {
176 std::vector<double> xdata;
179 std::vector<double> ydata;
182 std::vector<double> yerrdata;
183 yerrdata.reserve(64);
189 tinyxml2::XMLDocument doc;
191 if (doc.LoadFile(_filename.c_str()) != tinyxml2::XML_SUCCESS) {
196 tinyxml2::XMLElement *root = doc.RootElement();
198 if (strcmp(root->Name(),
"VOTABLE") != 0) {
200 throw Exception(
"Incorrect document root");
203 tinyxml2::XMLElement *resource = root->FirstChildElement(
"RESOURCE");
205 if (
nullptr == resource) {
207 throw Exception(
"VOTABLE has no resource");
210 tinyxml2::XMLElement *table = resource->FirstChildElement(
"TABLE");
212 if (
nullptr == table) {
214 throw Exception(
"VOTABLE resource has no table");
217 tinyxml2::XMLElement *data = table->FirstChildElement(
"DATA");
219 if (
nullptr == data) {
221 throw Exception(
"VOTABLE resource table has no data");
224 tinyxml2::XMLElement *tabledata = data->FirstChildElement(
"TABLEDATA");
226 if (
nullptr == tabledata) {
228 throw Exception(
"VOTABLE resource table data has no tabledata");
231 tinyxml2::XMLElement *tr = tabledata->FirstChildElement();
235 if (strcmp(tr->Name(),
"TR") != 0) {
237 throw Exception(
"VOTABLE resource table data tabledata expects row");
240 tinyxml2::XMLElement *td = tr->FirstChildElement();
242 if (strcmp(td->Name(),
"TD") != 0) {
245 "VOTABLE resource table data tabledata row expects table data");
248 d = strtod(td->GetText(), &endptr);
250 if (*endptr !=
'\0') {
252 throw Exception(
"Unable to convert string to double");
255 xdata.push_back(1e4 / d);
257 td = td->NextSiblingElement();
259 if (strcmp(td->Name(),
"TD") != 0) {
262 "VOTABLE resource table data tabledata row expects table data");
265 d = strtod(td->GetText(), &endptr);
267 if (*endptr !=
'\0') {
269 throw Exception(
"Unable to convert string to double");
274 tr = tr->NextSiblingElement();
282 return std::make_tuple(xdata, ydata, yerrdata);
285std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
286FileInput::readGuessedFileFromDisk() {
288 std::unique_ptr<CCfits::FITS> fits;
292 fits = std::make_unique<CCfits::FITS>(_filename);
293 }
catch (CCfits::FitsError &e) {
295 std::ifstream ifstr(_filename);
299 getline(ifstr, line);
301 if (line.compare(0, 5,
"<?xml") == 0) {
303 return FileInput::readVOTableFileFromDisk();
306 return FileInput::readASCIIFileFromDisk();
310 return FileInput::readYAAARFileFromDisk();