{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "metropolitan-miracle",
   "metadata": {},
   "source": [
    "# Base run\n",
    "A simple run of `xagg`, aggregating gridded temperature data over US counties. For a deeper dive into `xagg`'s functionality, see the [Detailed Code Run](./full_run.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "sound-choir",
   "metadata": {},
   "outputs": [],
   "source": [
    "import xagg as xa\n",
    "import xarray as xr\n",
    "import numpy as np\n",
    "import geopandas as gpd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "disabled-ribbon",
   "metadata": {},
   "source": [
    "## Import\n",
    "The sample data in this example are:\n",
    "- gridded: month-of-year average temperature projections for the end-of-century from a climate model (CCSM4)\n",
    "- shapefiles: US counties"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "simple-spelling",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load some climate data as an xarray dataset\n",
    "ds = xr.open_dataset('../../data/climate_data/tas_Amon_CCSM4_rcp85_monthavg_20700101-20991231.nc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "indoor-darwin",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load US counties shapefile as a geopandas GeoDataFrame\n",
    "gdf = gpd.read_file('../../data/geo_data/UScounties.shp')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "proof-corner",
   "metadata": {},
   "source": [
    "## Aggregate\n",
    "Now, aggregate the gridded variable in `ds` onto the polygons in `gdf`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "written-lindsay",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "creating polygons for each pixel...\n",
      "calculating overlaps between pixels and output polygons...\n",
      "success!\n"
     ]
    }
   ],
   "source": [
    "# Calculate overlaps\n",
    "weightmap = xa.pixel_overlaps(ds,gdf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "composite-chemistry",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "adjusting grid... (this may happen because only a subset of pixels were used for aggregation for efficiency - i.e. [subset_bbox=True] in xa.pixel_overlaps())\n",
      "grid adjustment successful\n",
      "aggregating tas...\n",
      "all variables aggregated to polygons!\n"
     ]
    }
   ],
   "source": [
    "# Aggregate\n",
    "aggregated = xa.aggregate(ds,weightmap)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "grateful-romania",
   "metadata": {},
   "source": [
    "## Convert\n",
    "Finally, convert the aggregated data back into the format you would like. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "indie-compression",
   "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",
       "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;\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-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",
       "  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",
       "  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-attrs {\n",
       "  padding-left: 25px !important;\n",
       "}\n",
       "\n",
       ".xr-attrs,\n",
       ".xr-var-attrs,\n",
       ".xr-var-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",
       "  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;\n",
       "Dimensions:     (month: 12, pix_idx: 3141)\n",
       "Coordinates:\n",
       "  * pix_idx     (pix_idx) int64 0 1 2 3 4 5 6 ... 3135 3136 3137 3138 3139 3140\n",
       "  * month       (month) int64 1 2 3 4 5 6 7 8 9 10 11 12\n",
       "Data variables:\n",
       "    NAME        (pix_idx) object &#x27;Lake of the Woods&#x27; &#x27;Ferry&#x27; ... &#x27;Broomfield&#x27;\n",
       "    STATE_NAME  (pix_idx) object &#x27;Minnesota&#x27; &#x27;Washington&#x27; ... &#x27;Colorado&#x27;\n",
       "    STATE_FIPS  (pix_idx) object &#x27;27&#x27; &#x27;53&#x27; &#x27;53&#x27; &#x27;53&#x27; ... &#x27;02&#x27; &#x27;02&#x27; &#x27;02&#x27; &#x27;08&#x27;\n",
       "    CNTY_FIPS   (pix_idx) object &#x27;077&#x27; &#x27;019&#x27; &#x27;065&#x27; &#x27;047&#x27; ... &#x27;240&#x27; &#x27;068&#x27; &#x27;014&#x27;\n",
       "    FIPS        (pix_idx) object &#x27;27077&#x27; &#x27;53019&#x27; &#x27;53065&#x27; ... &#x27;02068&#x27; &#x27;08014&#x27;\n",
       "    tas         (pix_idx, month) float64 263.9 268.8 274.0 ... 283.5 276.4 270.4</pre><div class='xr-wrap' hidden><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-b203bdea-a4a3-418a-ab42-610d00a0fc16' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-b203bdea-a4a3-418a-ab42-610d00a0fc16' class='xr-section-summary'  title='Expand/collapse section'>Dimensions:</label><div class='xr-section-inline-details'><ul class='xr-dim-list'><li><span class='xr-has-index'>month</span>: 12</li><li><span class='xr-has-index'>pix_idx</span>: 3141</li></ul></div><div class='xr-section-details'></div></li><li class='xr-section-item'><input id='section-4d88a50d-67e6-463c-bd7a-08ebcc0e20bd' class='xr-section-summary-in' type='checkbox'  checked><label for='section-4d88a50d-67e6-463c-bd7a-08ebcc0e20bd' class='xr-section-summary' >Coordinates: <span>(2)</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 class='xr-has-index'>pix_idx</span></div><div class='xr-var-dims'>(pix_idx)</div><div class='xr-var-dtype'>int64</div><div class='xr-var-preview xr-preview'>0 1 2 3 4 ... 3137 3138 3139 3140</div><input id='attrs-68fa62c4-95f4-416d-b6c3-a0483bbfc117' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-68fa62c4-95f4-416d-b6c3-a0483bbfc117' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-db18ea92-8889-4b4f-948d-846b0e588770' class='xr-var-data-in' type='checkbox'><label for='data-db18ea92-8889-4b4f-948d-846b0e588770' 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,    1,    2, ..., 3138, 3139, 3140])</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>month</span></div><div class='xr-var-dims'>(month)</div><div class='xr-var-dtype'>int64</div><div class='xr-var-preview xr-preview'>1 2 3 4 5 6 7 8 9 10 11 12</div><input id='attrs-daef8f7f-d1c2-4cfc-b274-e5046fb40eff' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-daef8f7f-d1c2-4cfc-b274-e5046fb40eff' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-e47dd7ed-1267-4cae-8e23-51bd8b5d11e4' class='xr-var-data-in' type='checkbox'><label for='data-e47dd7ed-1267-4cae-8e23-51bd8b5d11e4' 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([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-fbe3391f-1033-42c8-813a-c00a42bab204' class='xr-section-summary-in' type='checkbox'  checked><label for='section-fbe3391f-1033-42c8-813a-c00a42bab204' 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>NAME</span></div><div class='xr-var-dims'>(pix_idx)</div><div class='xr-var-dtype'>object</div><div class='xr-var-preview xr-preview'>&#x27;Lake of the Woods&#x27; ... &#x27;Broomfi...</div><input id='attrs-5723c92e-88e3-4289-bd51-841eb98a14bb' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-5723c92e-88e3-4289-bd51-841eb98a14bb' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-c9861d9e-86aa-4dea-9940-62faf36ef76b' class='xr-var-data-in' type='checkbox'><label for='data-c9861d9e-86aa-4dea-9940-62faf36ef76b' 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([&#x27;Lake of the Woods&#x27;, &#x27;Ferry&#x27;, &#x27;Stevens&#x27;, ...,\n",
       "       &#x27;Southeast Fairbanks&#x27;, &#x27;Denali&#x27;, &#x27;Broomfield&#x27;], dtype=object)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>STATE_NAME</span></div><div class='xr-var-dims'>(pix_idx)</div><div class='xr-var-dtype'>object</div><div class='xr-var-preview xr-preview'>&#x27;Minnesota&#x27; ... &#x27;Colorado&#x27;</div><input id='attrs-9e87f314-16cb-4d1c-b1ee-2bd73b33cd70' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-9e87f314-16cb-4d1c-b1ee-2bd73b33cd70' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-eec79f95-1004-4845-81b2-311df774f822' class='xr-var-data-in' type='checkbox'><label for='data-eec79f95-1004-4845-81b2-311df774f822' 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([&#x27;Minnesota&#x27;, &#x27;Washington&#x27;, &#x27;Washington&#x27;, ..., &#x27;Alaska&#x27;, &#x27;Alaska&#x27;,\n",
       "       &#x27;Colorado&#x27;], dtype=object)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>STATE_FIPS</span></div><div class='xr-var-dims'>(pix_idx)</div><div class='xr-var-dtype'>object</div><div class='xr-var-preview xr-preview'>&#x27;27&#x27; &#x27;53&#x27; &#x27;53&#x27; ... &#x27;02&#x27; &#x27;02&#x27; &#x27;08&#x27;</div><input id='attrs-4608ce8e-8acf-4c25-8af0-411c4b7078d0' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-4608ce8e-8acf-4c25-8af0-411c4b7078d0' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-56938886-816a-403b-9ecf-37c673686b95' class='xr-var-data-in' type='checkbox'><label for='data-56938886-816a-403b-9ecf-37c673686b95' 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([&#x27;27&#x27;, &#x27;53&#x27;, &#x27;53&#x27;, ..., &#x27;02&#x27;, &#x27;02&#x27;, &#x27;08&#x27;], dtype=object)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>CNTY_FIPS</span></div><div class='xr-var-dims'>(pix_idx)</div><div class='xr-var-dtype'>object</div><div class='xr-var-preview xr-preview'>&#x27;077&#x27; &#x27;019&#x27; &#x27;065&#x27; ... &#x27;068&#x27; &#x27;014&#x27;</div><input id='attrs-6788a5ee-3e41-46f6-8292-813c26d9c6c5' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-6788a5ee-3e41-46f6-8292-813c26d9c6c5' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-6a94bf18-94b4-4e51-b6ed-5e1ddbf570ae' class='xr-var-data-in' type='checkbox'><label for='data-6a94bf18-94b4-4e51-b6ed-5e1ddbf570ae' 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([&#x27;077&#x27;, &#x27;019&#x27;, &#x27;065&#x27;, ..., &#x27;240&#x27;, &#x27;068&#x27;, &#x27;014&#x27;], dtype=object)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>FIPS</span></div><div class='xr-var-dims'>(pix_idx)</div><div class='xr-var-dtype'>object</div><div class='xr-var-preview xr-preview'>&#x27;27077&#x27; &#x27;53019&#x27; ... &#x27;02068&#x27; &#x27;08014&#x27;</div><input id='attrs-1712c52c-97e8-41ef-8a4f-2399535ed6c1' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-1712c52c-97e8-41ef-8a4f-2399535ed6c1' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-42467765-ca2a-4932-a5dd-27386b532ce6' class='xr-var-data-in' type='checkbox'><label for='data-42467765-ca2a-4932-a5dd-27386b532ce6' 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([&#x27;27077&#x27;, &#x27;53019&#x27;, &#x27;53065&#x27;, ..., &#x27;02240&#x27;, &#x27;02068&#x27;, &#x27;08014&#x27;],\n",
       "      dtype=object)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>tas</span></div><div class='xr-var-dims'>(pix_idx, month)</div><div class='xr-var-dtype'>float64</div><div class='xr-var-preview xr-preview'>263.9 268.8 274.0 ... 276.4 270.4</div><input id='attrs-f42c04c7-63b2-4165-a796-a7ec772b2f34' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-f42c04c7-63b2-4165-a796-a7ec772b2f34' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-c08108d7-e476-4c56-8470-f566af534d8c' class='xr-var-data-in' type='checkbox'><label for='data-c08108d7-e476-4c56-8470-f566af534d8c' 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([[263.91894338, 268.83407312, 273.97753272, ..., 283.79866008,\n",
       "        275.10910005, 266.01617602],\n",
       "       [271.79416912, 275.6313644 , 276.94708001, ..., 281.64892744,\n",
       "        276.72788621, 272.2569337 ],\n",
       "       [272.11315505, 275.91027861, 277.35535375, ..., 282.07634405,\n",
       "        277.01922179, 272.51605575],\n",
       "       ...,\n",
       "       [262.8463124 , 263.0001854 , 265.4380367 , ..., 271.71306123,\n",
       "        264.94652565, 263.29793632],\n",
       "       [265.08434154, 264.54793604, 267.20395358, ..., 272.51411657,\n",
       "        266.14508751, 265.68265969],\n",
       "       [270.80386353, 273.4302063 , 275.95550537, ..., 283.54470825,\n",
       "        276.38360596, 270.44485474]])</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-96354ece-38c9-4efb-b4f2-718f1d5f0eae' class='xr-section-summary-in' type='checkbox' disabled ><label for='section-96354ece-38c9-4efb-b4f2-718f1d5f0eae' class='xr-section-summary'  title='Expand/collapse section'>Attributes: <span>(0)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><dl class='xr-attrs'></dl></div></li></ul></div></div>"
      ],
      "text/plain": [
       "<xarray.Dataset>\n",
       "Dimensions:     (month: 12, pix_idx: 3141)\n",
       "Coordinates:\n",
       "  * pix_idx     (pix_idx) int64 0 1 2 3 4 5 6 ... 3135 3136 3137 3138 3139 3140\n",
       "  * month       (month) int64 1 2 3 4 5 6 7 8 9 10 11 12\n",
       "Data variables:\n",
       "    NAME        (pix_idx) object 'Lake of the Woods' 'Ferry' ... 'Broomfield'\n",
       "    STATE_NAME  (pix_idx) object 'Minnesota' 'Washington' ... 'Colorado'\n",
       "    STATE_FIPS  (pix_idx) object '27' '53' '53' '53' ... '02' '02' '02' '08'\n",
       "    CNTY_FIPS   (pix_idx) object '077' '019' '065' '047' ... '240' '068' '014'\n",
       "    FIPS        (pix_idx) object '27077' '53019' '53065' ... '02068' '08014'\n",
       "    tas         (pix_idx, month) float64 263.9 268.8 274.0 ... 283.5 276.4 270.4"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Example as an xarray dataset\n",
    "ds_out = aggregated.to_dataset()\n",
    "ds_out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "organic-encoding",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>NAME</th>\n",
       "      <th>STATE_NAME</th>\n",
       "      <th>STATE_FIPS</th>\n",
       "      <th>CNTY_FIPS</th>\n",
       "      <th>FIPS</th>\n",
       "      <th>tas0</th>\n",
       "      <th>tas1</th>\n",
       "      <th>tas2</th>\n",
       "      <th>tas3</th>\n",
       "      <th>tas4</th>\n",
       "      <th>tas5</th>\n",
       "      <th>tas6</th>\n",
       "      <th>tas7</th>\n",
       "      <th>tas8</th>\n",
       "      <th>tas9</th>\n",
       "      <th>tas10</th>\n",
       "      <th>tas11</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Lake of the Woods</td>\n",
       "      <td>Minnesota</td>\n",
       "      <td>27</td>\n",
       "      <td>077</td>\n",
       "      <td>27077</td>\n",
       "      <td>263.918943</td>\n",
       "      <td>268.834073</td>\n",
       "      <td>273.977533</td>\n",
       "      <td>283.141960</td>\n",
       "      <td>290.623952</td>\n",
       "      <td>297.858885</td>\n",
       "      <td>302.068017</td>\n",
       "      <td>300.362248</td>\n",
       "      <td>293.471128</td>\n",
       "      <td>283.798660</td>\n",
       "      <td>275.109100</td>\n",
       "      <td>266.016176</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Ferry</td>\n",
       "      <td>Washington</td>\n",
       "      <td>53</td>\n",
       "      <td>019</td>\n",
       "      <td>53019</td>\n",
       "      <td>271.794169</td>\n",
       "      <td>275.631364</td>\n",
       "      <td>276.947080</td>\n",
       "      <td>279.837102</td>\n",
       "      <td>286.630023</td>\n",
       "      <td>293.769471</td>\n",
       "      <td>299.073178</td>\n",
       "      <td>297.151514</td>\n",
       "      <td>289.866690</td>\n",
       "      <td>281.648927</td>\n",
       "      <td>276.727886</td>\n",
       "      <td>272.256934</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Stevens</td>\n",
       "      <td>Washington</td>\n",
       "      <td>53</td>\n",
       "      <td>065</td>\n",
       "      <td>53065</td>\n",
       "      <td>272.113155</td>\n",
       "      <td>275.910279</td>\n",
       "      <td>277.355354</td>\n",
       "      <td>280.428965</td>\n",
       "      <td>287.247099</td>\n",
       "      <td>294.356788</td>\n",
       "      <td>299.847098</td>\n",
       "      <td>297.967740</td>\n",
       "      <td>290.637124</td>\n",
       "      <td>282.076344</td>\n",
       "      <td>277.019222</td>\n",
       "      <td>272.516056</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Okanogan</td>\n",
       "      <td>Washington</td>\n",
       "      <td>53</td>\n",
       "      <td>047</td>\n",
       "      <td>53047</td>\n",
       "      <td>271.772021</td>\n",
       "      <td>275.539162</td>\n",
       "      <td>276.654805</td>\n",
       "      <td>279.317270</td>\n",
       "      <td>285.794503</td>\n",
       "      <td>292.650947</td>\n",
       "      <td>297.741617</td>\n",
       "      <td>295.915714</td>\n",
       "      <td>289.090624</td>\n",
       "      <td>281.372544</td>\n",
       "      <td>276.598377</td>\n",
       "      <td>272.208944</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Pend Oreille</td>\n",
       "      <td>Washington</td>\n",
       "      <td>53</td>\n",
       "      <td>051</td>\n",
       "      <td>53051</td>\n",
       "      <td>271.721285</td>\n",
       "      <td>275.542011</td>\n",
       "      <td>276.993355</td>\n",
       "      <td>280.157156</td>\n",
       "      <td>287.086018</td>\n",
       "      <td>294.169635</td>\n",
       "      <td>299.503768</td>\n",
       "      <td>297.523382</td>\n",
       "      <td>290.086946</td>\n",
       "      <td>281.657134</td>\n",
       "      <td>276.644670</td>\n",
       "      <td>272.095152</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3136</th>\n",
       "      <td>Skagway-Hoonah-Angoon</td>\n",
       "      <td>Alaska</td>\n",
       "      <td>02</td>\n",
       "      <td>232</td>\n",
       "      <td>02232</td>\n",
       "      <td>270.709185</td>\n",
       "      <td>272.455135</td>\n",
       "      <td>273.717142</td>\n",
       "      <td>276.188285</td>\n",
       "      <td>281.253285</td>\n",
       "      <td>286.791100</td>\n",
       "      <td>288.361128</td>\n",
       "      <td>287.822862</td>\n",
       "      <td>284.093411</td>\n",
       "      <td>278.681980</td>\n",
       "      <td>274.221760</td>\n",
       "      <td>271.175471</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3137</th>\n",
       "      <td>Yukon-Koyukuk</td>\n",
       "      <td>Alaska</td>\n",
       "      <td>02</td>\n",
       "      <td>290</td>\n",
       "      <td>02290</td>\n",
       "      <td>263.970656</td>\n",
       "      <td>263.404975</td>\n",
       "      <td>266.670047</td>\n",
       "      <td>272.394716</td>\n",
       "      <td>280.492861</td>\n",
       "      <td>288.813169</td>\n",
       "      <td>288.513645</td>\n",
       "      <td>285.724033</td>\n",
       "      <td>280.243361</td>\n",
       "      <td>273.044271</td>\n",
       "      <td>266.155923</td>\n",
       "      <td>265.022613</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3138</th>\n",
       "      <td>Southeast Fairbanks</td>\n",
       "      <td>Alaska</td>\n",
       "      <td>02</td>\n",
       "      <td>240</td>\n",
       "      <td>02240</td>\n",
       "      <td>262.846312</td>\n",
       "      <td>263.000185</td>\n",
       "      <td>265.438037</td>\n",
       "      <td>270.754788</td>\n",
       "      <td>278.476096</td>\n",
       "      <td>286.669566</td>\n",
       "      <td>287.315147</td>\n",
       "      <td>284.920161</td>\n",
       "      <td>279.230840</td>\n",
       "      <td>271.713061</td>\n",
       "      <td>264.946526</td>\n",
       "      <td>263.297936</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3139</th>\n",
       "      <td>Denali</td>\n",
       "      <td>Alaska</td>\n",
       "      <td>02</td>\n",
       "      <td>068</td>\n",
       "      <td>02068</td>\n",
       "      <td>265.084342</td>\n",
       "      <td>264.547936</td>\n",
       "      <td>267.203954</td>\n",
       "      <td>271.782649</td>\n",
       "      <td>278.898267</td>\n",
       "      <td>287.059920</td>\n",
       "      <td>287.375217</td>\n",
       "      <td>285.069283</td>\n",
       "      <td>279.833609</td>\n",
       "      <td>272.514117</td>\n",
       "      <td>266.145088</td>\n",
       "      <td>265.682660</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3140</th>\n",
       "      <td>Broomfield</td>\n",
       "      <td>Colorado</td>\n",
       "      <td>08</td>\n",
       "      <td>014</td>\n",
       "      <td>08014</td>\n",
       "      <td>270.803864</td>\n",
       "      <td>273.430206</td>\n",
       "      <td>275.955505</td>\n",
       "      <td>280.790070</td>\n",
       "      <td>287.303619</td>\n",
       "      <td>292.830048</td>\n",
       "      <td>297.615662</td>\n",
       "      <td>297.646820</td>\n",
       "      <td>292.368988</td>\n",
       "      <td>283.544708</td>\n",
       "      <td>276.383606</td>\n",
       "      <td>270.444855</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3141 rows × 17 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                       NAME  STATE_NAME STATE_FIPS CNTY_FIPS   FIPS  \\\n",
       "0         Lake of the Woods   Minnesota         27       077  27077   \n",
       "1                     Ferry  Washington         53       019  53019   \n",
       "2                   Stevens  Washington         53       065  53065   \n",
       "3                  Okanogan  Washington         53       047  53047   \n",
       "4              Pend Oreille  Washington         53       051  53051   \n",
       "...                     ...         ...        ...       ...    ...   \n",
       "3136  Skagway-Hoonah-Angoon      Alaska         02       232  02232   \n",
       "3137          Yukon-Koyukuk      Alaska         02       290  02290   \n",
       "3138    Southeast Fairbanks      Alaska         02       240  02240   \n",
       "3139                 Denali      Alaska         02       068  02068   \n",
       "3140             Broomfield    Colorado         08       014  08014   \n",
       "\n",
       "            tas0        tas1        tas2        tas3        tas4        tas5  \\\n",
       "0     263.918943  268.834073  273.977533  283.141960  290.623952  297.858885   \n",
       "1     271.794169  275.631364  276.947080  279.837102  286.630023  293.769471   \n",
       "2     272.113155  275.910279  277.355354  280.428965  287.247099  294.356788   \n",
       "3     271.772021  275.539162  276.654805  279.317270  285.794503  292.650947   \n",
       "4     271.721285  275.542011  276.993355  280.157156  287.086018  294.169635   \n",
       "...          ...         ...         ...         ...         ...         ...   \n",
       "3136  270.709185  272.455135  273.717142  276.188285  281.253285  286.791100   \n",
       "3137  263.970656  263.404975  266.670047  272.394716  280.492861  288.813169   \n",
       "3138  262.846312  263.000185  265.438037  270.754788  278.476096  286.669566   \n",
       "3139  265.084342  264.547936  267.203954  271.782649  278.898267  287.059920   \n",
       "3140  270.803864  273.430206  275.955505  280.790070  287.303619  292.830048   \n",
       "\n",
       "            tas6        tas7        tas8        tas9       tas10       tas11  \n",
       "0     302.068017  300.362248  293.471128  283.798660  275.109100  266.016176  \n",
       "1     299.073178  297.151514  289.866690  281.648927  276.727886  272.256934  \n",
       "2     299.847098  297.967740  290.637124  282.076344  277.019222  272.516056  \n",
       "3     297.741617  295.915714  289.090624  281.372544  276.598377  272.208944  \n",
       "4     299.503768  297.523382  290.086946  281.657134  276.644670  272.095152  \n",
       "...          ...         ...         ...         ...         ...         ...  \n",
       "3136  288.361128  287.822862  284.093411  278.681980  274.221760  271.175471  \n",
       "3137  288.513645  285.724033  280.243361  273.044271  266.155923  265.022613  \n",
       "3138  287.315147  284.920161  279.230840  271.713061  264.946526  263.297936  \n",
       "3139  287.375217  285.069283  279.833609  272.514117  266.145088  265.682660  \n",
       "3140  297.615662  297.646820  292.368988  283.544708  276.383606  270.444855  \n",
       "\n",
       "[3141 rows x 17 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Example as a pandas dataframe\n",
    "df_out = aggregated.to_dataframe()\n",
    "df_out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "289ebe21",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
