-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfits_standard_spec.h
553 lines (353 loc) · 15 KB
/
fits_standard_spec.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#pragma once
#include<complex>
#include <vector>
#include <string>
#include <algorithm>
#include <variant>
#include <boost/spirit/include/qi.hpp>
#include <boost/algorithm/string/trim.hpp>
namespace fits {
class fits_standard_spec {
const std::vector<std::string>required_keywords = { "SIMPLE","BITPIX","NAXIS" };
const std::vector<std::pair<std::string, std::pair<char, bool>>>reserved_keywords = {
{"HISTORY",{'N',true}},
{"COMMENT",{'N', true}},
{"BUNIT",{'C', false}},
{"BSCALE",{'F',false}},
{"BZERO",{'F',false }},
{"BLANK",{'I',false}},
{"DATAMAX",{'F',false }},
{"DATAMIN",{'F',false}},
{"DATE",{'C',false}},
{"DATE-OBS",{'C',false }},
{"ORIGIN",{'C',false}},
{"TELESCOP",{'C',false}},
{"INSTRUME",{'C',false}},
{"OBSERVER",{'C',false }},
{"OBJECT",{'C',false}},
{"AUTHOR",{'C',false}},
{"REFERENC",{'C',false }},
{"EQUINOX",{'F',false}},
{"BLOCKED",{'L',false}},
};
public:
typedef std::variant<std::monostate, std::string, long long, double, char, std::complex<long long>, std::complex<double>> value_type;
enum keyword_class {
reserved,
user_defined,
blank,
no_value,
none
};
enum value_format {
fixed,
variable
};
struct keyword_info {
std::string name;
keyword_class key_class;
bool multivalued{ false };
};
fits_standard_spec() {
}
// Keyword Related Helpers
keyword_info getKeyword(const std::string& raw_card);
bool isReservedKeyword(const std::string& keyword);
bool isRequiredKeyword(const std::string& keyword);
bool isRequiredKeywordInOrder(const std::string& keyword, size_t index);
bool isMultivalued(const std::string& keyword);
// Value Related Helpers
auto getReservedKeyPos(const std::string& keyword);
value_type parseValue(const std::string& raw_card, const std::string& keyword, keyword_class key_class);
value_type parseValueForReserved(const std::string& raw_card, const std::string& keyword);
// Type parse related Helpers
char parseLogical(const std::string& value_part);
std::string parseCharacterString(const std::string& value_part);
std::pair<long long, bool> parseInteger(const std::string& value_part);
std::pair<double, bool>parseFloatingPoint(const std::string& value_part);
template<typename Type>
std::pair<std::complex<Type>, bool> parse_fixed_Complex(const std::string& value_part);
template<typename Type>
std::pair<std::complex<Type>, bool> parse_Complex(const std::string& value_part);
std::string cnvToString(std::monostate);
std::string cnvToString(long long value);
std::string cnvToString(double value);
std::string cnvToString(std::complex<long long> value);
std::string cnvToString(std::complex<double>value);
std::string cnvToString(char value);
std::string cnvToString(const std::string& value);
};
//----------------------------------------------------------------------------------------------------------------------------
bool fits_standard_spec::isMultivalued(const std::string& keyword) {
return std::any_of(reserved_keywords.begin(), reserved_keywords.end(), [&keyword](const std::pair<std::string, std::pair<char, bool>>& r_key)
{return keyword == r_key.first && r_key.second.second; }
);
}
std::string fits_standard_spec::cnvToString(const std::string& value) { return value; }
std::string fits_standard_spec::cnvToString(std::monostate) { return ""; }
std::string fits_standard_spec::cnvToString(char value) {
return std::string(1, value);
}
std::string fits_standard_spec::cnvToString(long long value) {
std::string value_string = std::to_string(value);;
std::string result_string;
if (value_string.length() < 20) {
auto difference = 20 - value_string.length();
while (difference--) { result_string.push_back(' '); }
result_string += value_string;
return result_string;
}
return value_string;
}
std::string fits_standard_spec::cnvToString(double value) {
std::ostringstream digit_stream;
std::string result_string;
digit_stream << value;
if (digit_stream.str().length() < 20)
{
auto difference = 20 - digit_stream.str().length();
while (difference--) { result_string.push_back(' '); }
result_string += digit_stream.str();
}
return result_string;
}
std::string fits_standard_spec::cnvToString(std::complex<long long> value) {
auto real_part = std::to_string(value.real());
auto imaginary_part = std::to_string(value.imag());
return real_part + " " + imaginary_part;
}
std::string fits_standard_spec::cnvToString(std::complex<double> value) {
std::ostringstream value_stream;
value_stream << value.real() << " " << value.imag();
return value_stream.str();
}
// Is it really required here ? . I have to research
template<typename T>
struct get_spirit_type { typedef T value_type; };
template<>
struct get_spirit_type<long long> { static inline auto value_type = boost::spirit::qi::long_long; };
template<>
struct get_spirit_type<double> { static inline auto value_type = boost::spirit::qi::double_; };
template<>
struct get_spirit_type<int> { static inline auto value_type = boost::spirit::qi::int_; };
//-----------------------------------------------------------------------------------------------------------------------------
template<typename Type>
std::pair<std::complex<Type>, bool> fits_standard_spec::parse_Complex(const std::string& value_part) {
auto starting_position = value_part.begin();
auto current_position = starting_position;
auto comment_position = std::find(starting_position, value_part.end(), '/');
auto ending_position = comment_position;
// Remove spaces before '/'
while (*ending_position == ' ' && ending_position != starting_position) { ending_position--; }
// Parse Real Part
Type real_part{ 0 };
Type imaginary_part{ 0 };
// Get the real part
boost::spirit::qi::parse(current_position, ending_position, get_spirit_type<Type>::value_type, real_part);
if (current_position == ending_position) {
// Imaginary value is 0
return { std::complex<Type>(), false };
}
else if (*current_position == ' ') {
while (*current_position == ' ') current_position++;
boost::spirit::qi::parse(current_position, ending_position, get_spirit_type<Type>::value_type, imaginary_part);
if (current_position == ending_position) {
return { std::complex<Type>(real_part,imaginary_part),true };
};
}
return { std::complex<Type>(),false };
}
template<typename Type>
std::pair<std::complex<Type>, bool> fits_standard_spec::parse_fixed_Complex(const std::string& value_part) {
Type real_part{ 0 };
Type imaginary_part{ 0 };
auto starting_position = value_part.begin();
auto current_position = starting_position;
auto real_end_position = current_position + 30;
if (boost::spirit::qi::parse(current_position, real_end_position, get_spirit_type<Type>::value_type, real_part) && std::distance(current_position, starting_position) == 29) {
// Clear the leading spaces before imaginary number
while (*current_position == ' ') { current_position++; }
if (boost::spirit::qi::parse(current_position, value_part.end(), get_spirit_type<Type>::value_type, imaginary_part) && std::distance(current_position, starting_position) == 49) {
return { std::complex<Type>(real_part,imaginary_part),true };
}
}
return { std::complex<Type>(),false };
}
// Implementation ( Could be defined in terms of a compiled binary as well )
auto fits_standard_spec::getReservedKeyPos(const std::string& keyword) {
return std::find_if(reserved_keywords.begin(), reserved_keywords.end(), [&keyword](std::pair<std::string, std::pair<char, bool>> keyw) {return keyword == keyw.first; });
}
bool fits_standard_spec::isReservedKeyword(const std::string& keyword) {
return std::find_if(reserved_keywords.begin(), reserved_keywords.end(), [&keyword](std::pair<std::string, std::pair<char, bool>> keyw) {return keyword == keyw.first; }) != reserved_keywords.end();
}
bool fits_standard_spec::isRequiredKeyword(const std::string& keyword) {
return std::find(required_keywords.begin(), required_keywords.end(), keyword) != required_keywords.end();
}
std::pair<long long, bool> fits_standard_spec::parseInteger(const std::string& value_part) {
// A lot of optimizations can be done here
auto current_position = value_part.begin();
auto comment_position = std::find(value_part.begin(), value_part.end(), '/');
long long resultant_integer;
if (comment_position != value_part.end()) {
if (*(comment_position - 1) == ' ') { comment_position--; }
if (boost::spirit::qi::parse(current_position, comment_position, boost::spirit::qi::long_long, resultant_integer) && current_position == comment_position) {
return { resultant_integer,true };
}
}
else {
if (boost::spirit::qi::parse(current_position, value_part.end(), boost::spirit::qi::long_long, resultant_integer) && current_position == value_part.end()) {
return { resultant_integer,true };
}
}
return { resultant_integer,false };
}
std::pair<double, bool> fits_standard_spec::parseFloatingPoint(const std::string& value_part) {
// A lot of optimizations can be done here
auto current_position = value_part.begin();
auto comment_position = std::find(value_part.begin(), value_part.end(), '/');
double resultant_fp;
if (comment_position != value_part.end()) {
if (*(comment_position - 1) == ' ') { comment_position--; }
if (boost::spirit::qi::parse(current_position, comment_position, boost::spirit::qi::double_, resultant_fp) && current_position == comment_position) {
return { resultant_fp,true };
}
}
else {
if (boost::spirit::qi::parse(current_position, value_part.end(), boost::spirit::qi::double_, resultant_fp) && current_position == value_part.end()) {
return { resultant_fp,true };
}
}
return { resultant_fp,false };
}
std::string fits_standard_spec::parseCharacterString(const std::string& value_part) {
std::string value_string;
value_string.reserve(70);
if (value_part[0] == '\'') {
if (value_part.length() >= 21 && value_part[19] == '\'' && value_part[20] != '\'') {
// Means it followed fixed format ( This is actually not needed )
std::copy_n(value_part.begin() + 1, 7, std::back_inserter(value_string));
}
else {
// Copying it the old fashioned way
std::string temp_string;
auto current_pos = value_part.begin() + 1;
auto ending_pos = value_part.end();
while (current_pos != ending_pos) {
if ((current_pos != ending_pos - 1) && (*current_pos == '\'') && (*(current_pos + 1)) == '\'') {
temp_string.push_back(*current_pos);
current_pos += 2;
continue;
}
else if (*current_pos == '\'') {
value_string = std::move(temp_string);
break;
}
temp_string.push_back(*current_pos++);
}
}
}
return value_string;
}
fits_standard_spec::value_type fits_standard_spec::parseValueForReserved(const std::string& raw_card, const std::string& keyword) {
// Need to optimize this code further
if (keyword == "COMMENT" || keyword == "HISTORY") {
std::string::const_iterator starting_pos = raw_card.begin();
if (raw_card[8] == '=') {
std::advance(starting_pos, 9);
}
else {
std::advance(starting_pos, 8);
}
return std::string(starting_pos, raw_card.end());
}
auto reserved_key_type = getReservedKeyPos(keyword)->second.first;
std::string r_c(raw_card.begin() + 10, raw_card.end());
boost::algorithm::trim(r_c);
if (reserved_key_type == 'L') {
if (auto x = parseLogical(r_c); x != 'N') { return x; }
}
else if (reserved_key_type == 'F') {
if (auto x = parseFloatingPoint(r_c); x.second) { return x.first; }
}
else if (reserved_key_type == 'I') {
if (auto x = parseInteger(r_c); x.second) { return x.first; }
}
else if (reserved_key_type == 'C') {
if (auto x = parseCharacterString(r_c); !x.empty()) { return x; }
}
return std::monostate{};
}
char fits_standard_spec::parseLogical(const std::string& value_part) {
if (value_part[0] == 'T' || value_part[0] == 'F') { return value_part[0]; }
// Indicates no value
return 'N';
}
fits_standard_spec::value_type fits_standard_spec::parseValue(const std::string& raw_card, const std::string& keyword, keyword_class key_class) {
// Check for Logical
if (key_class == keyword_class::reserved) {
return parseValueForReserved(raw_card, keyword);
}
if (key_class == keyword_class::user_defined) {
std::string r_c(raw_card.begin() + 10, raw_card.end());
boost::algorithm::trim(r_c);
if (auto x = parseLogical(r_c); x != 'N') { return x; }
else if (auto x = parseCharacterString(r_c); !x.empty()) { return x; }
else if (auto x = parseInteger(r_c); x.second) { return x.first; }
else if (auto x = parseFloatingPoint(r_c); x.second) { return x.first; }
else if (auto x = parse_fixed_Complex<long long>(r_c); x.second) { return x.first; }
else if (auto x = parse_fixed_Complex<double>(r_c); x.second) { return x.first; }
else if (auto x = parse_Complex<long long>(r_c); x.second) { return x.first; }
else if (auto x = parse_Complex<double>(r_c); x.second) { return x.first; }
}
return std::monostate{};
}
bool fits_standard_spec::isRequiredKeywordInOrder(const std::string& keyword, size_t index) {
// This function is basically a hack for getting performance and will work with fits parser only.
if (index > required_keywords.size()) return false;
if (required_keywords[index] == keyword) {
// Element in its spec pos
return true;
}
// Not in spec pos
return false;
}
fits_standard_spec::keyword_info fits_standard_spec::getKeyword(const std::string& raw_card) {
keyword_info key_info;
auto keyword_start_pos = raw_card.begin();
auto keyword_end_pos = keyword_start_pos + 7;
while (*(keyword_end_pos) == ' ' && keyword_end_pos != raw_card.begin()) { keyword_end_pos--; } // Neglect all the trailing spaces
if (keyword_end_pos == keyword_start_pos && *keyword_end_pos == ' ') {
key_info.key_class = keyword_class::blank;
}
else {
std::string keyword;
std::copy_if(keyword_start_pos, keyword_end_pos + 1, std::back_inserter(keyword), [](char key_char) {
return isupper(key_char) || isdigit(key_char) || key_char == '-' || key_char == '_';
});
// If the keyword satisfies all criteria's then the length will be same
size_t distance = static_cast<size_t>(std::distance(keyword_start_pos, keyword_end_pos));
if (distance == keyword.length() - 1)
{
if (auto element = getReservedKeyPos(keyword); element != reserved_keywords.end()) {
// Checking whether the format is valid is done at the value end
key_info.key_class = keyword_class::reserved;
key_info.name = std::move(keyword);
key_info.multivalued = element->second.second;
}
else if (raw_card[8] == '=' && raw_card[9] == ' ') {
key_info.name = std::move(keyword);
key_info.key_class = keyword_class::user_defined;
}
else {
key_info.name = std::move(keyword);
key_info.key_class = keyword_class::no_value;
}
}
else {
key_info.key_class = keyword_class::none;
}
}
// Return the keyword along with its class
return key_info;
}
}