This repository has been archived by the owner on Jan 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
09_RemoteSensing_appeears.Rmd
518 lines (391 loc) · 13.1 KB
/
09_RemoteSensing_appeears.Rmd
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
---
title: "Satellite Remote Sensing"
---
```{r, echo=FALSE, message=FALSE, results='hide', purl=FALSE}
source("knitr_header.R")
```
[<i class="fa fa-file-code-o fa-3x" aria-hidden="true"></i> The R Script associated with this page is available here](`r output`). If you like, you can download this file and open it (or copy-paste into a new script) with RStudio so you can follow along.
### Libraries
```{r,results='hide',message=FALSE, warning=F}
library(raster)
library(rasterVis)
library(rgdal)
library(ggplot2)
library(ggmap)
library(tidyverse)
library(DataScienceData)
# New Packages
library(gdalUtils)
library(rts)
library(ncdf4)
```
## Identify (and create) download folders
Today we'll work with:
* Land Surface Temperature (`lst`): MOD11A2
* Land Cover (`lc`): MCD12Q1
## Land Use Land Cover
You will need to update the DataScienceData package before the command below will work. Run `devtools::install_github("adammwilson/DataScienceData")` and then `library(DataScienceData)`. If that doesn't work, you can download the needed files directly from [here](https://github.com/adammwilson/DataScienceData/tree/master/inst/extdata/appeears).
```{r}
lulcf=system.file("extdata",
"appeears/MCD12Q1.051_aid0001.nc",
package = "DataScienceData")
lulcf
```
```{r, warning=F, message=FALSE,results='hide'}
lulc=stack(lulcf,varname="Land_Cover_Type_1")
plot(lulc)
```
You may see some errors similar to
```
">>>> WARNING <<< attribute false_northing is an 8-byte value, but R"
[1] "does not support this data type. I am returning a double precision"
[1] "floating point, but you must be aware that this could lose precision!"
```
and you can ignore those.
We'll just pick one year to work with to keep this simple:
```{r, warning=F}
lulc=lulc[[13]]
plot(lulc)
```
### Process landcover data
Get cover clases from [MODIS website](https://lpdaac.usgs.gov/dataset_discovery/modis/modis_products_table/mcd12q1)
```{r}
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))
```
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.
```{r}
# convert to raster (easy)
lulc=as.factor(lulc)
# update the RAT with a left join
levels(lulc)=left_join(levels(lulc)[[1]],lcd)
```
```{r, fig.height=12, warning=F}
# 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))
```
## Land Surface Temperature
```{r, warning=F, message=FALSE,results='hide'}
lstf=system.file("extdata",
"appeears/MOD11A2.006_aid0001.nc",
package = "DataScienceData")
lstf
lst=stack(lstf,varname="LST_Day_1km")
plot(lst[[1:12]])
```
## Convert LST to Degrees C
You can convert LST from Degrees Kelvin (K) to Celcius (C) with `offs()`.
```{r}
offs(lst)=-273.15
plot(lst[[1:10]])
```
# MODLAND Quality control
See a detailed explaination [here](https://lpdaac.usgs.gov/sites/default/files/public/modis/docs/MODIS_LP_QA_Tutorial-1b.pdf). Some code below from [Steven Mosher's blog](https://stevemosher.wordpress.com/2012/12/05/modis-qc-bits/).
## MOD11A2 (Land Surface Temperature) Quality Control
[MOD11A2 QC Layer table](https://lpdaac.usgs.gov/dataset_discovery/modis/modis_products_table/mod11a2)
![](09_presentation/09_assets/lst_qc.png)
```{r, warning=F, message=FALSE,results='hide'}
lstqc=stack(lstf,varname="QC_Day")
plot(lstqc[[1:2]])
```
### LST QC data
QC data are encoded in 8-bit 'words' to compress information.
```{r}
values(lstqc[[1:2]])%>%table()
```
![](09_presentation/09_assets/QCdata.png)
```{r}
intToBits(65)
intToBits(65)[1:8]
as.integer(intToBits(65)[1:8])
```
#### MODIS QC data are _Big Endian_
Format Digits value sum
---- ---- ---- ----
Little Endian 1 0 0 0 0 0 1 0 65 2^0 + 2^6
Big Endian 0 1 0 0 0 0 0 1 65 2^6 + 2^0
Reverse the digits with `rev()` and compare with QC table above.
```{r}
rev(as.integer(intToBits(65)[1:8]))
```
QC for value `65`:
* LST produced, other quality, recommend examination of more detailed QA
* good data quality of L1B in 7 TIR bands
* average emissivity error <= 0.01
* Average LST error <= 2K
<div class="well">
## Your turn
What does a QC value of 81 represent?
<button data-toggle="collapse" class="btn btn-primary btn-sm round" data-target="#demo1">Show Solution</button>
<div id="demo1" class="collapse">
```{r, purl=F}
rev(as.integer(intToBits(81)[1:8]))
# LST produced, other quality, recommend exampination of more detailed QA
# Other quality data
# Average emissivity error <= 0.01
# Average LST error <= 2K
```
</div>
</div>
### Filter the the lst data using the QC data
```{r}
## 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))
```
### Select which QC Levels to keep
```{r}
keep=QC_Data[QC_Data$Bit1 == 0,]
keepvals=unique(keep$Integer_Value)
keepvals
```
### How many observations will be dropped?
```{r,warning=F}
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()
```
Do you want to update the values you are keeping?
### Filter the LST Data keeping only `keepvals`
These steps take a couple minutes.
Make logical flag to use for mask
```{r}
lstkeep=calc(lstqc,function(x) x%in%keepvals)
```
Plot the mask
```{r,fig.height=12}
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")
```
Mask the lst data using the QC data
```{r}
lst2=mask(lst,mask=lstkeep,maskval=0)
```
## Add Dates to Z dimension
```{r}
tdates=names(lst)%>%
sub(pattern="X",replacement="")%>%
as.Date("%Y.%m.%d")
names(lst2)=1:nlayers(lst2)
lst2=setZ(lst2,tdates)
```
## Summarize to Seasonal climatologies
Use `stackApply()` with a seasonal index.
```{r}
tseas=as.numeric(sub("Q","",quarters(getZ(lst2))))
tseas[1:20]
lst_seas=stackApply(lst2,
indices = tseas,
mean,na.rm=T)
names(lst_seas)=c("Q1_Winter",
"Q2_Spring",
"Q3_Summer",
"Q4_Fall")
```
```{r,fig.height=9}
gplot(lst_seas)+geom_raster(aes(fill=value))+
facet_wrap(~variable)+
scale_fill_gradientn(colours=c("blue",mid="grey","red"))+
coord_equal()+
theme(axis.text.x=element_text(angle=60, hjust=1))
```
<div class="well">
## Your turn
Use `stackApply()` to generate and plot monthly median lst values.
Hints:
1. First make a tmonth variable by converting the dates to months using `format(getZ(lst2),"%m")`
2. Use `stackApply()` to summarize the mean value per month
3. Rename the layers by the number of the months with `sprintf("%02d",1:12)`
4. Plot it like above.
<button data-toggle="collapse" class="btn btn-primary btn-sm round" data-target="#demo2">Show Solution</button>
<div id="demo2" class="collapse">
```{r, purl=F}
tmonth=as.numeric(format(getZ(lst2),"%m"))
lst_month=stackApply(lst2,indices = tmonth,mean,na.rm=T)
names(lst_month)=sprintf("%02d",1:12)
gplot(lst_month)+geom_raster(aes(fill=value))+
facet_wrap(~variable)+
scale_fill_gradientn(colours=c("blue",mid="grey","red"))+
coord_equal()
```
</div>
</div>
## Extract timeseries for a point
```{r, warning=F}
lw=SpatialPoints(
data.frame(
x= -78.791547,
y=43.007211))
projection(lw)="+proj=longlat"
lw=spTransform(lw,projection(lst2))
lwt=data.frame(date=getZ(lst2),
lst=t(raster::extract(
lst2,lw,
buffer=1000,
fun=mean,na.rm=T)))
ggplot(lwt,aes(x=date,y=lst))+
geom_path()
```
See the `library(rts)` for more timeseries related functions.
## Combine Land Cover and LST data
### Resample `lc` to `lst` grid
```{r}
lulc2=resample(lulc,
lst,
method="ngb")
par(mfrow=c(1,2))
plot(lulc)
plot(lulc2)
par(mfrow=c(1,1))
```
### Summarize mean monthly temperatures by Landcover
```{r}
table(values(lulc))
```
Extract values from `lst` and `lc` rasters.
```{r}
lcds1=cbind.data.frame(
values(lst_seas),
ID=values(lulc2[[1]]))%>%
na.omit()
head(lcds1)
```
Melt table and add LandCover Name
```{r}
lcds2=lcds1%>%
gather(key="season", value = "value", -ID)%>%
mutate(ID=as.numeric(ID))%>%
left_join(lcd)
head(lcds2)
```
#### Explore LST distributions by landcover
```{r,fig.height=12}
ggplot(lcds2,aes(y=value,x=landcover,group=landcover))+
facet_wrap(~season)+
geom_point(alpha=.5,position="jitter")+
geom_violin(alpha=.5,col="red",scale = "width")+
theme(axis.text.x=element_text(angle=90, hjust=1))
```
### Use Zonal Statistics to calculate summaries
```{r}
lct.mean=raster::zonal(lst_seas,
lulc2,
'mean',na.rm=T)%>%
data.frame()
lct.sd=zonal(lst_seas,
lulc2,
'sd',na.rm=T)%>%
data.frame()
lct.count=zonal(lst_seas,
lulc2,
'count',na.rm=T)%>%
data.frame()
lct.summary=rbind(data.frame(lct.mean,var="mean"),
data.frame(lct.sd,var="sd"),
data.frame(lct.count,var="count"))
```
#### Summarize seasonal values
```{r}
lctl=gather(lct.summary, key="season", value="value", -var, -zone)
lctl$season=factor(lctl$season,
labels=c("Winter","Spring","Summer","Fall"),
ordered=T)
lctl$zone=names(Land_Cover_Type_1)[lctl$zone+1]
lctl=spread(lctl,var,value="value")
head(lctl)%>%kable()
```
## Build summary table
```{r}
filter(lctl,count>=100)%>%
mutate(txt=paste0(round(mean,2),
" (±",round(sd,2),")"))%>%
dplyr::select(zone,count,txt,season)%>%
spread(season, txt)%>%
kable()
```
<div class="well">
## Your turn
Calculate the maximum observed seasonal average lst in each land cover type.
Hints:
1. First use `zonal()` of `lst_seas` and `lulc2` to calculate the `max()` with `na.rm=T`
2. convert the output to a `data.frame()`
3. use `arrange()` to sort by `desc(max)`
4. use `kable()` if desired to make it print nicely.
<button data-toggle="collapse" class="btn btn-primary btn-sm round" data-target="#demo3">Show Solution</button>
<div id="demo3" class="collapse">
```{r, purl=F}
zonal(max(lst_seas),lulc2,'max',na.rm=T)%>%
data.frame()%>%
arrange(desc(max))%>%
kable()
```
</div>
</div>
Things to think about:
* What tests would you use to identify differences?
* Do you need to worry about unequal sample sizes?