Skip to content

Commit fe7db11

Browse files
author
Ben Mather
committed
Added numpy+scipy and mapping notebooks
1 parent 5e7c9e9 commit fe7db11

18 files changed

+13819
-0
lines changed

CourseContent/Notebooks/Mapping/0-Preliminaries.ipynb

Lines changed: 844 additions & 0 deletions
Large diffs are not rendered by default.

CourseContent/Notebooks/Mapping/1-Introducting Cartopy.ipynb

Lines changed: 250 additions & 0 deletions
Large diffs are not rendered by default.

CourseContent/Notebooks/Mapping/2-Images and GeoTIFFs.ipynb

Lines changed: 303 additions & 0 deletions
Large diffs are not rendered by default.

CourseContent/Notebooks/Mapping/3-Working with real data.ipynb

Lines changed: 294 additions & 0 deletions
Large diffs are not rendered by default.

CourseContent/Notebooks/Mapping/4-Contouring Global Data.ipynb

Lines changed: 279 additions & 0 deletions
Large diffs are not rendered by default.

CourseContent/Notebooks/Mapping/5-WorkingWithPointData.ipynb

Lines changed: 580 additions & 0 deletions
Large diffs are not rendered by default.

CourseContent/Notebooks/Mapping/6-WorkingWithShuttleRadarTopography.ipynb

Lines changed: 536 additions & 0 deletions
Large diffs are not rendered by default.

CourseContent/Notebooks/Mapping/7-WorkingWithOn-DemandMappingServices.ipynb

Lines changed: 536 additions & 0 deletions
Large diffs are not rendered by default.

CourseContent/Notebooks/Mapping/8-GlobalPlateMotionsEtc.ipynb

Lines changed: 467 additions & 0 deletions
Large diffs are not rendered by default.

CourseContent/Notebooks/Mapping/9-HimalayaRegionMapsAndImages.ipynb

Lines changed: 505 additions & 0 deletions
Large diffs are not rendered by default.

CourseContent/Notebooks/Mapping/9a-HimalayaRegionMapsAndImages-Pt2.ipynb

Lines changed: 695 additions & 0 deletions
Large diffs are not rendered by default.

CourseContent/Notebooks/Numpy+Scipy/1-IntroductionToNumpy.ipynb

Lines changed: 4012 additions & 0 deletions
Large diffs are not rendered by default.

CourseContent/Notebooks/Numpy+Scipy/2-IntroductionToScipy.ipynb

Lines changed: 2328 additions & 0 deletions
Large diffs are not rendered by default.

CourseContent/Notebooks/Numpy+Scipy/3-ScipyInterpolate.ipynb

Lines changed: 430 additions & 0 deletions
Large diffs are not rendered by default.

CourseContent/Notebooks/Numpy+Scipy/4-ScipySpatialAndMeshing.ipynb

Lines changed: 490 additions & 0 deletions
Large diffs are not rendered by default.

CourseContent/Notebooks/Numpy+Scipy/5-ScipyOptimize.ipynb

Lines changed: 342 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Data storage\n",
8+
"\n",
9+
"Python provides file read write and object serialisation / reconstruction (python pickle module). `numpy` provides methods for storing and retrieving structured arrays quickly and efficiently (including data compression). `scipy` provides some helper functions for common file formats such as `netcdf` and `matlab` etc etc.\n",
10+
"\n",
11+
"Sometimes data are hard won - and reformatting them into easily retrieved files can be a lifesaver."
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 2,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"import numpy as np\n",
21+
"from scipy.io import netcdf"
22+
]
23+
},
24+
{
25+
"cell_type": "markdown",
26+
"metadata": {},
27+
"source": [
28+
"## text-based data files\n",
29+
"\n",
30+
"Completely portable and human readable, text-based formats are common outputs from simple scripted programs, web searches, program logs etc. Reading and writing them is trivial, and it is easy to append information to a file. The only problem is that the conversion can be extremely slow. \n",
31+
"\n",
32+
"This example is taken from our mapping exercise and shows the benefit of converting data to binary formats.\n",
33+
"\n",
34+
"---\n",
35+
"\n",
36+
"```python\n",
37+
"\n",
38+
"# Seafloor age data and global image - data from Earthbyters\n",
39+
"\n",
40+
"# The data come as ascii lon / lat / age tuples with NaN for no data. \n",
41+
"# This can be loaded with ...\n",
42+
"\n",
43+
"age = np.loadtxt(\"global_age_data.3.6.xyz\")\n",
44+
"age_data = age.reshape(1801,3601,3) # I looked at the data and figured out what numbers to use\n",
45+
"age_img = age_data[:,:,2]\n",
46+
"\n",
47+
"# But this is super slow, so I have just stored the Age data on the grid (1801 x 3601) which we can reconstruct easily\n",
48+
"\n",
49+
"datasize = (1801, 3601, 3)\n",
50+
"age_data = np.empty(datasize)\n",
51+
"\n",
52+
"ages = np.load(\"global_age_data.3.6.z.npz\")[\"ageData\"]\n",
53+
"\n",
54+
"lats = np.linspace(90, -90, datasize[0])\n",
55+
"lons = np.linspace(-180.0,180.0, datasize[1])\n",
56+
"\n",
57+
"arrlons,arrlats = np.meshgrid(lons, lats)\n",
58+
"\n",
59+
"age_data[...,0] = arrlons[...]\n",
60+
"age_data[...,1] = arrlats[...]\n",
61+
"age_data[...,2] = ages[...]\n",
62+
"```\n",
63+
"\n",
64+
"\n",
65+
"---\n",
66+
"\n",
67+
"The timing comparison is astonishing\n",
68+
"\n",
69+
"On my laptop the numpy binary file is about a million times faster to read. I cut out the lat/lon values from this file to save some space, but this would add, at most, a factor of 3 to the npz timing. "
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": 12,
75+
"metadata": {},
76+
"outputs": [
77+
{
78+
"name": "stdout",
79+
"output_type": "stream",
80+
"text": [
81+
"28.5 s ± 104 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
82+
]
83+
}
84+
],
85+
"source": [
86+
"%%timeit\n",
87+
"\n",
88+
"gadtxt = np.loadtxt(\"../../Data/Resources/global_age_data.3.6.xyz\")"
89+
]
90+
},
91+
{
92+
"cell_type": "code",
93+
"execution_count": 13,
94+
"metadata": {},
95+
"outputs": [
96+
{
97+
"name": "stdout",
98+
"output_type": "stream",
99+
"text": [
100+
"35.7 µs ± 130 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
101+
]
102+
}
103+
],
104+
"source": [
105+
"%%timeit\n",
106+
"\n",
107+
"gadnpy = np.load(\"../../Data/Resources/global_age_data.3.6.z.npz\")"
108+
]
109+
},
110+
{
111+
"cell_type": "markdown",
112+
"metadata": {},
113+
"source": [
114+
"## netcdf\n",
115+
"\n",
116+
"Many earth-science datasets are stored in the `netcdf` format. `scipy` provides a reader for this format"
117+
]
118+
},
119+
{
120+
"cell_type": "code",
121+
"execution_count": 9,
122+
"metadata": {},
123+
"outputs": [
124+
{
125+
"name": "stdout",
126+
"output_type": "stream",
127+
"text": [
128+
"OrderedDict([('lat', 161), ('lon', 360)])\n",
129+
"OrderedDict([('ve', <scipy.io.netcdf.netcdf_variable object at 0x7f34817cf940>),\n",
130+
" ('vn', <scipy.io.netcdf.netcdf_variable object at 0x7f34817cf908>),\n",
131+
" ('lat',\n",
132+
" <scipy.io.netcdf.netcdf_variable object at 0x7f34817cf9e8>),\n",
133+
" ('lon',\n",
134+
" <scipy.io.netcdf.netcdf_variable object at 0x7f34817cfa90>)])\n",
135+
"(161,)\n",
136+
"(360,)\n",
137+
"(360, 161)\n",
138+
"(360, 161)\n"
139+
]
140+
}
141+
],
142+
"source": [
143+
"nf = netcdf.netcdf_file(filename=\"../../Data/Reference/velocity_AU.nc\")\n",
144+
"\n",
145+
"from pprint import pprint # pretty printer for python objects\n",
146+
"\n",
147+
"pprint( nf.dimensions )\n",
148+
"pprint( nf.variables )\n",
149+
"\n",
150+
"print (nf.variables[\"lat\"].data.shape)\n",
151+
"print (nf.variables[\"lon\"].data.shape)\n",
152+
"print (nf.variables['ve'].data.shape)\n",
153+
"print (nf.variables['vn'].data.shape)"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": null,
159+
"metadata": {},
160+
"outputs": [],
161+
"source": []
162+
}
163+
],
164+
"metadata": {
165+
"kernelspec": {
166+
"display_name": "Python 3",
167+
"language": "python",
168+
"name": "python3"
169+
},
170+
"language_info": {
171+
"codemirror_mode": {
172+
"name": "ipython",
173+
"version": 3
174+
},
175+
"file_extension": ".py",
176+
"mimetype": "text/x-python",
177+
"name": "python",
178+
"nbconvert_exporter": "python",
179+
"pygments_lexer": "ipython3",
180+
"version": "3.6.8"
181+
}
182+
},
183+
"nbformat": 4,
184+
"nbformat_minor": 2
185+
}

0 commit comments

Comments
 (0)