|
|
|
@ -84,7 +84,7 @@ class NCFileWBase
|
|
|
|
|
public: |
|
|
|
|
constexpr Error(): err(NC_NOERR) {} |
|
|
|
|
constexpr Error(int n_err): err(n_err) {} |
|
|
|
|
explicit operator bool() const { return err == NC_NOERR; } |
|
|
|
|
explicit operator bool() const { return err == NC_NOERR; } |
|
|
|
|
const char* ErrMessage() const { return nc_strerror(err); } |
|
|
|
|
int Err() const { return err; } |
|
|
|
|
bool IsErr() const { return err != NC_NOERR; } |
|
|
|
@ -100,7 +100,7 @@ class NCFileWBase
|
|
|
|
|
const char* ErrMessage() const { return err.ErrMessage(); } |
|
|
|
|
|
|
|
|
|
explicit operator bool() const { return Ok(); } |
|
|
|
|
bool Ok() const { return opened && !err.IsErr(); } |
|
|
|
|
bool Ok() const { return opened && !err.IsErr(); } |
|
|
|
|
operator int() const { return ncid; } |
|
|
|
|
|
|
|
|
|
void Close() |
|
|
|
@ -124,11 +124,91 @@ class NCFileWBase
|
|
|
|
|
err.Reset(nc_def_dim(ncid, name.Buf(), len, &dimid)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
MString AddAtts(const michlib_internal::ParameterListEx& pars) |
|
|
|
|
{ |
|
|
|
|
for(const auto& p: pars.GetParameterList()) |
|
|
|
|
{ |
|
|
|
|
MString ret; |
|
|
|
|
if(p.first.Prefix() != "") continue; // Only parameters with empty prefix considered for now
|
|
|
|
|
switch(p.second.valtype) |
|
|
|
|
{ |
|
|
|
|
case michlib_internal::Parameter::UINT1: |
|
|
|
|
{ |
|
|
|
|
ret = AddAtt(p.first.Name(), p.second.value.u1); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case michlib_internal::Parameter::UINT2: |
|
|
|
|
{ |
|
|
|
|
ret = AddAtt(p.first.Name(), p.second.value.u2); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case michlib_internal::Parameter::UINT4: |
|
|
|
|
{ |
|
|
|
|
ret = AddAtt(p.first.Name(), p.second.value.u4); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case michlib_internal::Parameter::UINT8: |
|
|
|
|
{ |
|
|
|
|
ret = AddAtt(p.first.Name(), p.second.value.u8); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case michlib_internal::Parameter::INT1: |
|
|
|
|
{ |
|
|
|
|
ret = AddAtt(p.first.Name(), p.second.value.i1); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case michlib_internal::Parameter::INT2: |
|
|
|
|
{ |
|
|
|
|
ret = AddAtt(p.first.Name(), p.second.value.i2); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case michlib_internal::Parameter::INT4: |
|
|
|
|
{ |
|
|
|
|
ret = AddAtt(p.first.Name(), p.second.value.i4); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case michlib_internal::Parameter::INT8: |
|
|
|
|
{ |
|
|
|
|
ret = AddAtt(p.first.Name(), p.second.value.i8); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case michlib_internal::Parameter::FLOAT: |
|
|
|
|
{ |
|
|
|
|
ret = AddAtt(p.first.Name(), p.second.value.r4); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case michlib_internal::Parameter::DOUBLE: |
|
|
|
|
{ |
|
|
|
|
ret = AddAtt(p.first.Name(), p.second.value.r8); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case michlib_internal::Parameter::LDOUBLE: |
|
|
|
|
{ |
|
|
|
|
ret = AddAtt(p.first.Name(), static_cast<double>(p.second.value.r10)); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case michlib_internal::Parameter::BOOL: |
|
|
|
|
{ |
|
|
|
|
ret = AddAtt(p.first.Name(), p.second.value.b ? 1 : 0); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case michlib_internal::Parameter::STRING: |
|
|
|
|
{ |
|
|
|
|
ret = AddAtt(p.first.Name(), p.second.svalue); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
case michlib_internal::Parameter::INVALID: break; // Silently skip
|
|
|
|
|
} |
|
|
|
|
if(ret.Exist()) return ret; |
|
|
|
|
} |
|
|
|
|
return ""; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template<class T> |
|
|
|
|
requires(Type2NCType<T> != NC_NAT) |
|
|
|
|
void AddAtt(const MString& name, const T& val) |
|
|
|
|
MString AddAtt(const MString& name, const T& val) |
|
|
|
|
{ |
|
|
|
|
if(err.IsErr()) return; |
|
|
|
|
if(err.IsErr()) return "Can't write attribute " + name + " due to previous errors"; |
|
|
|
|
static constexpr auto nct = Type2NCType<T>; |
|
|
|
|
static constexpr bool ischar = std::is_same_v<T, char*>; |
|
|
|
|
|
|
|
|
@ -138,19 +218,21 @@ class NCFileWBase
|
|
|
|
|
err.Reset(nc_put_att_text(ncid, NC_GLOBAL, name.Buf(), strlen(val) + 1, val)); |
|
|
|
|
else |
|
|
|
|
err.Reset(nc_put_att_text(ncid, NC_GLOBAL, name.Buf(), val.Len() + 1, val.Buf())); |
|
|
|
|
|
|
|
|
|
return err.IsErr() ? "Can't write attribute " + name + ": " + ErrMessage() : ""; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template<class T> |
|
|
|
|
requires(Type2NCType<T> != NC_NAT) |
|
|
|
|
void AddAtt(const MString& vname, const MString& name, const T& val) |
|
|
|
|
MString AddAtt(const MString& vname, const MString& name, const T& val) |
|
|
|
|
{ |
|
|
|
|
if(err.IsErr()) return; |
|
|
|
|
if(err.IsErr()) return "Can't write attribute " + name + " to the variable " + vname + " due to previous errors"; |
|
|
|
|
static constexpr auto nct = Type2NCType<T>; |
|
|
|
|
static constexpr bool ischar = std::is_same_v<T, char*>; |
|
|
|
|
int varid; |
|
|
|
|
|
|
|
|
|
err.Reset(nc_inq_varid(ncid, vname.Buf(), &varid)); |
|
|
|
|
if(err.IsErr()) return; |
|
|
|
|
if(err.IsErr()) return "Can't find variable " + vname + ": " + ErrMessage(); |
|
|
|
|
|
|
|
|
|
if constexpr(nct != NC_CHAR) |
|
|
|
|
err.Reset(nc_put_att(ncid, varid, name.Buf(), nct, 1, &val)); |
|
|
|
@ -158,16 +240,19 @@ class NCFileWBase
|
|
|
|
|
err.Reset(nc_put_att_text(ncid, varid, name.Buf(), strlen(val) + 1, val)); |
|
|
|
|
else |
|
|
|
|
err.Reset(nc_put_att_text(ncid, varid, name.Buf(), val.Len() + 1, val.Buf())); |
|
|
|
|
|
|
|
|
|
return err.IsErr() ? "Can't write attribute " + name + " to the variable " + vname + ": " + ErrMessage() : ""; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void AddAtt(const MString& vname, const MString& name, const char* val) |
|
|
|
|
MString AddAtt(const MString& vname, const MString& name, const char* val) |
|
|
|
|
{ |
|
|
|
|
if(err.IsErr()) return; |
|
|
|
|
if(err.IsErr()) return "Can't write attribute " + name + " to the variable " + vname + " due to previous errors"; |
|
|
|
|
int varid; |
|
|
|
|
|
|
|
|
|
err.Reset(nc_inq_varid(ncid, vname.Buf(), &varid)); |
|
|
|
|
if(err.IsErr()) return; |
|
|
|
|
if(err.IsErr()) return "Can't find variable " + vname + ": " + ErrMessage(); |
|
|
|
|
err.Reset(nc_put_att_text(ncid, varid, name.Buf(), strlen(val) + 1, val)); |
|
|
|
|
return err.IsErr() ? "Can't write attribute " + name + " to the variable " + vname + ": " + ErrMessage() : ""; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template<class... T> void AddVar(const MString& vname, nc_type type, const T&... dnames) { AddVar(vname, type, std::vector<MString>({dnames...})); } |
|
|
|
@ -257,18 +342,18 @@ class NCFileW: public NCFileWBase
|
|
|
|
|
static constexpr bool IsGeoType(Type t) { return t == GPSET || t == GGRID || t == GRGRID; } |
|
|
|
|
static constexpr bool Is1DType(Type t) { return t == PSET || t == GPSET; } |
|
|
|
|
|
|
|
|
|
MString CreateFile(Type stype, const MString& name, const MString& history, int compression, size_t nx, size_t ny); |
|
|
|
|
MString CreateFile(Type stype, const MString& name, int compression, size_t nx, size_t ny); |
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
static constexpr auto Fill() { return fill; } |
|
|
|
|
|
|
|
|
|
template<class D> MString Create(const D& data, const MString& name, const MString& history, int compression) |
|
|
|
|
template<class D> MString Create(const D& data, const MString& name, int compression) |
|
|
|
|
{ |
|
|
|
|
if(type != UNKNOWN) return "File already created"; |
|
|
|
|
if constexpr(Is1DType<D>()) |
|
|
|
|
return CreateFile(DetType<D>(), name, history, compression, data.N(), 0); |
|
|
|
|
return CreateFile(DetType<D>(), name, compression, data.N(), 0); |
|
|
|
|
else |
|
|
|
|
return CreateFile(DetType<D>(), name, history, compression, data.Nx(), data.Ny()); |
|
|
|
|
return CreateFile(DetType<D>(), name, compression, data.Nx(), data.Ny()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Close() |
|
|
|
|