{ "cells": [ { "cell_type": "markdown", "id": "5336254d-8bd3-400a-9dc8-93eca08b8208", "metadata": {}, "source": [ "# Details of intersection of radar grid with CML paths" ] }, { "cell_type": "markdown", "id": "da148d1a-aa2c-4556-82ca-4d731eaf1fab", "metadata": {}, "source": [ "This notebook shows the details of how the intersection of a grid and lines is done. The typicall use case is doing this for gridded radar rainfall data and CML paths, but it would work the same for gridded satellite data and SMLs." ] }, { "cell_type": "code", "execution_count": 1, "id": "49388feb", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import xarray as xr\n", "\n", "import poligrain as plg" ] }, { "cell_type": "markdown", "id": "415dca87", "metadata": {}, "source": [ "### Load example data" ] }, { "cell_type": "code", "execution_count": 2, "id": "9eaab2d5", "metadata": {}, "outputs": [], "source": [ "ds_cmls = xr.open_dataset(\"../../tests/test_data/openMRG_CML_180minutes.nc\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "08efe99f", "metadata": {}, "outputs": [], "source": [ "ds_radar = xr.open_dataset(\"../../tests/test_data/openMRG_Radar_180minutes.nc\")" ] }, { "cell_type": "markdown", "id": "3ddb93dd", "metadata": {}, "source": [ "## Calculate intersection weights\n", "\n", "Note that the intersection weights are stored as `sparse.arrays` in a `xarray.DataArray` because the matrix of intersection weights for each CMLs, based on the radar grid, contains mostly zeros. Hence, we can save a lot of space. For large CML networks this is crucial because storing thousands of intersection weight matrices, one for each CML, easily eats up 10s of GBs of memory.\n", "\n", "Note that this calculation is fairly fast, i.e. approx. 2 seconds for 500 CMLs." ] }, { "cell_type": "code", "execution_count": 4, "id": "d89aefb7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 585 ms, sys: 10.4 ms, total: 595 ms\n", "Wall time: 609 ms\n" ] } ], "source": [ "%%time\n", "da_intersect_weights = plg.spatial.calc_sparse_intersect_weights_for_several_cmls(\n", " x1_line=ds_cmls.site_0_lon.values,\n", " y1_line=ds_cmls.site_0_lat.values,\n", " x2_line=ds_cmls.site_1_lon.values,\n", " y2_line=ds_cmls.site_1_lat.values,\n", " cml_id=ds_cmls.cml_id.values,\n", " x_grid=ds_radar.lon.values,\n", " y_grid=ds_radar.lat.values,\n", " grid_point_location=\"center\",\n", ")" ] }, { "cell_type": "code", "execution_count": 5, "id": "ac984c71", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray (y: 48, x: 37)>\n",
"<COO: shape=(48, 37), dtype=float64, nnz=1, fill_value=0.0>\n",
"Coordinates:\n",
" x_grid (y, x) float64 11.41 11.45 11.48 11.51 ... 12.56 12.59 12.62 12.66\n",
" y_grid (y, x) float64 58.04 58.04 58.04 58.04 ... 57.23 57.23 57.23 57.23\n",
" cml_id int64 10001\n",
"Dimensions without coordinates: y, x<xarray.DataArray (time: 36, cml_id: 364)>\n",
"array([[5.45577289e-02, 1.29376820e-01, 5.15058173e-02, ...,\n",
" 4.86375435e-04, 2.60098642e-01, 7.85087184e-02],\n",
" [4.86246270e-02, 4.86246443e-04, 4.86246236e-04, ...,\n",
" 2.68530211e-02, 4.86150392e-04, 4.86179854e-04],\n",
" [4.86246236e-04, 4.86246443e-04, 4.86246236e-04, ...,\n",
" 4.86375435e-04, 4.86150392e-04, 4.86179854e-04],\n",
" ...,\n",
" [1.22139538e-02, 4.86246443e-04, 1.15307163e-01, ...,\n",
" 4.86375435e-04, 5.55353035e-02, 5.13666248e-02],\n",
" [2.17198235e-01, 3.44236153e-01, 6.86841139e-01, ...,\n",
" 4.86375435e-04, 7.50213222e-02, 2.72055320e-01],\n",
" [1.08856978e-02, 3.06800794e-01, 3.06800653e-02, ...,\n",
" 3.92748636e-02, 1.30498263e-01, 5.51433563e-02]])\n",
"Coordinates:\n",
" * time (time) datetime64[ns] 2015-08-27T00:20:00 ... 2015-08-27T03:15:00\n",
" * cml_id (cml_id) int64 10001 10002 10003 10004 ... 10361 10362 10363 10364