我能够计算出lat,lon维度声明,如下面的代码所示。我想我很快就能得到netCDF文件了。但是,我得到一个错误。在
import numpy as np
import netCDF4
import os
# load the data
path='C:\Users\.spyder2'
os.chdir(path)
# this load the file into a Nx3 array (three columns)
data = np.loadtxt('TRMM_1998_01_0100_newntcl.csv', delimiter=',')
# create a netcdf Data object
with netCDF4.dataset('TEST_file.nc', mode="w", format='NETCDF4') as ds:
# some file-level meta-data attributes:
ds.Conventions = "CF-1.6"
ds.title = 'Non TC precipitation'
ds.institution = 'USU'
lat = data[:,0] # the first column
lon = data[:,1] # the second column
precip = data[:,2] # the third column
nlat = lat.reshape( (161, 321) )
nlon = lon.reshape( (161, 321) )
# time = ds.createDimension('time', 0)
ds.createDimension('latitude', 161)
ds.createDimension('longitude', 321)
precip = ds.createVariable('precip', 'f4', ('latitude', 'longitude'))
precip[:] = data[:,:]
## adds some attributes
precip.units = 'mm'
precip.long_name = 'Precipitation'
错误:
^{pr2}$如果你能在这里澄清一下或者给我一些线索来纠正它,我将不胜感激。在
提前谢谢你
错误“数据数组的大小不符合切片”表明}的项数不同。在代码中,看起来
precip[:]
和{precip
应该是一个大小为161x321的变量。您正试图分配它data[:,:]
,它看起来像所有3列信息。在我想你真正想要的是:
precip[:] = data[:,2]
创建维度是通过给变量赋值来完成的。定义维度时只需删除},然后就可以在创建变量时引用这些维度。在
lat=
和{另外,在脚本中多次重复使用}或{}作为维度名称。在
lat
时,请注意范围问题。在处理实际数据、命名维度和维度大小时,最好使用唯一的名称。我通常做一些类似于lat_arr
(对于数据)、nlat
(对于维度大小),以及{相关问题 更多 >
编程相关推荐