diff --git a/include/actiondep.h b/include/actiondep.h index 50e2db5..27fc75d 100644 --- a/include/actiondep.h +++ b/include/actiondep.h @@ -5,6 +5,8 @@ #include "mregex.h" #include "traits.h" #include "uvdata.h" +#include +#include using michlib::MDateTime; @@ -21,6 +23,41 @@ using michlib::MDateTime; }; #endif +struct TimeData +{ + static constexpr auto stepunits = std::to_array({3600 * 24, 3600, 60, 1}); + static constexpr std::array stepnames{"days", "hours", "minutes", "seconds"}; + + MDateTime refdate; + size_t unitind; + std::vector steps; + + template TimeData(const D& data, const TIndex& tindexes): refdate(), unitind(stepunits.size()), steps(tindexes.size()) + { + if(steps.size() == 0) return; + refdate = data.Time(tindexes[0]); + unitind = 0; + + for(size_t i = 0; i < steps.size(); i++) + { + auto delta = data.Time(tindexes[i]) - refdate; + while(delta % stepunits[unitind] != 0) unitind++; + } + + for(size_t i = 0; i < steps.size(); i++) + { + auto delta = data.Time(tindexes[i]) - refdate; + steps[i] = michlib::int_cast(delta / stepunits[unitind]); + } + } + + MString UnitName() const + { + MString out = stepnames[unitind].c_str(); + return out + " since " + refdate.ToString(); + } +}; + template size_t GetTIndex(const D& data, const MDateTime& t) { size_t nt = data.NTimes(); @@ -98,27 +135,33 @@ template std::pair GetTIndexes(const D& data, const CL return {tindexes, ""}; } +template std::vector> Read(const D& data, const std::vector& vnames, const BaseParameters* p, size_t ind) +{ + using RT = ReadType; + std::vector out; + michlib::message("Time: " + data.Time(ind).ToTString()); + std::map cache; + for(const auto& vname: vnames) + { + bool res; + if constexpr(ReadPSupported) + res = data.Read(vname, cache, p, ind); + else if constexpr(ReadSupported) + res = data.Read(vname, cache, ind); + if(!res) return out; + } + for(size_t i = 0; i < vnames.size(); i++) out.emplace_back(std::move(cache[vnames[i]])); + + return out; +} + template std::vector> Read(const D& data, const std::vector& vnames, const BaseParameters* p, const TIndex& tindex) { using RT = ReadType; size_t ind; std::vector out; if(tindex.size() == 1) - { - ind = tindex[0]; - michlib::message("Time: " + data.Time(ind).ToTString()); - std::map cache; - for(const auto& vname: vnames) - { - bool res; - if constexpr(ReadPSupported) - res = data.Read(vname, cache, p, ind); - else if constexpr(ReadSupported) - res = data.Read(vname, cache, ind); - if(!res) return out; - } - for(size_t i = 0; i < vnames.size(); i++) out.emplace_back(std::move(cache[vnames[i]])); - } + return Read(data, vnames, p, tindex[0]); else { std::vector> aver(vnames.size());