Skip to content

Instantly share code, notes, and snippets.

@ccarouge
Last active May 18, 2020 04:26
Show Gist options
  • Save ccarouge/09ed81db7759f92f9f0eb42ac2feb7c5 to your computer and use it in GitHub Desktop.
Save ccarouge/09ed81db7759f92f9f0eb42ac2feb7c5 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The idea is to create an empty array on the whole grid and then merge with the land-only array. This creates a dataset with the 2 arrays \"expanded\" with NaNs on the additional points."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import xarray as xr\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ds = xr.open_dataset(\"/g/data/w35/lt0205/research/lpj_guess/runs/CRUNCEP/agpp_LPJ-GUESS_1901-2015.nc\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ds"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Longitude and Latitude resolution\n",
"dx = ds.Lon - ds.Lon.shift(shifts={'Lon':1})\n",
"dx = dx.min()\n",
"dy = ds.Lat - ds.Lat.shift(shifts={'Lat':1})\n",
"dy = dy.min()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"newlon = np.arange((-180.+dx/2.),180.,dx)\n",
"newlon = xr.DataArray(newlon, dims=(\"Lon\"),coords={\"Lon\":newlon},\n",
" attrs=ds.Lon.attrs)\n",
"newlon"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"newlat = np.arange((-90.+dy/2.),90., dy)\n",
"newlat = xr.DataArray(newlat, dims=(\"Lat\"), coords={\"Lat\":newlat}, \n",
" attrs=ds.Lat.attrs)\n",
"newlat"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"foo = xr.DataArray(np.empty((newlat.size, newlon.size)),\n",
" dims=(\"Lat\", \"Lon\"),\n",
" coords={\"Lat\":newlat, \"Lon\":newlon},\n",
" name=\"foo\")\n",
"foo[:]=np.NaN\n",
"foo"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ds2 = ds.broadcast_like(foo)\n",
"ds2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ds2.Total.sel(Time=\"1910\").plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment