为什么只有在后续执行python函数时才会出现此错误?IndexError:数据数组的大小不符合切片
我正在运行一个python脚本,将一种netCDF4文件转换为另一种,并通过调用我编写的模块中的函数来完成此操作。
该脚本按顺序处理几个文件。当我到达列表中的第二个文件时,在我的函数中的这一位代码中,在“data ['time'] [:]”处得到“IndexError:数据数组的大小不符合切片”:
varobj = cdf.createVariable('time','f8',('time'))
varobj.setncatts(dictifyatts(data['time'],''))
varobj[:] = data['time'][:]
无论文件是什么。脚本总是愉快地处理第一个文件,然后在第二个文件上扼杀,例如,第二次它唤起失败的功能,第一次是OK。
使用调试器我发现varobj [:]和data ['time'] [:]从第一个调用到第二个调用没有区别。具体如下:
二时间的函数被调用,检查变量揭示:
ipdb> data['time']
<class 'netCDF4._netCDF4.Variable'>
float64 time(time)
description: time of measurement
calendar: gregorian
units: seconds since 1970-01-01T00:00:00 UTC
path = /Data/Burst
unlimited dimensions:
current shape = (357060,)
filling off
ipdb> varobj
<class 'netCDF4._netCDF4.Variable'>
float64 time(time)
description: time of measurement
calendar: gregorian
units: seconds since 1970-01-01T00:00:00 UTC
unlimited dimensions:
current shape = (357056,)
filling on, default _FillValue of 9.969209968386869e+36 used
第一次函数被调用,检查变量揭示正好与形状相同大小相同的结果。
在这里报道,这同样的错误: Error when creating variable to create a netCDF file
并在此基础上我想下面的代码来代替:
cf_time = data['time'][:]
cdf.createVariable('time','f8',('time'))
cdf['time'].setncatts(dictifyatts(data['time'],''))
cdf['time'][:] = cf_time[:]
这也不能工作。在相同的情况下也有同样的错误
我出来的想法,可以使用建议下一步检查什么。
感谢巴特刺探形状的变化。这是一个很大的线索。我正在检查文件名。
当我调查的形状变化,我发现我的函数中,输入变量之一是保持从之前时间的函数被调用的信息。
首先,为什么只有一个输入变量会保留陈旧信息?
二,这不应该发生,应该超出范围。
我会尝试重现最小代码这种行为,诠释,他的同时,回答关于蟒蛇范围的问题,将不胜感激 - 我想我明白蟒蛇如何处理范围。
下面是最少的代码将说明此问题。不知何故主叫功能可以改变一个变量(good_ens),其超出范围。
def doFile(infileName, outfileName, goodens, timetype, flen):
print('infilename = %s' % infileName)
print('outfilename = %s' % outfileName)
print('goodens at input are from %d to %d' % (goodens[0],goodens[1]))
print('timetype is %s' % timetype)
maxens = flen # fake file length
print('%s time variable has %d ensembles' % (infileName,maxens))
# TODO - goodens[1] has the file size from the previous file run when multiple files are processed!
if goodens[1] < 0:
goodens[1] = maxens
print('goodens adjusted for input file length are from %d to %d' % (goodens[0],goodens[1]))
nens = goodens[1]-goodens[0]
print('creating new netCDF file %s with %d records (should match input file)' % (outfileName, nens))
datapath = ""
datafiles = ['file0.nc',\
'file1.nc',\
'file2.nc',\
'file3.nc']
# fake file lengths for this demonstration
datalengths = [357056, 357086, 357060, 199866]
outfileroot = 'outfile'
attFile = datapath + 'attfile.txt'
# this gets changed! It should never be changed!
# ask for all ensembles in the file
good_ens = [0,-1]
# -------------- beyond here the user should not need to change things
for filenum in range(len(datafiles)):
print('\n--------------\n')
print('Input Parameters before function call')
print(good_ens)
inputFile = datapath + datafiles[filenum]
print(inputFile)
l = datalengths[filenum]
print(l)
outputFile = datapath + ('%s%03d.cdf' % (outfileroot,filenum))
print(outputFile)
print('Converting from %s to %s' % (inputFile,outputFile))
# the variable good_ens gets changed by this calling function, and should not be
doFile(inputFile, outputFile, good_ens, 'CF', l)
# this works, but will not work for me in using this function
#doNortekRawFile(inputFile, outputFile, [0,-1], 'CF', l)
我可以证实这是发生在两个不同的anaconda python安装上,conda 4.2.13和4.3.22 –
奇怪的问题;我发现形状发生了变化,但这不应该是无限维度的问题。你能用[最小工作示例](https://stackoverflow.com/help/mcve)重现问题吗?这使得其他人可以更容易地尝试调试此问题。 – Bart
感谢您看到形状变化 - 我认为它没有。可能就是这样。 –