From acec3634ab6f46cd7c1b9a358ee64c05e593f729 Mon Sep 17 00:00:00 2001 From: Chelle Gentemann <35538868+cgentemann@users.noreply.github.com> Date: Tue, 19 Oct 2021 14:03:01 -0700 Subject: [PATCH] updates --- Ch2_Intro_JypiterNotebook.ipynb | 4 +- Ch4a_Python_Tools.ipynb | 68 +-- Ch4b_Plotting_Tools.ipynb | 706 +++++--------------------------- Ch6_Ocean_Example.ipynb | 290 +++++++------ Ch7_Atmosphere_Example.ipynb | 359 ++++++++++------ 5 files changed, 555 insertions(+), 872 deletions(-) diff --git a/Ch2_Intro_JypiterNotebook.ipynb b/Ch2_Intro_JypiterNotebook.ipynb index c62538d..df704d6 100644 --- a/Ch2_Intro_JypiterNotebook.ipynb +++ b/Ch2_Intro_JypiterNotebook.ipynb @@ -76,7 +76,7 @@ "source": [ "## __Test this:__\n", "### change the type of this cell between __Markdown__ and __Code__, and then run it to see the difference\n", - "myvar = 5+6\n", + "myvar = 5 + 6\n", "print(myvar)" ] }, @@ -143,7 +143,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.6" + "version": "3.7.10" } }, "nbformat": 4, diff --git a/Ch4a_Python_Tools.ipynb b/Ch4a_Python_Tools.ipynb index 3e4da01..d7ab952 100644 --- a/Ch4a_Python_Tools.ipynb +++ b/Ch4a_Python_Tools.ipynb @@ -26,13 +26,15 @@ "metadata": {}, "outputs": [], "source": [ + "# this library helps to make your code execution less messy\n", + "import warnings\n", + "\n", "import numpy as np\n", "import pandas as pd\n", "import xarray as xr\n", "\n", - "# this library helps to make your code execution less messy\n", - "import warnings\n", - "warnings.simplefilter('ignore') # filter some warning messages" + "xr.set_options(keep_attrs=True)\n", + "warnings.simplefilter(\"ignore\") # filter some warning messages" ] }, { @@ -54,8 +56,8 @@ "metadata": {}, "outputs": [], "source": [ - "ds = xr.open_dataset('./data/HadISST_sst_2000-2020.nc') # read a local netcdf file\n", - "ds.close() # close the file, so can be used by you or others. it is good practice.\n", + "ds = xr.open_dataset(\"./data/HadISST_sst_2000-2020.nc\") # read a local netcdf file\n", + "ds.close() # close the file, so can be used by you or others. it is good practice.\n", "ds # display the content of the dataset object" ] }, @@ -73,8 +75,10 @@ "outputs": [], "source": [ "# assign a string variable with the url address of the datafile\n", - "url = 'https://podaac-opendap.jpl.nasa.gov/opendap/allData/ghrsst/data/GDS2/L4/GLOB/CMC/CMC0.2deg/v2/2011/305/20111101120000-CMC-L4_GHRSST-SSTfnd-CMC0.2deg-GLOB-v02.0-fv02.0.nc'\n", - "ds_sst = xr.open_dataset(url) # reads the online file and display it the same way as local files\n", + "url = \"https://podaac-opendap.jpl.nasa.gov/opendap/allData/ghrsst/data/GDS2/L4/GLOB/CMC/CMC0.2deg/v2/2011/305/20111101120000-CMC-L4_GHRSST-SSTfnd-CMC0.2deg-GLOB-v02.0-fv02.0.nc\"\n", + "ds_sst = xr.open_dataset(\n", + " url\n", + ") # reads the online file and display it the same way as local files\n", "ds_sst" ] }, @@ -96,7 +100,7 @@ "metadata": {}, "outputs": [], "source": [ - "ds_sst.analysed_sst.plot() # note that we needed to choose one of the variable in the Dataset to be displayed" + "ds_sst.analysed_sst.plot() # note that we needed to choose one of the variable in the Dataset to be displayed" ] }, { @@ -105,7 +109,9 @@ "metadata": {}, "outputs": [], "source": [ - "ds.sst[0,:,:].plot() # in addition to choosing the variable, we choose a time to visualize the spatial data (lat, lon) at that time (zero or the first time entry)" + "ds.sst[\n", + " 0, :, :\n", + "].plot() # in addition to choosing the variable, we choose a time to visualize the spatial data (lat, lon) at that time (zero or the first time entry)" ] }, { @@ -123,7 +129,9 @@ "metadata": {}, "outputs": [], "source": [ - "ds.sst.mean(dim=['latitude','longitude']).plot() # we select a variable and average over spatial dimensions, and plot the final result" + "ds.sst.mean(\n", + " dim=[\"latitude\", \"longitude\"]\n", + ").plot() # we select a variable and average over spatial dimensions, and plot the final result" ] }, { @@ -141,7 +149,9 @@ "metadata": {}, "outputs": [], "source": [ - "ds.sst.sel(time=slice('2012-01-01','2013-12-31')).mean(dim=['time']).plot() # select a period of time" + "ds.sst.sel(time=slice(\"2012-01-01\", \"2013-12-31\")).mean(\n", + " dim=[\"time\"]\n", + ").plot() # select a period of time" ] }, { @@ -150,7 +160,9 @@ "metadata": {}, "outputs": [], "source": [ - "ds.sst.sel(latitude=slice(50,-50)).mean(dim=['time']).plot() # select a range of latitudes. \n", + "ds.sst.sel(latitude=slice(50, -50)).mean(\n", + " dim=[\"time\"]\n", + ").plot() # select a range of latitudes.\n", "# note that we need to go from 50 to -50 as the coordinate data goes from 90 to -90" ] }, @@ -167,7 +179,9 @@ "metadata": {}, "outputs": [], "source": [ - "ds_sst.analysed_sst.where(ds_sst.mask==1).plot() # we select, using .where, the data in the variable 'mask' that is equal to 1, \n", + "ds_sst.analysed_sst.where(\n", + " ds_sst.mask == 1\n", + ").plot() # we select, using .where, the data in the variable 'mask' that is equal to 1,\n", "# applied it to the variable 'analysed_sst', and plot the data. Try changing the value for mask - for example 2 is land, 8 is ice." ] }, @@ -186,10 +200,18 @@ "metadata": {}, "outputs": [], "source": [ - "# comparing 2015 and 2012 sea surface temperatures\n", - "(ds.sst.sel(time=slice('2015-01-01','2015-12-31')).mean(dim=['time'])\n", - "-ds.sst.sel(time=slice('2012-01-01','2012-12-31')).mean(dim=['time'])).plot() # note that in this case i could split the line in two\n", - "# makes it easier to read" + "# comparing 2012 and 2015 sea surface temperatures\n", + "ds2012 = ds.sst.sel(time=\"2012\").mean(dim=[\"time\"])\n", + "ds2015 = ds.sst.sel(time=\"2015\").mean(dim=[\"time\"])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "(ds2015 - ds2012).plot() " ] }, { @@ -214,11 +236,9 @@ "outputs": [], "source": [ "# same operation as before, minus the plotting method\n", - "my_ds = (ds.sst.sel(time=slice('2015-01-01','2015-12-31')).mean(dim=['time'])-ds.sst.sel(time=slice('2012-01-01','2012-12-31')).mean(dim=['time']))\n", - "# save the new dataset `my_ds` to a file in the directory data\n", - "my_ds.to_netcdf('./data/Global_SST_2015-2012.nc')\n", - "# explore the content of `my_ds`. note that the time dimension does not existe anymore\n", - "my_ds" + "my_ds = ds2015 - ds2012\n", + "my_ds.to_netcdf(\"./data/Global_SST_2015-2012.nc\") # save the new dataset `my_ds` to a file in the directory data\n", + "my_ds # explore the content of `my_ds`. note that the time dimension does not existe anymore" ] }, { @@ -258,7 +278,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -272,7 +292,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.6" + "version": "3.7.10" } }, "nbformat": 4, diff --git a/Ch4b_Plotting_Tools.ipynb b/Ch4b_Plotting_Tools.ipynb index 6a1df3b..857ed68 100644 --- a/Ch4b_Plotting_Tools.ipynb +++ b/Ch4b_Plotting_Tools.ipynb @@ -15,21 +15,23 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# basic libraries\n", + "import warnings\n", + "\n", + "import cartopy.crs as ccrs\n", + "\n", + "# necesary libraries for plotting\n", + "import matplotlib.pyplot as plt # note that in both cases we import one object within the library\n", "import numpy as np\n", "import pandas as pd\n", "import xarray as xr\n", "\n", - "# necesary libraries for plotting\n", - "import matplotlib.pyplot as plt # note that in both cases we import one object within the library\n", - "import cartopy.crs as ccrs\n", - "\n", - "import warnings\n", - "warnings.simplefilter('ignore') # filter some warning messages" + "xr.set_options(keep_attrs=True)\n", + "warnings.simplefilter(\"ignore\") # filter some warning messages" ] }, { @@ -43,518 +45,27 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
array([-3.30897331e-01, 4.54846382e-01, 3.00818443e-01, 1.29148483e-01,\n", - " 8.01112175e-01, -1.00877762e+00, -3.31048012e-01, -3.97064209e-01,\n", - " -3.38874817e-01, -3.19175720e-01, -4.45547104e-01, 1.49649620e-01,\n", - " 3.49483490e-01, 2.27182388e-01, 7.71281242e-01, 1.02271080e+00,\n", - " 1.36554623e+00, -8.92753601e-02, -1.98171616e-01, -3.17031860e-01,\n", - " -3.07672501e-01, -2.43781090e-01, -3.72982025e-01, -2.11442947e-01,\n", - " 6.04343414e-02, 1.31679535e-01, 2.71362305e-01, -1.52760506e-01,\n", - " 1.01775837e+00, 1.11868954e+00, -1.68485641e-01, -3.78059387e-01,\n", - " -3.19509506e-01, -2.73833275e-01, -6.25120163e-01, -6.94087982e-01,\n", - " -9.42012787e-01, -2.35974312e-01, -1.57097816e-01, 5.65128326e-02,\n", - " -8.33005905e-01, 7.19216347e-01, -1.31516457e-01, -1.88463211e-01,\n", - " -1.60152435e-01, -8.85782242e-02, 1.76785469e-01, -1.40716553e-01,\n", - " 3.80771637e-01, 4.51512337e-01, 4.52872276e-01, 7.53019333e-01,\n", - " 6.35172844e-01, 7.76444435e-01, -6.02464676e-02, -1.81871414e-01,\n", - " -1.54745102e-01, -1.44616127e-01, 9.26447868e-01, 7.74226189e-01,\n", - " 1.40981865e+00, 5.86309433e-01, 6.15759850e-01, 8.01628113e-01,\n", - " 5.05560875e-01, 1.79129314e+00, -4.17070389e-02, 1.00601196e-01,\n", - " -3.49731445e-02, -9.72738266e-02, 5.20937920e-01, 9.10547256e-01,\n", - " 1.43687248e-01, 2.08891869e-01, 3.59481812e-01, 1.13406658e+00,\n", - " 3.45243454e-01, -1.08886719e-01, -1.20860100e-01, 3.14006805e-02,\n", - " -1.38828278e-01, -1.95417404e-01, -5.52134514e-01, -6.87589645e-02,\n", - " -4.46586609e-02, 2.32618332e-01, -4.76045609e-02, -5.84993362e-02,\n", - " 1.43807125e+00, -3.24426651e-01, -1.87075615e-01, -3.28876495e-01,\n", - " -2.05194473e-01, -2.16888428e-01, 7.60424614e-01, -4.37872887e-01,\n", - " 5.76897621e-01, 8.21498871e-01, 9.83402252e-01, 5.02943993e-01,\n", - " -3.41018677e-01, -1.22421646e+00, -1.29365921e-01, -2.33255386e-01,\n", - " -2.19539642e-01, -2.35297203e-01, -3.17799568e-01, -2.10827827e-01,\n", - " 2.26475716e-01, -6.63522720e-01, -1.62494659e-01, -1.01596260e+00,\n", - " -1.25256920e+00, 2.66690254e-01, 2.68864632e-01, 1.79115295e-01,\n", - " 3.43954086e-01, -1.74370766e-01, -7.06786156e-01, -6.43065453e-01,\n", - " -1.00566292e+00, -7.40477562e-01, -5.96883774e-01, -1.25046825e+00,\n", - " -8.20542336e-01, -8.15141678e-01, -1.24084473e-01, -8.83731842e-02,\n", - " 5.24875641e-01, 3.83309364e-01, -4.90839005e-01, -8.28794479e-01,\n", - " -1.32228374e+00, -1.23059082e+00, -1.40923214e+00, -1.38831139e+00,\n", - " -1.43156815e+00, -9.79669571e-01, -2.33493805e-01, -2.24025726e-01,\n", - " -9.33818817e-02, 4.99567032e-01, 5.96946716e-01, -1.78184509e-02,\n", - " -1.02391911e+00, -4.07871246e-01, -1.15186119e+00, -5.45604706e-01,\n", - " -4.45263863e-01, 2.36883163e-02, -1.32392883e-01, 2.86050797e-01,\n", - " 5.06000519e-02, 4.54120636e-02, -7.22856522e-02, 3.20945740e-01,\n", - " 2.01607704e-01, 1.30550385e-01, -4.62534904e-01, -2.70747185e-01,\n", - " -1.08127594e-01, -9.84119415e-01, 1.46083832e-02, -7.34024048e-02,\n", - " -5.88932037e-02, -9.53493118e-02, 3.68225098e-01, 2.88352966e-02,\n", - " -3.86842728e-01, -5.89871407e-01, -2.30422020e-01, -4.67920303e-01,\n", - " -5.10611534e-01, -7.29843140e-01, 2.25609779e-01, 9.05000687e-01,\n", - " 1.43318176e-01, 5.00793457e-01, 6.71805382e-01, 6.15715027e-01,\n", - " 1.65065765e-01, -2.64510155e-01, -5.18951416e-02, -8.50521088e-01,\n", - " -5.83344460e-01, -3.63260269e-01, 1.63125992e-02, 1.28406525e-01,\n", - " 2.79020309e-01, 1.05409622e-01, 8.74489784e-01, 1.09649181e+00,\n", - " 2.77795792e-02, 5.51834106e-02, -2.36040115e-01, -3.56391907e-01,\n", - " -1.00806713e+00, -3.05541992e-01, 1.69708252e-01, 1.21448517e-01,\n", - " 1.39780045e-01, 3.89154434e-01, -5.84437370e-01, -6.20881081e-01,\n", - " -2.35655785e-01, 4.92454529e-01, 3.91243935e-01, 1.66019726e+00,\n", - " 1.48748493e+00, 1.15177441e+00, 5.53584099e-01, 1.09727859e-01,\n", - " 2.54249573e-02, 5.07860184e-02, 2.59017944e-03, 3.83799553e-01,\n", - " 1.38415432e+00, 7.78230667e-01, 5.61948776e-01, 1.53582478e+00,\n", - " 1.49139690e+00, 8.82372856e-01, 5.35593033e-02, 1.11961365e-03,\n", - " 5.51986694e-02, -2.03218460e-02, -2.22192764e-01, -3.59700203e-01,\n", - " 1.89393997e-01, -3.69663239e-01, 1.45672798e-01, -5.06082535e-01,\n", - " -8.53225708e-01, -3.05669785e-01, 3.61281395e-01, 2.76041031e-01,\n", - " 1.91179276e-01, 7.40022659e-02, -5.25636673e-02, 4.80451584e-01,\n", - " 1.76343918e-01, -6.84862137e-02, -3.47769737e-01, -7.32810974e-01,\n", - " -8.99991989e-01, 5.08675575e-01, 1.94899559e-01, 2.71512985e-01,\n", - " 2.78450012e-01, 5.64737320e-02, -4.55958366e-01, -5.26703835e-01],\n", - " dtype=float32)
array(['2000-01-16T12:00:00.000000000', '2000-02-15T00:00:00.000000000',\n", - " '2000-03-16T12:00:00.000000000', ..., '2020-10-16T12:00:00.000000000',\n", - " '2020-11-16T12:00:00.000000000', '2020-12-16T12:00:00.000000000'],\n", - " dtype='datetime64[ns]')
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5,\n", - " 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,\n", - " 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3,\n", - " 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8,\n", - " 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1,\n", - " 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6,\n", - " 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,\n", - " 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4,\n", - " 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9,\n", - " 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2,\n", - " 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7,\n", - " 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,\n", - " 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5,\n", - " 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,\n", - " 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])