Skip to content

Commit

Permalink
Fits external interface(reading) (BoostGSoC19#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
gopi487krishna committed Aug 11, 2020
1 parent 8caea42 commit 7c6487e
Show file tree
Hide file tree
Showing 34 changed files with 6,144 additions and 1,902 deletions.
11 changes: 11 additions & 0 deletions include/boost/astronomy/exception/fits_exception.hpp
Expand Up @@ -10,6 +10,7 @@ file License.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
#define BOOST_ASTRONOMY_EXCEPTION_FITS_EXCEPTION_HPP

#include <exception>
#include <string>

namespace boost
{
Expand Down Expand Up @@ -86,6 +87,16 @@ namespace boost
return "invalid_table_colum_format";
}
};
class file_reading_exception : public fits_exception {
std::string message;
public:
file_reading_exception(const std::string& error_message):message(error_message) {}
const char* what() const noexcept override {
return message.c_str();
}

};


} //namespace astronomy
} //namespace boost
Expand Down
263 changes: 91 additions & 172 deletions include/boost/astronomy/io/ascii_table.hpp

Large diffs are not rendered by default.

612 changes: 126 additions & 486 deletions include/boost/astronomy/io/binary_table.hpp

Large diffs are not rendered by default.

70 changes: 65 additions & 5 deletions include/boost/astronomy/io/bitpix.hpp
@@ -1,5 +1,6 @@
/*=============================================================================
Copyright 2018 Pranam Lashkari <plashkari628@gmail.com>
Copyright 2020 Gopi Krishna Menon <krishnagopi487.github@outlook.com>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file License.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
Expand All @@ -8,11 +9,8 @@ file License.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_ASTRONOMY_IO_BITPIX_HPP
#define BOOST_ASTRONOMY_IO_BITPIX_HPP

/**
* @file bitpix.hpp
* @author Pranam Lashkari
* @details Contains the enum declaration for representing bitpix values in header
*/
#include<boost/cstdint.hpp>
#include<boost/cstdfloat.hpp>

namespace boost { namespace astronomy { namespace io {

Expand All @@ -29,6 +27,68 @@ enum class bitpix
_B64 //! 64-bit IEEE double precision floating point
};

/**
* @brief Returns the element's size on the basis of its bitpix type
* @param[in] bitpix_value The bitpix value associated with the element
*/
int get_element_size_from_bitpix(bitpix bitpix_value) {
switch (bitpix_value)
{
case boost::astronomy::io::bitpix::B8:
return 1;
break;
case boost::astronomy::io::bitpix::B16:
return 2;
break;
case boost::astronomy::io::bitpix::B32:
return 4;
break;
case boost::astronomy::io::bitpix::_B32:
return 4;
break;
case boost::astronomy::io::bitpix::_B64:
return 8;
break;
default:
return 0;
}
}

/**
* @brief Converts bitpix enum value to its underlying type
* @tparam type bitpix enum value
*/

template<bitpix type>
struct bitpix_type;

template<>
struct bitpix_type<bitpix::B8> {
typedef typename boost::int8_t underlying_type;
};

template<>
struct bitpix_type<bitpix::B16> {
typedef typename boost::int16_t underlying_type;
};
template<>
struct bitpix_type<bitpix::B32> {
typedef typename boost::int32_t underlying_type;
};
template<>
struct bitpix_type<bitpix::_B32> {
typedef typename boost::float32_t underlying_type;
};
template<>
struct bitpix_type<bitpix::_B64> {
typedef typename boost::float64_t underlying_type;
};






}}} //namespace boost::astronomy::io

#endif // !BOOST_ASTRONOMY_IO_BITPIX_HPP
119 changes: 87 additions & 32 deletions include/boost/astronomy/io/card.hpp
Expand Up @@ -37,6 +37,15 @@ struct card
std::string card_;

public:

enum value_type {
logical_val,
string_val,
numeric_val,
complex_val
};


/**
* @brief Default Constructor to create a standalone object of <strong>card</strong>
* @throws std::bad_alloc If the request of 80 bytes of memory fails.
Expand Down Expand Up @@ -111,37 +120,44 @@ struct card
* @brief Constructs a card from key,value,comment(optional) supplied as the argument to the function
* @see card (std::string const& key,std::string const& value,std::string const& comment = "")
*/
void create_card
(
std::string const& key,
std::string const& value,
std::string const& comment = ""
)
{
if (key.length() > 8)
{
void create_card(std::string const& key, std::string const& value,
std::string const& comment = "", value_type v_type = value_type::string_val) {

if (key.length() > 8) {
throw invalid_key_length_exception();
}
if (!comment.empty() && (value.length() + comment.length() > 68))
{
if (!comment.empty() && (value.length() + comment.length() > 68)) {
throw invalid_value_length_exception();
}
else if (value.length() > 70)
{
else if (value.length() > 70) {
throw invalid_value_length_exception();
}

if (!comment.empty())
{
this->card_ = std::string(key).append(8 - key.length(), ' ') +
"= " + value + " /" + comment +
std::string("").append(68 - value.length() + comment.length(), ' ');
std::string content_string;

if (v_type == value_type::string_val) {
if (!comment.empty()) {
content_string = std::string(key).append(8 - key.length(), ' ') + "= " +
'\'' + value + '\'' + " /" + comment;
}
else {
content_string = std::string(key).append(8 - key.length(), ' ') + "= " +
'\'' + std::string(value) + '\'';
}
}
else
{
this->card_ = std::string(key).append(8 - key.length(), ' ') +
"= " + std::string(value).append(70 - key.length(), ' ');
else {
if (!comment.empty()) {
content_string = std::string(key).append(8 - key.length(), ' ') + "= " +
value + " /" + comment;
}
else {
content_string = std::string(key).append(8 - key.length(), ' ') + "= " +
std::string(value);
}
}


this->card_ = content_string + std::string(80 - content_string.length(), ' ');
}

/**
Expand All @@ -153,7 +169,7 @@ struct card
{
if (value)
{
create_card(key, std::string("T").insert(0, 19, ' '), comment);
create_card(key, std::string("T").insert(0, 19, ' '), comment, value_type::logical_val);
}
else
{
Expand All @@ -173,9 +189,27 @@ struct card
template <typename Value>
void create_card(std::string const& key, Value value, std::string const& comment = "")
{
std::string val = std::to_string(value);
val.insert(0, ' ', 20 - val.length());
create_card(key, val, comment);
std::string val;

std::stringstream value_stream;
value_stream << value;
val = value_stream.str();

// TODO: Macro to run this code in C++14 :). Average performance improvement 4 times
/*boost::hana::eval_if(
boost::is_same<double, Value>::value,
[&val, &value](void) { val = std::to_string(value); },
[&val, &value](void) {
std::stringstream value_stream;
value_stream << std::scientific << value;
val = value_stream.str();
}
);*/

val.insert(0, 20 - val.length(), ' ');
create_card(key, val, comment, value_type::numeric_val);

}


Expand All @@ -202,7 +236,7 @@ struct card
{
std::string value = "(" + std::to_string(real)+','+std::to_string(imaginary) + ")";

create_card(key, value, comment);
create_card(key, value, comment, value_type::complex_val);
}

/**
Expand All @@ -228,8 +262,8 @@ struct card
throw invalid_value_length_exception();
}

this->card_ = std::string(key).append(8 - key.length(), ' ') +
" " + std::string(value).append(70 - key.length(), ' ');
this->card_ = std::string(key).append(8 - key.length(), ' ') + " " +
std::string(value).append(70 - value.length(), ' ');
}

/**
Expand Down Expand Up @@ -266,7 +300,7 @@ struct card
*/
std::string value_with_comment() const
{
return this->card_.substr(10);
return boost::algorithm::trim_copy(this->card_.substr(10));
}

/**
Expand All @@ -289,6 +323,11 @@ struct card
this->card_.append(70 - value.length(), ' ');
}

/**
* @brief Returns the complete card as string
*/
const std::string& raw_card() { return card_; }

private:

/**
Expand All @@ -301,18 +340,34 @@ struct card
ReturnType value_imp(boost::type<ReturnType>) const
{
std::string val = boost::algorithm::trim_copy(
this->card_.substr(10, this->card_.find('/') - 10));
this->card_.substr(9, this->card_.find('/') - 10));
return boost::lexical_cast<ReturnType>(val);
}


/**
* @brief Returns the value associated with the card in string type
* @return string value
*/
std::string value_imp(boost::type<std::string>) const {
std::string val = boost::algorithm::trim_copy(
this->card_.substr(9, this->card_.find('/') - 10));
if (val[0] == '\'') {
return boost::algorithm::trim_copy(std::string(val.begin() + 1, val.end() - 1));
}
else {
return val;
}
}

/**
* @brief Returns the value associated with the card in logical type
* @return boolean value
*/
bool value_imp(boost::type<bool>) const
{
std::string val = boost::algorithm::trim_copy(
this->card_.substr(10, this->card_.find('/') - 10));
this->card_.substr(9, this->card_.find('/') - 10));
if (val == "T")
{
return true;
Expand Down

0 comments on commit 7c6487e

Please sign in to comment.