0

I'm trying to run the code below, where Bwavelength, throughput and newflux are lists.

def ABconversion(Bwavelength, throughput):
 ABconstant=[]
 c=3e18
 i=0
 for i in range(0, len(Bwavelength)):
 ABconstant.append(((3e18/((Bwavelength[i])**2))*throughput[i]))
 i+=1
 print len(Bwavelength), len(ABconstant), ABconstant
 a=Bwavelength[0]
 b=Bwavelength[-1]
 h=((b-a)/len(Bwavelength))
 ABflux = numpy.trapz(Bwavelength, ABconstant, h)
 return ABflux
 def ABmagnitude(newflux, ABflux):
 ABmagarray=[]
 for i in range(len(newflux)):
 ABmag = -2.5*log10((newflux[i])/ABflux) - 48.6
 ABmagarray.append(ABmag)
 return ABmagarray
ABflux1 = ABconversion(Bwavelength, throughput)
print ABflux1
ABmagarray = ABmagnitude(z, ABflux1)
print epoch, ABmagarray

z is defined earlier in the file and is also a list.

However, when I run this I get the message:

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,)

I don't quite understand the error (I'm fairly new to programming), but I think it means the two "shapes" don't have the same dimensions. I'm not sure why this is.

Thanks in advance.

asked Nov 22, 2014 at 15:11
2
  • What does the line print len(Bwavelength), len(ABconstant), ABconstant print? Commented Nov 22, 2014 at 15:27
  • It prints 445, 1, [0.0] Commented Nov 22, 2014 at 15:31

2 Answers 2

1

According to this documentation the parameters to trapz(y, x, dx, axis) are:

  • y - Array like - input array to integrate.
  • x - Optional array - If x is None, then spacing between all y elements is dx.
  • dx - Optional scalar - If x is None, spacing given by dx is assumed. Default is 1.
  • axis - Optional Int - specify the axis.

So you shouldn't specify both x and dx - one of them should be None.

Perhaps this is what you want: trapz(Bwavelength, None, h).

See this answer for more details on the error message and NumPy's "braodcasting rule".

answered Nov 22, 2014 at 15:43
Sign up to request clarification or add additional context in comments.

Comments

1

Replace:

numpy.trapz(Bwavelength, ABconstant, h)

with:

numpy.trapz(np.array(Bwavelength)[:,np.newaxis], ABconstant, h)
answered Nov 22, 2014 at 16:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.