我是netCDF库的新用户,所以我为这个基本问题道歉。 我试图将具有4列(lon,lat,depth,velocity)的ascii文件转换为netCDF。我收到以下错误:
Vp [:] = file [:,:,:,] IndexError:数组
的索引太多你能帮我找一下错误的原因吗?我已将变量的尺寸设置为行数。是吗?
提前致谢。
文件很大,您可以在此处找到示例:https://www.dropbox.com/s/qq9529vcvq8t1av/test.txt?dl=0
# -*- coding: utf-8 -*-
from netCDF4 import Dataset
import numpy as np
import sys
file = np.loadtxt('test.txt', delimiter=' ')
# NC file setup
mydata = Dataset('Vp.nc', 'w', format='NETCDF4')
mydata.description = '3D VP velocity model'
# dimensions
mydata.createDimension('latitude', 1000)
mydata.createDimension('longitude', 1000)
mydata.createDimension('depth', 1000)
Vp = mydata.createVariable('Vp', 'f4', ('latitude', 'longitude', 'depth'), fill_value=0)
Vp[:] = file[:,:,:]
Vp.units = 'km/s'
答案 0 :(得分:1)
以下是几个问题
file
这是一个有效的例子,但我没有提取正确的变量数据,因为我不知道实际的结构,也要小心重塑。
from netCDF4 import Dataset
import numpy as np
import sys
input_file = np.loadtxt('test.txt', delimiter=' ')
# NC file setup
mydata = Dataset('Vp.nc', 'w', format='NETCDF4')
mydata.description = '3D VP velocity model'
# dimensions
mydata.createDimension('longitude', 128)
mydata.createDimension('latitude', 4)
lat = mydata.createVariable('latitude', 'f4','longitude')
lat.long_name = "latitude"
lat.units = "degrees_north"
lat.standard_name = "latitude"
lon = mydata.createVariable('longitude', 'f4','latitude')
lon.long_name = "longitude"
lon.units = "degrees_east"
lon.standard_name = "longitude"
## mydata.createVariable('depth', 1)
Vp = mydata.createVariable('Vp', 'f4', ('longitude','latitude'), fill_value=0)
Vp[:128,:4] = input_file[:512,3].reshape(128,4)
## lon[:] = ???
## lat[:] = ???
Vp.units = 'km/s'