This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
>From: Markus Werle <address@hidden> >Subject: Non ANSI C++ >Organization: Lehr- und Forschungsgebiet fuer Mechanik, RWTH-Aachen >Keywords: 199805041934.NAA23467 netCDF g++ Hi Markus, I wrote: > > OriginalMesh.NcMeshPointsInFile->put(MeshPointData, spacedim, jdim, idim); > > > > > > Is there any workaround or do I have to use/write an Array class > > which provides one-dimensional data access for netCDF routines? > > If you declare and allocate space for the multidimensional array in a > single declaration, e.g. > > double MeshPointData[spacedim][jdim][idim]; > > then the space for the values are allocated contiguously, so you can > just provide the address of the first value instead of creating another > 1-dimensional array: > > ...->put(&MeshPointData[0][0][0], spacedim, jdim, idim); > > > Do You know about any C++-examples in the WWW using netCDF with > > multidimensional arrays from which I could learn? > > No, sorry, I don't know of any C++ examples. I just modified the test program cxx/nctst.cc that comes with the distribution to make it use a 3-dimensional data array for pressure instead of a 1-dimensional array. I think this actually makes the example clearer, so I will commit this change for the next version. I've appended a "context diff" of the old and new version, from which you should be able to construct the new example with "patch". If you have problems with this, let me know and I can send you the entire example. --Russ Index: nctst.cc =================================================================== RCS file: /upc/share/CVS/netcdf-3/cxx/nctst.cc,v retrieving revision 1.2 diff -c -r1.2 nctst.cc *** 1.2 1997/11/19 20:33:28 --- nctst.cc 1998/05/07 18:46:17 *************** *** 67,83 **** static char* s = "1992-3-21 12:00" ; reftime->put(s, strlen(s)); ! static float P_data[] = { ! 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, ! 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973 }; // We could write all P data at once with P->put(P_data, P->edges()), // but instead we write one record at a time, to show use of setcur(). long rec = 0; // start at zero-th const long nrecs = 1; // # records to write ! P->put(&P_data[0], nrecs, NLATS, NLONS); // write zero-th record P->set_cur(++rec); // set to next record ! P->put(&P_data[NLATS*NLONS], nrecs, NLATS, NLONS); // write next record // close of nc takes place in destructor } --- 67,83 ---- static char* s = "1992-3-21 12:00" ; reftime->put(s, strlen(s)); ! static float P_data[2][4][3] = { ! {{950, 951, 952}, {953, 954, 955}, {956, 957, 958}, {959, 960, 961}}, ! {{962, 963, 964}, {965, 966, 967}, {968, 969, 970}, {971, 972, 973}} }; // We could write all P data at once with P->put(P_data, P->edges()), // but instead we write one record at a time, to show use of setcur(). long rec = 0; // start at zero-th const long nrecs = 1; // # records to write ! P->put(&P_data[0][0][0], nrecs, NLATS, NLONS); // write zero-th record P->set_cur(++rec); // set to next record ! P->put(&P_data[1][0][0], nrecs, NLATS, NLONS); // write next record // close of nc takes place in destructor }