-
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
Changes from 3 commits
f8d483a
d13528d
e16203a
dad4054
f31cc52
9bf142c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,70 @@ 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" in crs.to_wkt(): | ||
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. It would be great if this |
||
# 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]]) | ||
self._boundary = sgeom.LinearRing(coords.T) | ||
else: | ||
self._boundary = super().boundary | ||
|
||
@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? |
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?