2013-05-08 24 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 ; 
}