Project

General

Profile

Download (18.4 KB) Statistics
| Branch: | Revision:
1 ddd9a810 Adam M. Wilson
## Figures associated with MOD35 Cloud Mask Exploration
2
3
setwd("~/acrobates/adamw/projects/MOD35C5")
4
5
library(raster);beginCluster(10)
6
library(rasterVis)
7
library(rgdal)
8
library(plotKML)
9
library(Cairo)
10
library(reshape)
11 be7b1081 Adam M. Wilson
library(rgeos)
12 14ad4a96 Adam M. Wilson
library(splancs)
13 ddd9a810 Adam M. Wilson
14 be7b1081 Adam M. Wilson
## get % cloudy
15
mod09=raster("data/MOD09_2009.tif")
16 d39ab57e Adam M. Wilson
names(mod09)="C5MOD09CF"
17 be7b1081 Adam M. Wilson
NAvalue(mod09)=0
18
19
mod35c5=raster("data/MOD35_2009.tif")
20
names(mod35c5)="C5MOD35CF"
21
NAvalue(mod35c5)=0
22
23
## mod35C6 annual
24 a57c4e3d Adam M. Wilson
if(!file.exists("data/MOD35C6_2009.tif")){
25 555815c9 Adam M. Wilson
#  system("/usr/local/gdal-1.10.0/bin/gdalbuildvrt  -a_srs '+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs' -sd 1 -b 1 data/MOD35C6.vrt `find /home/adamw/acrobates/adamw/projects/interp/data/modis/mod35/summary/ -name '*h[1]*_mean.nc'` ")
26
  system("gdalbuildvrt data/MOD35C6.vrt `find /home/adamw/acrobates/adamw/projects/interp/data/modis/mod35/summary/ -name '*h[1]*_mean.nc'` ")
27
28
  system("/usr/local/gdal-1.10.0/bin/gdalbuildvrt -a_srs '+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs' -sd 4 -b 1 data/MOD35C6_CFday_pmiss.vrt `find /home/adamw/acrobates/adamw/projects/interp/data/modis/mod35/summary/ -name '*h[1]*.nc'` ")
29
  system("gdalwarp data/MOD35C6_CFday_pmiss.vrt data/MOD35C6_CFday_pmiss.tif -r bilinear")
30
31 598244e1 Adam M. Wilson
  system("align.sh data/MOD35C6.vrt data/MOD09_2009.tif data/MOD35C6_2009.tif")
32 555815c9 Adam M. Wilson
  system("align.sh data/MOD35C6_CFday_pmiss.vrt data/MOD09_2009.tif data/MOD35C6_CFday_pmiss.tif")
33 a57c4e3d Adam M. Wilson
}
34 d39ab57e Adam M. Wilson
mod35c6=raster("data/MOD35C6_2009_v1.tif")
35 be7b1081 Adam M. Wilson
names(mod35c6)="C6MOD35CF"
36
NAvalue(mod35c6)=255
37
38 a57c4e3d Adam M. Wilson
## landcover
39 5f212fe8 Adam M. Wilson
if(!file.exists("data/MCD12Q1_IGBP_2009_051_wgs84_1km.tif")){
40 a57c4e3d Adam M. Wilson
  system(paste("/usr/local/gdal-1.10.0/bin/gdalwarp -tr 0.008983153 0.008983153 -r mode -ot Byte -co \"COMPRESS=LZW\"",
41 d39ab57e Adam M. Wilson
               " /mnt/data/jetzlab/Data/environ/global/MODIS/MCD12Q1/051/MCD12Q1_051_2009_wgs84.tif ",
42 a57c4e3d Adam M. Wilson
               " -t_srs \"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\" ",
43 5f212fe8 Adam M. Wilson
               " -te -180.0044166 -60.0074610 180.0044166 90.0022083 ",
44
               "data/MCD12Q1_IGBP_2009_051_wgs84_1km.tif -overwrite ",sep=""))}
45
lulc=raster("data/MCD12Q1_IGBP_2009_051_wgs84_1km.tif")
46 a57c4e3d Adam M. Wilson
47
#  lulc=ratify(lulc)
48
  data(worldgrids_pal)  #load palette
49
  IGBP=data.frame(ID=0:16,col=worldgrids_pal$IGBP[-c(18,19)],
50
    lulc_levels2=c("Water","Forest","Forest","Forest","Forest","Forest","Shrublands","Shrublands","Savannas","Savannas","Grasslands","Permanent wetlands","Croplands","Urban and built-up","Cropland/Natural vegetation mosaic","Snow and ice","Barren or sparsely vegetated"),stringsAsFactors=F)
51
  IGBP$class=rownames(IGBP);rownames(IGBP)=1:nrow(IGBP)
52
  levels(lulc)=list(IGBP)
53 5f212fe8 Adam M. Wilson
#lulc=crop(lulc,mod09)
54 a57c4e3d Adam M. Wilson
names(lulc)="MCD12Q1"
55
56
## make land mask
57
if(!file.exists("data/land.tif"))
58
  land=calc(lulc,function(x) ifelse(x==0,NA,1),file="data/land.tif",options=c("COMPRESS=LZW","ZLEVEL=9","PREDICTOR=2"),datatype="INT1U",overwrite=T)
59
land=raster("data/land.tif")
60
61 be7b1081 Adam M. Wilson
## mask cloud masks to land pixels
62
#mod09l=mask(mod09,land)
63
#mod35l=mask(mod35,land)
64
65 ddd9a810 Adam M. Wilson
#####################################
66
### compare MOD43 and MOD17 products
67
68
## MOD17
69
#extent(mod17)=alignExtent(mod17,mod09)
70 a57c4e3d Adam M. Wilson
if(!file.exists("data/MOD17.tif"))
71
system("align.sh ~/acrobates/adamw/projects/interp/data/modis/MOD17/MOD17A3_Science_NPP_mean_00_12.tif data/MOD09_2009.tif data/MOD17.tif")
72
mod17=raster("data/MOD17.tif",format="GTiff")
73
NAvalue(mod17)=65535
74 d39ab57e Adam M. Wilson
names(mod17)="MOD17_unscaled"
75 ddd9a810 Adam M. Wilson
76 a57c4e3d Adam M. Wilson
if(!file.exists("data/MOD17qc.tif"))
77
  system("align.sh ~/acrobates/adamw/projects/interp/data/modis/MOD17/MOD17A3_Science_NPP_Qc_mean_00_12.tif data/MOD09_2009.tif data/MOD17qc.tif")
78
mod17qc=raster("data/MOD17qc.tif",format="GTiff")
79 ddd9a810 Adam M. Wilson
NAvalue(mod17qc)=255
80 be7b1081 Adam M. Wilson
names(mod17qc)="MOD17CF"
81 ddd9a810 Adam M. Wilson
82
## MOD11 via earth engine
83 a57c4e3d Adam M. Wilson
if(!file.exists("data/MOD11_2009.tif"))
84
  system("align.sh ~/acrobates/adamw/projects/interp/data/modis/mod11/2009/MOD11_LST_2009.tif data/MOD09_2009.tif data/MOD11_2009.tif")
85
mod11=raster("data/MOD11_2009.tif",format="GTiff")
86 d39ab57e Adam M. Wilson
names(mod11)="MOD11_unscaled"
87 be7b1081 Adam M. Wilson
NAvalue(mod11)=0
88 a57c4e3d Adam M. Wilson
if(!file.exists("data/MOD11qc_2009.tif"))
89
  system("align.sh ~/acrobates/adamw/projects/interp/data/modis/mod11/2009/MOD11_Pmiss_2009.tif data/MOD09_2009.tif data/MOD11qc_2009.tif")
90
mod11qc=raster("data/MOD11qc_2009.tif",format="GTiff")
91 be7b1081 Adam M. Wilson
names(mod11qc)="MOD11CF"
92 ddd9a810 Adam M. Wilson
93
### Processing path
94 a57c4e3d Adam M. Wilson
if(!file.exists("data/MOD35pp.tif"))
95
system("align.sh data/MOD35_ProcessPath.tif data/MOD09_2009.tif data/MOD35pp.tif")
96
pp=raster("data/MOD35pp.tif")
97 be7b1081 Adam M. Wilson
NAvalue(pp)=255
98
names(pp)="MOD35pp"
99 ddd9a810 Adam M. Wilson
100
101 be7b1081 Adam M. Wilson
#hist(dif,maxsamp=1000000)
102
## draw lulc-stratified random sample of mod35-mod09 differences 
103
#samp=sampleStratified(lulc, 1000, exp=10)
104
#save(samp,file="LULC_StratifiedSample_10000.Rdata")
105
#mean(dif[samp],na.rm=T)
106
#Stats(dif,function(x) c(mean=mean(x),sd=sd(x)))
107 ddd9a810 Adam M. Wilson
108
109
###
110
111
n=100
112
at=seq(0,100,len=n)
113
cols=grey(seq(0,1,len=n))
114
cols=rainbow(n)
115
bgyr=colorRampPalette(c("blue","green","yellow","red"))
116
cols=bgyr(n)
117
118
119
### Transects
120
r1=Lines(list(
121
  Line(matrix(c(
122 be7b1081 Adam M. Wilson
                -61.688,4.098,
123
                -59.251,3.430
124 ddd9a810 Adam M. Wilson
                ),ncol=2,byrow=T))),"Venezuela")
125
r2=Lines(list(
126
  Line(matrix(c(
127
                133.746,-31.834,
128
                134.226,-32.143
129
                ),ncol=2,byrow=T))),"Australia")
130
r3=Lines(list(
131
  Line(matrix(c(
132
                73.943,27.419,
133
                74.369,26.877
134
                ),ncol=2,byrow=T))),"India")
135 d39ab57e Adam M. Wilson
#r4=Lines(list(
136
#  Line(matrix(c(
137
#                -5.164,42.270,
138
#                -4.948,42.162
139
#                ),ncol=2,byrow=T))),"Spain")
140 ddd9a810 Adam M. Wilson
141
r5=Lines(list(
142
  Line(matrix(c(
143 be7b1081 Adam M. Wilson
                33.195,12.512,
144
                33.802,12.894
145
                ),ncol=2,byrow=T))),"Sudan")
146
147 d39ab57e Adam M. Wilson
#r6=Lines(list(
148
#  Line(matrix(c(
149
#                -63.353,-10.746,
150
#                -63.376,-9.310
151
#                ),ncol=2,byrow=T))),"Brazil")
152 be7b1081 Adam M. Wilson
153
154
trans=SpatialLines(list(r1,r2,r3,r5),CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs "))
155
### write out shapefiles of transects
156
writeOGR(SpatialLinesDataFrame(trans,data=data.frame(ID=names(trans)),match.ID=F),"output",layer="transects",driver="ESRI Shapefile",overwrite=T)
157
158
## buffer transects to get regional values 
159 14ad4a96 Adam M. Wilson
transb=gBuffer(trans,byid=T,width=0.4)
160 be7b1081 Adam M. Wilson
161
## make polygons of bounding boxes
162
bb0 <- lapply(slot(transb, "polygons"), bbox)
163
bb1 <- lapply(bb0, bboxx)
164
# turn these into matrices using a helper function in splancs
165
bb2 <- lapply(bb1, function(x) rbind(x, x[1,]))
166
# close the matrix rings by appending the first coordinate
167
rn <- row.names(transb)
168
# get the IDs
169
bb3 <- vector(mode="list", length=length(bb2))
170
# make somewhere to keep the output
171
for (i in seq(along=bb3)) bb3[[i]] <- Polygons(list(Polygon(bb2[[i]])),
172
                   ID=rn[i])
173
# loop over the closed matrix rings, adding the IDs
174
bbs <- SpatialPolygons(bb3, proj4string=CRS(proj4string(transb)))
175
176
trd1=lapply(1:length(transb),function(x) {
177
  td=crop(mod11,transb[x])
178
  tdd=lapply(list(mod35c5,mod35c6,mod09,mod17,mod17qc,mod11,mod11qc,lulc,pp),function(l) resample(crop(l,transb[x]),td,method="ngb"))
179
  ## normalize MOD11 and MOD17
180 d39ab57e Adam M. Wilson
  for(j in which(do.call(c,lapply(tdd,function(i) names(i)))%in%c("MOD11_unscaled","MOD17_unscaled"))){
181 be7b1081 Adam M. Wilson
    trange=cellStats(tdd[[j]],range)
182 5f212fe8 Adam M. Wilson
    tscaled=100*(tdd[[j]]-trange[1])/(trange[2]-trange[1])
183
    tscaled@history=list(range=trange)
184 d39ab57e Adam M. Wilson
    names(tscaled)=sub("_unscaled","",names(tdd[[j]]))
185 5f212fe8 Adam M. Wilson
    tdd=c(tdd,tscaled)
186 be7b1081 Adam M. Wilson
  }
187
  return(brick(tdd))
188
})
189
190
## bind all subregions into single dataframe for plotting
191
trd=do.call(rbind.data.frame,lapply(1:length(trd1),function(i){
192
  d=as.data.frame(as.matrix(trd1[[i]]))
193
  d[,c("x","y")]=coordinates(trd1[[i]])
194
  d$trans=names(trans)[i]
195
  d=melt(d,id.vars=c("trans","x","y"))
196
  return(d)
197
}))
198
199
transd=do.call(rbind.data.frame,lapply(1:length(trans),function(l) {
200
  td=as.data.frame(extract(trd1[[l]],trans[l],along=T,cellnumbers=F)[[1]])
201
  td$loc=extract(trd1[[l]],trans[l],along=T,cellnumbers=T)[[1]][,1]
202
  td[,c("x","y")]=xyFromCell(trd1[[l]],td$loc)
203
  td$dist=spDistsN1(as.matrix(td[,c("x","y")]), as.matrix(td[1,c("x","y")]),longlat=T)
204
  td$transect=names(trans[l])
205
  td2=melt(td,id.vars=c("loc","x","y","dist","transect"))
206
  td2=td2[order(td2$variable,td2$dist),]
207
  # get per variable ranges to normalize
208
  tr=cast(melt.list(tapply(td2$value,td2$variable,function(x) data.frame(min=min(x,na.rm=T),max=max(x,na.rm=T)))),L1~variable)
209
  td2$min=tr$min[match(td2$variable,tr$L1)]
210
  td2$max=tr$max[match(td2$variable,tr$L1)]
211
  print(paste("Finished ",names(trans[l])))
212 ddd9a810 Adam M. Wilson
  return(td2)}
213 be7b1081 Adam M. Wilson
  ))
214
215
transd$type=ifelse(grepl("MOD35|MOD09|CF",transd$variable),"CF","Data")
216
217 5f212fe8 Adam M. Wilson
218
## comparison of % cloudy days
219 d39ab57e Adam M. Wilson
if(!file.exists("data/dif_c5_09.tif"))
220
  overlay(mod35c5,mod09,fun=function(x,y) {return(x-y)},file="data/dif_c5_09.tif",format="GTiff",options=c("COMPRESS=LZW","ZLEVEL=9"),overwrite=T)
221
dif_c5_09=raster("data/dif_c5_09.tif",format="GTiff")
222
223 14ad4a96 Adam M. Wilson
#dif_c6_09=mod35c6-mod09
224
#dif_c5_c6=mod35c5-mod35c6
225 5f212fe8 Adam M. Wilson
226 d39ab57e Adam M. Wilson
## exploring various ways to compare cloud products along landcover or processing path edges
227
#t1=trd1[[1]]
228
#dif_p=calc(trd1[[1]], function(x) (x[1]-x[3])/(1-x[1]))
229
#edge=calc(edge(subset(t1,"MCD12Q1"),classes=T,type="inner"),function(x) ifelse(x==1,1,NA))
230
#edgeb=buffer(edge,width=5000)
231
#edgeb=calc(edgeb,function(x) ifelse(is.na(x),0,1))
232
#names(edge)="edge"
233
#names(edgeb)="edgeb"
234
#td1=as.data.frame(stack(t1,edge,edgeb))
235
#cor(td1$MOD17,td1$C6MOD35,use="complete",method="spearman")
236
#cor(td1$MOD17[td1$edgeb==1],td1$C5MOD35[td1$edgeb==1],use="complete",method="spearman")
237
238
### Correlations
239
#trdw=cast(trd,trans+x+y~variable,value="value")
240
#cor(trdw$MOD17,trdw$C5MOD35,use="complete",method="spearman")
241 5f212fe8 Adam M. Wilson
242 d39ab57e Adam M. Wilson
#Across all pixels in the four regions analyzed in Figure 3 there is a much larger correlation between mean NPP and the C5 MOD35 CF (Spearman’s ρ = -0.61, n=58,756) than the C6 MOD35 CF (ρ = 0.00, n=58,756) or MOD09 (ρ = -0.07, n=58,756) products.  
243
#by(trdw,trdw$trans,function(x) cor(as.data.frame(na.omit(x[,c("C5MOD35CF","C6MOD35CF","C5MOD09CF","MOD17","MOD11")])),use="complete",method="spearman"))
244 598244e1 Adam M. Wilson
245
246 d39ab57e Adam M. Wilson
## table of correlations
247
#trdw_cor=as.data.frame(na.omit(trdw[,c("C5MOD35CF","C6MOD35CF","C5MOD09CF","MOD17","MOD11")]))
248
#nrow(trdw_cor)
249
#round(cor(trdw_cor,method="spearman"),2)
250 5f212fe8 Adam M. Wilson
251 be7b1081 Adam M. Wilson
252 d39ab57e Adam M. Wilson
## set up some graphing parameters
253 be7b1081 Adam M. Wilson
at=seq(0,100,leng=100)
254
bgyr=colorRampPalette(c("purple","blue","green","yellow","orange","red","red"))
255
bgrayr=colorRampPalette(c("purple","blue","grey","red","red"))
256
cols=bgyr(100)
257
258
## global map
259
library(maptools)
260
coast=map2SpatialLines(map("world", interior=FALSE, plot=FALSE),proj4string=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
261
262 5f212fe8 Adam M. Wilson
g1=levelplot(stack(mod35c5,mod09),xlab=" ",scales=list(x=list(draw=F),y=list(alternating=1)),col.regions=cols,at=at)+layer(sp.polygons(bbs[1:4],lwd=2))+layer(sp.lines(coast,lwd=.5))
263 598244e1 Adam M. Wilson
264 14ad4a96 Adam M. Wilson
g2=levelplot(dif_c5_09,col.regions=bgrayr(100),at=seq(-70,70,len=100),margin=F,ylab=" ",colorkey=list("right"))+layer(sp.polygons(bbs[1:4],lwd=2))+layer(sp.lines(coast,lwd=.5))
265 d39ab57e Adam M. Wilson
g2$strip=strip.custom(var.name="Difference (C5MOD35-C5MOD09)",style=1,strip.names=T,strip.levels=F)  #update strip text
266
#g3=histogram(dif_c5_09,col="black",border=NA,scales=list(x=list(at=c(-50,0,50)),y=list(draw=F),cex=1))+layer(panel.abline(v=0,col="red",lwd=2))
267 be7b1081 Adam M. Wilson
268
### regional plots
269 d39ab57e Adam M. Wilson
p1=useOuterStrips(levelplot(value~x*y|variable+trans,data=trd[!trd$variable%in%c("MOD17_unscaled","MOD11_unscaled","MCD12Q1","MOD35pp"),],asp=1,scales=list(draw=F,rot=0,relation="free"),
270 be7b1081 Adam M. Wilson
                                       at=at,col.regions=cols,maxpixels=7e6,
271 d39ab57e Adam M. Wilson
                                       ylab="Latitude",xlab="Longitude"),strip = strip.custom(par.strip.text=list(cex=.7)))+layer(sp.lines(trans,lwd=2))
272 be7b1081 Adam M. Wilson
273
p2=useOuterStrips(
274
  levelplot(value~x*y|variable+trans,data=trd[trd$variable%in%c("MCD12Q1"),],
275
            asp=1,scales=list(draw=F,rot=0,relation="free"),colorkey=F,
276
            at=c(-1,IGBP$ID),col.regions=IGBP$col,maxpixels=7e7,
277
            legend=list(
278
              right=list(fun=draw.key(list(columns=1,#title="MCD12Q1 \n IGBP Land \n Cover",
279
                           rectangles=list(col=IGBP$col,size=1),
280
                           text=list(as.character(IGBP$ID),at=IGBP$ID-.5))))),
281 d39ab57e Adam M. Wilson
            ylab="",xlab=" "),strip = strip.custom(par.strip.text=list(cex=.7)),strip.left=F)+layer(sp.lines(trans,lwd=2))
282 be7b1081 Adam M. Wilson
p3=useOuterStrips(
283
  levelplot(value~x*y|variable+trans,data=trd[trd$variable%in%c("MOD35pp"),],
284
            asp=1,scales=list(draw=F,rot=0,relation="free"),colorkey=F,
285
            at=c(-1:4),col.regions=c("blue","cyan","tan","darkgreen"),maxpixels=7e7,
286
            legend=list(
287
              right=list(fun=draw.key(list(columns=1,#title="MOD35 \n Processing \n Path",
288
                           rectangles=list(col=c("blue","cyan","tan","darkgreen"),size=1),
289
                           text=list(c("Water","Coast","Desert","Land")))))),
290 d39ab57e Adam M. Wilson
            ylab="",xlab=" "),strip = strip.custom(par.strip.text=list(cex=.7)),strip.left=F)+layer(sp.lines(trans,lwd=2))
291 be7b1081 Adam M. Wilson
292
## transects
293
p4=xyplot(value~dist|transect,groups=variable,type=c("smooth","p"),
294
       data=transd,panel=function(...,subscripts=subscripts) {
295
         td=transd[subscripts,]
296 ddd9a810 Adam M. Wilson
         ## mod09
297 d39ab57e Adam M. Wilson
         imod09=td$variable=="C5MOD09CF"
298 be7b1081 Adam M. Wilson
         panel.xyplot(td$dist[imod09],td$value[imod09],type=c("p","smooth"),span=0.2,subscripts=1:sum(imod09),col="red",pch=16,cex=.25)
299 ddd9a810 Adam M. Wilson
         ## mod35C5
300 be7b1081 Adam M. Wilson
         imod35=td$variable=="C5MOD35CF"
301
         panel.xyplot(td$dist[imod35],td$value[imod35],type=c("p","smooth"),span=0.09,subscripts=1:sum(imod35),col="blue",pch=16,cex=.25)
302
         ## mod35C6
303
         imod35c6=td$variable=="C6MOD35CF"
304
         panel.xyplot(td$dist[imod35c6],td$value[imod35c6],type=c("p","smooth"),span=0.09,subscripts=1:sum(imod35c6),col="black",pch=16,cex=.25)
305 ddd9a810 Adam M. Wilson
         ## mod17
306 be7b1081 Adam M. Wilson
         imod17=td$variable=="MOD17"
307
         panel.xyplot(td$dist[imod17],100*((td$value[imod17]-td$min[imod17][1])/(td$max[imod17][1]-td$min[imod17][1])),
308
                      type=c("smooth"),span=0.09,subscripts=1:sum(imod17),col="darkgreen",lty=5,pch=1,cex=.25)
309
         imod17qc=td$variable=="MOD17CF"
310
         panel.xyplot(td$dist[imod17qc],td$value[imod17qc],type=c("p","smooth"),span=0.09,subscripts=1:sum(imod17qc),col="darkgreen",pch=16,cex=.25)
311 ddd9a810 Adam M. Wilson
         ## mod11
312 be7b1081 Adam M. Wilson
         imod11=td$variable=="MOD11"
313
         panel.xyplot(td$dist[imod11],100*((td$value[imod11]-td$min[imod11][1])/(td$max[imod11][1]-td$min[imod11][1])),
314
                      type=c("smooth"),span=0.09,subscripts=1:sum(imod17),col="orange",lty="dashed",pch=1,cex=.25)
315
         imod11qc=td$variable=="MOD11CF"
316
         qcspan=ifelse(td$transect[1]=="Australia",0.2,0.05)
317
         panel.xyplot(td$dist[imod11qc],td$value[imod11qc],type=c("p","smooth"),npoints=100,span=qcspan,subscripts=1:sum(imod11qc),col="orange",pch=16,cex=.25)
318 ddd9a810 Adam M. Wilson
         ## land
319 be7b1081 Adam M. Wilson
         path=td[td$variable=="MOD35pp",]
320 d39ab57e Adam M. Wilson
         panel.segments(path$dist,-10,c(path$dist[-1],max(path$dist,na.rm=T)),-10,col=c("blue","cyan","tan","darkgreen")[path$value+1],subscripts=1:nrow(path),lwd=10,type="l")
321 be7b1081 Adam M. Wilson
         land=td[td$variable=="MCD12Q1",]
322 d39ab57e Adam M. Wilson
         panel.segments(land$dist,-20,c(land$dist[-1],max(land$dist,na.rm=T)),-20,col=IGBP$col[land$value+1],subscripts=1:nrow(land),lwd=10,type="l")
323 be7b1081 Adam M. Wilson
        },subscripts=T,par.settings = list(grid.pars = list(lineend = "butt")),
324 ddd9a810 Adam M. Wilson
       scales=list(
325 be7b1081 Adam M. Wilson
         x=list(alternating=1,relation="free"),#, lim=c(0,70)),
326 d39ab57e Adam M. Wilson
         y=list(at=c(-18,-10,seq(0,100,len=5)),
327
           labels=c("MCD12Q1 IGBP","MOD35 path",seq(0,100,len=5)),
328
           lim=c(-25,100)),
329
         alternating=F),
330 be7b1081 Adam M. Wilson
       xlab="Distance Along Transect (km)", ylab="% Missing Data / % of Maximum Value",
331
       legend=list(
332 a57c4e3d Adam M. Wilson
         bottom=list(fun=draw.key(list( rep=FALSE,columns=1,title=" ",
333 d39ab57e Adam M. Wilson
                      lines=list(type=c("b","b","b","b","b","l","b","l"),pch=16,cex=.5,
334
                        lty=c(0,1,1,1,1,5,1,5),
335
                        col=c("transparent","red","blue","black","darkgreen","darkgreen","orange","orange")),
336
                       text=list(
337
                         c("MODIS Products","C5 MOD09 % Cloudy","C5 MOD35 % Cloudy","C6 MOD35 % Cloudy","MOD17 % Missing","MOD17 (scaled)","MOD11 % Missing","MOD11 (scaled)")),
338 a57c4e3d Adam M. Wilson
                       rectangles=list(border=NA,col=c(NA,"tan","darkgreen")),
339
                       text=list(c("C5 MOD35 Processing Path","Desert","Land")),
340 d39ab57e Adam M. Wilson
                       rectangles=list(border=NA,col=c(NA,IGBP$col[sort(unique(transd$value[transd$variable=="MCD12Q1"]+1))])),
341
                       text=list(c("MCD12Q1 IGBP Land Cover",IGBP$class[sort(unique(transd$value[transd$variable=="MCD12Q1"]+1))])))))),
342
  strip = strip.custom(par.strip.text=list(cex=.75)))
343
print(p4)
344 a57c4e3d Adam M. Wilson
345 be7b1081 Adam M. Wilson
346
347
CairoPDF("output/mod35compare.pdf",width=11,height=7)
348
#CairoPNG("output/mod35compare_%d.png",units="in", width=11,height=8.5,pointsize=4000,dpi=1200,antialias="subpixel")
349
### Global Comparison
350 598244e1 Adam M. Wilson
print(g1,position=c(0,.35,1,1),more=T)
351
print(g2,position=c(0,0,1,0.415),more=F)
352
#print(g3,position=c(0.31,0.06,.42,0.27),more=F)
353
         
354 be7b1081 Adam M. Wilson
### MOD35 Desert Processing path
355
levelplot(pp,asp=1,scales=list(draw=T,rot=0),maxpixels=1e6,
356
          at=c(-1:3),col.regions=c("blue","cyan","tan","darkgreen"),margin=F,
357
          colorkey=list(space="bottom",title="MOD35 Processing Path",labels=list(labels=c("Water","Coast","Desert","Land"),at=0:4-.5)))+layer(sp.polygons(bbs,lwd=2))+layer(sp.lines(coast,lwd=.5))
358 ddd9a810 Adam M. Wilson
### levelplot of regions
359 be7b1081 Adam M. Wilson
print(p1,position=c(0,0,.62,1),more=T)
360
print(p2,position=c(0.6,0.21,0.78,0.79),more=T)
361
print(p3,position=c(0.76,0.21,1,0.79))
362
### profile plots
363
print(p4)
364
dev.off()
365 ddd9a810 Adam M. Wilson
366 be7b1081 Adam M. Wilson
### summary stats for paper
367 598244e1 Adam M. Wilson
td=cast(transect+loc+dist~variable,value="value",data=transd)
368
td2=melt.data.frame(td,id.vars=c("transect","dist","loc","MOD35pp","MCD12Q1"))
369 ddd9a810 Adam M. Wilson
370 be7b1081 Adam M. Wilson
## function to prettyprint mean/sd's
371
msd= function(x) paste(round(mean(x,na.rm=T),1),"% ±",round(sd(x,na.rm=T),1),sep="")
372 ddd9a810 Adam M. Wilson
373 be7b1081 Adam M. Wilson
cast(td2,transect+variable~MOD35pp,value="value",fun=msd)
374
cast(td2,transect+variable~MOD35pp+MCD12Q1,value="value",fun=msd)
375
cast(td2,transect+variable~.,value="value",fun=msd)
376
377
cast(td2,transect+variable~.,value="value",fun=msd)
378
379
cast(td2,variable~MOD35pp,value="value",fun=msd)
380
cast(td2,variable~.,value="value",fun=msd)
381
382
td[td$transect=="Venezuela",]
383 ddd9a810 Adam M. Wilson
384
385 d39ab57e Adam M. Wilson
#### export KML
386 ddd9a810 Adam M. Wilson
library(plotKML)
387
388 d39ab57e Adam M. Wilson
kml_open("output/modiscloud.kml")
389 ddd9a810 Adam M. Wilson
390 d39ab57e Adam M. Wilson
readAll(mod35c5)
391 ddd9a810 Adam M. Wilson
392 d39ab57e Adam M. Wilson
kml_layer.Raster(mod35c5,
393
     plot.legend = TRUE,raster_name="Collection 5 MOD35 Cloud Frequency",
394
    z.lim = c(0,100),colour_scale = get("colour_scale_numeric", envir = plotKML.opts),
395 ddd9a810 Adam M. Wilson
#    home_url = get("home_url", envir = plotKML.opts),
396
#    metadata = NULL, html.table = NULL,
397 d39ab57e Adam M. Wilson
    altitudeMode = "clampToGround", balloon = FALSE
398 ddd9a810 Adam M. Wilson
)
399
400 d39ab57e Adam M. Wilson
system(paste("gdal_translate -of KMLSUPEROVERLAY ",mod35c5@file@name," output/mod35c5.kmz -co FORMAT=JPEG"))
401
402 ddd9a810 Adam M. Wilson
logo = "http://static.tumblr.com/t0afs9f/KWTm94tpm/yale_logo.png"
403
kml_screen(image.file = logo, position = "UL", sname = "YALE logo",size=c(.1,.1))
404 d39ab57e Adam M. Wilson
kml_close("modiscloud.kml")
405
kml_compress("modiscloud.kml",files=c(paste(month.name,".png",sep=""),"obj_legend.png"),zip="/usr/bin/zip")