Scipy - netcdf - 数据类型不写和得到奇怪的数字

时间:2013-05-08 23:11:59

标签: python scipy netcdf

我正在尝试使用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 ;
}

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

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

Python代码:

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 ;
}

答案 2 :(得分: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])