WSL/SLF GitLab Repository

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

MeteoData is now able to hold any meteo parameters, however the ones in...

MeteoData is now able to hold any meteo parameters, however the ones in MeteoData::Parameters are the only ones that go through the filtering and resampling for now. To add a new parameter to a MeteoData object do the following:

md.addParameter("VVT");
md.param("VVT") = 222.0;
parent 2b615630
......@@ -190,13 +190,11 @@ void BufferedIOHandler::readMeteoData(const Date& i_date, std::vector<MeteoData>
try {
vector<StationData> vecStations;
iohandler.readStationData(i_date, vecStations);
if (vecStations.size() > ii)sd = vecStations[ii];
if (vecStations.size() > ii) sd = vecStations[ii];
} catch(exception& ex) {/*Ignore any exception*/}
}
if (index != IOUtils::npos) {
//vecMeteo.push_back(meteoBuffer[ii][index]);
//vecStation.push_back(stationBuffer[ii][index]);
vecMeteo.push_back(md);
vecStation.push_back(sd);
} else {
......
......@@ -24,25 +24,24 @@ namespace mio {
* static section *
************************************************************/
const unsigned int MeteoData::nrOfParameters = MeteoData::lastparam - MeteoData::firstparam + 1;
map<unsigned int, string> MeteoData::meteoparamname;
map<unsigned int, string> MeteoData::static_meteoparamname;
const bool MeteoData::__init = MeteoData::initStaticData();
bool MeteoData::initStaticData()
{
//This function should only be executed once for all MeteoData instances
//Associate unsigned int value and a string representation of a meteo parameter
meteoparamname[TA] = "TA";
meteoparamname[ISWR] = "ISWR";
meteoparamname[VW] = "VW";
meteoparamname[DW] = "DW";
meteoparamname[RH] = "RH";
meteoparamname[ILWR] = "ILWR";
meteoparamname[HNW] = "HNW";
meteoparamname[TSG] = "TSG";
meteoparamname[TSS] = "TSS";
meteoparamname[HS] = "HS";
meteoparamname[RSWR] = "RSWR";
meteoparamname[P] = "P";
static_meteoparamname[TA] = "TA";
static_meteoparamname[ISWR] = "ISWR";
static_meteoparamname[VW] = "VW";
static_meteoparamname[DW] = "DW";
static_meteoparamname[RH] = "RH";
static_meteoparamname[ILWR] = "ILWR";
static_meteoparamname[HNW] = "HNW";
static_meteoparamname[TSG] = "TSG";
static_meteoparamname[TSS] = "TSS";
static_meteoparamname[HS] = "HS";
static_meteoparamname[RSWR] = "RSWR";
static_meteoparamname[P] = "P";
return true;
}
......@@ -52,15 +51,42 @@ const std::string& MeteoData::getParameterName(const unsigned int& parindex)
if (parindex >= MeteoData::nrOfParameters)
throw IndexOutOfBoundsException("Trying to access meteo parameter that does not exist", AT);
return MeteoData::meteoparamname[parindex];
return MeteoData::static_meteoparamname[parindex];
}
/************************************************************
* non-static section *
************************************************************/
const std::string& MeteoData::getNameForParameter(const unsigned int& parindex) const
{
std::map<unsigned int, std::string>::const_iterator it;
it = meteoparamname.find(parindex);
#ifndef NOSAFECHECKS
if (it == meteoparamname.end())
throw IndexOutOfBoundsException("Trying to get name for parameter that does not exist", AT);
#endif
return it->second;
}
void MeteoData::initParameterMap()
{
//Associate unsigned int value and a string representation of a meteo parameter
meteoparamname[TA] = "TA";
meteoparamname[ISWR] = "ISWR";
meteoparamname[VW] = "VW";
meteoparamname[DW] = "DW";
meteoparamname[RH] = "RH";
meteoparamname[ILWR] = "ILWR";
meteoparamname[HNW] = "HNW";
meteoparamname[TSG] = "TSG";
meteoparamname[TSS] = "TSS";
meteoparamname[HS] = "HS";
meteoparamname[RSWR] = "RSWR";
meteoparamname[P] = "P";
//The following mapping needs to be done for every instance of MeteoData
meteoparam[TA] = &ta;
meteoparam[ISWR] = &iswr;
......@@ -75,9 +101,46 @@ void MeteoData::initParameterMap()
meteoparam[RSWR] = &rswr;
meteoparam[P] = &p;
std::map<std::string, double>::iterator it;
unsigned int counter = lastparam + 1;
for (it=extraparameters.begin(); it!=extraparameters.end(); it++){
meteoparamname[counter] = it->first;
meteoparam[counter] = &(it->second); //points to the address of the element
counter++;
}
nrOfAllParameters = meteoparam.size();
for (unsigned int ii=0; ii<getNrOfParameters(); ii++){
mapParameterByName[getNameForParameter(ii)] = meteoparam[ii];
}
//Check for inconsistency between enum Parameters and the two maps meteoparam and meteoparamname
if ((meteoparam.size() != meteoparamname.size()) || (meteoparam.size() != MeteoData::nrOfParameters))
throw IOException("Inconsistency within class MeteoData: Check function initMaps()", AT);
if ((meteoparam.size() != meteoparamname.size()) || (meteoparam.size() != getNrOfParameters()))
throw IOException("Inconsistency within class MeteoData: Check function initParameterMap()", AT);
}
void MeteoData::addParameter(const std::string& i_paramname)
{
//Check whether name is taken
std::map<std::string, double*>::const_iterator it;
it = mapParameterByName.find(i_paramname);
if (it != mapParameterByName.end())
throw IndexOutOfBoundsException("Trying to add a meteo parameter that already exists: " + i_paramname, AT);
//Add parameter to extraparameters map
extraparameters[i_paramname] = IOUtils::nodata;
meteoparamname[getNrOfParameters()] = i_paramname;
meteoparam[getNrOfParameters()] = &extraparameters[i_paramname];
mapParameterByName[i_paramname] = &extraparameters[i_paramname];
//Increase nrOfAllParameters
nrOfAllParameters++;
}
unsigned int MeteoData::getNrOfParameters()
{
return nrOfAllParameters;
}
MeteoData::MeteoData() : date(0.0), resampled(false)
......@@ -171,7 +234,7 @@ bool MeteoData::operator!=(const MeteoData& in) const
double& MeteoData::param(const unsigned int& parindex)
{
#ifndef NOSAFECHECKS
if (parindex >= MeteoData::nrOfParameters)
if (parindex >= getNrOfParameters())
throw IndexOutOfBoundsException("Trying to access meteo parameter that does not exist", AT);
#endif
return *(meteoparam[parindex]);
......@@ -189,6 +252,30 @@ const double& MeteoData::param(const unsigned int& parindex) const
return *(it->second);
}
double& MeteoData::param(const std::string& parname)
{
std::map<std::string, double*>::iterator it;
it = mapParameterByName.find(parname);
#ifndef NOSAFECHECKS
if (it == mapParameterByName.end())
throw IndexOutOfBoundsException("Trying to access meteo parameter that does not exist", AT);
#endif
return *(it->second);
}
const double& MeteoData::param(const std::string& parname) const
{
std::map<std::string, double*>::const_iterator it;
it = mapParameterByName.find(parname);
#ifndef NOSAFECHECKS
if (it == mapParameterByName.end())
throw IndexOutOfBoundsException("Trying to access meteo parameter that does not exist", AT);
#endif
return *(it->second);
}
std::ostream& operator<<(std::ostream& os, const MeteoData& data) {
os << "<meteo>\n";
......@@ -197,7 +284,7 @@ std::ostream& operator<<(std::ostream& os, const MeteoData& data) {
std::map<unsigned int, double*>::const_iterator it1;
for (it1=data.meteoparam.begin(); it1 != data.meteoparam.end(); it1++){
if( (*it1->second) != IOUtils::nodata ) {
os << setw(7) << MeteoData::getParameterName(it1->first) << ":" << setw(15) << *it1->second << "\n";
os << setw(7) << data.getNameForParameter(it1->first) << ":" << setw(15) << *it1->second << "\n";
}
}
os << "</meteo>\n";
......
......@@ -61,6 +61,7 @@ class MeteoData {
lastparam=P};
static const unsigned int nrOfParameters; ///<holds the number of meteo parameters stored in MeteoData
static const std::string& getParameterName(const unsigned int& parindex);
/**
* @brief The default constructor initializing every double attribute to nodata and the Date to julian==0.0
......@@ -93,6 +94,13 @@ class MeteoData {
*/
void setDate(const Date& _date);
/**
* @brief Add another variable to the MeteoData object,
* a double value will be added and the nrOfParameters increased
* @param i_paramname A parameter name, e.g. "VSWR"
*/
void addParameter(const std::string& i_paramname);
bool isResampled();
void setResampled(const bool&);
......@@ -100,7 +108,9 @@ class MeteoData {
double& param(const unsigned int& parindex);
const double& param(const unsigned int& parindex) const;
static const std::string& getParameterName(const unsigned int& parindex);
double& param(const std::string& parname);
const double& param(const std::string& parname) const;
const std::string& getNameForParameter(const unsigned int& parindex) const;
friend std::ostream& operator<<(std::ostream& os, const MeteoData& data);
......@@ -123,15 +133,21 @@ class MeteoData {
double hs; ///<Snow height in m
double p; ///<Atmospheric pressure in Pa
unsigned int getNrOfParameters();
private:
void initAllParameters();
std::map<unsigned int, double*> meteoparam; ///<Associate an unsigned int with every meteo parameter
static std::map<unsigned int, std::string> meteoparamname; ///<Associate a name with every meteo parameter
static std::map<unsigned int, std::string> static_meteoparamname; ///<Associate a name with meteo parameters in Parameters
static const bool __init; ///<helper variable to enable the init of static collection data
static bool initStaticData();///<initialize the static map meteoparamname
bool resampled; ///<set this to true if MeteoData is result of resampling
unsigned int nrOfAllParameters;
std::map<std::string, double> extraparameters; ///<All non-standard meteo parameters will end up in this map
std::map<std::string, double*> mapParameterByName; ///<Associate name and meteo parameter
std::map<unsigned int, double*> meteoparam; ///<Associate an unsigned int with every meteo parameter
std::map<unsigned int, std::string> meteoparamname; ///<Associate a name with every meteo parameter
void initAllParameters();
void initParameterMap(); ///<initializes the meteoparam map that allows sequential access to meteo parameters
bool resampled; ///<set this to true if MeteoData is result of resampling
};
typedef std::vector<MeteoData> METEO_DATASET;
......
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