1

我正在尝试使用 scipy 编写 netcdf 文件。我已经从 scipy 网站复制了这个例子——但是当我查看输出时,我得到了奇怪的数字。

我也一直在尝试其他事情,甚至为我声明为“float32”的另一个变量指定 .astype(np.float32)。

Python代码:

import numpy as np
from pylab import *
from scipy.io import netcdf
f = netcdf.netcdf_file('simple.nc', 'w')
f.history = 'Created for a test'
f.createDimension('time', 10)
time = f.createVariable('time', 'i', ('time',))
time[:] = np.arange(10)
time.units = 'days since 2008-01-01'
f.close() 

输出:

ncdump -v time simple.nc 
netcdf simple {
dimensions:
    time = 10 ;
variables:
    int time(time) ;
        time:units = "days since 2008-01-01" ;

// global attributes:
    :history = "Created for a test" ;
data:

 time = 0, 16777216, 33554432, 50331648, 67108864, 83886080, 100663296, 
    117440512, 134217728, 150994944 ;
}
4

3 回答 3

2

这是 Scipy 0.11.0 中的一个错误,已在 0.12.0 https://github.com/scipy/scipy/commit/d2b5014中修复

于 2013-05-10T00:21:32.137 回答
1

我认为我的问题是使用 scipy.io 而不是 Scientific.IO

该页面表明 scipy.io 仅用于阅读,Scientific.IO 用于阅读和写作。我不得不使用双数据类型。 http://www-pord.ucsd.edu/~cjiang/python.html

蟒蛇代码:

from Scientific.IO.NetCDF import NetCDFFile
import numpy as np

f = NetCDFFile('simple.nc', 'w')
f.history = 'Created for a test'
f.createDimension('time', 10)
time = f.createVariable('time', 'd', ('time',))
time[:] = np.arange(10)
time.units = 'days since 2008-01-01'
f.close()

输出:

ncdump -v time simple.nc 
netcdf simple {
dimensions:
    time = 10 ;
variables:
double time(time) ;
    time:units = "days since 2008-01-01" ;

// global attributes:
    :history = "Created for a test" ;
data:

 time = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ;
}
于 2013-05-09T14:02:25.040 回答
0

不是解决方案,只是评论:

问题似乎涉及数据类型的字节顺序:

In [23]: x = np.arange(10)
In [30]: x.view('>i4')
Out[30]: 
array([        0,  16777216,  33554432,  50331648,  67108864,  83886080,
       100663296, 117440512, 134217728, 150994944])
于 2013-05-08T23:40:16.007 回答