{ "cells": [ { "cell_type": "markdown", "id": "a0fb83b4", "metadata": {}, "source": [ "# How to use a xarray Dataset as NadirData source" ] }, { "cell_type": "markdown", "id": "59d2a216", "metadata": {}, "source": [ "NadirData can be created using anything than can be opened as a xarray dataset and compute statistics on it in the exact same manner.\n", "It allows to dynamically create a dataset by using as many inputs as you want, align their coordinates if needed, arrange them however you want and use it has your data source [[doc](../ad_source.rst#xarray-datasets)].\n" ] }, { "cell_type": "markdown", "id": "2d68f14d", "metadata": {}, "source": [ "## Example: JASON 3 - 20hz netCDF" ] }, { "cell_type": "code", "id": "06c782ff-b964-409a-b774-09f832bfe5c6", "metadata": {}, "source": [ "import xarray as xr\n", "\n", "from casys import NadirData, CasysPlot, Field, PlotParams\n", "\n", "NadirData.enable_loginfo()" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "id": "f9c51515", "metadata": {}, "source": [ "### Create your source xarray dataset" ] }, { "cell_type": "code", "id": "8d343556", "metadata": {}, "source": [ "# Open with xarray the product files of Jason-3 (Track 1 to 9 of cycle 127)\n", "ds = xr.open_mfdataset(\n", " \"/data/OCTANT_NG/cvi/JA3/JA3_GPN_2PdP127_00*.nc\", combine=\"by_coords\"\n", ")" ], "outputs": [], "execution_count": null }, { "cell_type": "code", "id": "39d23a27", "metadata": {}, "source": [ "ds[\"alt_echo_type\"]" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "id": "7e09f0ca", "metadata": {}, "source": [ "### Create your NadirData" ] }, { "cell_type": "code", "id": "18cba4e5", "metadata": {}, "source": [ "# Create your NadirData object\n", "\n", "selection = \"is_bounded(-40, deg_normalize(-90, lat_20hz), 60) && is_bounded(-50, deg_normalize(-180, lon_20hz), 150)\"\n", "\n", "cd_ncdf = NadirData(\n", " source=ds,\n", " select_clip=selection,\n", " time=\"time_20hz\",\n", " longitude=\"lon_20hz\",\n", " latitude=\"lat_20hz\",\n", ")" ], "outputs": [], "execution_count": null }, { "cell_type": "code", "id": "3d4919d2", "metadata": {}, "source": [ "cd_ncdf.show_fields(containing=\"swh_20hz\")" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "id": "d9d4c483", "metadata": {}, "source": [ "### Play with it !" ] }, { "cell_type": "markdown", "id": "1b1e1095", "metadata": {}, "source": [ "Add fields created with clips:" ] }, { "cell_type": "code", "id": "1f21a005", "metadata": {}, "source": [ "f1 = Field(name=\"f1\", source=\"swh_20hz_ku * swh_20hz_c + 3\")\n", "\n", "cd_ncdf.add_raw_data(name=\"F1\", field=f1)\n", "\n", "cd_ncdf.compute()" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "id": "36644ee2", "metadata": {}, "source": [ "Insert new customized fields:" ] }, { "cell_type": "code", "id": "85285ff0", "metadata": {}, "source": [ "dsx = cd_ncdf.data\n", "\n", "flag = xr.where(dsx[\"lat_20hz\"] > 0, 1, 0)\n", "dsx[\"f2\"] = flag\n", "\n", "cd_ncdf.data = dsx" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "id": "663633b6", "metadata": {}, "source": [ "Use these new fields:" ] }, { "cell_type": "code", "id": "5f3a2446", "metadata": {}, "source": [ "f3 = Field(\"f3\", source=\"IIF(f2:==1, f1, DV)\")\n", "\n", "cd_ncdf.add_raw_data(name=\"F3\", field=f3)\n", "\n", "cd_ncdf.add_time_stat(\n", " name=\"F1 stat\", field=f1, freq=\"15min\", stats=[\"mean\", \"std\", \"count\"]\n", ")\n", "cd_ncdf.add_time_stat(\n", " name=\"F2 stat\", field=Field(\"f2\"), freq=\"15min\", stats=[\"mean\", \"std\", \"count\"]\n", ")\n", "\n", "cd_ncdf.compute()" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "id": "01b158b5", "metadata": {}, "source": [ "Visualize everything:" ] }, { "cell_type": "code", "id": "fa29084a", "metadata": {}, "source": [ "cp1 = CasysPlot(\n", " data=cd_ncdf, data_name=\"F3\", plot_params=PlotParams(grid=True), plot=\"map\"\n", ")\n", "\n", "cp1.show()" ], "outputs": [], "execution_count": null }, { "cell_type": "code", "id": "612de0a2", "metadata": {}, "source": [ "cp2 = CasysPlot(\n", " data=cd_ncdf, data_name=\"F2 stat\", stat=\"mean\", plot_params=PlotParams(grid=True)\n", ")\n", "cp2.show()" ], "outputs": [], "execution_count": null } ], "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 }