2

Question:

Is there a way of forcing netCDF4 to always output a masked array, regardless of whether it slice contains any fill values?

Background:

I have a netCDF dataset of values on a grid, over time that I read using the netCDF4 package.

nc_data = netCDF4.Dataset('file.nc', 'r')

The initial timesteps yield masked arrays:

var1_t0 = nc_data.variables['var1'][0][:]
var1_t0
masked_array(...)

The later timesteps yield standard ndarrays:

var1_t200 = nc_data.variables['var1'][200][:]
var1_t200
ndarray(...)

Desired result:

I would like masked arrays for the latter with a mask of all False, rather than a standard ndarray.

gerrit
  • 24,025
  • 17
  • 97
  • 170
ryanjdillon
  • 17,658
  • 9
  • 85
  • 110

1 Answers1

4

I don't think this is directly possible, but you can work it around by creating a masked_array if necessary:

var1_t0 = nc_data.variables['var1'][0][:]
if type(var1_t0) is numpy.ma.core.MaskedArray:
    var1_t0 = numpy.ma.core.MaskedArray(var1_t0, numpy.zeros(var1_t0.shape, dtype = bool))
Constantinius
  • 34,183
  • 8
  • 77
  • 85
Holt
  • 36,600
  • 7
  • 92
  • 139