cleanup of simple.c

This commit is contained in:
Ed Hartnett 2019-07-02 08:59:27 -06:00
parent c8a2ad233f
commit 472fd0b348

View File

@ -1,12 +1,10 @@
/*! \file /*! \file
Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
2015, 2016, 2017, 2018 2015, 2016, 2017, 2018
University Corporation for Atmospheric Research/Unidata. University Corporation for Atmospheric Research/Unidata.
See \ref copyright file for more info.
See \ref copyright file for more info.
*/ */
/* This example program is part of Unidata's netCDF library for /* This example program is part of Unidata's netCDF library for
@ -15,7 +13,6 @@ See \ref copyright file for more info.
How about a short, but meaningful, netCDF program? How about a short, but meaningful, netCDF program?
Ed Hartnett, 6/19/4 Ed Hartnett, 6/19/4
$Id: simple.c,v 1.1 2004/07/26 14:04:42 ed Exp $
*/ */
#include <netcdf.h> #include <netcdf.h>
@ -25,11 +22,11 @@ See \ref copyright file for more info.
/* This macro handles errors by outputting a message to stdout and /* This macro handles errors by outputting a message to stdout and
then exiting. */ then exiting. */
#define NC_EXAMPLE_ERROR 2 /* This is the exit code for failure. */ #define NC_EXAMPLE_ERROR 2 /* This is the exit code for failure. */
#define BAIL(e) do { \ #define BAIL(e) do { \
printf("Bailing out in file %s, line %d, error:%s.\n", \ printf("Bailing out in file %s, line %d, error:%s.\n", \
__FILE__, __LINE__, nc_strerror(e)); \ __FILE__, __LINE__, nc_strerror(e)); \
return NC_EXAMPLE_ERROR; \ return NC_EXAMPLE_ERROR; \
} while (0) } while (0)
#define NUMDIMS 2 #define NUMDIMS 2
#define NUMVARS 1 #define NUMVARS 1
@ -40,45 +37,45 @@ return NC_EXAMPLE_ERROR; \
int int
main() main()
{ {
int ncid, temp_varid, dimids[NUMDIMS]; int ncid, temp_varid, dimids[NUMDIMS];
float temp[LAT_LEN][LON_LEN], *fp; float temp[LAT_LEN][LON_LEN], *fp;
int i, res; int i, res;
/* Create a bunch of phoney data so we have something to write in /* Create a bunch of phoney data so we have something to write in
the example file. */ the example file. */
for (fp=(float *)temp, i=0; i<LAT_LEN*LON_LEN; i++) for (fp=(float *)temp, i=0; i<LAT_LEN*LON_LEN; i++)
*fp++ = 10. + i/10.; *fp++ = 10. + i/10.;
/* Create the netCDF file. */ /* Create the netCDF file. */
if ((res = nc_create("short.nc", NC_CLOBBER, &ncid))) if ((res = nc_create("short.nc", NC_CLOBBER, &ncid)))
BAIL(res); BAIL(res);
/* Define dimensions. */ /* Define dimensions. */
if ((res = nc_def_dim(ncid, "latitude", LAT_LEN, dimids))) if ((res = nc_def_dim(ncid, "latitude", LAT_LEN, dimids)))
BAIL(res); BAIL(res);
if ((res = nc_def_dim(ncid, "longitude", LON_LEN, &dimids[1]))) if ((res = nc_def_dim(ncid, "longitude", LON_LEN, &dimids[1])))
BAIL(res); BAIL(res);
/* Define the variable. */ /* Define the variable. */
if ((res = nc_def_var(ncid, "sfc_temp", NC_FLOAT, NUMDIMS, if ((res = nc_def_var(ncid, "sfc_temp", NC_FLOAT, NUMDIMS,
dimids, &temp_varid))) dimids, &temp_varid)))
BAIL(res); BAIL(res);
/* We'll store the units. */ /* We'll store the units. */
if ((res = nc_put_att_text(ncid, temp_varid, "units", if ((res = nc_put_att_text(ncid, temp_varid, "units",
strlen(CELSIUS), CELSIUS))) strlen(CELSIUS), CELSIUS)))
BAIL(res); BAIL(res);
/* We're finished defining metadata. */ /* We're finished defining metadata. */
if ((res = nc_enddef(ncid))) if ((res = nc_enddef(ncid)))
BAIL(res); BAIL(res);
if ((res = nc_put_var_float(ncid, temp_varid, (float *)temp))) if ((res = nc_put_var_float(ncid, temp_varid, (float *)temp)))
BAIL(res); BAIL(res);
/* We're done! */ /* We're done! */
if ((res = nc_close(ncid))) if ((res = nc_close(ncid)))
BAIL(res); BAIL(res);
return 0; return 0;
} }