Last active
October 26, 2021 21:15
-
-
Save brews/4c13460edf928893978778eeef5df090 to your computer and use it in GitHub Desktop.
Example python function to get the 'inverse longitude' from opposite global hemisphere, from -180 to 180.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Example python and numpy function to get the 'inverse longitude' from opposite global hemisphere, from -180 to 180. | |
| """ | |
| import numpy as np | |
| def wraparound_lon(x): | |
| """Get the 'inverse longitude' from opposite global hemisphere, from -180 to 180. | |
| I'm not sure what the proper term for this is. | |
| """ | |
| a = x - 180.0 | |
| b = 360.0 | |
| # Plug into nearest modulo int: | |
| return a - b * np.round(a / b) # Can also just use `round` if numpy not needed. | |
| # For eg, these should pass. | |
| assert wraparound_lon(-130) == 50.0 | |
| assert wraparound_lon(50) == -130.0 | |
| assert wraparound_lon(130) == -50.0 | |
| assert wraparound_lon(-50) == 130.0 | |
| assert wraparound_lon(10) == -170.0 | |
| assert wraparound_lon(0.5) == -179.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment