example_2.ipynb 6.91 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "681fb4de",
   "metadata": {},
   "source": [
    "# Generate synthetic data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c938d6b",
   "metadata": {},
   "source": [
    "Here we briefly show how `pymagglobal` can be used to generate synthetic data. We first set up the model we want to use to generate the synthetic data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
22
   "id": "f52a366b",
23
24
25
26
27
28
29
30
31
32
   "metadata": {},
   "outputs": [],
   "source": [
    "from pymagglobal import Model\n",
    "\n",
    "myModel = Model('CALS10k.2')"
   ]
  },
  {
   "cell_type": "markdown",
33
   "id": "13988168",
34
35
36
37
38
39
40
41
   "metadata": {},
   "source": [
    "Next we have to generate a data distribution. We use `pymagglobal`s `get_z_at` routine, to generate `n_at` random locations, that are uniformly distributed on the sphere. Times are drawn uniformly as well."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
42
   "id": "6f57b72a",
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
   "metadata": {},
   "outputs": [],
   "source": [
    "from pymagglobal.utils import get_z_at\n",
    "\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "from cartopy import crs as ccrs\n",
    "\n",
    "# the number of artificial records\n",
    "n_at = 400\n",
    "z_at = get_z_at(n_at, random=True)\n",
    "# set the times to something arbitrary\n",
    "z_at[3] = np.random.uniform(-1000, 1900, size=n_at)\n",
    "\n",
    "# plot the records, convert co-lat to lat\n",
    "fig, ax = plt.subplots(1, 1, subplot_kw={'projection': ccrs.Mollweide()})\n",
    "ax.scatter(z_at[1], 90-z_at[0], transform=ccrs.PlateCarree())\n",
    "ax.set_global()\n",
    "ax.coastlines();"
   ]
  },
  {
   "cell_type": "markdown",
67
   "id": "806081dc",
68
69
70
71
72
73
74
75
   "metadata": {},
   "source": [
    "Finally we can evaluate the model at the inputs, to get synthetic ''observations'' of the field"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
76
   "id": "5842b198",
77
78
79
80
81
82
83
84
85
   "metadata": {},
   "outputs": [],
   "source": [
    "from pymagglobal.core import field\n",
    "obs = field(z_at, myModel)"
   ]
  },
  {
   "cell_type": "markdown",
86
   "id": "14502d90",
87
88
89
90
91
92
93
94
   "metadata": {},
   "source": [
    "Let's have a look at the records:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
95
   "id": "67cd6ed7",
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, axs = plt.subplots(1, 3, subplot_kw={'projection': ccrs.Mollweide()},\n",
    "                        figsize=(15, 4))\n",
    "titles = [r'$B_N$', r'$B_E$', r'$B_Z$']\n",
    "for it in range(3):\n",
    "    axs[it].set_title(titles[it])\n",
    "    axs[it].scatter(z_at[1], 90-z_at[0], c=obs[it], transform=ccrs.PlateCarree())\n",
    "    axs[it].set_global()\n",
    "    axs[it].coastlines();"
   ]
  },
  {
   "cell_type": "markdown",
111
   "id": "e96b4ec7",
112
113
114
115
116
117
118
119
   "metadata": {},
   "source": [
    "Declination, inclination and intensity records are also easily generated, by using the `field` kwargs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
120
   "id": "57b38352",
121
122
123
124
125
126
127
128
129
130
131
132
133
   "metadata": {},
   "outputs": [],
   "source": [
    "obs_dif = field(z_at, myModel, field_type='dif')\n",
    "fig, axs = plt.subplots(1, 3, subplot_kw={'projection': ccrs.Mollweide()},\n",
    "                        figsize=(15, 4))\n",
    "titles = [r'$D$', r'$I$', r'$F$']\n",
    "for it in range(3):\n",
    "    axs[it].set_title(titles[it])\n",
    "    axs[it].scatter(z_at[1], 90-z_at[0], c=obs_dif[it], transform=ccrs.PlateCarree())\n",
    "    axs[it].set_global()\n",
    "    axs[it].coastlines();"
   ]
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
  },
  {
   "cell_type": "markdown",
   "id": "a16058b9",
   "metadata": {},
   "source": [
    "##  Data from an arbitray set of coefficients"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "003f37e3",
   "metadata": {},
   "source": [
    "Sometimes it is helpful to generate data not from a model that's included in pymagglobal, but from an arbitrary set of coefficients. Provided they are in the ''standard order'', i.e. $g_1^0, g_1^1, g_1^{-1}, g_2^0, g_2^1, g_2^{-1}, ...$, this is also straight-forward using utility routines from pymagglobal:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fdbdfd1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pymagglobal.core import coefficients\n",
    "\n",
    "epoch = -3000\n",
    "# First generate coefficients at epoch\n",
    "_, _, coeffs = coefficients(epoch, myModel)\n",
    "\n",
    "# Generate new input locations at the given epoch\n",
    "z_at = get_z_at(n_at, random=True, t=epoch)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e81a742d",
   "metadata": {},
   "source": [
    "We use the pymagglobal routine `dsh_basis`, which evaluates the derivatives of the spherical harmonics up to a given degree at given inputs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5924dde3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pymagglobal.utils import dsh_basis\n",
    "\n",
    "# dsh_basis was designed to work with a C backend,\n",
    "# so the return array has to be allocated beforehand\n",
    "# and is filled during the function call\n",
    "base = np.empty((len(coeffs), 3*z_at.shape[1]))\n",
    "dsh_basis(myModel.l_max, z_at, base)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d875959",
   "metadata": {},
   "source": [
    "Field values are then generated via a dot product:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0a43e60",
   "metadata": {},
   "outputs": [],
   "source": [
    "obs = coeffs @ base"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c804ae2",
   "metadata": {},
   "source": [
    "The outputs are now given by obs, every third value is N, E, Z. To have a more convenient form, we reshape the output:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6eb5bc3",
   "metadata": {},
   "outputs": [],
   "source": [
    "obs = obs.reshape(n_at, 3).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3ffc91a",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, axs = plt.subplots(1, 3, subplot_kw={'projection': ccrs.Mollweide()},\n",
    "                        figsize=(15, 4))\n",
    "titles = [r'$B_N$', r'$B_E$', r'$B_Z$']\n",
    "for it in range(3):\n",
    "    axs[it].set_title(titles[it])\n",
    "    axs[it].scatter(z_at[1], 90-z_at[0], c=obs[it], transform=ccrs.PlateCarree())\n",
    "    axs[it].set_global()\n",
    "    axs[it].coastlines();"
   ]
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}