Project

General

Profile

Download (11.9 KB) Statistics
| Branch: | Revision:
1
## 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
library(spgrass6)
12

    
13
### set up grass database
14
  tf=paste(getwd(),"/grassdata",sep="")
15
  if(!file.exists(tf)) dir.create(tf)
16

    
17
## set up GRASS
18
  gisBase="/usr/lib/grass64"
19
  initGRASS(gisBase=gisBase,override=T,gisDbase=tf,SG=as(raster("data/MOD17A3_Science_NPP_mean_00_12.tif"),SpatialGridDataFrame))
20
  system(paste("g.proj -c proj4=\"",projection(glb),"\"",sep=""),ignore.stdout=T,ignore.stderr=T)
21
## read in NPP grid to serve as grid
22
  execGRASS("r.in.gdal",input=t@file@name,output="grid")
23
  system("g.region rast=grid n=90 s=-90 save=roi --overwrite",ignore.stdout=F,ignore.stderr=F)
24
   
25

    
26
## get % cloudy
27
mod09=raster("data/MOD09_2009.tif")
28
names(mod09)="MOD09_cloud"
29

    
30
mod35c5=raster("data/MOD35_2009.tif")
31
mod35c5=crop(mod35c5,mod09)
32
names(mod35c5)="MOD35C5_cloud"
33

    
34
mod35c6=raster("")
35
 
36
## landcover
37
if(!file.exists("data/MCD12Q1_IGBP_2005_v51_1km_wgs84.tif")){
38
  system(paste("gdalwarp -r near -ot Byte -co \"COMPRESS=LZW\"",
39
               " ~/acrobatesroot/jetzlab/Data/environ/global/landcover/MODIS/MCD12Q1_IGBP_2005_v51.tif ",
40
               " -t_srs \"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\" ",
41
               tempdir(),"/MCD12Q1_IGBP_2005_v51_wgs84.tif -overwrite ",sep=""))
42
  lulc=raster(paste(tempdir(),"/MCD12Q1_IGBP_2005_v51_wgs84.tif",sep=""))
43
  ## aggregate to 1km resolution
44
  lulc2=aggregate(lulc,2,fun=function(x,na.rm=T) {
45
    x=na.omit(x)
46
    ux <- unique(x)
47
    ux[which.max(tabulate(match(x, ux)))]
48
  },file=paste(tempdir(),"/1km.tif",sep=""))
49
  writeRaster(lulc2,"data/MCD12Q1_IGBP_2005_v51_1km_wgs84.tif",options=c("COMPRESS=LZW","ZLEVEL=9","PREDICTOR=2"),datatype="INT1U",overwrite=T)
50
}
51
lulc=raster("data/MCD12Q1_IGBP_2005_v51_1km_wgs84.tif")
52
#  lulc=ratify(lulc)
53
  data(worldgrids_pal)  #load palette
54
  IGBP=data.frame(ID=0:16,col=worldgrids_pal$IGBP[-c(18,19)],
55
    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)
56
  IGBP$class=rownames(IGBP);rownames(IGBP)=1:nrow(IGBP)
57
  levels(lulc)=list(IGBP)
58
extent(lulc)=alignExtent(lulc,mod09)
59
names(lulc)="MCD12Q1"
60

    
61
## make land mask
62
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)
63
land=raster("data/land.tif")
64

    
65
#####################################
66
### compare MOD43 and MOD17 products
67

    
68
## MOD17
69
mod17=raster("data/MOD17A3_Science_NPP_mean_00_12.tif",format="GTiff")
70
NAvalue(mod17)=65535
71
#extent(mod17)=alignExtent(mod17,mod09)
72
mod17=crop(mod17,mod09)
73
names(mod17)="MOD17"
74

    
75
mod17qc=raster("data/MOD17A3_Science_NPP_Qc_mean_00_12.tif",format="GTiff")
76
NAvalue(mod17qc)=255
77
                                        #extent(mod17qc)=alignExtent(mod17qc,mod09)
78
mod17qc=crop(mod17qc,mod09)
79
names(mod17qc)="MOD17qc"
80

    
81
## MOD11 via earth engine
82
mod11=raster("data/MOD11_2009.tif",format="GTiff")
83
names(mod11)="MOD11"
84
mod11qc=raster("data/MOD11_Pmiss_2009.tif",format="GTiff")
85
names(mod11qc)="MOD11qc"
86

    
87
## MOD43 via earth engine
88
mod43=raster("data/mod43_2009.tif",format="GTiff")
89
mod43qc=raster("data/mod43_2009.tif",format="GTiff")
90

    
91

    
92
### Create some summary objects for plotting
93
#difm=v6m-v5m
94
#v5v6compare=stack(v5m,v6m,difm)
95
#names(v5v6compare)=c("Collection 5","Collection 6","Difference (C6-C5)")
96

    
97
### Processing path
98
pp=raster("data/MOD35_ProcessPath.tif")
99
extent(pp)=alignExtent(pp,mod09)
100
pp=crop(pp,mod09)
101

    
102
## Summary plot of mod17 and mod43
103
modprod=stack(mod35c5,mod09,pp,lulc)#,mod43,mod43qc)
104
names(modprod)=c("MOD17","MOD17qc")#,"MOD43","MOD43qc")
105

    
106

    
107
## comparison of % cloudy days
108
dif=mod35c5-mod09
109
hist(dif,maxsamp=1000000)
110

    
111
## draw lulc-stratified random sample of mod35-mod09 differences 
112
samp=sampleStratified(lulc, 1000, exp=10)
113
save(samp,file="LULC_StratifiedSample_10000.Rdata")
114

    
115
mean(dif[samp],na.rm=T)
116

    
117
Stats(dif,function(x) c(mean=mean(x),sd=sd(x)))
118

    
119

    
120
###
121

    
122
n=100
123
at=seq(0,100,len=n)
124
cols=grey(seq(0,1,len=n))
125
cols=rainbow(n)
126
bgyr=colorRampPalette(c("blue","green","yellow","red"))
127
cols=bgyr(n)
128

    
129
#levelplot(lulcf,margin=F,layers="LULC")
130

    
131
CairoPDF("output/mod35compare.pdf",width=11,height=8.5)
132
#CairoPNG("output/mod35compare_%d.png",units="in", width=11,height=8.5,pointsize=4000,dpi=1200,antialias="subpixel")
133

    
134
### Transects
135
r1=Lines(list(
136
  Line(matrix(c(
137
                -61.183,1.165,
138
                -60.881,0.825
139
                ),ncol=2,byrow=T))),"Venezuela")
140
r2=Lines(list(
141
  Line(matrix(c(
142
                133.746,-31.834,
143
                134.226,-32.143
144
                ),ncol=2,byrow=T))),"Australia")
145
r3=Lines(list(
146
  Line(matrix(c(
147
                73.943,27.419,
148
                74.369,26.877
149
                ),ncol=2,byrow=T))),"India")
150
r4=Lines(list(
151
  Line(matrix(c(
152
                -5.164,42.270,
153
                -4.948,42.162
154
                ),ncol=2,byrow=T))),"Spain")
155

    
156
r5=Lines(list(
157
  Line(matrix(c(
158
                24.170,-17.769,
159
                24.616,-18.084
160
                ),ncol=2,byrow=T))),"Africa")
161

    
162

    
163
trans=SpatialLines(list(r1,r2,r3,r4,r5),CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs "))
164

    
165
transd=lapply(list(mod35c5,mod09,mod17,mod17qc,mod11qc,lulc,pp),function(l) {
166
  td=extract(l,trans,along=T,cellnumbers=F)
167
  names(td)=names(trans)                                        #  colnames(td)=c("value","transect")
168
  cells=extract(l,trans,along=T,cellnumbers=T)
169
  cells2=lapply(cells,function(x) xyFromCell(l,x[,1]))
170
  dists=lapply(cells2,function(x) spDistsN1(x,x[1,],longlat=T))
171
  td2=do.call(rbind.data.frame,lapply(1:length(td),function(i) cbind.data.frame(value=td[[i]],cells2[[i]],dist=dists[[i]],transect=names(td)[i])))
172
  td2$prod=names(l)
173
  td2$loc=rownames(td2)
174
  td2=td2[order(td2$dist),]
175
  print(paste("Finished ",names(l)))
176
  return(td2)}
177
  )
178
transdl=melt(transd,id.vars=c("prod","transect","loc","x","y","dist"))
179
transd$loc=as.numeric(transd$loc)
180
transdl$type=ifelse(grepl("MOD35|MOD09|qc",transdl$prod),"QC","Data")
181
  
182
nppid=transdl$prod=="MOD17"
183

    
184
xyplot(value~dist|transect,groups=prod,type=c("smooth","p"),
185
       data=transdl,panel=function(...,subscripts=subscripts) {
186
         td=transdl[subscripts,]
187
         ## mod09
188
         imod09=td$prod=="MOD09_cloud"
189
         panel.xyplot(td$dist[imod09],td$value[imod09],type=c("p","smooth"),span=0.2,subscripts=1:sum(imod09),col="red",pch=16,cex=.5)
190
         ## mod35C5
191
         imod35=td$prod=="MOD35C5_cloud"
192
         panel.xyplot(td$dist[imod35],td$value[imod35],type=c("p","smooth"),span=0.09,subscripts=1:sum(imod35),col="blue",pch=16,cex=.5)
193
         ## mod17
194
         imod17=td$prod=="MOD17"
195
         panel.xyplot(td$dist[imod17],100*td$value[imod17]/max(td$value[imod17]),
196
                      type=c("smooth"),span=0.09,subscripts=1:sum(imod17),col="darkgreen",lty="dashed",pch=1,cex=.5)
197
         imod17qc=td$prod=="MOD17qc"
198
         panel.xyplot(td$dist[imod17qc],td$value[imod17qc],type=c("p","smooth"),span=0.09,subscripts=1:sum(imod17qc),col="darkgreen",pch=16,cex=.5)
199
         ## mod11
200
#         imod11=td$prod=="MOD11"
201
#         panel.xyplot(td$dist[imod17],100*td$value[imod17]/max(td$value[imod17]),
202
#                      type=c("smooth"),span=0.09,subscripts=1:sum(imod17),col="darkgreen",lty="dashed",pch=1,cex=.5)
203
         imod11qc=td$prod=="MOD11qc"
204
         panel.xyplot(td$dist[imod11qc],td$value[imod11qc],type=c("p","smooth"),span=0.09,subscripts=1:sum(imod11qc),col="maroon",pch=16,cex=.5)
205
         ## means
206
         means=td$prod%in%c("","MOD17qc","MOD09_cloud","MOD35_cloud")
207
         ## land
208
         path=td[td$prod=="MOD35_ProcessPath",]
209
         panel.segments(path$dist,0,c(path$dist[-1],max(path$dist)),0,col=IGBP$col[path$value],subscripts=1:nrow(path),lwd=15,type="l")
210
         land=td[td$prod=="MCD12Q1",]
211
         panel.segments(land$dist,-5,c(land$dist[-1],max(land$dist)),-5,col=IGBP$col[land$value],subscripts=1:nrow(land),lwd=15,type="l")
212
       },subscripts=T,par.settings = list(grid.pars = list(lineend = "butt")),
213
       scales=list(
214
         x=list(alternating=1), #lim=c(0,50),
215
         y=list(at=c(-5,0,seq(20,100,len=5)),
216
           labels=c("IGBP","MOD35",seq(20,100,len=5)),
217
           lim=c(-10,100))),
218
       xlab="Distance Along Transect (km)", 
219
       key=list(space="right",lines=list(col=c("red","blue","darkgreen","maroon")),text=list(c("MOD09 % Cloudy","MOD35 % Cloudy","MOD17 % Missing","MOD11 % Missing"),lwd=1,col=c("red","blue","darkgreen","maroon"))))
220

    
221
### levelplot of regions
222

    
223

    
224
c(levelplot(mod35c5,margin=F),levelplot(mod09,margin=F),levelplot(mod11qc),levelplot(mod17qc),x.same = T, y.same = T)
225

    
226
levelplot(modprod)
227

    
228

    
229

    
230
### LANDCOVER
231
levelplot(lulcf,col.regions=levels(lulcf)[[1]]$col,
232
          scales=list(cex=2),
233
          colorkey=list(space="right",at=0:16,labels=list(at=seq(0.5,16.5,by=1),labels=levels(lulcf)[[1]]$class,cex=2)),margin=F)
234

    
235

    
236
levelplot(mcompare,col.regions=cols,at=at,margin=F,sub="Frequency of MOD35 Clouds in March")
237
#levelplot(dif,col.regions=bgyr(20),margin=F)
238
levelplot(mdiff,col.regions=bgyr(100),at=seq(mdiff@data@min,mdiff@data@max,len=100),margin=F)
239

    
240

    
241
boxplot(as.matrix(subset(dif,subset=1))~forest,varwidth=T,notch=T);abline(h=0)
242

    
243

    
244
levelplot(modprod,main="Missing Data (%) in MOD17 (NPP) and MOD43 (BRDF Reflectance)",
245
          sub="Tile H11v08 (Venezuela)",col.regions=cols,at=at)
246

    
247

    
248

    
249

    
250
levelplot(modprod,main="Missing Data (%) in MOD17 (NPP) and MOD43 (BRDF Reflectance)",
251
          sub="Tile H11v08 (Venezuela)",col.regions=cols,at=at,
252
          xlim=c(-7300000,-6670000),ylim=c(0,600000))
253

    
254
levelplot(v5m,main="Missing Data (%) in MOD17 (NPP) and MOD43 (BRDF Reflectance)",
255
          sub="Tile H11v08 (Venezuela)",col.regions=cols,at=at,
256
          xlim=c(-7200000,-6670000),ylim=c(0,400000),margin=F)
257

    
258

    
259
levelplot(subset(v5v6compare,1:2),main="Proportion Cloudy Days (%) in Collection 5 and 6 MOD35",
260
          sub="Tile H11v08 (Venezuela)",col.regions=cols,at=at,
261
          margin=F)
262

    
263
levelplot(subset(v5v6compare,1:2),main="Proportion Cloudy Days (%) in Collection 5 and 6 MOD35",
264
          sub="Tile H11v08 (Venezuela)",col.regions=cols,at=at,
265
          xlim=c(-7200000,-6670000),ylim=c(0,400000),margin=F)
266

    
267
levelplot(subset(v5v6compare,1:2),main="Proportion Cloudy Days (%) in Collection 5 and 6 MOD35",
268
          sub="Tile H11v08 (Venezuela)",col.regions=cols,at=at,
269
          xlim=c(-7500000,-7200000),ylim=c(700000,1000000),margin=F)
270

    
271

    
272
dev.off()
273

    
274
### smoothing plots
275
## explore smoothed version
276
td=subset(v6,m)
277
## build weight matrix
278
s=3
279
w=matrix(1/(s*s),nrow=s,ncol=s)
280
#w[s-1,s-1]=4/12; w
281
td2=focal(td,w=w)
282
td3=stack(td,td2)
283

    
284
levelplot(td3,col.regions=cols,at=at,margin=F)
285

    
286
dev.off()
287
plot(stack(difm,lulc))
288

    
289
### ROI
290
tile_ll=projectExtent(v6, "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
291

    
292
62,59
293
0,3
294

    
295

    
296

    
297
#### export KML timeseries
298
library(plotKML)
299
tile="h11v08"
300
file=paste("summary/MOD35_",tile,".nc",sep="")
301
system(paste("gdalwarp -overwrite -multi -ot INT16 -r cubicspline -srcnodata 255 -dstnodata 255 -s_srs '+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs' -t_srs 'EPSG:4326' NETCDF:",file,":PCloud  MOD35_",tile,".tif",sep=""))
302

    
303
v6sp=brick(paste("MOD35_",tile,".tif",sep=""))
304
v6sp=readAll(v6sp)
305

    
306
## wasn't working with line below, perhaps Z should just be text? not date?
307
v6sp=setZ(v6sp,as.Date(paste("2011-",1:12,"-15",sep="")))
308
names(v6sp)=month.name
309

    
310
kml_open("output/mod35.kml")
311

    
312

    
313
kml_layer.RasterBrick(v6sp,
314
     plot.legend = TRUE, dtime = "", tz = "GMT",
315
    z.lim = c(0,100),colour_scale = get("colour_scale_numeric", envir = plotKML.opts))
316
#    home_url = get("home_url", envir = plotKML.opts),
317
#    metadata = NULL, html.table = NULL,
318
#    altitudeMode = "clampToGround", balloon = FALSE,
319
)
320

    
321
logo = "http://static.tumblr.com/t0afs9f/KWTm94tpm/yale_logo.png"
322
kml_screen(image.file = logo, position = "UL", sname = "YALE logo",size=c(.1,.1))
323
kml_close("mod35.kml")
324
kml_compress("mod35.kml",files=c(paste(month.name,".png",sep=""),"obj_legend.png"),zip="/usr/bin/zip")
(21-21/41)