X
2428

Example how to use IDL to edit and existing attribute in a CDF file

Topic:

This help article provides a quick example of how a user can change the value of an existing attribute within a CDF file.

Discussion:

If you want to change the value of an existing attribute entry within a CDF file, you will need to complete the following steps:

- Open the CDF file
- Determine the number of entries within the attribute and the starting entry number
- Cycle through the entries in the file and use CDF_ATTEXISTS to test which entries exist
- Use CDF_ATTPUT to change the value

Since an attribute can have multiple entries, and the "entry number" associated with each entry is controlled by the creator of the file,  there is probably not great way to determine the entry number programmatically. However, if there are multiple entries in the attribute, you could assume that the entry number starts at 0 or 1 and increments by 1 to the maxgentry value. Then, you can use the "numgentries" and "maxgentry" values to determine the starting entry value.  With this information, you can cycle through the entries in the attribute and  use the CDF_ATTEXISTS procedure to test which entries exist.

Below is an example called "cdf_ex_test.pro". This program first creates a CDF file and closes it. Then, it is reopened and the value of an entry in an existing attribute is changed.

pro cdf_ex_test
  compile_opt idl2


  ;generate a cdf file with a global attribute
  id = cdf_create('temperature.cdf', [2,3], /clobber )
  att_id = cdf_attcreate(id, 'title', /global)
  att_id2 = cdf_attcreate(id, 'author', /global_scope)
  cdf_attput, id, att_id, 0, 'my fancy cdf'
  cdf_attput, id, att_id, 1, 'line 2 of file'


  cdf_close, id ;close the cdf file

  ;reopen the cdf file  read the current attribute
  ;value
  ocdf = cdf_open('temperature.cdf')

  ;get the number of attributes
  inq=cdf_inquire(ocdf)
  num_att = inq.natts
 
  ;get the max value starting value for the global entries
  cdf_control, ocdf, attribute='title', get_attr_info=att_info
  ngentry = att_info.numgentries ;2
  mgentry =  att_info.maxgentry ;1
  start_gentry = mgentry - ngentry + 1 ;0


  if (cdf_attexists(ocdf,'title',start_gentry)) then begin

    ;use cdf_attget to retrieve the value of the data
    cdf_attget, ocdf,'title',start_gentry,x
    print, "original value:"
    print, x
 
    ;use cdf_attput to change value of the attribute
    cdf_attput, ocdf,'title',start_gentry,"change my value"
    ;use cdf_attget to retrieve the value of the data
    cdf_attget, ocdf,'title',start_gentry,x
    print, "new value:"
    print, x
 
    cdf_close, ocdf ;close the cdf value
   
  endif

end

Written by DS Reviewed by JU (9/4/2014)