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

Re: Dynamic allocation of arrays



> Organization: Arkansas-Red Basin River Forecast Center
> Keywords: 199408051749.AA04434

Hi Suzanne,

> I have been attempting, without success, to create a NETCDF
> file of gridded precipitation data.  My problem is due to the
> way I am allocating memory for the array to hold the data.  I
> am using dynamic allocation, instead of specifying the dimension
> specifically.  
> 
> The NETCDF procedure "ncvarput" (I am using the "C" interface)
> cannot seem to decipher the information stored in a dynamically
> allocated array.  I have attempted to define the array as a
> NETCDF variable with no success.  Any suggestions?

We use ncvarput with dynamically allocated arrays all the time with no
problems.  I would have to see an example of your use of ncvarput to
diagnose the problem.  I have appended a simple example that works, in case
that helps.  If this is run and compiled, the resulting file "test.nc" can
be viewed with ncdump and contains the expected data.

--
Russ Rew                                                   UCAR Unidata Program
address@hidden                                             P.O. Box 3000
http://www.unidata.ucar.edu/                             Boulder, CO 80307-3000

#include <stdlib.h>
#include <stdio.h>
#include "netcdf.h"

#define NVALS   10

main() {
    int ncid, dimid, varid;
    int i;
    
    int dims[NVALS];
    float *fp;
    static long start[] = {0};    /* start at first value */
    static long count[] = {NVALS};

    /* Create a new netCDF file */
    ncid = nccreate("test.nc", NC_CLOBBER);

    /* define a dimension */
    dimid = ncdimdef(ncid, "n", NVALS);

    /* define a 1-D variable using that dimension */
    dims[0] = dimid;
    varid = ncvardef(ncid, "v", NC_FLOAT, 1, dims);

    /* leave define mode */
    ncendef (ncid);

    /* Allocate some space for NVALS floating-point values */
    fp = (float *) malloc(NVALS * sizeof(float));
    for (i=0; i < NVALS; i++)
        fp[i] = i;
    
    /* put data into netCDF variable */
    ncvarput(ncid, varid, start, count, (void *) fp);

    ncclose(ncid);
}