From 3b40409fb2a093fdf3910225cf4faead4600dd2d Mon Sep 17 00:00:00 2001 From: Michael Uleysky Date: Wed, 2 Aug 2023 18:31:17 +1000 Subject: [PATCH] Sources can now set the standart_name, long_name, and comment attributes on read data. If the standart_name and long_name attributes are not set, the tsc module will attempt to set them according to the variable names. --- actions/actiontsc.h | 4 +++- actions/actionuv.h | 18 +++++++++--------- include/basedata.h | 8 +++++++- include/ncfilew.h | 2 +- src/ncfilew.cpp | 4 +++- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/actions/actiontsc.h b/actions/actiontsc.h index 96a4131..899d720 100644 --- a/actions/actiontsc.h +++ b/actions/actiontsc.h @@ -50,6 +50,8 @@ template MString ActionTSC::DoAction(const CLArgs& args, D& ds) auto data = Read(ds, vname, p, tindexes); if(!data) return "Can't read data"; if(!data.Unit().Exist()) michlib::errmessage("Unknown measurement unit!"); + if(!data.StandartName().Exist()) data.SetStandartName(NCFuncs::Name2StName(vname)); + if(!data.LongName().Exist()) data.SetStandartName(NCFuncs::Name2LongName(vname)); MString name = args.contains("out") ? args.at("out") : "out.bin"; MString outfmt = args.contains("format") ? args.at("format") : (GetExt(name) == "nc" ? "nc" : "bin"); @@ -83,7 +85,7 @@ template MString ActionTSC::DoAction(const CLArgs& args, D& ds) if(args.contains("compress")) compress = args.at("compress").ToInt(); if(!err.Exist()) err = fw.Create(data, name, args.at("_cmdline"), compress); - if(!err.Exist()) err = fw.AddVariable(vname, NCFuncs::Name2StName(vname), NCFuncs::Name2LongName(vname), data.Unit()); + if(!err.Exist()) err = fw.AddVariable(vname, data.StandartName(), data.LongName(), data.Unit(), data.Comment()); if(!err.Exist()) err = fw.WriteGrid(data); if(!err.Exist()) err = fw.WriteVariable(data, vname); if(err.Exist()) return err; diff --git a/actions/actionuv.h b/actions/actionuv.h index 810b267..01c96f7 100644 --- a/actions/actionuv.h +++ b/actions/actionuv.h @@ -177,13 +177,13 @@ template MString ActionUV::DoAction(const CLArgs& args, D& ds) if(args.contains("compress")) compress = args.at("compress").ToInt(); if(!err.Exist()) err = fw.Create(data, name, args.at("_cmdline"), compress); - if(!err.Exist()) err = fw.AddVariable("u", "", "Eastward velocity", u); - if(!err.Exist()) err = fw.AddVariable("v", "", "Northward velocity", u); - if(!err.Exist()) err = fw.AddVariable("div", "", "Velocity divergence", "(" + u + ")/" + d); - if(!err.Exist()) err = fw.AddVariable("rot", "", "Velocity rotor", "(" + u + ")/" + d); - if(!err.Exist()) err = fw.AddVariable("ow", "", "Okubo-Weiss parameter", "(" + u + ")2/" + d + "2"); - if(!err.Exist()) err = fw.AddVariable("ke", "", "Squared velocity module, u^2+v^2", "(" + u + ")2"); - if(!err.Exist()) err = fw.AddVariable("eke", "", "Squared velocity dispersion aka eddy kinetic energy, -^2-^2", "(" + u + ")2"); + if(!err.Exist()) err = fw.AddVariable("u", "", "Eastward velocity", u, ""); + if(!err.Exist()) err = fw.AddVariable("v", "", "Northward velocity", u, ""); + if(!err.Exist()) err = fw.AddVariable("div", "", "Velocity divergence", "(" + u + ")/" + d, ""); + if(!err.Exist()) err = fw.AddVariable("rot", "", "Velocity rotor", "(" + u + ")/" + d, ""); + if(!err.Exist()) err = fw.AddVariable("ow", "", "Okubo-Weiss parameter", "(" + u + ")2/" + d + "2", ""); + if(!err.Exist()) err = fw.AddVariable("ke", "", "Squared velocity module, u^2+v^2", "(" + u + ")2", ""); + if(!err.Exist()) err = fw.AddVariable("eke", "", "Squared velocity dispersion aka eddy kinetic energy, -^2-^2", "(" + u + ")2", ""); if(!err.Exist()) err = fw.WriteGrid(data); @@ -243,8 +243,8 @@ template MString ActionUV::DoAction(const CLArgs& args, D& ds) if(args.contains("compress")) compress = args.at("compress").ToInt(); if(!err.Exist()) err = fw.Create(sdata, name, args.at("_cmdline"), compress); - if(!err.Exist()) err = fw.AddVariable("u", "", "Eastward velocity", u); - if(!err.Exist()) err = fw.AddVariable("v", "", "Northward velocity", u); + if(!err.Exist()) err = fw.AddVariable("u", "", "Eastward velocity", u, ""); + if(!err.Exist()) err = fw.AddVariable("v", "", "Northward velocity", u, ""); if(!err.Exist()) err = fw.WriteGrid(sdata); diff --git a/include/basedata.h b/include/basedata.h index 922d3f6..4df7b67 100644 --- a/include/basedata.h +++ b/include/basedata.h @@ -20,7 +20,7 @@ class BaseData protected: static constexpr real fillval = 1.0e10; std::vector data; - MString unit; + MString unit, stname, lname, comment; BaseData(size_t n, MString&& unit_ = ""): data(n), unit(std::move(unit_)) {} @@ -42,8 +42,14 @@ class BaseData explicit operator bool() const { return N() != 0; } void SetUnit(const MString& str) { unit = str; } + void SetStandartName(const MString& str) { stname = str; } + void SetLongName(const MString& str) { lname = str; } + void SetComment(const MString& str) { comment = str; } const MString& Unit() const { return unit; } + const MString& StandartName() const { return stname; } + const MString& LongName() const { return lname; } + const MString& Comment() const { return comment; } }; class UngriddedData: public BaseData diff --git a/include/ncfilew.h b/include/ncfilew.h index 08bc46e..fdce58c 100644 --- a/include/ncfilew.h +++ b/include/ncfilew.h @@ -87,7 +87,7 @@ class NCFileW type = UNKNOWN; } - MString AddVariable(const MString& name, const MString& stname, const MString& lname, const MString& units); + MString AddVariable(const MString& name, const MString& stname, const MString& lname, const MString& units, const MString& comment); template MString WriteVariable(const D& data, size_t varid, Op op) const { diff --git a/src/ncfilew.cpp b/src/ncfilew.cpp index 1d450e1..a4f0f35 100644 --- a/src/ncfilew.cpp +++ b/src/ncfilew.cpp @@ -71,7 +71,7 @@ MString NCFileW::CreateFile(NCFileW::Type stype, const MString& name, const MStr return ""; } -MString NCFileW::AddVariable(const MString& name, const MString& stname, const MString& lname, const MString& units) +MString NCFileW::AddVariable(const MString& name, const MString& stname, const MString& lname, const MString& units, const MString& comment) { if(type == UNKNOWN) return "File not opened"; if(HaveVar(name)) return "Variable " + name + " already defined"; @@ -94,6 +94,8 @@ MString NCFileW::AddVariable(const MString& name, const MString& stname, const M if(ret != NC_NOERR) return "Can't write long_name attribute of " + v.name + " variable in the netcdf file"; if(units.Exist()) ret = nc_put_att_text(ncid, v.id, "units", units.Len() + 1, units.Buf()); if(ret != NC_NOERR) return "Can't write units attribute of " + v.name + " variable in the netcdf file"; + if(comment.Exist()) ret = nc_put_att_text(ncid, v.id, "comment", comment.Len() + 1, comment.Buf()); + if(ret != NC_NOERR) return "Can't write comment attribute of " + v.name + " variable in the netcdf file"; ret = nc_put_att_float(ncid, v.id, "_FillValue", NC_FLOAT, 1, &fill); if(ret != NC_NOERR) return "Can't write _FillValue attribute of " + v.name + " variable in the netcdf file";