Skip to content

Commit

Permalink
notebooks: Add notebooks/API.ipynb + fix some minor issues in BBox_Fi…
Browse files Browse the repository at this point in the history
…ltering.
  • Loading branch information
pmav99 committed Mar 22, 2023
1 parent 83e399e commit 5677aee
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 43 deletions.
169 changes: 169 additions & 0 deletions notebooks/API.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "f3e4d71b-357e-45e2-aa3e-9e89b1b662ab",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2\n",
"\n",
"from __future__ import annotations"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15c46224-c0c6-4c7c-8c4b-fd1a0580398a",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ['USE_PYGEOS'] = '0' # remove geopandas warnings\n",
"\n",
"import holoviews as hv\n",
"import numpy as np\n",
"import numpy_indexed as npi\n",
"import pandas as pd\n",
"import xarray as xr\n",
"\n",
"hv.extension(\"bokeh\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1779d199-055f-47f6-a296-f823cec4329c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"%%bash\n",
"pushd /tmp\n",
"wget --quiet https://github.com/ec-jrc/Thalassa/files/10867068/fort.63.zip \n",
"unzip -o fort.63.zip\n",
"popd"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "73965237-0675-49d1-ae01-e45a67bd77d4",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from thalassa import api\n",
"\n",
"filename = \"/tmp/fort.63.nc\"\n",
"ds = api.open_dataset(filename)\n",
"ds"
]
},
{
"cell_type": "markdown",
"id": "d2957b88-7e35-47c4-be69-fc510b809572",
"metadata": {
"tags": []
},
"source": [
"## Main API\n",
"\n",
"`thalassa` supports several types of graphs, including:\n",
" \n",
"- A visualization of the variables at various timestamps and or layers.\n",
"- A visualization of the mesh\n",
"- Extraction of timeseries from specific points\n",
"\n",
"The following cell shows the basic usage:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f63f4df8-7160-47cd-b40b-322751b60e75",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"#variable, layer, timestamp = \"salt\", 40, ds.time.values[4]\n",
"#variable, layer, timestamp = \"depth\", None, None\n",
"variable, layer, timestamp = \"zeta\", None, \"max\"\n",
"\n",
"# The trimesh is the most basic object. This is what you need to create all the others graphs\n",
"# It is on this object that you specify the timestamp and/or the layer.\n",
"trimesh = api.create_trimesh(ds=ds, variable=variable, timestamp=timestamp, layer=layer)\n",
"\n",
"# The wireframe is the representation of the mesh\n",
"wireframe = api.get_wireframe(trimesh=trimesh)\n",
"\n",
"# The tiles is using the tiling service from Open Street maps\n",
"tiles = api.get_tiles() \n",
"\n",
"# The raster object is the basic Map that visualizes the variable. \n",
"# You can specify things like the colorbar limits and/or the extents\n",
"#raster = api.get_raster(trimesh, clim_min=0, clim_max=15)\n",
"raster = api.get_raster(trimesh)\n",
"\n",
"# The pointer/tap timeseries extract the timeseries of a specific node from the xr.Dataset and visualize it.\n",
"pointer_dmap = api.get_pointer_timeseries(ds=ds, variable=variable, source_raster=raster, layer=layer)\n",
"tap_dmap = api.get_tap_timeseries(ds=ds, variable=variable, source_raster=raster, layer=layer)"
]
},
{
"cell_type": "markdown",
"id": "c1d58fec-9a9a-45c5-b408-780b9bdd01ed",
"metadata": {},
"source": [
"### Let's visualize!\n",
"\n",
"After you render the layout, move the mouse over the map and click on it. This will fill in the `pointer_dmap` and the `tap_dmap`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f42fc4d5-37ff-4195-8cb2-03986c2bcfaa",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"layout = tiles * raster.opts(width=600, cmap=\"viridis\") \n",
"layout\n",
"(pointer_dmap + tap_dmap).opts(shared_axes=False)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "thalassa2",
"language": "python",
"name": "thalassa2"
},
"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.8.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
81 changes: 38 additions & 43 deletions notebooks/BBox_filtering.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "67f71151-7407-4c23-a9a7-742969973bc9",
"id": "871b1568-5742-46dd-9072-8e3785f8475c",
"metadata": {
"tags": []
},
Expand All @@ -12,19 +12,7 @@
"%load_ext autoreload\n",
"%autoreload 2\n",
"\n",
"from __future__ import annotations\n",
"\n",
"import os\n",
"\n",
"os.environ['USE_PYGEOS'] = '0' # remove geopandas warnings\n",
"\n",
"import holoviews as hv\n",
"import numpy as np\n",
"import numpy_indexed as npi\n",
"import pandas as pd\n",
"import xarray as xr\n",
"\n",
"hv.extension(\"bokeh\")"
"from __future__ import annotations"
]
},
{
Expand Down Expand Up @@ -59,8 +47,29 @@
"try:\n",
" import numpy_indexed\n",
"except ImportError:\n",
" %pip install numpy_indexed\n",
" "
" %pip install numpy_indexed"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "95c5af35-560d-4222-8e8d-6f680a9d24b3",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ['USE_PYGEOS'] = '0' # remove geopandas warnings\n",
"\n",
"import holoviews as hv\n",
"import numpy as np\n",
"import numpy_indexed as npi\n",
"import pandas as pd\n",
"import xarray as xr\n",
"\n",
"hv.extension(\"bokeh\")"
]
},
{
Expand Down Expand Up @@ -138,6 +147,8 @@
"tags": []
},
"source": [
"### Filtering \n",
"\n",
"Now let's define the function that does the filtering by BBox"
]
},
Expand All @@ -152,10 +163,10 @@
"source": [
"def filter_thalassa_by_bbox(\n",
" ds: xr.Dataset,\n",
" lon_min: float,\n",
" lon_max: float,\n",
" lat_min: float,\n",
" lat_max: float\n",
" lon_min: float = -180,\n",
" lon_max: float = 180,\n",
" lat_min: float = -90,\n",
" lat_max: float = 90,\n",
") -> xr.Dataset:\n",
" indices_of_nodes_in_bbox = np.where(\n",
" True \n",
Expand Down Expand Up @@ -221,35 +232,26 @@
"tags": []
},
"source": [
"### Visual diff\n",
"Let's use `thalassa` to visualize the difference:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "53593673-6c37-4579-b1a2-e97460c97e4f",
"id": "4f810fb2-140f-44b2-999d-16deab9c07ca",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# The full dataset\n",
"trimesh = api.create_trimesh(\n",
" ds=ds,\n",
" variable=\"zeta\",\n",
" timestamp=\"max\",\n",
" layer=None,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2fd3776-581f-4788-add7-68f24d51b8fa",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
")\n",
"dmap = api.get_tiles() * api.get_raster(trimesh=trimesh) #* api.get_wireframe(trimesh=trimesh)\n",
"dmap.opts(width=600, title=\"Full dataset\")"
]
Expand All @@ -263,14 +265,15 @@
},
"outputs": [],
"source": [
"# The filtered dataset\n",
"ftrimesh = api.create_trimesh(\n",
" ds=fds,\n",
" variable=\"zeta\",\n",
" timestamp=\"max\",\n",
" layer=None,\n",
")\n",
"dmap = api.get_tiles() * api.get_raster(trimesh=ftrimesh) * api.get_wireframe(trimesh=ftrimesh)\n",
"dmap.opts(width=600, title=\"Filtered Dataset\")"
"fdmap = api.get_tiles() * api.get_raster(trimesh=ftrimesh) * api.get_wireframe(trimesh=ftrimesh)\n",
"fdmap.opts(width=600, title=\"Filtered Dataset\")"
]
},
{
Expand All @@ -287,14 +290,6 @@
"1. it supports different Dataset schemas (e.g. vanilla ADCIRC, vanilla SCHISM) but this is left as an exercise to the reader.\n",
"2. it filters using a vector object (e.g. a shapefile) instead of a simple BBox."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e7196c2b-1052-4e1b-9a23-f6e44d768c5f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 5677aee

Please sign in to comment.