static void ncdf_write_file(const char *filename, const struct gibbs_problem *p) { int ncid; int time_dim_id; int cons_dim_id; int draw_dim_id; int dim_ids[3]; int data_var_id; int mthet_var_id; int msig_var_id; size_t i; int err; err = nc_create(filename, NC_CLOBBER | NC_CLASSIC_MODEL, &ncid); if (err != NC_NOERR) { fprintf(stderr, "%s\n", nc_strerror(err)); return; } nc_def_dim(ncid, "time", p->data->size1, &time_dim_id); nc_def_dim(ncid, "constituents", p->data->size2, &cons_dim_id); nc_def_dim(ncid, "draws", p->draws, &draw_dim_id); nc_put_att_long(ncid, NC_GLOBAL, "iterations", NC_INT, 1, (long *) &p->iterations); nc_put_att_long(ncid, NC_GLOBAL, "burn", NC_INT, 1, (long *) &p->burn); nc_put_att_long(ncid, NC_GLOBAL, "seed", NC_INT, 1, &p->seed); nc_def_var(ncid, "mean", NC_DOUBLE, 1, &cons_dim_id, &mthet_var_id); dim_ids[0] = cons_dim_id; dim_ids[1] = cons_dim_id; nc_def_var(ncid, "covariance", NC_DOUBLE, 2, dim_ids, &msig_var_id); dim_ids[2] = cons_dim_id; dim_ids[1] = time_dim_id; dim_ids[0] = draw_dim_id; nc_def_var(ncid, "data", NC_DOUBLE, 3, dim_ids, &data_var_id); nc_enddef(ncid); nc_put_var_double(ncid, mthet_var_id, p->mthet->data); if (p->msig->size2 == p->msig->tda) { nc_put_var_double(ncid, msig_var_id, p->msig->data); } else { for (i = 0; i < p->msig->size1; i++) { gsl_vector_const_view v = gsl_matrix_const_row(p->msig, i); size_t start[2] = {0, i}; size_t count[2] = {p->msig->size2, 1}; nc_put_vara_double(ncid, msig_var_id, start, count, v.vector.data); } } for (i = 0; i < p->draws; i++) { const gsl_matrix *m = p->ddata[i]; if (m->size2 == m->tda) { size_t start[3] = {i, 0, 0}; size_t count[3] = {1, m->size1, m->size2}; nc_put_vara_double(ncid, data_var_id, start, count, m->data); } else { size_t j; for (j = 0; j < m->size1; i++) { gsl_vector_const_view v = gsl_matrix_const_row(m, j); size_t start[3] = {0, j, i}; size_t count[3] = {m->size2, 1, 1}; nc_put_vara_double(ncid, data_var_id, start, count, v.vector.data); } } } nc_close(ncid); }
/* * Add or changes a numeric variable or global attribute of an open * netCDF file. */ static void c_ncapt ( int ncid, /* netCDF ID */ int varid, /* variable ID */ const char* attname, /* attribute name */ nc_type datatype, /* attribute datatype */ size_t attlen, /* attribute length */ const void* value, /* pointer to data values */ int* rcode /* returned error code */ ) { int status; switch (datatype) { case NC_CHAR: status = NC_ECHAR; break; case NC_BYTE: # if NF_INT1_IS_C_SIGNED_CHAR status = nc_put_att_schar(ncid, varid, attname, datatype, attlen, (const signed char*)value); # elif NF_INT1_IS_C_SHORT status = nc_put_att_short(ncid, varid, attname, datatype, attlen, (const short*)value); # elif NF_INT1_IS_C_INT status = nc_put_att_int(ncid, varid, attname, datatype, attlen, (const int*)value); # elif NF_INT1_IS_C_LONG status = nc_put_att_long(ncid, varid, attname, datatype, attlen, (const long*)value); # endif break; case NC_SHORT: # if NF_INT2_IS_C_SHORT status = nc_put_att_short(ncid, varid, attname, datatype, attlen, (const short*)value); # elif NF_INT2_IS_C_INT status = nc_put_att_int(ncid, varid, attname, datatype, attlen, (const int*)value); # elif NF_INT2_IS_C_LONG status = nc_put_att_long(ncid, varid, attname, datatype, attlen, (const long*)value); # endif break; case NC_INT: # if NF_INT_IS_C_INT status = nc_put_att_int(ncid, varid, attname, datatype, attlen, (const int*)value); # elif NF_INT_IS_C_LONG status = nc_put_att_long(ncid, varid, attname, datatype, attlen, (const long*)value); # endif break; case NC_FLOAT: # if NF_REAL_IS_C_FLOAT status = nc_put_att_float(ncid, varid, attname, datatype, attlen, (const float*)value); # elif NF_REAL_IS_C_DOUBLE status = nc_put_att_double(ncid, varid, attname, datatype, attlen, (const double*)value); # endif break; case NC_DOUBLE: # if NF_DOUBLEPRECISION_IS_C_FLOAT status = nc_put_att_float(ncid, varid, attname, datatype, attlen, (const float*)value); # elif NF_DOUBLEPRECISION_IS_C_DOUBLE status = nc_put_att_double(ncid, varid, attname, datatype, attlen, (const double*)value); # endif break; } if (status == 0) *rcode = 0; else { nc_advise("NCAPT", status, ""); *rcode = ncerr; } }
int CNetCdfInterface::ncPutAttType(int ncid, int varid, const char* attrName, StdSize numVal, const long* data) { return nc_put_att_long(ncid, varid, attrName, NC_LONG, numVal, data); }