Hi everyone,
I am having some issues when writing an array of floats into a new
netcdf dataset created using nc_create().
Specifically, although all goes well (compilation is correct, without
errors or warning), and the call to nc_create is free of errors, and
so is the definition of the variable dimensions, I get an error when I
call first nc_redef() and then function nc_put_vara_float()
Could anyone help me see what it is that I am doing wrong?
Thank you very much in advance,
Simone
Here is my code for a better understanding:
(wrt2NCDF() is called by main.c)
#include<stdio.h>
#include<netcdf.h>
#define DIMENSIONS 2
int wrt2NCDF(int nx, int ny)
{
int i,j,k;
int status; //Error status
int ncid; //NetCDF ID
int xdim, ydim; //dimensions in X and Y
int theta_id; //id of variable Theta
int vars_dimids[DIMENSIONS]; //id of the variable dimensions
static size_t start[DIMENSIONS];//Start at first value
static size_t count[DIMENSIONS];
float theta[nx*ny];
/***********************************************************/
/*Preliminary computations to prepare some strings**********/
/***********************************************************/
start[0] = 0;
start[1] = 0;
count[0] = nx+1;
count[1] = ny+1;
//Create/open dataset:
status = nc_create("test.nc", 0, &ncid);
if(status != NC_NOERR)
printf(" # Error in Creating file %d %d\n", status, NC_NOERR);
else printf("\n # OK - File 'mbm.nc' created with ID: %d\n\n", ncid);
// ERROR HERE Put the netCDF file in define mode so that variables
can be added:
!!!!!! status = nc_redef(ncid);
!!!!!! if(status != NC_NOERR)
!!!!!! printf(" # Error in setting the file in Define Mode\n");
//Create dimensions:
status = nc_def_dim(ncid, "X", nx, &xdim);
status = nc_def_dim(ncid, "Y", ny, &ydim);
//Create variables: THETA, MIXRA
vars_dimids[0] = xdim;
vars_dimids[1] = ydim;
status = nc_def_var(ncid, "THETA", NC_FLOAT, 2, vars_dimids,
&theta_id);
status = nc_def_var(ncid, "MIXRA", NC_FLOAT, 2, vars_dimids,
&mixra_id);
//Write the array to the nc file:
for(i = 0; i < nx*ny; i++){
theta[i] = 0.5;
mixra[i] = 1.0;
}
!!!! ERROR HERE
status = nc_put_vara_float(ncid, theta_id, start, count, theta);
if (status != NC_NOERR)
printf(" # Error in writing THETA values into the nc file\n");
return;
}