-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some python notebook tutorials.
- Loading branch information
Showing
3 changed files
with
471 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Climate Network Tutorial\n", | ||
"\n", | ||
"Tutorial on analyzing climate networks using Python.\n", | ||
"\n", | ||
"Uses the Python packages ``core`` and ``climate`` providing all kinds of tools\n", | ||
"related to climate networks. Written as part of a diploma / phd thesis in\n", | ||
"Physics by Jonathan F. Donges (donges@pik-potsdam.de) at University of Potsdam\n", | ||
"/ Humboldt University Berlin and Potsdam Institute of Climate Impact Research\n", | ||
"(PIK),\n", | ||
"\n", | ||
"Copyright 2008-2018." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Download the data set and copy it to the path below or change the path to the location of the data set" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"DATA_FILENAME = \"../../../Daten/Reanalysis/NCEP-NCAR/air.mon.mean.nc\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Create ClimateData object containing the data and print information" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from pyunicorn import climate\n", | ||
"\n", | ||
"# Settings\n", | ||
"\n", | ||
"# Type of data file (\"NetCDF\" indicates a NetCDF file with data on a regular\n", | ||
"# lat-lon grid, \"iNetCDF\" allows for arbitrary grids - > see documentation).\n", | ||
"# For example, the \"NetCDF\" FILE_TYPE is compatible with data from the IPCC\n", | ||
"# AR4 model ensemble or the reanalysis data provided by NCEP/NCAR.\n", | ||
"FILE_TYPE = \"NetCDF\"\n", | ||
"\n", | ||
"# Indicate data source (optional)\n", | ||
"DATA_SOURCE = \"ncep_ncar_reanalysis\"\n", | ||
"\n", | ||
"# Name of observable in NetCDF file (\"air\" indicates surface air temperature\n", | ||
"# in NCEP/NCAR reanalysis data)\n", | ||
"OBSERVABLE_NAME = \"air\"\n", | ||
"\n", | ||
"# Select a subset in time and space from the data (e.g., a particular region\n", | ||
"# or a particular time window, or both)\n", | ||
"WINDOW = {\"time_min\": 0., \"time_max\": 0., \"lat_min\": 0, \"lon_min\": 0,\n", | ||
" \"lat_max\": 30, \"lon_max\": 0} # selects the whole data set\n", | ||
"\n", | ||
"# Indicate the length of the annual cycle in the data (e.g., 12 for monthly\n", | ||
"# data). This is used for calculating climatological anomaly values\n", | ||
"# correctly.\n", | ||
"TIME_CYCLE = 12\n", | ||
"\n", | ||
"data = climate.ClimateData.Load(file_name=DATA_FILENAME,\n", | ||
" observable_name=OBSERVABLE_NAME,\n", | ||
" data_source=DATA_SOURCE,\n", | ||
" file_type=FILE_TYPE,\n", | ||
" window=WINDOW,\n", | ||
" time_cycle=TIME_CYCLE)\n", | ||
"\n", | ||
"print(data)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Generate climate network using various procedures" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"scrolled": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Indicates whether to use only data from winter months (DJF) for calculating\n", | ||
"# correlations\n", | ||
"WINTER_ONLY = False\n", | ||
"\n", | ||
"# fixed threshold\n", | ||
"THRESHOLD = 0.5\n", | ||
"\n", | ||
"# fixed link density\n", | ||
"LINK_DENSITY = 0.005\n", | ||
"\n", | ||
"# Create a climate network based on Pearson correlation without lag and with\n", | ||
"# fixed threshold\n", | ||
"net = climate.TsonisClimateNetwork(data, threshold=THRESHOLD, winter_only=WINTER_ONLY)\n", | ||
"\n", | ||
"# Create a climate network based on Pearson correlation without lag and with\n", | ||
"# fixed link density\n", | ||
"# net = climate.TsonisClimateNetwork(data, link_density=LINK_DENSITY, winter_only=WINTER_ONLY)\n", | ||
"\n", | ||
"# Create a climate network based on Spearman's rank order correlation without\n", | ||
"# lag and with fixed threshold\n", | ||
"# net = climate.SpearmanClimateNetwork(data, threshold=THRESHOLD, winter_only=WINTER_ONLY)\n", | ||
"\n", | ||
"# Create a climate network based on mutual information without lag and with\n", | ||
"# fixed threshold\n", | ||
"# net = climate.MutualInfoClimateNetwork(data, threshold=THRESHOLD, winter_only=WINTER_ONLY)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Calculate some measures" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"print(\"Link density:\", net.link_density)\n", | ||
"\n", | ||
"# Get degree\n", | ||
"degree = net.degree()\n", | ||
"# Get closeness\n", | ||
"closeness = net.closeness()\n", | ||
"# Get betweenness\n", | ||
"betweenness = net.betweenness()\n", | ||
"# Get local clustering coefficient\n", | ||
"clustering = net.local_clustering()\n", | ||
"# Get average link distance\n", | ||
"ald = net.average_link_distance()\n", | ||
"# Get maximum link distance\n", | ||
"mld = net.max_link_distance()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Save results to text files" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"\n", | ||
"# Save the grid (mainly vertex coordinates) to text files\n", | ||
"data.grid.save_txt(filename=\"grid.txt\")\n", | ||
"\n", | ||
"# Save the degree sequence. Other measures may be saved similarly.\n", | ||
"np.savetxt(\"degree.txt\", degree)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Plot the results into the file \"climate_network_measures\". This only works if you have the pyngl package!" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"map_plots = climate.MapPlots(data.grid, DATA_SOURCE)\n", | ||
"\n", | ||
"# Add network measures to the plotting queue\n", | ||
"map_plots.add_dataset(\"Degree\", degree)\n", | ||
"map_plots.add_dataset(\"Closeness\", closeness)\n", | ||
"map_plots.add_dataset(\"Betweenness (log10)\", np.log10(betweenness + 1))\n", | ||
"map_plots.add_dataset(\"Clustering\", clustering)\n", | ||
"map_plots.add_dataset(\"Average link distance\", ald)\n", | ||
"map_plots.add_dataset(\"Maximum link distance\", mld)\n", | ||
"\n", | ||
"# Change the map projection\n", | ||
"map_plots.resources.mpProjection = \"Robinson\"\n", | ||
"map_plots.resources.mpCenterLonF = 0\n", | ||
"\n", | ||
"# Change the levels of contouring\n", | ||
"map_plots.resources.cnLevelSelectionMode = \"EqualSpacedLevels\"\n", | ||
"map_plots.resources.cnMaxLevelCount = 20\n", | ||
"\n", | ||
"# map_plots.resources.cnRasterSmoothingOn = True\n", | ||
"# map_plots.resources.cnFillMode = \"AreaFill\"\n", | ||
"\n", | ||
"map_plots.generate_map_plots(file_name=\"climate_network_measures\",\n", | ||
" title_on=False, labels_on=True)" | ||
] | ||
} | ||
], | ||
"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.6.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.