Skip to content

Commit

Permalink
finish the unfinished code yesterday
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelchin committed Oct 19, 2024
1 parent 2d27fcf commit 0bbcd21
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 93 deletions.
2 changes: 0 additions & 2 deletions gplately/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@
from .tools import EARTH_RADIUS
from .utils import io_utils
from .utils.io_utils import get_geometries, get_valid_geometries
from .utils.plot_utils import get_gplot

__pdoc__ = {
"data": False,
Expand All @@ -268,7 +267,6 @@
"data",
"download",
"geometry",
"get_gplot",
"gpml",
"grids",
"oceans",
Expand Down
56 changes: 56 additions & 0 deletions gplately/auxiliary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from typing import Union

from plate_model_manager import PlateModel, PlateModelManager

from .mapping.plot_engine import PlotEngine
from .plot import PlotTopologies
from .reconstruction import PlateReconstruction


def get_gplot(
model_name: str,
model_repo_dir: str,
age: Union[int, float],
plot_engine: PlotEngine = PlotEngine.CARTOPY,
) -> PlotTopologies:
"""auxiliary function to get gplot object"""
try:
model = PlateModelManager().get_model(model_name, data_dir=model_repo_dir)
except:
model = PlateModel(model_name, data_dir=model_repo_dir, readonly=True)

if model is None:
raise Exception(f"Unable to get model ({model_name})")

topology_features = None
static_polygons = None
coastlines = None
COBs = None
continents = None

all_layers = model.get_avail_layers()

if "Topologies" in all_layers:
topology_features = model.get_layer("Topologies")
if "StaticPolygons" in all_layers:
static_polygons = model.get_layer("StaticPolygons")
if "Coastlines" in all_layers:
coastlines = model.get_layer("Coastlines")
if "COBs" in all_layers:
COBs = model.get_layer("COBs")
if "ContinentalPolygons" in all_layers:
continents = model.get_layer("ContinentalPolygons")

m = PlateReconstruction(
model.get_rotation_model(),
topology_features=topology_features,
static_polygons=static_polygons,
)
return PlotTopologies(
m,
coastlines=coastlines,
COBs=COBs,
continents=continents,
time=age,
plot_engine=plot_engine,
)
6 changes: 3 additions & 3 deletions gplately/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@
validate_topology_availability,
)
from .gpml import _load_FeatureCollection
from .mapping.plot_engine import PlotEngine
from .mapping.pygmt_plot import plot_geo_data_frame
from .pygplates import FeatureCollection as _FeatureCollection
from .reconstruction import PlateReconstruction as _PlateReconstruction
from .tools import EARTH_RADIUS
from .utils.feature_utils import shapelify_features as _shapelify_features
from .utils.plot_utils import _clean_polygons, _meridian_from_ax
from .utils.plot_utils import plot_subduction_teeth as _plot_subduction_teeth
from .mapping.plot_engine import PlotEngine
from .mapping.pygmt_plot import plot_geo_data_frame

logger = logging.getLogger("gplately")

Expand Down Expand Up @@ -306,7 +306,7 @@ def __init__(

if self.plate_reconstruction.topology_features is None:
self.plate_reconstruction.topology_features = []
logger.warn("Plate model does not have topology features.")
logger.warning("Plate model does not have topology features.")

self.base_projection = ccrs.PlateCarree()

Expand Down
51 changes: 0 additions & 51 deletions gplately/utils/plot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,8 @@
from shapely.geometry import LineString, MultiPolygon, Point, Polygon, box
from shapely.geometry.base import BaseGeometry, BaseMultipartGeometry
from shapely.ops import linemerge, substring
from plate_model_manager import PlateModel, PlateModelManager
from typing import Union

from .io_utils import get_geometries as _get_geometries
from ..reconstruction import PlateReconstruction
from ..plot import PlotTopologies
from ..mapping.plot_engine import PlotEngine

logger = logging.getLogger("gplately")

Expand Down Expand Up @@ -637,49 +632,3 @@ def plot_subduction_teeth(
else:
for triangle in triangles:
ax.fill(*triangle.exterior.xy, **kwargs)


def get_gplot(
model_name: str, model_repo_dir: str, age: Union[int, float]
) -> PlotTopologies:
"""convenient function to get gplot object"""
try:
model = PlateModelManager().get_model(model_name, data_dir=model_repo_dir)
except:
model = PlateModel(model_name, data_dir=model_repo_dir, readonly=True)

if model is None:
raise Exception(f"Unable to get model ({model_name})")

topology_features = None
static_polygons = None
coastlines = None
COBs = None
continents = None

all_layers = model.get_avail_layers()

if "Topologies" in all_layers:
topology_features = model.get_layer("Topologies")
if "StaticPolygons" in all_layers:
static_polygons = model.get_layer("StaticPolygons")
if "Coastlines" in all_layers:
coastlines = model.get_layer("Coastlines")
if "COBs" in all_layers:
COBs = model.get_layer("COBs")
if "ContinentalPolygons" in all_layers:
continents = (model.get_layer("ContinentalPolygons"),)

m = PlateReconstruction(
model.get_rotation_model(),
topology_features=topology_features,
static_polygons=static_polygons,
)
return PlotTopologies(
m,
coastlines=coastlines,
COBs=COBs,
continents=continents,
time=age,
plot_engine=PlotEngine.PYGMT,
)
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ dependencies = [
"netcdf4",
"rasterio",
"geopandas",
"gmt",
"stripy",
"plate-model-manager>=1.2.1",
"pyyaml",
Expand Down
39 changes: 3 additions & 36 deletions tests-dir/unittest/test_pygmt.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@
"source": [
"import sys\n",
"\n",
"import cartopy.crs as ccrs\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from common import MODEL_REPO_DIR, save_fig\n",
"from plate_model_manager import PlateModel, PlateModelManager\n",
"\n",
"from gplately import PlateReconstruction, PlotTopologies\n",
"from gplately.mapping.plot_engine import PlotEngine\n",
"from gplately import get_gplot\n",
"from gplately.auxiliary import get_gplot\n",
"import pygmt\n",
"pygmt.config(\n",
" FONT_ANNOT=8,\n",
Expand All @@ -28,36 +24,7 @@
" MAP_TICK_LENGTH_PRIMARY=\"4p\",\n",
" )\n",
"\n",
"# MODEL_NAME = \"Clennett2020\"\n",
"# MODEL_NAME = \"Muller2019\"\n",
"MODEL_NAME = \"merdith2021\"\n",
"\n",
"\"\"\"\n",
"try:\n",
" model = PlateModelManager().get_model(MODEL_NAME, data_dir=MODEL_REPO_DIR)\n",
"except:\n",
" model = PlateModel(MODEL_NAME, data_dir=MODEL_REPO_DIR, readonly=True)\n",
"\n",
"if model is None:\n",
" raise Exception(f\"Unable to get model ({MODEL_NAME})\")\n",
"\n",
"age = 55\n",
"\n",
"test_model = PlateReconstruction(\n",
" model.get_rotation_model(),\n",
" topology_features=model.get_layer(\"Topologies\"),\n",
" static_polygons=model.get_layer(\"StaticPolygons\"),\n",
")\n",
"gplot = PlotTopologies(\n",
" test_model,\n",
" coastlines=model.get_layer(\"Coastlines\"),\n",
" COBs=model.get_layer(\"COBs\", return_none_if_not_exist=True),\n",
" continents=model.get_layer(\"ContinentalPolygons\"),\n",
" time=age,\n",
" plot_engine=PlotEngine.PYGMT,\n",
")\n",
"\"\"\"\n",
"gplot = get_gplot(MODEL_NAME,MODEL_REPO_DIR,55)\n",
"gplot = get_gplot(\"merdith2021\", \"plate-model-repo\", age=55, plot_engine=PlotEngine.PYGMT)\n",
"fig = pygmt.Figure()\n",
"fig.basemap(region=\"d\", projection=\"N180/10c\", frame=\"lrtb\")\n",
"#fig.coast(shorelines=True)\n",
Expand Down Expand Up @@ -95,7 +62,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.11.6"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 0bbcd21

Please sign in to comment.