Browse Source

Variable name is now set explicitly in function Read() arguments

interpolate
Michael Uleysky 2 years ago
parent
commit
a4d7e0b950
  1. 19
      include/layereddata.h
  2. 17
      include/odm.h
  3. 15
      src/actiontsc.cpp
  4. 56
      src/layereddata.cpp

19
include/layereddata.h

@ -133,7 +133,6 @@ class LayeredData
struct Parameters: public BaseParameters struct Parameters: public BaseParameters
{ {
size_t xb, yb, xe, ye, layer; size_t xb, yb, xe, ye, layer;
MString varname;
virtual ~Parameters() override = default; virtual ~Parameters() override = default;
}; };
@ -147,7 +146,7 @@ class LayeredData
std::pair<const BaseParameters*, MString> Parameters(michlib_internal::ParameterListEx& pars, const CLArgs& args) const; std::pair<const BaseParameters*, MString> Parameters(michlib_internal::ParameterListEx& pars, const CLArgs& args) const;
Data Read(const BaseParameters* ip, size_t i) const; Data Read(const MString& vname, const BaseParameters* ip, size_t i) const;
bool isOk() const { return nc.size() > 0; } bool isOk() const { return nc.size() > 0; }
@ -184,10 +183,24 @@ class LayeredData
// clang-format on // clang-format on
} }
bool CheckVar(const MString& vname) const
{
if(!HaveVar(vname))
{
bool varexist = false;
if(vname == "temp" && HaveVar("ptemp") && HaveVar("sal")) varexist = true;
if(vname == "ptemp" && HaveVar("temp") && HaveVar("sal")) varexist = true;
if(vname == "pdens" && (HaveVar("ptemp") || HaveVar("temp")) && HaveVar("sal")) varexist = true;
if((vname == "U" || vname == "U2") && HaveVar("u") && HaveVar("v")) varexist = true;
if(!varexist) return false;
}
return true;
}
private: private:
template<class DataType> Data ReadVarRaw(const NC& f, const MString& name, size_t i, bool nodepth, const struct Parameters* p) const; template<class DataType> Data ReadVarRaw(const NC& f, const MString& name, size_t i, bool nodepth, const struct Parameters* p) const;
bool HaveVar(const MString vname) const bool HaveVar(const MString& vname) const
{ {
for(size_t i = 0; i < nc.size(); i++) for(size_t i = 0; i < nc.size(); i++)
{ {

17
include/odm.h

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "BFileW.h" #include "BFileW.h"
#include "NEMO.h"
#include "HYCOM.h" #include "HYCOM.h"
#include "NEMO.h"
#include <utility> #include <utility>
#include <variant> #include <variant>
@ -10,7 +10,7 @@ using michlib::errmessage;
using michlib::GPL; using michlib::GPL;
using michlib::message; using michlib::message;
using DataVariants = std::variant<NEMOData,HYCOMData>; using DataVariants = std::variant<NEMOData, HYCOMData>;
template<class T> template<class T>
concept InfoSupported = requires(T t) { concept InfoSupported = requires(T t) {
@ -27,15 +27,15 @@ concept ParametersSupported = requires(T t, michlib_internal::ParameterListEx& p
}; };
template<class T> template<class T>
concept ReadPSupported = requires(T t, const BaseParameters* ip, size_t i) { concept ReadPSupported = requires(T t, const MString& vname, const BaseParameters* ip, size_t i) {
{ {
t.template Read(ip, i)(0) t.template Read(vname, ip, i)(0)
} -> std::convertible_to<real>; } -> std::convertible_to<real>;
}; };
template<class T> template<class T>
concept ReadSupported = requires(T t, size_t i) { concept ReadSupported = requires(T t, const MString& vname, size_t i) {
{ {
t.template Read(i)(0) t.template Read(vname, i)(0)
} -> std::convertible_to<real>; } -> std::convertible_to<real>;
}; };
@ -133,6 +133,11 @@ class Data: public DataVariants
return std::visit([](const auto& arg) -> auto { return arg.NTimes(); }, *this); return std::visit([](const auto& arg) -> auto { return arg.NTimes(); }, *this);
} }
auto CheckVar(const MString& vname) const
{
return std::visit([&vname = std::as_const(vname)](const auto& arg) -> auto { return arg.CheckVar(vname); }, *this);
}
MString Init(const CLArgs& args) MString Init(const CLArgs& args)
{ {
MString src = args.contains("source") ? args.at("source") : ""; MString src = args.contains("source") ? args.at("source") : "";

15
src/actiontsc.cpp

@ -31,6 +31,11 @@ MString Data::ActionTsc(const CLArgs& args)
pars.SetParameter("timee", te.ToString()); pars.SetParameter("timee", te.ToString());
} }
if(!args.contains("var")) return "Variable not specified";
MString vname = args.at("var");
if(!CheckVar(vname)) return "Variable " + vname + " not exists in this dataset";
pars.SetParameter("variable", vname);
std::unique_ptr<const BaseParameters> sourcepars; std::unique_ptr<const BaseParameters> sourcepars;
{ {
std::pair<const BaseParameters*, MString> ppar = std::visit( std::pair<const BaseParameters*, MString> ppar = std::visit(
@ -49,13 +54,13 @@ MString Data::ActionTsc(const CLArgs& args)
auto p = sourcepars.get(); auto p = sourcepars.get();
size_t ind; size_t ind;
auto read = [p = p, &ind = ind](const auto& arg) -> BaseData auto read = [&vname = std::as_const(vname), p = p, &ind = ind](const auto& arg) -> BaseData
{ {
using T = std::decay_t<decltype(arg)>; using T = std::decay_t<decltype(arg)>;
if constexpr(ParametersSupported<T> && ReadPSupported<T>) if constexpr(ParametersSupported<T> && ReadPSupported<T>)
return arg.Read(p, ind); return arg.Read(vname, p, ind);
else if constexpr(!ParametersSupported<T> && ReadSupported<T>) else if constexpr(!ParametersSupported<T> && ReadSupported<T>)
return arg.Read(ind); return arg.Read(vname, ind);
else else
return BaseData(); return BaseData();
}; };
@ -64,7 +69,7 @@ MString Data::ActionTsc(const CLArgs& args)
if(tindexes.size() == 1) if(tindexes.size() == 1)
{ {
ind = tindexes[0]; ind = tindexes[0];
michlib::message("Time: "+Time(ind).ToTString()); michlib::message("Time: " + Time(ind).ToTString());
data = std::visit(read, *this); data = std::visit(read, *this);
} }
else else
@ -75,7 +80,7 @@ MString Data::ActionTsc(const CLArgs& args)
{ {
if(!ok) break; if(!ok) break;
ind = tindexes[i]; ind = tindexes[i];
michlib::message("Time: "+Time(ind).ToTString()); michlib::message("Time: " + Time(ind).ToTString());
auto dat = std::visit(read, *this); auto dat = std::visit(read, *this);
if(dat) if(dat)
out.Add(dat); out.Add(dat);

56
src/layereddata.cpp

@ -141,17 +141,6 @@ std::pair<const BaseParameters*, MString> LayeredData::Parameters(michlib_intern
{ {
std::unique_ptr<struct Parameters> ppar{new struct Parameters}; std::unique_ptr<struct Parameters> ppar{new struct Parameters};
if(!args.contains("var")) return {nullptr, "Variable not specified"};
ppar->varname = args.at("var");
if(!HaveVar(ppar->varname))
{
bool varexist = false;
if(ppar->varname == "temp" && HaveVar("ptemp") && HaveVar("sal")) varexist = true;
if(ppar->varname == "ptemp" && HaveVar("temp") && HaveVar("sal")) varexist = true;
if(ppar->varname == "pdens" && (HaveVar("ptemp") || HaveVar("temp")) && HaveVar("sal")) varexist = true;
if((ppar->varname == "U" || ppar->varname == "U2") && HaveVar("u") && HaveVar("v")) varexist = true;
if(!varexist) return {nullptr, "Variable " + ppar->varname + " not exists in this dataset"};
}
if(args.contains("layer")) ppar->layer = args.at("layer").ToInteger<size_t>(); if(args.contains("layer")) ppar->layer = args.at("layer").ToInteger<size_t>();
if(!args.contains("depth") && ppar->layer >= NDepths()) return {nullptr, MString("Layer ") + ppar->layer + " is too deep!"}; if(!args.contains("depth") && ppar->layer >= NDepths()) return {nullptr, MString("Layer ") + ppar->layer + " is too deep!"};
real depth = args.contains("depth") ? args.at("depth").ToReal() : Depth(ppar->layer); real depth = args.contains("depth") ? args.at("depth").ToReal() : Depth(ppar->layer);
@ -188,7 +177,6 @@ std::pair<const BaseParameters*, MString> LayeredData::Parameters(michlib_intern
} }
} }
pars.SetParameter("variable", ppar->varname);
pars.SetParameter("depth", Depth(ppar->layer)); pars.SetParameter("depth", Depth(ppar->layer));
pars.SetParameter("layer", ppar->layer); pars.SetParameter("layer", ppar->layer);
pars.SetParameter("dataset", Title()); pars.SetParameter("dataset", Title());
@ -200,23 +188,20 @@ std::pair<const BaseParameters*, MString> LayeredData::Parameters(michlib_intern
return {ppar.release(), ""}; return {ppar.release(), ""};
} }
LayeredData::Data LayeredData::Read(const BaseParameters* ip, size_t i) const LayeredData::Data LayeredData::Read(const MString& vname, const BaseParameters* ip, size_t i) const
{ {
if(!isOk()) return Data(); if(!isOk()) return Data();
bool nodepth = false; bool nodepth = false;
auto p = dynamic_cast<const struct Parameters*>(ip); auto p = dynamic_cast<const struct Parameters*>(ip);
auto [name, id, tid] = VarNameLoc(p->varname, times[i]); auto [name, id, tid] = VarNameLoc(vname, times[i]);
if(!name.Exist()) // Conversion read if(!name.Exist()) // Conversion read
{ {
// ptemp from temp and sal // ptemp from temp and sal
if(p->varname == "ptemp") if(vname == "ptemp")
{ {
struct Parameters params = *p; auto temp = Read("temp", ip, i);
params.varname = "temp"; auto sal = Read("sal", ip, i);
auto temp = Read(&params, i);
params.varname = "sal";
auto sal = Read(&params, i);
if(!(temp && sal)) return Data(); if(!(temp && sal)) return Data();
auto out = temp; auto out = temp;
for(size_t ind = 0; ind < out.N(); ind++) for(size_t ind = 0; ind < out.N(); ind++)
@ -229,13 +214,10 @@ LayeredData::Data LayeredData::Read(const BaseParameters* ip, size_t i) const
return out; return out;
} }
// temp from ptemp and sal // temp from ptemp and sal
if(p->varname == "temp") if(vname == "temp")
{ {
struct Parameters params = *p; auto temp = Read("ptemp", ip, i);
params.varname = "ptemp"; auto sal = Read("sal", ip, i);
auto temp = Read(&params, i);
params.varname = "sal";
auto sal = Read(&params, i);
if(!(temp && sal)) return Data(); if(!(temp && sal)) return Data();
auto out = temp; auto out = temp;
for(size_t ind = 0; ind < out.N(); ind++) for(size_t ind = 0; ind < out.N(); ind++)
@ -248,14 +230,11 @@ LayeredData::Data LayeredData::Read(const BaseParameters* ip, size_t i) const
return out; return out;
} }
// pdens from temp and sal // pdens from temp and sal
if(p->varname == "pdens") if(vname == "pdens")
{ {
struct Parameters params = *p;
bool tempispot = HaveVar("ptemp"); bool tempispot = HaveVar("ptemp");
params.varname = tempispot ? "ptemp" : "temp"; auto temp = Read(tempispot ? "ptemp" : "temp", ip, i);
auto temp = Read(&params, i); auto sal = Read("sal", ip, i);
params.varname = "sal";
auto sal = Read(&params, i);
if(!(temp && sal)) return Data(); if(!(temp && sal)) return Data();
auto out = temp; auto out = temp;
for(size_t ind = 0; ind < out.N(); ind++) for(size_t ind = 0; ind < out.N(); ind++)
@ -269,14 +248,11 @@ LayeredData::Data LayeredData::Read(const BaseParameters* ip, size_t i) const
return out; return out;
} }
// U and U2 from u and v // U and U2 from u and v
if(p->varname == "U" || p->varname == "U2") if(vname == "U" || vname == "U2")
{ {
bool square = p->varname == "U2"; bool square = vname == "U2";
struct Parameters params = *p; auto u = Read("u", ip, i);
params.varname = "u"; auto v = Read("v", ip, i);
auto u = Read(&params, i);
params.varname = "v";
auto v = Read(&params, i);
if(!(u && v)) return Data(); if(!(u && v)) return Data();
auto out = u; auto out = u;
for(size_t ind = 0; ind < out.N(); ind++) for(size_t ind = 0; ind < out.N(); ind++)

Loading…
Cancel
Save