0

I'm trying to move values from a numpy array to a NetCDF file, which I am creating. Currently I'm trying to find the best way to emulate 'fancy indexing' of numpy arrays when creating a netCDF file, but the two indexing systems don't match when the dataset only has two points.

import netCDF4
import numpy as np

rootgrp = netCDF4.Dataset('Test.nc','w',format='NETCDF4')
time = rootgrp.createDimension('time',None)
dim1 = rootgrp.createDimension('dim1',100)
dim2 = rootgrp.createDimension('dim2',100)
dim3 = rootgrp.createDimension('dim3',100)
ncVar = rootgrp.createVariable('ncVar','f4',('time','dim1','dim2','dim3'))
npArr = np.arange(0,10000)
npArr = np.reshape(npArr,(100,100))

So this works just fine:

x,y=np.array(([1,75,10,99],[40,88,19,2]))
ncVar[0,x,y,0] = npArr[x,y]

While this does not:

x,y=np.array(([1,75],[40,88]))
ncVar[0,x,y,0] = npArr[x,y]

These assignments are part of a dynamic loop that determines x,y to create values for ncVar at ~1000 time-steps

EDIT: the issue seems to be that the first case recognizes x,y as defining a series of pts, and so returns a [4,] size array (despite the documentation on netCDF4 'fancy indexing'), while the second interprets them combinatorially and so returns a [2,2] size array (as stated in the documentation). Has anyone run into this or found a workaround?

  • I'm not sure what's going on here, but given that the first one appears to work, the second should work too. I'd open an issue here with this same information: https://github.com/Unidata/netcdf4-python/ – DopplerShift Oct 28 '14 at 15:37
  • What is the error message associated with the second attempt? – N1B4 Oct 28 '14 at 17:49
  • I'm not certain it's a bug. The NetCDF4 documentation states that it indexes differently from numpy arrays (see link at end of this comment). That said, I'm not certain why it accepts the first case and I don't know how to work around the second case. (See following link under sub-heading 6, about halfway through for indexing rules of NetCDF4: http://netcdf4-python.googlecode.com/svn/trunk/docs/netCDF4-module.html) – Wes Anderson Oct 28 '14 at 20:35
  • @N1B4 the issue is that the first case recognizes x,y as defining a series of pts, and so returns a [4,] size array (despite the documentation of netCDF4 'fancy indexing'), while the second interprets them combinatorially and so returns a [2,2] size array (as stated in the documentation). Has anyone run into this or found a workaround? – Wes Anderson Oct 28 '14 at 20:40
  • @DopplerShift You may be right. I opened an issue here: https://github.com/Unidata/netcdf4-python/issues/300 – Wes Anderson Oct 30 '14 at 18:50

0 Answers0