[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[netCDF #FTI-935060]: Questions regarding string fields for NetCDF-4 format

This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.


  • Subject: [netCDF #FTI-935060]: Questions regarding string fields for NetCDF-4 format
  • Date: Tue, 20 Dec 2016 13:12:55 -0700

I put together a program that might embody what you are doing.
Does it convey what you are trying to do?

#include <stdlib.h>
#include <stdio.h>
#include <netcdf.h>
#include <string.h>

/* The data file we will create. */
#define FILE_NAME "t.nc"
#define NDIMS 2
#define NX 2
#define NY 2

static const char* data[4] = {"abc", "def", "ghi", "jkl"};

static void
fail(int ret)
{
    fprintf(stderr,"fail: %d:%s\n",ret,nc_strerror(ret));
    exit(1);
}

int
main(int argc, char **argv)
{
    int ret = NC_NOERR;
    int ncid, varid;
    int dimids[NDIMS];
    
    if((ret=nc_create(FILE_NAME, NC_CLOBBER|NC_NETCDF4, &ncid)))
        fail(ret);
    
    if((ret=nc_def_dim(ncid, "nx", NX, &dimids[0])))
        fail(ret);
    if((ret=nc_def_dim(ncid, "ny", NY, &dimids[1])))
        fail(ret);
    
    if((ret=nc_def_var(ncid, "var", NC_STRING, NDIMS, dimids, &varid)))
        fail(ret);
    
    if((ret=nc_enddef(ncid)))
        fail(ret);
    
    if((ret=nc_put_var(ncid, varid, data)))
        fail(ret);
    
    if((ret=nc_close(ncid)))
        fail(ret);
    
    return 0;
}
    

=Dennis Heimbigner
  Unidata


Ticket Details
===================
Ticket ID: FTI-935060
Department: Support netCDF
Priority: Normal
Status: Open
===================
NOTE: All email exchanges with Unidata User Support are recorded in the Unidata 
inquiry tracking system and then made publicly available through the web.  If 
you do not want to have your interactions made available in this way, you must 
let us know in each email you send to us.