Skip to content

Instantly share code, notes, and snippets.

@navidcy
Created March 13, 2019 08:51
Show Gist options
  • Save navidcy/2535802336124297522d1f3fe5250cec to your computer and use it in GitHub Desktop.
Save navidcy/2535802336124297522d1f3fe5250cec to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"%config InlineBackend.figure_format='retina'"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import xarray as xr\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from xgcm import Grid"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<xarray.Dataset>\n",
"Dimensions: (Time: 48, xh: 800, xq: 800, yh: 400, yq: 400, zi: 3, zl: 2)\n",
"Coordinates:\n",
" * xq (xq) float64 -1.596e+03 -1.592e+03 -1.588e+03 ... 1.596e+03 1.6e+03\n",
" * yh (yh) float64 -798.0 -794.0 -790.0 -786.0 ... 790.0 794.0 798.0\n",
" * zl (zl) float64 1.027e+03 1.028e+03\n",
" * xh (xh) float64 -1.598e+03 -1.594e+03 ... 1.594e+03 1.598e+03\n",
" * yq (yq) float64 -796.0 -792.0 -788.0 -784.0 ... 792.0 796.0 800.0\n",
" * zi (zi) float64 1.027e+03 1.028e+03 1.028e+03\n",
" * Time (Time) float64 600.0 600.1 600.1 600.2 ... 601.9 601.9 601.9 602.0\n",
"Data variables:\n",
" u (Time, zl, yh, xq) float32 dask.array<shape=(48, 2, 400, 800), chunksize=(24, 2, 400, 800)>\n",
" v (Time, zl, yq, xh) float32 dask.array<shape=(48, 2, 400, 800), chunksize=(24, 2, 400, 800)>\n",
" h (Time, zl, yh, xh) float32 dask.array<shape=(48, 2, 400, 800), chunksize=(24, 2, 400, 800)>\n",
" e (Time, zi, yh, xh) float32 dask.array<shape=(48, 3, 400, 800), chunksize=(24, 3, 400, 800)>\n",
" uh (Time, zl, yh, xq) float32 dask.array<shape=(48, 2, 400, 800), chunksize=(24, 2, 400, 800)>\n",
" vh (Time, zl, yq, xh) float32 dask.array<shape=(48, 2, 400, 800), chunksize=(24, 2, 400, 800)>\n",
"Attributes:\n",
" filename: prog.nc\n",
" title: Navid Channel\n",
" grid_type: regular\n",
" grid_tile: N/A\n"
]
}
],
"source": [
"# Now plot mean interface height\n",
"FileList = '/home/552/nc3020/gdata/SOchanBcBtEddySat/layer2/layer2_tau5e-3_manyshortridges/output[6]*/prog.nc'\n",
"dsx = xr.open_mfdataset(FileList, concat_dim='Time', decode_times=False, engine='netcdf4')\n",
"dsx.coords['Time'] = dsx.coords['Time']/(365)\n",
"print(dsx)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "__init__() got an unexpected keyword argument 'coords'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-5-8c7b97904d6f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mgrid\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mGrid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdsx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcoords\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m'X'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'center'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'xh'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'right'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'xq'\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Y'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'center'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'yh'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'right'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'yq'\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: __init__() got an unexpected keyword argument 'coords'"
]
}
],
"source": [
"grid = Grid(dsx, coords={'X': {'center': 'xh', 'right': 'xq'}, 'Y': {'center': 'yh', 'right': 'yq'}})"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<xgcm.Grid>\n",
"X Axis (periodic):\n",
" * center xh (800) --> right\n",
" * right xq (800) --> center\n",
"Y Axis (not periodic):\n",
" * center yh (400) --> right\n",
" * right yq (400) --> center"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dsx.coords['xh'].attrs.update(axis='X')\n",
"dsx.coords['xq'].attrs.update(axis='X', c_grid_axis_shift=0.5)\n",
"dsx.coords['yh'].attrs.update(axis='Y')\n",
"dsx.coords['yq'].attrs.update(axis='Y', c_grid_axis_shift=0.5)\n",
"\n",
"grid = Grid(dsx, periodic=['X'])\n",
"\n",
"grid"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [default]",
"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.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment