Re: [netcdfgroup] Reading data from a variable into a numpy.ma.MaskedArray

On 2014/01/21 6:17 AM, James Adams wrote:
When I read data for a lon/lat point from a NetCDF variable I get a
MaskedArray if all values are missing and an ndarray if all values are
present.  I'd like to instead get it returned in one way or the other
in either case, preferably as a MaskedArray so I can check if all
values are masked and skip to the next lon/lat point.  Can someone
advise as to how I can best go about this?

I'm using the netCDF4 Python module from here:
https://code.google.com/p/netcdf4-python/

My code looks like this:

     # get a "chunk" of data from the NetCDF variable
     precipChunk = inputPrecipVariable[0:len(inputTimeDimension):1,
lonChunkOffset:lonChunkOffset + lonChunkSize:lonChunkSize,
latChunkOffset:latChunkOffset + latChunkSize:latChunkSize]

insert this:
        precipChunk = np.ma.array(precipChunk, copy=False)

That will efficiently guarantee that you have a masked array.

If no values are masked, the mask attribute will be np.ma.nomask, not a full array of booleans, but the test below will still work correctly.

Eric


     # skip this entire chunk if all values are masked
     if (precipChunk.mask.all()):
         continue

The above works fine if the data (precipChunk) is returned as a
MaskedArray, but it bombs out if it's returned as an ndarray.  Maybe I
should do some sort of type check on the returned array before
checking to see if all values are masked?  Is data only returned as a
MaskedArray if *all* values are missing/fill values, or can it happen
that a section of data which does contain valid values is also
returned as MaskedArray with the valid data values unmasked?

Thanks in advance for any suggestions and/or insight.

--James

_______________________________________________
netcdfgroup mailing list
netcdfgroup@xxxxxxxxxxxxxxxx
For list information or to unsubscribe,  visit: 
http://www.unidata.ucar.edu/mailing_lists/




  • 2014 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: