在python中从NetCDF文件中提取变量的问题

时间:2014-06-24 20:19:53

标签: python matlab netcdf

我是python的新手,并且对matlab有一些经验。我没有从netCDF文件中获取正确的输出长波辐射值,我已经尝试了scipy.io.netcdf模块和Scientific.IO.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发生了什么的任何想法? 感谢

1 个答案:

答案 0 :(得分:0)

试试这个语法:

from scipy.io.netcdf import netcdf_file as Dataset

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