# Re: assigning a fill value

From: Gary Bates <gary.bates_at_nyahnyahspammersnyahnyah>
Date: Tue, 09 Dec 2008 10:33:55 -0700

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
>
> The function signature is
>
>
> 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:
>
>
> 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 - 10:33:55 MST

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