sac-format 0.6.0 C++20 SAC (Seismic Analysis Code) File Library |
sac-format namespace More...
Namespaces | |
namespace | bitset_type |
bitset type-safety namespace. | |
Classes | |
class | coord |
Defines a geographic coordinant (degrees/radians) More... | |
class | io_error |
Class for generic I/O exceptions. More... | |
struct | point |
Defines a geographic point (latitude, longitude) More... | |
struct | read_spec |
Struct that specifies parameters for reading. More... | |
class | Trace |
The Trace class. More... | |
struct | word_pair |
Struct containing a pair of words. More... | |
Typedefs | |
using | char_bit = std::bitset< bits_per_byte > |
One binary character (useful for building strings). | |
using | word_one = std::bitset< binary_word_size > |
One binary word (useful for non-strings). | |
using | word_two = std::bitset< static_cast< size_t >(2) *binary_word_size > |
Two binary words (useful for strings). | |
using | word_four = std::bitset< static_cast< size_t >(4) *binary_word_size > |
Four binary words (kEvNm only). | |
template<class T > | |
using | unsigned_int = typename bitset_type::uint< sizeof(T) *bits_per_byte >::type |
Convert variable to unsigned-integer using type-safe conversions. | |
Enumerations | |
enum class | name { depmin , depmax , odelta , resp0 , resp1 , resp2 , resp3 , resp4 , resp5 , resp6 , resp7 , resp8 , resp9 , stel , stdp , evel , evdp , mag , user0 , user1 , user2 , user3 , user4 , user5 , user6 , user7 , user8 , user9 , dist , az , baz , gcarc , depmen , cmpaz , cmpinc , xminimum , xmaximum , yminimum , ymaximum , delta , b , e , o , a , t0 , t1 , t2 , t3 , t4 , t5 , t6 , t7 , t8 , t9 , f , stla , stlo , evla , evlo , sb , sdelta , nzyear , nzjday , nzhour , nzmin , nzsec , nzmsec , nvhdr , norid , nevid , npts , nsnpts , nwfid , nxsize , nysize , iftype , idep , iztype , iinst , istreg , ievreg , ievtyp , iqual , isynth , imagtyp , imagsrc , ibody , leven , lpspol , lovrok , lcalda , kstnm , kevnm , khole , ko , ka , kt0 , kt1 , kt2 , kt3 , kt4 , kt5 , kt6 , kt7 , kt8 , kt9 , kf , kuser0 , kuser1 , kuser2 , kcmpnm , knetwk , kdatrd , kinst , data1 , data2 } |
Enumeration of all SAC fields. More... | |
Functions | |
std::streamoff | word_position (const size_t word_number) noexcept |
Calculates position of word in SAC-file. | |
word_one | uint_to_binary (uint num) noexcept |
Convert unsigned integer to 32-bit (one word) binary bitset. | |
word_one | int_to_binary (int num) noexcept |
Convert integer to 32-bit (one word) binary bitset. | |
int | binary_to_int (word_one bin) noexcept |
Convert 32-bit (one word) binary bitset to integer. | |
word_one | float_to_binary (const float num) noexcept |
Convert floating-point value to 32-bit (one word) binary bitset. | |
float | binary_to_float (const word_one &bin) noexcept |
Convert 32-bit (one word) binary bitset to a floating-point value. | |
word_two | double_to_binary (const double num) noexcept |
Convert double-precision value to 64-bit (two words) binary bitset. | |
double | binary_to_double (const word_two &bin) noexcept |
Convert 64-bit (two words) binary bitset to double-precision value. | |
void | remove_leading_spaces (std::string *str) noexcept |
Remove all leading spaces from a string. | |
void | remove_trailing_spaces (std::string *str) noexcept |
Remove all trailing spaces from a string. | |
std::string | string_cleaning (const std::string &str) noexcept |
Remove leading/trailing spaces and control characters from a string. | |
void | prep_string (std::string *str, const size_t str_size) noexcept |
Cleans string and then truncates/pads as necessary. | |
template<typename T > | |
void | string_bits (T *bits, const std::string &str, const size_t str_size) noexcept |
Template function to convert string into binary bitset. | |
template<typename T > | |
std::string | bits_string (const T &bits, const size_t num_words) noexcept |
Template function to convert binary bitset to string. | |
word_two | string_to_binary (std::string str) noexcept |
Convert string to a 64-bit (two word) binary bitset. | |
std::string | binary_to_string (const word_two &str) noexcept |
Convert a 64-bit (two word) binary bitset to a string. | |
word_four | long_string_to_binary (std::string str) noexcept |
Convert a string to a 128-bit (four word) binary bitset. | |
std::string | binary_to_long_string (const word_four &str) noexcept |
Convert a 128-bit (four word) binary bitset to a string. | |
word_one | bool_to_binary (const bool flag) noexcept |
Convert a boolean to a 32-bit (one word) binary bitset. | |
bool | binary_to_bool (const word_one &flag) noexcept |
Convert a 32-bit (one word) binary bitset to a boolean. | |
word_two | concat_words (const word_pair< word_one > &pair_words) noexcept |
Concatenate two word_one binary strings into a single word_two string. | |
word_four | concat_words (const word_pair< word_two > &pair_words) noexcept |
Concatenate two word_two binary strings into a single word_four string. | |
bool | nwords_after_current (std::ifstream *sac, const read_spec &spec) noexcept |
Determine if the SAC-file has enough remaining data to read the requested amount of data. | |
void | safe_to_read_header (std::ifstream *sac) |
Determine if the SAC-file is large enough to contain a complete header. | |
void | safe_to_read_footer (std::ifstream *sac) |
Determines if the SAC-file has enough space remaining to contain a complete footer. | |
void | safe_to_read_data (std::ifstream *sac, const size_t n_words, const bool data2) |
Determines if the SAC-file has enough space remaining to contain a complete data vector. | |
void | safe_to_finish_reading (std::ifstream *sac) |
Determines if the SAC-file is finished. | |
word_one | read_word (std::ifstream *sac) |
Read one word (32 bits, useful for non-strings) from a binary SAC-File. | |
word_two | read_two_words (std::ifstream *sac) |
Read two words (64 bits, useful for most strings) from a binary SAC-file. | |
word_four | read_four_words (std::ifstream *sac) |
Read four words (128 bits, kEvNm only) from a binary SAC-file. | |
std::vector< double > | read_data (std::ifstream *sac, const read_spec &spec) |
Reader arbitrary number of words (useful for vectors) from a binary SAC-file. | |
void | write_words (std::ofstream *sac_file, const std::vector< char > &input) |
Write arbitrary number of words (useful for vectors) to a binary SAC-file. | |
template<typename T > | |
std::vector< char > | convert_to_word (const T input) noexcept |
Template function to convert input value into a std::vector<char> for writing. | |
std::vector< char > | convert_to_word (const double input) noexcept |
Convert double value into a std::vector<char> for writing. | |
template<size_t N> | |
std::array< char, N > | convert_to_words (const std::string &str, const size_t n_words) noexcept |
Template function to convert input string value into a std::array<char> for writing. | |
std::vector< char > | bool_to_word (const bool flag) noexcept |
Convert boolean to a word for writing. | |
bool | equal_within_tolerance (const std::vector< double > &vector1, const std::vector< double > &vector2, const double tolerance) noexcept |
Check if two std::vector<double> are equal within a tolerance limit. | |
bool | equal_within_tolerance (const double val1, const double val2, const double tolerance) noexcept |
Check if two double values are equal within a tolerance limit. | |
double | degrees_to_radians (const double degrees) noexcept |
Convert decimal degrees to radians. | |
double | radians_to_degrees (const double radians) noexcept |
Convert radians to decimal degrees. | |
double | gcarc (const point location1, const point location2) noexcept |
Calculate great circle arc distance in decimal degrees between two points. | |
double | azimuth (const point location1, const point location2) noexcept |
Calculate azimuth between two points. | |
double | limit_360 (const double degrees) noexcept |
Takes a decimal degree value and constrains it to full circle using symmetry. | |
double | limit_180 (const double degrees) noexcept |
Takes a decimal degree value and constrains it to a half circle using symmetry. | |
double | limit_90 (const double degrees) noexcept |
Takes a decimal degree value and constrains it to a quarter circle using symmetry. | |
template std::vector< char > | convert_to_word (const float input) noexcept |
template std::vector< char > | convert_to_word (const int x) noexcept |
template std::array< char, word_length > | convert_to_words (const std::string &str, const size_t n_words) noexcept |
Variables | |
constexpr size_t | word_length {4} |
Size (bytes) of fundamental data-chunk. | |
constexpr size_t | bits_per_byte {8} |
Size (bits) of binary character. | |
constexpr size_t | binary_word_size {word_length * bits_per_byte} |
Size (bits) of funamental data-chunk. | |
constexpr std::streamoff | data_word {158} |
First word of (first) data-section (stream offset). | |
constexpr int | unset_int {-12345} |
Integer unset value (SAC Magic). | |
constexpr float | unset_float {-12345.0F} |
Float-point unset value (SAC Magic). | |
constexpr double | unset_double {-12345.0} |
Double-precision unset value (SAC Magic). | |
constexpr bool | unset_bool {false} |
Boolean unset value (SAC Magic). | |
const std::string | unset_word {"-12345"} |
String unset value (SAC Magic). | |
constexpr float | f_eps {2.75e-6F} |
Accuracy precision expected of SAC floating-point values. | |
constexpr int | ascii_space {32} |
ASCII-code of 'space' character. | |
constexpr int | num_float {39} |
Number of float-poing header values in SAC format. | |
constexpr int | num_double {22} |
Number of double-precision header values in SAC format. | |
constexpr int | num_int {26} |
Number of integer header values in SAC format. | |
constexpr int | num_bool {4} |
Number of boolean header values in SAC format. | |
constexpr int | num_string {23} |
Number of string header values in SAC format. | |
constexpr int | num_data {2} |
Number of data arrays in SAC format. | |
constexpr int | num_footer {22} |
Number of double-precision footer values in SAC format (version 7). | |
constexpr int | modern_hdr_version {7} |
nVHdr value for newest SAC format (2020+). | |
constexpr int | old_hdr_version {6} |
nVHdr value for historic SAC format (pre-2020). | |
constexpr int | common_skip_num {7} |
Extremely common number of 'internal use' headers in SAC format. | |
constexpr double | rad_per_deg {std::numbers::pi_v<double> / 180.0} |
Radians per degree. | |
constexpr double | deg_per_rad {1.0 / rad_per_deg} |
Degrees per radian. | |
constexpr double | circle_deg {360.0} |
Degrees in a circle. | |
constexpr double | earth_radius {6378.14} |
Average radius of Earth (kilometers). | |
const std::unordered_map< name, const size_t > | sac_map |
Lookup table for variable locations. | |
sac-format namespace
using sacfmt::char_bit = typedef std::bitset<bits_per_byte> |
One binary character (useful for building strings).
Convert variable to unsigned-integer using type-safe conversions.
using sacfmt::word_four = typedef std::bitset<static_cast<size_t>(4) * binary_word_size> |
Four binary words (kEvNm only).
using sacfmt::word_one = typedef std::bitset<binary_word_size> |
One binary word (useful for non-strings).
using sacfmt::word_two = typedef std::bitset<static_cast<size_t>(2) * binary_word_size> |
Two binary words (useful for strings).
| strong |
Enumeration of all SAC fields.
Additional information can be found at SAC-file format
Calculate azimuth between two points.
Assumes spherical Earth (in future may update to solve on a more general body).
is latitude. is longitude. is azimuth.
[in] | location1 | point of first location. |
[in] | location2 | point of second location. |
Convert 64-bit (two words) binary bitset to double-precision value.
Converts bitset to unsigned long long then to double.
[in] | bin | word_two Binary value to be converted. |
Convert 32-bit (one word) binary bitset to a floating-point value.
Converts bitset to unsigned long then to float.
[in] | bin | word_one Binary value to be converted. |
Convert 32-bit (one word) binary bitset to integer.
Uses two's complement to convert a binary value into an integer.
[in] | bin | Binary value to be converted. |
Convert a 128-bit (four word) binary bitset to a string.
Exclusively used to work with the kEvNm header.
[in] | str | word_four to be converted to a string. |
Convert a 64-bit (two word) binary bitset to a string.
[in] | str | word_two to be converted to a string. |
Template function to convert binary bitset to string.
[in] | bits | Source bitset for the string. |
[in] | num_words | Length of string in words (4 chars = 1 word) |
Convert a boolean to a 32-bit (one word) binary bitset.
[in] | flag | Boolean value to be converted to a bitset (sets zeroth element). |
Concatenate two word_one binary strings into a single word_two string.
Useful for reading strings from SAC-files.
[in] | pair_words | word_pair Words to be concatenated. |
Concatenate two word_two binary strings into a single word_four string.
Exclusively used to read kEvNm header from SAC-file.
[in] | pair_words | word_pair Words to be concatenated. |
Convert double value into a std::vector<char> for writing.
[in] | input | Input value to convert (double). |
Template function to convert input value into a std::vector<char> for writing.
[in] | input | Input value (float or int) to convert. |
| noexcept |
Template function to convert input string value into a std::array<char> for writing.
[in] | str | Input string to convert. |
[in] | n_words | Number of words |
Convert decimal degrees to radians.
[in] | degrees | Angle in decimal degrees to be converted. |
Convert double-precision value to 64-bit (two words) binary bitset.
Converts double to unsigned-integer of same size for storage in bitset.
[in] | num | Double value to be converted. |
| noexcept |
Check if two double values are equal within a tolerance limit.
Default tolerance is f_eps.
[in] | val1 | First double in comparison. |
[in] | val2 | Second double in comparison. |
[in] | tolerance | Numerical equality tolerance (default f_eps). |
| noexcept |
Check if two std::vector<double> are equal within a tolerance limit.
Default tolerance is f_eps.
[in] | vector1 | First data vector in comparison. |
[in] | vector2 | Second data vector in comparison. |
[in] | tolerance | Numerical equality tolerance (default f_eps). |
Convert floating-point value to 32-bit (one word) binary bitset.
Converts float to unsigned-integer of same size for storage in bitset.
[in] | num | Float value to be converted. |
Calculate great circle arc distance in decimal degrees between two points.
Assumes spherical Earth (in future will include flatenning and adjustable radius for other bodies/greater accuracy).
is latitude. is longitude. is great circle arc distance (gcarc).
[in] | location1 | point of first location. |
[in] | location2 | point of second location |
Convert integer to 32-bit (one word) binary bitset.
Uses two's complement to convert an integer into a binary value.
[in] | num | Number to be converted. |
Takes a decimal degree value and constrains it to a half circle using symmetry.
[in] | degrees | Decimal degrees to be constrained. |
Takes a decimal degree value and constrains it to full circle using symmetry.
[in] | degrees | Decimal degrees to be constrained. |
Takes a decimal degree value and constrains it to a quarter circle using symmetry.
[in] | degrees | Decimal degrees to be constrained. |
| noexcept |
Convert a string to a 128-bit (four word) binary bitset.
If the string is longer than 16 characters, then only the first 16 characters are kept. If the string is less than 16 characters long, it is right-padded with spaces.
Exclusively used to work with the kEvNm header.
[in] | str | String to be converted to a bitset. |
Determine if the SAC-file has enough remaining data to read the requested amount of data.
[in] | sac | std::ifstream* SAC-file to read. |
[in] | spec | read_spec reading specification. |
Cleans string and then truncates/pads as necessary.
This edits the string in-place.
[in,out] | str | std::string* String to be prepared. |
[in] | str_size | Desired string length. |
Convert radians to decimal degrees.
[in] | radians | Angle in radians to be converted. |
Reader arbitrary number of words (useful for vectors) from a binary SAC-file.
Note that this modifies the position of the reader within the stream (to the end of the read words).
[in,out] | sac | std::ifstream* Input binary SAC-file. |
[in] | spec | read_spec Reading specification. |
word_four sacfmt::read_four_words | ( | std::ifstream * | sac | ) |
Read four words (128 bits, kEvNm only) from a binary SAC-file.
Note that this modifies the position of the reader within the stream (to the end of the read words).
[in,out] | sac | std::ifstream* Input binary SAC-file. |
word_two sacfmt::read_two_words | ( | std::ifstream * | sac | ) |
Read two words (64 bits, useful for most strings) from a binary SAC-file.
Note that this modifies the position of the reader within the stream (to the end of the read words).
[in,out] | sac | std::ifstream* Input binary SAC-file. |
word_one sacfmt::read_word | ( | std::ifstream * | sac | ) |
Read one word (32 bits, useful for non-strings) from a binary SAC-File.
Note that this modifies the position of the reader within the stream (to the end of the read word).
[in,out] | sac | std::ifstream* Input binary SAC-file. |
| noexcept |
| noexcept |
void sacfmt::safe_to_finish_reading | ( | std::ifstream * | sac | ) |
Determines if the SAC-file is finished.
This must run after reading the header, data vector(s), and footer (if applicable). This checks to ensure there is no additional data in the SAC-file (there shouldn't be, and out of safety it throws an io_error to inform the user if there are shenanigans).
[in] | sac | std::ifstream* SAC-file to be checked. |
io_error | If the file is not finished. |
Determines if the SAC-file has enough space remaining to contain a complete data vector.
This must be run after reading the header (and first data vector if applicable) and before the footer (if applicable).
[in] | sac | std::ifstream* SAC-file to read. |
[in] | n_words | Number of values in data vector. |
[in] | data2 | bool True if reading data2, false (default) if reading data1. |
io_error | If unsafe to read. |
void sacfmt::safe_to_read_footer | ( | std::ifstream * | sac | ) |
Determines if the SAC-file has enough space remaining to contain a complete footer.
This must be run after reading the header and data vector(s), not before.
[in] | sac | std::ifstream* SAC-file to read. |
io_error | If unsafe to read. |
void sacfmt::safe_to_read_header | ( | std::ifstream * | sac | ) |
Determine if the SAC-file is large enough to contain a complete header.
This must be run prior to reading the data vector(s) and footer (if applicable), not after.
[in] | sac | std::ifstream* SAC-file to read. |
io_error | If unsafe to read. |
Template function to convert string into binary bitset.
Note that this edits the bitset in place.
[out] | bits | Destintation bitset for the string (result). |
[in] | str | String to undergo conversion. |
[in] | str_size | Desired string size in words (4 chars = 1 word). |
| noexcept |
Remove leading/trailing spaces and control characters from a string.
[in] | str | std::string String to be cleaned. |
| noexcept |
Convert string to a 64-bit (two word) binary bitset.
If the string is longer than 8 characters, then only the first 8 characters are kept. If the string is less than 8 characters long, it is right-padded with spaces.
[in] | str | String to be converted to a bitset. |
| noexcept |
Convert unsigned integer to 32-bit (one word) binary bitset.
This sets the current bit using bitwise and, updates the bit to manipulate and performs a right-shift (division by 2) until the number is zero.
[in] | num | Number to be converted. |
Calculates position of word in SAC-file.
Multiplies given word number by the word-length in bytes (defined by the SAC format.)
[in] | word_number | Number of desired word in file stream. |
Write arbitrary number of words (useful for vectors) to a binary SAC-file.
Note that this modifies the position of the writer within the stream (to the end of the written words).
[in,out] | sac_file | std::ofstream* Output binary SAC-file. |
[in] | input | std::vector<char> Character vector representation of data for writing. |
| constexpr |
Size (bits) of funamental data-chunk.
Extremely common number of 'internal use' headers in SAC format.
| constexpr |
First word of (first) data-section (stream offset).
| constexpr |
Degrees per radian.
Average radius of Earth (kilometers).
Accuracy precision expected of SAC floating-point values.
nVHdr value for newest SAC format (2020+).
Number of double-precision header values in SAC format.
Number of float-poing header values in SAC format.
Number of double-precision footer values in SAC format (version 7).
nVHdr value for historic SAC format (pre-2020).
Radians per degree.
Lookup table for variable locations.
Maps SAC variables (headers and data) to their internal locations in the Trace class.
Double-precision unset value (SAC Magic).
Float-point unset value (SAC Magic).
const std::string sacfmt::unset_word {"-12345"} |
String unset value (SAC Magic).