1

I am farily new to python, so that may be why I haven't been able to search properly in this site for an answer to my problem, in case someone know about it already.

I am reading several data files consisting in six columns: XYZ coordinates and 3-vector components around a sphere. I am using the X and Z coordinates to find the angle location of my vectors, and using the angle results as a new column to my array, as a fourth column. So, I have a 7 column array.

The angles are calculated with np.arctan and they get (-) or (+) sign depending on the quadrant the XZ coordinates are located.

Now, I want to make all the angles around the upper half of the sphere (Z positive) positive and spanning from 0º to 180º. On the other hand, I want my angles around the lower half to be negative, and going from 0º to -180º.

The operations to change sign and apply the 0º-180º range are easy to implement. But, I want to use the third column of my array (Z coordinates) as the boolean to decide how to change the angles (fourth column of the array). I have read quite a lot of info about slicing arrays, or modifying columns/rows based on arbitrary boolean conditions applied to the same columns/rows.

I am trying to avoid combining for-loops and if-statements, and learn how to use the power of the python :)

Thanks in advance!

4
  • 3
    You should use np.arctan2(a, b) in place of np.arctan(a/b). i think this might solve your entire problem Commented Feb 5, 2016 at 17:10
  • 2
    It might help if you could provide sample data, and what you might expect the output to be. If you have some code, even better! Commented Feb 5, 2016 at 17:12
  • Thank you Eric, that has fixed it! I guess I need to re-read about arctan2 more carefully. Commented Feb 7, 2016 at 20:45
  • Wayne, I didn't think it was necessary to post a chunck of my data. Apologies for that, next time I will be mroe thorough in my question. Commented Feb 8, 2016 at 11:56

1 Answer 1

1

np.where(cond, if_true, if_false) solves the problem you think you have:

fixed_angle = np.where(z > 0, angle, -angle)

The problem you actually have is that you are not using atan2, or as it's spelt in numpy, np.arctan2

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the detailed answer Eric!
Double thanks! I've just found out it also converts my angle range to the one desired. Where can I add stars to your comment???

Your Answer

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.