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.
>To: address@hidden >From: Igor Pesenson <address@hidden> >Subject: Re: 20030203: subject >Organization: Lawrence Berkeley National Laboratory >Keywords: 200302031956.h13Ju0623075 Hi Igor, > I've only recentely started using cdf and I have two, hopefully > simple, questions. > 1) I'm trying to document an attribute in the following way (taken from > someone else's file): > int base_time ; > base_time:string = "12-Nov-2002,0:00:00 GMT" ; > base_time:long_name = "Base time in Epoch" ; > But I can't figure out how to do this. I've created a char variable to > hold the "string" value, but I can't seem to put it in properly. My code > says: > > char base_time_str[25]; > nc_status=nc_def_var(ncid,"base_time",NC_INT,0,&idimid,&ivarid[1]); > nc_status=nc_put_att_text(ncid,out.ivarid[1],"string",strlen(base_time_str), > base_time_str); > > and it fails with the result of: > > int base_time ; > base_time:string = "" ; > The cdf documentation requires that the base_time_str was type "const > char" but this would prevent it changing from file to file. Am I > horribly misguided? I think > nc_status=nc_put_att_text(ncid,out.ivarid[1],"string",strlen(base_time_str), > base_time_str); should instead be nc_status=nc_put_att_text(ncid,ivarid[1],"string",strlen(base_time_str), base_time_str); given that you stored the variable ID in "ivarid[1]". I've appended a C program generated by using "ncgen -c igor.cdl" that seems to work, if you need a complete example. I used the following CDL file for input to ncgen: netcdf igor { variables: int base_time ; base_time:string = "12-Nov-2002,0:00:00 GMT" ; base_time:long_name = "Base time in Epoch" ; } I'll send a separate reply to your second question, assuming I can figure it out. --Russ #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)); exit(1); } } int main() { /* create igor.nc */ int ncid; /* netCDF id */ /* variable ids */ int base_time_id; /* rank (number of dimensions) for each variable */ # define RANK_base_time 0 /* enter define mode */ int stat = nc_create("igor.nc", NC_CLOBBER, &ncid); check_err(stat,__LINE__,__FILE__); /* define variables */ stat = nc_def_var(ncid, "base_time", NC_INT, RANK_base_time, 0, &base_time_id); check_err(stat,__LINE__,__FILE__); /* assign attributes */ stat = nc_put_att_text(ncid, base_time_id, "string", 23, "12-Nov-2002,0:00:00 GMT"); check_err(stat,__LINE__,__FILE__); stat = nc_put_att_text(ncid, base_time_id, "long_name", 18, "Base time in Epoch"); check_err(stat,__LINE__,__FILE__); /* leave define mode */ stat = nc_enddef (ncid); check_err(stat,__LINE__,__FILE__); stat = nc_close(ncid); check_err(stat,__LINE__,__FILE__); return 0; }