#define MICHLIB_NOSOURCE #include "ncfilew.h" MString NCFileW::CreateFile(NCFileW::Type stype, const MString& name, const MString& history, int compression, size_t nx, size_t ny) { if(stype == UNKNOWN) return "Can't determine file type"; compress = compression; int ret; MString text; const float node_offset = 0.0; ret = nc_create(name.Buf(), NC_CLOBBER | NC_NETCDF4, &ncid); if(ret != NC_NOERR) return "Can't create netcdf file: " + name; ret = nc_put_att_text(ncid, NC_GLOBAL, "history", history.Len() + 1, history.Buf()); if(ret != NC_NOERR) return "Can't write history attribute in the netcdf file"; ret = nc_put_att(ncid, NC_GLOBAL, "node_offset", NC_FLOAT, 1, &node_offset); if(ret != NC_NOERR) return "Can't write history attribute in the netcdf file"; switch(stype) { case(G1V1): { ret = nc_def_dim(ncid, "i", nx, &xdimid); if(ret != NC_NOERR) return "Can't create dimension in the netcdf file"; ret = nc_def_var(ncid, "longitude", NC_FLOAT, 1, &xdimid, &xid); if(ret != NC_NOERR) return "Can't create longitude variable in the netcdf file"; ret = nc_def_var(ncid, "latitude", NC_FLOAT, 1, &xdimid, &yid); if(ret != NC_NOERR) return "Can't create latitude variable in the netcdf file"; break; } case(G1V2): { ret = nc_def_dim(ncid, "longitude", nx, &xdimid); if(ret != NC_NOERR) return "Can't create x-dimension in the netcdf file"; ret = nc_def_dim(ncid, "latitude", ny, &ydimid); if(ret != NC_NOERR) return "Can't create y-dimension in the netcdf file"; ret = nc_def_var(ncid, "longitude", NC_FLOAT, 1, &xdimid, &xid); if(ret != NC_NOERR) return "Can't create longitude variable in the netcdf file"; ret = nc_def_var(ncid, "latitude", NC_FLOAT, 1, &ydimid, &yid); if(ret != NC_NOERR) return "Can't create latitude variable in the netcdf file"; break; } case(G2V2): { ret = nc_def_dim(ncid, "longitude", nx, &xdimid); if(ret != NC_NOERR) return "Can't create x-dimension in the netcdf file"; ret = nc_def_dim(ncid, "latitude", ny, &ydimid); if(ret != NC_NOERR) return "Can't create y-dimension in the netcdf file"; ret = nc_def_var(ncid, "longitude", NC_FLOAT, 2, dimid, &xid); if(ret != NC_NOERR) return "Can't create longitude variable in the netcdf file"; ret = nc_def_var(ncid, "latitude", NC_FLOAT, 2, dimid, &yid); if(ret != NC_NOERR) return "Can't create latitude variable in the netcdf file"; break; } case(UNKNOWN): return "Can't determine file type"; } ret = nc_def_var_deflate(ncid, xid, 1, 1, compress); if(ret != NC_NOERR) return "Can't set deflate parameters for longitude variable in the netcdf file"; ret = nc_def_var_deflate(ncid, yid, 1, 1, compress); if(ret != NC_NOERR) return "Can't set deflate parameters for latitude variable in the netcdf file"; text = "longitude"; ret = nc_put_att_text(ncid, xid, "standard_name", text.Len() + 1, text.Buf()); if(ret != NC_NOERR) return "Can't write standard_name attribute of longitude variable in the netcdf file"; text = "latitude"; ret = nc_put_att_text(ncid, yid, "standard_name", text.Len() + 1, text.Buf()); if(ret != NC_NOERR) return "Can't write standard_name attribute of latitude variable in the netcdf file"; text = "Longitude"; ret = nc_put_att_text(ncid, xid, "long_name", text.Len() + 1, text.Buf()); if(ret != NC_NOERR) return "Can't write long_name attribute of longitude variable in the netcdf file"; text = "Latitude"; ret = nc_put_att_text(ncid, yid, "long_name", text.Len() + 1, text.Buf()); if(ret != NC_NOERR) return "Can't write long_name attribute of latitude variable in the netcdf file"; type = stype; 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(HaveVar(name)) return "Variable " + name + " already defined"; struct Var v(name, 0); int ret; if(type == G1V1) ret = nc_def_var(ncid, v.name.Buf(), NC_FLOAT, 1, &xdimid, &v.id); else ret = nc_def_var(ncid, v.name.Buf(), NC_FLOAT, 2, dimid, &v.id); if(ret != NC_NOERR) return "Can't create " + v.name + " variable in the netcdf file"; ret = nc_def_var_deflate(ncid, v.id, 1, 1, compress); if(ret != NC_NOERR) return "Can't set deflate parameters for " + v.name + " variable in the netcdf file"; if(stname.Exist()) ret = nc_put_att_text(ncid, v.id, "standard_name", stname.Len() + 1, stname.Buf()); if(ret != NC_NOERR) return "Can't write standard_name attribute of " + v.name + " variable in the netcdf file"; if(lname.Exist()) ret = nc_put_att_text(ncid, v.id, "long_name", lname.Len() + 1, lname.Buf()); 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"; vars.push_back(v); return ""; }