-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix spill over of ocean/and features in cartopy plots in case of geostationary full disc plot. #601
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f8d483a
remove custom feature setup in favour of automatic cartopy features
BENR0 d13528d
fix: spill over of cartopy features in case of full disc
BENR0 e16203a
refactor: import WGS parameters from cartopy
BENR0 dad4054
refactor projection class
BENR0 f31cc52
refactor: make geos boundary static method
9bf142c
Cleanup HTML repr proj warnings and extent floats
djhoese File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
DataArray = np.ndarray | ||
|
||
import cartopy.crs as ccrs | ||
import shapely.geometry as sgeom | ||
|
||
|
||
class Projection(ccrs.Projection): | ||
|
@@ -69,3 +70,77 @@ def __init__(self, | |
if self.bounds is not None: | ||
x0, x1, y0, y1 = self.bounds | ||
self.threshold = min(abs(x1 - x0), abs(y1 - y0)) / 100. | ||
|
||
# For geostationary full disc the boundary needs to be the actuall boundary of the earth disc otherwise | ||
# when ocean or land features are added to the cartopy plot these spill over. | ||
if "Geostationary Satellite" not in crs.to_wkt(): | ||
self._boundary = super().boundary | ||
else: | ||
self._boundary = self._geos_boundary() | ||
|
||
@staticmethod | ||
def _geos_boundary(): | ||
"""Calculate full disk boundary. | ||
|
||
This code is copied over from the 'Geostationary' class in 'cartopy/lib/cartopy/crs.py'. | ||
""" | ||
satellite_height = 35785831 | ||
false_easting = 0 | ||
false_northing = 0 | ||
a = float(ccrs.WGS84_SEMIMAJOR_AXIS) | ||
b = float(ccrs.WGS84_SEMIMINOR_AXIS) | ||
h = float(satellite_height) | ||
|
||
# To find the bound we trace around where the line from the satellite | ||
# is tangent to the surface. This involves trigonometry on a sphere | ||
# centered at the satellite. The two scanning angles form two legs of | ||
# triangle on this sphere--the hypotenuse "c" (angle arc) is controlled | ||
# by distance from center to the edge of the ellipse being seen. | ||
|
||
# This is one of the angles in the spherical triangle and used to | ||
# rotate around and "scan" the boundary | ||
angleA = np.linspace(0, -2 * np.pi, 91) # Clockwise boundary. | ||
|
||
# Convert the angle around center to the proper value to use in the | ||
# parametric form of an ellipse | ||
th = np.arctan(a / b * np.tan(angleA)) | ||
|
||
# Given the position on the ellipse, what is the distance from center | ||
# to the ellipse--and thus the tangent point | ||
r = np.hypot(a * np.cos(th), b * np.sin(th)) | ||
sat_dist = a + h | ||
|
||
# Using this distance, solve for sin and tan of c in the triangle that | ||
# includes the satellite, Earth center, and tangent point--we need to | ||
# figure out the location of this tangent point on the elliptical | ||
# cross-section through the Earth towards the satellite, where the | ||
# major axis is a and the minor is r. With the ellipse centered on the | ||
# Earth and the satellite on the y-axis (at y = a + h = sat_dist), the | ||
# equation for an ellipse and some calculus gives us the tangent point | ||
# (x0, y0) as: | ||
# y0 = a**2 / sat_dist | ||
# x0 = r * np.sqrt(1 - a**2 / sat_dist**2) | ||
# which gives: | ||
# sin_c = x0 / np.hypot(x0, sat_dist - y0) | ||
# tan_c = x0 / (sat_dist - y0) | ||
# A bit of algebra combines these to give directly: | ||
sin_c = r / np.sqrt(sat_dist ** 2 - a ** 2 + r ** 2) | ||
tan_c = r / np.sqrt(sat_dist ** 2 - a ** 2) | ||
|
||
# Using Napier's rules for right spherical triangles R2 and R6, | ||
# (See https://en.wikipedia.org/wiki/Spherical_trigonometry), we can | ||
# solve for arc angles b and a, which are our x and y scanning angles, | ||
# respectively. | ||
coords = np.vstack([np.arctan(np.cos(angleA) * tan_c), # R6 | ||
np.arcsin(np.sin(angleA) * sin_c)]) # R2 | ||
|
||
# Need to multiply scanning angles by satellite height to get to the | ||
# actual native coordinates for the projection. | ||
coords *= h | ||
coords += np.array([[false_easting], [false_northing]]) | ||
return sgeom.LinearRing(coords.T) | ||
|
||
@property | ||
def boundary(self): | ||
"""Return boundary.""" | ||
return self._boundary | ||
Comment on lines
+143
to
+146
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this needed? |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if it's only part of the disc, but including some boundary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this gets into the difference between how I thought and wanted the cartopy projection classes to work (as an AreaDefinition clone) versus how they are intended to work. In all my usage and examples of this class I use it to hold the projection information but also the literal bounds of the data. Cartopy uses bounds to mean the actual physical (?) bounds of the projection space, not of the data. Cartopy/matplotlib use xlims/ylims for data bounds.
So I wonder if we could take the produced geostationary bound here and clip it to the x and y limits passed by the user. I'm not sure that is always accurate though.
Or...we completely rewrite these interfaces and/or examples so that they use upstream cartopy projection objects and includes an extra step to set the xlim/ylims of the plot to match the data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is exactly the issue. And while I think it makes sense to rethink this for the display of the html representation of the
AreaDefinition
this currently seems to work.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok @BENR0, but doesn't the current method (
_geos_boundary
) get the full disk geostationary boundary? So if I try to do the HTML representations of a subset of the full disk (ex. ABI CONUS or mesoscale sector) then my representation would be much bigger than intended/expected?