Skip to content
Snippets Groups Projects
Commit 7da18a6f authored by Jordi Inglada's avatar Jordi Inglada
Browse files

REFAC: move methods to cxx

parent 055be5ad
No related branches found
No related tags found
No related merge requests found
...@@ -22,18 +22,14 @@ ...@@ -22,18 +22,14 @@
#ifndef otbSoilDataBase_h #ifndef otbSoilDataBase_h
#define otbSoilDataBase_h #define otbSoilDataBase_h
#include "OTBSimulationExport.h"
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <boost/algorithm/string.hpp>
#include "itkMacro.h"
namespace otb namespace otb
{ {
class SoilDataBase class OTBSimulation_EXPORT SoilDataBase
{ {
public: public:
//wavelength in nm //wavelength in nm
...@@ -41,96 +37,13 @@ public: ...@@ -41,96 +37,13 @@ public:
using SoilData = std::unordered_map<WavelenghtType, double>; using SoilData = std::unordered_map<WavelenghtType, double>;
using SoilDataVector = std::vector<SoilData>; using SoilDataVector = std::vector<SoilData>;
SoilDataBase(const std::string& SoilFileName, double wlfactor) : SoilDataBase(const std::string& SoilFileName, double wlfactor);
m_SoilFileName(SoilFileName), m_WlFactor(wlfactor) const SoilDataVector& GetDB() const;
{ double GetReflectance(size_t SoilIndex, WavelenghtType wl) const;
ParseSoilFile();
};
const SoilDataVector& GetDB() const
{
return m_SoilDataVector;
}
double GetReflectance(size_t SoilIndex, WavelenghtType wl)
{
assert(SoilIndex<m_SoilDataVector.size());
// wl not in the set of measured ones
if(m_SoilDataVector[SoilIndex].find(wl)==m_SoilDataVector[SoilIndex].end())
{
const auto wlmin = m_Wavelengths[0];
const auto wlmax = m_Wavelengths[m_Wavelengths.size()-1];
if(wl<wlmin) return m_SoilDataVector[SoilIndex][wlmin];
if(wl>wlmax) return m_SoilDataVector[SoilIndex][wlmax];
const auto p = std::partition_point(m_Wavelengths.cbegin(), m_Wavelengths.cend(),
[&](WavelenghtType w){ return w<wl;}
);
const auto wlinf = *(p-1);
const auto wlsup = *p;
const auto factinf = wl-wlinf;
const auto factsup = wlsup-wl;
return (m_SoilDataVector[SoilIndex][wlinf]*factinf
+m_SoilDataVector[SoilIndex][wlsup]*factsup)/(factinf+factsup);
}
else
{
return m_SoilDataVector[SoilIndex][wl];
}
}
protected: protected:
size_t countColumns(std::string fileName) size_t countColumns(std::string fileName) const;
{ void ParseSoilFile();
std::ifstream ifile(fileName.c_str());
std::string line;
if (ifile.is_open())
{
size_t nbSpaces = 0;
getline(ifile,line);
ifile.close();
boost::trim(line);
auto found = line.find(' ');
while(found!=std::string::npos)
{
++nbSpaces;
while(line[found+1] == ' ') ++found;
found = line.find(' ', found+1);
}
return nbSpaces+1;
}
else
{
itkGenericExceptionMacro(<< "Could not open file " << fileName);
}
}
void ParseSoilFile()
{
unsigned int number_of_soils = countColumns(m_SoilFileName) - 1;
m_SoilDataVector.resize(number_of_soils);
std::ifstream sdb(m_SoilFileName);
std::string line;
while(sdb.good())
{
std::getline(sdb, line);
if(line.size() > 3)
{
std::stringstream ss(line);
double tmpwl;
ss >> tmpwl;
WavelenghtType wl = static_cast<WavelenghtType>(m_WlFactor*tmpwl);
for(size_t i=0; i< number_of_soils; ++i)
{
if(i==0)
m_Wavelengths.push_back(wl);
double refl;
ss >> refl;
m_SoilDataVector[i][wl] = refl;
}
}
}
std::sort(m_Wavelengths.begin(), m_Wavelengths.end());
}
std::string m_SoilFileName; std::string m_SoilFileName;
double m_WlFactor; double m_WlFactor;
......
...@@ -35,20 +35,6 @@ int otbSoilDataBaseParseFile(int argc, char * argv[]) ...@@ -35,20 +35,6 @@ int otbSoilDataBaseParseFile(int argc, char * argv[])
otb::SoilDataBase sdb(sfn, 1000); otb::SoilDataBase sdb(sfn, 1000);
auto db = sdb.GetDB(); auto db = sdb.GetDB();
// auto wlmin = static_cast<unsigned int>(std::stoi(argv[2]));
// auto wlmax = static_cast<unsigned int>(std::stoi(argv[3]));
// auto wlstep = static_cast<unsigned int>(std::stoi(argv[4]));
// for(unsigned int wl=wlmin; wl<=wlmax; wl+=wlstep)
// {
// std::cout << wl << " ";
// for(size_t i=0; i<db.size(); ++i)
// {
// std::cout << db[i][wl] << " ";
// }
// std::cout << '\n';
// }
for(unsigned int i=5; i<static_cast<unsigned int>(argc); i+=3) for(unsigned int i=5; i<static_cast<unsigned int>(argc); i+=3)
{ {
auto wltest = std::stoi(argv[i]); auto wltest = std::stoi(argv[i]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment