[netcdfgroup] Creating NetCDF dataset from 2D array by progressive writing of array columns

Hello--

I would like to store an M-by-N array in a NetCDF file. The array would be stored as a single variable. I know the values of M and N, but due to physical memory limitations on my desktop computer, I cannot load the entire 2D array, which would consist of thousands of rows and hundreds of columns. Instead of loading the entire 2D array, I would like to load M-by-1 arrays (1D arrays), and then progressively add each column to the NetCDF file to create the M-by-N array.

Is this possible? The following code snippet (based on the C++ example given in the documentation) demonstrates what I would like to accomplish. However, after running this code snippet, the NetCDF file produced appears to be nothing but junk data. (I am using ParaView from Kitware as the NetCDF viewer.)

/// start of program
 #include <iostream>
 #include <netcdfcpp.h>

 using namespace std;
 static const int NX = 100;
 static const int NY = 100;
 static const int NC_ERR = 2;

 int main(void)
  {
       int dataOut[NX];

        for(int i = 0; i < NX; i++)
            dataOut[i] = i;

        NcFile dataFile("simple_xy.nc", NcFile::Replace);

        if (!dataFile.is_valid())
        {
           cout << "Couldn't open file!\n";
           return NC_ERR;
        }

        NcDim* xDim = dataFile.add_dim("x", NX);
        NcDim* yDim = dataFile.add_dim("y", NY);
        NcVar *data = dataFile.add_var("data", ncInt, xDim, yDim);

        // Write each column to file?
        for(int i = 0; i <  NY; i++)
            data->put( dataOut, i, NX );

        return 0;
}


/// end of program

Thank you for helping to shed some light on this matter.

Nicholas Kinar
n.kinar@xxxxxxxx
Researcher, Centre for Hydrology
University of Saskatchewan
Saskatoon, Saskatchewan, CANADA






  • 2010 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: