{ "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", "\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": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:   (lon: 288, lat: 192, month: 12, bnds: 2)\n",
       "Coordinates:\n",
       "    height    float64 ...\n",
       "  * lon       (lon) float64 0.0 1.25 2.5 3.75 5.0 ... 355.0 356.2 357.5 358.8\n",
       "  * lat       (lat) float64 -90.0 -89.06 -88.12 -87.17 ... 88.12 89.06 90.0\n",
       "  * month     (month) int64 1 2 3 4 5 6 7 8 9 10 11 12\n",
       "Dimensions without coordinates: bnds\n",
       "Data variables:\n",
       "    lat_bnds  (month, lat, bnds) float64 ...\n",
       "    lon_bnds  (month, lon, bnds) float64 ...\n",
       "    tas       (month, lat, lon) float32 ...
" ], "text/plain": [ "\n", "Dimensions: (lon: 288, lat: 192, month: 12, bnds: 2)\n", "Coordinates:\n", " height float64 ...\n", " * lon (lon) float64 0.0 1.25 2.5 3.75 5.0 ... 355.0 356.2 357.5 358.8\n", " * lat (lat) float64 -90.0 -89.06 -88.12 -87.17 ... 88.12 89.06 90.0\n", " * month (month) int64 1 2 3 4 5 6 7 8 9 10 11 12\n", "Dimensions without coordinates: bnds\n", "Data variables:\n", " lat_bnds (month, lat, bnds) float64 ...\n", " lon_bnds (month, lon, bnds) float64 ...\n", " tas (month, lat, lon) float32 ..." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "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')\n", "ds" ] }, { "cell_type": "code", "execution_count": 3, "id": "indoor-darwin", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
NAMESTATE_NAMESTATE_FIPSCNTY_FIPSFIPSgeometry
0Lake of the WoodsMinnesota2707727077POLYGON ((-95.34283 48.54668, -95.34105 48.715...
1FerryWashington5301953019POLYGON ((-118.85163 47.94956, -118.84846 48.4...
2StevensWashington5306553065POLYGON ((-117.43883 48.04412, -117.54219 48.0...
3OkanoganWashington5304753047POLYGON ((-118.97209 47.93915, -118.97406 47.9...
4Pend OreilleWashington5305153051POLYGON ((-117.43858 48.99992, -117.03205 48.9...
\n", "
" ], "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", " geometry \n", "0 POLYGON ((-95.34283 48.54668, -95.34105 48.715... \n", "1 POLYGON ((-118.85163 47.94956, -118.84846 48.4... \n", "2 POLYGON ((-117.43883 48.04412, -117.54219 48.0... \n", "3 POLYGON ((-118.97209 47.93915, -118.97406 47.9... \n", "4 POLYGON ((-117.43858 48.99992, -117.03205 48.9... " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Load US counties shapefile as a geopandas GeoDataFrame\n", "gdf = gpd.read_file('../../data/geo_data/UScounties.shp')\n", "gdf.head()" ] }, { "cell_type": "markdown", "id": "proof-corner", "metadata": {}, "source": [ "## Aggregate\n", "Now, aggregate the gridded variable in `ds` onto the polygons in `gdf`. Use the option `silent=True` if you'd like to suppress the status updates, or set it as a default using `xa.set_defaults(silent=True)`. " ] }, { "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": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:     (poly_idx: 3141, month: 12)\n",
       "Coordinates:\n",
       "  * poly_idx    (poly_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        (poly_idx) object 'Lake of the Woods' 'Ferry' ... 'Broomfield'\n",
       "    STATE_NAME  (poly_idx) object 'Minnesota' 'Washington' ... 'Colorado'\n",
       "    STATE_FIPS  (poly_idx) object '27' '53' '53' '53' ... '02' '02' '02' '08'\n",
       "    CNTY_FIPS   (poly_idx) object '077' '019' '065' '047' ... '240' '068' '014'\n",
       "    FIPS        (poly_idx) object '27077' '53019' '53065' ... '02068' '08014'\n",
       "    tas         (poly_idx, month) float64 263.9 268.8 274.0 ... 276.4 270.4
" ], "text/plain": [ "\n", "Dimensions: (poly_idx: 3141, month: 12)\n", "Coordinates:\n", " * poly_idx (poly_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 (poly_idx) object 'Lake of the Woods' 'Ferry' ... 'Broomfield'\n", " STATE_NAME (poly_idx) object 'Minnesota' 'Washington' ... 'Colorado'\n", " STATE_FIPS (poly_idx) object '27' '53' '53' '53' ... '02' '02' '02' '08'\n", " CNTY_FIPS (poly_idx) object '077' '019' '065' '047' ... '240' '068' '014'\n", " FIPS (poly_idx) object '27077' '53019' '53065' ... '02068' '08014'\n", " tas (poly_idx, month) float64 263.9 268.8 274.0 ... 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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
NAMESTATE_NAMESTATE_FIPSCNTY_FIPSFIPStas
poly_idxmonth
01Lake of the WoodsMinnesota2707727077263.918943
2Lake of the WoodsMinnesota2707727077268.834073
3Lake of the WoodsMinnesota2707727077273.977533
4Lake of the WoodsMinnesota2707727077283.141960
5Lake of the WoodsMinnesota2707727077290.623952
........................
31408BroomfieldColorado0801408014297.646820
9BroomfieldColorado0801408014292.368988
10BroomfieldColorado0801408014283.544708
11BroomfieldColorado0801408014276.383606
12BroomfieldColorado0801408014270.444855
\n", "

37692 rows × 6 columns

\n", "
" ], "text/plain": [ " NAME STATE_NAME STATE_FIPS CNTY_FIPS FIPS \\\n", "poly_idx month \n", "0 1 Lake of the Woods Minnesota 27 077 27077 \n", " 2 Lake of the Woods Minnesota 27 077 27077 \n", " 3 Lake of the Woods Minnesota 27 077 27077 \n", " 4 Lake of the Woods Minnesota 27 077 27077 \n", " 5 Lake of the Woods Minnesota 27 077 27077 \n", "... ... ... ... ... ... \n", "3140 8 Broomfield Colorado 08 014 08014 \n", " 9 Broomfield Colorado 08 014 08014 \n", " 10 Broomfield Colorado 08 014 08014 \n", " 11 Broomfield Colorado 08 014 08014 \n", " 12 Broomfield Colorado 08 014 08014 \n", "\n", " tas \n", "poly_idx month \n", "0 1 263.918943 \n", " 2 268.834073 \n", " 3 273.977533 \n", " 4 283.141960 \n", " 5 290.623952 \n", "... ... \n", "3140 8 297.646820 \n", " 9 292.368988 \n", " 10 283.544708 \n", " 11 276.383606 \n", " 12 270.444855 \n", "\n", "[37692 rows x 6 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 (ipykernel)", "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.12.1" } }, "nbformat": 4, "nbformat_minor": 5 }