netcdf-c/ncdump/ref_ctest_small_3.c

82 lines
2.0 KiB
C
Raw Permalink Normal View History

2019-11-01 06:08:57 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <netcdf.h>
void
check_err(const int stat, const int line, const char *file) {
if (stat != NC_NOERR) {
(void)fprintf(stderr,"line %d of %s: %s\n", line, file, nc_strerror(stat));
fflush(stderr);
exit(1);
}
}
int
2019-11-04 03:03:13 +08:00
main() {/* create ref_tst_small.nc */
2019-11-01 06:08:57 +08:00
int stat; /* return status */
int ncid; /* netCDF id */
2019-11-04 03:03:13 +08:00
/* dimension ids */
int Time_dim;
int DateStrLen_dim;
/* dimension lengths */
size_t Time_len = NC_UNLIMITED;
size_t DateStrLen_len = 19;
2019-11-01 06:08:57 +08:00
/* variable ids */
2019-11-04 03:03:13 +08:00
int Times_id;
2019-11-01 06:08:57 +08:00
/* rank (number of dimensions) for each variable */
2019-11-04 03:03:13 +08:00
# define RANK_Times 2
/* variable shapes */
int Times_dims[RANK_Times];
2019-11-01 06:08:57 +08:00
/* enter define mode */
2019-11-04 03:03:13 +08:00
stat = nc_create("ref_tst_small.nc", NC_CLOBBER, &ncid);
check_err(stat,__LINE__,__FILE__);
/* define dimensions */
stat = nc_def_dim(ncid, "Time", Time_len, &Time_dim);
check_err(stat,__LINE__,__FILE__);
stat = nc_def_dim(ncid, "DateStrLen", DateStrLen_len, &DateStrLen_dim);
2019-11-01 06:08:57 +08:00
check_err(stat,__LINE__,__FILE__);
/* define variables */
2019-11-04 03:03:13 +08:00
Times_dims[0] = Time_dim;
Times_dims[1] = DateStrLen_dim;
stat = nc_def_var(ncid, "Times", NC_CHAR, RANK_Times, Times_dims, &Times_id);
2019-11-01 06:08:57 +08:00
check_err(stat,__LINE__,__FILE__);
2019-11-04 03:03:13 +08:00
/* assign global attributes */
{
stat = nc_put_att_text(ncid, NC_GLOBAL, "TITLE", 31, " OUTPUT FROM WRF V2.0.3.1 MODEL");
check_err(stat,__LINE__,__FILE__);
}
2019-11-01 06:08:57 +08:00
/* leave define mode */
stat = nc_enddef (ncid);
check_err(stat,__LINE__,__FILE__);
/* assign variable data */
{
2019-11-04 03:03:13 +08:00
char* Times_data = "2005-04-11_12:00:002005-04-11_13:00:00" ;
size_t Times_startset[2] = {0, 0} ;
size_t Times_countset[2] = {2, 19};
stat = nc_put_vara(ncid, Times_id, Times_startset, Times_countset, Times_data);
2019-11-01 06:08:57 +08:00
check_err(stat,__LINE__,__FILE__);
}
stat = nc_close(ncid);
check_err(stat,__LINE__,__FILE__);
return 0;
}