On 4/1/10 7:35 PM, demail wrote:
Question about slicing from variable values, not index?
using:
netCDF4-0.9.win32-py2.6.exe
numpy-1.3.0-py2.6
#This works:
rootgrp = Dataset(NC_FIL, 'w', format='NETCDF4')
rootgrp.createDimension('lat', 5)
rootgrp.createDimension('lon', 5)
dogs = rootgrp.createVariable('dogs','u1',('lat','lon'))
latitudes = rootgrp.createVariable('latitude','f4',('lat',))
longitudes = rootgrp.createVariable('longitude','f4',('lon',))
lats = numpy.arange(35,36,.2) #lats = array([ 35. , 35.2, 35.4, 35.6,
35.8])
lons = numpy.arange(82,83,.2) #lons = array([ 82. , 82.2, 82.4, 82.6,
82.8])
latitudes[:] = lats
longitudes[:] = lons
dogs[:] = NumPy_2dArray #NumPy 2 d array, number of dogs at lat / lon (5 x
5)
rootgrp.close()
#Can get results:
getrootgrp = Dataset(NC_FIL, 'r', format='NETCDF4')
temp_dogs = getrootgrp.variables['dogs'][:]
temp_lat= getrootgrp.variables['latitude'][:]
temp_lon = getrootgrp.variables['longitude'][:]
OK: I want to be able to perform a slice from
getrootgrp.variables['latitude'] and getrootgrp.variables['longitude'],
based on value, not index,
that will return array [latitude, latitude, number of dogs].
Conceptually, I am thinking along the lines of Coordinate Variables or array
subscripting, but have not been successful in using it to have something
like:
temp_lat_lon_dogs = getrootgrp.variables['dogs'][:], latitude[35.2:35.8],
longitude> 82.2]
You can slice with logical operations on coordinate arrays, i.e.
data = f.variables['dogs'][numpy.logical_and(temp_lat >35.2, temp_lon <
35.8]. )
Note that you have to use numpy.logical_and since there is no &&
operator in python.
-Jeff