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.
Hi Edin, > I am using netCDF library to save my data sets. I didn't have much trouble > using it until I faced the problem of saving the multidimensional array of > data which memory space was dynamically allocated during the process. > When dynamic allocation of single variable array was performed in contiguous > way, use of nc_put_var_ resulted in correct data set. However, when memory > for structured array is dynamically allocated in non contiguous way (relying > on pointers to guide me thru the data), then, as expected, simple use of > nc_put_var_ was not successful. > > Could you please advise what are my potential solutions to storing a > variable pointer array into netCDF data set. > I included some portions of my code for your convenience. If you are using netCDF-3, I suggest you store the data in pieces, each Z_SIZE values of a single parameter, in a nested loop for X_SIZE and Y_SIZE. There is no way to store all the values scattered all over memory with pointers returned from calloc using nc_put_varm_TYPE() calls, as these only handle regular spacing in memory determined by a linear function of the indices. So the psuedocode for writing out all the data might be something like: for (x = 0; x < X_SIZE; x++) { for (y = 0; y < Y_SIZE; y++) { start[0] = x; start[1] = y; start[2] = 0; count[0] = 1; count[1] = 1; count[2] = Z_SIZE; stride[0] = 1; stride[1] = 1; stride[2] = 1; imap[0] = 1; /* doesn't matter, won't be used */ imap[1] = 1; /* doesn't matter, won't be used */ imap[2] = sizeof(NODE_STRUCT); /* write subsampled or mapped array of values into netCDF variable */ nc_put_varm_float(ncid, Refl_id, start, count, stride, imap, &cart_grid[x][y][0].Refl); nc_put_varm_float(ncid, Vel_id, start, count, stride, imap, &cart_grid[x][y][0].Vel); ... nc_put_varm_int(ncid, Cnt_id, start, count, stride, imap, &cart_grid[x][y][0].Cnt); } } Another suggestion is to consider using netCDF-4, which permits writing all the values of a C struct at once into a netCDF variable of user-defined type, where you define the type to correspond to your NODE_STRUCT. That would permit making only a single nc_put_var() call within the nested loops above to replace the 14 calls for each struct member individually, but you would still need the nested loops on x and y. --Russ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu Ticket Details =================== Ticket ID: VRF-491011 Department: Support netCDF Priority: Normal Status: Closed