forked from AdamWilsonLabEDU/SpatialDataScience
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09_RemoteSensing_appeears.html
More file actions
1420 lines (1318 loc) · 43.9 KB
/
09_RemoteSensing_appeears.html
File metadata and controls
1420 lines (1318 loc) · 43.9 KB
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 xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<title>Satellite Remote Sensing</title>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/cosmo.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/textmate.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<link href="site_libs/font-awesome-5.0.13/css/fa-svg-with-js.css" rel="stylesheet" />
<script src="site_libs/font-awesome-5.0.13/js/fontawesome-all.min.js"></script>
<script src="site_libs/font-awesome-5.0.13/js/fa-v4-shims.min.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
<link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
height: auto;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
</style>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 51px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 56px;
margin-top: -56px;
}
.section h2 {
padding-top: 56px;
margin-top: -56px;
}
.section h3 {
padding-top: 56px;
margin-top: -56px;
}
.section h4 {
padding-top: 56px;
margin-top: -56px;
}
.section h5 {
padding-top: 56px;
margin-top: -56px;
}
.section h6 {
padding-top: 56px;
margin-top: -56px;
}
</style>
<script>
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<div class="container-fluid main-container">
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_').toLowerCase();
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
padding-left: 25px;
text-indent: 0;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">GEO 503: Spatial Data Science</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="Syllabus.html">Syllabus</a>
</li>
<li>
<a href="Schedule.html">Schedule</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Content
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="CourseContent.html">About Course Content</a>
</li>
<li class="dropdown-header">Module 1: Introduction to R</li>
<li>
<a href="00_CourseIntroductionFrame.html">00 Course Introduction</a>
</li>
<li>
<a href="01_Rintro.html">01 First Steps</a>
</li>
<li>
<a href="02_graphics.html">02 Graphics</a>
</li>
<li>
<a href="03_DataWrangling.html">03 Data Wrangling</a>
</li>
<li>
<a href="03b_DataWrangling.html">03 Data Wrangling 2</a>
</li>
<li class="divider"></li>
<li class="dropdown-header">Module 2: Spatial Analyses</li>
<li>
<a href="04_Spatial_with_sf.html">04 Spatial Data with sf</a>
</li>
<li>
<a href="05_Raster.html">05 Spatial Raster Data</a>
</li>
<li class="divider"></li>
<li class="dropdown-header">Module 3: Advanced Programming</li>
<li>
<a href="06_CreatingWorkflows.html">06 Creating Workflows</a>
</li>
<li>
<a href="07_Reproducibile.html">07 Reproducible Research</a>
</li>
<li>
<a href="08_WeatherData.html">08 Weather Data Processing</a>
</li>
<li>
<a href="09_RemoteSensing_appeears.html">09 Satellite Data Processing</a>
</li>
<li>
<a href="11_ParallelProcessing.html">11 Parallel Processing</a>
</li>
<li>
<a href="12_DynamicVisualization.html">12 Dynamic Visualization</a>
</li>
<li>
<a href="13_SDM_Exercise.html">13 Species Distribution Modeling</a>
</li>
<li class="divider"></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Assignments
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="Tasklist.html">Task list</a>
</li>
<li>
<a href="DataCamp.html">DataCamp</a>
</li>
<li>
<a href="PackageIntro.html">Package Introduction</a>
</li>
<li>
<a href="Project.html">Final Project</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Resources
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="Provenance.html">Provenance</a>
</li>
<li>
<a href="Projects_2017.html">2017 Final Projects</a>
</li>
<li>
<a href="Resources.html">Resources</a>
</li>
<li>
<a href="GitSSHNotes.html">Setting up Github</a>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="https://github.com/adammwilson/RDataScience">
<span class="fa fa-github fa-lg"></span>
</a>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">Satellite Remote Sensing</h1>
</div>
<div>
<iframe src="09_presentation/09_RemoteSensing.html" width="75%" height="400px">
</iframe>
</div>
<p><a href="scripts/09_RemoteSensing_appeears.R"><i class="fa fa-file-code-o fa-3x" aria-hidden="true"></i> The R Script associated with this page is available here</a>. Download this file and open it (or copy-paste into a new script) with RStudio so you can follow along.</p>
<div id="libraries" class="section level3">
<h3>Libraries</h3>
<pre class="r"><code>library(raster)
library(rasterVis)
library(rgdal)
library(ggplot2)
library(ggmap)
library(dplyr)
library(knitr)
library(tidyr)
library(DataScienceData)
# New Packages
library(gdalUtils)
library(rts)</code></pre>
</div>
<div id="identify-and-create-download-folders" class="section level2">
<h2>Identify (and create) download folders</h2>
<p>Today we’ll work with:</p>
<ul>
<li>Land Surface Temperature (<code>lst</code>): MOD11A2</li>
<li>Land Cover (<code>lc</code>): MCD12Q1</li>
</ul>
</div>
<div id="land-use-land-cover" class="section level2">
<h2>Land Use Land Cover</h2>
<p>You will need to update the DataScienceData package before the command below will work. Run <code>devtools::install_github("adammwilson/DataScienceData"); library(DataScienceData)</code>. If that doesn’t work, you can download the needed files directly from <a href="https://github.com/adammwilson/DataScienceData/tree/master/inst/extdata/appeears">here</a>.</p>
<pre class="r"><code>lulcf=system.file("extdata",
"appeears/MCD12Q1.051_aid0001.nc",
package = "DataScienceData")
lulcf</code></pre>
<pre><code>## [1] "/Library/Frameworks/R.framework/Versions/3.4/Resources/library/DataScienceData/extdata/appeears/MCD12Q1.051_aid0001.nc"</code></pre>
<pre class="r"><code>#IF that doesn't work</code></pre>
<pre class="r"><code>lulc=stack(lulcf,varname="Land_Cover_Type_1")
plot(lulc)</code></pre>
<p><img src="09_RemoteSensing_appeears_files/figure-html/unnamed-chunk-4-1.png" /><!-- --></p>
<p>We’ll just pick one year to work with to keep this simple:</p>
<pre class="r"><code>lulc=lulc[[13]]
plot(lulc)</code></pre>
<p><img src="09_RemoteSensing_appeears_files/figure-html/unnamed-chunk-5-1.png" /><!-- --></p>
<div id="process-landcover-data" class="section level3">
<h3>Process landcover data</h3>
<p>Get cover clases from <a href="https://lpdaac.usgs.gov/dataset_discovery/modis/modis_products_table/mcd12q1">MODIS website</a></p>
<pre class="r"><code> Land_Cover_Type_1 = c(
Water = 0,
`Evergreen Needleleaf forest` = 1,
`Evergreen Broadleaf forest` = 2,
`Deciduous Needleleaf forest` = 3,
`Deciduous Broadleaf forest` = 4,
`Mixed forest` = 5,
`Closed shrublands` = 6,
`Open shrublands` = 7,
`Woody savannas` = 8,
Savannas = 9,
Grasslands = 10,
`Permanent wetlands` = 11,
Croplands = 12,
`Urban & built-up` = 13,
`Cropland/Natural vegetation mosaic` = 14,
`Snow & ice` = 15,
`Barren/Sparsely vegetated` = 16,
Unclassified = 254,
NoDataFill = 255)
lcd=data.frame(
ID=Land_Cover_Type_1,
landcover=names(Land_Cover_Type_1),
col=c("#000080","#008000","#00FF00", "#99CC00","#99FF99", "#339966", "#993366", "#FFCC99", "#CCFFCC", "#FFCC00", "#FF9900", "#006699", "#FFFF00", "#FF0000", "#999966", "#FFFFFF", "#808080", "#000000", "#000000"),
stringsAsFactors = F)
# colors from https://lpdaac.usgs.gov/about/news_archive/modisterra_land_cover_types_yearly_l3_global_005deg_cmg_mod12c1
kable(head(lcd))</code></pre>
<table>
<thead>
<tr class="header">
<th></th>
<th align="right">ID</th>
<th align="left">landcover</th>
<th align="left">col</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Water</td>
<td align="right">0</td>
<td align="left">Water</td>
<td align="left">#000080</td>
</tr>
<tr class="even">
<td>Evergreen Needleleaf forest</td>
<td align="right">1</td>
<td align="left">Evergreen Needleleaf forest</td>
<td align="left">#008000</td>
</tr>
<tr class="odd">
<td>Evergreen Broadleaf forest</td>
<td align="right">2</td>
<td align="left">Evergreen Broadleaf forest</td>
<td align="left">#00FF00</td>
</tr>
<tr class="even">
<td>Deciduous Needleleaf forest</td>
<td align="right">3</td>
<td align="left">Deciduous Needleleaf forest</td>
<td align="left">#99CC00</td>
</tr>
<tr class="odd">
<td>Deciduous Broadleaf forest</td>
<td align="right">4</td>
<td align="left">Deciduous Broadleaf forest</td>
<td align="left">#99FF99</td>
</tr>
<tr class="even">
<td>Mixed forest</td>
<td align="right">5</td>
<td align="left">Mixed forest</td>
<td align="left">#339966</td>
</tr>
</tbody>
</table>
<p>Convert LULC raster into a ‘factor’ (categorical) raster. This requires building the Raster Attribute Table (RAT). Unfortunately, this is a bit of manual process as follows.</p>
<pre class="r"><code># convert to raster (easy)
lulc=as.factor(lulc)
# update the RAT with a left join
levels(lulc)=left_join(levels(lulc)[[1]],lcd)</code></pre>
<pre><code>## Joining, by = "ID"</code></pre>
<pre class="r"><code># plot it
gplot(lulc)+
geom_raster(aes(fill=as.factor(value)))+
scale_fill_manual(values=levels(lulc)[[1]]$col,
labels=levels(lulc)[[1]]$landcover,
name="Landcover Type")+
coord_equal()+
theme(legend.position = "bottom")+
guides(fill=guide_legend(ncol=1,byrow=TRUE))</code></pre>
<p><img src="09_RemoteSensing_appeears_files/figure-html/unnamed-chunk-8-1.png" /><!-- --></p>
</div>
</div>
<div id="land-surface-temperature" class="section level2">
<h2>Land Surface Temperature</h2>
<pre class="r"><code>lstf=system.file("extdata",
"appeears/MOD11A2.006_aid0001.nc",
package = "DataScienceData")
lstf
lst=stack(lstf,varname="LST_Day_1km")
plot(lst[[1:12]])</code></pre>
<p><img src="09_RemoteSensing_appeears_files/figure-html/unnamed-chunk-9-1.png" /><!-- --></p>
<p>You may get a warning about some attributes being 8-byte converted to double precisions. You can ignore these warnings.</p>
</div>
<div id="convert-lst-to-degrees-c" class="section level2">
<h2>Convert LST to Degrees C</h2>
<p>You can convert LST from Degrees Kelvin (K) to Celcius (C) with <code>offs()</code>.</p>
<pre class="r"><code>offs(lst)=-273.15
plot(lst[[1:10]])</code></pre>
<p><img src="09_RemoteSensing_appeears_files/figure-html/unnamed-chunk-10-1.png" /><!-- --></p>
</div>
<div id="modland-quality-control" class="section level1">
<h1>MODLAND Quality control</h1>
<p>See a detailed explaination <a href="https://lpdaac.usgs.gov/sites/default/files/public/modis/docs/MODIS_LP_QA_Tutorial-1b.pdf">here</a>. Some code below from <a href="https://stevemosher.wordpress.com/2012/12/05/modis-qc-bits/">Steven Mosher’s blog</a>.</p>
<div id="mod11a2-land-surface-temperature-quality-control" class="section level2">
<h2>MOD11A2 (Land Surface Temperature) Quality Control</h2>
<p><a href="https://lpdaac.usgs.gov/dataset_discovery/modis/modis_products_table/mod11a2">MOD11A2 QC Layer table</a></p>
<p><img src="09_presentation/09_assets/lst_qc.png" /></p>
<pre class="r"><code>lstqc=stack(lstf,varname="QC_Day")
plot(lstqc[[1:2]])</code></pre>
<p><img src="09_RemoteSensing_appeears_files/figure-html/unnamed-chunk-11-1.png" /><!-- --></p>
<div id="lst-qc-data" class="section level3">
<h3>LST QC data</h3>
<p>QC data are encoded in 8-bit ‘words’ to compress information.</p>
<pre class="r"><code>values(lstqc[[1:2]])%>%table()</code></pre>
<pre><code>## .
## 2 17 33 65 81 97 145
## 1569 8 5 675 335 4 90</code></pre>
<p><img src="09_presentation/09_assets/QCdata.png" /></p>
<pre class="r"><code>intToBits(65)</code></pre>
<pre><code>## [1] 01 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
## [24] 00 00 00 00 00 00 00 00 00</code></pre>
<pre class="r"><code>intToBits(65)[1:8]</code></pre>
<pre><code>## [1] 01 00 00 00 00 00 01 00</code></pre>
<pre class="r"><code>as.integer(intToBits(65)[1:8])</code></pre>
<pre><code>## [1] 1 0 0 0 0 0 1 0</code></pre>
<div id="modis-qc-data-are-big-endian" class="section level4">
<h4>MODIS QC data are <em>Big Endian</em></h4>
<table>
<thead>
<tr class="header">
<th>Format</th>
<th>Digits</th>
<th>value</th>
<th align="left">sum</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Little Endian</td>
<td>1 0 0 0 0 0 1 0</td>
<td>65</td>
<td align="left">2^0 + 2^6</td>
</tr>
<tr class="even">
<td>Big Endian</td>
<td>0 1 0 0 0 0 0 1</td>
<td>65</td>
<td align="left">2^6 + 2^0</td>
</tr>
</tbody>
</table>
<p>Reverse the digits with <code>rev()</code> and compare with QC table above.</p>
<pre class="r"><code>rev(as.integer(intToBits(65)[1:8]))</code></pre>
<pre><code>## [1] 0 1 0 0 0 0 0 1</code></pre>
<p>QC for value <code>65</code>:</p>
<ul>
<li>LST produced, other quality, recommend examination of more detailed QA</li>
<li>good data quality of L1B in 7 TIR bands</li>
<li>average emissivity error <= 0.01</li>
<li>Average LST error <= 2K</li>
</ul>
<div class="well">
<h2 id="your-turn">Your turn</h2>
<p>What does a QC value of 81 represent?</p>
<button data-toggle="collapse" class="btn btn-primary btn-sm round" data-target="#demo1">
Show Solution
</button>
<div id="demo1" class="collapse">
<pre class="r"><code>rev(as.integer(intToBits(81)[1:8]))</code></pre>
<pre><code>## [1] 0 1 0 1 0 0 0 1</code></pre>
<pre class="r"><code># LST produced, other quality, recommend exampination of more detailed QA
# Other quality data
# Average emissivity error <= 0.01
# Average LST error <= 2K</code></pre>
</div>
</div>
</div>
</div>
<div id="filter-the-the-lst-data-using-the-qc-data" class="section level3">
<h3>Filter the the lst data using the QC data</h3>
<pre class="r"><code>## set up data frame to hold all combinations
QC_Data <- data.frame(Integer_Value = 0:255,
Bit7 = NA, Bit6 = NA, Bit5 = NA, Bit4 = NA,
Bit3 = NA, Bit2 = NA, Bit1 = NA, Bit0 = NA,
QA_word1 = NA, QA_word2 = NA, QA_word3 = NA,
QA_word4 = NA)
##
for(i in QC_Data$Integer_Value){
AsInt <- as.integer(intToBits(i)[1:8])
QC_Data[i+1,2:9]<- AsInt[8:1]
}
QC_Data$QA_word1[QC_Data$Bit1 == 0 & QC_Data$Bit0==0] <- "LST GOOD"
QC_Data$QA_word1[QC_Data$Bit1 == 0 & QC_Data$Bit0==1] <- "LST Produced,Other Quality"
QC_Data$QA_word1[QC_Data$Bit1 == 1 & QC_Data$Bit0==0] <- "No Pixel,clouds"
QC_Data$QA_word1[QC_Data$Bit1 == 1 & QC_Data$Bit0==1] <- "No Pixel, Other QA"
QC_Data$QA_word2[QC_Data$Bit3 == 0 & QC_Data$Bit2==0] <- "Good Data"
QC_Data$QA_word2[QC_Data$Bit3 == 0 & QC_Data$Bit2==1] <- "Other Quality"
QC_Data$QA_word2[QC_Data$Bit3 == 1 & QC_Data$Bit2==0] <- "TBD"
QC_Data$QA_word2[QC_Data$Bit3 == 1 & QC_Data$Bit2==1] <- "TBD"
QC_Data$QA_word3[QC_Data$Bit5 == 0 & QC_Data$Bit4==0] <- "Emiss Error <= .01"
QC_Data$QA_word3[QC_Data$Bit5 == 0 & QC_Data$Bit4==1] <- "Emiss Err >.01 <=.02"
QC_Data$QA_word3[QC_Data$Bit5 == 1 & QC_Data$Bit4==0] <- "Emiss Err >.02 <=.04"
QC_Data$QA_word3[QC_Data$Bit5 == 1 & QC_Data$Bit4==1] <- "Emiss Err > .04"
QC_Data$QA_word4[QC_Data$Bit7 == 0 & QC_Data$Bit6==0] <- "LST Err <= 1"
QC_Data$QA_word4[QC_Data$Bit7 == 0 & QC_Data$Bit6==1] <- "LST Err > 2 LST Err <= 3"
QC_Data$QA_word4[QC_Data$Bit7 == 1 & QC_Data$Bit6==0] <- "LST Err > 1 LST Err <= 2"
QC_Data$QA_word4[QC_Data$Bit7 == 1 & QC_Data$Bit6==1] <- "LST Err > 4"
kable(head(QC_Data))</code></pre>
<table>
<thead>
<tr class="header">
<th align="right">Integer_Value</th>
<th align="right">Bit7</th>
<th align="right">Bit6</th>
<th align="right">Bit5</th>
<th align="right">Bit4</th>
<th align="right">Bit3</th>
<th align="right">Bit2</th>
<th align="right">Bit1</th>
<th align="right">Bit0</th>
<th align="left">QA_word1</th>
<th align="left">QA_word2</th>
<th align="left">QA_word3</th>
<th align="left">QA_word4</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="left">LST GOOD</td>
<td align="left">Good Data</td>
<td align="left">Emiss Error <= .01</td>
<td align="left">LST Err <= 1</td>
</tr>
<tr class="even">
<td align="right">1</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">1</td>
<td align="left">LST Produced,Other Quality</td>
<td align="left">Good Data</td>
<td align="left">Emiss Error <= .01</td>
<td align="left">LST Err <= 1</td>
</tr>
<tr class="odd">
<td align="right">2</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">1</td>
<td align="right">0</td>
<td align="left">No Pixel,clouds</td>
<td align="left">Good Data</td>
<td align="left">Emiss Error <= .01</td>
<td align="left">LST Err <= 1</td>
</tr>
<tr class="even">
<td align="right">3</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">1</td>
<td align="right">1</td>
<td align="left">No Pixel, Other QA</td>
<td align="left">Good Data</td>
<td align="left">Emiss Error <= .01</td>
<td align="left">LST Err <= 1</td>
</tr>
<tr class="odd">
<td align="right">4</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">1</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="left">LST GOOD</td>
<td align="left">Other Quality</td>
<td align="left">Emiss Error <= .01</td>
<td align="left">LST Err <= 1</td>
</tr>
<tr class="even">
<td align="right">5</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">1</td>
<td align="right">0</td>
<td align="right">1</td>
<td align="left">LST Produced,Other Quality</td>
<td align="left">Other Quality</td>
<td align="left">Emiss Error <= .01</td>
<td align="left">LST Err <= 1</td>
</tr>
</tbody>
</table>
</div>
<div id="select-which-qc-levels-to-keep" class="section level3">
<h3>Select which QC Levels to keep</h3>
<pre class="r"><code>keep=QC_Data[QC_Data$Bit1 == 0,]
keepvals=unique(keep$Integer_Value)
keepvals</code></pre>
<pre><code>## [1] 0 1 4 5 8 9 12 13 16 17 20 21 24 25 28 29 32
## [18] 33 36 37 40 41 44 45 48 49 52 53 56 57 60 61 64 65
## [35] 68 69 72 73 76 77 80 81 84 85 88 89 92 93 96 97 100
## [52] 101 104 105 108 109 112 113 116 117 120 121 124 125 128 129 132 133
## [69] 136 137 140 141 144 145 148 149 152 153 156 157 160 161 164 165 168
## [86] 169 172 173 176 177 180 181 184 185 188 189 192 193 196 197 200 201
## [103] 204 205 208 209 212 213 216 217 220 221 224 225 228 229 232 233 236
## [120] 237 240 241 244 245 248 249 252 253</code></pre>
</div>
<div id="how-many-observations-will-be-dropped" class="section level3">
<h3>How many observations will be dropped?</h3>
<pre class="r"><code>qcvals=table(values(lstqc)) # this takes a minute or two
QC_Data%>%
dplyr::select(everything(),-contains("Bit"))%>%
mutate(Var1=as.character(Integer_Value),
keep=Integer_Value%in%keepvals)%>%
inner_join(data.frame(qcvals))%>%
kable()</code></pre>
<pre><code>## Joining, by = "Var1"</code></pre>
<table>
<thead>
<tr class="header">
<th align="right">Integer_Value</th>
<th align="left">QA_word1</th>
<th align="left">QA_word2</th>
<th align="left">QA_word3</th>
<th align="left">QA_word4</th>
<th align="left">Var1</th>
<th align="left">keep</th>
<th align="right">Freq</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="right">2</td>
<td align="left">No Pixel,clouds</td>
<td align="left">Good Data</td>
<td align="left">Emiss Error <= .01</td>
<td align="left">LST Err <= 1</td>
<td align="left">2</td>
<td align="left">FALSE</td>
<td align="right">150019</td>
</tr>
<tr class="even">
<td align="right">17</td>
<td align="left">LST Produced,Other Quality</td>
<td align="left">Good Data</td>
<td align="left">Emiss Err >.01 <=.02</td>
<td align="left">LST Err <= 1</td>
<td align="left">17</td>
<td align="left">TRUE</td>
<td align="right">44552</td>
</tr>
<tr class="odd">
<td align="right">33</td>
<td align="left">LST Produced,Other Quality</td>
<td align="left">Good Data</td>
<td align="left">Emiss Err >.02 <=.04</td>
<td align="left">LST Err <= 1</td>
<td align="left">33</td>
<td align="left">TRUE</td>
<td align="right">20225</td>
</tr>
<tr class="even">
<td align="right">49</td>
<td align="left">LST Produced,Other Quality</td>
<td align="left">Good Data</td>
<td align="left">Emiss Err > .04</td>
<td align="left">LST Err <= 1</td>
<td align="left">49</td>
<td align="left">TRUE</td>
<td align="right">3</td>
</tr>
<tr class="odd">
<td align="right">65</td>
<td align="left">LST Produced,Other Quality</td>
<td align="left">Good Data</td>
<td align="left">Emiss Error <= .01</td>
<td align="left">LST Err > 2 LST Err <= 3</td>
<td align="left">65</td>
<td align="left">TRUE</td>
<td align="right">243391</td>
</tr>
<tr class="even">
<td align="right">81</td>
<td align="left">LST Produced,Other Quality</td>
<td align="left">Good Data</td>
<td align="left">Emiss Err >.01 <=.02</td>
<td align="left">LST Err > 2 LST Err <= 3</td>
<td align="left">81</td>
<td align="left">TRUE</td>
<td align="right">203501</td>
</tr>
<tr class="odd">
<td align="right">97</td>
<td align="left">LST Produced,Other Quality</td>
<td align="left">Good Data</td>
<td align="left">Emiss Err >.02 <=.04</td>
<td align="left">LST Err > 2 LST Err <= 3</td>
<td align="left">97</td>
<td align="left">TRUE</td>
<td align="right">25897</td>
</tr>
<tr class="even">
<td align="right">113</td>
<td align="left">LST Produced,Other Quality</td>
<td align="left">Good Data</td>
<td align="left">Emiss Err > .04</td>
<td align="left">LST Err > 2 LST Err <= 3</td>
<td align="left">113</td>
<td align="left">TRUE</td>
<td align="right">32</td>
</tr>
<tr class="odd">
<td align="right">129</td>
<td align="left">LST Produced,Other Quality</td>
<td align="left">Good Data</td>
<td align="left">Emiss Error <= .01</td>
<td align="left">LST Err > 1 LST Err <= 2</td>
<td align="left">129</td>
<td align="left">TRUE</td>
<td align="right">57</td>
</tr>
<tr class="even">
<td align="right">145</td>
<td align="left">LST Produced,Other Quality</td>
<td align="left">Good Data</td>
<td align="left">Emiss Err >.01 <=.02</td>
<td align="left">LST Err > 1 LST Err <= 2</td>
<td align="left">145</td>
<td align="left">TRUE</td>
<td align="right">29607</td>
</tr>
<tr class="odd">
<td align="right">161</td>
<td align="left">LST Produced,Other Quality</td>
<td align="left">Good Data</td>
<td align="left">Emiss Err >.02 <=.04</td>
<td align="left">LST Err > 1 LST Err <= 2</td>
<td align="left">161</td>
<td align="left">TRUE</td>
<td align="right">3</td>
</tr>
<tr class="even">
<td align="right">177</td>
<td align="left">LST Produced,Other Quality</td>
<td align="left">Good Data</td>
<td align="left">Emiss Err > .04</td>
<td align="left">LST Err > 1 LST Err <= 2</td>
<td align="left">177</td>
<td align="left">TRUE</td>
<td align="right">5</td>
</tr>
</tbody>
</table>
<p>Do you want to update the values you are keeping?</p>
</div>
<div id="filter-the-lst-data-keeping-only-keepvals" class="section level3">
<h3>Filter the LST Data keeping only <code>keepvals</code></h3>
<p>These steps take a couple minutes.</p>
<p>Make logical flag to use for mask</p>
<pre class="r"><code>lstkeep=calc(lstqc,function(x) x%in%keepvals)</code></pre>
<p>Plot the mask</p>
<pre class="r"><code>gplot(lstkeep[[4:8]])+
geom_raster(aes(fill=as.factor(value)))+
facet_grid(variable~.)+
scale_fill_manual(values=c("blue","red"),name="Keep")+
coord_equal()+
theme(legend.position = "bottom")</code></pre>
<p><img src="09_RemoteSensing_appeears_files/figure-html/unnamed-chunk-20-1.png" /><!-- --></p>
<p>Mask the lst data using the QC data</p>
<pre class="r"><code>lst2=mask(lst,mask=lstkeep,maskval=0)</code></pre>
</div>
</div>
<div id="add-dates-to-z-dimension" class="section level2">
<h2>Add Dates to Z dimension</h2>
<pre class="r"><code>tdates=names(lst)%>%
sub(pattern="X",replacement="")%>%
as.Date("%Y.%m.%d")
names(lst2)=1:nlayers(lst2)
lst2=setZ(lst2,tdates)</code></pre>
</div>
<div id="summarize-to-seasonal-climatologies" class="section level2">
<h2>Summarize to Seasonal climatologies</h2>
<p>Use <code>stackApply()</code> with a seasonal index.</p>
<pre class="r"><code>tseas=as.numeric(sub("Q","",quarters(getZ(lst2))))
tseas[1:20]</code></pre>
<pre><code>## [1] 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 3 3 3</code></pre>