Reputation: 395
I am opening a NetCDF file in Python, as a file object. However, when I want to check the data and put the variables in a list, it shows that the values are masked! How do I unmask them?
The code I have is:
file = 'C:/Users/cru/0.5x0.5/pre/cru_ts3.23.2001.2010_.pre.dat.nc'
fileobj = netCDF4.Dataset(file)
tsvar = fileobj.variables[varname]
dec_list = []
dec_list.append(numpy.mean(tsvar[12,25,35]))
print dec_list
tsvar
shape is: (120, 360, 720) #(month, lat,lon)
The output of printing dec_list
is: [masked]
. I see the same result for any month, lat or lon.
Upvotes: 1
Views: 3970
Reputation: 3794
Read about numpy masked arrays numpy.ma
if type(tsvar) == <class 'numpy.ma.core.MaskedArray'>
You can use numpy.ma.mean():
#instead of this numpy.mean(tsvar[12,25,35]) # try numpy.ma.mean(tsvar[12,25,35])
Upvotes: 1
Reputation: 11
netCDF4 load the data to masked array of numpy in default. The masked element is considered to be invalid for the data processing. Here is a good link for explanation: https://currents.soest.hawaii.edu/ocn760_4/_static/masked_arrays.html
Masked array may have different sum, mean and variance, you can check there is masked value in your array by:
print(np.where(tsvar.mask==True))
If you want to change the default set of loading in masked array, you can turn the mask operation off by:
for k in fileobj.variables:
fileobj.variables[k].set_auto_mask(False)
Upvotes: 1