Re: assigning a fill value

From: David Brown <dbrown_at_nyahnyahspammersnyahnyah>
Date: Tue, 9 Dec 2008 11:43:55 -0700

Hi Gary,
Are you using PyNIO (current version: 1.3.0b1) to read the file? By
default, it should automatically create a masked array
for any variable in a NetCDF file that has a _FillValue or a
missing_value attribute.

Otherwise I am not sure what is causing your error.
The following code executes without error for me:

>>> import numpy
>>> from numpy import ma
>>> y= numpy.zeros(20,'f')
>>> y[4:10] = -999.9
>>> y = ma.masked_where(y == -999.9, y, copy = 0)
>>> y
masked_array(data = [0.0 0.0 0.0 0.0 -- -- -- -- -- -- 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0],
       mask = [False False False False True True True True True
True False False
  False False False False False False False False],
       fill_value=1e+20)
>>> y.set_fill_value(-999.9)
>>> y
masked_array(data = [0.0 0.0 0.0 0.0 -- -- -- -- -- -- 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0],
       mask = [False False False False True True True True True
True False False
  False False False False False False False False],
       fill_value=-999.900024414)

  -dave

On Dec 9, 2008, at 10:33 AM, Gary Bates wrote:

> hi Dave,
>
> y = ma.masked_where(y == -999.9, y, copy = 0) # avoids data copy
>
> Using the above works, however I do get a warning when doing so:
>
> /Users/jwhitaker/lib/python/numpy/ma/core.py:2304: UserWarning:
> Warning: converting a masked element to nan.
> warnings.warn("Warning: converting a masked element to nan.")
>
>
> Perhaps there is a better way to do what I want. What I'm doing is
> reading in data from a netcdf file --- this data already has a
> fillvalue associated with it. All I want to do is to transfer a
> subset of this data to the array y and then plot y. How can I
> transfer the fillvalue of the incoming data to y?
>
> Thanks, Gary
>
>
>
>
>
> David Brown wrote:
>> Hi Gary,
>> Ordinary NumPy arrays do not support the concept of missing
>> values. To get support for missing data you need to use the NumPy
>> MaskedArray subclass. The paradigm is a little different from the
>> NetCDF _FillValue concept. Associated with the masked array is a
>> 'mask' array, a Boolean array with the same dimensions as the data
>> array. It is set to True for values that need to be masked and
>> false otherwise. It also includes a fill_value attribute, which is
>> only used when converting to a 'filled' array.
>> In your case one way you could get a masked array is as follows
>> (assume that you don't know that the array actually contains no
>> values equal to the filled value):
>> from numpy import ma
>> y= numpy.zeros((plot_len+15),'f')
>> y = ma.masked_where(y == -999.9, y, copy = 0) # avoids data copy
>> # set the fill value
>> y.set_fill_value(-999.9)
>> You can also create a masked array directly using ma.array() or
>> its equivalent ma.MaskedArray().
>> The function signature is
>> x = MaskedArray(data, mask=nomask, dtype=None, copy=True,
>> fill_value=None, keep_mask=True, hard_mask=False,
>> shrink=True)
>> PyNgl (and PyNio) understand masked arrays and will behave
>> appropriately when given one.
>> PyNIo has various options when reading data from a file concerning
>> how or whether to create a masked array.
>> To get a pure NumPy array that has the masked elements replaced by
>> the fill value use:
>> y_notmasked = y.filled(fill_value)
>> Hope this helps.
>> -dave
>> On Dec 8, 2008, at 5:12 PM, Gary Bates wrote:
>>> How does one assign a fill value to an array in PyNGL?
>>>
>>> Here's what I tried and the error I got:
>>>
>>> y= numpy.zeros((plot_len+15),'f')
>>> y._FillValue = -999.9
>>>
>>> AttributeError: 'numpy.ndarray' object has no attribute '_FillValue'
>>>
>>> -Gary
>>> _______________________________________________
>>> pyngl-talk mailing list
>>> List instructions, subscriber options, unsubscribe:
>>> http://mailman.ucar.edu/mailman/listinfo/pyngl-talk

_______________________________________________
pyngl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/pyngl-talk
Received on Tue Dec 09 2008 - 11:43:55 MST

This archive was generated by hypermail 2.2.0 : Thu Jan 01 2009 - 08:39:20 MST