Re: Problems with adding float constant to NioVariables

From: David Brown <dbrown_at_nyahnyahspammersnyahnyah>
Date: Wed Mar 17 2010 - 15:08:42 MDT

Hi Ray,

PyNIO was modeled on the original Python interface to NetCDF, written
years ago by Konrad Hinson, and still supported as
Scientific.IO.NetCDF (documented here:
http://dirac.cnrs-orleans.fr/ScientificPython/ScientificPythonManual/Scientific.IO.NetCDF-module.html)
. The essential thing to note is that, using your example:

T = f.variables['sat']

T is a reference to the variable 'sat' in the file. It is not a NumPy
array. You can get a NumPy array either by using the get_value()
method, which returns all the values in the array as a (possibly
masked) ndarray, or else by applying a slicing operator. So if you
want an ndarray variable you could say, e.g.:

Tnd = T.get_value() # the only way to read a scalar variable from a
file, because scalars cannot be subscripted
or
Tnd = T[:]
or
Tnd = T[0] # a slice consisting of all elements of the first element
of the first dimension

The corollary is that, if the file is open for writing, you can write
to the file by assigning to T, e.g.

T[:] = Tnd * 4.0

This is discussed under the headings:

Get the data in a variable object into a NumPy array

and

Assigning values to a variable in the file

on the main PyNIO web page.

So while you are correct that this syntax does not work exactly like
the usual Python/NumPy syntax, it is well-documented and has a long
history.
Hope this helps.
  -dave

On Mar 17, 2010, at 10:18 AM, Raymond P. wrote:

> Hello,
>
> I am using the Nio OS10.5 build from the ESG portal, under Python 2.6
> with OS 10.6 .
> (This is a bit of a kludge pending a better build for 10.6/2.6 but it
> sort of works). To
> Get PyNio to work with Python 2.6, I had to edit the line in the PyNio
> Python script
> that was causing problems on account of the way it was passing an
> argument
> to an __init__ . This was only supposed to affect some advanced
> capabilities,
> but perhaps it has also created the problem I note below.
>
> The problem I am having is that the NioVariable object doesn't know
> how to perform
> an operation with a float scalar. This behavior contrasts with all
> other Python
> arrays -- numpy arrays, masked arrays, etc. Either this broke in
> Python 2.6, or
> there is a bug or oversight in Nio. Here's an example of the
> problem:
>
> Bring in a variable from a file
>
>>>> T = f.variables['sat']
>
> Check out that it looks OK.
>
>>>> T[0:10]
> masked_array(data = [ 13.06858921 13.07066441 13.07257843
> 13.07219887 13.07272339
> 13.07136154 13.07164955 13.07108593 13.07037163 13.06673241],
> mask = False,
> fill_value = 9.96920996839e+36)
>
> Now try to create a new array, subtracting off a float:
>>>> T1 = T - 13.068
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: unsupported operand type(s) for -: 'NioVariable' and
> 'float'
>
> That didn't work. The operation seems not have been defined for an
> NioVariable object.
> On the other hand, the following works:
>>>> T1 = T[:] - 13.068
>>>> T1[0:10]
> masked_array(data = [ 0.00058937 0.00266457 0.00457859 0.00419903
> 0.00472355 0.0033617
> 0.00364971 0.00308609 0.00237179 -0.00126743],
> mask = False,
> fill_value = 9.96920996839e+36)
>
>
>
> --------------------------------
>
> For consistency with all the other array systems in the Python world,
> you should be able to do array/scalar operations on NioVariable
> objects.
> Are people having the same problem when running with Python 2.5?
>
> Thanks,
> Ray Pierrehumbert
> _______________________________________________
> 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 Wed Mar 17 15:08:46 2010

This archive was generated by hypermail 2.1.8 : Mon Mar 29 2010 - 12:19:51 MDT