{ "cells": [ { "cell_type": "markdown", "id": "d5a466a9-0be2-48ea-8648-49ad2c97a359", "metadata": {}, "source": [ "# Calculating pairwise distances between a contour and every grid cell\n", "In this notebook, we will demonstrate how to calculate the distance from every grid cell to the nearest grid cell along a contour. We will demonstrate this by computing the distance from each grid cell in a data array to the sea ice edge. We will use monthly sea ice concentration from ACCESS-OM2-01 over the Southern Ocean to perform these calculations. \n", " \n", "*Useful definitions* \n", "**Nearest neighbour**: Refers to the search of the point within a predetermined set of points that is located closest (spatially) to a given point. In order words, what grid cell along the sea ice edge is closest to a grid cell with coordinates `i`, `j`. \n", "**Sea ice edge**: Refers to the northernmost grid cell where sea ice concentration is $10\\%$ or above." ] }, { "cell_type": "markdown", "id": "3df39598-3b8d-4fae-956d-f50399f3aaaf", "metadata": {}, "source": [ "## Loading modules" ] }, { "cell_type": "code", "execution_count": 1, "id": "5d591a48-9911-4c59-a8ee-481d883777cc", "metadata": { "tags": [] }, "outputs": [], "source": [ "import cosima_cookbook as cc\n", "import xarray as xr\n", "import numpy as np\n", "import datetime as dt\n", "from sklearn.neighbors import BallTree\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "id": "f95ceb03-d722-49c3-b4cb-71bd9e0b1304", "metadata": {}, "source": [ "## Creating a session in the COSIMA cookbook" ] }, { "cell_type": "code", "execution_count": 2, "id": "5076a238-0d66-4c10-8f95-b6de4824a47a", "metadata": { "tags": [] }, "outputs": [], "source": [ "session = cc.database.create_session()" ] }, { "cell_type": "markdown", "id": "c45e1335-cd0b-4773-9e8f-4df654d38aa8", "metadata": {}, "source": [ "## Accessing ACCESS-OM2-01 data\n", "We will use monthly sea ice outputs from cycle four. For this example, we will load only two months of sea ice data. Note the use of `decode_coords = False`. This avoids lengthly delays in accessing sea ice data. See more information [here](https://forum.access-hive.org.au/t/issues-loading-access-om2-01-data-from-cycle-4/418/3)." ] }, { "cell_type": "code", "execution_count": 3, "id": "b1de38e7-862a-4974-9a64-0b1046391571", "metadata": { "tags": [] }, "outputs": [], "source": [ "var_ice = cc.querying.getvar('01deg_jra55v140_iaf_cycle4', 'aice_m', session, \n", " start_time='1978-01', end_time='1978-03',\n", " decode_coords=False)" ] }, { "cell_type": "markdown", "id": "ce17902e-7eae-4a71-aa8a-94703a1bb4ad", "metadata": {}, "source": [ "The sea ice outputs need some processing before we can start our calculations. You can check this [example](IcePlottingExample.ipynb) for a guide on how to load and plot sea ice data. \n", " \n", "We will follow these processing steps:\n", "1. Correct time dimension values by subtracting 12 hours,\n", "2. Attach the ocean model grid so we can calculate distances." ] }, { "cell_type": "code", "execution_count": 4, "id": "5f3c2530-9225-4b59-ba62-22f374698b00", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray 'aice_m' (time: 3, yt_ocean: 713, xt_ocean: 3600)>\n", "dask.array<getitem, shape=(3, 713, 3600), dtype=float32, chunksize=(1, 270, 360), chunktype=numpy.ndarray>\n", "Coordinates:\n", " * time (time) datetime64[ns] 1977-12-31T12:00:00 ... 1978-02-28T12:00:00\n", " * xt_ocean (xt_ocean) float64 -279.9 -279.8 -279.7 ... 79.75 79.85 79.95\n", " * yt_ocean (yt_ocean) float64 -79.97 -79.93 -79.88 ... -45.18 -45.11 -45.04\n", "Attributes:\n", " units: 1\n", " long_name: ice area (aggregate)\n", " coordinates: TLON TLAT time\n", " cell_measures: area: tarea\n", " cell_methods: time: mean\n", " time_rep: averaged\n", " ncfiles: ['/g/data/cj50/access-om2/raw-output/access-om2-01/01deg_...\n", " contact: Andrew Kiss\n", " email: andrew.kiss@anu.edu.au\n", " created: 2022-04-27\n", " description: 0.1 degree ACCESS-OM2 global model configuration under in...\n", " notes: Run configuration and history: https://github.com/COSIMA/...
<xarray.DataArray (yt_ocean: 713, xt_ocean: 3600)>\n", "array([[ 805.00144028, 803.19226827, 801.38249999, ..., 810.42536522,\n", " 808.61799012, 806.81001469],\n", " [ 806.69292563, 804.88000362, 803.06648432, ..., 812.12809432,\n", " 810.31697234, 808.50524899],\n", " [ 808.40803346, 806.59143086, 804.77423025, ..., 813.85423949,\n", " 812.03943914, 810.22403667],\n", " ...,\n", " [1607.76336769, 1606.55053756, 1605.36467069, ..., 1611.56299844,\n", " 1610.26966326, 1609.00309777],\n", " [1615.51041181, 1614.30178279, 1613.12003095, ..., 1619.29692977,\n", " 1618.0080497 , 1616.74585538],\n", " [1623.26793377, 1622.06347241, 1620.88580274, ..., 1627.04144284,\n", " 1625.75698268, 1624.4991249 ]])\n", "Coordinates:\n", " * yt_ocean (yt_ocean) float64 -79.97 -79.93 -79.88 ... -45.18 -45.11 -45.04\n", " * xt_ocean (xt_ocean) float64 -279.9 -279.8 -279.7 ... 79.75 79.85 79.95
<xarray.DataArray (time: 2, yt_ocean: 713, xt_ocean: 3600)>\n", "dask.array<concatenate, shape=(2, 713, 3600), dtype=float64, chunksize=(1, 270, 360), chunktype=numpy.ndarray>\n", "Coordinates:\n", " * yt_ocean (yt_ocean) float64 -79.97 -79.93 -79.88 ... -45.18 -45.11 -45.04\n", " * xt_ocean (xt_ocean) float64 -279.9 -279.8 -279.7 ... 79.75 79.85 79.95\n", " * time (time) datetime64[ns] 1977-12-31T12:00:00 1978-01-31T12:00:00