11 |
11 |
library(reshape)
|
12 |
12 |
library(caTools)
|
13 |
13 |
library(rgeos)
|
|
14 |
library(raster)
|
14 |
15 |
|
15 |
16 |
## read in data
|
16 |
17 |
#cld=read.csv("data/NDP026D/cld.csv")
|
... | ... | |
21 |
22 |
|
22 |
23 |
|
23 |
24 |
## add lulc factor information
|
24 |
|
require(plotKML); data(worldgrids_pal) #load IGBP palette
|
25 |
|
IGBP=data.frame(ID=0:16,col=worldgrids_pal$IGBP[-c(18,19)],stringsAsFactors=F)
|
26 |
|
IGBP$class=rownames(IGBP);rownames(IGBP)=1:nrow(IGBP)
|
|
25 |
#require(plotKML); data(worldgrids_pal) #load IGBP palette
|
|
26 |
#IGBP=data.frame(ID=0:16,col=worldgrids_pal$IGBP[-c(18,19)],stringsAsFactors=F)
|
|
27 |
#IGBP$class=rownames(IGBP);rownames(IGBP)=1:nrow(IGBP)
|
27 |
28 |
|
28 |
29 |
## month factors
|
29 |
30 |
#cld$month2=factor(cld$month,labels=month.name)
|
... | ... | |
72 |
73 |
coast <- gIntersection(coast, CP, byid=F)
|
73 |
74 |
|
74 |
75 |
## get stratified sample of points from biomes for illustration
|
75 |
|
## if(!file.exists("output/biomesamplepoints.csv")){
|
76 |
|
## n_biomesamples=1000
|
77 |
|
## library(multicore)
|
78 |
|
## biomesample=do.call(rbind.data.frame,mclapply(1:length(biome),function(i)
|
79 |
|
## data.frame(biome=biome$BiomeID[i],coordinates(spsample(biome[i,],n=n_biomesamples,type="stratified",nsig=2)))))
|
80 |
|
## write.csv(biomesample,"output/biomesamplepoints.csv",row.names=F)
|
81 |
|
## }
|
82 |
|
## biomesample=read.csv("output/biomesamplepoints.csv")
|
83 |
|
## coordinates(biomesample)=c("x1","x2")
|
|
76 |
if(!file.exists("output/biomesamplepoints.csv")){
|
|
77 |
biome=readOGR("data/teow/","biomes")
|
|
78 |
n_biomesamples=1000
|
|
79 |
library(multicore)
|
|
80 |
biomesample=do.call(rbind.data.frame,mclapply(1:length(biome),function(i)
|
|
81 |
data.frame(code=biome$code[i],coordinates(spsample(biome[i,],n=n_biomesamples,type="stratified",nsig=2)))))
|
|
82 |
colnames(biomesample)[2:3]=c("lon","lat")
|
|
83 |
biomesample[,c("biome","realm")]=biome@data[match(biomesample$code,biome$code),c("biome","realm")]
|
|
84 |
write.csv(biomesample,"output/biomesamplepoints.csv",row.names=F)
|
|
85 |
}
|
|
86 |
|
|
87 |
## Extract data for points
|
|
88 |
if(!file.exists("output/biomesamplepoints_cloud.csv")){
|
|
89 |
biomesample=read.csv("output/biomesamplepoints.csv")
|
|
90 |
biomep=raster::extract(mod09c,biomesample,sp=T)
|
|
91 |
biomep$lon=biomesample$lon
|
|
92 |
biomep@data[,c("lon","lat")]=coordinates(biomep)
|
|
93 |
write.csv(biomep@data,"output/biomesamplepoints_cloud.csv",row.names=F)
|
|
94 |
}
|
|
95 |
biomep=read.csv("output/biomesamplepoints_cloud.csv")
|
|
96 |
coordinates(biomep)=c("lon","lat")
|
|
97 |
projection(biomep)="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
|
84 |
98 |
|
85 |
|
#biomesample=extract(biomesample,mod09c)
|
86 |
99 |
|
87 |
100 |
## Figures
|
88 |
101 |
n=100
|
89 |
|
at=seq(0,100,length=n)
|
|
102 |
at=seq(0,100,length=n)
|
90 |
103 |
colr=colorRampPalette(c("black","green","red"))
|
91 |
104 |
cols=colr(n)
|
92 |
105 |
|
|
106 |
## set plotting parameters
|
|
107 |
my.theme = trellis.par.get()
|
|
108 |
my.theme$strip.background=list(col="transparent")
|
|
109 |
trellis.par.set(my.theme)
|
93 |
110 |
|
94 |
111 |
#pdf("output/Figures.pdf",width=11,height=8.5)
|
95 |
|
png("output/CF_Figures_%03d.png",width=5000,height=4000,res=600,pointsize=36,bg="transparent")
|
|
112 |
png("output/CF_Figures_%03d.png",width=5000,height=4000,res=600,pointsize=42,bg="white")
|
96 |
113 |
|
97 |
114 |
res=1e4
|
98 |
115 |
greg=list(ylim=c(-60,84),xlim=c(-180,180))
|
99 |
116 |
|
100 |
117 |
## Figure 1: 4-panel summaries
|
101 |
118 |
#- Annual average
|
102 |
|
levelplot(mod09a,col.regions=colr(n),cuts=100,at=seq(0,100,len=100),colorkey=list(space="bottom",adj=1),
|
|
119 |
levelplot(mod09a,col.regions=colr(n),cuts=100,at=seq(0,100,len=100),colorkey=list(title="Cloud Frequency (%)",space="bottom",adj=1),
|
103 |
120 |
margin=F,maxpixels=res,ylab="",xlab="",useRaster=T,ylim=greg$ylim)+
|
104 |
121 |
layer(sp.lines(coast,col="black"),under=F)
|
105 |
122 |
## Mean annual with validation stations
|
106 |
|
levelplot(mod09a,col.regions=colr(n),cuts=100,at=seq(0,100,len=100),colorkey=list(space="bottom",adj=1),
|
|
123 |
levelplot(mod09a,col.regions=colr(n),cuts=100,at=seq(0,100,len=100),colorkey=list(title="Cloud Frequency (%)",space="bottom",adj=1),
|
107 |
124 |
margin=F,maxpixels=res,ylab="",xlab="",useRaster=T,ylim=greg$ylim)+
|
108 |
125 |
layer(panel.xyplot(lon,lat,pch=16,cex=.3,col="black"),data=data.frame(coordinates(st)))+
|
109 |
126 |
layer(sp.lines(coast,col="black"),under=F)
|
110 |
127 |
|
111 |
128 |
## Seasonal Means
|
112 |
|
levelplot(mod09s,col.regions=colr(n),cuts=100,at=seq(0,100,len=100),colorkey=list(space="bottom",adj=2),
|
|
129 |
levelplot(mod09s,col.regions=colr(n),cuts=100,at=seq(0,100,len=100),colorkey=list(title="Cloud Frequency (%)", space="bottom",adj=2),
|
113 |
130 |
margin=F,maxpixels=res,ylab="",xlab="",useRaster=T,ylim=greg$ylim)+
|
114 |
131 |
layer(sp.lines(coast,col="black"),under=F)
|
115 |
132 |
|
116 |
133 |
## four metics
|
117 |
|
levelplot(mod09metrics,col.regions=colr(n),cuts=100,at=seq(0,100,len=100),colorkey=list(space="bottom",adj=2),
|
|
134 |
levelplot(mod09metrics,col.regions=colr(n),cuts=100,at=seq(0,100,len=100),colorkey=list(title="Cloud Frequency (%)", space="bottom",adj=2),
|
118 |
135 |
margin=F,maxpixels=res,ylab="",xlab="",useRaster=T,ylim=greg$ylim)+
|
119 |
136 |
layer(sp.lines(coast,col="black"),under=F)
|
120 |
137 |
|
121 |
138 |
## Monthly Means
|
122 |
|
levelplot(mod09c,col.regions=colr(n),cuts=100,at=seq(0,100,len=100),colorkey=list(space="bottom",adj=1),
|
|
139 |
levelplot(mod09c,col.regions=colr(n),cuts=100,at=seq(0,100,len=100),colorkey=list(title="Cloud Frequency (%)", space="bottom",adj=1),
|
123 |
140 |
margin=F,maxpixels=res,ylab="Latitude",xlab="Longitude",useRaster=T,ylim=greg$ylim)+
|
124 |
141 |
layer(sp.lines(coast,col="black"),under=F)
|
125 |
142 |
|
126 |
143 |
#- Monthly minimum
|
127 |
144 |
#- Monthly maximum
|
128 |
145 |
#- STDEV or Min-Max
|
129 |
|
p_mac=levelplot(mod09a,col.regions=colr(n),cuts=99,at=seq(0,100,length=100),margin=F,maxpixels=res/10,colorkey=list(space="bottom",height=.75),xlab="",ylab="",useRaster=T)+
|
|
146 |
p_mac=levelplot(mod09a,col.regions=colr(n),cuts=99,at=seq(0,100,length=100),margin=F,maxpixels=res/10,colorkey=list(title="Cloud Frequency (%)", space="bottom",height=.75),xlab="",ylab="",useRaster=T)+
|
130 |
147 |
layer(sp.lines(coast,col="black"),under=F)
|
131 |
|
p_min=levelplot(mod09min,col.regions=colr(n),cuts=99,margin=F,maxpixels=res/10,colorkey=list(space="bottom",height=.75),useRaster=T)+
|
|
148 |
p_min=levelplot(mod09min,col.regions=colr(n),cuts=99,margin=F,maxpixels=res/10,colorkey=list(title="Cloud Frequency (%)", space="bottom",height=.75),useRaster=T)+
|
132 |
149 |
layer(sp.lines(coast,col="black"),under=F)
|
133 |
150 |
p_max=levelplot(mod09max,col.regions=colr(n),cuts=99,margin=F,maxpixels=res/10,colorkey=list(space="bottom",height=.75),useRaster=T)+
|
134 |
151 |
layer(sp.lines(coast,col="black"),under=F)
|
... | ... | |
207 |
224 |
#wc=stack(as.list(paste("/mnt/data/jetzlab/Data/environ/global/worldclim/prec_",1:12,".bil",sep="")))
|
208 |
225 |
wc_map=stack(as.list(paste("/mnt/data/jetzlab/Data/environ/global/worldclim/bio_12.bil",sep="")))
|
209 |
226 |
|
210 |
|
|
211 |
|
pdf("output/mod09_worldclim.pdf",width=11,height=8.5)
|
212 |
227 |
regs=list(
|
213 |
228 |
Cascades=extent(c(-122.8,-118,44.9,47)),
|
214 |
229 |
Hawaii=extent(c(-156.5,-154,18.75,20.5)),
|
... | ... | |
218 |
233 |
Madagascar=extent(c(46,52,-17,-12))
|
219 |
234 |
#reg2=extent(c(-81,-70,-4,10))
|
220 |
235 |
)
|
221 |
|
for(r in 1:length(regs)){
|
222 |
|
tmap=crop(wc_map,regs[[r]])
|
223 |
|
p_map=levelplot(tmap,col.regions=grey(seq(0,1,len=100)),cuts=100,at=seq(tmap@data@min,tmap@data@max,len=100),margin=F,maxpixels=1e5,colorkey=list(space="bottom",height=.75),xlab="",ylab="",main=names(regs)[r],useRaster=T)
|
224 |
|
tmac=crop(mod09a,regs[[r]])
|
225 |
|
p_mac=levelplot(tmac,col.regions=grey(seq(0,1,len=100)),cuts=100,at=seq(tmac@data@min,tmac@data@max,len=100),margin=F,maxpixels=1e5,colorkey=list(space="bottom",height=.75),useRaster=T)
|
|
236 |
|
|
237 |
|
|
238 |
#pdf("output/mod09_worldclim.pdf",width=11,height=8.5)
|
|
239 |
#for(r in 1:length(regs)){
|
|
240 |
#tmap=crop(wc_map,regs[[r]])
|
|
241 |
#p_map=levelplot(tmap,col.regions=grey(seq(0,1,len=100)),cuts=100,at=seq(tmap@data@min,tmap@data@max,len=100),margin=F,maxpixels=1e5,colorkey=list(space="bottom",height=.75),xlab="",ylab="",main=names(regs)[r],useRaster=T)
|
|
242 |
#tmac=crop(mod09a,regs[[r]])
|
|
243 |
#p_mac=levelplot(tmac,col.regions=grey(seq(0,1,len=100)),cuts=100,at=seq(tmac@data@min,tmac@data@max,len=100),margin=F,maxpixels=1e5,colorkey=list(space="bottom",height=.75),useRaster=T)
|
226 |
244 |
#p_npp=levelplot(crop(npp,regs[[r]]),col.regions=grey(seq(0,1,len=100)),cuts=99,margin=F,maxpixels=1e5,colorkey=list(space="bottom",height=.5),zscaleLog=T,useRaster=T) #"NPP"=p_npp,
|
227 |
|
p3=c("MOD09 Cloud Frequency (%)"=p_mac,"WorldClim Mean Annual Precip (mm)"=p_map,x.same=T,y.same=T,merge.legends=T,layout=c(2,1))
|
228 |
|
print(p3)
|
229 |
|
}
|
230 |
|
dev.off()
|
|
245 |
#p3=c("MOD09 Cloud Frequency (%)"=p_mac,"WorldClim Mean Annual Precip (mm)"=p_map,x.same=T,y.same=T,merge.legends=T,layout=c(2,1))
|
|
246 |
#print(p3)
|
|
247 |
#}
|
|
248 |
#dev.off()
|
231 |
249 |
|
232 |
250 |
|
233 |
251 |
## reduced resolution
|
234 |
252 |
|
235 |
253 |
## read in GEWEX 1-degree data
|
236 |
254 |
gewex=mean(brick("data/gewex/CA_PATMOSX_NOAA.nc",varname="a_CA"))
|
|
255 |
names(gewex)="PATMOS-x GEWEX AVHRR"
|
|
256 |
|
|
257 |
## calculate 1-degree means of MODCF data
|
|
258 |
#MOD_gewex=gewex
|
|
259 |
#MOD_gewex@data@values=1:length(MOD_gewex@data@values)
|
|
260 |
#MOD_gewex2=zonal(mod09a,MOD_gewex,fun='mean')
|
|
261 |
png("output/Resolution_Figures_%03d.png",width=5500,height=2000,res=600,pointsize=36,bg="white")
|
|
262 |
trellis.par.set(my.theme)
|
|
263 |
#pdf("output/mod09_resolution.pdf",width=11,height=8.5)
|
|
264 |
|
|
265 |
r="Venezuela"
|
|
266 |
# ylab.right = "Cloud Frequency (%)",par.settings = list(layout.widths = list(axis.key.padding = 0.1,axis.left=0.6,ylab.right = 3,right.padding=2)),
|
|
267 |
p1=levelplot(crop(mod09a,regs[[r]]),col.regions=grey(seq(0,1,len=100)),at=seq(45,100,len=99),
|
|
268 |
colorkey=list(space="bottom",width=1,height=1,labels=list(labels=c(50,75,100),at=c(50,75,100))),
|
|
269 |
cuts=99,margin=F,max.pixels=1e5,par.settings = list(layout.heights=list(key.bottom=4),layout.widths = list(axis.key.padding = 0,axis.left=0.6)))
|
|
270 |
p2=levelplot(crop(gewex,regs[[r]]),col.regions=grey(seq(0,1,len=100)),at=seq(.45,1,len=99),cuts=99,margin=F,max.pixels=1e5,colorkey=F)
|
|
271 |
tmap=crop(wc_map,regs[[r]])
|
|
272 |
p3=levelplot(tmap,col.regions=grey(seq(0,1,len=100)),cuts=100,at=seq(tmap@data@min,tmap@data@max,len=100),margin=F,maxpixels=1e5,
|
|
273 |
colorkey=list(space="bottom",height=.33,width=1,labels=list(labels=c(1000,4000),at=c(1000,4000))),xlab="",ylab="",main=names(regs)[r],useRaster=T)
|
|
274 |
print(c("MODCF (%)"=p1,"PATMOS-x GEWEX (%)"=p2,"WorldClim Precip (mm)"=p3,x.same=T,y.same=T,merge.legends=T,layout=c(3,1)))
|
237 |
275 |
|
238 |
|
mod09_8km=aggregate(mod09_mac,8)
|
239 |
|
|
240 |
|
pdf("output/mod09_resolution.pdf",width=11,height=8.5)
|
241 |
|
p1=levelplot(mod09_mac,col.regions=grey(seq(0,1,len=100)),cuts=99,margin=F,max.pixels=1e5)
|
242 |
|
#p2=levelplot(mod09_8km,col.regions=grey(seq(0,1,len=100)),cuts=99,margin=F,max.pixels=1e5)
|
243 |
|
p3=levelplot(gewex,col.regions=grey(seq(0,1,len=100)),cuts=99,margin=F,max.pixels=1e5)
|
244 |
|
print(c(p1,p3,x.same=T,y.same=T,merge.legends=F))
|
|
276 |
dev.off()
|
245 |
277 |
|
246 |
|
p1=levelplot(crop(mac,regs[["Venezuela"]]),col.regions=grey(seq(0,1,len=100)),cuts=99,margin=F,max.pixels=1e5)
|
247 |
|
#p2=levelplot(crop(mod09_8km,reg2),col.regions=grey(seq(0,1,len=100)),cuts=99,margin=F,max.pixels=1e5)
|
248 |
|
p3=levelplot(crop(gewex,regs[["Venezuela"]]),col.regions=grey(seq(0,1,len=100)),cuts=99,margin=F,max.pixels=1e5)
|
249 |
|
print(c(MOD09=p1,GEWEX=p3,x.same=T,y.same=T,merge.legends=F))
|
250 |
278 |
|
251 |
|
p1=levelplot(crop(mod09_mac,reg3),col.regions=grey(seq(0,1,len=100)),cuts=99,margin=F,max.pixels=1e5)
|
252 |
|
#p2=levelplot(crop(mod09_8km,reg3),col.regions=grey(seq(0,1,len=100)),cuts=99,margin=F,max.pixels=1e5)
|
253 |
|
p3=levelplot(crop(mod09_1deg,reg3),col.regions=grey(seq(0,1,len=100)),cuts=99,margin=F,max.pixels=1e5)
|
254 |
|
print(c(p1,p3,x.same=T,y.same=T,merge.legends=F))
|
255 |
|
|
256 |
|
dev.off()
|
|
279 |
#########################################
|
|
280 |
### Some stats for paper
|
257 |
281 |
|
|
282 |
# approximate size of mod09ga archive - get total size for one day from the USGS website
|
|
283 |
size=scan("http://e4ftl01.cr.usgs.gov/MOLT/MOD09GA.005/2000.04.30/",what="char")
|
|
284 |
## extract all filesizes in MB (all the HDFs) and sum them and covert to TB for the length of the full record
|
|
285 |
sum(as.numeric(sub("M","",grep("[0-9]*M$",size,value=T))))/1024/1024*as.numeric(as.Date("2013-12-31")-as.Date("2000-02-24"))
|
258 |
286 |
|
|
287 |
## seasonal variability
|
|
288 |
cellStats(mod09sd,"mean")
|
259 |
289 |
|
260 |
290 |
## Validation table construction
|
261 |
291 |
quantile(cldm$difm,na.rm=T)
|
262 |
292 |
|
263 |
293 |
summary(lm(cld_all~mod09+lat,data=cldm))
|
264 |
294 |
|
|
295 |
|
|
296 |
|
265 |
297 |
|
266 |
298 |
## assess latitude bias
|
267 |
299 |
cldm$abslat=abs(cldm$lat)
|
268 |
300 |
cldm$absdif=abs(cldm$difm)
|
269 |
301 |
|
270 |
302 |
###################################################################
|
271 |
|
### validation by biome
|
272 |
|
bs=biome$BiomeID
|
273 |
|
mod_bs=lapply(bs,function(bs) lm(cld_all~mod09,data=cldm[cldm$biome==bs,]))
|
274 |
|
names(mod_bs)=biome$Biome
|
275 |
|
|
276 |
|
bt=do.call(rbind,tapply(cldm$difm,list(cldm$month,cldm$biome),function(x) c(n=length(x),mean=round(mean(x,na.rm=T),1),sd=round(sd(x,na.rm=T),1))))
|
277 |
|
bt
|
278 |
|
cast(cldm,biome~month,fun=function(x) paste(round(mean(x,na.rm=T),1)," (",round(sd(x,na.rm=T),1),")",sep=""),value="difm")
|
279 |
|
melt(cast(cldm,biome~month,fun=function(x) c(mean=mean(x,na.rm=T),sd=sd(x,na.rm=T)),value="difm"))
|
280 |
|
|
281 |
|
bt=melt(cast(cldm,biome~month2,fun=function(x) c(mean=mean(x,na.rm=T),sd=sd(x,na.rm=T)),value="mod09"))
|
282 |
|
xyplot(value~month2,data=bt)
|
|
303 |
### summary by biome
|
|
304 |
biomep$id=1:nrow(biomep)
|
|
305 |
biomepl=melt(biomep@data,id.vars=c("id","code","biome","realm"))
|
|
306 |
colnames(biomepl)[grep("variable",colnames(biomepl))]="month"
|
|
307 |
biomepl$month=factor(biomepl$month,ordered=T,levels=month.name)
|
|
308 |
biomepl$realm=factor(biomepl$realm,ordered=T,levels=c("Antarctic","Australasia","Oceania", "IndoMalay", "Neotropics","Palearctic","Nearctic" ))
|
|
309 |
biomepl$value[biomepl$value<0]=NA
|
|
310 |
|
|
311 |
|
|
312 |
png("output/Biome_Figures_%03d.png",width=5500,height=4000,res=600,pointsize=36,bg="white")
|
|
313 |
trellis.par.set(my.theme)
|
|
314 |
|
|
315 |
#[biomepl$id%in%sample(biomep$id,10000),]
|
|
316 |
p1=useOuterStrips(xyplot(value~month|realm+biome,groups=id,data=biomepl,panel=function(x,y,groups = groups, subscripts = subscripts){
|
|
317 |
panel.xyplot(x,y,col=grey(0.6,0.2),type="l",lwd=.5,groups=groups,subscripts=subscripts)
|
|
318 |
panel.smoother(y ~ s(x), method = "gam",lwd=2,subscripts=subscripts,n=24)
|
|
319 |
},scales=list(y=list(at=c(0,100),lim=c(-20,120),cex=.75,alternating=2,tck=c(0,1)),x=list(at=c(1,7),rot=45,alternating=1)),ylab="Biome",xlab.top="Geographic Realm",ylab.right="MODCF (%)", xlab="Month"),
|
|
320 |
strip=strip.custom(par.strip.text = list(cex = .7)),strip.left=strip.custom(horizontal=TRUE,par.strip.text = list(cex = .75)))
|
|
321 |
p1$par.settings$layout.widths$strip.left[1]=13
|
|
322 |
p1$par.strip.text$lines=.65
|
|
323 |
print(p1)
|
283 |
324 |
|
284 |
|
print(xtable(bt),type="html")
|
285 |
|
|
286 |
|
#lm_all=lm(cld_all~mod09,data=cldm[!is.na(cldm$cld),])
|
287 |
|
lm_mod=lm(cld~mod09+biome,data=cldm)
|
|
325 |
dev.off()
|
288 |
326 |
|
289 |
|
screenreg(lm_mod,digits=2,single.row=F)
|
290 |
327 |
|
291 |
328 |
####################################################################
|
292 |
329 |
## assess temporal stability
|
updating figures