Commit 18d3760df8ff9e9e6c6f975664f5d893b9e1eec1

Authored by Jordi Inglada
1 parent 53ceef72
Exists in temperature

ENH: add the possibility of using temperature files

Showing 1 changed file with 44 additions and 3 deletions   Show diff stats
src/Applications/sigmo-maiseo.cxx
... ... @@ -22,6 +22,30 @@
22 22 #include <regex>
23 23  
24 24 namespace pheno{
  25 +std::pair<DateVector, std::vector<unsigned int>> parse_temperature_file(const std::string& fname)
  26 +{
  27 + DateVector dates;
  28 + std::vector<unsigned int> temps;
  29 +
  30 + std::ifstream temp_file(fname);
  31 + if(!temp_file)
  32 + itkGenericExceptionMacro(<< "Could not open file " << fname << "\n");
  33 +
  34 + while(!temp_file.eof())
  35 + {
  36 + std::string line;
  37 + std::getline(temp_file, line);
  38 + if(line!="")
  39 + {
  40 + auto tokens = string_split(line, " ");
  41 + auto date = make_date(tokens[0]);
  42 + auto temp = static_cast<unsigned int>(std::stod(tokens[1]));
  43 + dates.push_back(date);
  44 + temps.push_back(temp);
  45 + }
  46 + }
  47 + return std::make_pair(dates, temps);
  48 +}
25 49 std::tuple<DateVector, std::vector<long int>, std::vector<VectorType> > get_csv_profiles(const std::string& fname)
26 50 {
27 51 /* parcelle;mean_2013-02-16;mean_2013-02-17;mean_2013-02-21;mean_2013-02-22;mean_2013-02-27;mean_2013-03-03;mean_2013-03-04;mean_2013-03-08;mean_2013-03-14;mean_2013-03-18;mean_2013-03-19;mean_2013-03-23;mean_2013-03-24;mean_2013-03-29;mean_2013-04-03;mean_2013-04-12;mean_2013-04-13;mean_2013-04-14;mean_2013-04-17;mean_2013-04-22;mean_2013-04-23;mean_2013-05-17;mean_2013-05-23;mean_2013-05-27;mean_2013-06-06;mean_2013-06-07;mean_2013-06-11;mean_2013-06-12;mean_2013-06-16;mean_2013-07-19;mean_2013-08-04;mean_2013-08-20;mean_2013-09-05;mean_2013-10-07;mean_2013-10-23;mean_2013-12-10*/
... ... @@ -75,21 +99,36 @@ std::tuple&lt;DateVector, std::vector&lt;long int&gt;, std::vector&lt;VectorType&gt; &gt; get_csv_
75 99  
76 100 int main(int argc, char* argv[])
77 101 {
78   - if(argc!=3)
  102 + if(argc!=3 && argc!=4)
79 103 {
80   - std::cerr << "Usage: " << argv[0] << " filename output-prefix\n";
  104 + std::cerr << "Usage: " << argv[0]
  105 + << " filename output-prefix [temperature-filename]\n";
81 106 exit(1);
82 107 }
83 108 auto parse_result = pheno::get_csv_profiles(argv[1]);
84 109 auto dates = std::get<0>(parse_result);
85 110 auto ids = std::get<1>(parse_result);
86 111 auto profiles = std::get<2>(parse_result);
87   - std::vector<int> doys;
  112 + std::vector<unsigned int> doys;
88 113 for(auto d : dates)
89 114 doys.push_back(pheno::doy(d));
90 115 std::cout << "There are " << dates.size() << " dates and "
91 116 << profiles.size() << " profiles.\n";
92 117  
  118 + //parse temperature file
  119 + if(argc==4)
  120 + {
  121 + auto temp_result = pheno::parse_temperature_file(argv[3]);
  122 + auto tmp_dates = std::get<0>(temp_result);
  123 + auto temps = std::get<1>(temp_result);
  124 + if(tmp_dates.size()!=dates.size())
  125 + {
  126 + std::cout << "Dates in CSV file and temperature file must coincide"
  127 + << std::endl;
  128 + exit(1);
  129 + }
  130 + std::copy(temps.begin(), temps.end(), doys.begin());
  131 + }
93 132 for(auto pfid=0; pfid<profiles.size(); ++pfid)
94 133 {
95 134 std::cout << pfid << " ... ";
... ... @@ -97,6 +136,8 @@ int main(int argc, char* argv[])
97 136 auto vec = profiles[pfid];
98 137 auto pred = [=](int e){return !(std::isnan(vec[e]));};
99 138 auto f_profiles = pheno::filter_profile(vec, dates, pred);
  139 + if(argc==4)
  140 + f_profiles = pheno::filter_profile(vec, doys, pred);
100 141 decltype(vec) profile=f_profiles.first;
101 142 decltype(vec) t=f_profiles.second;
102 143 if(profile.size()>5)
... ...