I need to calculate in gdal_calc the following python script.

if (A == 0 and B == 0):
        wd = 0.0
        wd = np.arctan2(A,B)

I have tried

if(A = 0 && B = 0,(0.0), else,(arctan2(A,B)))

But the result isn't correct.

2 Answers 2


Nupmpy's arctan2 function already returns 0.0 when A==0 and B==0 so you don't need your if/else statement. Additionally, this function takes positional arguments of the form arctan2(y,x) so be sure to check this aligns with your intended calculation.

Based on your specified parameters, try --calc="arctan2(A,B)". If you can update your question with your actual gdal_calc command it will help with any further troubleshooting.

For more information on syntax see the gdal_calc documentation and this answer to a similar question.

  • Thanks, the returned values are between ]-180,180] but I need then between [0,360[. For this the expression could be like -- calc="A+2pi(less(A,0))"? Commented Jul 8, 2019 at 10:37
  • 1
    Finally I've figured it out. The expression is (A+360)*(A<0). I only need now is to combine the two. Thanks Commented Jul 8, 2019 at 10:56

The correct syntax formula for what I intended to do is:

((degrees (arctan2 (A,B)))+360)*((degrees (arctan2 (A,B)))<0)+(degrees (arctan2 (A,B)))*((degrees (arctan2 (A,B)))>=0)

This formula returns the wind direction in degrees [0, 360[ from U and V wind components.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.