2013-05-08 49 views
1

我想写一个netcdf文件使用scipy。 我已经从scipy网站上复制了这个例子 - 但是当我看着输出时,我得到了奇怪的数字。scipy --netcdf - 数据类型不写和得到奇怪的数字

我一直在尝试其他的东西,甚至为另一个我声明为'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 ; 
} 
+0

什么操作系统和你使用的是什么版本的scipy?我无法在Ubuntu上重现此问题,scipy版本为0.9.0或0.12.0。 – unutbu 2013-05-08 23:43:54

+0

看来如果我使用'导入Scientific.IO.NetCDF作为NetCDF'而不是'scipy.io import netcdf' - 我可以使用双精度值并且得到合理的值。不知道它是否会在以后给我更多的数据类型问题。 – dianei 2013-05-09 13:51:04

回答

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]) 
+0

我使用OS 10.8.2和scipy版本0.11.0。 – dianei 2013-05-09 13:50:25

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