#!/usr/bin/env python
# Can't write a _FillValue attribute to a Variable
# J-Y Peterschmitt - LSCE - 07/2015
import numpy as np
import cdms2
data = np.random.random((10, 10))
data = np.ma.masked_less(data, 0.5)
dummy_grid = cdms2.createUniformGrid(10, 10, 1, 0, 10, 1)
dummy_var = cdms2.createVariable(data,
axes=[dummy_grid.getLatitude(),
dummy_grid.getLongitude()],
id='my_var')
dummy_var.units = '10e3 dummy'
dummy_var._FillValue = 1.e+20
dummy_var.dummy_att = 'Dummy att'
dummy_var._dummy_att = 'Dummy att starting with _'
cdms2.setNetcdfShuffleFlag(0)
cdms2.setNetcdfDeflateFlag(0)
cdms2.setNetcdfDeflateLevelFlag(0)
# Test 1
f = cdms2.open('dummy1.nc', 'w')
f.write(dummy_var)
f.write(dummy_var, fill_value=999.999)
f.close()
# Test 2
f = cdms2.open('dummy2.nc', 'w')
f.write(dummy_var, fill_value=999.999)
f.close()
# Test 3
var_att = dummy_var.attributes
var_att['another_att'] = 'New att'
var_att['_another_att'] = 'New att starting with _'
f = cdms2.open('dummy3.nc', 'w')
f.write(dummy_var, attributes=var_att)
f.close()
# The end
The following script shows that you can add a _FillValue attribute to a variable, but it will not be written to the output file. Other attributes starting with a '_' are not written either, so I wonder if all the attributes starting with the underline character are silently ignored
Is that a bug or a feature?
Tested with 2.1