You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

143 lines
3.3 KiB

#define MICHLIB_NOSOURCE
#include "ncfilew.h"
MString NCFileW::CreateFile(NCFileW::Type stype, const MString& name, int compression, size_t nx, size_t ny)
{
if(stype == UNKNOWN) return "Can't determine file type";
compress = compression;
const float node_offset = 0.0;
Open(name);
if(!*this) return "Can't create netcdf file " + name + ": " + ErrMessage();
AddAtt("node_offset", node_offset);
switch(stype)
{
case(PSET):
{
AddDim("i", nx);
AddVar("x", NC_FLOAT, "i");
AddVar("y", NC_FLOAT, "i");
break;
}
case(GPSET):
{
AddDim("i", nx);
AddVar("longitude", NC_FLOAT, "i");
AddVar("latitude", NC_FLOAT, "i");
break;
}
case(RGRID):
{
AddDim("x", nx);
AddDim("y", ny);
AddVar("x", NC_FLOAT, "x");
AddVar("y", NC_FLOAT, "y");
break;
}
case(GRGRID):
{
AddDim("longitude", nx);
AddDim("latitude", ny);
AddVar("longitude", NC_FLOAT, "longitude");
AddVar("latitude", NC_FLOAT, "latitude");
break;
}
case(GRID):
{
AddDim("x", nx);
AddDim("y", ny);
AddVar("x", NC_FLOAT, "y", "x");
AddVar("y", NC_FLOAT, "y", "x");
break;
}
case(GGRID):
{
AddDim("longitude", nx);
AddDim("latitude", ny);
AddVar("longitude", NC_FLOAT, "latitude", "longitude");
AddVar("latitude", NC_FLOAT, "latitude", "longitude");
break;
}
case(UNKNOWN): return "Can't determine file type";
}
if(IsGeoType(stype))
{
SetComp("longitude", compress);
SetComp("latitude", compress);
AddAtt("longitude", "standard_name", "longitude");
AddAtt("longitude", "long_name", "Longitude");
AddAtt("latitude", "standard_name", "latitude");
AddAtt("latitude", "long_name", "Latitude");
}
else
{
SetComp("x", compress);
SetComp("y", compress);
AddAtt("x", "long_name", "x-coordinate");
AddAtt("y", "long_name", "y-coordinate");
}
if(!*this) return "Can't set grid in the netcdf file " + name + ": " + ErrMessage();
type = stype;
return "";
}
MString NCFileW::AddTimeData(const TimeData& tdata, bool tisindex)
{
tdep = tisindex;
AddDim("time", tdata.steps.size());
AddVar("time", Type2NCType<decltype(tdata.steps)::value_type>, "time");
SetComp("time", compress);
AddAtt("time", "standard_name", "time");
AddAtt("time", "long_name", "time");
AddAtt("time", "units", tdata.UnitName());
WriteVar("time", tdata.steps.data());
if(!*this) return MString("Can't add time data to the netcdf file: ") + ErrMessage();
return "";
}
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(Is1DType(type))
{
if(tdep)
AddVar(name, NC_FLOAT, "time", "i");
else
AddVar(name, NC_FLOAT, "i");
}
else if(IsGeoType(type))
{
if(tdep)
AddVar(name, NC_FLOAT, "time", "latitude", "longitude");
else
AddVar(name, NC_FLOAT, "latitude", "longitude");
}
else
{
if(tdep)
AddVar(name, NC_FLOAT, "time", "y", "x");
else
AddVar(name, NC_FLOAT, "y", "x");
}
SetComp(name, compress);
if(stname.Exist()) AddAtt(name, "standard_name", stname);
if(lname.Exist()) AddAtt(name, "long_name", lname);
if(units.Exist()) AddAtt(name, "units", units);
if(comment.Exist()) AddAtt(name, "comment", comment);
AddAtt(name, "_FillValue", fill);
if(!*this) return "Can't add variable " + name + ": " + ErrMessage();
return "";
}