设置NetCDF尺寸值

时间:2015-07-28 10:28:06

标签: python numpy scipy

我有一个4维变量,我想设置其中3个等于固定值,所以只有一个正在改变(即我可以获得变量的2D图形表示) - 但我找不到方法用scipy和numpy这样做。我不愿意安装NetCDF4(我只是在工作经验 - 我上周学习了python,所以我对此很新。)

谢谢!

1 个答案:

答案 0 :(得分:0)

NetCDF4 lets you designate one dimension as variable so it can organize the data file layout in a convenient way. You can only add data to the end of a file. Any other changes require rewriting the file, or at least large chunks of it. In addition a lot of NetCDF4 data has a natural 'growth' dimension, e.g spatial fixed sample points, but growing over time.

numpy arrays don't have such an option, and usually don't need it. If you need to add data to an array, you use concatenate to create a new array, one that contains both the old data and the new. Or rather it joins all the elements, without an 'old' v 'new' distinction. np.concatenate is compiled and thus reasonably fast. I believe it acts by creating new empty array of the right size, and copying over the data from the inputs.

There is a np.append, but it really is just a front end to concatenate. For a one time addition to an array it is fine, but new users often misuse it by putting it in a loop, adding items one by one to an array.

A common numpy idiom for constructing an array is to append the pieces to a list, and passing that list to np.array at the end. Python list has an efficient append method. Such a list could also be passed to np.concatenate.

Another common approach is to initialize a large enough empty array, and then insert subarrays along the appropriate dimension.

Creating an array incrementally is a common SO numpy question.