{
"cells": [
{
"cell_type": "raw",
"id": "bcf23fc8-ae63-4efe-84bb-c67acdffb973",
"metadata": {},
"source": [
"---\n",
"title: \"Working with xarray data cubes\"\n",
"author: Eli Holmes (NOAA)\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "4961bc93-b62a-41aa-b3ae-767e493bcea5",
"metadata": {},
"source": [
"[][colab-link]\n",
"\n",
" \n",
" [][download-link]\n",
"\n",
"[download-link]: https://nmfs-opensci.github.io/NMFSHackDays-2025/topics-2025/2025-02-14-earthdata/4-data-cubes.ipynb\n",
"[colab-link]: https://colab.research.google.com/github/nmfs-opensci/nmfshackdays-2025/blob/main/topics-2025/2025-02-14-earthdata/4-data-cubes.ipynb\n",
"[jupyter-link]: https://nmfs-openscapes.2i2c.cloud/hub/user-redirect/lab?fromURL=https://raw.githubusercontent.com/nmfs-opensci/nmfshackdays-2025/main/topics-2025/2025-02-14-earthdata/4-data-cubes.ipynb"
]
},
{
"cell_type": "markdown",
"id": "45bbcecf-537b-40a7-92a3-e040f2c64c2f",
"metadata": {},
"source": [
">📘 Learning Objectives\n",
">\n",
"> 1. Intro to some `xarray` tools for data cubes\n",
"> 2. Point you to resources on line"
]
},
{
"cell_type": "markdown",
"id": "a28d9430-1a3e-480c-bf15-c35f938b4210",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"Once we have an `xarray` DataArray, there are many things we can do with it like aggregation, sampling, means and interpolation. See [this tutorial](https://earth-env-data-science.github.io/lectures/xarray/xarray-part2.html) for examples. See the `xarray` [gallery](https://docs.xarray.dev/en/latest/gallery.html) in the documentation.\n",
"\n",
"We will use the [GHRSST Level 4 AVHRR_OI Global Blended Sea Surface Temperature Analysis](https://cmr.earthdata.nasa.gov/search/concepts/C2036881712-POCLOUD.html) data from NCEI. It is lower resolution than the MUR data and will load faster. Also keep in mind that we are on a JupyterHub on Azure while the NASA data is on AWS us-west-2. This means we have to do data access via `https` and not direct access via `S3` and our data access is much slower.\n",
"\n",
"#### For those not working in the JupyterHub\n",
"\n",
"Create a code cell and run `pip install earthaccess`."
]
},
{
"cell_type": "markdown",
"id": "66d78efc-2d62-428a-a813-e58f949ee1bf",
"metadata": {},
"source": [
"### Import Required Packages"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f04a653d-b9e5-4cfe-a198-b5b612389742",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Suppress warnings\n",
"import warnings\n",
"warnings.simplefilter('ignore')\n",
"warnings.filterwarnings('ignore')\n",
"from pprint import pprint\n",
"\n",
"import earthaccess\n",
"import xarray as xr"
]
},
{
"cell_type": "markdown",
"id": "442bd92a-8f2d-4448-a59e-da4567710730",
"metadata": {},
"source": [
"## Create our data cube\n",
"\n",
"1) Authenticate"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "0fe0002f-c759-4611-8dd7-861b8bd38971",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"auth = earthaccess.login()\n",
"# are we authenticated?\n",
"if not auth.authenticated:\n",
" # ask for credentials and persist them in a .netrc file\n",
" auth.login(strategy=\"interactive\", persist=True)"
]
},
{
"cell_type": "markdown",
"id": "a6a3cb10-6988-401e-a618-59e2f5ac3228",
"metadata": {},
"source": [
"2) Get a vector of urls to our nc files"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "3dbe9828-37e9-4949-846f-297057e5b0d5",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"366"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"short_name = 'AVHRR_OI-NCEI-L4-GLOB-v2.1'\n",
"version = \"2.1\"\n",
"date_range = (\"2020-01-02\", \"2020-12-31\")\n",
"\n",
"results = earthaccess.search_data(\n",
" short_name = short_name,\n",
" version = version,\n",
" temporal = date_range,\n",
" cloud_hosted=True\n",
")\n",
"len(results)"
]
},
{
"cell_type": "markdown",
"id": "c2e81469-efb9-43b9-bb08-e41d73825b5a",
"metadata": {},
"source": [
"We can look at the data links and see that we will be using `https`."
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "a4499303-42de-442d-8540-f51da03ebc4b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['https://archive.podaac.earthdata.nasa.gov/podaac-ops-cumulus-protected/AVHRR_OI-NCEI-L4-GLOB-v2.1/20200101120000-NCEI-L4_GHRSST-SSTblend-AVHRR_OI-GLOB-v02.0-fv02.1.nc']"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results[0].data_links()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "0459b9a1-beab-4046-b0ef-349fc2cde82a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
Data: 20200101120000-NCEI-L4_GHRSST-SSTblend-AVHRR_OI-GLOB-v02.0-fv02.1.nc
\n", "Size: 0.99 MB
\n", "Cloud Hosted: True
\n", "<xarray.DataArray 'analysed_sst' (time: 366, lat: 8, lon: 8)> Size: 94kB\n", "[23424 values with dtype=float32]\n", "Coordinates:\n", " * time (time) datetime64[ns] 3kB 2020-01-01 2020-01-02 ... 2020-12-31\n", " * lat (lat) float32 32B 33.62 33.88 34.12 34.38 34.62 34.88 35.12 35.38\n", " * lon (lon) float32 32B -75.38 -75.12 -74.88 ... -74.12 -73.88 -73.62\n", "Attributes:\n", " long_name: analysed sea surface temperature\n", " standard_name: sea_surface_temperature\n", " units: kelvin\n", " valid_min: -300\n", " valid_max: 4500\n", " source: UNKNOWN,ICOADS SHIPS,ICOADS BUOYS,ICOADS argos,MMAB_50KM-...\n", " comment: Single-sensor Pathfinder 5.0/5.1 AVHRR SSTs used until 20...
<xarray.DataArray 'analysed_sst' ()> Size: 4B\n", "array(298.5631, dtype=float32)
<xarray.DataArray 'analysed_sst' (time: 366)> Size: 1kB\n", "array([296.73938, 296.89343, 296.92328, 296.5633 , 296.70032, 296.6867 ,\n", " 296.5006 , 296.46686, 296.1819 , 296.1514 , 296.5408 , 296.08966,\n", " 295.66736, 296.16452, 295.9995 , 295.877 , 295.8689 , 295.81546,\n", " 295.4739 , 295.47705, 295.5378 , 295.7533 , 296.26517, 296.2378 ,\n", " 296.21436, 296.1822 , 295.98047, 295.8164 , 295.77423, 295.4317 ,\n", " 295.04297, 294.88858, 295.22015, 295.47452, 295.70532, 295.77185,\n", " 295.7725 , 295.91983, 295.9339 , 295.44202, 295.20624, 295.39514,\n", " 295.65704, 295.5511 , 295.38702, 295.16672, 295.48843, 295.60126,\n", " 295.6162 , 295.88547, 295.90265, 295.3266 , 294.7489 , 294.76062,\n", " 295.0053 , 295.41937, 295.71063, 295.4889 , 295.3653 , 295.50092,\n", " 295.05002, 295.18872, 296.33875, 296.76828, 296.80655, 296.55017,\n", " 297.15778, 296.6717 , 296.58655, 296.70078, 297.09686, 297.0692 ,\n", " 297.34595, 297.4648 , 297.54623, 297.45624, 297.36905, 297.41486,\n", " 297.29703, 296.94843, 297.00296, 297.10297, 297.04312, 296.8697 ,\n", " 296.79578, 296.73407, 296.51404, 296.18063, 296.0562 , 296.51172,\n", " 297.10498, 296.7483 , 296.03015, 295.92047, 295.88983, 296.16718,\n", " 296.38904, 296.59125, 296.5714 , 296.40424, 296.64355, 296.26392,\n", " 296.03467, 295.97986, 295.99875, 296.44983, 296.52673, 296.5022 ,\n", " 296.50577, 296.57452, 296.70624, 296.77856, 296.8775 , 296.67688,\n", " 296.6355 , 297.33484, 297.4214 , 298.06763, 298.31906, 298.20358,\n", "...\n", " 302.57956, 302.55872, 302.62 , 302.58328, 302.49185, 302.51434,\n", " 302.33905, 302.04706, 301.8042 , 301.58594, 301.35706, 301.13138,\n", " 300.88452, 300.43124, 300.33765, 300.18933, 300.16235, 300.14844,\n", " 300.14154, 300.1325 , 300.32718, 300.47137, 300.50735, 300.43372,\n", " 300.39575, 300.45062, 300.4033 , 300.4425 , 300.3039 , 300.35266,\n", " 300.3156 , 300.26 , 300.0495 , 300.2542 , 300.35547, 300.2417 ,\n", " 299.96796, 299.90717, 299.70624, 299.72375, 299.85138, 299.90015,\n", " 299.88782, 300.08936, 300.11 , 300.11578, 300.1589 , 299.89825,\n", " 299.90952, 299.89752, 299.88358, 299.8698 , 299.54364, 299.56876,\n", " 299.3811 , 299.2125 , 298.97998, 298.70248, 298.53064, 298.51123,\n", " 298.48468, 298.60687, 298.55936, 298.48657, 298.63562, 298.5814 ,\n", " 298.34875, 298.12827, 298.09656, 298.14514, 298.11514, 297.3639 ,\n", " 297.1286 , 297.10577, 297.1211 , 297.15747, 296.96002, 297.01843,\n", " 297.25876, 297.4072 , 297.40875, 297.13153, 297.27155, 297.52905,\n", " 297.53876, 296.89252, 296.70795, 296.85562, 296.985 , 297.0583 ,\n", " 296.807 , 296.40845, 296.39642, 296.32092, 296.14795, 296.52753,\n", " 296.67593, 296.8983 , 296.97797, 297.04175, 296.1742 , 296.05078,\n", " 295.62952, 295.8086 , 295.93842, 295.68674, 295.57547, 295.6925 ,\n", " 295.84045, 295.65625, 295.6156 , 295.69235, 295.31842, 295.1786 ],\n", " dtype=float32)\n", "Coordinates:\n", " * time (time) datetime64[ns] 3kB 2020-01-01 2020-01-02 ... 2020-12-31
<xarray.DataArray 'analysed_sst' (time: 12, lat: 8, lon: 8)> Size: 3kB\n", "array([[[296.9161 , 296.48288, 296.22327, 296.16 , 296.1964 ,\n", " 296.07352, 295.80743, 295.47256],\n", " [297.16415, 296.80353, 296.51355, 296.3316 , 296.1929 ,\n", " 295.99835, 295.6813 , 295.29578],\n", " [297.13968, 296.93286, 296.70193, 296.48032, 296.27744,\n", " 296.00903, 295.58774, 295.16193],\n", " [297.1003 , 297.0984 , 296.9694 , 296.74966, 296.46744,\n", " 296.12775, 295.62775, 295.13354],\n", " [297.02063, 297.2971 , 297.25 , 297.01392, 296.66196,\n", " 296.22934, 295.68643, 295.11255],\n", " [296.29773, 297.15997, 297.34677, 297.15097, 296.76773,\n", " 296.30838, 295.80222, 295.21222],\n", " [293.87357, 295.5761 , 296.5371 , 296.86255, 296.6787 ,\n", " 296.34323, 295.90228, 295.32126],\n", " [290.24966, 292.47968, 294.4603 , 295.85062, 296.49615,\n", " 296.558 , 296.20642, 295.63776]],\n", "\n", " [[296.3341 , 295.47723, 294.7462 , 294.3193 , 294.1693 ,\n", " 294.26376, 294.38898, 294.46448],\n", " [296.92584, 296.05792, 295.19794, 294.60654, 294.31033,\n", "...\n", " 298.79996, 298.06934, 297.46368],\n", " [294.257 , 295.36368, 296.789 , 298.23196, 299.116 ,\n", " 299.17365, 298.57928, 297.94464]],\n", "\n", " [[297.47675, 296.9103 , 296.28128, 295.7184 , 295.37866,\n", " 295.26645, 295.30548, 295.4164 ],\n", " [297.69293, 297.20352, 296.5761 , 295.95096, 295.51385,\n", " 295.31454, 295.24738, 295.26065],\n", " [297.78708, 297.49353, 296.95062, 296.30417, 295.76486,\n", " 295.4597 , 295.2748 , 295.15585],\n", " [297.77063, 297.78806, 297.40323, 296.78485, 296.12967,\n", " 295.72324, 295.4055 , 295.14032],\n", " [297.52805, 297.9558 , 297.86838, 297.3313 , 296.6045 ,\n", " 296.0803 , 295.64093, 295.22516],\n", " [296.6216 , 297.469 , 297.8261 , 297.66513, 297.12387,\n", " 296.64093, 296.08096, 295.49902],\n", " [294.5638 , 295.72772, 296.73032, 297.3248 , 297.4303 ,\n", " 297.3384 , 296.77286, 296.05383],\n", " [291.87 , 293.03546, 294.47287, 295.90158, 296.97806,\n", " 297.611 , 297.3674 , 296.68582]]], dtype=float32)\n", "Coordinates:\n", " * lat (lat) float32 32B 33.62 33.88 34.12 34.38 34.62 34.88 35.12 35.38\n", " * lon (lon) float32 32B -75.38 -75.12 -74.88 ... -74.12 -73.88 -73.62\n", " * time (time) datetime64[ns] 96B 2020-01-01 2020-02-01 ... 2020-12-01\n", "Attributes:\n", " long_name: analysed sea surface temperature\n", " standard_name: sea_surface_temperature\n", " units: kelvin\n", " valid_min: -300\n", " valid_max: 4500\n", " source: UNKNOWN,ICOADS SHIPS,ICOADS BUOYS,ICOADS argos,MMAB_50KM-...\n", " comment: Single-sensor Pathfinder 5.0/5.1 AVHRR SSTs used until 20...