Search code examples
pythonidl-programming-language

Converting the simple IDL argument code to python code


Simply I want to translate the IDL code to python code. This is an easy task for any IDL and Python Expert. But it is not possible for me, if anybody helps me to translate this code then I will thankful to him/her.

RelA = ABS((image1 ) - (image2))
index = where(RelA gt 180.D)
RelA[index] = 360.D- RelA[index]
index = where(RelA le 180.)
RelA[index] = 180.D- RelA[index]
CosRe = cos(RelA * !pi / 180.D)

Solution

  • It's a straightforward conversion to NumPy:

    #RelA = ABS((image1 ) - (image2))
    RelA = np.abs(image1 - image2)
    
    #index = where(RelA gt 180.D)
    index = np.where(RelA > 180.0)
    
    #RelA[index] = 360.D- RelA[index]
    RelA[index] = 360.0 - RelA[index]
    
    #index = where(RelA le 180.)
    index = np.where(RelA <= 180.0)
    
    #RelA[index] = 180.D- RelA[index]
    RelA[index] = 180.0 - RelA[index]
    
    #CosRe = cos(RelA * !pi / 180.D)
    CosRe = np.cos(np.radians(RelA))
    

    To translate your second piece of code, just remove the "D" specifiers (Python scalars are automatically higher precision), use "**" instead of "^", and use Numpy's cos function:

    D1 = (0.00864 + (0.0000065)) * (0.86D)**(-(3.916 + (0.074 * 0.86)+ (0.05/0.86D)))
    D2 = 0.008569 * ((0.8585)**(-4))*(1. + (0.0113 *(0.8585)**(-2)) + (0.00013 * (0.8585D)^(-4)))
    D1 = (Pz/Po) * (0.00864 + (0.0000065 * z)) * (0.55)**(-(3.916 + (0.074 * 0.55)+ (0.05/0.55)))
    Pr1 = (3./16.*np.pi)* (2/(2+delta))* ((1+delta)+((1+delta)*(np.cos(Agl) * np.cos(Agl))))
    Pr2 =  (3./16.*np.pi) * (1 + (np.cos(Agl) * np.cos(Agl)))