[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[netCDF #MCV-764151]: NetCDF C++: ncChar



Hi Karel,

> I really need your help because I have a lot of difficulties to solve my
> problem for generating a kind of netCDF document with char variables:
> 
> *This code works well but doesn't generate what I want:*
> 
> [code]
> #include <iostream>
> #include <netcdfcpp.h>
> #include <vector>
> #include <string>
> #include <cstring>
> 
> using namespace std;
> 
> static const int NY = 4;
> static const int NZ = 1;
> 
> int main(void)
> {
> NcFile dataFile("chaine.nc", NcFile::Replace);
> 
> NcDim* yDim = dataFile.add_dim("y", NY);
> NcDim* zDim = dataFile.add_dim("z", NZ);
> NcVar *monCharSimple2 = dataFile.add_var("monCharSimple2", ncChar,
> zDim, yDim);
> 
> vector<string> dataOut2Vec;
> string toto2 = "toto";
> toto2.resize(4);
> monCharSimple2->put_rec(zDim, &toto2[0], 0);
> 
> return 0;
> }
> [/code]
> 
> 
> The result is:
> 
> [code]
> netcdf chaine {
> dimensions:
> y = 4 ;
> z = 1 ;
> variables:
> char monCharSimple2*(z, y)* ;
> data:
> 
> monCharSimple2 =
> *"toto"* ;
> }
> [/code]
> 
> *but if I want to generate this kind of netCDF:*
> 
> [code]
> netcdf chaineSimple {
> dimensions:
> y = 4 ;
> variables:
> char monCharSimple2*(y)* ;
> data:
> monCharSimple2 = *"toto"* ;
> }
> [/code]
> 
> *i have dificulties with the dimension of toto(nbr): how to use only one
> dimention instead of two with one fixed at 1?*
> *I tried this but it doesn't work:*
> 
> [code]
> #include <iostream>
> #include <netcdfcpp.h>
> #include <vector>
> #include <string>
> #include <cstring>
> 
> using namespace std;
> 
> static const int NY = 4;
> //     static const int NZ = 1;
> 
> int main(void)
> {
> NcFile dataFile("chaineSimple.nc", NcFile::Replace);
> 
> NcDim* yDim = dataFile.add_dim("y", NY);
> //        NcDim* zDim = dataFile.add_dim("z", NZ);
> //        NcVar *monCharSimple2 = dataFile.add_var("monCharSimple2",
> ncChar, zDim, yDim);
> NcVar *monCharSimple2 = dataFile.add_var("monCharSimple2", ncChar,
> yDim);
> 
> vector<string> dataOut2Vec;
> string toto2 = "toto";
> toto2.resize(4);
> monCharSimple2->put_rec(&toto2[0]);
> 
> return 0;
> }
> [/code]
> 
> *Result*:
> 
> [code]
> netcdf chaineSimple {
> dimensions:
> y = 4 ;
> variables:
> char monCharSimple2*(y)* ;
> data:
> 
> monCharSimple2 = "t" ;
> }[/code]

I'm not a C++ expert, but I know the netcdf-cxx-4.2 package you're using is
the "legacy" API that I wrote back in the early 1990's, before C++ had 
standard exceptions, templates, and namespaces.  The netcdf-cxx-4.2 API 
supports char* arrays instead of C++ strings, so the only way I know to
get it to do what you want is:

[code]
int main(void)
     {
        NcFile dataFile("chaineSimple.nc", NcFile::Replace);

        NcDim* yDim = dataFile.add_dim("y", NY);
//        NcDim* zDim = dataFile.add_dim("z", NZ);
//        NcVar *monCharSimple2 = dataFile.add_var("monCharSimple2", ncChar, 
zDim, yDim);
        NcVar *monCharSimple2 = dataFile.add_var("monCharSimple2", ncChar,
yDim);

        vector<string> dataOut2Vec;
        char* toto2 = "toto";
        // toto2.resize(4);
        // monCharSimple2->put_rec(&toto2[0]);
        monCharSimple2->put(toto2, strlen(toto2));

        return 0;
     }
[/code]

using "char*" instead of string, and providing the string length as the
argument strlen(toto2) to the put method to tell how long the array is.

You might be able to do better by converting a string value to a char array
and using the string::length() method to provide the length, but I'm not
confident of the right way to do that.

If the above is not satisfactory, you might want to check out the newer
netcdf-cxx4-4.2 package contributed by Lynton Appel and available from
out netCDF downloads, but it's not backward compatible with the legacy C++
API.  However, I think it may supports C++ strings, since the underlying
netCDF-4 C API has string as a primitive netCDF external type.

--Russ

Russ Rew                                         UCAR Unidata Program
address@hidden                      http://www.unidata.ucar.edu



Ticket Details
===================
Ticket ID: MCV-764151
Department: Support netCDF
Priority: Normal
Status: Closed