diff --git a/notebooks/ocean_s_coordinate.ipynb b/notebooks/ocean_s_coordinate.ipynb index c251751..a16c028 100644 --- a/notebooks/ocean_s_coordinate.ipynb +++ b/notebooks/ocean_s_coordinate.ipynb @@ -3,26 +3,25 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "url = \"http://geoport.whoi.edu/thredds/dodsC/examples/bora_feb.nc\"" + "import dask\n", + "\n", + "dask.config.set(scheduler=\"single-threaded\")\n", + "\n", + "\n", + "url = \"https://geoport.whoi.edu/thredds/dodsC/examples/bora_feb.nc\"" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from netCDF4 import Dataset\n", - "\n", - "from odvc import get_formula_terms_variables\n", - "\n", + "from odvc.parse_formula_terms import get_formula_terms_variables\n", "\n", "nc = Dataset(url)\n", "\n", @@ -34,12 +33,10 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "from odvc import get_formula_terms\n", + "from odvc.parse_formula_terms import get_formula_terms\n", "\n", "formula_terms = get_formula_terms(var[0])\n", "\n", @@ -49,13 +46,10 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "from odvc import get_formula_terms_dims\n", - "\n", + "from odvc.parse_formula_terms import get_formula_terms_dims\n", "\n", "dims = get_formula_terms_dims(nc, formula_terms)\n", "\n", @@ -65,12 +59,10 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "from odvc import z_shape\n", + "from odvc.parse_formula_terms import z_shape\n", "\n", "new_shape = z_shape(nc, dims)\n", "\n", @@ -80,13 +72,10 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "from odvc import prepare_arrays\n", - "\n", + "from odvc.parse_formula_terms import prepare_arrays\n", "\n", "arrays = prepare_arrays(nc, formula_terms, new_shape)\n", "\n", @@ -96,9 +85,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "[(var, arr.shape) for var, arr in arrays.items()]" @@ -107,14 +94,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from odvc import ocean_s_coordinate\n", "\n", - "\n", "z = ocean_s_coordinate(\n", " arrays[\"s\"],\n", " arrays[\"eta\"],\n", @@ -130,15 +114,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "salt = nc.get_variables_by_attributes(long_name=\"averaged salinity\")[0]\n", - "temp = nc.get_variables_by_attributes(\n", - " long_name=\"averaged potential temperature\"\n", - ")[0]\n", + "temp = nc.get_variables_by_attributes(long_name=\"averaged potential temperature\")[0]\n", "\n", "s = salt[-1, :, 45, 137]\n", "t = temp[-1, :, 45, 137]\n", @@ -149,19 +129,14 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": false + "scrolled": false }, "outputs": [], "source": [ - "%matplotlib inline\n", - "\n", "import matplotlib.pyplot as plt\n", "import mpl_toolkits.axisartist as AA\n", "from mpl_toolkits.axes_grid1 import host_subplot\n", "\n", - "import seaborn\n", - "seaborn.set(style='ticks')\n", - "\n", "\n", "def adjust_xlim(ax, offset):\n", " x1, x2 = ax.get_xlim()\n", @@ -185,12 +160,12 @@ "ax1.axis[\"bottom\"].toggle(all=False)\n", "\n", "ax0.set_ylabel(\"Depth (m)\")\n", - "ax0.set_xlabel(u\"Temperature (\\xb0C)\")\n", + "ax0.set_xlabel(\"Temperature (\\xb0C)\")\n", "ax1.set_xlabel(r\"Salinity (g kg$^{-1}$)\")\n", "\n", "kw = dict(linewidth=2.5)\n", - "l0, = ax0.plot(t, -p, **kw)\n", - "l1, = ax1.plot(s, -p, **kw)\n", + "(l0,) = ax0.plot(t, -p, **kw)\n", + "(l1,) = ax1.plot(s, -p, **kw)\n", "\n", "adjust_xlim(ax0, offset=0.05)\n", "adjust_xlim(ax1, offset=0.05)\n", @@ -202,23 +177,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.8" + "pygments_lexer": "ipython3", + "version": "3.10.4" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/notebooks/ocean_s_coordinate_g1.ipynb b/notebooks/ocean_s_coordinate_g1.ipynb index fc5ec39..50dd4ab 100644 --- a/notebooks/ocean_s_coordinate_g1.ipynb +++ b/notebooks/ocean_s_coordinate_g1.ipynb @@ -3,11 +3,13 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ + "import dask\n", + "\n", + "dask.config.set(scheduler=\"single-threaded\")\n", + "\n", "url = (\n", " \"http://omgsrv1.meas.ncsu.edu:8080/thredds/dodsC/fmrc/sabgom/\"\n", " \"SABGOM_Forecast_Model_Run_Collection_best.ncd\"\n", @@ -17,15 +19,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from netCDF4 import Dataset\n", - "\n", - "from odvc import get_formula_terms_variables\n", - "\n", + "from odvc.parse_formula_terms import get_formula_terms_variables\n", "\n", "nc = Dataset(url)\n", "\n", @@ -37,9 +35,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from odvc import get_formula_terms\n", @@ -52,14 +48,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from odvc import get_formula_terms_dims\n", "\n", - "\n", "dims = get_formula_terms_dims(nc, formula_terms)\n", "\n", "dims" @@ -68,9 +61,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from odvc import z_shape\n", @@ -83,14 +74,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from odvc import prepare_arrays\n", "\n", - "\n", "arrays = prepare_arrays(nc, formula_terms, new_shape)\n", "\n", "arrays" @@ -99,9 +87,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "[(var, arr.shape) for var, arr in arrays.items()]" @@ -110,9 +96,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from odvc import ocean_s_coordinate_g1\n", @@ -127,15 +111,13 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "salt = nc.get_variables_by_attributes(standard_name=\"sea_water_salinity\")[0]\n", - "temp = nc.get_variables_by_attributes(\n", - " standard_name=\"sea_water_potential_temperature\"\n", - ")[0]\n", + "temp = nc.get_variables_by_attributes(standard_name=\"sea_water_potential_temperature\")[\n", + " 0\n", + "]\n", "\n", "s = salt[-1, :, 5, 364]\n", "t = temp[-1, :, 5, 364]\n", @@ -145,19 +127,17 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "\n", "import matplotlib.pyplot as plt\n", "import mpl_toolkits.axisartist as AA\n", + "import seaborn\n", "from mpl_toolkits.axes_grid1 import host_subplot\n", "\n", - "import seaborn\n", - "seaborn.set(style='ticks')\n", + "seaborn.set(style=\"ticks\")\n", "\n", "\n", "def adjust_xlim(ax, offset):\n", @@ -182,12 +162,12 @@ "ax1.axis[\"bottom\"].toggle(all=False)\n", "\n", "ax0.set_ylabel(\"Depth (m)\")\n", - "ax0.set_xlabel(u\"Temperature (\\xb0C)\")\n", + "ax0.set_xlabel(\"Temperature (\\xb0C)\")\n", "ax1.set_xlabel(r\"Salinity (g kg$^{-1}$)\")\n", "\n", "kw = dict(linewidth=2.5)\n", - "l0, = ax0.plot(t, -p, **kw)\n", - "l1, = ax1.plot(s, -p, **kw)\n", + "(l0,) = ax0.plot(t, -p, **kw)\n", + "(l1,) = ax1.plot(s, -p, **kw)\n", "\n", "adjust_xlim(ax0, offset=0.05)\n", "adjust_xlim(ax1, offset=0.05)\n", @@ -199,23 +179,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.8" + "pygments_lexer": "ipython3", + "version": "3.10.4" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/notebooks/ocean_s_coordinate_g2.ipynb b/notebooks/ocean_s_coordinate_g2.ipynb index f295bc0..6ada17c 100644 --- a/notebooks/ocean_s_coordinate_g2.ipynb +++ b/notebooks/ocean_s_coordinate_g2.ipynb @@ -3,11 +3,14 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ + "import dask\n", + "\n", + "dask.config.set(scheduler=\"single-threaded\")\n", + "\n", + "\n", "url = (\n", " \"http://dm2.caricoos.org/thredds/dodsC/content/roms/\"\n", " \"20150930/r1/ocean_his_0121.nc\"\n", @@ -17,15 +20,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from netCDF4 import Dataset\n", - "\n", - "from odvc import get_formula_terms_variables\n", - "\n", + "from odvc.parse_formula_terms import get_formula_terms_variables\n", "\n", "nc = Dataset(url)\n", "\n", @@ -37,9 +36,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from odvc import get_formula_terms\n", @@ -52,14 +49,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from odvc import get_formula_terms_dims\n", "\n", - "\n", "dims = get_formula_terms_dims(nc, formula_terms)\n", "\n", "dims" @@ -68,9 +62,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from odvc import z_shape\n", @@ -83,14 +75,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from odvc import prepare_arrays\n", "\n", - "\n", "arrays = prepare_arrays(nc, formula_terms, new_shape)\n", "\n", "arrays" @@ -99,9 +88,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "[(var, arr.shape) for var, arr in arrays.items()]" @@ -110,9 +97,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from odvc import ocean_s_coordinate_g1\n", @@ -127,9 +112,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "salt = nc.get_variables_by_attributes(long_name=\"salinity\")[0]\n", @@ -143,20 +126,13 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "%matplotlib inline\n", - "\n", "import matplotlib.pyplot as plt\n", "import mpl_toolkits.axisartist as AA\n", "from mpl_toolkits.axes_grid1 import host_subplot\n", "\n", - "import seaborn\n", - "seaborn.set(style='ticks')\n", - "\n", "\n", "def adjust_xlim(ax, offset):\n", " x1, x2 = ax.get_xlim()\n", @@ -180,12 +156,12 @@ "ax1.axis[\"bottom\"].toggle(all=False)\n", "\n", "ax0.set_ylabel(\"Depth (m)\")\n", - "ax0.set_xlabel(u\"Temperature (\\xb0C)\")\n", + "ax0.set_xlabel(\"Temperature (\\xb0C)\")\n", "ax1.set_xlabel(r\"Salinity (g kg$^{-1}$)\")\n", "\n", "kw = dict(linewidth=2.5)\n", - "l0, = ax0.plot(t, -p, **kw)\n", - "l1, = ax1.plot(s, -p, **kw)\n", + "(l0,) = ax0.plot(t, -p, **kw)\n", + "(l1,) = ax1.plot(s, -p, **kw)\n", "\n", "adjust_xlim(ax0, offset=0.05)\n", "adjust_xlim(ax1, offset=0.05)\n", @@ -197,23 +173,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.8" + "pygments_lexer": "ipython3", + "version": "3.10.4" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/notebooks/ocean_sigma_coordinate-FVCOM.ipynb b/notebooks/ocean_sigma_coordinate-FVCOM.ipynb index f08dbdb..dfa1c80 100644 --- a/notebooks/ocean_sigma_coordinate-FVCOM.ipynb +++ b/notebooks/ocean_sigma_coordinate-FVCOM.ipynb @@ -3,11 +3,14 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ + "import dask\n", + "\n", + "dask.config.set(scheduler=\"single-threaded\")\n", + "\n", + "\n", "url = (\n", " \"http://www.smast.umassd.edu:8080/thredds/dodsC/FVCOM/NECOFS/\"\n", " \"Forecasts/NECOFS_GOM3_FORECAST.nc\"\n", @@ -17,15 +20,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from netCDF4 import Dataset\n", - "\n", - "from odvc import get_formula_terms_variables\n", - "\n", + "from odvc.parse_formula_terms import get_formula_terms_variables\n", "\n", "nc = Dataset(url)\n", "\n", @@ -37,12 +36,10 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "from odvc import get_formula_terms\n", + "from odvc.parse_formula_terms import get_formula_terms\n", "\n", "formula_terms = get_formula_terms(var[0])\n", "\n", @@ -52,13 +49,10 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "from odvc import get_formula_terms_dims\n", - "\n", + "from odvc.parse_formula_terms import get_formula_terms_dims\n", "\n", "dims = get_formula_terms_dims(nc, formula_terms)\n", "\n", @@ -68,12 +62,10 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "from odvc import z_shape\n", + "from odvc.parse_formula_terms import z_shape\n", "\n", "new_shape = z_shape(nc, dims)\n", "\n", @@ -83,12 +75,10 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "from odvc import prepare_arrays\n", + "from odvc.parse_formula_terms import prepare_arrays\n", "\n", "arrays = prepare_arrays(nc, formula_terms, new_shape)\n", "\n", @@ -98,14 +88,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from odvc import ocean_sigma_coordinate\n", "\n", - "\n", "z = ocean_sigma_coordinate(arrays[\"sigma\"], arrays[\"eta\"], arrays[\"depth\"])\n", "print(z.shape)" ] @@ -113,15 +100,13 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "salt = nc.get_variables_by_attributes(standard_name=\"sea_water_salinity\")[0]\n", - "temp = nc.get_variables_by_attributes(\n", - " standard_name=\"sea_water_potential_temperature\"\n", - ")[0]\n", + "temp = nc.get_variables_by_attributes(standard_name=\"sea_water_potential_temperature\")[\n", + " 0\n", + "]\n", "\n", "s = salt[-1, :, 175]\n", "t = temp[-1, :, 175]\n", @@ -131,20 +116,13 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "%matplotlib inline\n", - "\n", "import matplotlib.pyplot as plt\n", "import mpl_toolkits.axisartist as AA\n", "from mpl_toolkits.axes_grid1 import host_subplot\n", "\n", - "import seaborn\n", - "seaborn.set(style='ticks')\n", - "\n", "\n", "def adjust_xlim(ax, offset):\n", " x1, x2 = ax.get_xlim()\n", @@ -168,12 +146,12 @@ "ax1.axis[\"bottom\"].toggle(all=False)\n", "\n", "ax0.set_ylabel(\"Depth (m)\")\n", - "ax0.set_xlabel(u\"Temperature (\\xb0C)\")\n", + "ax0.set_xlabel(\"Temperature (\\xb0C)\")\n", "ax1.set_xlabel(r\"Salinity (g kg$^{-1}$)\")\n", "\n", "kw = dict(linewidth=2.5)\n", - "l0, = ax0.plot(t, -p, **kw)\n", - "l1, = ax1.plot(s, -p, **kw)\n", + "(l0,) = ax0.plot(t, -p, **kw)\n", + "(l1,) = ax1.plot(s, -p, **kw)\n", "\n", "adjust_xlim(ax0, offset=0.05)\n", "adjust_xlim(ax1, offset=0.05)\n", @@ -185,23 +163,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.8" + "pygments_lexer": "ipython3", + "version": "3.10.4" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/notebooks/ocean_sigma_coordinate-NYHOPS.ipynb b/notebooks/ocean_sigma_coordinate-NYHOPS.ipynb index 5802b2b..694d8a1 100644 --- a/notebooks/ocean_sigma_coordinate-NYHOPS.ipynb +++ b/notebooks/ocean_sigma_coordinate-NYHOPS.ipynb @@ -3,29 +3,25 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "url = (\n", - " \"http://colossus.dl.stevens-tech.edu:8080/thredds/dodsC/\"\n", - " \"latest/Bight_gcmplt.nc\"\n", - ")" + "import dask\n", + "\n", + "dask.config.set(scheduler=\"single-threaded\")\n", + "\n", + "\n", + "url = \"http://colossus.dl.stevens-tech.edu:8080/thredds/dodsC/\" \"latest/Bight_gcmplt.nc\"" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from netCDF4 import Dataset\n", - "\n", - "from odvc import get_formula_terms_variables\n", - "\n", + "from odvc.parse_formula_terms import get_formula_terms_variables\n", "\n", "nc = Dataset(url)\n", "\n", @@ -37,9 +33,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from odvc import get_formula_terms\n", @@ -52,14 +46,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from odvc import get_formula_terms_dims\n", "\n", - "\n", "dims = get_formula_terms_dims(nc, formula_terms)\n", "\n", "dims" @@ -68,9 +59,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from odvc import z_shape\n", @@ -83,14 +72,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from odvc import prepare_arrays\n", "\n", - "\n", "arrays = prepare_arrays(nc, formula_terms, new_shape)\n", "\n", "arrays" @@ -99,9 +85,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "[(var, arr.shape) for var, arr in arrays.items()]" @@ -110,9 +94,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from odvc import ocean_sigma_coordinate\n", @@ -125,9 +107,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "salt = nc.get_variables_by_attributes(standard_name=\"sea_water_salinity\")[0]\n", @@ -141,20 +121,13 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ - "%matplotlib inline\n", - "\n", "import matplotlib.pyplot as plt\n", "import mpl_toolkits.axisartist as AA\n", "from mpl_toolkits.axes_grid1 import host_subplot\n", "\n", - "import seaborn\n", - "seaborn.set(style='ticks')\n", - "\n", "\n", "def adjust_xlim(ax, offset):\n", " x1, x2 = ax.get_xlim()\n", @@ -178,12 +151,12 @@ "ax1.axis[\"bottom\"].toggle(all=False)\n", "\n", "ax0.set_ylabel(\"Depth (m)\")\n", - "ax0.set_xlabel(u\"Temperature (\\xb0C)\")\n", + "ax0.set_xlabel(\"Temperature (\\xb0C)\")\n", "ax1.set_xlabel(r\"Salinity (g kg$^{-1}$)\")\n", "\n", "kw = dict(linewidth=2.5)\n", - "l0, = ax0.plot(t, -p, **kw)\n", - "l1, = ax1.plot(s, -p, **kw)\n", + "(l0,) = ax0.plot(t, -p, **kw)\n", + "(l1,) = ax1.plot(s, -p, **kw)\n", "\n", "adjust_xlim(ax0, offset=0.05)\n", "adjust_xlim(ax1, offset=0.05)\n", @@ -195,23 +168,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.8" + "pygments_lexer": "ipython3", + "version": "3.10.4" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/odvc/parse_formula_terms.py b/odvc/parse_formula_terms.py index f76471a..e902794 100644 --- a/odvc/parse_formula_terms.py +++ b/odvc/parse_formula_terms.py @@ -47,7 +47,7 @@ def get_formula_terms_dims(nc, formula_terms): def z_shape(nc, dims): - """Return the vertical coordinate `shape` based on the combined dimensions of the formula_terms `dims`.""" + """Return the vertical coordinate `shape` based on the formula_terms `dims`.""" all_dims = ( dims.get("eta"), dims.get("depth"), diff --git a/tests/test_ocean_s_coordinate_g1.py b/tests/test_ocean_s_coordinate_g1.py index 3137d3e..bd50ceb 100644 --- a/tests/test_ocean_s_coordinate_g1.py +++ b/tests/test_ocean_s_coordinate_g1.py @@ -1,10 +1,10 @@ -"""Test ocean_s_coordinate_g1""" +"""Test ocean_s_coordinate_g1.""" -import os -import unittest +from pathlib import Path import dask.array as da import numpy as np +import pytest from netCDF4 import Dataset from odvc import ocean_s_coordinate_g1 @@ -16,18 +16,19 @@ z_shape, ) -data_path = os.path.join(os.path.dirname(__file__), "data") +data_path = Path(__file__).parent.resolve().joinpath("data") -class OceanSCoordinateG1(unittest.TestCase): - def setUp(self): - roms = "ocean_s_coordinate_g1_roms.nc" - self.nc = Dataset(os.path.join(data_path, roms)) - formula_terms_variable = get_formula_terms_variables(self.nc)[0] +@pytest.fixture +def setup(): + """Set all the formula terms objects up for the tests.""" + roms = "ocean_s_coordinate_g1_roms.nc" + with Dataset(data_path.joinpath(roms)) as nc: + formula_terms_variable = get_formula_terms_variables(nc)[0] formula_terms = get_formula_terms(formula_terms_variable) - dims = get_formula_terms_dims(self.nc, formula_terms) - new_shape = z_shape(self.nc, dims) - arrays = prepare_arrays(self.nc, formula_terms, new_shape) + dims = get_formula_terms_dims(nc, formula_terms) + new_shape = z_shape(nc, dims) + arrays = prepare_arrays(nc, formula_terms, new_shape) s = arrays["s"] c = arrays["C"] @@ -35,61 +36,67 @@ def setUp(self): depth = arrays["depth"] depth_c = arrays["depth_c"] - self.z = ocean_s_coordinate_g1(s, c, eta, depth, depth_c) - self.sliced = self.z[0, :, 30, 80] - - def tearDown(self): - self.nc.close() - unittest.TestCase.tearDown(self) - - def test_shape(self): - assert self.z.shape == (1, 36, 82, 130) - - def test_slice(self): - assert isinstance(self.sliced, da.Array) - - def test_slice_ndarray(self): - assert isinstance(self.sliced.compute(), np.ndarray) - - def test_z_values(self): - z_comp = np.array( - [ - -2531.46656205, - -2337.58793694, - -2167.67949235, - -2018.28088531, - -1886.27977235, - -1768.84270488, - -1663.35273478, - -1567.35533466, - -1478.516278, - -1394.59779732, - -1313.46227081, - -1233.11474357, - -1151.79449631, - -1068.11831429, - -981.26114252, - -891.13435155, - -798.4970414, - -704.92970128, - -612.62956129, - -524.05182095, - -441.49011516, - -366.71960543, - -300.79609284, - -244.03541034, - -196.13254143, - -156.34993361, - -123.71062861, - -97.1566744, - -75.65879794, - -58.28034096, - -44.20608553, - -32.74776123, - -23.33605136, - -15.50607592, - -8.88076055, - -3.15458049, - ], - ) - np.testing.assert_allclose(self.sliced.compute(), z_comp) + z = ocean_s_coordinate_g1(s, c, eta, depth, depth_c) + sliced = z[0, :, 30, 80] + yield {"z": z, "sliced": sliced} + + +def test_shape(setup): + """Assert that the calculated shape is correct.""" + expected = (1, 36, 82, 130) + assert setup["z"].shape == expected + + +def test_slice(setup): + """Assert that the slice is a dask array.""" + assert isinstance(setup["sliced"], da.Array) + + +def test_slice_ndarray(setup): + """Assert that theslice is a numpy array.""" + assert isinstance(setup["sliced"].compute(), np.ndarray) + + +def test_z_values(setup): + """Assert that the calculated z values are correct.""" + z_comp = np.array( + [ + -2531.46656205, + -2337.58793694, + -2167.67949235, + -2018.28088531, + -1886.27977235, + -1768.84270488, + -1663.35273478, + -1567.35533466, + -1478.516278, + -1394.59779732, + -1313.46227081, + -1233.11474357, + -1151.79449631, + -1068.11831429, + -981.26114252, + -891.13435155, + -798.4970414, + -704.92970128, + -612.62956129, + -524.05182095, + -441.49011516, + -366.71960543, + -300.79609284, + -244.03541034, + -196.13254143, + -156.34993361, + -123.71062861, + -97.1566744, + -75.65879794, + -58.28034096, + -44.20608553, + -32.74776123, + -23.33605136, + -15.50607592, + -8.88076055, + -3.15458049, + ], + ) + np.testing.assert_allclose(setup["sliced"].compute(), z_comp) diff --git a/tests/test_parse_formula_terms.py b/tests/test_parse_formula_terms.py index 4fa6b90..a3676b0 100644 --- a/tests/test_parse_formula_terms.py +++ b/tests/test_parse_formula_terms.py @@ -1,10 +1,11 @@ -import os -import unittest +"""Test parse formula terms.""" + +from pathlib import Path import dask.array as da -import netCDF4 import numpy as np import pytest +from netCDF4 import Dataset from odvc.parse_formula_terms import ( get_formula_terms, @@ -14,60 +15,60 @@ z_shape, ) -# py2/3 basestring compat. -try: - basestring -except NameError: - basestring = str +data_path = Path(__file__).parent.resolve().joinpath("data") -data_path = os.path.join(os.path.dirname(__file__), "data") +@pytest.fixture +def setup(): + """Set all the formula terms and arrays objects up for the tests.""" + roms = "ocean_s_coordinate_g1_roms.nc" + with Dataset(data_path.joinpath(roms)) as nc: + formula_terms_variable = get_formula_terms_variables(nc)[0] + formula_terms = get_formula_terms(formula_terms_variable) + dims = get_formula_terms_dims(nc, formula_terms) + new_shape = z_shape(nc, dims) + arrays = prepare_arrays(nc, formula_terms, new_shape) + yield { + "formula_terms_variable": formula_terms_variable, + "formula_terms": formula_terms, + "dims": dims, + "new_shape": new_shape, + "arrays": arrays, + } -class UtilTests(unittest.TestCase): - def setUp(self): - roms = "ocean_s_coordinate_g1_roms.nc" - self.nc = netCDF4.Dataset(os.path.join(data_path, roms)) - self.formula_terms_variables = get_formula_terms_variables(self.nc) - self.formula_terms_variable = self.formula_terms_variables[0] - self.formula_terms = get_formula_terms(self.formula_terms_variable) - self.dims = get_formula_terms_dims(self.nc, self.formula_terms) - self.new_shape = z_shape(self.nc, self.dims) - self.arrays = prepare_arrays(self.nc, self.formula_terms, self.new_shape) +def test_formula_terms_variables(setup): + """Assert that the formula_terms_variables are correctly identified.""" + assert hasattr(setup["formula_terms_variable"], "formula_terms") + assert setup["formula_terms_variable"].standard_name == "ocean_s_coordinate_g1" + for k, v in setup["formula_terms"].items(): + assert isinstance(v, str) - def tearDown(self): - self.nc.close() - unittest.TestCase.tearDown(self) - def test_no_formula_terms_variables(self): - no_formula_term = "no_formula_terms.nc" - with netCDF4.Dataset(os.path.join(data_path, no_formula_term)) as nc: - with pytest.raises(ValueError): - get_formula_terms_variables(nc) +def test_get_formula_terms_and_dims(setup): + """Assert that the formula_terms are correctly identified.""" + assert isinstance(setup["formula_terms"], dict) + s = set(setup["formula_terms"].keys()) + assert s.issubset(["s", "C", "eta", "depth", "depth_c"]) - def test_formula_terms_variables(self): - assert len(self.formula_terms_variables) == 1 - assert hasattr(self.formula_terms_variable, "formula_terms") - assert self.formula_terms_variable.standard_name == "ocean_s_coordinate_g1" + dims = {"s": 1, "C": 1, "eta": 3, "depth": 2, "depth_c": 0} + for k, v in setup["dims"].items(): + assert dims[k] == len(v) - def test_formula_terms_isstr(self): - for k, v in self.formula_terms.items(): - assert isinstance(v, basestring) - def test_get_formula_terms(self): - assert isinstance(self.formula_terms, dict) - s = set(self.formula_terms.keys()) - assert s.issubset(["s", "C", "eta", "depth", "depth_c"]) +def test_arrays(setup): + """Assert that the arrays are the right obj instance.""" + for k, v in setup["arrays"].items(): + if hasattr(v, "compute"): + assert isinstance(v, da.Array) + assert isinstance(v.compute(), np.ndarray) + else: + assert isinstance(v, np.ndarray) - def test_get_formula_terms_dims(self): - dims = {"s": 1, "C": 1, "eta": 3, "depth": 2, "depth_c": 0} - for k, v in self.dims.items(): - assert dims[k] == len(v) - def test_arrays(self): - for k, v in self.arrays.items(): - if hasattr(v, "compute"): - assert isinstance(v, da.Array) - assert isinstance(v.compute(), np.ndarray) - else: - assert isinstance(v, np.ndarray) +def test_no_formula_terms_variables(): + """Test if it will fail as expected when formula terms are missing.""" + no_formula_term = "no_formula_terms.nc" + with Dataset(data_path.joinpath(no_formula_term)) as nc: + with pytest.raises(ValueError): + get_formula_terms_variables(nc)