FunkySheep
FunkySheep

Reputation: 113

Why is the NETcdf data I created masked?

I create a netcdf file with some data, and when I import the data in another script, it is masked :

    >>> type(Data[:])
    <class 'numpy.ma.core.MaskedArray'>

Here is how I create the data :

    # Put in a grid
    print 'Putting the data in a grid...'
    LatRange = range( int(min(Lat)), int(max(Lat)), 1 )
    LonRange = np.arange( int(min(Lon)), int(max(Lon)), 1 )
    dRange = range(0,200,10) + range(200,4000,100)
    dateRange = np.arange( float(min(Dates).year)+min(Dates).month/12., float(max(Dates).year)+max(Dates).month/12., 1./12. )

    dataset = Dataset('gridded_data/DataAveraged.nc','w', format='NETCDF4_CLASSIC')
    zD = dataset.createDimension('zD',len(dRange))
    latD = dataset.createDimension('latD',len(LatRange))
    lonD = dataset.createDimension('lonD',len(LonRange))
    timeD = dataset.createDimension('timeD',len(dateRange))

    tempAve = dataset.createVariable('tempAve', np.float32, ('zD','latD','lonD','timeD'), fill_value=-9999)
    tempAve.units = 'psu'
    tempAve[:] = Tgrid_ave

Where Tgrid_ave is a numpy array.

Then, I import the data this way in another script :

    dataset = Dataset('gridded_data/DataAveraged.nc', 'r')

    LatRange = dataset.variables['lat'][:]
    LonRange = dataset.variables['lon'][:-1]

    Tgrid_ave = dataset.variables['tempAve']

And my Lat and Lon data are not masked, but my Tgrid_ave data is.

How can I avoid this!?

Upvotes: 1

Views: 4228

Answers (1)

titusjan
titusjan

Reputation: 5546

The netCDF4 library used to return either a masked array or a regular Numpy array, depending on if the data you request from the array (or array slice) contains fill values or not. This is unfortunate behavior but it seems to be fixed in PR 787. So I think that, from version 1.4 onward, the default behavior is always to return a masked array if a fill value is defined (I haven't tested it).

Anyway, you can ensure that you always get a regular numpy array by setting the set_auto_mask to False.

Upvotes: 2

Related Questions