Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions Ch2_Intro_JypiterNotebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
]
},
Expand Down Expand Up @@ -143,7 +143,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.7.10"
}
},
"nbformat": 4,
Expand Down
68 changes: 44 additions & 24 deletions Ch4a_Python_Tools.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand All @@ -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"
]
},
Expand All @@ -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"
]
},
Expand All @@ -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"
]
},
{
Expand All @@ -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)"
]
},
{
Expand All @@ -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"
]
},
{
Expand All @@ -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"
]
},
{
Expand All @@ -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"
]
},
Expand All @@ -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."
]
},
Expand All @@ -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() "
]
},
{
Expand All @@ -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"
]
},
{
Expand Down Expand Up @@ -258,7 +278,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -272,7 +292,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.7.10"
}
},
"nbformat": 4,
Expand Down
Loading