Browse Source

Common class for NCSimple and Zarr

lintest
Michael Uleysky 6 months ago
parent
commit
9a38bdb28b
  1. 4
      include/layereddataz.h
  2. 15
      include/ncfuncs.h
  3. 148
      include/nczarr.h
  4. 20
      include/nczarrcommon.h
  5. 2
      src/layereddataz.cpp
  6. 16
      src/ncfuncs.cpp

4
include/layereddataz.h

@ -1,8 +1,8 @@
#pragma once
#include "gsw.h"
#include "ncfuncs.h"
#include "nczarr.h"
#include "simple2ddata.h"
#include "zarr.h"
#include <memory>
using michlib::Ceil;
@ -17,7 +17,7 @@ class LayeredDataZ: public NCFuncs
using Data = Simple2DData;
private:
class NC: public Zarr
class NC: public NCZarr
{
std::vector<MDateTime> times;

15
include/ncfuncs.h

@ -2,8 +2,7 @@
#include "DataAdapters/ncfilealt.h"
#include "basedata.h"
#include "mdatetime.h"
#include "ncsimple.h"
#include "zarr.h"
#include "nczarr.h"
#include <map>
#include <set>
#include <tuple>
@ -30,10 +29,10 @@ class NCFuncs
static CoordNames GetDNames(const NCFileA& nc);
static bool HaveVar(const NCFileA& nc, const MString& vname);
template<class NcZarrFunctions> static void GetVars(const NcZarrRead<NcZarrFunctions>& nc, std::set<MString>& vars);
template<class NcZarrFunctions> static CoordNames GetCNames(const NcZarrRead<NcZarrFunctions>& nc);
template<class NcZarrFunctions> static CoordNames GetDNames(const NcZarrRead<NcZarrFunctions>& nc);
template<class NcZarrFunctions> static bool HaveVar(const NcZarrRead<NcZarrFunctions>& nc, const MString& vname);
static void GetVars(const NCZarr& nc, std::set<MString>& vars);
static CoordNames GetCNames(const NCZarr& nc);
static CoordNames GetDNames(const NCZarr& nc);
static bool HaveVar(const NCZarr& nc, const MString& vname);
template<class HV> static VarPresence CheckVar(const MString& vname, HV hv)
{
@ -53,9 +52,7 @@ class NCFuncs
template<class D> static bool TransformationRead(const D* data, const MString& vname, std::map<MString, typename D::Data>& cache, const BaseParameters* ip, size_t i)
{
if constexpr(requires(const D* d, const BaseParameters* p) {
{
d->Depth(p)
} -> std::convertible_to<real>;
{ d->Depth(p) } -> std::convertible_to<real>;
})
{
real depth = data->Depth(ip);

148
include/nczarr.h

@ -0,0 +1,148 @@
#pragma once
#include "ncsimple.h"
#include "zarr.h"
#include <variant>
using NCZarrBase = std::variant<Zarr, NCSimple>;
#define DEFFUNC(NAME) \
auto NAME() const \
{ \
return V([](const auto& arg) { return arg.NAME(); }); \
}
#define DEFFUNC1(NAME) \
auto NAME(const MString& v) const \
{ \
return V([&v = std::as_const(v)](const auto& arg) { return arg.NAME(v); }); \
}
#define DEFFUNC2(NAME) \
auto NAME(const MString& v1, const MString& v2) const \
{ \
return V([&v1 = std::as_const(v1), &v2 = std::as_const(v2)](const auto& arg) { return arg.NAME(v1, v2); }); \
}
class NCZarr: private NCZarrBase, private DimReqDef
{
template<class Visitor> auto V(Visitor&& visitor) const { return std::visit(std::forward<Visitor>(visitor), *static_cast<const NCZarrBase*>(this)); }
public:
explicit operator bool() const
{
return V([](const auto& arg) { return static_cast<bool>(arg); });
}
DEFFUNC(NDim)
DEFFUNC1(NDim)
DEFFUNC(NAtt)
DEFFUNC1(NAtt)
DEFFUNC(AttNames)
DEFFUNC1(AttNames)
DEFFUNC(VarNames)
DEFFUNC1(VarT)
DEFFUNC1(VarFill)
DEFFUNC(DimNames)
DEFFUNC1(DimNames)
DEFFUNC1(DimSize)
DEFFUNC2(AttT)
DEFFUNC2(AttInt)
DEFFUNC2(AttUInt)
DEFFUNC2(AttReal)
DEFFUNC2(AttString)
DEFFUNC2(AttBool)
DEFFUNC1(AttT)
DEFFUNC1(AttInt)
DEFFUNC1(AttUInt)
DEFFUNC1(AttReal)
DEFFUNC1(AttString)
DEFFUNC1(AttBool)
DEFFUNC1(HasDim)
DEFFUNC1(HasVar)
DEFFUNC1(HasAtt)
DEFFUNC2(HasAtt)
DEFFUNC(Vars)
DEFFUNC(Dims)
template<class Data, class Transform> Error Read(const MString& vname, Data& data, Transform transform, const char* request) const
{
return V([&vname = std::as_const(vname), &data = data, &transform = transform, request = request](const auto& arg) { return arg.Read(vname, data, transform, request); });
}
template<class Data, class Transform> Error Read(const MString& vname, Data& data, Transform transform, DimReq&& req1) const
{
return V([&vname = std::as_const(vname), &data = data, &transform = transform, &req1 = req1](const auto& arg) { return arg.Read(vname, data, transform, std::move(req1)); });
}
template<class Data, class Transform> Error Read(const MString& vname, Data& data, Transform transform, DimReq&& req1, DimReq&& req2) const
{
return V([&vname = std::as_const(vname), &data = data, &transform = transform, &req1 = req1, &req2 = req2](const auto& arg)
{ return arg.Read(vname, data, transform, std::move(req1), std::move(req2)); });
}
template<class Data, class Transform>
Error Read(const MString& vname, Data& data, Transform transform, DimReq&& req1, DimReq&& req2, DimReq&& req3) const
{
return V([&vname = std::as_const(vname), &data = data, &transform = transform, &req1 = req1, &req2 = req2, &req3 = req3](const auto& arg)
{ return arg.Read(vname, data, transform, std::move(req1), std::move(req2), std::move(req3)); });
}
template<class Data, class Transform>
Error Read(const MString& vname, Data& data, Transform transform, DimReq&& req1, DimReq&& req2, DimReq&& req3, DimReq&& req4) const
{
return V([&vname = std::as_const(vname), &data = data, &transform = transform, &req1 = req1, &req2 = req2, &req3 = req3, &req4 = req4](const auto& arg)
{ return arg.Read(vname, data, transform, std::move(req1), std::move(req2), std::move(req3), std::move(req4)); });
}
template<class Data, class Transform> Error Read(const MString& vname, Data& data, Transform transform) const
{
return V([&vname = std::as_const(vname), &data = data, &transform = transform](const auto& arg) { return arg.Read(vname, data, transform); });
}
template<class Data, class Transform> Error Read(const MString& vname, Data& data, Transform transform, const std::vector<DimReqDef::DimReq>& reqs) const
{
return V([&vname = std::as_const(vname), &data = data, &transform = transform, &reqs = std::as_const(reqs)](const auto& arg) { return arg.Read(vname, data, transform, reqs); });
}
template<class Data, class Transform> Error Read(const MString& vname, Data& data, Transform transform, const MString& request) const
{
return V([&vname = std::as_const(vname), &data = data, &transform = transform, &request = std::as_const(request)](const auto& arg)
{ return arg.Read(vname, data, transform, request); });
}
template<class Type> Error Read(const MString& vname, std::vector<Type>& out) const
{
return V([&vname = std::as_const(vname), &out = out](const auto& arg) { return arg.Read(vname, out); });
}
Error OpenNC(const MString& filename)
{
auto pv = static_cast<NCZarrBase*>(this);
*pv = NCZarrBase(NCSimple());
auto p = std::get_if<NCSimple>(pv);
if(p == nullptr) return Error("NCZarr::OpenNC", "Impossible error!");
return p->Open(filename);
}
Error OpenZarr(const MString& product, const MString& dataset, bool time = true)
{
auto pv = static_cast<NCZarrBase*>(this);
*pv = NCZarrBase(Zarr());
auto p = std::get_if<Zarr>(pv);
if(p == nullptr) return Error("NCZarr::OpenNC", "Impossible error!");
return p->Open(product, dataset, time);
}
};
#undef DEFFUNC
#undef DEFFUNC1
#undef DEFFUNC2

20
include/nczarrcommon.h

@ -379,7 +379,7 @@ class NcZarrTypes
std::vector<Variable> vars;
public:
operator bool() const { return !vars.empty(); }
explicit operator bool() const { return !vars.empty(); }
size_t NDim() const { return dims.size(); }
@ -603,8 +603,6 @@ template<class C> class NcZarrRead: public C, public DimReqDef
template<class VType, class Data, class Transform>
Error Read(const MString& vname, const std::vector<size_t>& transindex, Data& data, Transform transform, std::vector<DimReq> reqs) const
{
size_t nval = 1;
for(const auto& r: reqs) nval *= r.count;
const size_t indim = reqs.size();
constexpr size_t outdim = Dimensionity<Data>();
@ -633,9 +631,7 @@ template<class C> class NcZarrRead: public C, public DimReqDef
C::VarFill(vname));
if constexpr(requires(Data& d) { // Data have own fillvalue
{
d.Fillval()
} -> std::convertible_to<DataType>;
{ d.Fillval() } -> std::convertible_to<DataType>;
})
fillout = data.Fillval();
else // Data does'nt have own fillvalue, using variable fillvalue
@ -717,16 +713,13 @@ template<class C> class NcZarrRead: public C, public DimReqDef
std::vector<struct DimReq> pdims;
const auto vdims = C::DimNames(vname);
std::transform(
vdims.cbegin(), vdims.cend(), std::back_inserter(pdims), [this](const MString& n) -> struct DimReq {
return {n, 0, C::DimSize(n)};
});
std::transform(vdims.cbegin(), vdims.cend(), std::back_inserter(pdims), [this](const MString& n) -> struct DimReq { return {n, 0, C::DimSize(n)}; });
return Read(vname, data, transform, pdims);
}
// Base function for all Read's
template<class Data, class Transform> Error Read(const MString& vname, Data& data, Transform transform, std::vector<DimReq> reqs) const
template<class Data, class Transform> Error Read(const MString& vname, Data& data, Transform transform, const std::vector<DimReq>& reqs) const
{
static const MString pref = "NcZarrRead::Read";
@ -735,10 +728,7 @@ template<class C> class NcZarrRead: public C, public DimReqDef
std::vector<struct DimReq> pdims;
{
const auto vdims = C::DimNames(vname);
std::transform(
vdims.cbegin(), vdims.cend(), std::back_inserter(pdims), [](const MString& n) -> struct DimReq {
return {n, 0, 1};
});
std::transform(vdims.cbegin(), vdims.cend(), std::back_inserter(pdims), [](const MString& n) -> struct DimReq { return {n, 0, 1}; });
}
std::vector<size_t> transindex;

2
src/layereddataz.cpp

@ -59,7 +59,7 @@ MString LayeredDataZ::Open(const MString& dataset)
nc.emplace_back();
{
auto ret = nc.back().Open(product, dataset);
auto ret = nc.back().OpenZarr(product, dataset);
if(!ret)
{
nc.clear();

16
src/ncfuncs.cpp

@ -31,7 +31,7 @@ NCFuncs::CoordNames NCFuncs::GetDNames(const NCFileA& nc)
return out;
}
template<class NcZarrFunctions> NCFuncs::CoordNames NCFuncs::GetDNames(const NcZarrRead<NcZarrFunctions>& nc)
NCFuncs::CoordNames NCFuncs::GetDNames(const NCZarr& nc)
{
CoordNames out;
for(const auto& dim: nc.Dims())
@ -60,8 +60,6 @@ template<class NcZarrFunctions> NCFuncs::CoordNames NCFuncs::GetDNames(const NcZ
return out;
}
template NCFuncs::CoordNames NCFuncs::GetDNames<ZarrFunctions>(const NcZarrRead<ZarrFunctions>&);
NCFuncs::CoordNames NCFuncs::GetCNames(const NCFileA& nc)
{
CoordNames out;
@ -95,7 +93,7 @@ NCFuncs::CoordNames NCFuncs::GetCNames(const NCFileA& nc)
return out;
}
template<class NcZarrFunctions> NCFuncs::CoordNames NCFuncs::GetCNames(const NcZarrRead<NcZarrFunctions>& nc)
NCFuncs::CoordNames NCFuncs::GetCNames(const NCZarr& nc)
{
CoordNames out;
for(const auto& v: nc.Vars()) // Try to define coordinates by attribute standard_name or attribute axis
@ -133,8 +131,6 @@ template<class NcZarrFunctions> NCFuncs::CoordNames NCFuncs::GetCNames(const NcZ
return out;
}
template NCFuncs::CoordNames NCFuncs::GetCNames<ZarrFunctions>(const NcZarrRead<ZarrFunctions>&);
void NCFuncs::GetVars(const NCFileA& nc, std::set<MString>& vars)
{
auto head = nc.Header();
@ -154,7 +150,7 @@ void NCFuncs::GetVars(const NCFileA& nc, std::set<MString>& vars)
if(vars.contains("ssh")) vars.emplace("vgeo");
}
template<class NcZarrFunctions> void NCFuncs::GetVars(const NcZarrRead<NcZarrFunctions>& nc, std::set<MString>& vars)
void NCFuncs::GetVars(const NCZarr& nc, std::set<MString>& vars)
{
for(const auto& v: nc.Vars())
{
@ -172,8 +168,6 @@ template<class NcZarrFunctions> void NCFuncs::GetVars(const NcZarrRead<NcZarrFun
if(vars.contains("ssh")) vars.emplace("vgeo");
}
template void NCFuncs::GetVars<ZarrFunctions>(const NcZarrRead<ZarrFunctions>&, std::set<MString>&);
std::tuple<MDateTime, time_t, bool> NCFuncs::Refdate(const MString& refdate)
{
MDateTime out;
@ -278,7 +272,7 @@ bool NCFuncs::HaveVar(const NCFileA& nc, const MString& vname)
return false;
}
template<class NcZarrFunctions> bool NCFuncs::HaveVar(const NcZarrRead<NcZarrFunctions>& nc, const MString& vname)
bool NCFuncs::HaveVar(const NCZarr& nc, const MString& vname)
{
for(const auto& v: nc.Vars())
{
@ -288,5 +282,3 @@ template<class NcZarrFunctions> bool NCFuncs::HaveVar(const NcZarrRead<NcZarrFun
}
return false;
}
template bool NCFuncs::HaveVar<ZarrFunctions>(const NcZarrRead<ZarrFunctions>&, const MString&);

Loading…
Cancel
Save