Skip to content

Instantly share code, notes, and snippets.

@brews
Last active October 26, 2021 21:15
Show Gist options
  • Save brews/4c13460edf928893978778eeef5df090 to your computer and use it in GitHub Desktop.
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.
"""
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