sodri126 / netcdf4-python

Automatically exported from code.google.com/p/netcdf4-python
Other
0 stars 0 forks source link

Slicing a float32 variable fails #142

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Here is an example from the interactive window:

[Dbg]>>> variable
'ppt'
[Dbg]>>> ncFile
'C:\\NPSCAPE\\PRISM\\ZZZZ_PRISM4k_ppt_wyAvg_1898000000.nc'
[Dbg]>>> a = file.variables[variable]
[Dbg]>>> file.file_format
'NETCDF4'
[Dbg]>>> a.dtype
dtype('float32')
[Dbg]>>> a[:]
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "netCDF4.pyx", line 2642, in netCDF4.Variable.__getitem__ (netCDF4.c:29419)
  File "netCDF4.pyx", line 2660, in netCDF4.Variable._toma (netCDF4.c:29751)
AttributeError: 'bool' object has no attribute 'any'

Here is the function that is failing:

def _netcdf2Matrix(ncFile, variable):
###################################################
## Returns a 2-dimensional matrix from a netCDF file
###################################################

    from netCDF4 import Dataset
    file = Dataset(ncFile, 'r')
    grid =  file.variables[variable][:]
    file.close()
    return grid

Attached is the float32 netCDF file

Original issue reported on code.google.com by antipod...@gmail.com on 19 Sep 2012 at 7:25

Attachments:

GoogleCodeExporter commented 9 years ago
Confirmed.  Should be easy to fix.  In the meantime, here's a workaround

v = file.variables[variable]
v.set_auto_maskandscale(False)
grid = v[:]

Original comment by whitaker.jeffrey@gmail.com on 19 Sep 2012 at 7:45

GoogleCodeExporter commented 9 years ago
The missing value attribute in that netCDF file is a string.  The error occurs 
when checking for the occurence of a missing value in the data retrieved from 
the variable - numpy issues this warning

Unicode equal comparison failed to convert both arguments to Unicode - 
interpreting them as being unequal

and fails silently.  I've committed a fix for this - I cast the missing_value 
attribute to the type of the netcdf variable before doing the comparison.

Original comment by whitaker.jeffrey@gmail.com on 19 Sep 2012 at 8:05

GoogleCodeExporter commented 9 years ago
Thanks! 

Original comment by antipod...@gmail.com on 19 Sep 2012 at 9:57

GoogleCodeExporter commented 9 years ago
That's really a bug in your netcdf file - if that's a file you created you 
should probably fix it so the missing_value attribute is a float not a string.  
Other netcdf clients will probably have trouble with that as well.

Original comment by whitaker.jeffrey@gmail.com on 19 Sep 2012 at 9:59

GoogleCodeExporter commented 9 years ago

Original comment by whitaker.jeffrey@gmail.com on 7 Mar 2013 at 11:46