Skip to content
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

Replace Cartopy with PyGMT #327

Merged
merged 23 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
37fa3a7
pygmt for user guide examples
mdtanker Jun 9, 2022
f0fee50
pygmt for gallery examples
mdtanker Jun 9, 2022
2b08618
updated gallery to pygmt
mdtanker Jun 20, 2022
88b8acd
Merge branch 'main' into pygmt_examples
mdtanker Jun 20, 2022
6846d8a
formatting changes
mdtanker Jun 20, 2022
b810e95
fixed issue with EqSources
mdtanker Jun 21, 2022
44ca5dc
updated user_guide/forward_modelling to pygmt
mdtanker Jun 24, 2022
5444250
formatting fix
mdtanker Jun 24, 2022
7e3b2d4
fixed conflicts with examples/isostatic_moho_airy.py
mdtanker Jul 3, 2022
15f738f
fixed moho airy .py file
mdtanker Jul 3, 2022
e6e7951
fixed moho airy .py file
mdtanker Jul 3, 2022
b1245c5
fixed eq sources .rst files
mdtanker Jul 4, 2022
6cb9463
Merge branch 'fatiando:main' into pygmt_dev
mdtanker Jul 18, 2022
7deccf3
Merge branch 'pygmt_dev' of https:/mdtanker/harmonica int…
mdtanker Jul 18, 2022
70a49b8
set pygmt external display to off
mdtanker Jul 18, 2022
bc73cff
replaced cartopy and matpotlib with pygmt in install.rst description
mdtanker Jul 18, 2022
6560334
Merge branch 'main' into pygmt_examples
mdtanker Sep 2, 2022
131a566
Fix comment mentioning "cartopy" in point forward model
santisoler Sep 2, 2022
7ee4b3b
Use PyGMT in gravity disturbance example
santisoler Sep 2, 2022
0899474
Ditch cartopy from requirements and environment files
santisoler Sep 2, 2022
7eae0d5
Pin pygmt, gmt and gdal versions
santisoler Sep 2, 2022
fbddcc1
Center colormap in gravity disturbance plot
santisoler Sep 9, 2022
f181f53
Merge branch 'main' into pygmt_examples
santisoler Sep 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 29 additions & 28 deletions data/examples/britain_magnetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
If the file isn't already in your data directory, it will be downloaded
automatically.
"""
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np
import pygmt
import verde as vd

import harmonica as hm
Expand All @@ -40,31 +39,33 @@
data = hm.datasets.fetch_britain_magnetic()
print(data)

# Plot the observations in a Mercator map using Cartopy
fig = plt.figure(figsize=(7.5, 10))
ax = plt.axes(projection=ccrs.Mercator())
ax.set_title("Magnetic data from Great Britain", pad=25)
maxabs = np.percentile(data.total_field_anomaly_nt, 99)
tmp = ax.scatter(
data.longitude,
data.latitude,
c=data.total_field_anomaly_nt,
s=0.001,
cmap="seismic",
vmin=-maxabs,
vmax=maxabs,
transform=ccrs.PlateCarree(),
# Get the region of the data
region = vd.get_region((data.longitude, data.latitude))

# Plot the observations in a Mercator map using PyGMT
fig = pygmt.Figure()

title = "Magnetic data from Great Britain"

# Make colormap scaled to 97 percentile of data.
maxabs = np.percentile(data.total_field_anomaly_nt, 97)
pygmt.makecpt(cmap="polar", series=(-maxabs, maxabs), background=True)

fig.plot(
region=region,
projection="M10c",
frame=["ag", f"+t{title}"],
x=data.longitude,
y=data.latitude,
color=data.total_field_anomaly_nt,
style="c0.02c",
cmap=True,
)
plt.colorbar(
tmp,
ax=ax,
label="total field magnetic anomaly [nT]",
orientation="vertical",
aspect=50,
shrink=0.7,
pad=0.1,

fig.colorbar(
cmap=True, position="JMR", frame=["a100f50", "x+ltotal field magnetic anomaly [nT]"]
)
ax.set_extent(vd.get_region((data.longitude, data.latitude)))
ax.gridlines(draw_labels=True)
ax.coastlines(resolution="50m")
plt.show()

fig.coast(shorelines="1p,black")

fig.show()
29 changes: 18 additions & 11 deletions data/examples/earth_geoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,29 @@
with 0.5 degree spacing and was generated from the spherical harmonic model
EIGEN-6C4 [Forste_etal2014]_.
"""
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import pygmt

import harmonica as hm

# Load the geoid grid
data = hm.datasets.fetch_geoid_earth()
print(data)

# Make a plot of data using Cartopy
plt.figure(figsize=(10, 10))
ax = plt.axes(projection=ccrs.Orthographic(central_longitude=100))
pc = data.geoid.plot.pcolormesh(ax=ax, transform=ccrs.PlateCarree(), add_colorbar=False)
plt.colorbar(
pc, label="meters", orientation="horizontal", aspect=50, pad=0.01, shrink=0.6
# Make a plot using PyGMT
fig = pygmt.Figure()

title = "Geoid heights (EIGEN-6C4)"

fig.grdimage(
region="g",
projection="G100/0/15c",
frame=f"+t{title}",
grid=data.geoid,
cmap="vik",
)
ax.set_title("Geoid heights (EIGEN-6C4)")
ax.coastlines()
plt.show()

fig.coast(shorelines="0.5p,black", resolution="crude")

fig.colorbar(cmap=True, frame=["a25f10", "x+lmeters"])

fig.show()
31 changes: 18 additions & 13 deletions data/examples/earth_gravity.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,29 @@
spacing at 10km ellipsoidal height. It was generated from the spherical
harmonic model EIGEN-6C4 [Forste_etal2014]_.
"""
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import pygmt

import harmonica as hm

# Load the gravity grid
data = hm.datasets.fetch_gravity_earth()
print(data)

# Make a plot of data using Cartopy
plt.figure(figsize=(10, 10))
ax = plt.axes(projection=ccrs.Orthographic(central_longitude=150))
pc = data.gravity.plot.pcolormesh(
ax=ax, transform=ccrs.PlateCarree(), add_colorbar=False
)
plt.colorbar(
pc, label="mGal", orientation="horizontal", aspect=50, pad=0.01, shrink=0.6
# Make a plot using Pygmt
fig = pygmt.Figure()

title = "Gravity of the Earth (EIGEN-6C4)"

fig.grdimage(
region="g",
projection="G150/0/15c",
frame=f"+t{title}",
grid=data.gravity,
cmap="viridis",
)
ax.set_title("Gravity of the Earth (EIGEN-6C4)")
ax.coastlines()
plt.show()

fig.coast(shorelines="0.5p,black", resolution="crude")

fig.colorbar(cmap=True, frame=["a1000f250", "x+lmGal"])

fig.show()
31 changes: 18 additions & 13 deletions data/examples/earth_topography.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,29 @@
we downsampled to 0.5 degree grid spacing to save space and download times.
Heights are referenced to sea level.
"""
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import pygmt

import harmonica as hm

# Load the topography grid
data = hm.datasets.fetch_topography_earth()
print(data)

# Make a plot of data using Cartopy
plt.figure(figsize=(10, 10))
ax = plt.axes(projection=ccrs.Orthographic(central_longitude=-30))
pc = data.topography.plot.pcolormesh(
ax=ax, transform=ccrs.PlateCarree(), add_colorbar=False, cmap="terrain"
)
plt.colorbar(
pc, label="meters", orientation="horizontal", aspect=50, pad=0.01, shrink=0.6
# Make a plot using PyGMT
fig = pygmt.Figure()

title = "Topography of the Earth (ETOPO1)"

fig.grdimage(
region="g",
projection="G-30/0/15c",
frame=f"+t{title}",
grid=data.topography,
cmap="globe",
)
ax.set_title("Topography of the Earth (ETOPO1)")
ax.coastlines()
plt.show()

fig.coast(shorelines="0.5p,black", resolution="crude")

fig.colorbar(cmap=True, frame=["a2000f500", "x+lmeters"])

fig.show()
46 changes: 26 additions & 20 deletions data/examples/south_africa_gravity.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
documentation for :func:`harmonica.datasets.fetch_south_africa_gravity` for
more information.
"""
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import pygmt
import verde as vd

import harmonica as hm
Expand All @@ -28,22 +27,29 @@
data = hm.datasets.fetch_south_africa_gravity()
print(data)

# Plot the observations in a Mercator map using Cartopy
fig = plt.figure(figsize=(6.5, 5))
ax = plt.axes(projection=ccrs.Mercator())
ax.set_title("Observed gravity data from South Africa", pad=25)
tmp = ax.scatter(
data.longitude,
data.latitude,
c=data.gravity,
s=0.8,
cmap="viridis",
transform=ccrs.PlateCarree(),
)
plt.colorbar(
tmp, ax=ax, label="observed gravity [mGal]", aspect=50, pad=0.1, shrink=0.92
# Get the region of the grid
region = vd.get_region((data.longitude.values, data.latitude.values))

# Make a plot using PyGMT
fig = pygmt.Figure()

title = "Observed gravity data from South Africa"

pygmt.makecpt(cmap="viridis", series=(data.gravity.min(), data.gravity.max()))

fig.plot(
region=region,
projection="M15c",
frame=["ag", f"+t{title}"],
x=data.longitude,
y=data.latitude,
color=data.gravity,
style="c0.1c",
cmap=True,
)
ax.set_extent(vd.get_region((data.longitude, data.latitude)))
ax.gridlines(draw_labels=True)
ax.coastlines()
plt.show()

fig.coast(shorelines="1p,black")

fig.colorbar(cmap=True, frame=["a200f50", "x+lobserved gravity [mGal]"], position="JMR")

fig.show()
35 changes: 22 additions & 13 deletions data/examples/south_africa_topography.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,33 @@
we downsampled to 0.1 degree grid spacing to save space and download times.
Heights are referenced to sea level.
"""
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import pygmt
import verde as vd

import harmonica as hm

# Load the topography grid
data = hm.datasets.fetch_south_africa_topography()
print(data)

# Make a plot of data using Cartopy
plt.figure(figsize=(10, 10))
ax = plt.axes(projection=ccrs.Mercator())
pc = data.topography.plot.pcolormesh(
ax=ax, transform=ccrs.PlateCarree(), add_colorbar=False, cmap="terrain"
)
plt.colorbar(
pc, label="meters", orientation="horizontal", aspect=50, pad=0.01, shrink=0.6
# Get the region of the grid
region = vd.get_region((data.longitude.values, data.latitude.values))

# Make a plot using PyGMT
fig = pygmt.Figure()

title = "Topography of South africa (ETOPO1)"

fig.grdimage(
region=region,
projection="M15c",
grid=data.topography,
frame=["ag", f"+t{title}"],
cmap="earth",
)
ax.set_title("Topography of South africa (ETOPO1)")
ax.coastlines()
plt.show()

fig.colorbar(cmap=True, frame=["a2000f500", "x+lmeters"])

fig.coast(shorelines="1p,black")

fig.show()
3 changes: 2 additions & 1 deletion doc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ html: api
@echo
@echo "Building HTML files."
@echo
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
# Set PYGMT_USE_EXTERNAL_DISPLAY to "false" to disable external display
PYGMT_USE_EXTERNAL_DISPLAY="false" $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."

Expand Down
14 changes: 11 additions & 3 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#
import datetime

import pygmt.sphinx_gallery
import pyvista
from sphinx_gallery.sorting import FileNameSortKey

Expand Down Expand Up @@ -49,14 +50,17 @@
"pandas": ("http://pandas.pydata.org/pandas-docs/stable/", None),
"xarray": ("http://xarray.pydata.org/en/stable/", None),
"xrft": ("https://xrft.readthedocs.io/en/stable/", None),
"cartopy": ("https://scitools.org.uk/cartopy/docs/latest/", None),
"pooch": ("https://www.fatiando.org/pooch/latest/", None),
"verde": ("https://www.fatiando.org/verde/latest/", None),
"boule": ("https://www.fatiando.org/boule/latest/", None),
"matplotlib": ("https://matplotlib.org/", None),
"pyproj": ("https://pyproj4.github.io/pyproj/stable/", None),
"pyvista": ("https://docs.pyvista.org", None),
"numba_progress": ("https://pypi.org/project/numba-progress/", None),
"numba_progress": (
"https://pypi.org/project/numba-progress/",
None,
),
"pygmt": ("https://www.pygmt.org/latest/", None),
}

# Autosummary pages will be generated by sphinx-autogen instead of sphinx-build
Expand Down Expand Up @@ -103,7 +107,11 @@
# Insert links to documentation of objects in the examples
"reference_url": {"harmonica": None},
# Add pyvista to the image scrapers
"image_scrapers": ("pyvista", "matplotlib"),
"image_scrapers": (
"pyvista",
"matplotlib",
pygmt.sphinx_gallery.PyGMTScraper(),
),
}


Expand Down
3 changes: 1 addition & 2 deletions doc/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ Optional:
The examples in the :ref:`gallery` also use:

* `boule <http://www.fatiando.org/boule/>`__
* `matplotlib <https://matplotlib.org/>`__
* `cartopy <https://scitools.org.uk/cartopy/>`__ for plotting maps
* `pygmt <https://www.pygmt.org/>`__ for plotting maps
* `pyproj <https://jswhit.github.io/pyproj/>`__ for cartographic projections


Expand Down
Loading