1

I am trying to use the netCDF4 package with python. I want to do something which I think should be straightforward, but I can't make it work and I can't find any documentation on it. I have a list, and I simply want to store the list in a netCDF4.Variable object. I thought this code would store "newlist" in the netCDF4.Variable object "x_data", which is a component of the Dataset object "netdata":

netdata.variables['x_data'][:]=numpy.array(newlist)

But it's no go. I get the error message:

ValueError: setting an array element with a sequence.

BTW, I have imported both netCDF4 and numpy. Also BTW, this is the same error I get if I do this without converting newlist to a numpy array.

Surely there's a way to do this. Anyone know how? Thanks.

Addendum: This simple change gets me past the error message:

netdata.variables['x_data']=newlist

But I'm still not out of the woods. The data definitely gets stored in netdata.variables['x_data'], but then I close netdata and exit python, and examine the netCDF output, and the data just isn't there. Any ideas? Thanks again.

1 Answer 1

5

OK, if anyone is interested, apparently the way to do it is:

netdata.variables['x_data'][:]=newlist[:]

Hint to anyone who might be working on the netCDF4 python package: It's a great package in many ways, but I couldn't find this anywhere in the documentation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.