-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexploring-canadian-temperature-station-data.html
11034 lines (10701 loc) · 758 KB
/
exploring-canadian-temperature-station-data.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en"
>
<head>
<title>Exploring Canadian temperature station data - NCS</title>
<!-- Using the latest rendering mode for IE -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
/*some stuff for output/input prompts*/
div.cell{border:1px solid transparent;display:-webkit-box;-webkit-box-orient:vertical;-webkit-box-align:stretch;display:-moz-box;-moz-box-orient:vertical;-moz-box-align:stretch;display:box;box-orient:vertical;box-align:stretch;display:flex;flex-direction:column;align-items:stretch}div.cell.selected{border-radius:4px;border:thin #ababab solid}
div.cell.edit_mode{border-radius:4px;border:thin #008000 solid}
div.cell{width:100%;padding:5px 5px 5px 0;margin:0;outline:none}
div.prompt{min-width:11ex;padding:.4em;margin:0;font-family:monospace;text-align:right;line-height:1.21429em}
@media (max-width:480px){div.prompt{text-align:left}}div.inner_cell{display:-webkit-box;-webkit-box-orient:vertical;-webkit-box-align:stretch;display:-moz-box;-moz-box-orient:vertical;-moz-box-align:stretch;display:box;box-orient:vertical;box-align:stretch;display:flex;flex-direction:column;align-items:stretch;-webkit-box-flex:1;-moz-box-flex:1;box-flex:1;flex:1}
div.input_area{border:1px solid #cfcfcf;border-radius:4px;background:#f7f7f7;line-height:1.21429em}
div.prompt:empty{padding-top:0;padding-bottom:0}
div.input{page-break-inside:avoid;display:-webkit-box;-webkit-box-orient:horizontal;-webkit-box-align:stretch;display:-moz-box;-moz-box-orient:horizontal;-moz-box-align:stretch;display:box;box-orient:horizontal;box-align:stretch;}
div.inner_cell{width:90%;}
div.input_area{border:1px solid #cfcfcf;border-radius:4px;background:#f7f7f7;}
div.input_prompt{color:navy;border-top:1px solid transparent;}
div.output_wrapper{margin-top:5px;position:relative;display:-webkit-box;-webkit-box-orient:vertical;-webkit-box-align:stretch;display:-moz-box;-moz-box-orient:vertical;-moz-box-align:stretch;display:box;box-orient:vertical;box-align:stretch;width:100%;}
div.output_scroll{height:24em;width:100%;overflow:auto;border-radius:4px;-webkit-box-shadow:inset 0 2px 8px rgba(0, 0, 0, 0.8);-moz-box-shadow:inset 0 2px 8px rgba(0, 0, 0, 0.8);box-shadow:inset 0 2px 8px rgba(0, 0, 0, 0.8);}
div.output_collapsed{margin:0px;padding:0px;display:-webkit-box;-webkit-box-orient:vertical;-webkit-box-align:stretch;display:-moz-box;-moz-box-orient:vertical;-moz-box-align:stretch;display:box;box-orient:vertical;box-align:stretch;width:100%;}
div.out_prompt_overlay{height:100%;padding:0px 0.4em;position:absolute;border-radius:4px;}
div.out_prompt_overlay:hover{-webkit-box-shadow:inset 0 0 1px #000000;-moz-box-shadow:inset 0 0 1px #000000;box-shadow:inset 0 0 1px #000000;background:rgba(240, 240, 240, 0.5);}
div.output_prompt{color:darkred;}
a.anchor-link:link{text-decoration:none;padding:0px 20px;visibility:hidden;}
h1:hover .anchor-link,h2:hover .anchor-link,h3:hover .anchor-link,h4:hover .anchor-link,h5:hover .anchor-link,h6:hover .anchor-link{visibility:visible;}
/* end stuff for output/input prompts*/
.highlight-ipynb .hll { background-color: #ffffcc }
.highlight-ipynb { background: #f8f8f8; }
.highlight-ipynb .c { color: #408080; font-style: italic } /* Comment */
.highlight-ipynb .err { border: 1px solid #FF0000 } /* Error */
.highlight-ipynb .k { color: #008000; font-weight: bold } /* Keyword */
.highlight-ipynb .o { color: #666666 } /* Operator */
.highlight-ipynb .cm { color: #408080; font-style: italic } /* Comment.Multiline */
.highlight-ipynb .cp { color: #BC7A00 } /* Comment.Preproc */
.highlight-ipynb .c1 { color: #408080; font-style: italic } /* Comment.Single */
.highlight-ipynb .cs { color: #408080; font-style: italic } /* Comment.Special */
.highlight-ipynb .gd { color: #A00000 } /* Generic.Deleted */
.highlight-ipynb .ge { font-style: italic } /* Generic.Emph */
.highlight-ipynb .gr { color: #FF0000 } /* Generic.Error */
.highlight-ipynb .gh { color: #000080; font-weight: bold } /* Generic.Heading */
.highlight-ipynb .gi { color: #00A000 } /* Generic.Inserted */
.highlight-ipynb .go { color: #888888 } /* Generic.Output */
.highlight-ipynb .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
.highlight-ipynb .gs { font-weight: bold } /* Generic.Strong */
.highlight-ipynb .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.highlight-ipynb .gt { color: #0044DD } /* Generic.Traceback */
.highlight-ipynb .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
.highlight-ipynb .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
.highlight-ipynb .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
.highlight-ipynb .kp { color: #008000 } /* Keyword.Pseudo */
.highlight-ipynb .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
.highlight-ipynb .kt { color: #B00040 } /* Keyword.Type */
.highlight-ipynb .m { color: #666666 } /* Literal.Number */
.highlight-ipynb .s { color: #BA2121 } /* Literal.String */
.highlight-ipynb .na { color: #7D9029 } /* Name.Attribute */
.highlight-ipynb .nb { color: #008000 } /* Name.Builtin */
.highlight-ipynb .nc { color: #0000FF; font-weight: bold } /* Name.Class */
.highlight-ipynb .no { color: #880000 } /* Name.Constant */
.highlight-ipynb .nd { color: #AA22FF } /* Name.Decorator */
.highlight-ipynb .ni { color: #999999; font-weight: bold } /* Name.Entity */
.highlight-ipynb .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
.highlight-ipynb .nf { color: #0000FF } /* Name.Function */
.highlight-ipynb .nl { color: #A0A000 } /* Name.Label */
.highlight-ipynb .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
.highlight-ipynb .nt { color: #008000; font-weight: bold } /* Name.Tag */
.highlight-ipynb .nv { color: #19177C } /* Name.Variable */
.highlight-ipynb .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
.highlight-ipynb .w { color: #bbbbbb } /* Text.Whitespace */
.highlight-ipynb .mf { color: #666666 } /* Literal.Number.Float */
.highlight-ipynb .mh { color: #666666 } /* Literal.Number.Hex */
.highlight-ipynb .mi { color: #666666 } /* Literal.Number.Integer */
.highlight-ipynb .mo { color: #666666 } /* Literal.Number.Oct */
.highlight-ipynb .sb { color: #BA2121 } /* Literal.String.Backtick */
.highlight-ipynb .sc { color: #BA2121 } /* Literal.String.Char */
.highlight-ipynb .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
.highlight-ipynb .s2 { color: #BA2121 } /* Literal.String.Double */
.highlight-ipynb .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
.highlight-ipynb .sh { color: #BA2121 } /* Literal.String.Heredoc */
.highlight-ipynb .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
.highlight-ipynb .sx { color: #008000 } /* Literal.String.Other */
.highlight-ipynb .sr { color: #BB6688 } /* Literal.String.Regex */
.highlight-ipynb .s1 { color: #BA2121 } /* Literal.String.Single */
.highlight-ipynb .ss { color: #19177C } /* Literal.String.Symbol */
.highlight-ipynb .bp { color: #008000 } /* Name.Builtin.Pseudo */
.highlight-ipynb .vc { color: #19177C } /* Name.Variable.Class */
.highlight-ipynb .vg { color: #19177C } /* Name.Variable.Global */
.highlight-ipynb .vi { color: #19177C } /* Name.Variable.Instance */
.highlight-ipynb .il { color: #666666 } /* Literal.Number.Integer.Long */
</style>
<style type="text/css">
/* Overrides of notebook CSS for static HTML export */
div.entry-content {
overflow: visible;
padding: 8px;
}
.input_area {
padding: 0.2em;
}
a.heading-anchor {
white-space: normal;
}
.rendered_html
code {
font-size: .8em;
}
pre.ipynb {
color: black;
background: #f7f7f7;
border: none;
box-shadow: none;
margin-bottom: 0;
padding: 0;
margin: 0px;
font-size: 13px;
}
/* remove the prompt div from text cells */
div.text_cell .prompt {
display: none;
}
/* remove horizontal padding from text cells, */
/* so it aligns with outer body text */
div.text_cell_render {
padding: 0.5em 0em;
}
img.anim_icon{padding:0; border:0; vertical-align:middle; -webkit-box-shadow:none; -box-shadow:none}
</style>
<script src="https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS_HTML" type="text/javascript"></script>
<script type="text/javascript">
init_mathjax = function() {
if (window.MathJax) {
// MathJax loaded
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ]
},
displayAlign: 'left', // Change this to 'center' to center equations.
"HTML-CSS": {
styles: {'.MathJax_Display': {"margin": 0}}
}
});
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
}
}
init_mathjax();
</script>
<link rel="canonical" href="./exploring-canadian-temperature-station-data.html">
<meta name="author" content="NCS" />
<meta name="keywords" content="python,pandas,weather,EC station data,tutorial" />
<meta name="description" content="Exploring homogenized Canadian monthly mean temperatures This post serves the dual purpose of being a follow-on tutorial on using pandas to look at weather data, and also as an exploratory post on how appropriate the homogenized data might be for some of my projects. The background is that I've ..." />
<!-- Enable latex plugin -->
<!-- Bootstrap -->
<link rel="stylesheet" href="./theme/css/bootstrap.min.css" type="text/css"/>
<link href="./theme/css/font-awesome.min.css" rel="stylesheet">
<link href="./theme/css/pygments/manni.css" rel="stylesheet">
<link rel="stylesheet" href="./theme/css/style.css" type="text/css"/>
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="./" class="navbar-brand">NCS</a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li class="active">
<a href="./category/python-coding.html">Python & coding</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<p class="navbar-text">
<a class="navbar-link social-nav-link" href="http://twitter.com/Neil_C_Swart"><i class="fa fa-twitter fa-lg"></i></a>
<a class="navbar-link social-nav-link" href="https://github.com/swartn"><i class="fa fa-github fa-lg"></i></a>
</p>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
</div> <!-- /.navbar -->
<div class="container">
<div class="row">
<div class="col-sm-9">
<section id="content">
<article>
<header class="page-header">
<h1>
<a href="./exploring-canadian-temperature-station-data.html"
rel="bookmark"
title="Permalink to Exploring Canadian temperature station data">
Exploring Canadian temperature station data
</a>
</h1>
</header>
<div class="entry-content">
<div class="panel">
<div class="panel-body">
<footer class="post-info">
<span class="label label-default">Date</span>
<span class="published">
<i class="fa fa-calendar"></i><time datetime="2014-11-10T00:00:00"> Mon 10 November 2014</time>
</span>
<span class="label label-default">Tags</span>
<a href="./tag/python.html">python</a>
/
<a href="./tag/pandas.html">pandas</a>
/
<a href="./tag/weather.html">weather</a>
/
<a href="./tag/ec-station-data.html">EC station data</a>
/
<a href="./tag/tutorial.html">tutorial</a>
</footer><!-- /.post-info --> </div>
</div>
<p>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h1 id="exploring-homogenized-canadian-monthly-mean-temperatures">Exploring homogenized Canadian monthly mean temperatures</h1>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>This post serves the dual purpose of being a follow-on tutorial on using pandas to look at weather data, and also as an exploratory post on how appropriate the homogenized data might be for some of my projects.</p>
<p>The background is that I've been working on a dynamic, django based website, that would allows users to look at plots of the near real time-time weather being recorded by Environment Canada (EC) stations, and to compare it to historical norms and extremes. In a previous post <a href="http://swartn.github.io/weather-with-pandas.html">Weather with Pandas</a>, I discussed some techniques for accessing this EC weather station data directly from the API on the EC-weather site. There is a major problem, it turns out. The weather available on the EC site is basically raw station data. Every few years, old stations are replaced with new ones and the type of equipment used to make the measurements might change, the site's position might change by a few metres or more, and the site might dissappear entirely. Whenever such a change is made to a site, EC gives it a new station code and even sometimes a new name. Thus for any one location there may be tens of "stations", which all cover diffrent periods, with slightly different measurement characteristics. All these changes mean that it is basically impossible to use the "raw" data available through the EC-weather API to calculate long term averages, extremes, or trends. Basically you cannot use it for climate analysis.</p>
<p>All is not lost though. Scientists in the EC Climate Research Division have produced "homogenized" data which does not have any of these problems. It's not available for every variable, but there is surface temperature. To produce these homogenized surface temperatures for any one location, let's say Shawnigan Lake, all the stations are joined into one record, which is carefully checked for consistency, and "homogenized" so that there are no jumps due to changes in equipment or location etc. There are lots of challenges in producing this homogenized data, that are fully described in the documentation and associated publications, and I won't consider any of those details. Here, I want to explore this homogenized surface temperature data just to get a feel for it. The data is available here:</p>
<pre><code>http://www.ec.gc.ca/dccha-ahccd/default.asp?lang=en&n=1EEECD01-1</code></pre>
<p>I downloaded the documentation, station list, and "Monthly mean of daily mean temperature". The last of these is a zip archive that you need to extract. So what I have is:</p>
<pre><code>Temperature_Stations.xls
Homog_monthly_mean_temp/</code></pre>
<p>Within Homog_monthly_mean_temp/ there are about 340 txt files, starting with "mm" for "monthly mean", then a number indicating the station id. Those files contain the station id, name and the data, but not much else info. on the station. More detail on the stations (location etc) is provided in Temperature_Stations.xls. We'll use pandas to examine the timeseries as before, and we'll use basemap to map the station locations. The objectives here are to:</p>
<ol style="list-style-type: decimal">
<li>Figure out how to load in the station names etc from the xls file.</li>
<li>Set up a good system for loading in the data from the .txt files into a dataframe.</li>
<li>Look at the properties of one station (Vancouver) in some detail: we'll calculate a climatology, and look at some extremes.</li>
<li>Look at all the data from across Canada, and look for some interesting signals, mostly a quick and dirty look at the global warming signal.</li>
</ol>
<p>Before we start, if you want to download this notebook you can find it on my <a href="https://github.com/swartn/weather-with-pandas.git">github</a>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In [1]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight-ipynb"><pre class="ipynb"><span class="c"># Start off importing some basic python stuff that we will need.</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="kn">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">mpl_toolkits.basemap</span> <span class="kn">import</span> <span class="n">Basemap</span>
<span class="c"># plotting stuff</span>
<span class="o">%</span><span class="k">pylab</span> <span class="n">inline</span>
<span class="c"># set the plot fontsize</span>
<span class="n">font</span> <span class="o">=</span> <span class="p">{</span><span class="s">'size'</span> <span class="p">:</span> <span class="mi">14</span><span class="p">}</span>
<span class="n">matplotlib</span><span class="o">.</span><span class="n">rc</span><span class="p">(</span><span class="s">'font'</span><span class="p">,</span> <span class="o">**</span><span class="n">font</span><span class="p">)</span>
<span class="c"># Set default figure size for this notebook</span>
<span class="n">pylab</span><span class="o">.</span><span class="n">rcParams</span><span class="p">[</span><span class="s">'figure.figsize'</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="mf">8.0</span><span class="p">,</span> <span class="mf">6.4</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>
Populating the interactive namespace from numpy and matplotlib
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>We can load in the station information using the pandas read_excel function. There file has English and French headers. I just want to keep the English ones, so I'll specify the header column accordingly as 2, and to skip row 3 (French).</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In [2]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight-ipynb"><pre class="ipynb"><span class="n">stns</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_excel</span><span class="p">(</span><span class="s">'Temperature_Stations.xls'</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">skiprows</span><span class="o">=</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Lets look at the first ten rows to get a feeling for the type of information, and get the shape so that we know how many stations that there are.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In [3]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight-ipynb"><pre class="ipynb"><span class="n">stns</span><span class="o">.</span><span class="n">head</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt output_prompt">
Out[3]:</div>
<div class="output_html rendered_html output_subarea output_pyout">
<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Prov</th>
<th>station's name</th>
<th>stnid</th>
<th>beg yr</th>
<th>beg mon </th>
<th>end yr</th>
<th>end mon</th>
<th>lat (deg)</th>
<th>long (deg)</th>
<th>elev (m)</th>
<th>stns joined</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td> BC</td>
<td> AGASSIZ</td>
<td> 1100120</td>
<td> 1893</td>
<td> 1</td>
<td> 2012</td>
<td> 12</td>
<td> 49.25</td>
<td>-121.77</td>
<td> 15</td>
<td> N</td>
</tr>
<tr>
<th>1</th>
<td> BC</td>
<td> ATLIN</td>
<td> 1200560</td>
<td> 1905</td>
<td> 8</td>
<td> 2012</td>
<td> 12</td>
<td> 59.57</td>
<td>-133.70</td>
<td> 674</td>
<td> N</td>
</tr>
<tr>
<th>2</th>
<td> BC</td>
<td> BARKERVILLE</td>
<td> 1090660</td>
<td> 1888</td>
<td> 2</td>
<td> 2012</td>
<td> 10</td>
<td> 53.07</td>
<td>-121.52</td>
<td> 1265</td>
<td> N</td>
</tr>
<tr>
<th>3</th>
<td> BC</td>
<td> BEAVERDELL</td>
<td> 1130771</td>
<td> 1939</td>
<td> 1</td>
<td> 2006</td>
<td> 9</td>
<td> 49.48</td>
<td>-119.05</td>
<td> 838</td>
<td> Y</td>
</tr>
<tr>
<th>4</th>
<td> BC</td>
<td> BELLA COOLA</td>
<td> 1060841</td>
<td> 1895</td>
<td> 5</td>
<td> 2012</td>
<td> 12</td>
<td> 52.37</td>
<td>-126.68</td>
<td> 18</td>
<td> Y</td>
</tr>
<tr>
<th>5</th>
<td> BC</td>
<td> BLIND CHANNEL</td>
<td> 1021480</td>
<td> 1958</td>
<td> 7</td>
<td> 2012</td>
<td> 9</td>
<td> 50.42</td>
<td>-125.50</td>
<td> 23</td>
<td> N</td>
</tr>
<tr>
<th>6</th>
<td> BC</td>
<td> BLUE RIVER</td>
<td> 1160899</td>
<td> 1946</td>
<td> 9</td>
<td> 2012</td>
<td> 12</td>
<td> 52.13</td>
<td>-119.28</td>
<td> 683</td>
<td> Y</td>
</tr>
<tr>
<th>7</th>
<td> BC</td>
<td> CAPE SCOTT</td>
<td> 1031353</td>
<td> 1921</td>
<td> 7</td>
<td> 2012</td>
<td> 1</td>
<td> 50.78</td>
<td>-128.43</td>
<td> 72</td>
<td> Y</td>
</tr>
<tr>
<th>8</th>
<td> BC</td>
<td> CAPE ST JAMES</td>
<td> 1051351</td>
<td> 1944</td>
<td> 12</td>
<td> 2012</td>
<td> 12</td>
<td> 51.93</td>
<td>-131.02</td>
<td> 89</td>
<td> Y</td>
</tr>
<tr>
<th>9</th>
<td> BC</td>
<td> CARIBOO LODGE</td>
<td> 1171393</td>
<td> 1914</td>
<td> 1</td>
<td> 2012</td>
<td> 8</td>
<td> 52.72</td>
<td>-119.47</td>
<td> 1096</td>
<td> Y</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In [4]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight-ipynb"><pre class="ipynb"><span class="n">stns</span><span class="o">.</span><span class="n">shape</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt output_prompt">
Out[4]:</div>
<div class="output_text output_subarea output_pyout">
<pre>
(338, 11)
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Okay so we have 338 stations across Canada, and information on the station name, id, province, date-span and 3D position. Lets find info on Vancouver stations so that we can explore those further. We can use the <em>str</em> method and <em>contains</em> to find the station by name.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In [5]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight-ipynb"><pre class="ipynb"><span class="n">stns</span><span class="p">[</span> <span class="n">stns</span><span class="p">[</span><span class="s">"station's name"</span><span class="p">]</span><span class="o">.</span><span class="n">str</span><span class="o">.</span><span class="n">contains</span><span class="p">(</span><span class="s">'VANCOUVER'</span><span class="p">)</span> <span class="p">]</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt output_prompt">
Out[5]:</div>
<div class="output_html rendered_html output_subarea output_pyout">
<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Prov</th>
<th>station's name</th>
<th>stnid</th>
<th>beg yr</th>
<th>beg mon </th>
<th>end yr</th>
<th>end mon</th>
<th>lat (deg)</th>
<th>long (deg)</th>
<th>elev (m)</th>
<th>stns joined</th>
</tr>
</thead>
<tbody>
<tr>
<th>47</th>
<td> BC</td>
<td> VANCOUVER</td>
<td> 1108447</td>
<td> 1896</td>
<td> 2</td>
<td> 2012</td>
<td> 12</td>
<td> 49.2</td>
<td>-123.18</td>
<td> 4</td>
<td> Y</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>So now that we know the stnid for Vancouver, we can load in the corresponding .txt file and have a look. I'm going to skip rows 0,1 and 3, since those contain the French header and other unwanted stuff.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In [6]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight-ipynb"><pre class="ipynb"><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s">'Homog_monthly_mean_temp/mm1108447.txt'</span><span class="p">,</span><span class="n">skiprows</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">]</span>
<span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">skipinitialspace</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">na_values</span><span class="o">=-</span><span class="mf">9999.9</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In [7]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight-ipynb"><pre class="ipynb"><span class="n">s</span><span class="o">.</span><span class="n">head</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt output_prompt">
Out[7]:</div>
<div class="output_html rendered_html output_subarea output_pyout">
<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Year</th>
<th>Jan</th>
<th>Unnamed: 2</th>
<th>Feb</th>
<th>Unnamed: 4</th>
<th>Mar</th>
<th>Unnamed: 6</th>
<th>Apr</th>
<th>Unnamed: 8</th>
<th>May</th>
<th>...</th>
<th>Unnamed: 24</th>
<th>Annual</th>
<th>Unnamed: 26</th>
<th>Winter</th>
<th>Unnamed: 28</th>
<th>Spring</th>
<th>Unnamed: 30</th>
<th>Summer</th>
<th>Unnamed: 32</th>
<th>Autumn</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td> 1896</td>
<td> NaN</td>
<td> M</td>
<td> 4.8</td>
<td> NaN</td>
<td> 4.0</td>
<td> NaN</td>
<td> 7.3</td>
<td> NaN</td>
<td> 10.4</td>
<td>...</td>
<td> NaN</td>
<td> NaN</td>
<td> M</td>
<td> NaN</td>
<td> M</td>
<td> 7.2</td>
<td> NaN</td>
<td> 15.3</td>
<td> NaN</td>
<td> 7.1</td>
</tr>
<tr>
<th>1</th>
<td> 1897</td>
<td> 3.0</td>
<td> NaN</td>
<td> 3.9</td>
<td> NaN</td>
<td> 3.2</td>
<td> NaN</td>
<td> 8.9</td>
<td> NaN</td>
<td> 12.5</td>
<td>...</td>
<td> NaN</td>
<td> 9.0</td>
<td> NaN</td>
<td> 4.1</td>
<td> NaN</td>
<td> 8.2</td>
<td> NaN</td>
<td> 15.7</td>
<td> NaN</td>
<td> 8.4</td>
</tr>
<tr>
<th>2</th>
<td> 1898</td>
<td> 2.8</td>
<td> NaN</td>
<td> 5.6</td>
<td> NaN</td>
<td> 4.2</td>
<td> NaN</td>
<td> 8.1</td>
<td> NaN</td>
<td> 11.7</td>
<td>...</td>
<td> NaN</td>
<td> 9.1</td>
<td> NaN</td>
<td> 4.2</td>
<td> NaN</td>
<td> 8.0</td>
<td> NaN</td>
<td> 15.5</td>
<td> NaN</td>
<td> 9.1</td>
</tr>
<tr>
<th>3</th>
<td> 1899</td>
<td> 2.1</td>
<td> NaN</td>
<td> 1.5</td>
<td> NaN</td>
<td> 4.5</td>
<td> NaN</td>
<td> 7.6</td>
<td> NaN</td>
<td> 9.2</td>
<td>...</td>
<td> NaN</td>
<td> 8.6</td>
<td> NaN</td>
<td> 2.2</td>
<td> NaN</td>
<td> 7.1</td>
<td> NaN</td>
<td> 14.3</td>
<td> NaN</td>
<td> 10.1</td>
</tr>
<tr>
<th>4</th>
<td> 1900</td>
<td> 4.7</td>
<td> NaN</td>
<td> 3.2</td>
<td> NaN</td>
<td> 7.1</td>
<td> NaN</td>
<td> 9.0</td>
<td> NaN</td>
<td> 12.0</td>
<td>...</td>
<td> NaN</td>
<td> 9.2</td>
<td> NaN</td>
<td> 4.3</td>
<td> NaN</td>
<td> 9.4</td>
<td> NaN</td>
<td> 15.1</td>
<td> NaN</td>
<td> 8.1</td>
</tr>
</tbody>
</table>
<p>5 rows × 34 columns</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>There are columns for each month, as well as the seasonal and annual means. Above we saw that there are columns ("Unnamed") with letters ("a", "M" etc) following each column of data. Those letters are flags for the quality of the data, and are described in the documentation. For this exercise lets just assume that all the data is of good quality, and get rid of the flag columns. One way to filter a dataframe is by using regular expressions. For example, if we just wanted to get the columns for months starting with a "M", we could do this:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In [8]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight-ipynb"><pre class="ipynb"><span class="n">s</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">regex</span><span class="o">=</span><span class="s">'^M'</span><span class="p">)</span><span class="o">.</span><span class="n">head</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt output_prompt">
Out[8]:</div>
<div class="output_html rendered_html output_subarea output_pyout">
<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Mar</th>
<th>May</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td> 4.0</td>
<td> 10.4</td>
</tr>
<tr>
<th>1</th>
<td> 3.2</td>
<td> 12.5</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>So above we used the filter method, and used a regular expression to find all columns starting with "M". The carrot (^) means "starting with". Regular expressions are a powerful tool used for pattern matching in all types of programming. What we want to do here is just find the columns of s that contain letters of the months (and y for year). We'll use a regex to do a match of any letters. To indicate the range we use square brackets, like this []. We'll also use the plus sign to indicate match "one or more" letters. Here goes:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In [9]:
</div>
<div class="inner_cell">
<div class="input_area">
<div class="highlight-ipynb"><pre class="ipynb"><span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">regex</span><span class="o">=</span><span class="s">'[JFMASONDYW]+'</span><span class="p">)</span>
<span class="n">s</span><span class="o">.</span><span class="n">head</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt output_prompt">
Out[9]:</div>
<div class="output_html rendered_html output_subarea output_pyout">
<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Year</th>
<th>Jan</th>
<th>Feb</th>
<th>Mar</th>
<th>Apr</th>
<th>May</th>
<th>Jun</th>
<th>Jul</th>
<th>Aug</th>
<th>Sep</th>
<th>Oct</th>
<th>Nov</th>
<th>Dec</th>
<th>Annual</th>
<th>Winter</th>