WSL/SLF GitLab Repository

Commit d18d5de5 authored by Thomas Egger's avatar Thomas Egger
Browse files

An update of libsmet. Added SMETReader::get_filename() and...

An update of libsmet. Added SMETReader::get_filename() and SMETReader::get_header_intvalue(string key) with which a header value can be directly converted into an integer.

Added libsmet.h to MeteoIO.h
parent 7ed3fa77
......@@ -72,6 +72,7 @@
#include <meteoio/ResamplingAlgorithms2D.h>
#include <meteoio/StationData.h>
#include <meteoio/Timer.h>
#include <meteoio/libsmet.h>
#ifdef _POPC_
#include <meteoio/marshal_meteoio.h>
......
......@@ -85,6 +85,15 @@ double SMETCommon::convert_to_double(const std::string& in_string)
return value;
}
int SMETCommon::convert_to_int(const std::string& in_string)
{
istringstream ss(in_string);
int value;
if (!(ss >> value)) throw SMETException("Value '" + in_string + "' cannot be converted to int", SMET_AT);
return value;
}
void SMETCommon::trim(std::string& str)
{
const std::string whitespaces(" \t\f\v\n\r");
......@@ -250,7 +259,10 @@ void SMETWriter::set_header_value(const std::string& key, const double& value)
set_header_value(key, ss.str());
} else {
throw SMETException("Trying to set a decimal value when a non-decimal is expected", SMET_AT);
//It's a non-standard header value
stringstream ss;
ss << value; //for nodata
set_header_value(key, ss.str());
}
}
......@@ -259,6 +271,10 @@ void SMETWriter::set_header_value(const std::string& key, const std::string& val
//check if header key/value pair is valid
if (valid_header_pair(key, value)){
header[key] = value;
if ((SMETCommon::all_optional_header_keys.find(key) == SMETCommon::all_optional_header_keys.end())
&& (SMETCommon::all_mandatory_header_keys.find(key) == SMETCommon::all_mandatory_header_keys.end()))
other_header_keys.push_back(key);
} else {
throw SMETException("Invalid, inconsistent or unknown key/value pair: " + key + " = " + value, SMET_AT);
}
......@@ -266,16 +282,13 @@ void SMETWriter::set_header_value(const std::string& key, const std::string& val
bool SMETWriter::valid_header_pair(const std::string& key, const std::string& value)
{
bool key_ok = false;
bool key_ok = true;
if (SMETCommon::all_mandatory_header_keys.find(key) != SMETCommon::all_mandatory_header_keys.end()){
mandatory_header_keys.insert(key);
key_ok = true;
}
//if (SMETCommon::all_optional_header_keys.find(key) != SMETCommon::all_optional_header_keys.end())
key_ok = true; //it doesn't matter if key is mandatory or optional
//nodata value needs extra treatment
if (key == "nodata"){
istringstream ss(value);
......@@ -328,7 +341,7 @@ bool SMETWriter::check_fields(const std::string& key, const std::string& value)
} else {
nr_of_fields = counter;
}
size_t count_wgs84 = 0, count_epsg = 0;
if (key == "fields"){
//set<string> fieldnames; //this will help us locate duplicate fields
......@@ -544,6 +557,10 @@ void SMETWriter::write_header()
if (it != header.end())
fout << "comment = " << it->second << endl;
for (size_t ii=0; ii<other_header_keys.size(); ii++){
fout << other_header_keys[ii] << " = " << header[other_header_keys[ii]] << endl;
}
fout << "fields = " << header["fields"] << endl;
fout << "[DATA]" << endl;
}
......@@ -762,13 +779,9 @@ void SMETReader::process_header()
else if (it->first == "altitude") location_epsg |= 4;
else if (it->first == "epsg") location_epsg |= 8;
//Now do some value checks
if (it->first == "epsg"){
istringstream ss(it->second);
int intvalue;
if (!(ss >> intvalue))
throw SMETException("In " + filename + ": EPSG code not an integer number", SMET_AT);
}
//Now do a value check on EPSG
if (it->first == "epsg")
SMETCommon::convert_to_int(it->second);
}
if (get_header_value("units_offset") != ""){
......@@ -1115,6 +1128,15 @@ double SMETReader::get_header_doublevalue(const std::string& key) const
return nodata_value;
}
int SMETReader::get_header_intvalue(const std::string& key) const
{
map<string,string>::const_iterator it = header.find(key);
if (it != header.end())
return SMETCommon::convert_to_int(it->second);
return floor(nodata_value + 0.1);
}
std::string SMETReader::get_header_value(const std::string& key) const
{
map<string,string>::const_iterator it = header.find(key);
......@@ -1129,5 +1151,10 @@ bool SMETReader::contains_timestamp() const
return timestamp_present;
}
std::string SMETReader::get_filename() const
{
return filename;
}
} //end namespace
......@@ -18,6 +18,7 @@
#ifndef __LIBSMET_H__
#define __LIBSMET_H__
#include <cmath>
#include <string>
#include <sstream>
#include <iostream>
......@@ -60,6 +61,7 @@ class SMETException : public std::exception {
class SMETCommon {
public:
static double convert_to_double(const std::string& in_string);
static int convert_to_int(const std::string& in_string);
static void stripComments(std::string& str);
static char getEoln(std::istream& fin);
static void trim(std::string& str);
......@@ -143,6 +145,13 @@ class SMETReader {
* @return The value for the key, if the key is not present return nodata
*/
double get_header_doublevalue(const std::string& key) const;
/**
* @brief Get an int value for a header key in a SMET file
* @param[in] key A key in the header section of a SMET file
* @return The value for the key, if the key is not present return (int)nodata
*/
int get_header_intvalue(const std::string& key) const;
/**
* @brief Check whether timestamp is a part of the fields
......@@ -194,6 +203,12 @@ class SMETReader {
*/
void convert_to_MKSA(const bool& in_mksa);
/**
* @brief Retrieve the filename that this reader operates upon
* @return a std::string representing the filename
*/
std::string get_filename() const;
private:
void read_data_ascii(std::ifstream& fin, std::vector<std::string>& vec_timestamp, std::vector<double>& vec_data);
void read_data_binary(std::ifstream& fin, std::vector<double>& vec_data);
......@@ -314,6 +329,7 @@ class SMETWriter {
bool file_is_binary;
char location_wgs84, location_epsg;
std::vector<std::string> other_header_keys; //this vector is used to preserve the sequence of header keys
std::vector<size_t> ascii_precision, ascii_width;
std::map< std::string, std::string > header;
std::set<std::string> mandatory_header_keys;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment