modis_utils.cpp
Go to the documentation of this file.
1 // MODIS
2 #include "modis_utils.h"
3 //#include "data_set.h"
4 
5 // Boost
6 #include <boost/algorithm/string/case_conv.hpp>
7 #include <boost/filesystem.hpp>
8 #include <boost/lexical_cast.hpp>
9 #include <boost/tokenizer.hpp>
10 
11 void msearch(const boost::filesystem::path& mpath, std::string product, std::string hv, ProdHVDate& fnames)
12 {
14  {
15 
16 // Take the filename from the full path and check if it starts with MOD
17  std::string filename = mpath.leaf().string();
18  std::string::size_type pos = filename.find("MOD");
19  if (pos != 0) return;
20 
21 // Extract the fields from filename - MOD09A1.A2013201.h11v11.005.2013213171641.hdf
22  boost::char_separator<char> sep(".");
23  boost::tokenizer<boost::char_separator<char> > tokens(filename, sep);
24  boost::tokenizer<boost::char_separator<char> >::iterator beg;
25  beg = tokens.begin();
26  std::string mproduct(*beg);
27  ++beg; if (beg == tokens.end()) return;
28  std::string mdate(*beg);
29  ++beg; if (beg == tokens.end()) return;
30  std::string mhv(*beg);
31 
32  if (!product.empty() && product != mproduct) return;
33  if (!hv.empty() && hv != mhv) return;
34 // std::cout << "File " << mpath.string() << std::endl;
35 // Compose the date in YYYMMDD format
36  int year;
37  int jday;
38  char a;
39  sscanf(mdate.c_str(),"%1c%4d%3d",&a,&year,&jday);
40  if (a!='A') return;
41  boost::gregorian::date d(year,boost::gregorian::Jan,1);
42  boost::gregorian::date_duration dd(jday-1);
43  boost::gregorian::date bdate = d + dd;
44  mdate = to_iso_extended_string(bdate);
45 
46  fnames[mproduct][mhv][mdate] = mpath.string();
47  return;
48  }
49 
50  for (boost::filesystem::directory_iterator it(mpath), itEnd; it !=itEnd; ++it)
51  msearch(*it,product,hv,fnames);
52 }
53 
54 void modis_print(const unsigned char* values,
55  const std::vector<boost::gregorian::date>* times)
56 {
57  std::size_t nvals = times->size();
58 
59  for(std::size_t i = 0; i != nvals; ++i)
60  {
61  std::cout << "date: " << boost::gregorian::to_simple_string((*times)[i])
62  << ", value: " << *((boost::int16_t*)(values)) << std::endl;
63 
64  values += sizeof(boost::int16_t);
65  }
66 }
67 
std::map< std::string, std::map< std::string, std::map< std::string, std::string > > > ProdHVDate
Definition: modis_utils.h:13
static te::dt::DateTime d(2010, 8, 9, 15, 58, 39)
std::complex< double > times(std::complex< double > lhs, std::complex< double > rhs)
void modis_print(const unsigned char *values, const std::vector< boost::gregorian::date > *times)
Definition: modis_utils.cpp:54
static bool isRegularFile(const std::string &path)
Checks if a given path in UTF-8 is a regular file.
Definition: FileSystem.cpp:98
void msearch(const boost::filesystem::path &mpath, std::string product, std::string hv, ProdHVDate &fnames)
Definition: modis_utils.cpp:11