Question

Afternoon everyone. I'm currently porting over an IDL code to python and it's been plain sailing up until this point so far. I'm stuck on this section of IDL code:

nsteps = 266    
ind2 = ((lindgen(nsteps+1,nsteps+1)) mod (nsteps+1))
dk2 = (k2arr((ind2+1) < nsteps) - k2arr(ind2-1) > 0)) / 2.

My version of this includes a rewritten lindgen function as follows:

def pylindgen(shape):
    nelem = numpy.prod(numpy.array(shape))
    out = numpy.arange(nelem,dtype=int)
    return numpy.reshape(out,shape)

... and the ported code where k2arr is an array of shape (267,):

ind2 = pylindgen((nsteps+1,nsteps+1)) % (nsteps+1)
dk2 = (k2arr[ (ind2+1) < nsteps ] - k2arr[ (ind2-1) > 0. ]) / 2.

Now, the problem is that my code makes ind2 an array where, by looking at the IDL code and the errors thrown in the python script, I'm sure it's meant to be a scalar. Am I missing some feature of these IDL functions?

Any thoughts would be greatly appreciated. Cheers.

Était-ce utile?

La solution

My knowledge of IDL is not what it used to be, I had to research a little. The operator ">" in IDL is not an equivalent of python (or other languages). It stablishes a maximum, anything above it will be set to that value. Same goes for "<", obviously, it sets a minimum.

dk2 = (k2arr((ind2+1) < nsteps) - k2arr(ind2-1) > 0)) where k2arr is 266 and ind2 is (266,266) is equivalent to saying:

 - (ind2+1 < nsteps) take ind2+1 and, in any place that ind2+1 
   is greater than nsteps, replace by nsteps. 
 - (ind2-1 > 0) take ind2-1 and, in any place that ind2-1 is 
   less than zero, put zero instead. 

The tricky part is now. k2arr (266,) is evaluated for each of the rows of (ind2+1) and (ind2-1), meaning that if (ind2+1 < nsteps) = [1,2,3,...,nsteps-1, nsteps, nsteps] the k2arr will be evaluated for exactly that 266 times, one on top of the other, with the result being (266,266) array.

And NOW I remember why I stopped programming in IDL!

Autres conseils

The code for pylindgen works perfectly for me. Produces an array of (267,267), though. IF k2array is a (267,) array, you should be getting an error like:

ValueError: boolean index array should have 1 dimension

Is that your problem? Cheers

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top