Browse Source

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.

interpolate
Michael Uleysky 1 year ago
parent
commit
3b40409fb2
  1. 4
      actions/actiontsc.h
  2. 18
      actions/actionuv.h
  3. 8
      include/basedata.h
  4. 2
      include/ncfilew.h
  5. 4
      src/ncfilew.cpp

4
actions/actiontsc.h

@ -50,6 +50,8 @@ template<class D> MString ActionTSC::DoAction(const CLArgs& args, D& ds)
auto data = Read(ds, vname, p, tindexes); auto data = Read(ds, vname, p, tindexes);
if(!data) return "Can't read data"; if(!data) return "Can't read data";
if(!data.Unit().Exist()) michlib::errmessage("Unknown measurement unit!"); 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 name = args.contains("out") ? args.at("out") : "out.bin";
MString outfmt = args.contains("format") ? args.at("format") : (GetExt(name) == "nc" ? "nc" : "bin"); MString outfmt = args.contains("format") ? args.at("format") : (GetExt(name) == "nc" ? "nc" : "bin");
@ -83,7 +85,7 @@ template<class D> MString ActionTSC::DoAction(const CLArgs& args, D& ds)
if(args.contains("compress")) compress = args.at("compress").ToInt(); 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.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.WriteGrid(data);
if(!err.Exist()) err = fw.WriteVariable(data, vname); if(!err.Exist()) err = fw.WriteVariable(data, vname);
if(err.Exist()) return err; if(err.Exist()) return err;

18
actions/actionuv.h

@ -177,13 +177,13 @@ template<class D> MString ActionUV::DoAction(const CLArgs& args, D& ds)
if(args.contains("compress")) compress = args.at("compress").ToInt(); 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.Create(data, name, args.at("_cmdline"), compress);
if(!err.Exist()) err = fw.AddVariable("u", "", "Eastward 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.AddVariable("v", "", "Northward velocity", u, "");
if(!err.Exist()) err = fw.AddVariable("div", "", "Velocity divergence", "(" + u + ")/" + d); 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("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("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("ke", "", "Squared velocity module, u^2+v^2", "(" + u + ")2", "");
if(!err.Exist()) err = fw.AddVariable("eke", "", "Squared velocity dispersion aka eddy kinetic energy, <u^2+v^2>-<u>^2-<v>^2", "(" + u + ")2"); if(!err.Exist()) err = fw.AddVariable("eke", "", "Squared velocity dispersion aka eddy kinetic energy, <u^2+v^2>-<u>^2-<v>^2", "(" + u + ")2", "");
if(!err.Exist()) err = fw.WriteGrid(data); if(!err.Exist()) err = fw.WriteGrid(data);
@ -243,8 +243,8 @@ template<class D> MString ActionUV::DoAction(const CLArgs& args, D& ds)
if(args.contains("compress")) compress = args.at("compress").ToInt(); 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.Create(sdata, name, args.at("_cmdline"), compress);
if(!err.Exist()) err = fw.AddVariable("u", "", "Eastward 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.AddVariable("v", "", "Northward velocity", u, "");
if(!err.Exist()) err = fw.WriteGrid(sdata); if(!err.Exist()) err = fw.WriteGrid(sdata);

8
include/basedata.h

@ -20,7 +20,7 @@ class BaseData
protected: protected:
static constexpr real fillval = 1.0e10; static constexpr real fillval = 1.0e10;
std::vector<real> data; std::vector<real> data;
MString unit; MString unit, stname, lname, comment;
BaseData(size_t n, MString&& unit_ = ""): data(n), unit(std::move(unit_)) {} 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; } explicit operator bool() const { return N() != 0; }
void SetUnit(const MString& str) { unit = str; } 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& 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 class UngriddedData: public BaseData

2
include/ncfilew.h

@ -87,7 +87,7 @@ class NCFileW
type = UNKNOWN; 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<class D, class Op> MString WriteVariable(const D& data, size_t varid, Op op) const template<class D, class Op> MString WriteVariable(const D& data, size_t varid, Op op) const
{ {

4
src/ncfilew.cpp

@ -71,7 +71,7 @@ MString NCFileW::CreateFile(NCFileW::Type stype, const MString& name, const MStr
return ""; 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(type == UNKNOWN) return "File not opened";
if(HaveVar(name)) return "Variable " + name + " already defined"; 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(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(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(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); 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"; if(ret != NC_NOERR) return "Can't write _FillValue attribute of " + v.name + " variable in the netcdf file";

Loading…
Cancel
Save