I have a four-dimensional array T (shape=(361,30,100,257)) that I need to scale by a 30-element vector, P. I need the resulting vector to have the same shape as the original T array, and I need the scaling to be indexed by the second element of T. I have tried the obvious:
for i in range(len(PressFrac)):
theta = T[:,i,:,:]*(PressFrac[i])
but the resulting theta loses the second dimension.
What I need is the first, third, and fourth dimensions multiplied by PressFrac[i] and the second dimension (which is an array called "lev") preserved.
I have tried using np.expand_dims and np.insert below the loop to add back in the second dimension but I get various broadcast errors. I have tried the following and combinations thereof:
theta = np.expand_dims(theta, axis = 1)
np.insert(theta,1,lev,axis=1)
theta = np.stack(theta, lev, axis =1)
theta = np.array(theta)[:,np.newaxis,:,:]
Any ideas would be great!