{ "cells": [ { "cell_type": "markdown", "id": "e08fa6bf-d683-4dc7-885e-62fd22d3ad1c", "metadata": {}, "source": [ "# How to add custom points to a CasysPlot" ] }, { "cell_type": "markdown", "id": "714a5967-7114-4aa4-849c-6b2ef8f3bc7c", "metadata": {}, "source": [ "CasysPlot objects can be created both from Casys sources (data container like NadirData and SwathData) or from xarray datasets. \n", "Plots can be merged with the method add_plot of CasysPlot objects, allowing to display customi points on top of classic CasysPlot. \n", "This notebook will show some examples." ] }, { "cell_type": "code", "execution_count": null, "id": "e74b1f0c-9c28-42cb-a707-e10c9d19cb34", "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "import numpy as np\n", "import xarray as xr\n", "\n", "from casys import CasysPlot, NadirData, PlotParams\n", "\n", "NadirData.enable_loginfo()" ] }, { "cell_type": "markdown", "id": "3447fcd4-cc77-43cb-93e7-fefadd5abcda", "metadata": {}, "source": [ "## Creating CasysPlot objects from data container" ] }, { "cell_type": "code", "execution_count": null, "id": "48d74532-a9a6-4789-9a37-b40b9a4febde", "metadata": {}, "outputs": [], "source": [ "# Using the model's computed NadirData object.\n", "ad = NadirData.load(os.environ[\"DOC_MODEL\"])\n", "ad.add_raw_data(name=\"SIGMA0\", field=ad.fields[\"SIGMA0.ALTI\"])\n", "ad.add_geobox_stat(name=\"SIGMA0 geobox\", field=ad.fields[\"SIGMA0.ALTI\"])\n", "ad.compute()" ] }, { "cell_type": "code", "execution_count": null, "id": "14d3a42e-e03e-476d-a97c-7ea8270c56a2", "metadata": {}, "outputs": [], "source": [ "plot_casys_raw = CasysPlot(data=ad, data_name=\"SIGMA0\")\n", "plot_casys_raw.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "190701aa-4eb1-4d65-a532-e005c6b73946", "metadata": {}, "outputs": [], "source": [ "plot_casys_geobox = CasysPlot(data=ad, data_name=\"SIGMA0 geobox\")\n", "plot_casys_geobox.showi()" ] }, { "cell_type": "markdown", "id": "23f2e8fb-7541-4f73-9b04-0e92c09857b5", "metadata": {}, "source": [ "## Creating CasysPlot objects with custom points" ] }, { "cell_type": "markdown", "id": "6586c6bb-15be-4c03-9eed-36652f8f927f", "metadata": {}, "source": [ "Taking a custom set of points:" ] }, { "cell_type": "code", "execution_count": null, "id": "b4f44e96-8f4c-4a17-8d62-ae12af1b9c56", "metadata": {}, "outputs": [], "source": [ "values = np.random.rand(10) * 20 + 15\n", "time = np.arange(\n", " np.datetime64(\"2019-06-03\"),\n", " np.datetime64(\"2019-06-08\"),\n", " np.timedelta64(12, \"h\").astype(np.timedelta64(1, \"ns\")),\n", ")\n", "lon = np.random.randint(low=-180, high=180, size=10)\n", "lat = np.random.randint(low=-90, high=90, size=10)\n", "\n", "ds_custom_points = xr.Dataset(\n", " data_vars={\n", " \"CUSTOM_POINTS_VALUES\": (\"time\", values),\n", " \"LONGITUDE\": (\"time\", lon),\n", " \"LATITUDE\": (\"time\", lat),\n", " },\n", " coords={\"time\": time},\n", ")" ] }, { "cell_type": "markdown", "id": "259e2f60-a217-4523-b989-b6be49e1b2bb", "metadata": {}, "source": [ "A CasysPlot object can be created using the ``from_array`` method. \n", "The ``dtype`` parameter indicates the type of plot. Available dtypes and associated usage are detailed [here](../cp_main.rst#from-a-xarray-dataset)." ] }, { "cell_type": "markdown", "id": "a6d3ff62-c909-4b64-9bc0-428e55154574", "metadata": {}, "source": [ "For an along time plot of custom data, use ``dtype=\"STAT_TIME\"``:" ] }, { "cell_type": "code", "execution_count": null, "id": "8133416f-8e75-4ece-bb98-94eed7437751", "metadata": {}, "outputs": [], "source": [ "plot_custom_time = CasysPlot.from_array(\n", " name=\"Custom Points\",\n", " data=ds_custom_points,\n", " x=\"time\",\n", " y=\"CUSTOM_POINTS_VALUES\",\n", " dtype=\"STAT_TIME\",\n", " plot_params=PlotParams(\n", " marker_style=\"+\",\n", " marker_size=10,\n", " ),\n", ")\n", "plot_custom_time.showi()" ] }, { "cell_type": "markdown", "id": "028d885c-5733-4482-b8c7-a993ddca2eb8", "metadata": {}, "source": [ "For a map plot of the custom data, use ``dtype=\"RAW_DATA\"``:" ] }, { "cell_type": "code", "execution_count": null, "id": "32977f31-a831-439f-81c5-7027139f80f0", "metadata": {}, "outputs": [], "source": [ "plot_custom_map = CasysPlot.from_array(\n", " name=\"Custom Points\",\n", " data=ds_custom_points,\n", " x=\"LONGITUDE\",\n", " y=\"LATITUDE\",\n", " z=\"CUSTOM_POINTS_VALUES\",\n", " time=\"time\",\n", " dtype=\"RAW_DATA\",\n", " plot_params=PlotParams(\n", " marker_style=\"+\",\n", " marker_size=100,\n", " color_map=\"magma\",\n", " ),\n", ")\n", "plot_custom_map.showi()" ] }, { "cell_type": "markdown", "id": "5139edc4-332e-43a1-b547-d4ae55f3107f", "metadata": {}, "source": [ "## Merging CasysPlot objects" ] }, { "cell_type": "markdown", "id": "a79796e4-03f4-4a09-b007-8e2244c19497", "metadata": {}, "source": [ "Using the ``add_plot`` method of the previously created CasysPlot objects, custom points will be added on the CasysPlot objects created from a data container:" ] }, { "cell_type": "code", "execution_count": null, "id": "f84377ab-d312-405a-a5c4-c1f685162d4d", "metadata": {}, "outputs": [], "source": [ "plot_casys_raw.add_plot(plot_custom_time)\n", "plot_casys_raw.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "90ed259f-d4fe-4cd9-9b67-9177b8ecf009", "metadata": {}, "outputs": [], "source": [ "plot_casys_geobox.add_plot(plot_custom_map)\n", "plot_casys_geobox.show()" ] }, { "cell_type": "markdown", "id": "cde781b2-f27d-4c15-8929-e5d21472c82d", "metadata": {}, "source": [ "For more details on merging plots see [plots merging documentation](../cp_operations.rst#merging-plots)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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": 5 }