Jeff Whitaker wrote:
#include <stdio.h>
#include <string.h>
#include <netcdf.h>
#define FILE_NAME "test.nc"
#define NDIMS 2
#define NLAT 10
#define NLON 20 #define LAT_NAME "lat"
#define LON_NAME "lon"
#define START_LAT 25.0
#define START_LON -125.0
int
main()
{
int ncid, lon_dimid, lat_dimid;
int lat_varid, lon_varid;
int dimids[NDIMS];
float lats[NLAT], lons[NLON];
int lat, lon;
int retval;
for (lat = 0; lat < NLAT; lat++)
lats[lat] = START_LAT + 5.*lat;
for (lon = 0; lon < NLON; lon++)
lons[lon] = START_LON + 5.*lon;
retval = nc_create(FILE_NAME, NC_NETCDF4 | NC_CLOBBER, &ncid);
retval = nc_def_dim(ncid, LAT_NAME, NLAT, &lat_dimid);
retval = nc_def_dim(ncid, LON_NAME, NLON, &lon_dimid);
retval = nc_def_var(ncid, LON_NAME, NC_FLOAT, 1, &lon_dimid,
&lon_varid);
retval = nc_put_var_float(ncid, lon_varid, &lons[0]);
retval = nc_def_var(ncid, LAT_NAME, NC_FLOAT, 1, &lat_dimid,
&lat_varid);
retval = nc_put_var_float(ncid, lat_varid, &lats[0]);
retval = nc_close(ncid);
return 0;
}
I had weird results too, with a previous daily snapshot I
have here.
But if I call "nc_put_var_float" after defining everything,
and put a "nc_enddef(ncid)" in between, then the resulting
file seems correct. (See code below.)
Maybe try that?
By the way you should check the value of retval after
each call to the netcdf library and react in a way or
another if it is not 0, which means there was a problem.
On your example, I have a failure at the first
"nc_put_var_float" with the old daily snapshot (but no
failure with 4.0.1).
Modified example:
---------------
#include <stdio.h>
#include <string.h>
#include <netcdf.h>
#define FILE_NAME "test.nc"
#define NDIMS 2
#define NLAT 10
#define NLON 20
#define LAT_NAME "lat"
#define LON_NAME "lon"
#define START_LAT 25.0
#define START_LON -125.0
int
main()
{
int ncid, lon_dimid, lat_dimid;
int lat_varid, lon_varid;
int dimids[NDIMS];
float lats[NLAT], lons[NLON];
int lat, lon;
int retval;
for (lat = 0; lat < NLAT; lat++)
lats[lat] = START_LAT + 5.*lat;
for (lon = 0; lon < NLON; lon++)
lons[lon] = START_LON + 5.*lon;
retval = nc_create(FILE_NAME, NC_NETCDF4 | NC_CLOBBER, &ncid);
retval = nc_def_dim(ncid, LAT_NAME, NLAT, &lat_dimid);
retval = nc_def_dim(ncid, LON_NAME, NLON, &lon_dimid);
retval = nc_def_var(ncid, LON_NAME, NC_FLOAT, 1, &lon_dimid, &lon_varid);
retval = nc_def_var(ncid, LAT_NAME, NC_FLOAT, 1, &lat_dimid, &lat_varid);
nc_enddef(ncid);
retval = nc_put_var_float(ncid, lon_varid, &lons[0]);
retval = nc_put_var_float(ncid, lat_varid, &lats[0]);
retval = nc_close(ncid);
return 0;
}
---------------