Skip to content

Instantly share code, notes, and snippets.

@willirath
Created September 6, 2024 12:29
Show Gist options
  • Save willirath/a458bb6d36ee8ed6794e691702207a74 to your computer and use it in GitHub Desktop.
Save willirath/a458bb6d36ee8ed6794e691702207a74 to your computer and use it in GitHub Desktop.
This file has been truncated, but you can view the full file.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "04e421ee-39ff-4987-ad7b-7d65793fd042",
"metadata": {},
"outputs": [],
"source": [
"import xarray as xr\n",
"import numpy as np\n",
"from pathlib import Path\n",
"from matplotlib import pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b7b93d4e-46cb-44ab-8b1a-c3827583968d",
"metadata": {},
"outputs": [],
"source": [
"from itertools import zip_longest\n",
"from functools import reduce\n",
"from operator import add"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "63647bd0-b37f-401e-84e6-4af4298e532e",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div><svg style=\"position: absolute; width: 0; height: 0; overflow: hidden\">\n",
"<defs>\n",
"<symbol id=\"icon-database\" viewBox=\"0 0 32 32\">\n",
"<path d=\"M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z\"></path>\n",
"<path d=\"M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z\"></path>\n",
"<path d=\"M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z\"></path>\n",
"</symbol>\n",
"<symbol id=\"icon-file-text2\" viewBox=\"0 0 32 32\">\n",
"<path d=\"M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z\"></path>\n",
"<path d=\"M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z\"></path>\n",
"<path d=\"M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z\"></path>\n",
"<path d=\"M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z\"></path>\n",
"</symbol>\n",
"</defs>\n",
"</svg>\n",
"<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.\n",
" *\n",
" */\n",
"\n",
":root {\n",
" --xr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));\n",
" --xr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));\n",
" --xr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));\n",
" --xr-border-color: var(--jp-border-color2, #e0e0e0);\n",
" --xr-disabled-color: var(--jp-layout-color3, #bdbdbd);\n",
" --xr-background-color: var(--jp-layout-color0, white);\n",
" --xr-background-color-row-even: var(--jp-layout-color1, white);\n",
" --xr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);\n",
"}\n",
"\n",
"html[theme=dark],\n",
"html[data-theme=dark],\n",
"body[data-theme=dark],\n",
"body.vscode-dark {\n",
" --xr-font-color0: rgba(255, 255, 255, 1);\n",
" --xr-font-color2: rgba(255, 255, 255, 0.54);\n",
" --xr-font-color3: rgba(255, 255, 255, 0.38);\n",
" --xr-border-color: #1F1F1F;\n",
" --xr-disabled-color: #515151;\n",
" --xr-background-color: #111111;\n",
" --xr-background-color-row-even: #111111;\n",
" --xr-background-color-row-odd: #313131;\n",
"}\n",
"\n",
".xr-wrap {\n",
" display: block !important;\n",
" min-width: 300px;\n",
" max-width: 700px;\n",
"}\n",
"\n",
".xr-text-repr-fallback {\n",
" /* fallback to plain text repr when CSS is not injected (untrusted notebook) */\n",
" display: none;\n",
"}\n",
"\n",
".xr-header {\n",
" padding-top: 6px;\n",
" padding-bottom: 6px;\n",
" margin-bottom: 4px;\n",
" border-bottom: solid 1px var(--xr-border-color);\n",
"}\n",
"\n",
".xr-header > div,\n",
".xr-header > ul {\n",
" display: inline;\n",
" margin-top: 0;\n",
" margin-bottom: 0;\n",
"}\n",
"\n",
".xr-obj-type,\n",
".xr-array-name {\n",
" margin-left: 2px;\n",
" margin-right: 10px;\n",
"}\n",
"\n",
".xr-obj-type {\n",
" color: var(--xr-font-color2);\n",
"}\n",
"\n",
".xr-sections {\n",
" padding-left: 0 !important;\n",
" display: grid;\n",
" grid-template-columns: 150px auto auto 1fr 20px 20px;\n",
"}\n",
"\n",
".xr-section-item {\n",
" display: contents;\n",
"}\n",
"\n",
".xr-section-item input {\n",
" display: none;\n",
"}\n",
"\n",
".xr-section-item input + label {\n",
" color: var(--xr-disabled-color);\n",
"}\n",
"\n",
".xr-section-item input:enabled + label {\n",
" cursor: pointer;\n",
" color: var(--xr-font-color2);\n",
"}\n",
"\n",
".xr-section-item input:enabled + label:hover {\n",
" color: var(--xr-font-color0);\n",
"}\n",
"\n",
".xr-section-summary {\n",
" grid-column: 1;\n",
" color: var(--xr-font-color2);\n",
" font-weight: 500;\n",
"}\n",
"\n",
".xr-section-summary > span {\n",
" display: inline-block;\n",
" padding-left: 0.5em;\n",
"}\n",
"\n",
".xr-section-summary-in:disabled + label {\n",
" color: var(--xr-font-color2);\n",
"}\n",
"\n",
".xr-section-summary-in + label:before {\n",
" display: inline-block;\n",
" content: '►';\n",
" font-size: 11px;\n",
" width: 15px;\n",
" text-align: center;\n",
"}\n",
"\n",
".xr-section-summary-in:disabled + label:before {\n",
" color: var(--xr-disabled-color);\n",
"}\n",
"\n",
".xr-section-summary-in:checked + label:before {\n",
" content: '▼';\n",
"}\n",
"\n",
".xr-section-summary-in:checked + label > span {\n",
" display: none;\n",
"}\n",
"\n",
".xr-section-summary,\n",
".xr-section-inline-details {\n",
" padding-top: 4px;\n",
" padding-bottom: 4px;\n",
"}\n",
"\n",
".xr-section-inline-details {\n",
" grid-column: 2 / -1;\n",
"}\n",
"\n",
".xr-section-details {\n",
" display: none;\n",
" grid-column: 1 / -1;\n",
" margin-bottom: 5px;\n",
"}\n",
"\n",
".xr-section-summary-in:checked ~ .xr-section-details {\n",
" display: contents;\n",
"}\n",
"\n",
".xr-array-wrap {\n",
" grid-column: 1 / -1;\n",
" display: grid;\n",
" grid-template-columns: 20px auto;\n",
"}\n",
"\n",
".xr-array-wrap > label {\n",
" grid-column: 1;\n",
" vertical-align: top;\n",
"}\n",
"\n",
".xr-preview {\n",
" color: var(--xr-font-color3);\n",
"}\n",
"\n",
".xr-array-preview,\n",
".xr-array-data {\n",
" padding: 0 5px !important;\n",
" grid-column: 2;\n",
"}\n",
"\n",
".xr-array-data,\n",
".xr-array-in:checked ~ .xr-array-preview {\n",
" display: none;\n",
"}\n",
"\n",
".xr-array-in:checked ~ .xr-array-data,\n",
".xr-array-preview {\n",
" display: inline-block;\n",
"}\n",
"\n",
".xr-dim-list {\n",
" display: inline-block !important;\n",
" list-style: none;\n",
" padding: 0 !important;\n",
" margin: 0;\n",
"}\n",
"\n",
".xr-dim-list li {\n",
" display: inline-block;\n",
" padding: 0;\n",
" margin: 0;\n",
"}\n",
"\n",
".xr-dim-list:before {\n",
" content: '(';\n",
"}\n",
"\n",
".xr-dim-list:after {\n",
" content: ')';\n",
"}\n",
"\n",
".xr-dim-list li:not(:last-child):after {\n",
" content: ',';\n",
" padding-right: 5px;\n",
"}\n",
"\n",
".xr-has-index {\n",
" font-weight: bold;\n",
"}\n",
"\n",
".xr-var-list,\n",
".xr-var-item {\n",
" display: contents;\n",
"}\n",
"\n",
".xr-var-item > div,\n",
".xr-var-item label,\n",
".xr-var-item > .xr-var-name span {\n",
" background-color: var(--xr-background-color-row-even);\n",
" margin-bottom: 0;\n",
"}\n",
"\n",
".xr-var-item > .xr-var-name:hover span {\n",
" padding-right: 5px;\n",
"}\n",
"\n",
".xr-var-list > li:nth-child(odd) > div,\n",
".xr-var-list > li:nth-child(odd) > label,\n",
".xr-var-list > li:nth-child(odd) > .xr-var-name span {\n",
" background-color: var(--xr-background-color-row-odd);\n",
"}\n",
"\n",
".xr-var-name {\n",
" grid-column: 1;\n",
"}\n",
"\n",
".xr-var-dims {\n",
" grid-column: 2;\n",
"}\n",
"\n",
".xr-var-dtype {\n",
" grid-column: 3;\n",
" text-align: right;\n",
" color: var(--xr-font-color2);\n",
"}\n",
"\n",
".xr-var-preview {\n",
" grid-column: 4;\n",
"}\n",
"\n",
".xr-index-preview {\n",
" grid-column: 2 / 5;\n",
" color: var(--xr-font-color2);\n",
"}\n",
"\n",
".xr-var-name,\n",
".xr-var-dims,\n",
".xr-var-dtype,\n",
".xr-preview,\n",
".xr-attrs dt {\n",
" white-space: nowrap;\n",
" overflow: hidden;\n",
" text-overflow: ellipsis;\n",
" padding-right: 10px;\n",
"}\n",
"\n",
".xr-var-name:hover,\n",
".xr-var-dims:hover,\n",
".xr-var-dtype:hover,\n",
".xr-attrs dt:hover {\n",
" overflow: visible;\n",
" width: auto;\n",
" z-index: 1;\n",
"}\n",
"\n",
".xr-var-attrs,\n",
".xr-var-data,\n",
".xr-index-data {\n",
" display: none;\n",
" background-color: var(--xr-background-color) !important;\n",
" padding-bottom: 5px !important;\n",
"}\n",
"\n",
".xr-var-attrs-in:checked ~ .xr-var-attrs,\n",
".xr-var-data-in:checked ~ .xr-var-data,\n",
".xr-index-data-in:checked ~ .xr-index-data {\n",
" display: block;\n",
"}\n",
"\n",
".xr-var-data > table {\n",
" float: right;\n",
"}\n",
"\n",
".xr-var-name span,\n",
".xr-var-data,\n",
".xr-index-name div,\n",
".xr-index-data,\n",
".xr-attrs {\n",
" padding-left: 25px !important;\n",
"}\n",
"\n",
".xr-attrs,\n",
".xr-var-attrs,\n",
".xr-var-data,\n",
".xr-index-data {\n",
" grid-column: 1 / -1;\n",
"}\n",
"\n",
"dl.xr-attrs {\n",
" padding: 0;\n",
" margin: 0;\n",
" display: grid;\n",
" grid-template-columns: 125px auto;\n",
"}\n",
"\n",
".xr-attrs dt,\n",
".xr-attrs dd {\n",
" padding: 0;\n",
" margin: 0;\n",
" float: left;\n",
" padding-right: 10px;\n",
" width: auto;\n",
"}\n",
"\n",
".xr-attrs dt {\n",
" font-weight: normal;\n",
" grid-column: 1;\n",
"}\n",
"\n",
".xr-attrs dt:hover span {\n",
" display: inline-block;\n",
" background: var(--xr-background-color);\n",
" padding-right: 10px;\n",
"}\n",
"\n",
".xr-attrs dd {\n",
" grid-column: 2;\n",
" white-space: pre-wrap;\n",
" word-break: break-all;\n",
"}\n",
"\n",
".xr-icon-database,\n",
".xr-icon-file-text2,\n",
".xr-no-icon {\n",
" display: inline-block;\n",
" vertical-align: middle;\n",
" width: 1em;\n",
" height: 1.5em !important;\n",
" stroke-width: 0;\n",
" stroke: currentColor;\n",
" fill: currentColor;\n",
"}\n",
"</style><pre class='xr-text-repr-fallback'>&lt;xarray.Dataset&gt; Size: 2GB\n",
"Dimensions: (traj: 100000, obs: 673)\n",
"Coordinates:\n",
" offset int64 8B 0\n",
"Dimensions without coordinates: traj, obs\n",
"Data variables:\n",
" land (traj, obs) float32 269MB 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0\n",
" lat (traj, obs) float32 269MB 61.29 61.28 61.29 ... 56.53 56.54\n",
" lon (traj, obs) float32 269MB -14.75 -14.75 ... -11.88 -11.89\n",
" salinity (traj, obs) float32 269MB 35.13 35.13 35.13 ... 35.3 35.3 35.3\n",
" temperature (traj, obs) float32 269MB 8.616 8.614 8.608 ... 12.1 12.1 12.09\n",
" time (traj, obs) datetime64[ns] 538MB 2019-05-15 ... 2019-06-12\n",
"Attributes:\n",
" Conventions: CF-1.6/CF-1.7\n",
" feature_type: trajectory\n",
" ncei_template_version: NCEI_NetCDF_Trajectory_Template_v2.0\n",
" parcels_mesh: spherical\n",
" parcels_version: 2.3.1</pre><div class='xr-wrap' style='display:none'><div class='xr-header'><div class='xr-obj-type'>xarray.Dataset</div></div><ul class='xr-sections'><li class='xr-section-item'><input id='section-a3e4927a-9ae4-4e58-86bf-0dae99e3354a' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-a3e4927a-9ae4-4e58-86bf-0dae99e3354a' class='xr-section-summary' title='Expand/collapse section'>Dimensions:</label><div class='xr-section-inline-details'><ul class='xr-dim-list'><li><span>traj</span>: 100000</li><li><span>obs</span>: 673</li></ul></div><div class='xr-section-details'></div></li><li class='xr-section-item'><input id='section-64ce12f2-2eac-4c21-9555-588c14e7ecb7' class='xr-section-summary-in' type='checkbox' checked><label for='section-64ce12f2-2eac-4c21-9555-588c14e7ecb7' class='xr-section-summary' >Coordinates: <span>(1)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-var-name'><span>offset</span></div><div class='xr-var-dims'>()</div><div class='xr-var-dtype'>int64</div><div class='xr-var-preview xr-preview'>0</div><input id='attrs-dcca6391-e532-4eb8-b42d-db5ca99ca8a4' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-dcca6391-e532-4eb8-b42d-db5ca99ca8a4' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-d1c21e88-9cee-4311-98cf-892243ad754f' class='xr-var-data-in' type='checkbox'><label for='data-d1c21e88-9cee-4311-98cf-892243ad754f' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array(0)</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-89c3eae5-6e49-404f-a4ea-9546545a92e1' class='xr-section-summary-in' type='checkbox' checked><label for='section-89c3eae5-6e49-404f-a4ea-9546545a92e1' class='xr-section-summary' >Data variables: <span>(6)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-var-name'><span>land</span></div><div class='xr-var-dims'>(traj, obs)</div><div class='xr-var-dtype'>float32</div><div class='xr-var-preview xr-preview'>0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0</div><input id='attrs-90f2bbbd-0cda-452d-a74d-ed8fcf500229' class='xr-var-attrs-in' type='checkbox' ><label for='attrs-90f2bbbd-0cda-452d-a74d-ed8fcf500229' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-ba836bdf-b9ad-4002-92ea-466beb0cad10' class='xr-var-data-in' type='checkbox'><label for='data-ba836bdf-b9ad-4002-92ea-466beb0cad10' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'><dt><span>long_name :</span></dt><dd></dd><dt><span>standard_name :</span></dt><dd>land</dd><dt><span>units :</span></dt><dd>unknown</dd></dl></div><div class='xr-var-data'><pre>array([[0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" ...,\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.],\n",
" [0., 0., 0., ..., 0., 0., 0.]], dtype=float32)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>lat</span></div><div class='xr-var-dims'>(traj, obs)</div><div class='xr-var-dtype'>float32</div><div class='xr-var-preview xr-preview'>61.29 61.28 61.29 ... 56.53 56.54</div><input id='attrs-0c04a6ed-31d9-4205-b64e-c3671cce5018' class='xr-var-attrs-in' type='checkbox' ><label for='attrs-0c04a6ed-31d9-4205-b64e-c3671cce5018' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-86fe3856-d073-41ed-970d-88aa6a730a0a' class='xr-var-data-in' type='checkbox'><label for='data-86fe3856-d073-41ed-970d-88aa6a730a0a' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'><dt><span>axis :</span></dt><dd>Y</dd><dt><span>long_name :</span></dt><dd></dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>units :</span></dt><dd>degrees_north</dd></dl></div><div class='xr-var-data'><pre>array([[61.285473, 61.284927, 61.28532 , ..., 62.71376 , 62.71153 ,\n",
" 62.70902 ],\n",
" [48.941383, 48.94064 , 48.93731 , ..., 49.01928 , 49.003345,\n",
" 48.987934],\n",
" [54.650616, 54.654938, 54.658684, ..., 55.54535 , 55.545307,\n",
" 55.54547 ],\n",
" ...,\n",
" [55.866844, 55.860252, 55.858585, ..., 55.967144, 55.960712,\n",
" 55.95742 ],\n",
" [55.810215, 55.805927, 55.805897, ..., 55.640965, 55.636017,\n",
" 55.6324 ],\n",
" [57.27783 , 57.28191 , 57.287617, ..., 56.527634, 56.534416,\n",
" 56.542187]], dtype=float32)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>lon</span></div><div class='xr-var-dims'>(traj, obs)</div><div class='xr-var-dtype'>float32</div><div class='xr-var-preview xr-preview'>-14.75 -14.75 ... -11.88 -11.89</div><input id='attrs-a3c14954-a938-4164-92f5-3d9e79fdb155' class='xr-var-attrs-in' type='checkbox' ><label for='attrs-a3c14954-a938-4164-92f5-3d9e79fdb155' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-0e586adc-0e99-4da5-8630-617418357a06' class='xr-var-data-in' type='checkbox'><label for='data-0e586adc-0e99-4da5-8630-617418357a06' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'><dt><span>axis :</span></dt><dd>X</dd><dt><span>long_name :</span></dt><dd></dd><dt><span>standard_name :</span></dt><dd>longitude</dd><dt><span>units :</span></dt><dd>degrees_east</dd></dl></div><div class='xr-var-data'><pre>array([[-14.749123 , -14.749268 , -14.7495775 , ..., -15.581957 ,\n",
" -15.5886 , -15.59525 ],\n",
" [ -3.1824825 , -3.1187384 , -3.0546582 , ..., -2.9364817 ,\n",
" -2.8909383 , -2.8420727 ],\n",
" [-13.899103 , -13.898216 , -13.897724 , ..., -10.758175 ,\n",
" -10.756895 , -10.758032 ],\n",
" ...,\n",
" [ 0.47636342, 0.4765542 , 0.47941044, ..., 0.70302504,\n",
" 0.6990518 , 0.6969647 ],\n",
" [ 1.3673725 , 1.3631386 , 1.363806 , ..., 1.9310127 ,\n",
" 1.9263697 , 1.9253011 ],\n",
" [-11.575566 , -11.583425 , -11.587372 , ..., -11.873968 ,\n",
" -11.883909 , -11.890164 ]], dtype=float32)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>salinity</span></div><div class='xr-var-dims'>(traj, obs)</div><div class='xr-var-dtype'>float32</div><div class='xr-var-preview xr-preview'>35.13 35.13 35.13 ... 35.3 35.3</div><input id='attrs-039f4465-7518-498b-a6ce-1a9a5e65166c' class='xr-var-attrs-in' type='checkbox' ><label for='attrs-039f4465-7518-498b-a6ce-1a9a5e65166c' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-ebf84f7d-a537-4738-8f56-face953cef2a' class='xr-var-data-in' type='checkbox'><label for='data-ebf84f7d-a537-4738-8f56-face953cef2a' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'><dt><span>long_name :</span></dt><dd></dd><dt><span>standard_name :</span></dt><dd>salinity</dd><dt><span>units :</span></dt><dd>unknown</dd></dl></div><div class='xr-var-data'><pre>array([[35.132275, 35.132378, 35.13241 , ..., 35.185387, 35.185654,\n",
" 35.186142],\n",
" [34.982162, 34.99977 , 35.00254 , ..., 35.055252, 35.05264 ,\n",
" 35.05198 ],\n",
" [35.31447 , 35.31297 , 35.31247 , ..., 35.293976, 35.294373,\n",
" 35.2948 ],\n",
" ...,\n",
" [34.898018, 34.898483, 34.899742, ..., 34.916832, 34.915092,\n",
" 34.91388 ],\n",
" [34.936546, 34.93721 , 34.938137, ..., 34.926025, 34.923958,\n",
" 34.92224 ],\n",
" [35.36109 , 35.36182 , 35.3619 , ..., 35.302307, 35.30191 ,\n",
" 35.302105]], dtype=float32)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>temperature</span></div><div class='xr-var-dims'>(traj, obs)</div><div class='xr-var-dtype'>float32</div><div class='xr-var-preview xr-preview'>8.616 8.614 8.608 ... 12.1 12.09</div><input id='attrs-969bed5a-05b6-4ba0-b478-48d2dcd8d8b6' class='xr-var-attrs-in' type='checkbox' ><label for='attrs-969bed5a-05b6-4ba0-b478-48d2dcd8d8b6' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-4186bcf6-398c-42e7-a346-e4a022f5f31e' class='xr-var-data-in' type='checkbox'><label for='data-4186bcf6-398c-42e7-a346-e4a022f5f31e' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'><dt><span>long_name :</span></dt><dd></dd><dt><span>standard_name :</span></dt><dd>temperature</dd><dt><span>units :</span></dt><dd>unknown</dd></dl></div><div class='xr-var-data'><pre>array([[ 8.616366 , 8.613953 , 8.608484 , ..., 9.707571 , 9.713455 ,\n",
" 9.7187195],\n",
" [12.140737 , 12.18153 , 12.181167 , ..., 13.80144 , 13.796449 ,\n",
" 13.790367 ],\n",
" [10.732507 , 10.720863 , 10.712863 , ..., 12.229744 , 12.21837 ,\n",
" 12.210012 ],\n",
" ...,\n",
" [ 9.661629 , 9.708296 , 9.754385 , ..., 12.336579 , 12.317524 ,\n",
" 12.295458 ],\n",
" [ 9.837917 , 9.837573 , 9.847862 , ..., 12.255448 , 12.230164 ,\n",
" 12.207233 ],\n",
" [10.676577 , 10.682186 , 10.685406 , ..., 12.101152 , 12.095234 ,\n",
" 12.087187 ]], dtype=float32)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>time</span></div><div class='xr-var-dims'>(traj, obs)</div><div class='xr-var-dtype'>datetime64[ns]</div><div class='xr-var-preview xr-preview'>2019-05-15 ... 2019-06-12</div><input id='attrs-da14e8f3-83b1-43ed-8c95-f5b47fec7f83' class='xr-var-attrs-in' type='checkbox' ><label for='attrs-da14e8f3-83b1-43ed-8c95-f5b47fec7f83' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-2f7b9685-84a0-4551-b8e7-9aa8f6375d3d' class='xr-var-data-in' type='checkbox'><label for='data-2f7b9685-84a0-4551-b8e7-9aa8f6375d3d' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'><dt><span>axis :</span></dt><dd>T</dd><dt><span>long_name :</span></dt><dd></dd><dt><span>standard_name :</span></dt><dd>time</dd></dl></div><div class='xr-var-data'><pre>array([[&#x27;2019-05-15T00:00:00.000000000&#x27;, &#x27;2019-05-15T01:00:00.000000000&#x27;,\n",
" &#x27;2019-05-15T02:00:00.000000000&#x27;, ...,\n",
" &#x27;2019-06-11T22:00:00.000000000&#x27;, &#x27;2019-06-11T23:00:00.000000000&#x27;,\n",
" &#x27;2019-06-12T00:00:00.000000000&#x27;],\n",
" [&#x27;2019-05-15T00:00:00.000000000&#x27;, &#x27;2019-05-15T01:00:00.000000000&#x27;,\n",
" &#x27;2019-05-15T02:00:00.000000000&#x27;, ...,\n",
" &#x27;2019-06-11T22:00:00.000000000&#x27;, &#x27;2019-06-11T23:00:00.000000000&#x27;,\n",
" &#x27;2019-06-12T00:00:00.000000000&#x27;],\n",
" [&#x27;2019-05-15T00:00:00.000000000&#x27;, &#x27;2019-05-15T01:00:00.000000000&#x27;,\n",
" &#x27;2019-05-15T02:00:00.000000000&#x27;, ...,\n",
" &#x27;2019-06-11T22:00:00.000000000&#x27;, &#x27;2019-06-11T23:00:00.000000000&#x27;,\n",
" &#x27;2019-06-12T00:00:00.000000000&#x27;],\n",
" ...,\n",
" [&#x27;2019-05-15T00:00:00.000000000&#x27;, &#x27;2019-05-15T01:00:00.000000000&#x27;,\n",
" &#x27;2019-05-15T02:00:00.000000000&#x27;, ...,\n",
" &#x27;2019-06-11T22:00:00.000000000&#x27;, &#x27;2019-06-11T23:00:00.000000000&#x27;,\n",
" &#x27;2019-06-12T00:00:00.000000000&#x27;],\n",
" [&#x27;2019-05-15T00:00:00.000000000&#x27;, &#x27;2019-05-15T01:00:00.000000000&#x27;,\n",
" &#x27;2019-05-15T02:00:00.000000000&#x27;, ...,\n",
" &#x27;2019-06-11T22:00:00.000000000&#x27;, &#x27;2019-06-11T23:00:00.000000000&#x27;,\n",
" &#x27;2019-06-12T00:00:00.000000000&#x27;],\n",
" [&#x27;2019-05-15T00:00:00.000000000&#x27;, &#x27;2019-05-15T01:00:00.000000000&#x27;,\n",
" &#x27;2019-05-15T02:00:00.000000000&#x27;, ...,\n",
" &#x27;2019-06-11T22:00:00.000000000&#x27;, &#x27;2019-06-11T23:00:00.000000000&#x27;,\n",
" &#x27;2019-06-12T00:00:00.000000000&#x27;]], dtype=&#x27;datetime64[ns]&#x27;)</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-ffa861c3-06c6-41c7-99e8-f518f11ecf51' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-ffa861c3-06c6-41c7-99e8-f518f11ecf51' class='xr-section-summary' title='Expand/collapse section'>Indexes: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'></ul></div></li><li class='xr-section-item'><input id='section-ba42bb0d-7eef-49b5-8969-ae67b3bf5468' class='xr-section-summary-in' type='checkbox' checked><label for='section-ba42bb0d-7eef-49b5-8969-ae67b3bf5468' class='xr-section-summary' >Attributes: <span>(5)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><dl class='xr-attrs'><dt><span>Conventions :</span></dt><dd>CF-1.6/CF-1.7</dd><dt><span>feature_type :</span></dt><dd>trajectory</dd><dt><span>ncei_template_version :</span></dt><dd>NCEI_NetCDF_Trajectory_Template_v2.0</dd><dt><span>parcels_mesh :</span></dt><dd>spherical</dd><dt><span>parcels_version :</span></dt><dd>2.3.1</dd></dl></div></li></ul></div></div>"
],
"text/plain": [
"<xarray.Dataset> Size: 2GB\n",
"Dimensions: (traj: 100000, obs: 673)\n",
"Coordinates:\n",
" offset int64 8B 0\n",
"Dimensions without coordinates: traj, obs\n",
"Data variables:\n",
" land (traj, obs) float32 269MB 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0\n",
" lat (traj, obs) float32 269MB 61.29 61.28 61.29 ... 56.53 56.54\n",
" lon (traj, obs) float32 269MB -14.75 -14.75 ... -11.88 -11.89\n",
" salinity (traj, obs) float32 269MB 35.13 35.13 35.13 ... 35.3 35.3 35.3\n",
" temperature (traj, obs) float32 269MB 8.616 8.614 8.608 ... 12.1 12.1 12.09\n",
" time (traj, obs) datetime64[ns] 538MB 2019-05-15 ... 2019-06-12\n",
"Attributes:\n",
" Conventions: CF-1.6/CF-1.7\n",
" feature_type: trajectory\n",
" ncei_template_version: NCEI_NetCDF_Trajectory_Template_v2.0\n",
" parcels_mesh: spherical\n",
" parcels_version: 2.3.1"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ds_traj = xr.open_zarr(\"...\")\n",
"ds_traj = ds_traj.isel(year=0, offset=0)\n",
"ds_traj = ds_traj.compute()\n",
"ds_traj"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "4b693f9e-f099-4551-999c-9c3eeee1add0",
"metadata": {},
"outputs": [],
"source": [
"import tqdm"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6cf96f32-7624-4fee-b89c-99f584eec1fa",
"metadata": {},
"outputs": [],
"source": [
"def line_between(start, end):\n",
" \"\"\"Find intermediate points on a line from (x0, y0) to (x1, y1).\n",
" \n",
" Parameters\n",
" ----------\n",
" start: tuple\n",
" Contains x0 and y0.\n",
" end: tuple\n",
" Contains x1 and y1.\n",
" \n",
" Returns\n",
" -------\n",
" list\n",
" List of all intermediate points (x, y).\n",
" \n",
" \"\"\"\n",
" x0, y0 = start\n",
" x1, y1 = end\n",
" N = max(abs(x1 - x0) + 1, abs(y1 - y0) + 1)\n",
" dx = (x1 - x0) / (N - 1)\n",
" dy = (y1 - y0) / (N - 1)\n",
" xx = (round(x0 + n * dx) for n in range(N))\n",
" yy = (round(y0 + n * dy) for n in range(N))\n",
" return list(zip(xx, yy))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "5f6d4587-a03e-420a-98b5-a2779cbfbf32",
"metadata": {},
"outputs": [],
"source": [
"def line_between_sequence(points):\n",
" \"\"\"Fill in lines on all segments of points.\n",
" \n",
" Parameters\n",
" ----------\n",
" points: list\n",
" List of points (x, y).\n",
" \n",
" Returns\n",
" -------\n",
" list\n",
" List of points (x, y) with all segments filled in.\n",
"\n",
" \"\"\"\n",
" segments = [\n",
" line_between(start, end)[:-1]\n",
" for start, end in zip(points[:-1], points[1:])\n",
" ] + [points[-1:], ]\n",
" return reduce(add, segments)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "231f5e68-4c1f-4468-ba30-9a2811fa4cb6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"((-16.2, 10.2), (45.8, 62.900000000000006))"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lon_bds = round(ds_traj.lon.min().data[()], 1) - 0.2, round(ds_traj.lon.max().data[()], 1) + 0.2\n",
"lat_bds = round(ds_traj.lat.min().data[()], 1) - 0.2, round(ds_traj.lat.max().data[()], 1) + 0.2\n",
"lon_bds, lat_bds"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "dc5b4fab-2f75-4de0-ba7f-255e7faa0160",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 10000/10000 [00:56<00:00, 177.79it/s]\n"
]
}
],
"source": [
"N = 1_000\n",
"\n",
"dens = np.zeros((N, N), dtype=int)\n",
"\n",
"# for t in tqdm.tqdm(range(ds_traj.sizes[\"traj\"])):\n",
"for t in tqdm.tqdm(range(0, ds_traj.sizes[\"traj\"], 10)):\n",
" lon = ds_traj.isel(traj=t).lon\n",
" lat = ds_traj.isel(traj=t).lat\n",
" lon = lon.where(~lat.isnull(), drop=True)\n",
" lat = lat.where(~lat.isnull(), drop=True)\n",
"\n",
" point_list = list(zip(\n",
" np.digitize(lon.data, np.linspace(*lon_bds, N)).astype(int) - 1,\n",
" np.digitize(lat.data, np.linspace(*lat_bds, N)).astype(int) - 1,\n",
" ))\n",
" point_list = [i for i, j in zip_longest(point_list, point_list[1:]) if i != j]\n",
"\n",
" pos, count = np.unique(np.array(line_between_sequence(point_list)), axis=0, return_counts=True)\n",
"\n",
" dens[*pos.T[::-1]] += count"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "33ece664-90f4-4777-8f3a-369431143b0e",
"metadata": {},
"outputs": [],
"source": [
"_dens = xr.DataArray(\n",
" dens ** 0.4,\n",
" name=\"traj_dens\",\n",
" dims=(\"lon\", \"lat\"),\n",
" coords={\"lon\": np.linspace(*lon_bds, N), \"lat\": np.linspace(*lat_bds, N)},\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "41fb29f9-b223-40ca-b88c-ef4b8e1f25ab",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x14a4338c6ea0>"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment