#pragma once #include "DataAdapters/ncfilealt.h" #include "ParseArgs.h" #include "mdatetime.h" #include "simple2ddata.h" #include #include using michlib::Ceil; using michlib::DetGeoDomain; using michlib::Floor; using michlib::GPL; using michlib::int2; using michlib::MDateTime; using michlib::MString; using michlib::NCFileA; using michlib::ToGeoDomain; class LayeredData { public: using Data = Simple2DData; private: std::vector nc; std::vector urls; std::vector depths; std::vector times; MString lonname, latname; size_t nx, ny; real lonb, latb, lone, late; real lonstep, latstep; MString title; class EnvVar { MString name, oldvalue; bool activated, saved; public: EnvVar(): activated(false) {} ~EnvVar() { Deactivate(); } void Activate(const MString& var, const MString& val) { if(activated) Deactivate(); name = var; char* curval = getenv(name.Buf()); if(nullptr == curval) saved = false; else { oldvalue = curval; saved = true; } setenv(name.Buf(), val.Buf(), 1); } void Deactivate() { if(!activated) return; if(saved) setenv(name.Buf(), oldvalue.Buf(), 1); else unsetenv(name.Buf()); activated = false; } }; EnvVar proxy; protected: struct Parameters: public BaseParameters { size_t xb, yb, xe, ye, layer; MString varname; virtual ~Parameters() override = default; }; // TODO: RetVal MString Open(const CLArgs& args, const MString& dataset); void SetTitle(const MString& newtitle) { title = newtitle; } public: MString Info() const; std::pair Parameters(michlib_internal::ParameterListEx& pars, const CLArgs& args) const; Data Read(const BaseParameters* ip, size_t i) const; bool isOk() const { return nc.size() > 0; } explicit operator bool() const { return nc.size() > 0; } real Depth(size_t l) const { return isOk() ? depths[l] : -1000.0; } real Lon(size_t ix) const { return isOk() ? (lonb + ix * lonstep) : -1000.0; } real Lat(size_t iy) const { return isOk() ? (latb + iy * latstep) : -1000.0; } size_t NDepths() const { return depths.size(); } size_t NTimes() const { return times.size(); } MDateTime Time(size_t i) const { if(!isOk() || i >= times.size()) return MDateTime(); return times[i]; } time_t Timestep() const { return isOk() ? (times[1] - times[0]) : 0; } MString Title() const { return title; } MString Dump(const struct Parameters* ppar) const { // clang-format off return "Current settings:\n" + MString() + " Longitudes: from " + Lon(ppar->xb) + " (" + ppar->xb + ") to "+ Lon(ppar->xe) + " (" + ppar->xe + ")\n" + " Latitudes: from " + Lat(ppar->yb) + " (" + ppar->yb + ") to "+ Lat(ppar->ye) + " (" + ppar->ye + ")\n" + " Depth: layer " + ppar->layer + ", depth " + Depth(ppar->layer) + " m\n"; // clang-format on } private: template Data ReadVarRaw(const NCFileA& f, const MString& name, size_t i, bool nodepth, const struct Parameters* p) const; std::pair VarNameLoc(const MString vname) const { for(size_t i = 0; i < nc.size(); i++) { auto head = nc[i].Header(); for(const auto& v: head.Variables()) { auto stname = nc[i].A(v.Name(), "standard_name"); if(!stname) continue; if(StName2Name(stname) == vname) return {v.Name(), i}; } } return {"", 0}; } static MString StName2Name(const MString& stname) { if(stname == "sea_water_potential_temperature") return "temp"; if(stname == "sea_water_temperature") return "temp"; if(stname == "sea_water_salinity") return "sal"; if(stname == "ocean_mixed_layer_thickness_defined_by_sigma_theta") return "mld"; if(stname == "sea_surface_height_above_geoid") return "ssh"; if(stname == "sea_surface_elevation") return "ssh"; if(stname == "upward_sea_water_velocity") return "w"; return ""; } };