Last active
December 7, 2018 21:16
-
-
Save dennissergeev/0e362a8a8a8f12b87221c1519b9903cf to your computer and use it in GitHub Desktop.
Takes a cube which goes longitude 0-360 back to -180-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
| def roll_cube_e2w(cube_in, inplace=False): | |
| """Takes a cube which goes longitude 0-360 back to -180-180.""" | |
| if inplace: | |
| cube = cube_in | |
| else: | |
| cube = cube_in.copy() | |
| lon = cube.coord('longitude') | |
| if (lon.points >= 0.).all(): | |
| if (lon.points <= 360.).all(): | |
| subtract = -180. | |
| cube.data = np.roll(cube.data, | |
| len(lon.points) // 2) | |
| else: | |
| # because for regional runs, UM output | |
| # can have longitudes > 360 | |
| # In this case no data roll needed | |
| subtract = -360. | |
| if lon.has_bounds(): | |
| bounds = lon.bounds + subtract | |
| else: | |
| bounds = None | |
| cube.replace_coord(lon.copy(points=lon.points + subtract, | |
| bounds=bounds)) | |
| else: | |
| # Nothing to do, the cube is already centered on 0 longitude | |
| # unless there is something wrong with longitude | |
| msg = ('Incorrect longitude values:' | |
| f'from {lon.points.min()} to {lon.points.max()}') | |
| assert ((lon.points >= -180.) & (lon.points <= 180.)).all(), msg | |
| if not inplace: | |
| return cube |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment