Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API extending xgcm vertical regridding #388

Merged
merged 75 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 68 commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
fd34905
Adds initial vertical regridding via xgcm
jasonb5 Nov 11, 2022
e997d76
Merge remote-tracking branch 'upstream/main' into adds_vertical_regrid
jasonb5 Nov 29, 2022
da38684
Refactors create_grid to be more flexible
jasonb5 Dec 1, 2022
5795a45
Fixes typings
jasonb5 Dec 1, 2022
724c889
Adds some initial documentation
jasonb5 Dec 2, 2022
abcba85
Fixes grid method to handle vertical grids
jasonb5 Dec 2, 2022
3bbe0b7
Removes unused test
jasonb5 Dec 2, 2022
5edcb27
Resolves type issue
jasonb5 Dec 2, 2022
3b30e64
Temporary pin esmpy
jasonb5 Dec 15, 2022
cc83984
Updates testing
jasonb5 Dec 23, 2022
6e112c2
Fixes type errors
jasonb5 Dec 23, 2022
b001c9e
Fixes using input grid's Z coordinate
jasonb5 Jan 10, 2023
246c094
Adds method to derive coord positions for regridding
jasonb5 Jan 11, 2023
7988f9d
Fixes failing pre-commit
jasonb5 Jan 11, 2023
e242e7a
Updates tests
jasonb5 Jan 18, 2023
be862df
Fixes typing for method
jasonb5 Jan 24, 2023
d1ed882
Adds default method
jasonb5 Jan 24, 2023
1324cea
Fixes target data argument
jasonb5 Jan 25, 2023
d32cd74
Fixes black and type errors
jasonb5 Jan 26, 2023
ae5c3aa
Exposes extra options
jasonb5 Jan 26, 2023
00569a6
Adds initial example notebook
jasonb5 Jan 26, 2023
1c31300
Adds initial vertical regridding notebook
jasonb5 Jan 26, 2023
9820113
Fixes loaded kernel
jasonb5 Jan 26, 2023
f106f67
Reworks description of XGCMRegridder
jasonb5 Jan 26, 2023
d73b763
Updates error message
jasonb5 Feb 15, 2023
a482dea
Updates vertical regridding example
jasonb5 Feb 15, 2023
517e38b
Merge branch 'main' into adds_vertical_regrid
jasonb5 Feb 15, 2023
95cd6cb
Fixes unresolved merge conflict
jasonb5 Feb 15, 2023
f0530f3
Fixes black formatting
jasonb5 Feb 15, 2023
6616881
Removes notebook
jasonb5 Feb 15, 2023
878c090
Fixes dict typing
jasonb5 Feb 24, 2023
05a58ac
add more comments for ocean example
chengzhuzhang Mar 10, 2023
9e2800c
add dependency;add atm example to notebook
chengzhuzhang Mar 16, 2023
6fe96ea
Fixes mismatch vertical coordinate names
jasonb5 Apr 7, 2023
7a2a853
Merge branch 'main' into adds_vertical_regrid
jasonb5 Apr 12, 2023
27bacf1
Add regridding notebooks to docs
tomvothecoder Apr 17, 2023
d64c595
Merge branch 'main' into adds_vertical_regrid
jasonb5 Apr 21, 2023
280539d
Update xcdat/regridder/xgcm.py
jasonb5 Apr 21, 2023
b40c013
Merge branch 'adds_vertical_regrid' of https://github.com/jasonb5/xcd…
jasonb5 Apr 21, 2023
853b6d2
Fixes package name
jasonb5 Apr 21, 2023
7420928
Fixes formatting/type issues
jasonb5 Apr 21, 2023
fdd6bc8
Fixes failing tests
jasonb5 Apr 21, 2023
3c32cec
Updates VAR_NAME_MAP and removes custom ref in grid.py
jasonb5 Apr 21, 2023
0b36eb9
Moves COORD_DEFAULT_ATTRS, updates types and adds T axis
jasonb5 Apr 21, 2023
08c206e
Updates docstring to reference valid key values
jasonb5 Apr 21, 2023
e3691ff
Fixes spacing and adds comments
jasonb5 Apr 21, 2023
b9b5c76
Fixes raising error when coordinate name is not valid
jasonb5 Apr 21, 2023
4154f46
Improves vertical method docstring
jasonb5 Apr 21, 2023
929dbda
Fixes vertical regridder output
jasonb5 Apr 22, 2023
d91e34d
Apply suggestions from code review
jasonb5 May 10, 2023
160b814
Removes reference to old theta argument
jasonb5 May 10, 2023
d7daf8b
Updates documentation
jasonb5 May 10, 2023
7c60d5f
Updates regridder to use get_dim_coords
jasonb5 May 10, 2023
b83430c
Cleans up example notebook
jasonb5 May 10, 2023
b18e74e
Merge remote-tracking branch 'upstream/main' into adds_vertical_regrid
jasonb5 May 23, 2023
d4d2be4
Fixes typings
jasonb5 May 23, 2023
e875ab6
Fixes logger
jasonb5 May 23, 2023
0124e2b
Fixes formatting error
jasonb5 May 23, 2023
4c87c33
Merge remote-tracking branch 'upstream/main' into adds_vertical_regrid
jasonb5 May 23, 2023
ddf423f
Merge branch 'main' into adds_vertical_regrid
jasonb5 Jun 2, 2023
e78f8c8
Removes generating bounds from horizontal/vertical regrid methods
jasonb5 Jun 8, 2023
5f94530
Fixes preserve_bounds function
jasonb5 Jun 8, 2023
de08dee
Adds XCGMRegridder to module
jasonb5 Jun 9, 2023
85c33e7
Adds missing argument to docstring
jasonb5 Jun 9, 2023
ec84a03
Cleans up to documentation
jasonb5 Jun 9, 2023
753bded
Fixes setting the attributes on the output DataArray
jasonb5 Jun 9, 2023
48e4253
Merge branch 'main' into adds_vertical_regrid
jasonb5 Jun 12, 2023
e75f1bc
Merge branch 'main' into adds_vertical_regrid
jasonb5 Jun 15, 2023
f32db3d
Apply suggestions from code review
jasonb5 Jun 15, 2023
fadd95b
Update docs/examples/regridding-vertical.ipynb
jasonb5 Jun 15, 2023
e0d6fca
Fixes missed _grid_coords renamed to _grid_positions
jasonb5 Jun 15, 2023
111232d
Changes preserve_bounds visibility
jasonb5 Jun 15, 2023
c406642
Rearranges _preserve_bounds arguments
jasonb5 Jun 15, 2023
06b17b3
Updates preserve_bounds test
jasonb5 Jun 15, 2023
50f01d9
Fixes black formatting
jasonb5 Jun 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conda-env/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies:
# Constrained because 0.6.3 breaks with import ESMF
# Source: https://github.com/pangeo-data/xESMF/issues/212
- xesmf >0.6.3
- xgcm
# Quality Assurance
# ==================
- types-python-dateutil
Expand Down
2 changes: 2 additions & 0 deletions conda-env/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies:
- pandas=1.5.3
- python-dateutil=2.8.2
- xarray=2023.4.2
- xgcm=0.8.0
# ==================
# Optional
# ==================
Expand All @@ -35,6 +36,7 @@ dependencies:
- nbsphinx=0.9.1
- pandoc=3.1.1
- ipython=8.11.0 # Required for nbsphinx syntax highlighting
- gsw-xarray=0.3.0 # Required for vertical regridding example
# ==================
# Quality Assurance
# ==================
Expand Down
1 change: 1 addition & 0 deletions conda-env/readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies:
- pandas=1.5.3
- python-dateutil=2.8.2
- xarray=2023.4.2
- xgcm=0.8.0
# ==================
# Optional
# ==================
Expand Down
2 changes: 2 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Classes
xcdat.regridder.accessor.RegridderAccessor
xcdat.regridder.regrid2.Regrid2Regridder
xcdat.regridder.xesmf.XESMFRegridder
xcdat.regridder.xgcm.XGCMRegridder

.. currentmodule:: xarray

Expand Down Expand Up @@ -123,6 +124,7 @@ Methods
Dataset.regridder.horizontal
Dataset.regridder.horizontal_xesmf
Dataset.regridder.horizontal_regrid2
Dataset.regridder.vertical

.. _dsmeth_1:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "a31785d6-310c-4871-9ac6-064a7874945f",
"metadata": {
"tags": []
},
"source": [
"# Horizontal Regridding\n",
"\n",
"Author: [Jason Boutte](https://github.com/jasonb5)\n",
"\n",
"Date: 08/24/22\n",
"\n",
"Related APIs:\n",
"\n",
"* [xarray.Dataset.regridder.horizontal](../generated/xarray.Dataset.regridder.horizontal.rst)\n",
"- [xarray.Dataset.regridder.horizontal](../generated/xarray.Dataset.regridder.horizontal.rst)\n",
"\n",
"The data used in this example can be found through the [Earth System Grid Federation (ESGF) search portal](https://aims2.llnl.gov/search).\n",
"\n",
"## Overview\n",
"\n",
"We'll cover horizontal regridding using the `xESMF` and `Regrid2` tools as well as various methods supported by `xESMF`.\n",
"\n",
"It should be noted that `Regrid2` treats the grid cells as being flat."
"It should be noted that `Regrid2` treats the grid cells as being flat.\n"
]
},
{
Expand All @@ -39,14 +42,16 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "7d904843-4079-4ebe-a3d0-ab439e8ce052",
"metadata": {},
"source": [
"## 1. Open the Dataset\n",
"\n",
"We are using xarray's OPeNDAP support to read a netCDF4 dataset file directly from its source. The data is not loaded over the network until we perform operations on it (e.g., temperature unit adjustment).\n",
"\n",
"More information on the xarray's OPeNDAP support can be found [here](https://docs.xarray.dev/en/stable/user-guide/io.html#opendap)."
"More information on the xarray's OPeNDAP support can be found [here](https://docs.xarray.dev/en/stable/user-guide/io.html#opendap).\n"
]
},
{
Expand Down Expand Up @@ -640,25 +645,27 @@
}
],
"source": [
"filepath = 'http://aims3.llnl.gov/thredds/dodsC/css03_data/CMIP6/CMIP/CCCma/CanESM5/historical/r13i1p1f1/Amon/tas/gn/v20190429/tas_Amon_CanESM5_historical_r13i1p1f1_gn_185001-201412.nc'\n",
"filepath = \"http://aims3.llnl.gov/thredds/dodsC/css03_data/CMIP6/CMIP/CCCma/CanESM5/historical/r13i1p1f1/Amon/tas/gn/v20190429/tas_Amon_CanESM5_historical_r13i1p1f1_gn_185001-201412.nc\"\n",
"\n",
"ds = xcdat.open_dataset(filepath)\n",
"\n",
"# Unit adjust (-273.15, K to C)\n",
"ds['tas'] = ds['tas'] - 273.15\n",
"ds[\"tas\"] = ds[\"tas\"] - 273.15\n",
"\n",
"ds"
"ds\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "1aa0dc89-d664-4721-8759-45065060a5b1",
"metadata": {},
"source": [
"## 2. Create the output grid\n",
"\n",
"Related API: [xcdat.create_gaussian_grid()](../generated/xcdat.create_gaussian_grid.rst)\n",
"\n",
"In this example, we will generate a gaussian grid with 32 latitudes to regrid our input data to."
"In this example, we will generate a gaussian grid with 32 latitudes to regrid our input data to.\n"
]
},
{
Expand All @@ -685,24 +692,26 @@
"\n",
"fig, axes = plt.subplots(ncols=2, figsize=(16, 4))\n",
"\n",
"ds.regridder.grid.plot.scatter('lon', 'lat', s=0.01, ax=axes[0])\n",
"axes[0].set_title('Input Grid')\n",
"ds.regridder.grid.plot.scatter(\"lon\", \"lat\", s=0.01, ax=axes[0])\n",
"axes[0].set_title(\"Input Grid\")\n",
"\n",
"output_grid.plot.scatter('lon', 'lat', s=0.1, ax=axes[1])\n",
"axes[1].set_title('Output Grid')\n",
"output_grid.plot.scatter(\"lon\", \"lat\", s=0.1, ax=axes[1])\n",
"axes[1].set_title(\"Output Grid\")\n",
"\n",
"plt.tight_layout()"
"plt.tight_layout()\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "9d5fe21a-85ef-4737-95fd-0a0868e89579",
"metadata": {},
"source": [
"## 3. Regrid the data\n",
"\n",
"Related API: [xarray.Dataset.regridder.horizontal()](../generated/xarray.Dataset.regridder.horizontal.rst)\n",
"\n",
"Here we will regrid the input data to the ouptut grid using the `xESMF` tool and the `bilinear` method."
"Here we will regrid the input data to the ouptut grid using the `xESMF` tool and the `bilinear` method.\n"
]
},
{
Expand All @@ -725,25 +734,27 @@
}
],
"source": [
"output = ds.regridder.horizontal('tas', output_grid, tool='xesmf', method='bilinear')\n",
"output = ds.regridder.horizontal(\"tas\", output_grid, tool=\"xesmf\", method=\"bilinear\")\n",
"\n",
"fig, axes = plt.subplots(ncols=2, figsize=(16, 4))\n",
"\n",
"ds.tas.isel(time=0).plot(ax=axes[0])\n",
"axes[0].set_title('Input data')\n",
"axes[0].set_title(\"Input data\")\n",
"\n",
"output.tas.isel(time=0).plot(ax=axes[1])\n",
"axes[1].set_title('Output data')\n",
"axes[1].set_title(\"Output data\")\n",
"\n",
"plt.tight_layout()"
"plt.tight_layout()\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "4084129e-dd96-4d0d-ae01-4a66f897e4e5",
"metadata": {},
"source": [
"## 4. Regridding algorithms\n",
"\n",
"Related API: [xarray.Dataset.regridder.horizontal()](../generated/xarray.Dataset.regridder.horizontal.rst)\n",
"\n",
"In this example, we will compare the different regridding methods supported by `xESMF`.\n",
Expand All @@ -752,11 +763,11 @@
"\n",
"Methods:\n",
"\n",
"* bilinear\n",
"* conservative\n",
"* nearest_s2d\n",
"* nearest_d2s\n",
"* patch"
"- bilinear\n",
"- conservative\n",
"- nearest_s2d\n",
"- nearest_d2s\n",
"- patch\n"
]
},
{
Expand All @@ -779,35 +790,37 @@
}
],
"source": [
"methods = ['bilinear', 'conservative', 'nearest_s2d', 'nearest_d2s', 'patch']\n",
"methods = [\"bilinear\", \"conservative\", \"nearest_s2d\", \"nearest_d2s\", \"patch\"]\n",
"\n",
"fig, axes = plt.subplots(3, 2, figsize=(16, 12))\n",
"\n",
"axes = axes.flatten()\n",
"\n",
"for i, method in enumerate(methods):\n",
" output = ds.regridder.horizontal('tas', output_grid, tool='xesmf', method=method)\n",
" output = ds.regridder.horizontal(\"tas\", output_grid, tool=\"xesmf\", method=method)\n",
"\n",
" output.tas.isel(time=0).plot(ax=axes[i])\n",
"\n",
" axes[i].set_title(method)\n",
"\n",
"axes[-1].set_visible(False)\n",
"\n",
"plt.tight_layout()"
"plt.tight_layout()\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "92685e37-ed43-42c2-b9b3-8b4722075ebc",
"metadata": {},
"source": [
"## 5. Masking\n",
"\n",
"Related API: [xarray.Dataset.regridder.horizontal()](../generated/xarray.Dataset.regridder.horizontal.rst)\n",
"\n",
"`xESMF` supports masking by simply adding a data variable with the id `mask`.\n",
"\n",
"See `xESMF` [documentation](https://pangeo-xesmf.readthedocs.io/en/latest/notebooks/Masking.html) for additonal details."
"See `xESMF` [documentation](https://pangeo-xesmf.readthedocs.io/en/latest/notebooks/Masking.html) for additonal details.\n"
]
},
{
Expand All @@ -830,32 +843,36 @@
}
],
"source": [
"ds['mask'] = xr.where(ds.tas.isel(time=0)<-10, 1, 0)\n",
"ds[\"mask\"] = xr.where(ds.tas.isel(time=0) < -10, 1, 0)\n",
"\n",
"masked_output = ds.regridder.horizontal('tas', output_grid, tool='xesmf', method='bilinear')\n",
"masked_output = ds.regridder.horizontal(\n",
" \"tas\", output_grid, tool=\"xesmf\", method=\"bilinear\"\n",
")\n",
"\n",
"fig, axes = plt.subplots(ncols=2, figsize=(18, 4))\n",
"\n",
"ds['mask'].plot(ax=axes[0], cmap='binary_r')\n",
"axes[0].set_title('Mask')\n",
"ds[\"mask\"].plot(ax=axes[0], cmap=\"binary_r\")\n",
"axes[0].set_title(\"Mask\")\n",
"\n",
"masked_output.tas.isel(time=0).plot(ax=axes[1])\n",
"axes[1].set_title('Masked output')\n",
"axes[1].set_title(\"Masked output\")\n",
"\n",
"plt.tight_layout()"
"plt.tight_layout()\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "99daebe0-0b0d-4a88-988a-5e2d685de80d",
"metadata": {},
"source": [
"## 6. Regridding using `regrid2`\n",
"\n",
"Related API: [xarray.Dataset.regridder.horizontal()](../generated/xarray.Dataset.regridder.horizontal.rst)\n",
"\n",
"`Regrid2` is a conservative regridder for rectilinear (lat/lon) grids originally from the `cdutil` package from `CDAT`.\n",
"\n",
"This regridder assumes constant latitude lines when generating weights."
"This regridder assumes constant latitude lines when generating weights.\n"
]
},
{
Expand Down Expand Up @@ -888,19 +905,19 @@
}
],
"source": [
"output = ds.regridder.horizontal('tas', output_grid, tool='regrid2')\n",
"output = ds.regridder.horizontal(\"tas\", output_grid, tool=\"regrid2\")\n",
"\n",
"fig, axes = plt.subplots(ncols=2, figsize=(16, 4))\n",
"\n",
"ds.tas.isel(time=0).plot(ax=axes[0])\n",
"\n",
"output.tas.isel(time=0).plot(ax=axes[1])"
"output.tas.isel(time=0).plot(ax=axes[1])\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.13 ('xcdat_dev')",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -914,7 +931,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.10.8"
},
"vscode": {
"interpreter": {
Expand Down
Loading
Loading