1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
#include <stdio.h>
#include <stdlib.h>
#include <netcdf.h>
#define Clear ((unsigned char)0)
#define Cumulonimbus ((unsigned char)1)
#define Stratus ((unsigned char)2)
typedef unsigned char enum_t;
typedef unsigned char opaque_t[11];
typedef nc_vlen_t vlen_t;
typedef struct g_cmpd_t {
vlen_t f1;
enum_t f2;
} g_cmpd_t;
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
main() {/* create tst_diskless2.nc */
int stat; /* return status */
int ncid; /* netCDF id */
/* group ids */
int root_grp;
int g_grp;
int h_grp;
/* type ids */
int enum_t_typ;
int opaque_t_typ;
int vlen_t_typ;
int g_cmpd_t_typ;
/* dimension ids */
int lat_dim;
int lon_dim;
int time_dim;
/* dimension lengths */
size_t lat_len = 10;
size_t lon_len = 5;
size_t time_len = NC_UNLIMITED;
/* variable ids */
int lat_id;
int lon_id;
int time_id;
int Z_id;
int t_id;
int p_id;
int rh_id;
int country_id;
int tag_id;
int h_compoundvar_id;
/* rank (number of dimensions) for each variable */
# define RANK_lat 1
# define RANK_lon 1
# define RANK_time 1
# define RANK_Z 3
# define RANK_t 3
# define RANK_p 3
# define RANK_rh 3
# define RANK_country 3
# define RANK_tag 0
# define RANK_h_compoundvar 0
/* variable shapes */
int lat_dims[RANK_lat];
int lon_dims[RANK_lon];
int time_dims[RANK_time];
int Z_dims[RANK_Z];
int t_dims[RANK_t];
int p_dims[RANK_p];
int rh_dims[RANK_rh];
int country_dims[RANK_country];
/* enter define mode */
stat = nc_create("tst_diskless2.nc", NC_DISKLESS|NC_WRITE|NC_CLOBBER|NC_NETCDF4, &ncid);
check_err(stat,__LINE__,__FILE__);
root_grp = ncid;
stat = nc_def_grp(root_grp, "g", &g_grp);
check_err(stat,__LINE__,__FILE__);
stat = nc_def_grp(root_grp, "h", &h_grp);
check_err(stat,__LINE__,__FILE__);
{
unsigned char econst;
stat = nc_def_enum(root_grp, NC_UBYTE, "enum_t", &enum_t_typ);
check_err(stat,__LINE__,__FILE__);
econst = 0;
stat = nc_insert_enum(root_grp, enum_t_typ, "Clear", &econst);
check_err(stat,__LINE__,__FILE__);
econst = 1;
stat = nc_insert_enum(root_grp, enum_t_typ, "Cumulonimbus", &econst);
check_err(stat,__LINE__,__FILE__);
econst = 2;
stat = nc_insert_enum(root_grp, enum_t_typ, "Stratus", &econst);
check_err(stat,__LINE__,__FILE__);
}
stat = nc_def_opaque(root_grp, 11, "opaque_t", &opaque_t_typ);
check_err(stat,__LINE__,__FILE__);
stat = nc_def_vlen(root_grp, "vlen_t", NC_INT, &vlen_t_typ); check_err(stat,__LINE__,__FILE__);
stat = nc_def_compound(g_grp, sizeof(g_cmpd_t), "cmpd_t", &g_cmpd_t_typ); check_err(stat,__LINE__,__FILE__);
{
stat = nc_insert_compound(g_grp, g_cmpd_t_typ, "f1", NC_COMPOUND_OFFSET(g_cmpd_t,f1), vlen_t_typ); check_err(stat,__LINE__,__FILE__);
stat = nc_insert_compound(g_grp, g_cmpd_t_typ, "f2", NC_COMPOUND_OFFSET(g_cmpd_t,f2), enum_t_typ); check_err(stat,__LINE__,__FILE__);
}
/* define dimensions */
stat = nc_def_dim(root_grp, "lat", lat_len, &lat_dim);
check_err(stat,__LINE__,__FILE__);
stat = nc_def_dim(root_grp, "lon", lon_len, &lon_dim);
check_err(stat,__LINE__,__FILE__);
stat = nc_def_dim(root_grp, "time", time_len, &time_dim);
check_err(stat,__LINE__,__FILE__);
/* define variables */
lat_dims[0] = lat_dim;
stat = nc_def_var(root_grp, "lat", NC_INT, RANK_lat, lat_dims, &lat_id);
check_err(stat,__LINE__,__FILE__);
lon_dims[0] = lon_dim;
stat = nc_def_var(root_grp, "lon", NC_INT, RANK_lon, lon_dims, &lon_id);
check_err(stat,__LINE__,__FILE__);
time_dims[0] = time_dim;
stat = nc_def_var(root_grp, "time", NC_INT, RANK_time, time_dims, &time_id);
check_err(stat,__LINE__,__FILE__);
Z_dims[0] = time_dim;
Z_dims[1] = lat_dim;
Z_dims[2] = lon_dim;
stat = nc_def_var(root_grp, "Z", NC_FLOAT, RANK_Z, Z_dims, &Z_id);
check_err(stat,__LINE__,__FILE__);
t_dims[0] = time_dim;
t_dims[1] = lat_dim;
t_dims[2] = lon_dim;
stat = nc_def_var(root_grp, "t", NC_FLOAT, RANK_t, t_dims, &t_id);
check_err(stat,__LINE__,__FILE__);
p_dims[0] = time_dim;
p_dims[1] = lat_dim;
p_dims[2] = lon_dim;
stat = nc_def_var(root_grp, "p", NC_DOUBLE, RANK_p, p_dims, &p_id);
check_err(stat,__LINE__,__FILE__);
rh_dims[0] = time_dim;
rh_dims[1] = lat_dim;
rh_dims[2] = lon_dim;
stat = nc_def_var(root_grp, "rh", NC_INT, RANK_rh, rh_dims, &rh_id);
check_err(stat,__LINE__,__FILE__);
country_dims[0] = time_dim;
country_dims[1] = lat_dim;
country_dims[2] = lon_dim;
stat = nc_def_var(root_grp, "country", NC_STRING, RANK_country, country_dims, &country_id);
check_err(stat,__LINE__,__FILE__);
stat = nc_def_var(root_grp, "tag", NC_UBYTE, RANK_tag, 0, &tag_id);
check_err(stat,__LINE__,__FILE__);
stat = nc_def_var(h_grp, "compoundvar", g_cmpd_t_typ, RANK_h_compoundvar, 0, &h_compoundvar_id);
check_err(stat,__LINE__,__FILE__);
/* assign global attributes */
{
static const int vlen_2[] = {17, 18, 19} ;
static const vlen_t globalatt_att[1] = {{3, (void*)vlen_2}} ;
stat = nc_put_att(root_grp, NC_GLOBAL, "globalatt", vlen_t_typ, 1, globalatt_att);
check_err(stat,__LINE__,__FILE__);
}
/* assign per-variable attributes */
{
stat = nc_put_att_text(root_grp, lat_id, "long_name", 8, "latitude");
check_err(stat,__LINE__,__FILE__);
}
{
stat = nc_put_att_text(root_grp, lat_id, "units", 13, "degrees_north");
check_err(stat,__LINE__,__FILE__);
}
{
stat = nc_put_att_text(root_grp, lon_id, "long_name", 9, "longitude");
check_err(stat,__LINE__,__FILE__);
}
{
stat = nc_put_att_text(root_grp, lon_id, "units", 12, "degrees_east");
check_err(stat,__LINE__,__FILE__);
}
{
stat = nc_put_att_text(root_grp, time_id, "units", 31, "seconds since 1992-1-1 00:00:00");
check_err(stat,__LINE__,__FILE__);
}
{
static const char* Z_units_att[1] = {"geopotential meters"} ;
stat = nc_put_att_string(root_grp, Z_id, "units", 1, Z_units_att); check_err(stat,__LINE__,__FILE__);
}
{
static const float Z_valid_range_att[2] = {((float)0), ((float)5000)} ;
stat = nc_put_att_float(root_grp, Z_id, "valid_range", NC_FLOAT, 2, Z_valid_range_att);
check_err(stat,__LINE__,__FILE__);
}
{
static const double p_FillValue_att[1] = {((double)-9999)} ;
stat = nc_put_att_double(root_grp, p_id, "_FillValue", NC_DOUBLE, 1, p_FillValue_att);
check_err(stat,__LINE__,__FILE__);
}
{
static const int rh_FillValue_att[1] = {-1} ;
stat = nc_put_att_int(root_grp, rh_id, "_FillValue", NC_INT, 1, rh_FillValue_att);
check_err(stat,__LINE__,__FILE__);
}
/* leave define mode */
stat = nc_enddef (root_grp);
check_err(stat,__LINE__,__FILE__);
/* assign variable data */
{
int lat_data[10] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90} ;
size_t lat_startset[1] = {0} ;
size_t lat_countset[1] = {10};
stat = nc_put_vara(root_grp, lat_id, lat_startset, lat_countset, lat_data);
check_err(stat,__LINE__,__FILE__);
}
{
int lon_data[5] = {-140, -118, -96, -84, -52} ;
size_t lon_startset[1] = {0} ;
size_t lon_countset[1] = {5};
stat = nc_put_vara(root_grp, lon_id, lon_startset, lon_countset, lon_data);
check_err(stat,__LINE__,__FILE__);
}
{
static const int vlen_10[] = {3, 4, 5} ;
size_t zero = 0;
static g_cmpd_t h_compoundvar_data[1] = {{{3, (void*)vlen_10}, 2}};
stat = nc_put_var1(h_grp, h_compoundvar_id, &zero, h_compoundvar_data);
check_err(stat,__LINE__,__FILE__);
}
stat = nc_close(root_grp);
check_err(stat,__LINE__,__FILE__);
return 0;
}
|