modis_database.cpp
Go to the documentation of this file.
1 #include "modis_database.h"
2 
3 void MSearch(const boost::filesystem::path& mpath, std::vector<std::string>& fnames)
4 {
6  {
7  std::string aux = mpath.leaf().string();
8  unsigned pos = aux.find("MOD");
9  if (pos==0)
10  {
11  aux = mpath.string();
12  fnames.push_back(aux);
13  }
14  return;
15  }
16 
17  for (boost::filesystem::directory_iterator it(mpath), itEnd; it !=itEnd; ++it)
18  MSearch(*it,fnames);
19 }
20 
21 MODISDatabase::MODISDatabase(std::string rootpath)
22 {
23  std::vector<std::string> modis_files;
24  boost::filesystem::path mpath(rootpath);
25 
26  MSearch(mpath,modis_files);
27 
28  boost::regex efile("([[:alnum:]]+)\.A([[:digit:]]{4})([[:digit:]]{3})\.h([[:digit:]]{1,3})v([[:digit:]]{1,3})\.([[:alnum:]]+)\.([[:alnum:]]+)\.hdf");
29  boost::match_results<std::string::const_iterator> presult;
30 
31  BOOST_FOREACH(std::string& mfile, modis_files)
32  {
33  boost::filesystem::path fpath(mfile);
34  std::string filename = fpath.leaf().string();
35 
36  if (boost::regex_search(filename, presult, efile))
37  {
38  MODISRecord rec;
39  rec.filen = filename;
40  rec.product = presult[1];
41  rec.year = boost::lexical_cast<unsigned int>(presult[2]);
42  rec.day = boost::lexical_cast<unsigned int>(presult[3]);
43  rec.h = boost::lexical_cast<unsigned int>(presult[4]);
44  rec.v = boost::lexical_cast<unsigned int>(presult[5]);
45  m_db.insert(rec);
46  }
47  }
48 }
49 
50 size_t MODISDatabase::size() const
51 {
52  return m_db.size();
53 }
54 
56 {
57  m_db.clear();
58 }
59 
61 {
62  MODISDb::const_iterator it = m_db.begin();
63  while (it != m_db.end())
64  {
65  MODISRecord rec(*it);
66  rec.printToCout();
67  ++it;
68  }
69 }
70 
71 MODISRecord MODISDatabase::findByFile(const std::string& filename) const
72 {
73  boost::multi_index::nth_index<MODISDb,0>::type::iterator it = boost::multi_index::get<0>(m_db).find(filename);
74  if (it == boost::multi_index::get<0>(m_db).end())
75  return MODISRecord();
76 
77  MODISRecord result(*it);
78  return result;
79 }
80 
81 std::vector<MODISRecord> MODISDatabase::findByProduct(const std::string& product) const
82 {
83  std::vector<MODISRecord> result;
84 
85  boost::multi_index::nth_index<MODISDb,1>::type::iterator it_l = boost::multi_index::get<1>(m_db).lower_bound(product);
86  boost::multi_index::nth_index<MODISDb,1>::type::iterator it_u = boost::multi_index::get<1>(m_db).upper_bound(product);
87  while (it_l != it_u)
88  {
89  result.push_back(*it_l);
90  ++it_l;
91  }
92 
93  return result;
94 }
95 
96 std::vector<MODISRecord> MODISDatabase::findByYear(unsigned int year) const
97 {
98  std::vector<MODISRecord> result;
99 
100  boost::multi_index::nth_index<MODISDb,2>::type::iterator it_l = boost::multi_index::get<2>(m_db).lower_bound(year);
101  boost::multi_index::nth_index<MODISDb,2>::type::iterator it_u = boost::multi_index::get<2>(m_db).upper_bound(year);
102  while (it_l != it_u)
103  {
104  result.push_back(*it_l);
105  ++it_l;
106  }
107 
108  return result;
109 }
110 
111 std::vector<MODISRecord> MODISDatabase::findByDay(unsigned int day) const
112 {
113  std::vector<MODISRecord> result;
114 
115  boost::multi_index::nth_index<MODISDb,3>::type::iterator it_l = boost::multi_index::get<3>(m_db).lower_bound(day);
116  boost::multi_index::nth_index<MODISDb,3>::type::iterator it_u = boost::multi_index::get<3>(m_db).upper_bound(day);
117  while (it_l != it_u)
118  {
119  result.push_back(*it_l);
120  ++it_l;
121  }
122 
123  return result;
124 }
std::string product
unsigned int v
std::string filen
std::vector< MODISRecord > findByDay(unsigned int day) const
void MSearch(const boost::filesystem::path &mpath, std::vector< std::string > &fnames)
void printToCout()
MODISRecord findByFile(const std::string &filename) const
unsigned int year
MODISDatabase(std::string rootpath)
std::vector< MODISRecord > findByYear(unsigned int year) const
std::vector< MODISRecord > findByProduct(const std::string &product) const
unsigned int h
unsigned int day
static bool isRegularFile(const std::string &path)
Checks if a given path in UTF-8 is a regular file.
Definition: FileSystem.cpp:98
size_t size() const