Simone Marras wrote:
#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;
should be:
count[0] = nx;
count[1] = ny;
//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");
no need to call nc_redef here
//Create dimensions:
status = nc_def_dim(ncid, "X", nx, &xdim);
status = nc_def_dim(ncid, "Y", ny, &ydim);
(side note: you should check status twice here)
//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);
you must call nc_enddef at that point
//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");
you must call nc_close at that point
return;
maybe you should return something, your function
is defined as returning an int
}
Hope that helps.