Re: IndexError when trying to read masked value from variable

From: Carmen St. Jean <carmen.stjean_at_nyahnyahspammersnyahnyah>
Date: Fri Jul 06 2012 - 07:28:05 MDT

I see what you mean now with a period between var.variables and the
name; I didn't realize you were copying documentation into the email.
I would rather have a reference to the variable than load it into
memory, but I think what I am trying to do should work either way
anyway, so it's a good thing that Mary has opened a ticket for this.

Thanks,

Carmen

On Thu, Jul 5, 2012 at 4:58 PM, Oleksandr Huziy <guziy.sasha@gmail.com> wrote:
> Hi,
>
> [:] - retreives all the values to memory (gives you numpy array)
>
> but queuing the variable should work too, since it is not a good idea to
> load everything to the memory if you do not need it all.
> I meant the point after variables.["varName"] (but that's probably nothing)
>
> good luck
>
> Cheers
> --
> Oleksandr
>
> 2012/7/5 Carmen St. Jean <carmen.stjean@noaa.gov>
>
>> Hi Oleksandr,
>>
>> While I have to admit I don't understand your entire response, for
>> example the bit about reversing elements, your suggestion of accessing
>> the variable with "[:]" worked! I changed line 4 of my code so it
>> reads:
>>
>> zval = f.variables["zval"][:] # instead of zval = f.variables["zval"]
>>
>> Now, when I run the code, it doesn't fail here like before:
>>
>> zvalDirectMissingVal = zval[0, 1, 64387]
>>
>> When I print zvalDirectMissingVal, then I see "---", which is correct.
>>
>> Perhaps you were suggesting something slightly different, but in
>> either case the mystery seems somewhat solved. Still, it's kind of
>> strange. If I was able to read most values of zval without the "[:]"
>> at the end of the variable extraction, then I don't understand why I
>> couldn't read all values this way.
>>
>> Thanks for your help,
>>
>> Carmen
>>
>> On Tue, Jul 3, 2012 at 6:05 PM, Oleksandr Huziy <guziy.sasha@gmail.com>
>> wrote:
>> > Hi Carmen,
>> >
>> > I am not sure if this is important, but I don't see the point in your
>> > code
>> > as described here:
>> >
>> > Get the data in a variable object into a NumPy array
>> >
>> > If the variable is not scalar (rank > 0):
>> >
>> > data = f.variables.['varName'][:]
>> >
>> > or
>> >
>> > data = f.variables.['varName'],get_value()
>> >
>> > Assuming the shape of the variable is (5,20,30) get a slice along the
>> > first
>> > element of the first dimension:
>> >
>> > var = f.variables.['varName']
>> > data = var[0]
>> >
>> > Get the same slice but reverse the elements of the second dimension. The
>> > following two statements are equivalent:
>> >
>> > data = var[0,::-1,:]
>> > data = var[0,19::-1,:]
>> >
>> >
>> > I would test it myself but Nio (which I installed using macports), does
>> > not
>> > work on my system.
>> >
>> > Another option for you would be to try netcdf4-python
>> >
>> > Cheers
>> >
>> > --
>> > Oleksandr (Sasha) Huziy
>> >
>> >
>> >
>> > 2012/7/3 Carmen St. Jean <carmen.stjean@noaa.gov>
>> >>
>> >> Hello all,
>> >>
>> >> I'm getting this strange error message when I attempt to run the
>> >> attached code with this input file
>> >>
>> >>
>> >> (http://jetstreak.unh.edu/netcdf/nos.creofs.fields.nowcast.20120625.t12z.nc,
>> >> it is over one GB in size):
>> >>
>> >> Traceback (most recent call last):
>> >> File "creofstest.py", line 22, in <module>
>> >> zvalDirectMissingVal = zval[0, 1, 64387]
>> >> File "/usr/local/lib/python2.7/site-packages/PyNIO/Nio.py", line
>> >> 305, in __getitem__
>> >> ret = _fill_value_to_masked(self,ret)
>> >> File "/usr/local/lib/python2.7/site-packages/PyNIO/Nio.py", line
>> >> 283, in _fill_value_to_masked
>> >> a = ma.masked_where(a == self.__dict__['_FillValue'],a,copy=0)
>> >> File "/usr/local/lib/python2.7/site-packages/numpy/ma/core.py", line
>> >> 1802, in masked_where
>> >> " (got %s and %s)" % (cshape, ashape))
>> >> IndexError: Inconsistant shape between the condition and the input
>> >> (got (1,) and ())
>> >>
>> >> My code is very simple; it's just trying to access the zval variable
>> >> at different valid indices.
>> >>
>> >> I can directly access a specific value from the zval variable that
>> >> happens to not contain the missing value (e.g., zval[0, 0, 64387])
>> >> without a problem. But when I try to directly access a value that I
>> >> know to be a missing value (e.g., zval[0, 1, 64387]), then I get the
>> >> above error.
>> >>
>> >> I am, however, able to indirectly access that value zval[0, 1, 64387]
>> >> by saving zval[0, :, 64387] to a new variable and taking the 1st index
>> >> of that. This correctly prints out as a missing value.
>> >>
>> >> Does anyone have any ideas of why this might be happening?
>> >>
>> >> Thanks,
>> >>
>> >> Carmen
>> >>
>> >> _______________________________________________
>> >> 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 Fri Jul 6 07:28:13 2012

This archive was generated by hypermail 2.1.8 : Thu Sep 13 2012 - 15:38:36 MDT