IndexError:只有整数,切片(:
),省略号(...
),numpy.newaxis(None
)和整数或布尔数组是有效的索引,在使用时会弹出此错误python 2.7环境
我是python的新手,需要此错误的说明和解决方案
from numpy import *
import sys
import os
import csv
import cPickle
#import write_ncfile
#reload(write_ncfile)
#try:
from write_ncfile import *
#except ImportError:
# from write_ncfile_old import *
from helpers import *
from helpers3 import *
def mk_allfluxes(m, nseasons=12):
'''Calculates the net in/outflow from all compartments at every timestep'''
air = [0,1]
stationary = [2,5,6]
water = [3,4]
flux_key = normalizefluxkey(m.flux_key)
fluxdict = {}
fluxes=["flow", "air_to_water", "deg", "sed", "air_to_x", "water_to_x",
"air_to_veg", "veg_to_air", "air_to_freshwater", "freshwater_to_air", # last 3 lines added by RKG, 25.08.2014
"air_to_ocean", "ocean_to_air", "air_to_soil", "soil_to_air", # These are all gross fluxes
"air_to_all", "all_to_air", "air_to_all_net", # except: "air_to_all_net"
"upper_air_to_lower_air", # this one added by RKG, 28.08.2014
"flow_in", "flow_out", "flow_in_lower_air",# "flow_in_others"] # these are added by RKG, 28.08.2014
"flow_out_lower_air", "flow_in_upper_air", "flow_out_upper_air",
"flow_in_freshwater", "flow_out_freshwater",
"flow_in_ocean", "flow_out_ocean",
"flow_out_air", "flow_in_air",
"deg_upper_air", "deg_lower_air", "deg_air", "deg_vegetation",
"deg_freshwater", "deg_ocean", "deg_soil", "deg_sediment",
"lower_air_to_upper_air",
"air_to_sediment", "sediment_to_air", "air_to_sediment_net", #last 8 lines added by RKG, 27.11.2014
"flow_in_soil", "flow_out_soil", "flow_in_sediment", "flow_out_sediment",
"soil_to_freshwater", "freshwater_to_soil", "soil_to_freshwater_net",
"soil_to_ocean", "ocean_to_soil", "soil_to_ocean_net",
"sediment_to_freshwater", "freshwater_to_sediment", "sediment_to_freshwater_net",
"sediment_to_ocean", "ocean_to_sediment", "sediment_to_ocean_net", # last 5 lines added by RKG, 17.12.2014
"sed_upper_air", "sed_lower_air", "sed_air", "sed_vegetation",
"sed_freshwater", "sed_ocean", "sed_soil", "sed_sediment", # last 2 lines added by RKG, 07.02.2015
"freshwater_to_ocean"] # this line added by RKG, 02.06.2015
for key in fluxes:
fluxdict[key] = zeros((len(m.flux_res), m.nocells))
compmap = mk_compmap(m)
ts0 = time.time()
ts1 = time.time()
for ts in range(len(m.flux_res)):
season = ts % nseasons
year = ts / nseasons
if season ==0:
print ("Processing year %i \t" %(year)),
fluxmat = m.flux_res[ts]
x,y = fluxmat.nonzero()
for i in range(len(x)):
value = fluxmat[x[i],y[i]]
xcell, xcomp = x_to_cellcomp( x[i], compmap, m.nocomp)
ycell, ycomp = x_to_cellcomp( y[i], compmap, m.nocomp)
if x[i]==y[i]:
try :
match_dict = m.flux_key[season][xcell][xcomp]
except KeyError:
print [[season],[xcell],[xcomp]]
for mnkey in match_dict.keys():
if type(mnkey) is tuple:
#print "mk_allfluxes:", i, "mnkey: ", mnkey, "xcell: ",xcell, "xcomp: ", xcomp
fluxdict["flow"][ts][xcell] += flux_key[season][xcell][xcomp][mnkey] * value
fluxdict["flow"][ts][(mnkey[1])-1] -= flux_key[season][xcell][xcomp][mnkey] * value
IndexError: only integers, slices (
:), ellipsis (
... ), numpy.newaxis (
无) and integer or boolean arrays are valid indices