Skip to content

Instantly share code, notes, and snippets.

@darothen
Last active April 25, 2023 11:55
Show Gist options
  • Save darothen/c7560d8d19ffca90024c1f2df4927599 to your computer and use it in GitHub Desktop.
Save darothen/c7560d8d19ffca90024c1f2df4927599 to your computer and use it in GitHub Desktop.
Example of adding cyclic points to DataArrays for plotting with Cartopy
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@Sumit-Mukherjee
Copy link

Hi, Dan. I was plotting some model data where longitudes are at varying intervals [-279.515801, -278.547384, -277.578912, . . . , -100.484199, -99.515801, -98.547384, . . . , 77.578912, 78.547384, 79.515801]. Applying 'add_cyclic_point' I am getting "ValueError: The coordinate must be equally spaced". Can you please help me out?

@darothen
Copy link
Author

@Sumit-Mukherjee, you can see in the underlying implementation of add_cyclic_point() that there is an explicit check that the values are equally spaced. It's not obvious to me that the rest of the code wouldn't work if the values weren't equally spaced, so you could try copy/pasting that function into your code, removing the exception, and see if that works.

@Sumit-Mukherjee
Copy link

It worked! One more way I found is to set the tolerance (rtol) in numpy.allclose used in the function add_cyclic_point() according to my coordinate values. Thank you for the suggestion.

@sevfour
Copy link

sevfour commented Jan 25, 2022

Hello!
I am having a similar issue but my lat and lon are 2 dimensional and not equally spaced (it's in a polar projection). add_cyclic_point() seem to only work with 1 dimensional longitudes. Is there an equivalent way to make it work in this case?
Thank you!

@Wamashudu
Copy link

@Sumit-Mukherjee Please share the code

@Sumit-Mukherjee
Copy link

@Wamashudu Inside the function add_cyclic_point, modify the following snippet setting atol (default value 1e-5) according to your data values (for my case it is 0.1).
if not np.allclose(delta_coord, delta_coord[0], atol=0.1):
raise ValueError('The coordinate must be equally spaced.')

Commenting out these two lines will also work.

@xiuyongwu
Copy link

@sevfour I also encounter the same problem, have you found out a way to solve this? Thanks in advance if you can share your experience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment