Browse Source

Netcdf output files made more COARDS-compliant

lintest
Michael Uleysky 1 year ago
parent
commit
dcbc249fd7
  1. 7
      actions/actionuv.cpp
  2. 18
      include/ncfilew.h
  3. 12
      src/ncfilew.cpp

7
actions/actionuv.cpp

@ -49,6 +49,13 @@ MString UVMethods::StPoints::WriteNcFile(const MString& name, const MString& his
text = "latitude"; text = "latitude";
ret = nc_put_att_text(ncid, yid, "standard_name", text.Len() + 1, text.Buf()); 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"; 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";
} }
ret = nc_def_var_deflate(ncid, xid, 1, 1, comp); ret = nc_def_var_deflate(ncid, xid, 1, 1, comp);

18
include/ncfilew.h

@ -31,7 +31,7 @@ class NCFileW
{ {
struct struct
{ {
int xdimid, ydimid; int ydimid, xdimid;
}; };
int dimid[2]; int dimid[2];
}; };
@ -114,11 +114,11 @@ class NCFileW
else else
{ {
const size_t i[2] = {0, 0}; const size_t i[2] = {0, 0};
const size_t c[2] = {data.Nx(), data.Ny()}; const size_t c[2] = {data.Ny(), data.Nx()};
float buf[c[0] * c[1]]; float buf[c[0] * c[1]];
for(size_t ix = 0; ix < c[0]; ix++) for(size_t iy = 0; iy < c[0]; iy++)
for(size_t iy = 0; iy < c[1]; iy++) buf[ix * c[1] + iy] = data.IsFill(ix, iy) ? fill : op(ix, iy); for(size_t ix = 0; ix < c[1]; ix++) buf[iy * c[1] + ix] = data.IsFill(ix, iy) ? fill : op(ix, iy);
ret = nc_put_vara_float(ncid, vars[v].id, i, c, buf); ret = nc_put_vara_float(ncid, vars[v].id, i, c, buf);
if(ret != NC_NOERR) return "Can't write " + vars[v].name + " variable in the netcdf file"; if(ret != NC_NOERR) return "Can't write " + vars[v].name + " variable in the netcdf file";
@ -179,14 +179,14 @@ class NCFileW
else if constexpr(dtype == G2V2) else if constexpr(dtype == G2V2)
{ {
const size_t i[2] = {0, 0}; const size_t i[2] = {0, 0};
const size_t c[2] = {data.Nx(), data.Ny()}; const size_t c[2] = {data.Ny(), data.Nx()};
float bufx[c[0] * c[1]]; float bufx[c[0] * c[1]];
float bufy[c[0] * c[1]]; float bufy[c[0] * c[1]];
for(size_t ix = 0; ix < c[0]; ix++) for(size_t iy = 0; iy < c[0]; iy++)
for(size_t iy = 0; iy < c[1]; iy++) for(size_t ix = 0; ix < c[1]; ix++)
{ {
bufx[ix * c[1] + iy] = data.Lon(ix, iy); bufx[iy * c[1] + ix] = data.Lon(ix, iy);
bufy[ix * c[1] + iy] = data.Lat(ix, iy); bufy[iy * c[1] + ix] = data.Lat(ix, iy);
} }
ret = nc_put_vara_float(ncid, xid, i, c, bufx); ret = nc_put_vara_float(ncid, xid, i, c, bufx);
if(ret != NC_NOERR) return "Can't write longitude variable in the netcdf file"; if(ret != NC_NOERR) return "Can't write longitude variable in the netcdf file";

12
src/ncfilew.cpp

@ -10,12 +10,17 @@ MString NCFileW::CreateFile(NCFileW::Type stype, const MString& name, const MStr
int ret; int ret;
MString text; MString text;
const float node_offset = 0.0;
ret = nc_create(name.Buf(), NC_CLOBBER | NC_NETCDF4, &ncid); ret = nc_create(name.Buf(), NC_CLOBBER | NC_NETCDF4, &ncid);
if(ret != NC_NOERR) return "Can't create netcdf file: " + name; 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()); 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"; 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) switch(stype)
{ {
case(G1V1): case(G1V1):
@ -67,6 +72,13 @@ MString NCFileW::CreateFile(NCFileW::Type stype, const MString& name, const MStr
ret = nc_put_att_text(ncid, yid, "standard_name", text.Len() + 1, text.Buf()); 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"; 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; type = stype;
return ""; return "";
} }

Loading…
Cancel
Save