使用 Scipy 创建 netcdf 文件?

Creating netcdf file with Scipy?

我今天的工作是在 Numpy 中获取一个数组,并使用 Scipy 将其转储到网络 CDF 文件 (.nc) 中。 Scipy 有一个针对此 http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.io.netcdf.netcdf_file.html 的特定子模块,但那里没有很多用于创建实际 netcdf 文件的示例。

以下是需要写入文件的数据:

我已经将所有 3 个都保存在 Numpy 中,我现在必须使用 Scipy(我也有 NETCDF4 python 包,但我无法使用它来解决) 将该数据放入 .nc 文件。

到目前为止,这是我的代码:

#testing code about difference
f = netcdf.netcdf_file('2m air temperature difference.nc', 'w')    
f.history = 'describes the difference in 2m air temperature between the period (1992-2011) to (1961-1981)'
f.createDimension('lons', 192)    
f.createDimension('lats', 94)
print 'lons.size', lons.size
print 'lats.size', lats.size
longi = f.createVariable('lons',  'i', 192)
lati = f.createVariable('lats', 'i',  94)
f.createDimension('diff', difference.size)
lons = lons[:]
lats = lats[:]
differ = difference[:]
f.close()

我使用 scipy 文档中提供的默认示例,我只替换了为我的数据制作它所必需的部分,其他一切都与 th 中显示的相同eexample.

然而,

唯一的问题是,出于某种原因,我收到了 "Type Error: 'int' object is not iterable" 的错误。但是,给出的示例使用 i 作为数据类型,它没有提供任何其他示例来说明什么可以用作“createVariable()”方法中的数据类型...?

感谢任何帮助。

documentation for createVariable表示第三个参数:

List of the dimension names used by the variable, in the desired order.

因此您需要提供维度名称列表,而不是像 192 或 94 这样的整数。例如,

longi = f.createVariable('lons',  'i', ['lons'])

似乎是正确的。