Skip to content

Commit

Permalink
work on documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
b8raoult committed Mar 25, 2024
1 parent 0e5d45d commit 9c7b9d5
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
repos:

# Empty notebookds
- repo: local
hooks:
- id: jupyter-nb-clear-output
name: jupyter-nb-clear-output
files: tools/.*\.ipynb$
stages: [commit]
language: system
entry: jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace


- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
Expand Down
158 changes: 158 additions & 0 deletions tools/plots.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from anemoi.datasets import open_dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def plot_grid(ds, path, s=0.1, c=\"r\", grids=None, point=None):\n",
" import matplotlib.pyplot as plt\n",
" import cartopy.crs as ccrs\n",
" import numpy as np\n",
"\n",
" lats, lons = ds.latitudes, ds.longitudes\n",
"\n",
" fig = plt.figure(figsize=(9, 9))\n",
" proj = ccrs.NearsidePerspective(\n",
" central_latitude=50.0, central_longitude=-25.0, satellite_height=4e6\n",
" )\n",
"\n",
" ax = plt.axes(projection=proj)\n",
"\n",
" def plot(what, s, c):\n",
" x, y, _ = proj.transform_points(ccrs.PlateCarree(), lons[what], lats[what]).T\n",
"\n",
" mask = np.invert(np.logical_or(np.isinf(x), np.isinf(y)))\n",
" x = np.compress(mask, x)\n",
" y = np.compress(mask, y)\n",
"\n",
" # ax.tricontourf(x, y, values)\n",
" ax.scatter(x, y, s=s, c=c)\n",
"\n",
" if grids:\n",
" a = 0\n",
" for i, b in enumerate(grids):\n",
" if s[i] is not None:\n",
" plot(slice(a, b), s[i], c[i])\n",
" a += b\n",
" else:\n",
" plot(..., s, c)\n",
"\n",
" if point:\n",
" point = np.array(point, dtype=np.float64)\n",
" x, y, _ = proj.transform_points(ccrs.PlateCarree(), point[1], point[0]).T\n",
" ax.scatter(x, y, s=100, c=\"k\")\n",
"\n",
" ax.coastlines()\n",
" if isinstance(path, str):\n",
" fig.savefig(path, bbox_inches=\"tight\")\n",
" else:\n",
" for p in path:\n",
" fig.savefig(p, bbox_inches=\"tight\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ds = open_dataset(\"dataset1.zarr\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plot_grid(ds, [\"thinning-before.png\", \"cutout-1.png\"], s=0.5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ds = open_dataset(ds, thinning=4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plot_grid(ds, \"thinning-after.png\", s=1, c=\"b\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ds = open_dataset(grids=[\"dataset2.zarr\", \"dataset1.zarr\"], mode=\"cutout\")\n",
"\n",
"\n",
"plot_grid(\n",
" ds,\n",
" \"cutout-4.png\",\n",
" s=[0.5, 0.5],\n",
" grids=ds.grids,\n",
" c=[\"g\", \"r\"],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plot_grid(ds, \"cutout-2.png\", s=[0.5, None], grids=ds.grids, c=[\"g\", \"r\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plot_grid(ds, \"cutout-3.png\", s=[None, 0.5], grids=ds.grids, c=[\"g\", \"r\"])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "py311",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 9c7b9d5

Please sign in to comment.