NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.

To learn about what's going on, see About the Archive Site.

Re: [netcdfgroup] Question about slicing from variable values, not index?

  • To: demail <demail973@xxxxxxxxxx>
  • Subject: Re: [netcdfgroup] Question about slicing from variable values, not index?
  • From: Jeff Whitaker <jswhit@xxxxxxxxxxx>
  • Date: Sat, 03 Apr 2010 06:56:35 -0600
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



  • 2010 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: