Chapter 10.  Writing Attributes

The final missing piece is attributes. Again, it's pretty darned simple. The m-file nc_attput.m can create or overwrite either variable or global attributes. Let's use it to add a units attribute to the time variable and to add a title attribute to the netCDF file as a whole (that's what a global attribute is).

>> nc_attput ( 'test.nc', 'time', 'units', 'days since 2001-01-01' );
>> nc_attput  ( 'test.nc', nc_global, 'title', 'This is just a test' );
>> nc_dump ( 'test.nc' )
netcdf test.nc {

dimensions:
        latitude = 20 ;
        longitude = 30 ;
        time = UNLIMITED ; (2 currently)


variables:
        float myvar(time,latitude,longitude), shape = [2 20 30]
        double longitude(longitude), shape = [30]
        double latitude(latitude), shape = [20]
        double time(time), shape = [2]
                time:units = "days since 2001-01-01"


//global attributes:
                :title = "This is just a test"
}

    

An additional m-file that may be of interest is nc_addhist.m, which appends to the history attribute , rather than overwriting it, as nc_attput.m would do.

>> nc_addhist ( 'test.nc', 'This is the first history addendum' )
>> nc_addhist ( 'test.nc', 'And this is the 2nd...' )
>> nc_addhist ( 'test.nc', 'And this is the 3rd...' )
>> nc_dump ( 'test.nc' )
netcdf test.nc {

dimensions:
        latitude = 20 ;
        longitude = 30 ;
        time = UNLIMITED ; (2 currently)


variables:
        float myvar(time,latitude,longitude), shape = [2 20 30]
        double longitude(longitude), shape = [30]
        double latitude(latitude), shape = [20]
        double time(time), shape = [2]
                time:units = "days since 2001-01-01"


//global attributes:
                :title = "This is just a test"
                :history = "14-Oct-2006 18:20:17:  And this is the 3rd...
14-Oct-2006 18:20:13:  And this is the 2nd...
14-Oct-2006 18:19:57:  This is the first history addendum"

}

    

You can see that the history texts are timestamped and listed newer to older.