Python: Why isn't this working?/What does this error really mean?

132 views Asked by At

I've currently got the following, but it won't iterate over i. I don't understand why it isn't working. Bwavelength and throughput are lists. It appears that i starts at 0, but won't increase to 1.

ABconstant=[]

c=3e18
for i in range(0, ((len(Bwavelength))-1)):
    ABconstant1=(((3e18/((Bwavelength[i])**2))*throughput[i]))
    ABconstant.append(ABconstant1)
    i+=1
    a=Bwavelength[0]
    b=Bwavelength[-1]
    h=((b-a)/len(Bwavelength))
    ABflux = numpy.trapz(Bwavelength, ABconstant, h)
return ABflux

The error I get is:

Traceback (most recent call last):
  File "Rewrite17.11.2014.py", line 196, in <module>
    ABflux1 = ABconversion(Bwavelength, throughput)
  File "Rewrite17.11.2014.py", line 186, in ABconversion
    ABflux = numpy.trapz(Bwavelength, ABconstant, h)
  File "C:\Python27\lib\site-packages\numpy\lib\function_base.py, line 3234, in trapz
    ret = add.reduce(d * (y[slice1]+y[slice2]/2.0, axis)
ValueError: Operands could not be broadcast together with shapes (0,) (444,)

Bwavelength and throughput are of equal length.

I have no idea what that actually means, despite having looked it up.

Thanks in advance.

1

There are 1 answers

1
Daniel On BEST ANSWER

The loop can be substituted by vector calculations:

c=3e18
ABconstant = c / numpy.array(Bwavelength) ** 2 * throughput
ABflux = numpy.trapz(ABconstant, Bwavelength)
return ABflux