Re: [netcdfgroup] setting a global string attribute using nc_put_att() --- is this legal?

My understanding is the argument "len" in nc_put_att_string()
is the number of strings, not strlen of one string.

Wei-keng

On Nov 1, 2017, at 1:29 PM, Dan Hitt wrote:

> Awesome!
> 
> Thanks for cluing me in.
> 
> I was indeed not setting the file type, which i'm now doing in nc_create() by
>     nc_create( outfile, NC_NOCLOBBER | NC_NETCDF4, &ncid );
> 
> So i no longer get kicked out with error -45.
> 
> However, i am now segfaulting inside the nc_put_att_string() call,
> where it is apparently invoking strlen() (per gdb).
> 
> So i wonder if there's another piece of setup that i am missing.
> 
> Here's a complete program which illustrates the error (the malloc
> business at the top is to make sure that the string storage i pass in
> does not get optimized away or anything hokey like that):
> 
> #include <stdlib.h>
> #include <stdio.h>
> #include <netcdf.h>
> #include <string.h>
> 
> char* copy_string( const char* s ) {
>  char* new_str = malloc( strlen(s) + 1);
>  strcpy( new_str, s );
>  return new;
> }
> 
> int main(int argc,char** argv ) {
>  char* outfile = "deleteme.nc";
>  int ncid;
>  int status = nc_create( outfile, NC_NOCLOBBER | NC_NETCDF4, &ncid );
>  if ( status ) {
>    fprintf(stderr,"Could not create `%s'; failed with status %d: %s\n",
>            outfile, status, nc_strerror( status ) );
>    return 1;
>  }
>  const char* key = copy_string( "my-key" );
>  const char* val = copy_string( "my value" );
>  status = nc_put_att_string( ncid, NC_GLOBAL, key, strlen(val), &val );
>  if ( status ) {
>    fprintf(stderr,
>            "Failed on attempt to add string metadata; status %d: %s.\n",
>            status, nc_strerror( status ) );
>    return 2;
>  }
>  status = nc_close( ncid );
>  if ( status ) {
>    fprintf(stderr,"Failed on attempt to close %s; error %d: %s.\n",
>            outfile, status, nc_strerror( status ) );
>    return 3;
>  }
>  return 0;
> }
> 
> If there's anything obvious wrong with this, please let me know, or if
> you have any suggestions for debugging, please let me know, and TIA!
> 
> And thanks again for your earlier remark about the file type, which i
> would not have guessed.
> 
> dan
> 
> 
> 
> On Wed, Nov 1, 2017 at 8:59 AM, dmh@xxxxxxxx <dmh@xxxxxxxx> wrote:
>> Are you trying to create a netcdf-3 file, which does not
>> support string type.
>> =Dennis Heimbigner
>> Unidata
>> 
>> 
>> On 11/1/2017 1:15 AM, Dan Hitt wrote:
>>> 
>>> Hi,
>>> 
>>> I installed netcdf on a debian box using the the usual package means.
>>> 
>>> I'm trying to assign some global string metadata in a file that i
>>> create, successfully, with nc_create().
>>> 
>>> My statement to assign the metadata looks like this:
>>> int status =
>>>   nc_put_att_string( ncid, NC_GLOBAL, key, strlen( value ), &value );
>>> where key and value both have declaration const char*.
>>> 
>>> This compiles without error or warning, and i believe the values i'm
>>> passing in are innocuous.
>>> 
>>> Nevertheless, at runtime, i get a return status of -45, which
>>> nc_strerror() reports as
>>>    NetCDF: Not a valid data type or _FillValue type mismatch.
>>> 
>>> Am i missing some piece of setup that i need to do (besides getting
>>> the ncid for the file)?
>>> 
>>> This is my first call on the library after creating the file, so
>>> presumably my ncid for the file couldn't have gotten too messed up
>>> yet.
>>> 
>>> TIA for any clues, or pointers to the manual, or any other advice.
>>> 
>>> dan
>>> 
>>> _______________________________________________
>>> NOTE: All exchanges posted to Unidata maintained email lists are
>>> recorded in the Unidata inquiry tracking system and made publicly
>>> available through the web.  Users who post to any of the lists we
>>> maintain are reminded to remove any personal information that they
>>> do not want to be made public.
>>> 
>>> 
>>> netcdfgroup mailing list
>>> netcdfgroup@xxxxxxxxxxxxxxxx
>>> For list information or to unsubscribe,  visit:
>>> http://www.unidata.ucar.edu/mailing_lists/
>>> 
>> 
>> _______________________________________________
>> NOTE: All exchanges posted to Unidata maintained email lists are
>> recorded in the Unidata inquiry tracking system and made publicly
>> available through the web.  Users who post to any of the lists we
>> maintain are reminded to remove any personal information that they
>> do not want to be made public.
>> 
>> 
>> netcdfgroup mailing list
>> netcdfgroup@xxxxxxxxxxxxxxxx
>> For list information or to unsubscribe,  visit:
>> http://www.unidata.ucar.edu/mailing_lists/
> 
> _______________________________________________
> NOTE: All exchanges posted to Unidata maintained email lists are
> recorded in the Unidata inquiry tracking system and made publicly
> available through the web.  Users who post to any of the lists we
> maintain are reminded to remove any personal information that they
> do not want to be made public.
> 
> 
> netcdfgroup mailing list
> netcdfgroup@xxxxxxxxxxxxxxxx
> For list information or to unsubscribe,  visit: 
> http://www.unidata.ucar.edu/mailing_lists/ 



  • 2017 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: