#include #include #include #define FILENAME "test.nc" int main() { int ncid; int dim1_id, dim2_id, var1_id, var2_id, status; int var1ndims = 1; // vector int var1size = 3; // length 3 int var2ndims = 1; // vector int var2size = 2; // length 2 // create netcdf-4 classic file and overwrite if already exists; note MATLAB nccreate defaults to netcdf4-classic status = nc_create(FILENAME, NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid); if(status != NC_NOERR) { printf("Could not create file.\n"); } // define the dimension for var1 status = nc_def_dim(ncid, "dim1", var1size, &dim1_id); if(status != NC_NOERR) { printf("Could not create dim1.\n"); } // create var1 with dim1 status = nc_def_var(ncid, "var1", NC_DOUBLE, var1ndims, &dim1_id, &var1_id); if(status != NC_NOERR) { printf("Could not create var1.\n"); } // close the file status = nc_close(ncid); printf("status after close = %d\n", status); // close the file status = nc_close(ncid); printf("error code after close = %d\n", status); // now reopen the file and add a second variable with dimension name as previously-defined var1 printf("reopening file to write a second variable...\n"); status = nc_open("test.nc", NC_WRITE, &ncid); // put back into define mode status = nc_redef(ncid); if(status != NC_NOERR) { printf("Could not put back into define mode.\n"); } printf("status = %d\n", status); // define the dimension for var2 -- give it same name as existing var1 status = nc_def_dim(ncid, "var1", var2size, &dim2_id); if(status != NC_NOERR) { printf("Could not create dim2.\n"); } // create var2 with same dimension name as previously-defined var1, this should go awry status = nc_def_var(ncid, "var2", NC_DOUBLE, var2ndims, &dim2_id, &var2_id); if(status != NC_NOERR) { printf("Could not create var2.\n"); } printf("error code = %d\n", status); // close the file status = nc_close(ncid); printf("error code after close = %d\n", status); // close the file status = nc_close(ncid); printf("error code after close = %d\n", status); printf("End of test.\n\n"); return 0; }