From dcbc249fd70e4f2d519c9571c2f6f5c27f32ef97 Mon Sep 17 00:00:00 2001 From: Michael Uleysky Date: Fri, 1 Sep 2023 15:12:37 +1000 Subject: [PATCH] Netcdf output files made more COARDS-compliant --- actions/actionuv.cpp | 7 +++++++ include/ncfilew.h | 18 +++++++++--------- src/ncfilew.cpp | 12 ++++++++++++ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/actions/actionuv.cpp b/actions/actionuv.cpp index 434d72c..69ba2c0 100644 --- a/actions/actionuv.cpp +++ b/actions/actionuv.cpp @@ -49,6 +49,13 @@ MString UVMethods::StPoints::WriteNcFile(const MString& name, const MString& his 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"; } ret = nc_def_var_deflate(ncid, xid, 1, 1, comp); diff --git a/include/ncfilew.h b/include/ncfilew.h index fdce58c..1cfb1a6 100644 --- a/include/ncfilew.h +++ b/include/ncfilew.h @@ -31,7 +31,7 @@ class NCFileW { struct { - int xdimid, ydimid; + int ydimid, xdimid; }; int dimid[2]; }; @@ -114,11 +114,11 @@ class NCFileW else { 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]]; - for(size_t ix = 0; ix < c[0]; ix++) - 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 iy = 0; iy < c[0]; 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); 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) { 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 bufy[c[0] * c[1]]; - for(size_t ix = 0; ix < c[0]; ix++) - for(size_t iy = 0; iy < c[1]; iy++) + for(size_t iy = 0; iy < c[0]; iy++) + for(size_t ix = 0; ix < c[1]; ix++) { - bufx[ix * c[1] + iy] = data.Lon(ix, iy); - bufy[ix * c[1] + iy] = data.Lat(ix, iy); + bufx[iy * c[1] + ix] = data.Lon(ix, iy); + bufy[iy * c[1] + ix] = data.Lat(ix, iy); } ret = nc_put_vara_float(ncid, xid, i, c, bufx); if(ret != NC_NOERR) return "Can't write longitude variable in the netcdf file"; diff --git a/src/ncfilew.cpp b/src/ncfilew.cpp index a4f0f35..a5938e4 100644 --- a/src/ncfilew.cpp +++ b/src/ncfilew.cpp @@ -10,12 +10,17 @@ MString NCFileW::CreateFile(NCFileW::Type stype, const MString& name, const MStr 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): @@ -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()); 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 ""; }