adding data noise
[notebooks.git] / fmda_kf_rnn.ipynb
blobc5da89f07c6ef545b6df1041ecfadbd5e976b1a5
2   "cells": [
3     {
4       "cell_type": "markdown",
5       "metadata": {
6         "id": "khOTxJsYc91W"
7       },
8       "source": [
9         "# Kalman Filtering and Recurrent Neural Networks for Fuel Moisture\n",
10         "## Jan Mandel, University of Colorado Denver\n"
11       ]
12     },
13     {
14       "cell_type": "markdown",
15       "metadata": {
16         "id": "sXaqfI-EdCEk"
17       },
18       "source": [
19         "## Abstract"
20       ]
21     },
22     {
23       "cell_type": "markdown",
24       "metadata": {
25         "id": "ZbtSiYm4dF7B"
26       },
27       "source": [
28         "''Fuel moisture is an important factor of the spread of wildland fires. Some weather stations have fuel moisture sensors and data are available online. We review a simple model of fuel moisture from atmospheric conditions, and show how to adjust the model using the weather station data."
29       ]
30     },
31     {
32       "cell_type": "markdown",
33       "metadata": {
34         "id": "eZ6dfHlZ63j1"
35       },
36       "source": [
37         "## Table of contents"
38       ]
39     },
40     {
41       "cell_type": "markdown",
42       "source": [
43         ""
44       ],
45       "metadata": {
46         "id": "Snyr0_3sGxty"
47       }
48     },
49     {
50       "cell_type": "markdown",
51       "metadata": {
52         "id": "WHIgN2uZ689b"
53       },
54       "source": [
55         "1 Introduction\n",
56         "\n",
57         "2 Background\n",
58         "\n",
59         "2.1 Imports\n",
60         "\n",
61         "2.2 Kalman filter\n",
62         "\n",
63         "2.2.1 Overview\n",
64         "\n",
65         "2.2.2 Formulation\n",
66         "\n",
67         "2.2.3 A Kalman filter tester\n",
68         "\n",
69         "2.3 Fuel moisture model\n",
70         "\n",
71         "2.3.1 A simple time lag model\n",
72         "\n",
73         "2.3.1 Fuel moisture model with drying equilibrium, wetting equilibrium, and rain\n",
74         "\n",
75         "3 Methods\n",
76         "\n",
77         "3.1 Kalman filter demonstration on the simple model\n",
78         "\n",
79         "3.1.1 Creating synthetic data\n",
80         "\n",
81         "3.1.2 Running the Kalman filter\n",
82         "\n",
83         "3.2 Acquisition and preprocessing of real data\n",
84         "\n",
85         "3.2.1 Acquisition of fuel moisture observations\n",
86         "\n",
87         "3.2.2 Acquisition of weather data\n",
88         "\n",
89         "3.2.3 Preprocessing and visualization of the weather data\n",
90         "\n",
91         "4 Results\n",
92         "\n",
93         "4.1 Kalman filter with fuel moisture observations, followed by forecasting\n",
94         "\n",
95         "4.2 Model with an augmented state\n",
96         "\n",
97         "4.3 Kalman filter on the augmented model\n",
98         "\n",
99         "4.4 A comment on the information flow in the Kalman filter and in neural networks\n",
100         "\n",
101         "5. Conclusion\n",
102         "\n",
103         "Contributions of Authors\n",
104         "\n",
105         "Acknowledgements\n",
106         "\n",
107         "References\n",
108         "\n",
109         "\n",
110         "\n",
111         "\n",
112         "\n",
113         "\n",
114         "\n"
115       ]
116     },
117     {
118       "cell_type": "markdown",
119       "metadata": {
120         "id": "ZFafUPCTO1N1"
121       },
122       "source": [
123         "## 1 Introduction"
124       ]
125     },
126     {
127       "cell_type": "markdown",
128       "metadata": {
129         "id": "4_RcdWybPFks"
130       },
131       "source": [
132         "The Kalman filter is at the foundation of many technologies in daily use, from GPS to weather forecasting. No model is completely accurate. Think space navigation: the movement of a Apollo 13 between the moon and the earth, subject to gravitational forces and propulsion, with the position ascertained by visual measurements. No matter how accurate the model of spacecraft motion is, the measurements are always burdened with noise. The idea of Kalman filter is to evolve a quantification of the of the state (here, positin and velocity of the spacecraft) in the form of a covariance matrix, and, using an estimate of the uncertainty of the data, adjust the state to split the difference every time measurements are taken. \n",
133         "\n",
134         "Here, we use the Kalman filter to estimate the evolution of fuel (dead wood) moisture content from a simple theoretical model, adjusting the state of the model hourly for measurements from fuel moisture a sensor in a wood stick exposed to the elements. This is needed for forecasting of wildfire progress; for this purpose, we also want to have the filter adjust the model from the data, so that it gives more accurate data for future when we only have hourly weather forecast but no actual data - because the future has not happened yet. "
135       ]
136     },
137     {
138       "cell_type": "markdown",
139       "metadata": {
140         "id": "M2kbwDPBTB7A"
141       },
142       "source": [
143         "## 2 Background"
144       ]
145     },
146     {
147       "cell_type": "markdown",
148       "metadata": {
149         "id": "ar1BbXac49hO"
150       },
151       "source": [
152         "In this section, we take care of preliminaries: we install some packages we need, and then proceed with the Kalman filter."
153       ]
154     },
155     {
156       "cell_type": "markdown",
157       "metadata": {
158         "id": "_5F5CuRqc91X"
159       },
160       "source": [
161         "### 2.1 Imports"
162       ]
163     },
164     {
165       "cell_type": "markdown",
166       "metadata": {
167         "id": "K6sWUMf0c91Y"
168       },
169       "source": [
170         "We may need the pygrib package to read weather data, but pygrib requires current numpy while google colab is using an old numpy version for compatibility with tensorflow. We will upgrade numpy and restart the runtime then the notebook will need to be run again. If numpy is current, we just download and import packages we need."
171       ]
172     },
173     {
174       "cell_type": "markdown",
175       "metadata": {
176         "id": "X9rvlymMZdJg"
177       },
178       "source": [
179         "### 2.2 Kalman filter"
180       ]
181     },
182     {
183       "cell_type": "markdown",
184       "metadata": {
185         "id": "x5E2UE3F5gf2"
186       },
187       "source": [
188         "#### 2.2.1 Overview"
189       ]
190     },
191     {
192       "cell_type": "markdown",
193       "metadata": {
194         "id": "NPgTHlCLAlA-"
195       },
196       "source": [
197         "The Kalman filter provides an estimate $u$ of the time evolution of some unknown process, called \"nature\" or \"truth\". We do not know with certainty what the nature is, but we can observe it at regular intervals (steps) with some error. In each step, model $F$ advances the model state $u$ in time, $ u \\leftarrow F(u)$, and attempts to reconcile the state with an observation $d$ of the true state, so $u \\approx d$. The filter modifies the model state $u$ to balance the uncertainty in the model and the data (this is called *analysis*) and the cycle continues. For that purpose, the filter evolves also an estimate of the uncertainly of the model.\n",
198         "\n",
199         "More generally, instead of $u \\approx d$, only a part of the state is observed, and $Hu \\approx d$ where $H$ is a matrix, or observation function. Basically, $Hu$ is what the data would be if the model was completely accurate. \n",
200         "\n",
201         "See Kalman (1960) for the original publication, Kalnay (2003) for a gentle introduction, and the [Wikipedia article](https://en.wikipedia.org/wiki/Extended_Kalman_filter)."
202       ]
203     },
204     {
205       "cell_type": "markdown",
206       "metadata": {
207         "id": "y6j34L5s5pEL"
208       },
209       "source": [
210         "#### 2.2.2 Formulation\n",
211         "\n",
212         "---\n",
213         "\n"
214       ]
215     },
216     {
217       "cell_type": "markdown",
218       "metadata": {
219         "id": "b3GZW5vP5_o8"
220       },
221       "source": [
222         "We present the Kalman filter in perhaps the most used form, as extended to nonlinear models.\n",
223         " Consider a discrete time model of some natural\n",
224         "process. At time step $k$, the model has state $u_{k}\\in\\mathbb{R}^{n}$, which\n",
225         "can be approximated from the previous step $u_{k-1}$ by applying the model\n",
226         "$\\mathcal{M}$ to get a forecast $u_{k}^{f}=\\mathcal{M}\\left(  u_{k-1}\\right)\n",
227         "$. We model uncertainty in the model itself by adding normally distributed\n",
228         "noise with mean zero and covariance $Q$ to the uncertainty of $u_{k}^{f}$. We\n",
229         "also need to estimate now the uncertainty in the previous state $u_{k-1}$\n",
230         "propagates to the uncertainty of the forecast $u_{k}^{f}$. So, assume that the\n",
231         "model is differentiable and quantify the uncertainty of the state by a\n",
232         "covariance matrix. That is,  assume that at step $k-1$, the state has\n",
233         "(approximately) normal distribution with mean $u_{k-1}$ and covariance\n",
234         "$P_{k-1}$. Using the Taylor expansion of order $1$ of the model operator at\n",
235         "$u_{k-1}$, $\\mathcal{M}\\left(  u\\right)  \\approx\\mathcal{M}\\left(\n",
236         "u_{k-1}\\right)  +\\mathcal{M}^{\\prime}\\left(  u_{k-1}\\right)  \\left(\n",
237         "u-u_{k-1}\\right)  $, where $\\mathcal{M}^{\\prime}\\left(  u_{k-1}\\right)  $ is\n",
238         "the Jacobian matrix of $\\mathcal{M}$ at $u_{k-1}$. It can be shown that the\n",
239         "forecast has then (approximately)\\ normal distribution with mean and\n",
240         "covariance\n",
241         "$$\n",
242         "u_{k}^{f}=\\mathcal{M}\\left(  u_{k-1}\\right)  ,\\ P_{k}^{f}=\\mathcal{M}\\left(\n",
243         "u_{k-1}\\right)  P_{k-1}\\mathcal{M}^{\\prime}\\left(  u_{k-1}\\right)  +Q\n",
244         "$$\n",
245         "At time $k$, we also have an observation $d_{k}\\approx Hu_{k}$, where $H$ is a\n",
246         "given observation operator, and we want to find $u_{k}$ so that both\n",
247         "$$\n",
248         "u_{k}\\approx u_{k}^{f}\\text{ and }d_{k}\\approx Hu_{k}.\n",
249         "$$\n",
250         "We quantify the uncertainly of the error of observation $d_{k}$ by a covariance\n",
251         "matrix $R$: assume that the observation error has normal probability\n",
252         "distribution with a known covariance $R$. Then, the likelihood of state $u$ is\n",
253         "proportional to $e^{-\\left\\Vert d_{k}-Hu\\right\\Vert _{R^{-1}}^{2}/2}$, where\n",
254         "we used the notation for the norm $\\left\\Vert v\\right\\Vert _{A}%\n",
255         "=\\left(v^{\\top}Av\\right)^{1/2}$ induced by a positive definite matrix $A$. Similarly, we quantify the\n",
256         "uncertainty of the state by a covariance matrix $P_{k}$. That is, the forecast\n",
257         "state has (approximately) normal distribution with mean $u_{k}^{f}$  and covariance\n",
258         "$P_{k}^{f}$. From the Bayes theorem of statistics, the probability distribution\n",
259         "of the state after taking the data into account has density\n",
260         "$$\n",
261         "p_{k}\\left(  u\\right) \\propto e^\\frac{-\\left\\Vert d_{k}\n",
262         "-Hu\\right\\Vert_{R^{-1}}^{2}}{2}e^\\frac{-\\left\\Vert u-u_{k}^{f}\\right\\Vert _{\n",
263         "{P_{k}^f}^{-1}  }^{2}}{2}%\n",
264         "$$\n",
265         "where $\\propto$ means proportional.\n",
266         "Note that the probability density at $u$ is maximal when $\\left\\Vert\n",
267         "d_{k}-Hu\\right\\Vert _{R^{-1}}^{2}+\\left\\Vert u-u_{k}\\right\\Vert _{{P_{k}^{f}}^{-1}}^{2}$\n",
268         " is minimal, which quantifies the statement that $d_{k}\\approx\n",
269         "Hu_{k}$ and $u\\approx u_{k}^{f}$.  By a direct computation completing the\n",
270         "square and using the Sherman-Morrison-Woodbury formula, \n",
271         "$$p_{k}\\left(\n",
272         "\t\tu\n",
273         "\t   \\right) \\propto \n",
274         "e^{-\\frac{\n",
275         "\t\\left\\Vert u-u_{k\n",
276         "\t         }\n",
277         "\t\\right\\Vert_\n",
278         "\t\t{P_{k\n",
279         "\t\t      }^{-1}\n",
280         "\t\t}^{2}\n",
281         "\t}\n",
282         "\t{2}},\n",
283         "$$ \n",
284         "which is the density of the normal distribution with the mean\n",
285         "$$\n",
286         "u_{k}^{f}=u_{k}^{f}+K_{k}(d-Hu_{k}^{f}),\\ \\text{where }K_{k}=P_{k}%\n",
287         "^{f}H^{\\mathrm{T}}(HP_{k}^{f}H^{\\mathrm{T}}+R)^{-1}%\n",
288         "$$\n",
289         "and covariance\n",
290         "$$\n",
291         "P_{k}=\\left(  \\left(  P_{k}^{f}\\right)  ^{-1}+H^{\\mathrm{T}}R^{-1}H\\right)\n",
292         "^{-1}=(I-KH)P_{k}^{f}.\n",
293         "$$\n",
294         "\n",
295         "These are the equations of the extended Kalman filter. The original Kalman (1960) filter was\n",
296         "formulated for a linear process. The extension to the\n",
297         "nonlinear case made broad array of applications possible, including the Apollo spacecraft naviation (McGee and Schmidt, 1966),  and is\n",
298         "still a de-facto standard in navigation and GPS.\n"
299       ]
300     },
301     {
302       "cell_type": "code",
303       "execution_count": null,
304       "metadata": {
305         "id": "-bvUtJ_OLwQA"
306       },
307       "outputs": [],
308       "source": [
309         "import numpy as np\n",
310         "def ext_kf(u,P,F,Q=0,d=None,H=None,R=None):\n",
311         "  \"\"\"\n",
312         "  One step of the extended Kalman filter. \n",
313         "  If there is no data, only advance in time.\n",
314         "  :param u:   the state vector, shape n\n",
315         "  :param P:   the state covariance, shape (n,n)\n",
316         "  :param F:   the model function, args vector u, returns F(u) and Jacobian J(u)\n",
317         "  :param Q:   the process model noise covariance, shape (n,n)\n",
318         "  :param d:   data vector, shape (m). If none, only advance in time\n",
319         "  :param H:   observation matrix, shape (m,n)\n",
320         "  :param R:   data error covariance, shape (n,n)\n",
321         "  :return ua: the analysis state vector, shape (n)\n",
322         "  :return Pa: the analysis covariance matrix, shape (n,n)\n",
323         "  \"\"\"\n",
324         "  def d2(a):\n",
325         "    return np.atleast_2d(a) # convert to at least 2d array\n",
326         "\n",
327         "  def d1(a):\n",
328         "    return np.atleast_1d(a) # convert to at least 1d array\n",
329         "\n",
330         "  # forecast\n",
331         "  uf, J  = F(u)          # advance the model state in time and get the Jacobian\n",
332         "  uf = d1(uf)            # if scalar, make state a 1D array\n",
333         "  J = d2(J)              # if scalar, make jacobian a 2D array\n",
334         "  P = d2(P)              # if scalar, make Jacobian as 2D array\n",
335         "  Pf  = d2(J.T @ P) @ J + Q  # advance the state covariance Pf = J' * P * J + Q\n",
336         "  # analysis\n",
337         "  if d is None or not d.size :  # no data, no analysis\n",
338         "    return uf, Pf\n",
339         "  # K = P H' * inverse(H * P * H' + R) = (inverse(H * P * H' + R)*(H P))'\n",
340         "  H = d2(H)\n",
341         "  HP  = d2(H @ P)            # precompute a part used twice  \n",
342         "  K   = d2(np.linalg.solve( d2(HP @ H.T) + R, HP)).T  # Kalman gain\n",
343         "  # print('H',H)\n",
344         "  # print('K',K)\n",
345         "  res = d1(H @ d1(uf) - d)          # res = H*uf - d\n",
346         "  ua = uf - K @ res # analysis mean uf - K*res\n",
347         "  Pa = Pf - K @ d2(H @ P)        # analysis covariance\n",
348         "  return ua, d2(Pa)\n"
349       ]
350     },
351     {
352       "cell_type": "markdown",
353       "metadata": {
354         "id": "Uvsbbv2XZ2Hd"
355       },
356       "source": [
357         "#### 2.2.3 A Kalman filter tester"
358       ]
359     },
360     {
361       "cell_type": "markdown",
362       "metadata": {
363         "id": "gcmGBqPOU1e5"
364       },
365       "source": [
366         "It is a very good idea to make write a simple tester for every piece of code. How else would we know it actually works, and that something basic did not get broken inadvertently, perhaps as a side effect of changing something else? A simple tester may save a great deal of time trying to debug cryptic errors later. And, what better place for a tester that right after the code it is testing so that it gets run every time?"
367       ]
368     },
369     {
370       "cell_type": "code",
371       "execution_count": null,
372       "metadata": {
373         "id": "OsOqvQk6ZXZV"
374       },
375       "outputs": [],
376       "source": [
377         "# a basic ext_kf test\n",
378         "import numpy as np\n",
379         "u = [1,\n",
380         "     2]\n",
381         "P = [[2 , -1],\n",
382         "    [-1 , 2]]\n",
383         "A = [ [1 ,2],\n",
384         "      [3 ,4]]\n",
385         "u = np.array(u)      \n",
386         "Q = np.array([[1,0],[0,1]])\n",
387         "A = np.array(A)\n",
388         "def fun(u):\n",
389         "  return A @ u, A\n",
390         "F = lambda u: fun(u)\n",
391         "H = [[1, 0],\n",
392         "     [0, 1]]\n",
393         "d = [2,\n",
394         "    3]\n",
395         "R = [[2, 0],\n",
396         "    [0, 2]]\n",
397         "H = np.array(H)      \n",
398         "d = np.array(d)\n",
399         "R = np.array(R)\n",
400         "ua,Pa = ext_kf(u,P,F,Q)\n",
401         "print('ua=',ua)\n",
402         "print('Pa=',Pa)\n",
403         "ua,Pa = ext_kf(u,P,F,Q,d,H,R)\n",
404         "print('ua=',ua)\n",
405         "print('Pa=',Pa)\n"
406       ]
407     },
408     {
409       "cell_type": "markdown",
410       "metadata": {
411         "id": "A9ZpmNcdRpmp"
412       },
413       "source": [
414         "### 2.3  Fuel moisture models\n",
415         "\n",
416         "\n"
417       ]
418     },
419     {
420       "cell_type": "markdown",
421       "metadata": {
422         "id": "eZL8gN7ISGVh"
423       },
424       "source": [
425         "#### 2.3.1 A simple fuel moisture model"
426       ]
427     },
428     {
429       "cell_type": "markdown",
430       "metadata": {
431         "id": "1XvOC4kYSQgH"
432       },
433       "source": [
434         "First consider a simplified fuel moisture model without considering the effect of rain.\n",
435         "The evolution of fuel moisture content $m(t)$ is modeled by the time-lag differential equation on interval $\\left[\n",
436         "t_{0},t_{1}\\right]  $,\n",
437         "$$\n",
438         "\\frac{dm}{dt}=\\frac{E-m(t)}{T},\\quad m(t_{0})=m_{0}.\n",
439         "$$\n",
440         "where the initial fuel moisture content $m_{0}=m\\left(  t_{0}\\right)  $ is the\n",
441         "input, and $m_{1}=m(t_{1})$ is the output. Tnus, $m_1=F(m_0)$. The parameters of the model are the\n",
442         "fuel moisture equilibrium $E$, assumed to be constant over the interval $\\left[\n",
443         "t_{0},t_{1}\\right]  $, NS the characteristic decay time $T$. \n",
444         "\n",
445         "We can build the general model later by calling this simple model with different\n",
446         "equilibria and time constants (drying, wetting, rain).\n",
447         "\n",
448         "Since $E$ is constant in time, the solution can be found\n",
449         "analytically,\n",
450         "$$\n",
451         "m\\left(  t\\right)  =E+\\left(  m_{0}-E\\right)  e^{-t/T}%\n",
452         "$$\n",
453         "For convenience, we use $T_{1}=1/T$ instead of $T$, and the model becomes\n",
454         "$$\n",
455         "m_{1}=E+\\left(  m_{0}-E\\right)  e^{-\\left(  t_{1}-t_{0}\\right)  T_{1}}%\n",
456         "$$\n",
457         "In the extended Kalman filter, we will need the partial derivatives of $m_{1}$\n",
458         "with respect to the input and the parameters. Compute\n",
459         "$$\n",
460         "\\frac{dm_{1}}{d_{m0}}=e^{-\\left(  t_{1}-t_{0}\\right)  T_{1}}\n",
461         "$$\n",
462         "$$\n",
463         "\\frac{dm_{1}}{dE}=1-e^{-\\left(  t_{1}-t_{0}\\right)  T_{1}}\n",
464         "$$\n",
465         "$$\n",
466         "\\frac{dm_{1}}{dT_{1}}=-\\left(  m_{0}-E\\right)  \\left(  t_{1}-t_{0}\\right)\n",
467         "e^{-\\left(  t_{1}-t_{0}\\right)  T_{1}}\n",
468         "$$\n",
469         "At the moment, we need only ${dm_{1}}/{dm_{0}}$ but we put in the code all partials for possible use in future.\n"
470       ]
471     },
472     {
473       "cell_type": "code",
474       "execution_count": null,
475       "metadata": {
476         "id": "wuVIAGLiSeR8"
477       },
478       "outputs": [],
479       "source": [
480         "import numpy as np\n",
481         "def model_decay(m0,E,partials=0,T1=0.1,tlen=1):  \n",
482         "  # Arguments: \n",
483         "  #   m0          fuel moisture content at start dimensionless, unit (1)\n",
484         "  #   E           fuel moisture eqilibrium (1)\n",
485         "  #   partials=0: return m1 = fuel moisture contents after time tlen (1)\n",
486         "  #           =1: return m1, dm0/dm0 \n",
487         "  #           =2: return m1, dm1/dm0, dm1/dE\n",
488         "  #           =3: return m1, dm1/dm0, dm1/dE dm1/dT1   \n",
489         "  #   T1          1/T, where T is the time constant approaching the equilibrium\n",
490         "  #               default 0.1/hour\n",
491         "  #   tlen        the time interval length, default 1 hour\n",
492         "\n",
493         "  exp_t = np.exp(-tlen*T1)                  # compute this subexpression only once\n",
494         "  m1 = E + (m0 - E)*exp_t                   # the solution at end\n",
495         "  if partials==0:\n",
496         "    return m1\n",
497         "  dm1_dm0 = exp_t\n",
498         "  if partials==1:\n",
499         "    return m1, dm1_dm0          # return value and Jacobian\n",
500         "  dm1_dE = 1 - exp_t      \n",
501         "  if partials==2:\n",
502         "     return m1, dm1_dm0, dm1_dE \n",
503         "  dm1_dT1 = -(m0 - E)*tlen*exp_t            # partial derivative dm1 / dT1\n",
504         "  if partials==3:\n",
505         "    return m1, dm1_dm0, dm1_dE, dm1_dT1       # return value and all partial derivatives wrt m1 and parameters\n",
506         "  raise('Bad arg partials')\n",
507         "  "
508       ]
509     },
510     {
511       "cell_type": "markdown",
512       "metadata": {
513         "id": "dOARZlj-RUCi"
514       },
515       "source": [
516         "#### 2.3.2 Fuel moisture model with drying equilibrium, wetting equilibrium, and rain"
517       ]
518     },
519     {
520       "cell_type": "markdown",
521       "metadata": {
522         "id": "AJp6FTpTSx5B"
523       },
524       "source": [
525         "Here is a little more realistic fuel moisture model from Mandel et al. (2004). A rain-wetting lag time $t_{\\mathrm{r}}$ is reached for heavy rain only\n",
526         "asymptotically, when the rain intensity $r$ (mm/h) is\n",
527         "large:\n",
528         "$$\n",
529         "\\frac{\\mathrm{d}m}{\\mathrm{d}t}=\\frac{S-m}{t_{\\mathrm{r}}}\\left(1-\\exp\\left(-\\frac{r-r_0}{r_{\\mathrm{s}}}\n",
530         "\\right)  \\right),\\ \\text{if}\\ r>r_0, \n",
531         "$$\n",
532         "where $r_0$ is the threshold rain intensity below which no perceptible\n",
533         "wetting occurs, and $r_{\\mathrm{s}}$ is the saturation rain\n",
534         "intensity. At the saturation rain intensity, $1-1/e\\approx 0.63$ of\n",
535         "the maximal rain-wetting rate is achieved. For 10h fuel, the model takes $S=250\\,{\\%}$,\n",
536         "$t_{\\mathrm{r}}=14$h, $r_0=0.05$mm/h and\n",
537         "$r_{\\mathrm{s}}=8$mm/h. "
538       ]
539     },
540     {
541       "cell_type": "code",
542       "execution_count": null,
543       "metadata": {
544         "id": "ITsKE0psRblG"
545       },
546       "outputs": [],
547       "source": [
548         "### Define model function with drying, wetting, and rain equilibria\n",
549         "\n",
550         "# Parameters\n",
551         "r0 = 0.05                                   # threshold rainfall [mm/h]\n",
552         "rs = 8.0                                    # saturation rain intensity [mm/h]\n",
553         "Tr = 14.0                                   # time constant for rain wetting model [h]\n",
554         "S = 250                                     # saturation intensity [dimensionless]\n",
555         "T = 10.0                                    # time constant for wetting/drying\n",
556         "\n",
557         "def model_moisture(m0,Eqd,Eqw,r,t,partials=0,T=10.0,tlen=1.0):\n",
558         "    # arguments:\n",
559         "    # m0         starting fuel moistureb (%s\n",
560         "    # Eqd        drying equilibrium      (%) \n",
561         "    # Eqw        wetting equilibrium     (%)\n",
562         "    # r          rain intensity          (mm/h)\n",
563         "    # t          time\n",
564         "    # partials = 0, 1, 2\n",
565         "    # returns: same as model_decay\n",
566         "    #   if partials==0: m1 = fuel moisture contents after time 1 hour\n",
567         "    #              ==1: m1, dm1/dm0 \n",
568         "    #              ==2: m1, dm1/dm0, dm1/dE  \n",
569         "    \n",
570         "    if r > r0:\n",
571         "        # print('raining')\n",
572         "        E = S\n",
573         "        T1 =  (1.0 - np.exp(- (r - r0) / rs)) / Tr\n",
574         "    elif m0 <= Eqw: \n",
575         "        # print('wetting')\n",
576         "        E=Eqw\n",
577         "        T1 = 1.0/T\n",
578         "    elif m0 >= Eqd:\n",
579         "        # print('drying')\n",
580         "        E=Eqd\n",
581         "        T1 = 1.0/T\n",
582         "    else: # no change'\n",
583         "        E = m0\n",
584         "        T1=0.0\n",
585         "    exp_t = np.exp(-tlen*T1)\n",
586         "    m1 = E + (m0 - E)*exp_t  \n",
587         "    dm1_dm0 = exp_t\n",
588         "    dm1_dE = 1 - exp_t\n",
589         "    #if t>=933 and t < 940:\n",
590         "    #  print('t,Eqw,Eqd,r,T1,E,m0,m1,dm1_dm0,dm1_dE',\n",
591         "    #        t,Eqw,Eqd,r,T1,E,m0,m1,dm1_dm0,dm1_dE)   \n",
592         "    if partials==0: \n",
593         "        return m1\n",
594         "    if partials==1:\n",
595         "        return m1, dm1_dm0\n",
596         "    if partials==2:\n",
597         "        return m1, dm1_dm0, dm1_dE\n",
598         "    raise('bad partials')"
599       ]
600     },
601     {
602       "cell_type": "markdown",
603       "metadata": {
604         "id": "JDLU3B_jV42l"
605       },
606       "source": [
607         "## 3. Methods"
608       ]
609     },
610     {
611       "cell_type": "markdown",
612       "metadata": {
613         "id": "hLPJT3FcA2a7"
614       },
615       "source": [
616         "### 3.1 Kalman filter demonstration on the simple model"
617       ]
618     },
619     {
620       "cell_type": "markdown",
621       "metadata": {
622         "id": "kIA3X8vluFdd"
623       },
624       "source": [
625         "We demonstrate the Kalman filter for this model on a simple artificial example. The model is solving the differential equation for one hour. The equilibrium $E$ is constant during the hour, but it changes over the day so that it is higher at night and lower during the day, with a 24-hour period.  First, we create the \"truth\" by choosing the equilibrium $E$ and solving the differential aquation every hour, with a small additive noise. The synthetic data is obtained as the values of the \"truth\", with random noise to simulate observation error."
626       ]
627     },
628     {
629       "cell_type": "markdown",
630       "metadata": {
631         "id": "bBv10PTiChhm"
632       },
633       "source": [
634         "#### 3.1.1 Creating synthetic data"
635       ]
636     },
637     {
638       "cell_type": "code",
639       "execution_count": null,
640       "metadata": {
641         "id": "my6nnrk1iQo8"
642       },
643       "outputs": [],
644       "source": [
645         ""
646       ]
647     },
648     {
649       "cell_type": "code",
650       "execution_count": null,
651       "metadata": {
652         "id": "-_pz-wXnCMnP"
653       },
654       "outputs": [],
655       "source": [
656         "def create_synthetic_data(days=20,power=4,data_noise=0.02,process_noise=0.0,DeltaE=0.0):\n",
657         "  import numpy as np, random\n",
658         "  hours = days*24\n",
659         "  h2 = int(hours/2)\n",
660         "  hour = np.array(range(hours))\n",
661         "  day = np.array(range(hours))/24.\n",
662         "\n",
663         "  # artificial equilibrium data\n",
664         "  E = np.power(np.sin(np.pi*day),4) # diurnal curve\n",
665         "  E = 0.05+0.25*E\n",
666         "  # FMC free run\n",
667         "  m_f = np.zeros(hours)\n",
668         "  m_f[0] = 0.1         # initial FMC\n",
669         "  process_noise=0.\n",
670         "  for t in range(hours-1):\n",
671         "    m_f[t+1] = max(0.,model_decay(m_f[t],E[t])  + random.gauss(0,process_noise) )\n",
672         "  data = m_f + np.random.normal(loc=0,scale=data_noise,size=hours)\n",
673         "  E = E + DeltaE    \n",
674         "\n",
675         "  %matplotlib inline\n",
676         "  import matplotlib.pyplot as plt \n",
677         "  # fig1, ax1 = plt.subplots()\n",
678         "\n",
679         "  plt.figure(figsize=(16,4))\n",
680         "  plt.plot(hour,E,linestyle='--',c='r',label='Equilibrium')\n",
681         "  plt.plot(hour,m_f,linestyle='-',c='k',label='10-h fuel truth')\n",
682         "  plt.scatter(hour[:h2],data[:h2],c='b',label='10-h fuel data')\n",
683         "  plt.title('Synthetic data')\n",
684         "  plt.xlabel('Time (hours)')\n",
685         "  plt.ylabel('Fuel moisture content (%)')\n",
686         "  plt.legend()\n",
687         "  return E,m_f,data,hour,h2,DeltaE\n"
688       ]
689     },
690     {
691       "cell_type": "code",
692       "execution_count": null,
693       "metadata": {
694         "id": "GfRxLp4HkcVz"
695       },
696       "outputs": [],
697       "source": [
698         "E,m_f,data,hour,h2,DeltaE = create_synthetic_data(days=20,power=4,data_noise=0.01,process_noise=0.0,DeltaE=0.0) "
699       ]
700     },
701     {
702       "cell_type": "markdown",
703       "metadata": {
704         "id": "z-3WLAEpD2yJ"
705       },
706       "source": [
707         "#### 3.1.2 Running the Kalman filter"
708       ]
709     },
710     {
711       "cell_type": "markdown",
712       "metadata": {
713         "id": "T4g-RrrYAlBD"
714       },
715       "source": [
716         "We have used the same code for model and for the truth, and run the Kalman filter for 10 days. The graph below shows that the model state was remarkably close to the truth, even if the model is fed only noisy observations. This is because the dynamics of the model and of the truth are the same. After 10 days, we let the model continue without any new data to simulate forecasting the future, and the agreement with the truth was still very good."
717       ]
718     },
719     {
720       "cell_type": "code",
721       "execution_count": null,
722       "metadata": {
723         "id": "_-CjONZkD18n"
724       },
725       "outputs": [],
726       "source": [
727         "import numpy as np\n",
728         "import matplotlib.pyplot as plt \n",
729         "\n",
730         "# using global E, m_f\n",
731         "\n",
732         "def plot_m(m,Ec=None,title=None,):  # global hour\n",
733         "  hours=hour.shape[0]\n",
734         "  %matplotlib inline\n",
735         "  plt.figure(figsize=(16,4))\n",
736         "  plt.plot(hour,E,linestyle='--',c='r',label='E=Equilibrium data')\n",
737         "  # print(len(hour),len(m_f))\n",
738         "  plt.plot(hour,m_f,linestyle='-',c='b',label='m_f=10-h fuel truth')\n",
739         "  plt.scatter(hour[:h2],data[:h2],c='b',label='data=10-h fuel data')\n",
740         "  if m is not None:\n",
741         "    plt.plot(hour[:h2],m[:h2],linestyle='-',c='k',label='m=filtered')\n",
742         "    plt.plot(hour[h2:hours],m[h2:hours],linestyle='-',c='r',label='m=forecast')\n",
743         "  if Ec is not None:\n",
744         "    plt.plot(hour,Ec,linestyle='-',c='g',label='Ec=Equilibrium correction')\n",
745         "  if title is not None:\n",
746         "    plt.title(title) \n",
747         "  else:\n",
748         "    plt.title('Kalman filtering and forecast on artificial data')\n",
749         "  plt.xlabel('Time (hours)') \n",
750         "  plt.ylabel('Fuel moisture content (%)')\n",
751         "  plt.legend()\n",
752         "\n",
753         "def kf_example(DeltaE):\n",
754         "  hours=hour.shape[0]\n",
755         "  m = np.zeros(hours)\n",
756         "  m[0]=0.1             # background state  \n",
757         "  P = np.zeros(hours)\n",
758         "  P[0] = 0.03 # background state variance\n",
759         "  Q = np.array([0.02]) # process noise variance\n",
760         "  H = np.array([1.])   # all observed\n",
761         "  R = np.array([0.02]) # data variance\n",
762         "\n",
763         "  for t in range(h2):\n",
764         "    # use lambda construction to pass additional arguments to the model \n",
765         "    m[t+1],P[t+1] = ext_kf(m[t],P[t],lambda u: model_decay(u,E[t]+DeltaE,partials=1),Q,\n",
766         "                    d=data[t],H=H,R=R)\n",
767         "  for t in range(h2,hours - 1):\n",
768         "    m[t+1],P[t+1] = ext_kf(m[t],P[t],lambda u: model_decay(u,E[t]+DeltaE,partials=1))\n",
769         "  return m, P\n",
770         " "
771       ]
772     },
773     {
774       "cell_type": "code",
775       "execution_count": null,
776       "metadata": {
777         "id": "d0EFhTPZAlBD",
778         "scrolled": true
779       },
780       "outputs": [],
781       "source": [
782         "DeltaE = 0.0          # bias\n",
783         "m, P = kf_example(DeltaE)\n",
784         "plot_m(m)\n"
785       ]
786     },
787     {
788       "cell_type": "markdown",
789       "metadata": {
790         "id": "vqyB2Yz3uCsD"
791       },
792       "source": [
793         "We have recovered the fuel moisture from data with random noise - we **filtered** the noise out. "
794       ]
795     },
796     {
797       "cell_type": "markdown",
798       "metadata": {
799         "id": "Dl7pBZ9B3Nox"
800       },
801       "source": [
802         "Let's have a look at the evolution of the filter's estimate of the variance $P$. A common problem with the Kalman filter is when the variance converges to zero over time, then, since the filter trusts the model too much, it ignores the observations. Of course, once we switch to forecasting mode, the variance is not of interest. We could keep evolving the variance to bridge over periods when there are no observations, but not in this simplified version."
803       ]
804     },
805     {
806       "cell_type": "code",
807       "execution_count": null,
808       "metadata": {
809         "id": "wRJgbmGLc91g"
810       },
811       "outputs": [],
812       "source": [
813         "%matplotlib inline\n",
814         "plt.figure(figsize=(16,4))\n",
815         "plt.plot(P,linestyle='-',c='b',label='Estimated state variance P')\n",
816         "plt.title('Kalman filtering and forecast on artificial data')\n",
817         "plt.xlabel('Time (hours)') \n",
818         "plt.ylabel('Estimated variance of fuel moisture (%^2)')\n",
819         "plt.legend()"
820       ]
821     },
822     {
823       "cell_type": "markdown",
824       "metadata": {
825         "id": "Ccr-uKbmAlBE"
826       },
827       "source": [
828         "Now what if the model is wrong - different from nature? That is always so in reality. Now suppose that the model and the truth are not the same. That is always the case in reality.  Consider a simple case when the model thinks that the equilibrium $E$ is too high."
829       ]
830     },
831     {
832       "cell_type": "code",
833       "execution_count": null,
834       "metadata": {
835         "id": "spMdGW8oAlBE"
836       },
837       "outputs": [],
838       "source": [
839         "DeltaE = -0.05\n",
840         "m, P = kf_example(DeltaE)\n",
841         "plot_m(m)"
842       ]
843     },
844     {
845       "cell_type": "code",
846       "execution_count": null,
847       "metadata": {
848         "id": "Z_INvPNGCmM2"
849       },
850       "outputs": [],
851       "source": [
852         "DeltaE = 0.05\n",
853         "m, P = kf_example(DeltaE)\n",
854         "plot_m(m)"
855       ]
856     },
857     {
858       "cell_type": "markdown",
859       "metadata": {
860         "id": "DQeF7J8T4j2i"
861       },
862       "source": [
863         "We have found a good estimate of the state $m$, while data is available. Also, the estimated state variance $P$ converges with time - we have *learned* the variance that balances the noise. But for forecasting fuel moisture, we need to continue the fuel moisture model into the future, and we can't have any measurements from future. We only have the equilibrium from weather forecast. And the forecast and the truth disagree - as soon as there is no data to attract the simulation, the model is doing its own thing."
864       ]
865     },
866     {
867       "cell_type": "markdown",
868       "metadata": {
869         "id": "-WMWCDz4DX45"
870       },
871       "source": [
872         "#### 3.2 Model with an augmented state"
873       ]
874     },
875     {
876       "cell_type": "markdown",
877       "metadata": {
878         "id": "jivOYEhiXMi5"
879       },
880       "source": [
881         "In reality, the equilibrium moisture $E$ computed from atmospheric conditions\n",
882         "generally does not agree with the data. We want to add a correction $\\Delta\n",
883         "E$ to $E$ constant in time, and identify the new parameter $\\Delta E$ from data. \n",
884         "Because the Kalman filter identifies state, add the parameter to the state.\n",
885         "Define augmented state $u=\\left[\n",
886         "\\begin{array}\n",
887         "[c]{c}\n",
888         "m\\\\\n",
889         "\\Delta E\n",
890         "\\end{array}\n",
891         "\\right]  .$ Since $\\Delta E$ is constant in time, it satisfies the\n",
892         "differential equation $\\frac{d\\Delta E}{dt}=0.$ So, we want to estimate the\n",
893         "state $u$ governed by the\n",
894         "$$\n",
895         "\\frac{d}{dt}\\left[\n",
896         "\\begin{array}\n",
897         "[c]{c}\n",
898         "m\\\\\n",
899         "\\Delta E\n",
900         "\\end{array}\n",
901         "\\right]  =\\left[\n",
902         "\\begin{array}\n",
903         "[c]{c}\n",
904         "\\frac{E+\\Delta E-m(t)}{T}\\\\\n",
905         "0\n",
906         "\\end{array}\n",
907         "\\right]  ,\n",
908         "$$\n",
909         "which we write as $\\frac{du}{dt}=F(u),$ where\n",
910         "$$\n",
911         "F(u)=\\left[\n",
912         "\\begin{array}\n",
913         "[c]{c}\n",
914         "F_{1}\\left(  u\\right)  \\\\\n",
915         "F_{2}\\left(  u\\right)\n",
916         "\\end{array}\n",
917         "\\right]  =F\\left(  \\left[\n",
918         "\\begin{array}\n",
919         "[c]{c}\n",
920         "m\\\\\n",
921         "\\Delta E\n",
922         "\\end{array}\n",
923         "\\right]  \\right)  =\\left[\n",
924         "\\begin{array}\n",
925         "[c]{c}\n",
926         "\\left(  E+\\Delta E-m(t)\\right)  T_{1}\\\\\n",
927         "0\n",
928         "\\end{array}\n",
929         "\\right]  ,\\quad T_{1}=\\frac{1}{T}.\n",
930         "$$\n",
931         "The Jacobian of $F$ is\n",
932         "$$\n",
933         "\\left[\n",
934         "\\begin{array}\n",
935         "[c]{cc}\n",
936         "\\frac{\\partial F_{1}}{\\partial u_{1}} & \\frac{\\partial F_{1}}{\\partial u_{2}\n",
937         "}\\\\\n",
938         "\\frac{\\partial F_{2}}{\\partial u_{1}} & \\frac{\\partial F_{2}}{\\partial u_{2}}\n",
939         "\\end{array}\n",
940         "\\right]  =\\left[\n",
941         "\\begin{array}\n",
942         "[c]{cc}\n",
943         "\\frac{\\partial m_{1}}{\\partial m_{0}} & \\frac{\\partial m_{1}}{\\partial E}\\\\\n",
944         "\\frac{\\partial\\Delta E}{\\partial m_{0}} & \\frac{\\partial\\Delta E}\n",
945         "{\\partial\\Delta E}\n",
946         "\\end{array}\n",
947         "\\right]  =\\left[\n",
948         "\\begin{array}\n",
949         "[c]{cc}\n",
950         "\\frac{\\partial m_{1}}{\\partial m_{0}} & \\frac{\\partial m_{1}}{\\partial E}\\\\\n",
951         "0 & 1\n",
952         "\\end{array}\n",
953         "\\right]\n",
954         "$$\n",
955         "Here is a function that implements the augmented model $F$. The input is\n",
956         "$u_{0}$. The output is $u_{1}$ and the Jacobian $du_{1}/du_{0}$."
957       ]
958     },
959     {
960       "cell_type": "markdown",
961       "metadata": {
962         "id": "MJ1C_1Omc91s"
963       },
964       "source": [
965         "\n",
966         "Define augmented model function. Also, add use drying, wetting, and rain equilibria"
967       ]
968     },
969     {
970       "cell_type": "code",
971       "execution_count": null,
972       "metadata": {
973         "id": "GHtAaGp9WSHT"
974       },
975       "outputs": [],
976       "source": [
977         "def model_augmented(u0,Ed,Ew,r,t):\n",
978         "    # state u is the vector [m,dE] with dE correction to equilibria Ed and Ew at t\n",
979         "    # \n",
980         "    m0, Ec = u0  # decompose state u0\n",
981         "    # reuse model_moisture(m0,Eqd,Eqw,r,partials=0):\n",
982         "    # arguments:\n",
983         "    # m0         starting fuel moistureb (1)\n",
984         "    # Ed         drying equilibrium      (1) \n",
985         "    # Ew         wetting equilibrium     (1)\n",
986         "    # r          rain intensity          (mm/h)\n",
987         "    # partials = 0, 1, 2\n",
988         "    # returns: same as model_decay\n",
989         "    #   if partials==0: m1 = fuel moisture contents after time 1 hour\n",
990         "    #              ==1: m1, dm0/dm0 \n",
991         "    #              ==2: m1, dm1/dm0, dm1/dE \n",
992         "    m1, dm1_dm0, dm1_dE  = model_moisture(m0,Ed + Ec, Ew + Ec, r, t, partials=2)\n",
993         "    u1 = np.array([m1,Ec])   # dE is just copied\n",
994         "    J =  np.array([[dm1_dm0, dm1_dE],\n",
995         "                   [0.     ,     1.]])\n",
996         "    return u1, J"
997       ]
998     },
999     {
1000       "cell_type": "code",
1001       "execution_count": null,
1002       "metadata": {
1003         "id": "1No3g6HyAEh_"
1004       },
1005       "outputs": [],
1006       "source": [
1007         "def run_augmented_kf(d,Ed,Ew,rain,h2,hours):\n",
1008         "  u = np.zeros((2,hours))\n",
1009         "  u[:,0]=[0.1,0.0]       # initialize,background state  \n",
1010         "  P = np.zeros((2,2,hours))\n",
1011         "  P[:,:,0] = np.array([[1e-3, 0.],\n",
1012         "                      [0.,  1e-3]]) # background state covariance\n",
1013         "  Q = np.array([[1e-3, 0.],\n",
1014         "                [0,  1e-3]]) # process noise covariance\n",
1015         "  H = np.array([[1., 0.]])  # first component observed\n",
1016         "  R = np.array([1e-3]) # data variance\n",
1017         "\n",
1018         "  # ext_kf(u,P,F,Q=0,d=None,H=None,R=None) returns ua, Pa\n",
1019         "\n",
1020         "  # print('initial u=',u,'P=',P)\n",
1021         "  # print('Q=',Q,'H=',H,'R=',R)\n",
1022         "\n",
1023         "  for t in range(1,h2):\n",
1024         "      # use lambda construction to pass additional arguments to the model \n",
1025         "      u[:,t],P[:,:,t] = ext_kf(u[:,t-1],P[:,:,t-1],\n",
1026         "                                  lambda uu: model_augmented(uu,Ed[t],Ew[t],rain[t],t),\n",
1027         "                                  Q,d[t],H=H,R=R)\n",
1028         "      # print('time',t,'data',d[t],'filtered',u[0,t],'Ec',u[1,t])\n",
1029         "  for t in range(h2,hours):\n",
1030         "      u[:,t],P[:,:,t] = ext_kf(u[:,t-1],P[:,:,t-1],\n",
1031         "                                  lambda uu: model_augmented(uu,Ed[t],Ew[t],rain[t],t),\n",
1032         "                                  Q*0.0)\n",
1033         "      # print('time',t,'data',d[t],'forecast',u[0,t],'Ec',u[1,t])\n",
1034         "  return u"
1035       ]
1036     },
1037     {
1038       "cell_type": "code",
1039       "execution_count": null,
1040       "metadata": {
1041         "id": "9vUv1Uc5OfPI"
1042       },
1043       "outputs": [],
1044       "source": [
1045         "def augmented_example(DeltaE):\n",
1046         "  hours=hour.shape[0]\n",
1047         "  h2 = int(hours/2)\n",
1048         "  m, Ec = run_augmented_kf(data,E+DeltaE,E+DeltaE,0*E,h2,hours)  # data, E, hours are global\n",
1049         "  return m, Ec"
1050       ]
1051     },
1052     {
1053       "cell_type": "code",
1054       "execution_count": null,
1055       "metadata": {
1056         "id": "QQnRlereDHbf"
1057       },
1058       "outputs": [],
1059       "source": [
1060         "m, Ec=augmented_example(0.1)\n",
1061         "plot_m(m, Ec)"
1062       ]
1063     },
1064     {
1065       "cell_type": "code",
1066       "execution_count": null,
1067       "metadata": {
1068         "id": "ZOC6bplsIRCx"
1069       },
1070       "outputs": [],
1071       "source": [
1072         "m, Ec=augmented_example(0.0)\n",
1073         "plot_m(m, Ec)"
1074       ]
1075     },
1076     {
1077       "cell_type": "code",
1078       "execution_count": null,
1079       "metadata": {
1080         "id": "xuMx41fXIejB"
1081       },
1082       "outputs": [],
1083       "source": [
1084         "m, Ec=augmented_example(-0.1)\n",
1085         "plot_m(m, Ec)"
1086       ]
1087     },
1088     {
1089       "cell_type": "markdown",
1090       "metadata": {
1091         "id": "ejlc1sbENm6R"
1092       },
1093       "source": [
1094         "## From Kalman filter to neural networks"
1095       ]
1096     },
1097     {
1098       "cell_type": "markdown",
1099       "metadata": {
1100         "id": "6zeGArbmNkAW"
1101       },
1102       "source": [
1103         "In the Kalman filter, at each time step $k$,\n",
1104         "\n",
1105         "* the input state is $u_{k-1}$ size $n$ and its covariance matrix $P_{k-1}$ size $n \\times n$.\n",
1106         "* the model is applied to external data $e_k$ and the input $u_{k-1},P_{k-1}$ produce the forecast $u_k^f$ and its covariance $P^f_k$\n",
1107         "* the new state $u_k$ is found by minimizing $|| u^f_k - u_k||^2_{P^f_k} + ||H u_k - d_k||^2_{R}$   \n",
1108         "* the new state covariance is $P_k = ( (P^f_k)^{-1} + H^\\top R^{-1} H)^{-1}$.\n",
1109         "\n",
1110         "Here, the state consists of \n",
1111         "* the fuel moisture and the adjustment to the equilibrium, dimension 2\n",
1112         "* the covariance matrix of vector of dimension 2, which is symmetric $2 \\times 2$ matrix, given by 3 numbers because it is symmetric\n",
1113         "Thus, the dimension of the state is 2 + 3 = 5. The first component of the state, the fuel moisture, is the quantity of interest, the rest are auxiliary.\n",
1114         "\n",
1115         "\n",
1116         "This can be understood as:\n",
1117         "\n",
1118         "* a mapping $M$ of the 5-dimensional hidden and external data state to a new hidden state:\n",
1119         "$$M:(u_{k-1},P_{k-1},e_k) \\mapsto (u_{k},P_{k})$$\n",
1120         "* retrieving the output (the quantity of interest) as the first component of the hiddent state\n",
1121         "* feeding the hiddent state back to the mapping $M$ for the next step $k+1$\n",
1122         "* training consists of fitting the hidden state to minimize a loss function\n",
1123         "$$\\ell(u_{k},P_{k},d_k,R_k) \\to \\min$$\n",
1124         "\n",
1125         "Note that in the augmented Kalman filter above, the mapping $M$ is fixed and it has a one component of the hidden state as a parameter. To get a better fit, we could increase the number of parameters, e.g., by modeling the moisture in multiple layers, as in van der Kamp et al. (2017) two-layer model.\n"
1126       ]
1127     },
1128     {
1129       "cell_type": "markdown",
1130       "metadata": {
1131         "id": "fk72YB2mjuGk"
1132       },
1133       "source": [
1134         "Building and evaluating RNN"
1135       ]
1136     },
1137     {
1138       "cell_type": "markdown",
1139       "metadata": {
1140         "id": "svt8wUAsSA67"
1141       },
1142       "source": [
1143         "A recurrent neural network (RNN) has a similar information flow but it can be more flexible and look for the best model automatically, i.e., build the model from data. \n",
1144         "\n",
1145         "We'll start by how to evaluate the map, then actually create it later.\n",
1146         "\n",
1147         "Some of the code is from https://machinelearningmastery.com/understanding-simple-recurrent-neural-networks-in-keras/\n"
1148       ]
1149     },
1150     {
1151       "cell_type": "code",
1152       "execution_count": null,
1153       "metadata": {
1154         "id": "H3RTQCDG9q-4"
1155       },
1156       "outputs": [],
1157       "source": [
1158         "import numpy as np\n",
1159         "import tensorflow as tf\n",
1160         "from keras.models import Sequential\n",
1161         "from keras.layers import Dense, SimpleRNN\n",
1162         "from keras.utils.vis_utils import plot_model\n",
1163         "from sklearn.preprocessing import MinMaxScaler\n",
1164         "from sklearn.metrics import mean_squared_error\n",
1165         "import math\n",
1166         "import matplotlib.pyplot as plt\n",
1167         "import tensorflow as tf\n",
1168         "import keras.backend as K"
1169       ]
1170     },
1171     {
1172       "cell_type": "code",
1173       "execution_count": null,
1174       "metadata": {
1175         "id": "pcIU5lWhVEAy"
1176       },
1177       "outputs": [],
1178       "source": [
1179         "def create_RNN(hidden_units, dense_units, input_shape, activation):\n",
1180         "    inputs = tf.keras.Input(shape=input_shape)\n",
1181         "    # https://stackoverflow.com/questions/43448029/how-can-i-print-the-values-of-keras-tensors\n",
1182         "    # inputs2 = K.print_tensor(inputs, message='inputs = ')  # change allso inputs to inputs2 below, must be used\n",
1183         "    x = tf.keras.layers.SimpleRNN(hidden_units, input_shape=input_shape,\n",
1184         "                        activation=activation[0])(inputs)\n",
1185         "    outputs = tf.keras.layers.Dense(dense_units, activation=activation[1])(x)\n",
1186         "    model = tf.keras.Model(inputs=inputs, outputs=outputs)\n",
1187         "    model.compile(loss='mean_squared_error', optimizer='adam')\n",
1188         "    return model"
1189       ]
1190     },
1191     {
1192       "cell_type": "code",
1193       "execution_count": null,
1194       "metadata": {
1195         "id": "lphzeucMfI7L"
1196       },
1197       "outputs": [],
1198       "source": [
1199         "# Demo example\n",
1200         "hidden=5\n",
1201         "features=2\n",
1202         "timesteps=3\n",
1203         "demo_model = create_RNN(hidden_units=hidden, dense_units=1, \n",
1204         "                        input_shape=(timesteps,features), \n",
1205         "                        activation=['linear', 'linear'])\n",
1206         "print(demo_model.summary())\n",
1207         "w = demo_model.get_weights()\n",
1208         "#print(len(w),' weight arrays:',w)\n",
1209         "wname=('wx','wh','bh','wy','by','wz','bz')\n",
1210         "for i in range(len(w)):\n",
1211         "  print(i,':',wname[i],'shape=',w[i].shape)\n",
1212         "wx, wh, bh, wy, by = w\n",
1213         "plot_model(demo_model, to_file='model_plot.png', \n",
1214         "  show_shapes=True, show_layer_names=True,\n",
1215         "  expand_nested=True,)"
1216       ]
1217     },
1218     {
1219       "cell_type": "markdown",
1220       "metadata": {
1221         "id": "uFVQdXL0fODX"
1222       },
1223       "source": [
1224         "The input layer here is just a formality. The input of the hidden layer `simple_rnn` consist of vector passed by the input layer, followed by its own output from the previous time step.\n",
1225         "\n",
1226         "Now let’s do a simple experiment to see how the layers from a SimpleRNN and Dense layer produce an output. Keep this figure in view.\n",
1227         "<img src=\"https://machinelearningmastery.com/wp-content/uploads/2021/09/rnnCode1.png\">"
1228       ]
1229     },
1230     {
1231       "cell_type": "markdown",
1232       "metadata": {
1233         "id": "TcxvQPo1hYip"
1234       },
1235       "source": [
1236         "We’ll input x for three time steps and let the network generate an output. The values of the hidden units at time steps 1, 2 and 3 will be computed. $h(0)$ is initialized to the zero vector. The output $o(3)$ is computed from $h(3)$ and $w(3)$. An activation function is linear, $f(x)=x$, so the update of  $h(k)$  and the output $o(k)$ are given by\n",
1237         "\\begin{align*}\n",
1238         "h\\left(  0\\right)  = &0  \\\\\n",
1239         "h\\left(  k+1\\right)  =& \n",
1240         "x\\left(  k\\right) w_{x}\n",
1241         "  +h(k) w_{h}  + b_{h}\\\\\n",
1242         "o(k+1)=& h(k+1)w_{y} + b_y\n",
1243         "\\end{align*}"
1244       ]
1245     },
1246     {
1247       "cell_type": "code",
1248       "execution_count": null,
1249       "metadata": {
1250         "id": "fqL5TEfpml7q"
1251       },
1252       "outputs": [],
1253       "source": [
1254         "# Reshape the input to sample_size x time_steps x features \n",
1255         "samples=4   # number of samples\n",
1256         "x = tf.reshape(tf.range(samples*timesteps*features),[samples,timesteps,features]) \n",
1257         "print('test input x=',x)\n",
1258         "print('model.predict start')\n",
1259         "y_pred_model = demo_model.predict(x)\n",
1260         "print('model.predict end')\n",
1261         "\n",
1262         "o3=np.zeros([samples,1])\n",
1263         "for i in range(samples):\n",
1264         "  h_0 = np.zeros(hidden)\n",
1265         "  h_1 = np.dot(x[i,0,:], wx) + np.dot(h_0,wh) + bh\n",
1266         "  h_2 = np.dot(x[i,1,:], wx) + np.dot(h_1,wh) + bh\n",
1267         "  h_3 = np.dot(x[i,2,:], wx) + np.dot(h_2,wh) + bh\n",
1268         "  o3[i,0] = np.dot(h_3, wy) + by\n",
1269         "#print('h1 = ', h_1,'h2 = ', h_2,'h3 = ', h_3)\n",
1270         "\n",
1271         "print(\"Prediction from network \", y_pred_model)\n",
1272         "print(\"Prediction from our computation \", o3)"
1273       ]
1274     },
1275     {
1276       "cell_type": "markdown",
1277       "metadata": {
1278         "id": "6qdqOFCvhQL1"
1279       },
1280       "source": [
1281         "The result is the same."
1282       ]
1283     },
1284     {
1285       "cell_type": "markdown",
1286       "metadata": {
1287         "id": "AkyiGlZF0WrM"
1288       },
1289       "source": [
1290         "#### Training and forecasting with the RNN"
1291       ]
1292     },
1293     {
1294       "cell_type": "markdown",
1295       "metadata": {
1296         "id": "e265QFwlw22_"
1297       },
1298       "source": [
1299         "We are given a sequence `x` of inputs size `[train_steps+forecast_steps,features]` and want to train a model so that at step `i` in `range(train_steps)`, the model returns close to `features[i,:]`. The trained model then returns for `i` in `range(train_steps,train_steps+forecast_steps)` a forecast `features[i,:]`."
1300       ]
1301     },
1302     {
1303       "cell_type": "code",
1304       "execution_count": null,
1305       "metadata": {
1306         "id": "owX7OTg-RnMA"
1307       },
1308       "outputs": [],
1309       "source": [
1310         "def staircase(x,y,timesteps,trainsteps,return_sequences=False):\n",
1311         "  # x [trainsteps+forecaststeps,features]    all inputs\n",
1312         "  # y [trainsteps,outputs]\n",
1313         "  # timesteps: split x and y into samples length timesteps, shifted by 1\n",
1314         "  # trainsteps: number of timesteps to use for training, no more than y.shape[0]\n",
1315         "  print('shape x = ',x.shape)\n",
1316         "  print('shape y = ',y.shape)\n",
1317         "  print('timesteps=',timesteps)\n",
1318         "  print('trainsteps=',trainsteps)\n",
1319         "  outputs = y.shape[1]\n",
1320         "  features = x.shape[1]\n",
1321         "  forecaststeps = x.shape[0]-trainsteps\n",
1322         "  samples = trainsteps-timesteps+1\n",
1323         "  print('staircase: samples=',samples,'timesteps=',timesteps,'features=',features)\n",
1324         "  x_train = np.empty([samples, timesteps, features])\n",
1325         "  print('return_sequences=',return_sequences)\n",
1326         "  if return_sequences:\n",
1327         "    print('returning all timesteps in a sample')\n",
1328         "    y_train = np.empty([samples, timesteps, outputs])  # all\n",
1329         "    for i in range(samples):\n",
1330         "      for k in range(timesteps):\n",
1331         "        for j in range(features):\n",
1332         "          x_train[i,k,j] = x[i+k,j]\n",
1333         "        for j in range(outputs):\n",
1334         "          y_train[i,k,j] = y[i+k,j]\n",
1335         "  else:\n",
1336         "    print('returning only the last timestep in a sample')\n",
1337         "    y_train = np.empty([samples, outputs])\n",
1338         "    for i in range(samples):\n",
1339         "      for j in range(features):\n",
1340         "        for k in range(timesteps):\n",
1341         "          x_train[i,k,j] = x[i+k,j]\n",
1342         "      for j in range(outputs):\n",
1343         "        y_train[i,j] = y[i+timesteps-1,j]\n",
1344         "\n",
1345         "  return x_train, y_train"
1346       ]
1347     },
1348     {
1349       "cell_type": "code",
1350       "execution_count": null,
1351       "metadata": {
1352         "id": "FzOotSFf-tPR"
1353       },
1354       "outputs": [],
1355       "source": [
1356         "def seq2batches(x,y,timesteps,trainsteps):\n",
1357         "  # x [trainsteps+forecaststeps,features]    all inputs\n",
1358         "  # y [trainsteps,outputs]\n",
1359         "  # timesteps: split x and y into samples length timesteps, shifted by 1\n",
1360         "  # trainsteps: number of timesteps to use for training, no more than y.shape[0]\n",
1361         "  print('shape x = ',x.shape)\n",
1362         "  print('shape y = ',y.shape)\n",
1363         "  print('timesteps=',timesteps)\n",
1364         "  print('trainsteps=',trainsteps)\n",
1365         "  outputs = y.shape[1]\n",
1366         "  features = x.shape[1]\n",
1367         "  samples= trainsteps - timesteps + 1\n",
1368         "  print('samples=',samples)\n",
1369         "  x_train = np.empty([samples, timesteps, features])\n",
1370         "  y_train = np.empty([samples, timesteps, outputs])  # only the last\n",
1371         "  print('samples=',samples,' timesteps=',timesteps,\n",
1372         "        ' features=',features,' outputs=',outputs)\n",
1373         "  for i in range(samples):\n",
1374         "    for k in range(timesteps):\n",
1375         "      for j in range(features):\n",
1376         "        x_train[i,k,j] = x[i+k,j]\n",
1377         "      for j in range(outputs):\n",
1378         "        y_train[i,k,j] = y[i+k,j]  # return sequences\n",
1379         "  return x_train, y_train"
1380       ]
1381     },
1382     {
1383       "cell_type": "code",
1384       "execution_count": null,
1385       "metadata": {
1386         "id": "Kg7wSrkk-HrE"
1387       },
1388       "outputs": [],
1389       "source": [
1390         "print('test preprocessing for RNN')\n",
1391         "trainsteps=5\n",
1392         "features=1\n",
1393         "outputs=1\n",
1394         "timesteps=3\n",
1395         "x = tf.reshape(tf.range(trainsteps*features),[trainsteps,features])\n",
1396         "y = tf.reshape(tf.range(trainsteps*outputs),[trainsteps,outputs])\n",
1397         "print('x=',x)\n",
1398         "print('y=',y)\n",
1399         "x_train, y_train = staircase(x,y,timesteps,trainsteps)\n",
1400         "print('x_train=',x_train)\n",
1401         "print('y_train=',y_train)\n",
1402         "x_train, y_train = seq2batches(x,y,timesteps,trainsteps)\n",
1403         "print('x_train=',x_train)\n",
1404         "print('y_train=',y_train)"
1405       ]
1406     },
1407     {
1408       "cell_type": "code",
1409       "source": [
1410         "E,m_f,data,hour,h2,DeltaE = create_synthetic_data(days=20,power=4,data_noise=0.01,process_noise=0.0,DeltaE=0.1) "
1411       ],
1412       "metadata": {
1413         "id": "rHWxqARpSO_f"
1414       },
1415       "execution_count": null,
1416       "outputs": []
1417     },
1418     {
1419       "cell_type": "code",
1420       "execution_count": null,
1421       "metadata": {
1422         "id": "vfcxN9JCH5Ku"
1423       },
1424       "outputs": [],
1425       "source": [
1426         "scale=False\n",
1427         "# transform as 2D, (timesteps, features) and (timesteps, outputs)\n",
1428         "Et = np.reshape(E,[E.shape[0],1])\n",
1429         "datat = np.reshape(data,[data.shape[0],1])\n",
1430         "if scale:\n",
1431         "  scalerx = MinMaxScaler()\n",
1432         "  scalerx.fit(Et)\n",
1433         "  Et = scalerx.transform(Et)\n",
1434         "  scalery = MinMaxScaler()\n",
1435         "  scalery.fit(datat)\n",
1436         "  datat = scalery.transform(datat)"
1437       ]
1438     },
1439     {
1440       "cell_type": "code",
1441       "execution_count": null,
1442       "metadata": {
1443         "id": "PaHfJW7mSJE1"
1444       },
1445       "outputs": [],
1446       "source": [
1447         "def custom_loss(y_true, y_pred):\n",
1448         "    # calculate loss, using y_pred\n",
1449         "    return tf.dor(y_true())\n",
1450         "def create_RNN_2(hidden_units, dense_units, activation, stateful=False, \n",
1451         "                 batch_shape=None, input_shape=None, dense_layers=1,\n",
1452         "                 rnn_layers=1,return_sequences=False,\n",
1453         "                 initial_state=None):\n",
1454         "    if stateful:\n",
1455         "      inputs = tf.keras.Input(batch_shape=batch_shape)\n",
1456         "    else:\n",
1457         "      inputs = tf.keras.Input(shape=input_shape)\n",
1458         "    # https://stackoverflow.com/questions/43448029/how-can-i-print-the-values-of-keras-tensors\n",
1459         "    # inputs2 = K.print_tensor(inputs, message='inputs = ')  # change allso inputs to inputs2 below, must be used\n",
1460         "    x = inputs\n",
1461         "    for i in range(rnn_layers):\n",
1462         "      x = tf.keras.layers.SimpleRNN(hidden_units,activation=activation[0],\n",
1463         "              stateful=stateful,return_sequences=return_sequences)(x\n",
1464         "              # ,initial_state=initial_state\n",
1465         "              )\n",
1466         "    # x = tf.keras.layers.Dense(hidden_units, activation=activation[1])(x)\n",
1467         "    for i in range(dense_layers):\n",
1468         "      x = tf.keras.layers.Dense(dense_units, activation=activation[1])(x)\n",
1469         "    model = tf.keras.Model(inputs=inputs, outputs=x)\n",
1470         "    model.compile(loss='mean_squared_error', optimizer='adam')\n",
1471         "    return model"
1472       ]
1473     },
1474     {
1475       "cell_type": "code",
1476       "source": [
1477         "# split data\n",
1478         "return_sequences=False\n",
1479         "shift = 0.\n",
1480         "print('shifting inputs by',shift)\n",
1481         "x_train, y_train = staircase(Et+shift,datat+shift,timesteps=1,trainsteps=h2,\n",
1482         "                             return_sequences=return_sequences)\n",
1483         "print('x_train shape=',x_train.shape)\n",
1484         "samples, timesteps, features = x_train.shape\n",
1485         "print('y_train shape=',y_train.shape)\n",
1486         "# the simplest model possible\n",
1487         "activation=['linear','linear']\n",
1488         "hidden_units=1\n",
1489         "dense_units=1\n",
1490         "dense_layers=1\n",
1491         "features=1\n",
1492         "hours=Et.shape[0]\n",
1493         "h0 = tf.convert_to_tensor(datat[:samples],dtype=tf.float32)\n",
1494         "# print('initial state=',h0)\n",
1495         "# statefull model version for traning\n",
1496         "model_fit=create_RNN_2(hidden_units=hidden_units, \n",
1497         "                        dense_units=dense_units, \n",
1498         "                        batch_shape=(samples,timesteps,features),\n",
1499         "                        stateful=True,\n",
1500         "                        return_sequences=return_sequences,\n",
1501         "                        # initial_state=h0,\n",
1502         "                        activation=activation,\n",
1503         "                        dense_layers=dense_layers)\n",
1504         "# same model stateless for prediction on the entire dataset - to start onlg\n",
1505         "# the real application will switch to prediction after training data end\n",
1506         "# and start from the state there\n",
1507         "print('model_fit input shape',x_train.shape,'output shape',model_fit(x_train).shape)\n",
1508         "\n",
1509         "model_predict=create_RNN_2(hidden_units=hidden_units, dense_units=dense_units,  \n",
1510         "                        input_shape=(hours,features),stateful = False,\n",
1511         "                        return_sequences=True,\n",
1512         "                        activation=activation,dense_layers=dense_layers)\n",
1513         "# model_predict=create_RNN_sequences(hidden_units=1, dense_units=1, input_shape=(hours,1), \n",
1514         "#                        activation=['linear', 'linear'])\n",
1515         "print('model_predict input shape',Et.shape,'output shape',model_predict(Et).shape)\n",
1516         "print(model_predict.summary())\n",
1517         "from keras.utils.vis_utils import plot_model\n",
1518         "plot_model(model_predict, to_file='model_plot.png', \n",
1519         "           show_shapes=True, show_layer_names=True)"
1520       ],
1521       "metadata": {
1522         "id": "ZjJxHiEVL5sJ"
1523       },
1524       "execution_count": null,
1525       "outputs": []
1526     },
1527     {
1528       "cell_type": "code",
1529       "source": [
1530         "# fitting\n",
1531         "w_exact=  [np.array([[1.-np.exp(-0.1)]]), np.array([[np.exp(-0.1)]]), np.array([0.]),np.array([[1.0]]),np.array([-1.*DeltaE])]\n",
1532         "w_initial=[np.array([[1.-np.exp(-0.1)]]), np.array([[np.exp(-0.1)]]), np.array([0.]),np.array([[1.0]]),np.array([0.*DeltaE])]\n",
1533         "model_fit.set_weights(w_initial)\n",
1534         "model_fit.fit(x_train, y_train, epochs=1000, verbose=0,batch_size=samples)\n",
1535         "w_fitted=model_fit.get_weights()\n",
1536         "for i in range(len(w)):\n",
1537         "  print('weight',i,' exact:',w_exact[i],':  initial:',w_initial[i],' fitted:',w_fitted[i])"
1538       ],
1539       "metadata": {
1540         "id": "dE0OHg0tGVE9"
1541       },
1542       "execution_count": null,
1543       "outputs": []
1544     },
1545     {
1546       "cell_type": "code",
1547       "source": [
1548         "def model_eval(w,title):\n",
1549         "  # prediction on the entire dataset from zero state\n",
1550         "  model_predict.set_weights(w)\n",
1551         "  hours=Et.shape[0]\n",
1552         "  print('Et.shape=',Et.shape,'hours=',hours)\n",
1553         "  x_input=np.reshape(Et,(1, hours, 1))\n",
1554         "  y_output = model_predict.predict(x_input)\n",
1555         "  print('x_input.shape=',x_input.shape,'y_output.shape=',y_output.shape)\n",
1556         "  m = np.reshape(y_output,hours) - shift\n",
1557         "  print('weights=',w)\n",
1558         "  if scale:\n",
1559         "    print('scaling')\n",
1560         "    m = scalery.inverse_transform(m)\n",
1561         "  m = np.reshape(m,hours)\n",
1562         "  plot_m(m,title=title)\n",
1563         "  return m"
1564       ],
1565       "metadata": {
1566         "id": "-T8lCS6nBHCj"
1567       },
1568       "execution_count": null,
1569       "outputs": []
1570     },
1571     {
1572       "cell_type": "code",
1573       "execution_count": null,
1574       "metadata": {
1575         "id": "R2jkoZlAIaSb"
1576       },
1577       "outputs": [],
1578       "source": [
1579         "m_fitted=model_eval(w_fitted,'RNN prediction with fitted weights')"
1580       ]
1581     },
1582     {
1583       "cell_type": "code",
1584       "source": [
1585         "m_exact=model_eval(w_exact,'RNN prediction with exact weights')"
1586       ],
1587       "metadata": {
1588         "id": "bO1ewTj9gGvg"
1589       },
1590       "execution_count": null,
1591       "outputs": []
1592     },
1593     {
1594       "cell_type": "code",
1595       "source": [
1596         "m_initial=model_eval(w_initial,'RNN prediction with initial weights')"
1597       ],
1598       "metadata": {
1599         "id": "henChC0cmbuy"
1600       },
1601       "execution_count": null,
1602       "outputs": []
1603     },
1604     {
1605       "cell_type": "code",
1606       "source": [
1607         "out = np.empty((hours,1))\n",
1608         "w=w_exact\n",
1609         "h=0\n",
1610         "for i in range(Et.shape[0]):\n",
1611         "  h=np.dot(Et[i,0],w[0])+np.dot(h,w[1]) + w[2]\n",
1612         "  out[i]=np.dot(h,w[3]) + w[4]\n",
1613         "if scale:\n",
1614         "  print('scaling')\n",
1615         "  out = scalery.inverse_transform(out)\n",
1616         "out=np.reshape(out,hours)\n",
1617         "print('max abs diff',np.max(np.abs(m_exact-out)))\n",
1618         "plot_m(out,title='Hand computed RNN prediction with exact weights')"
1619       ],
1620       "metadata": {
1621         "id": "PZw7DNQD4Inr"
1622       },
1623       "execution_count": null,
1624       "outputs": []
1625     },
1626     {
1627       "cell_type": "markdown",
1628       "metadata": {
1629         "id": "6uXVJj9koGF2"
1630       },
1631       "source": [
1632         "### 3.2 Acquisition and preprocessing of real data"
1633       ]
1634     },
1635     {
1636       "cell_type": "markdown",
1637       "metadata": {
1638         "id": "glIkYrvdhXo6"
1639       },
1640       "source": [
1641         ""
1642       ]
1643     },
1644     {
1645       "cell_type": "markdown",
1646       "metadata": {
1647         "id": "ZUluRmF9tqko"
1648       },
1649       "source": [
1650         ""
1651       ]
1652     },
1653     {
1654       "cell_type": "markdown",
1655       "metadata": {
1656         "id": "q3BpOBuzc91i"
1657       },
1658       "source": [
1659         "Data assimilation for fuel moisture from Remote Automated Weather Stations (RAWS) was developed in Vejmelka et al. (2016). First, they use regression from all RAWS in a given area to extend the data spatially from RAWS to a grid in the whole area, then they run the extended Kalman filter at each grid node. Here, we are interested in a simplified problem: estimate future fuel moisture at a single RAWS location from weather data.  "
1660       ]
1661     },
1662     {
1663       "cell_type": "markdown",
1664       "metadata": {
1665         "id": "c8Y6bL1Yc91i"
1666       },
1667       "source": [
1668         "#### 3.2.1 Acquisition of fuel moisture observations"
1669       ]
1670     },
1671     {
1672       "cell_type": "markdown",
1673       "metadata": {
1674         "id": "0CuXyWBFc91i"
1675       },
1676       "source": [
1677         "We try to load the data from a saved file first. If that fails, retrieve the fuel moisture data from sensors on weather stations in the Mesowest network. Get all stations with fuel moisture data in a spatial box within one hour, then pick one station and retrieve the whole time series."
1678       ]
1679     },
1680     {
1681       "cell_type": "code",
1682       "execution_count": null,
1683       "metadata": {
1684         "id": "LFrlbbMmc91i"
1685       },
1686       "outputs": [],
1687       "source": [
1688         "import json\n",
1689         "jfile = 'raws.json'; vars='fuel_moisture'; case = 1\n",
1690         "# jfile = 'raws2.json'; vars='fuel_moisture,precip_accum_one_hour'; case = 2\n",
1691         "def json_w(j,f):\n",
1692         "  print('writing json file',f)\n",
1693         "  json.dump(j,open(f,'w'),indent=4)\n",
1694         "try:\n",
1695         "    #! wget --no-clobber http://math.ucdenver.edu/~jmandel/data/math4779f21/raws.json\n",
1696         "    j = json.load(open(jfile,'r'))\n",
1697         "    print('loaded from ',jfile)\n",
1698         "    # Take the first station in the boulding box that has data between time_start and time_s2.\n",
1699         "    # Then retrieve data for that station between time_start and time_end\n",
1700         "    time_start = j['time_start']      # start of data time series\n",
1701         "    # time_s2    = j['time_s2']         # end of segment to read coordinates\n",
1702         "    time_end  = j['time_end']         # end of data time series\n",
1703         "    meso_ts  = j['meso_ts']           # get meso observations time series\n",
1704         "    obs_lon =   j['obs_lon']          # where we retrieved observations\n",
1705         "    obs_lat =   j['obs_lat']\n",
1706         "except:\n",
1707         "    print(\"can't read\",jfile,', creating')\n",
1708         "    # set up bounds\n",
1709         "    time_start = \"201806010800\"  # June 1 2018 08:00 in format yyyymmddHHMM\n",
1710         "    time_s2    = \"201806010900\"  # June 1 2018 09:00 in format yyyymmddHHMM \n",
1711         "    time_end   = \"201907200900\"  # June 20 2018 09:00 in format yyyymmddHHMM \n",
1712         "    #time_start=  \"201810230100\"\n",
1713         "    #time_s2=  \"201810230300\"\n",
1714         "    #time_end  =  \"201806022300\"\n",
1715         "    !pip install MesoPy\n",
1716         "    from MesoPy import Meso\n",
1717         "    bounding_box = \"-115, 38, -110, 40\"  # min longtitude, latitude\n",
1718         "    meso_token=\"b40cb52cbdef43ef81329b84e8fd874f\"       # you should get your own if you do more of this\n",
1719         "    m = Meso(meso_token)# create a Meso object\n",
1720         "    print('reading MesoWest fuel moisture data')\n",
1721         "    json_w(m.variables(),'variables.json')\n",
1722         "    meso_obss = m.timeseries(time_start, time_s2, bbox=bounding_box, \n",
1723         "                             showemptystations = '0', vars=vars)   # ask the object for data\n",
1724         "    json_w(meso_obss,'meso_obss.json')                        \n",
1725         "    # pick one station and retrieve the whole time series.\n",
1726         "    station=meso_obss['STATION'][0]\n",
1727         "    json_w(station,'station.json')\n",
1728         "    lon,lat = (float(station['LONGITUDE']),float(station['LATITUDE']))\n",
1729         "    print(station['NAME'],'station',station['STID'],'at',lon,lat)\n",
1730         "    e = 0.01   # tolerance\n",
1731         "    bb = '%s, %s, %s, %s' % (lon - e, lat - e, lon + e, lat + e)\n",
1732         "    print('bounding box',bb)\n",
1733         "    meso_ts = m.timeseries(time_start, time_end, bbox=bb, showemptystations = '0', vars=vars)   # ask the object for data\n",
1734         "    json_w(meso_ts,'meso_ts.json')                        \n",
1735         "    obs_lon, obs_lat = (lon, lat)   # remember station coordinates for later\n",
1736         "    j={'time_start':time_start,'time_s2':time_s2,'time_end':time_end,\n",
1737         "       'meso_ts':meso_ts,'obs_lon':obs_lon,'obs_lat':obs_lat}\n",
1738         "    json_w(j,jfile)\n",
1739         "    print('done')"
1740       ]
1741     },
1742     {
1743       "cell_type": "code",
1744       "execution_count": null,
1745       "metadata": {
1746         "id": "3bXopS3btyz0",
1747         "scrolled": true
1748       },
1749       "outputs": [],
1750       "source": [
1751         "# process the data retrieved for this station\n",
1752         "# print(json.dumps(meso_ts['STATION'][0], indent=4))\n",
1753         "from datetime import datetime, timedelta, time\n",
1754         "import numpy as np\n",
1755         "import matplotlib.pyplot as plt\n",
1756         "import pytz\n",
1757         "station = meso_ts['STATION'][0]\n",
1758         "time_str  = station['OBSERVATIONS']['date_time']\n",
1759         "obs_time = [datetime.strptime(t, '%Y-%m-%dT%H:%M:%SZ').replace(tzinfo=pytz.UTC) for t in time_str]\n",
1760         "start_time = obs_time[0].replace(minute=0)     # remember obs_time and start_time for later\n",
1761         "end_time = obs_time[-1]\n",
1762         "obs_data = np.array(station['OBSERVATIONS'][\"fuel_moisture_set_1\"])\n",
1763         "# obs_data = np.array(station['OBSERVATIONS'][\"fuel_moisture\"])\n",
1764         "# display the data retrieved\n",
1765         "#for o_time,o_data in zip (obs_time,obs_data):\n",
1766         "#    print(o_time,o_data)\n",
1767         "%matplotlib inline\n",
1768         "plt.figure(figsize=(16,4))\n",
1769         "plt.plot(obs_data,linestyle='-',c='k',label='10-h fuel data')\n",
1770         "plt.title(station['STID'] + ' 10 h fuel moisture data')\n",
1771         "plt.xlabel('Time (hours)') \n",
1772         "plt.ylabel('Fuel moisture content (%)')\n",
1773         "plt.legend()\n",
1774         " "
1775       ]
1776     },
1777     {
1778       "cell_type": "code",
1779       "execution_count": null,
1780       "metadata": {
1781         "id": "lJQFB4rAYu9P"
1782       },
1783       "outputs": [],
1784       "source": [
1785         "# %debug"
1786       ]
1787     },
1788     {
1789       "cell_type": "markdown",
1790       "metadata": {
1791         "id": "F7bvUGx993Ae"
1792       },
1793       "source": [
1794         ""
1795       ]
1796     },
1797     {
1798       "cell_type": "markdown",
1799       "metadata": {
1800         "id": "pY4hPeATK9wZ"
1801       },
1802       "source": [
1803         "#### 3.2.2 Acquisition of weather data"
1804       ]
1805     },
1806     {
1807       "cell_type": "markdown",
1808       "metadata": {
1809         "id": "xhyjXqxVN6B2"
1810       },
1811       "source": [
1812         "Our weather data are results from atmospheric models, with assimilated observations from weather stations, satellites, radars, etc. The models can be run in reanalysis mode (for the past, with data for the period modeled)  or in forecast mode (for the future, with only past data assimilated - because future data are not here yet). We use the Real-Time Mesoscale Analysis ([RTMA](https://www.nco.ncep.noaa.gov/pmb/products/rtma/)) interpolated to the RAWS location. RTMA is a real-time product, posted hourly, and available only for few days in the past. We have our own collection of selected RAWS data over past few years, obtained as a side effect of running the fuel moisture modeling software [WRFXPY](https://github.com/openwfm/wrfxpy).\n",
1813         "\n",
1814         "First try to read the data already extracted for this RAWS and staged for download."
1815       ]
1816     },
1817     {
1818       "cell_type": "code",
1819       "execution_count": null,
1820       "metadata": {
1821         "id": "WlqJRP8Vc91o"
1822       },
1823       "outputs": [],
1824       "source": [
1825         "import json\n",
1826         "jfile = 'rtma.json'\n",
1827         "try:\n",
1828         "    ! wget --no-clobber http://math.ucdenver.edu/~jmandel/data/math4779f21/rtma.json\n",
1829         "    j = json.load(open(jfile,'r'))\n",
1830         "    print('loaded from ',jfile)\n",
1831         "    if j['obs_lat']!=obs_lat or j['obs_lon']!=obs_lon:\n",
1832         "      print('lon lat doesnot agree, need to load original RTMA files')\n",
1833         "      read_rtma=True\n",
1834         "    else:\n",
1835         "      read_rtma=False\n",
1836         "except:\n",
1837         "    print(\"can't read\",jfile,', creating')\n",
1838         "    read_rtma=True\n",
1839         "\n",
1840         "print('')"
1841       ]
1842     },
1843     {
1844       "cell_type": "markdown",
1845       "metadata": {
1846         "id": "THI6gElyHOOc"
1847       },
1848       "source": [
1849         "Next, functions to get the files, open as grib, and interpolate to the station coordinates"
1850       ]
1851     },
1852     {
1853       "cell_type": "markdown",
1854       "metadata": {
1855         "id": "2iBNHQg5hPxB"
1856       },
1857       "source": [
1858         "####<font color=red>Note: If read_rtma==True, the notebook will say it crashed when run the first time. This is because it needs to install different version of some python packages and restart runtime. Simply run it again.</fonr>"
1859       ]
1860     },
1861     {
1862       "cell_type": "code",
1863       "execution_count": null,
1864       "metadata": {
1865         "id": "mxZABVDxt0gd"
1866       },
1867       "outputs": [],
1868       "source": [
1869         "# Set up environment to read RTMA gribs\n",
1870         "# we will need current numpy for pygrib - needed on Colab, tensorflow is using numpy 1.19\\\n",
1871         "if read_rtma:\n",
1872         "  import subprocess,os\n",
1873         "  def load_rtma(path,file,reload=0):\n",
1874         "    url='http://math.ucdenver.edu/~jmandel/rtma/' + path \n",
1875         "    if os.path.exists(file):\n",
1876         "      if reload:\n",
1877         "        print(file + ' already exists, removing')\n",
1878         "        os.remove(file)\n",
1879         "      else:\n",
1880         "        print(file + ' already exists, exiting')\n",
1881         "        # add checking size here\n",
1882         "        return 0\n",
1883         "    try:\n",
1884         "      ret = subprocess.check_output(['wget','--no-clobber','--output-document='+ file, url,],stderr=subprocess.STDOUT).decode() # execute command from python strings\n",
1885         "      if os.path.exists(file):\n",
1886         "        print('loaded ' + url + ' as ' + file)\n",
1887         "        return 0\n",
1888         "      else: \n",
1889         "        print('file transfer completed, but the file is missing? ' + url)  \n",
1890         "      return 1\n",
1891         "    except:\n",
1892         "      print('file transfer failed: ' + url)\n",
1893         "      return 2\n"
1894       ]
1895     },
1896     {
1897       "cell_type": "markdown",
1898       "metadata": {
1899         "id": "dQ-uJI2sy6I3"
1900       },
1901       "source": [
1902         "Create a function to transfer RTMA files in GRIB2 format from the stash. The function returns zero if the file transfer succeeded. If the file is not available, it returns a nonzero value. Note: if needed, maybe in future add more sophisticated checks, check the return code of wget and if the file size is correct."
1903       ]
1904     },
1905     {
1906       "cell_type": "code",
1907       "execution_count": null,
1908       "metadata": {
1909         "id": "PL3gxK67AlBI"
1910       },
1911       "outputs": [],
1912       "source": [
1913         "if read_rtma:\n",
1914         "  def rtma_grib(t,var):\n",
1915         "    tpath = '%4i%02i%02i/%02i' % (t.year, t.month, t.day, t.hour)  # remote path on server\n",
1916         "    tstr  = '%4i%02i%02i%02i_' % (t.year, t.month, t.day, t.hour)  # time string for local path\n",
1917         "    gribfile = os.path.join('data',tstr + var + '.grib')\n",
1918         "    remote = tpath + '/' + var + '.grib'\n",
1919         "    if load_rtma(remote,gribfile):\n",
1920         "        print('cannot load remote file',remote,'as',gribfile)\n",
1921         "        return []\n",
1922         "    else:\n",
1923         "        try:\n",
1924         "            gf=GribFile(gribfile)\n",
1925         "            v = np.array(gf[1].values())\n",
1926         "        except:\n",
1927         "            print('cannot read grib file',gribfile)\n",
1928         "            return []\n",
1929         "        print('loaded ',gribfile,' containing array shape ',v.shape)\n",
1930         "        return gf[1]   # grib message\n"
1931       ]
1932     },
1933     {
1934       "cell_type": "code",
1935       "execution_count": null,
1936       "metadata": {
1937         "id": "OY1oTYKlfd17"
1938       },
1939       "outputs": [],
1940       "source": [
1941         "if read_rtma:\n",
1942         "    times = pd.date_range(start=time_start,end=time_end,freq='1H')\n",
1943         "    varnames=['temp','td','precipa']\n",
1944         "    j =    read_interp_rtma(varnames,times,obs_lat,obs_lon)      # temperature\n",
1945         "    for varname in varnames:\n",
1946         "        j[varname]=j[varname].tolist() \n",
1947         "    j['obs_lat']=obs_lat\n",
1948         "    j['obs_lon']=obs_lon\n",
1949         "    json.dump(j,open('rtma.json','w'),indent=4)\n",
1950         "    print('done')"
1951       ]
1952     },
1953     {
1954       "cell_type": "code",
1955       "execution_count": null,
1956       "metadata": {
1957         "id": "ccp10kurAlBI"
1958       },
1959       "outputs": [],
1960       "source": [
1961         "from scipy.interpolate import LinearNDInterpolator, interpn\n",
1962         "from scipy.optimize import root\n",
1963         "def interp_to_lat_lon_slow(lats,lons,v,lat,lon): \n",
1964         "    # on mesh with coordinates lats and lons interpolate v to given lat lon\n",
1965         "    interp=LinearNDInterpolator(list(zip(lats.flatten(),lons.flatten())),v.flatten())\n",
1966         "    return interp(lat,lon)\n",
1967         "def interp_to_lat_lon(lats,lons,v,lat,lon):\n",
1968         "    # on mesh with coordinates lats and lons interpolate v to given lat lon\n",
1969         "    points=(np.array(range(lats.shape[0]),float),np.array(range(lats.shape[1]),float))  # uniform mesh\n",
1970         "    def res(ij):  # interpolation of lons lats on the uniform mesh, to noninteger coordinates   \n",
1971         "       return np.hstack((interpn(points,lats,ij)-lat, interpn(points,lons,ij)-lon))\n",
1972         "    # solve for xi,xj such that lats(xi,xj)=lat lons(xi,xj)=lon, then interpolate to (xi, xj) on uniform grid \n",
1973         "    result = root(res,(0,0)) # solve res(ij) = 0\n",
1974         "    if not result.success:\n",
1975         "        print(result.message)\n",
1976         "        exit(1)\n",
1977         "    return interpn(points,v,result.x) \n"
1978       ]
1979     },
1980     {
1981       "cell_type": "markdown",
1982       "metadata": {
1983         "id": "jvnpq6S5AlBI"
1984       },
1985       "source": [
1986         "The interpolation function needs to  be tested."
1987       ]
1988     },
1989     {
1990       "cell_type": "code",
1991       "execution_count": null,
1992       "metadata": {
1993         "id": "NVMJBYI7AlBI"
1994       },
1995       "outputs": [],
1996       "source": [
1997         "def interp_to_lat_lon_test(lats,lons):\n",
1998         "    print('testing interp_to_lat_lon')\n",
1999         "    vx, vy = np.meshgrid(range(lats.shape[0]),range(lats.shape[1]),indexing='ij')\n",
2000         "    i, j = (1,2)\n",
2001         "    lat,lon = ((lats[i,j]+lats[i+1,j+1])/2,(lons[i,j]+lons[i+1,j+1])/2)\n",
2002         "    vi = interp_to_lat_lon(lats,lons,vx,lat,lon)\n",
2003         "    vj = interp_to_lat_lon(lats,lons,vy,lat,lon)\n",
2004         "    print(vi,vj,'should be about',i+0.5,j+0.5)\n",
2005         "    test_slow = 0\n",
2006         "    if test_slow:\n",
2007         "        print('Testing against the standard slow method scipy.interpolate.LinearNDInterpolator. Please wait...')\n",
2008         "        vi_slow = interp_to_lat_lon_slow(lats,lons,vx,lat,lon)\n",
2009         "        print(vi_slow)\n",
2010         "        vj_slow = interp_to_lat_lon_slow(lats,lons,vy,lat,lon)\n",
2011         "        print(vj_slow)\n",
2012         "        \n",
2013         "#gf = rtma_grib(start_time,'temp')      #  read the first grib file and use it to test interpolation\n",
2014         "#lats, lons = gf.latlons()\n",
2015         "#interp_to_lat_lon_test(lats,lons)\n"
2016       ]
2017     },
2018     {
2019       "cell_type": "code",
2020       "execution_count": null,
2021       "metadata": {
2022         "id": "vt-Mk8fIc91m"
2023       },
2024       "outputs": [],
2025       "source": [
2026         "#%debug\n"
2027       ]
2028     },
2029     {
2030       "cell_type": "markdown",
2031       "metadata": {
2032         "id": "LQbWB_3GAlBI"
2033       },
2034       "source": [
2035         "Now we are ready for a function to read the RTMA files and interpolate to the station coordinates"
2036       ]
2037     },
2038     {
2039       "cell_type": "code",
2040       "execution_count": null,
2041       "metadata": {
2042         "id": "b3JJH3XPAlBI"
2043       },
2044       "outputs": [],
2045       "source": [
2046         "if read_rtma:\n",
2047         "  import pandas as pd, json\n",
2048         "  def read_interp_rtma(varnames,times,lat,lon):\n",
2049         "    # read RTMA from start_time to end_time and interpolate to obs_lat obs_lon\n",
2050         "    ntimes = len(times)\n",
2051         "    time_str = 'time_str'\n",
2052         "    j={time_str:times.strftime('%Y-%m-%d %H:%M').tolist()}\n",
2053         "    for varname in varnames:\n",
2054         "        j[varname]=np.full(ntimes,np.nan)  # initialize array of nans as list\n",
2055         "    n=0\n",
2056         "    for t in times:\n",
2057         "        tim=t.strftime('%Y-%m-%d %H:%M')\n",
2058         "        should_be = j[time_str][n]\n",
2059         "        if tim != should_be:\n",
2060         "            print('n=',n,'time',tim,'expected',should_be)\n",
2061         "            raise 'Invalid time' \n",
2062         "        for varname in varnames:\n",
2063         "            gf = rtma_grib(t,varname)   # read and create grib object, download if needed\n",
2064         "            if gf:\n",
2065         "                lats,lons = gf.latlons()    # coordinates\n",
2066         "                v = gf.values()\n",
2067         "                vi=interp_to_lat_lon(lats,lons,v,lat,lon) # append to array\n",
2068         "                print(varname,'at',t,'interpolated to',lat,lon,' value ',vi)\n",
2069         "                j[varname][n] = vi\n",
2070         "            else:\n",
2071         "                print(varname,'at',t,' could not be loaded')\n",
2072         "        n = n+1\n",
2073         "    return j"
2074       ]
2075     },
2076     {
2077       "cell_type": "code",
2078       "execution_count": null,
2079       "metadata": {
2080         "id": "bMpYIZT6c91o"
2081       },
2082       "outputs": [],
2083       "source": [
2084         "# %debug\n"
2085       ]
2086     },
2087     {
2088       "cell_type": "markdown",
2089       "metadata": {
2090         "id": "KVXBjGA0CiXr"
2091       },
2092       "source": [
2093         "#### 3.2.3 Preprocessing and visualization of the weather data"
2094       ]
2095     },
2096     {
2097       "cell_type": "code",
2098       "execution_count": null,
2099       "metadata": {
2100         "id": "fNA3Vbo1c91o"
2101       },
2102       "outputs": [],
2103       "source": [
2104         "rtma = j\n",
2105         "td = np.array(rtma['td'])\n",
2106         "t2 = np.array(rtma['temp'])\n",
2107         "rain=np.array(rtma['precipa'])\n",
2108         "# compute relative humidity\n",
2109         "rh = 100*np.exp(17.625*243.04*(td - t2) / (243.04 + t2 - 273.15) / (243.0 + td - 273.15))\n",
2110         "Ed = 0.924*rh**0.679 + 0.000499*np.exp(0.1*rh) + 0.18*(21.1 + 273.15 - t2)*(1 - np.exp(-0.115*rh))\n",
2111         "Ew = 0.618*rh**0.753 + 0.000454*np.exp(0.1*rh) + 0.18*(21.1 + 273.15 - t2)*(1 - np.exp(-0.115*rh))"
2112       ]
2113     },
2114     {
2115       "cell_type": "code",
2116       "execution_count": null,
2117       "metadata": {
2118         "id": "tZIK59bJAlBJ"
2119       },
2120       "outputs": [],
2121       "source": [
2122         "%matplotlib inline\n",
2123         "plt.figure(figsize=(16,4))\n",
2124         "plt.plot(t2,linestyle='-',c='k',label='Temperature')\n",
2125         "plt.title(station['STID'] + ' Temperature')\n",
2126         "plt.xlabel('Time (hours)') \n",
2127         "plt.ylabel('Temperature (K)')\n",
2128         "plt.legend()"
2129       ]
2130     },
2131     {
2132       "cell_type": "code",
2133       "execution_count": null,
2134       "metadata": {
2135         "id": "LbyqcuXYAlBJ"
2136       },
2137       "outputs": [],
2138       "source": [
2139         "%matplotlib inline\n",
2140         "plt.figure(figsize=(16,4))\n",
2141         "plt.plot(td,linestyle='-',c='k',label='Dew point')\n",
2142         "plt.title(station['STID'] + ' Dew point (K)')\n",
2143         "plt.xlabel('Time (hours)') \n",
2144         "plt.ylabel('Dew point (K)')\n",
2145         "plt.legend()"
2146       ]
2147     },
2148     {
2149       "cell_type": "code",
2150       "execution_count": null,
2151       "metadata": {
2152         "id": "dfoOK2kSc91p"
2153       },
2154       "outputs": [],
2155       "source": [
2156         "%matplotlib inline\n",
2157         "plt.figure(figsize=(16,4))\n",
2158         "plt.plot(rh,linestyle='-',c='k',label='Dew point')\n",
2159         "plt.title(station['STID'] + ' relative humidity')\n",
2160         "plt.xlabel('Time (hours)') \n",
2161         "plt.ylabel('Relative humidity (%)')\n",
2162         "plt.legend()"
2163       ]
2164     },
2165     {
2166       "cell_type": "code",
2167       "execution_count": null,
2168       "metadata": {
2169         "id": "MWTJ5b2kc91p"
2170       },
2171       "outputs": [],
2172       "source": [
2173         "%matplotlib inline\n",
2174         "plt.figure(figsize=(16,4))\n",
2175         "plt.plot(Ed,linestyle='-',c='r',label='drying equilibrium')\n",
2176         "plt.plot(Ew,linestyle=':',c='b',label='wetting equilibrium')\n",
2177         "plt.title(station['STID'] + ' drying and wetting equilibria')\n",
2178         "plt.xlabel('Time (hours)') \n",
2179         "plt.ylabel('Fuel moisture contents (%)')\n",
2180         "plt.legend()"
2181       ]
2182     },
2183     {
2184       "cell_type": "markdown",
2185       "metadata": {
2186         "id": "jY3_eeBRc91p"
2187       },
2188       "source": [
2189         " "
2190       ]
2191     },
2192     {
2193       "cell_type": "code",
2194       "execution_count": null,
2195       "metadata": {
2196         "id": "PQKSRvRSAlBJ"
2197       },
2198       "outputs": [],
2199       "source": [
2200         "%matplotlib inline\n",
2201         "plt.figure(figsize=(16,4))\n",
2202         "plt.plot(rain,linestyle='-',c='k',label='Precipitation')\n",
2203         "plt.title(station['STID'] + ' Precipitation' )\n",
2204         "plt.xlabel('Time (hours)') \n",
2205         "plt.ylabel('Precipitation (mm/hour)')\n",
2206         "plt.legend()"
2207       ]
2208     },
2209     {
2210       "cell_type": "code",
2211       "execution_count": null,
2212       "metadata": {
2213         "id": "Dwbt4UXfro5x"
2214       },
2215       "outputs": [],
2216       "source": [
2217         "print(rain[1900:2000])"
2218       ]
2219     },
2220     {
2221       "cell_type": "markdown",
2222       "metadata": {
2223         "id": "_yRu_7WvHc6P"
2224       },
2225       "source": [
2226         "Precipitation from RTMA is in kg/m${}^2$. 1m water depth over 1m${}^2$ is 1m${}^3$ with mass 1000 kg thus 1 kg/m${}^2$ is the same as 1 mm of precipitation. RTMA values are accumulations over 1 h so these are values in mm/h. So 9999 mm/h = 10m/h makes no sense. Replace anything over 1m/h by nan and try again."
2227       ]
2228     },
2229     {
2230       "cell_type": "code",
2231       "execution_count": null,
2232       "metadata": {
2233         "id": "XPYO_Iuvc91q"
2234       },
2235       "outputs": [],
2236       "source": [
2237         "rain[rain > 1000] = np.NaN"
2238       ]
2239     },
2240     {
2241       "cell_type": "code",
2242       "execution_count": null,
2243       "metadata": {
2244         "id": "GYWTfbBBc91q",
2245         "scrolled": true
2246       },
2247       "outputs": [],
2248       "source": [
2249         "%matplotlib inline\n",
2250         "plt.figure(figsize=(16,4))\n",
2251         "plt.plot(rain,linestyle='-',c='k',label='Precipitation')\n",
2252         "plt.title(station['STID'] + ' Precipitation' )\n",
2253         "plt.xlabel('Time (hours)') \n",
2254         "plt.ylabel('Precipitation (mm/hour)')\n",
2255         "plt.legend()"
2256       ]
2257     },
2258     {
2259       "cell_type": "markdown",
2260       "metadata": {
2261         "id": "Q_L0R2Njc91q"
2262       },
2263       "source": [
2264         "Fix some missing data, then we can use the data for up to 1942 hours until a biger gap."
2265       ]
2266     },
2267     {
2268       "cell_type": "code",
2269       "execution_count": null,
2270       "metadata": {
2271         "id": "_tkU7UJic91q"
2272       },
2273       "outputs": [],
2274       "source": [
2275         "# fix isolated nans\n",
2276         "def fixnan(a,n):\n",
2277         "    for c in range(n):\n",
2278         "        for i in np.where(np.isnan(a)):\n",
2279         "            a[i]=0.5*(a[i-1]+a[i+1])\n",
2280         "        if not any(np.isnan(a)):\n",
2281         "            break\n",
2282         "    return a\n",
2283         "\n",
2284         "rain=fixnan(rain,2)\n",
2285         "t2=fixnan(t2,2)\n",
2286         "rh=fixnan(rh,2)\n",
2287         "obs_data=fixnan(obs_data,2)\n",
2288         "Ed=fixnan(Ed,2)\n",
2289         "Ew=fixnan(Ew,2)\n",
2290         "\n",
2291         "print(np.where(np.isnan(rain)))\n",
2292         "print(np.where(np.isnan(t2)))\n",
2293         "print(np.where(np.isnan(rh)))\n",
2294         "print(np.where(np.isnan(obs_data)))"
2295       ]
2296     },
2297     {
2298       "cell_type": "markdown",
2299       "metadata": {
2300         "id": "XqQYnyI9DIy1"
2301       },
2302       "source": [
2303         "## 4 Results"
2304       ]
2305     },
2306     {
2307       "cell_type": "markdown",
2308       "metadata": {
2309         "id": "2tIC_Tqnc91r"
2310       },
2311       "source": [
2312         "### 4.1 Kalman filter with fuel moisture observations, followed by forecasting\n",
2313         "We run the model first with Kalman filter for 150 hours. The observations are the RAWS data\n",
2314         "After 150 hours, we run in forecast mode - the RAWS data are no longer used, and we run the model from the weather data without the Kalman filter. The weather data are taken to be RTMA interpolated to one RAWS location.\n",
2315         "In a real forecasting application, the model would be run from weather forecast rather than data."
2316       ]
2317     },
2318     {
2319       "cell_type": "code",
2320       "execution_count": null,
2321       "metadata": {
2322         "id": "aXnSQM7wc91r"
2323       },
2324       "outputs": [],
2325       "source": [
2326         "# run KF on an initial data seqment\n",
2327         "import numpy as np\n",
2328         "import matplotlib.pyplot as plt \n",
2329         "\n",
2330         "hours=1200 # total simulation\n",
2331         "h2 = 300\n",
2332         "m = np.zeros(hours) # preallocate\n",
2333         "m[0]= obs_data[0]             # initial state  \n",
2334         "P = np.zeros(hours)\n",
2335         "P[0] = 1e-3 # background state variance\n",
2336         "H = np.array([1.])   # all oQ = np.array([0.02]) # process noise variancebserved\n",
2337         "Q = np.array([1e-3]) # process noise variance\n",
2338         "R = np.array([1e-3]) # data variance\n",
2339         "for t in range(hours-1):\n",
2340         "    # using lambda construction to pass additional arguments to the model \n",
2341         "    if t < h2 and not np.isnan(obs_data[t]) and not np.isnan(Ew[t]) and not np.isnan(rain[t]): # advance model and run KF\n",
2342         "        m[t+1],P[t+1] = ext_kf(m[t],P[t],lambda u: model_moisture(u,Ed[t],Ew[t],rain[t],t,partials=1),Q,\n",
2343         "                    d=obs_data[t],H=H,R=R)\n",
2344         "    else:  # just advance to next hour, no process noise\n",
2345         "        m[t+1],P[t+1] = ext_kf(m[t],P[t],lambda u: model_moisture(u,Ed[t],Ew[t],rain[t],t,partials=1),Q*0.0)"
2346       ]
2347     },
2348     {
2349       "cell_type": "code",
2350       "execution_count": null,
2351       "metadata": {
2352         "id": "peMi-OF3c91r",
2353         "scrolled": true
2354       },
2355       "outputs": [],
2356       "source": [
2357         "%matplotlib inline\n",
2358         "plt.figure(figsize=(16,4))\n",
2359         "plt.plot(Ed[:hours],linestyle='--',c='r',label='Drying Equilibrium')\n",
2360         "plt.plot(Ew[:hours],linestyle='--',c='b',label='Wetting Equilibrium')\n",
2361         "plt.plot(obs_data[:hours],linestyle=':',c='k',label='RAWS data')\n",
2362         "plt.plot(m[:h2],linestyle='-',c='k',label='filtered')\n",
2363         "plt.plot(range(h2,hours),m[h2:hours],linestyle='-',c='r',label='forecast')\n",
2364         "plt.title(station['STID'] + ' Kalman filtering and forecast with real data')\n",
2365         "plt.xlabel('Time (hours)') \n",
2366         "plt.ylabel('Fuel moisture content (%)')\n",
2367         "plt.legend()"
2368       ]
2369     },
2370     {
2371       "cell_type": "markdown",
2372       "metadata": {
2373         "id": "3TnwXYcLc91r"
2374       },
2375       "source": [
2376         "Clearly, there is a problem - the forecast fuel moisture is too high. We need to assimilate also some parameters of the model, not just its output state. "
2377       ]
2378     },
2379     {
2380       "cell_type": "markdown",
2381       "metadata": {
2382         "id": "8SuVNg8TsW4d"
2383       },
2384       "source": [
2385         "### 4.3 Kalman filter on the augmented model"
2386       ]
2387     },
2388     {
2389       "cell_type": "markdown",
2390       "metadata": {
2391         "id": "FYAbWNCfk2wD"
2392       },
2393       "source": [
2394         "Run augmented filter and plot the result:\n"
2395       ]
2396     },
2397     {
2398       "cell_type": "code",
2399       "execution_count": null,
2400       "metadata": {
2401         "id": "Q3NHr3oBsDg6"
2402       },
2403       "outputs": [],
2404       "source": [
2405         "m,Ec = run_augmented_kf(obs_data,Ed,Ew,rain,h2,hours)  # extract from state"
2406       ]
2407     },
2408     {
2409       "cell_type": "code",
2410       "execution_count": null,
2411       "metadata": {
2412         "id": "hlkby_oTlB_f"
2413       },
2414       "outputs": [],
2415       "source": [
2416         "def plot_moisture(hmin,hmax):\n",
2417         "  print('training from 0 to',h2,'plot from',hmin,'to',hmax)\n",
2418         "  plt.figure(figsize=(16,4))\n",
2419         "  plt.plot(range(hmin,hmax),Ed[hmin:hmax],linestyle='--',c='r',label='Drying Equilibrium (%)')\n",
2420         "  plt.plot(range(hmin,hmax),Ew[hmin:hmax],linestyle='--',c='b',label='Wetting Equilibrium (%)')\n",
2421         "  plt.plot(range(hmin,hmax),Ec[hmin:hmax],linestyle='--',c='g',label='Equilibrium Correction (%)')\n",
2422         "  plt.plot(range(hmin,hmax),obs_data[hmin:hmax],linestyle='-',c='b',label='RAWS data (%)')\n",
2423         "  plt.plot(range(hmin,hmax),rain[hmin:hmax],linestyle='-',c='b',label='RTMA rain (mm/h)')\n",
2424         "  if hmin>=h2:\n",
2425         "    plt.plot(m[hmin:h2],linestyle='-',c='k',label='Filtered')\n",
2426         "  h1 = np.maximum(hmin,h2)\n",
2427         "  plt.plot(range(h1,hmax),m[h1:hmax],linestyle='-',c='r',label='Forecast (%)')\n",
2428         "  plt.title(station['STID'] +' Kalman filtering and forecast with augmented state, real data. Training 0:%i hmax' % h2)\n",
2429         "  plt.xlabel('Time (hours)') \n",
2430         "  plt.ylabel('Fuel moisture content (%)')\n",
2431         "  plt.legend()"
2432       ]
2433     },
2434     {
2435       "cell_type": "code",
2436       "execution_count": null,
2437       "metadata": {
2438         "id": "q-h5omKgnow2"
2439       },
2440       "outputs": [],
2441       "source": [
2442         "plot_moisture(0,hours)"
2443       ]
2444     },
2445     {
2446       "cell_type": "markdown",
2447       "metadata": {
2448         "id": "0w0YtHtqnza5"
2449       },
2450       "source": [
2451         "A detailed view of transition from training to forecast:"
2452       ]
2453     },
2454     {
2455       "cell_type": "code",
2456       "execution_count": null,
2457       "metadata": {
2458         "id": "B7sXGUotc91s"
2459       },
2460       "outputs": [],
2461       "source": [
2462         "plot_moisture(0,600)\n",
2463         "\n"
2464       ]
2465     },
2466     {
2467       "cell_type": "code",
2468       "execution_count": null,
2469       "metadata": {
2470         "id": "xy7sIs0z_Kk6"
2471       },
2472       "outputs": [],
2473       "source": [
2474         "plot_moisture(300,800)"
2475       ]
2476     },
2477     {
2478       "cell_type": "code",
2479       "execution_count": null,
2480       "metadata": {
2481         "id": "TvlCtT0X2ejp"
2482       },
2483       "outputs": [],
2484       "source": [
2485         "plot_moisture(800,1200)"
2486       ]
2487     },
2488     {
2489       "cell_type": "markdown",
2490       "metadata": {
2491         "id": "7W03QTo3c91t"
2492       },
2493       "source": [
2494         "Filtering by extended Kalman filter using RAWS data until 150 hours, then forecasting mode - running the model from interpolated RTMA only. For the first 60 hours the forecast is good, the equilibium correction made the model quite close to data. But then the big spike in equilibrium moisture around 230 hours attracted the solution, and it took a while for it to get back. The spike in the RAWS measurement is there but much smaller. The model becomes inaccurate during periods when the fuel moisture equilibrium is large.\n",
2495         "\n",
2496         "Possible reasons include: 1. There was something in the data we do not know about - maybe it rained but RTMA did not tell us. Try comparing with data from the RAWS itself? 2. The model is too simple, assumes the whole depth of the wood stick is wetting and drying at the same time. Perhaps the moisture got stored in the inside layers of the measurement stick. Try a two-layer model as in van der Kamp (2017) and make the state larger? "
2497       ]
2498     },
2499     {
2500       "cell_type": "markdown",
2501       "metadata": {
2502         "id": "owEI4EtTo7Ek"
2503       },
2504       "source": [
2505         "A detailed view of rain episode:"
2506       ]
2507     },
2508     {
2509       "cell_type": "code",
2510       "execution_count": null,
2511       "metadata": {
2512         "id": "C_hoDjgtpMEJ"
2513       },
2514       "outputs": [],
2515       "source": [
2516         "plot_moisture(900,1100)"
2517       ]
2518     },
2519     {
2520       "cell_type": "markdown",
2521       "metadata": {
2522         "id": "DRraWhwdpSkV"
2523       },
2524       "source": [
2525         "It seems there is some rain that the model does not know about."
2526       ]
2527     },
2528     {
2529       "cell_type": "markdown",
2530       "metadata": {
2531         "id": "gVQxv9Blc91t"
2532       },
2533       "source": [
2534         "### 4.4 A comment on the information flow in the Kalman filter and in neural networks"
2535       ]
2536     },
2537     {
2538       "cell_type": "markdown",
2539       "metadata": {
2540         "id": "CZmR4HPAc91t"
2541       },
2542       "source": [
2543         ""
2544       ]
2545     },
2546     {
2547       "cell_type": "markdown",
2548       "metadata": {
2549         "id": "_g_OTEg6ePb9"
2550       },
2551       "source": [
2552         "## 5. Conclusion"
2553       ]
2554     },
2555     {
2556       "cell_type": "markdown",
2557       "metadata": {
2558         "id": "aNxw7xI3FqFt"
2559       },
2560       "source": [
2561         "We have shown how to combine a model and data for improved forecasting of fuel moisture from weather forecast using the Kalman filter. Augmenting the filter state by a model parameter and joint estimation of augmented state resulted in an improvement of the forecast."
2562       ]
2563     },
2564     {
2565       "cell_type": "markdown",
2566       "metadata": {
2567         "id": "IWpmDwUPGElR"
2568       },
2569       "source": [
2570         "## Contributions of authors "
2571       ]
2572     },
2573     {
2574       "cell_type": "markdown",
2575       "metadata": {
2576         "id": "jujW1VFgGOCn"
2577       },
2578       "source": [
2579         "Not applicable."
2580       ]
2581     },
2582     {
2583       "cell_type": "markdown",
2584       "metadata": {
2585         "id": "HWslw7HmGZmP"
2586       },
2587       "source": [
2588         "## Acknowledgements"
2589       ]
2590     },
2591     {
2592       "cell_type": "markdown",
2593       "metadata": {
2594         "id": "xubqDAV2GjkZ"
2595       },
2596       "source": [
2597         "This Math Clinic was sponsored by the team of investigators of the NASA grant no. 80NSSC19K1091 *Coupled Interactive Forecasting of Weather, Fire Behavior, and Smoke Impact for Improved Wildland Fire Decision Making* under the NASA ROSES18 Disasters program. The author would like to thank Brian Zhang from the Math Clinic class for bringing the reference van der Kamp et al. (2017) to his attention."
2598       ]
2599     },
2600     {
2601       "cell_type": "markdown",
2602       "metadata": {
2603         "id": "ZsNZxOv7c91t"
2604       },
2605       "source": [
2606         "## References"
2607       ]
2608     },
2609     {
2610       "cell_type": "markdown",
2611       "metadata": {
2612         "id": "vFY-iS1Wc91t"
2613       },
2614       "source": [
2615         "J. Mandel, S. Amram, J. D. Beezley, G. Kelman, A. K. Kochanski, V. Y. Kondratenko, B. H. Lynn, B. Regev, and M. Vejmelka. *Recent advances and applications of WRF-SFIRE.* Natural Hazards and Earth System Science, 14(10):2829–2845, 2014. [doi:10.5194/nhessd-2-1759-2014](https://doi.org/10.5194/nhessd-2-1759-2014)\n",
2616         "\n",
2617         "R. E. Kalman. *A new approach to linear filtering and prediction problems.* Transactions of the ASME – Journal of Basic Engineering, Series D, 82:35–45, 1960. [doi:10.1115/1.3662552](https://doi.org/10.1115/1.3662552)\n",
2618         "\n",
2619         "E. Kalnay. *Atmospheric Modeling, Data Assimilation and Predictability.* Cambridge University Press, 2003. [doi:10.1017/CBO9780511802270](https://doi.org/10.1017/CBO9780511802270)\n",
2620         "\n",
2621         "D. W. van der Kamp, R. D. Moore, and I. G. McKendry. *A model for simulating the moisture content of standardized fuel sticks of various sizes.* Agricultural and Forest Meteorology, 236:123–134, 2017. [doi:10.1016/j.agrformet.2017.01.013](https://doi.org/10.1016/j.agrformet.2017.01.013)\n",
2622         "\n",
2623         "S. F. Schmidt. *Application of state-space methods to navigation problems.* volume 3 of Advances in Control Systems, C. T.  Leondes, ed., pages 293–340. Elsevier, 1966. [doi:10.1016/B978-1-4831-6716-9.50011-4](https://doi.org/10.1016/B978-1-4831-6716-9.50011-4)\n",
2624         "\n",
2625         "M. Vejmelka, A. K. Kochanski, and J. Mandel. *Data assimilation of dead fuel moisture observations from remote automatic weather stations.* International Journal of Wildland Fire, 25:558– 568, 2016. [doi:10.1071/WF14085](https://doi.org/10.1071/WF14085)\n"
2626       ]
2627     }
2628   ],
2629   "metadata": {
2630     "colab": {
2631       "collapsed_sections": [],
2632       "name": "fmda_kf_rnn.ipynb",
2633       "provenance": []
2634     },
2635     "kernelspec": {
2636       "display_name": "Python 3",
2637       "language": "python",
2638       "name": "python3"
2639     },
2640     "language_info": {
2641       "codemirror_mode": {
2642         "name": "ipython",
2643         "version": 3
2644       },
2645       "file_extension": ".py",
2646       "mimetype": "text/x-python",
2647       "name": "python",
2648       "nbconvert_exporter": "python",
2649       "pygments_lexer": "ipython3",
2650       "version": "3.8.5"
2651     },
2652     "accelerator": "GPU",
2653     "gpuClass": "standard"
2654   },
2655   "nbformat": 4,
2656   "nbformat_minor": 0