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.
David, The netCDF library stores on disk an array of in-memory values in the same order as the values exist in memory, i.e. contiguous memory locations map to contiguous on-disk locations (with the exception of arrays whose outermost dimension has been declared as unlimited -- but this exception may safely be ignored). Put another way, memory locations of an array vary most rapidly for the innermost dimension of the array -- the same holds true for the on-disk locations of the array. The C programming language has the outermost dimension in the leftmost position and the innermost dimension in the rightmost position. Consequently, the C netCDF API adopts the convention that the outermost dimension maps to the first element of an index vector and the innermost dimension maps to the last element. The Fortran programming language has the outermost dimension in the rightmost position and the innermost dimension in the leftmost position. Consequently, the Fortran netCDF API adopts the convention that the innermost dimension maps to the first element of an vector and the outermost dimension maps to the last element. The Fortran interface layer reverses the order of these vectors and changes the indicies from 1-based to 0-based before invoking the C routines. Now for your particular example: > To: address@hidden > cc: address@hidden > From: "David C. Fox" <address@hidden> > Subject: array element order > Organization: Harvard > Keywords: 200105121848.f4CImop14346 The above message contained the following: > The documentation says that multidimensional arrays are stored in the > same order regardless of the language and can be stored and retrieved by > programs in different languages. I assume that means > that if you create > > real a(3, 5) In the above, the innermost dimension has length 3 and the outermost dimension has length 5. In Fortran, the index vector for the last element would be integer ilast(2) / 3, 5 / whereas in C it would be int ilast[2] = {4, 2}; > in Fortran and store it using the Fortran netCDF interface, the Fortran > netCDF interfaces uses the Fortran-ordering to retrieve the contents of > a from memory and then stores it in the file in netCDF order The "netCDF order" of an array on-disk is the same as the in-memory order. It is the left/right ordering of the dimensions that differs between the C and Fortran languages -- not the order of array elements in-memory or on disk. > whereas if you then read it in to a C array > > float a[3][5] The above declaration would be incorrect for the above "a" array because it reverses the sizes of the dimensions. The correct C declaration would be float a[5][3] because, in C, the leftmost dimension is the outermost and the rightmost dimension is the innermost. I hope this explanation helps. Feel free to contact me if you have additional questions. Regards, Steve Emmerson <http://www.unidata.ucar.edu>