2014-06-24 50 views
1

我是python的新手,並且對matlab有一些經驗。我沒有從netCDF文件中獲得正確的輸出長波輻射值,並且我嘗試了scipy.io.netcdf模塊和Scientific.IO.NetCDF模塊。在python中從NetCDF文件中提取變量的問題

import Scientific.IO.NetCDF as S 
fileobj = S.NetCDFFile('filename.nc', mode='r') 
data = fileobj.variables['var'].getValue() # I tried both with and w/o the .getValue() 
print data[0:10,43,80] #first ten points in time at a specific lat/lon 
[ -5124 -5335 -5121 -5499 -5508 -8930 -10111 -9435 -8534 -8487] 

我的代碼使用scipy.io.netcdf是相同的,除了我沒有使用.getValue()。然後我嘗試在matlab中的這個練習

data = ncread('filename.nc','var'); 
data[80,43,1:10] %note matlab orders the data lon, lat, time 
ans(:,:,1) = 

    275.0400 


ans(:,:,2) = 

    279.0800 


ans(:,:,3) = 

    279.6800 


ans(:,:,4) = 

    277.8700 


ans(:,:,5) = 

    275.5900 


ans(:,:,6) = 

    241.4700 


ans(:,:,7) = 

    223.1900 


ans(:,:,8) = 

    235.5700 


ans(:,:,9) = 

    239.8200 


ans(:,:,10) = 

    249.5400 

我知道matlab產生的值是正確的。這個變量應該在80-330(瓦每平方米)的範圍內。任何關於python進展的想法? 感謝

+0

它不應該是'打印數據[0:10,42,79]'呢? Python索引從0開始,Matlab從1開始。 –

+0

啊,是的。你是對的。但是,這些值仍然在-10,000範圍內,而它們應該在80-330範圍內。我今天做了更多的工作,發現如果我使用MATLAB的'低級'netcdf軟件包,只需調用netcdf.getVar,那麼我可以得到與python相同的答案。 – asl

回答

0

試試這個語法:

from scipy.io.netcdf import netcdf_file as Dataset 

ncfile = Dataset('filename.nc','r') 
data = ncfile.variables['var'][:,:,:]