Project

General

Profile

Download (8.54 KB) Statistics
| Branch: | Revision:
1 f5ef0596 Adam M. Wilson
### Figures and tables for MOD09 Cloud Manuscript
2
3
setwd("~/acrobates/adamw/projects/cloud/")
4
5
6
## libraries
7
library(rasterVis)
8
library(latticeExtra)
9
library(xtable)
10
library(reshape)
11
library(caTools)
12
13
## read in data
14
cld=read.csv("data/NDP026D/cld.csv")
15
cldm=read.csv("data/NDP026D/cldm.csv")
16
cldy=read.csv("data/NDP026D/cldy.csv")
17
clda=read.csv("data/NDP026D/clda.csv")
18
st=read.csv("data/NDP026D/stations.csv")
19
20
21
## add lulc factor information
22
require(plotKML); data(worldgrids_pal)  #load IGBP palette
23
IGBP=data.frame(ID=0:16,col=worldgrids_pal$IGBP[-c(18,19)],stringsAsFactors=F)
24
IGBP$class=rownames(IGBP);rownames(IGBP)=1:nrow(IGBP)
25
26
## month factors
27
cld$month2=factor(cld$month,labels=month.name)
28
cldm$month2=factor(cldm$month,labels=month.name)
29
30
coordinates(st)=c("lon","lat")
31
projection(st)=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
32
33
##make spatial object
34
cldms=cldm
35
coordinates(cldms)=c("lon","lat")
36
projection(cldms)=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
37
38
##make spatial object
39
cldys=cldy
40
coordinates(cldys)=c("lon","lat")
41
projection(cldys)=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
42
43
#### Evaluate MOD35 Cloud data
44 1dc46eb9 Adam M. Wilson
mod09=brick("data/cloud_monthly.nc")
45 8009a075 Adam M. Wilson
mod09c=brick("data/cloud_ymonmean.nc",varname="CF");names(mod09c)=month.name
46
mod09a=brick("data/cloud_mean.nc",varname="CF_annual")#;names(mod09c)=month.name
47
48
mod09min=raster("data/cloud_min.nc",varname="CFmin")
49
mod09max=raster("data/cloud_max.nc",varname="CFmax")
50
mod09sd=raster("data/cloud_std.nc",varname="CFsd")
51
#mod09mean=raster("data/mod09_clim_mac.nc")
52
#names(mod09d)=c("Mean","Minimum","Maximum","Standard Deviation")
53 f5ef0596 Adam M. Wilson
54 ba7057c4 Adam M. Wilson
#plot(mod09a,layers=1,margin=F,maxpixels=100)
55 f5ef0596 Adam M. Wilson
56
## calculated differences
57
cldm$dif=cldm$mod09-cldm$cld
58
clda$dif=clda$mod09-clda$cld
59
60
## read in global coasts for nice plotting
61
library(maptools)
62
63
data(wrld_simpl)
64
coast <- unionSpatialPolygons(wrld_simpl, rep("land",nrow(wrld_simpl)), threshold=5)
65
coast=as(coast,"SpatialLines")
66
67
68
## Figures
69
n=100
70
  at=seq(0,100,length=n)
71
colr=colorRampPalette(c("black","green","red"))
72
cols=colr(n)
73
74
75 ba7057c4 Adam M. Wilson
pdf("output/Figures.pdf",width=11,height=8.5)
76 f5ef0596 Adam M. Wilson
77 ba7057c4 Adam M. Wilson
## Figure 1: 4-panel summaries
78 f5ef0596 Adam M. Wilson
#- Annual average
79 1dc46eb9 Adam M. Wilson
levelplot(mod09a,col.regions=colr(n),cuts=100,at=seq(0,100,len=100),colorkey=list(space="bottom",adj=1),
80 f5ef0596 Adam M. Wilson
  margin=F,maxpixels=1e6,ylab="Latitude",xlab="Longitude",useRaster=T)+
81
  layer(sp.lines(coast,col="black"),under=F)
82
#- Monthly minimum
83
#- Monthly maximum
84
#- STDEV or Min-Max
85 1dc46eb9 Adam M. Wilson
p_mac=levelplot(mod09a,col.regions=colr(n),cuts=99,margin=F,maxpixels=1e5,colorkey=list(space="bottom",height=.75),xlab="",ylab="",main=names(regs)[r],useRaster=T)
86
p_min=levelplot(mod09min,col.regions=colr(n),cuts=99,margin=F,maxpixels=1e5,colorkey=list(space="bottom",height=.75),useRaster=T)
87
p_max=levelplot(mod09max,col.regions=colr(n),cuts=99,margin=F,maxpixels=1e5,colorkey=list(space="bottom",height=.75),useRaster=T)
88
p_sd=levelplot(mod09sd,col.regions=colr(n),cuts=99,margin=F,maxpixels=1e5,colorkey=list(space="bottom",height=.75),useRaster=T)
89 ba7057c4 Adam M. Wilson
p3=c("Mean Cloud Frequency (%)"=p_mac,"Max Cloud Frequency (%)"=p_max,"Min Cloud Frequency (%)"=p_min,"Cloud Frequency Variability (SD)"=p_sd,x.same=T,y.same=T,merge.legends=T,layout=c(2,2))
90
print(p3)
91 f5ef0596 Adam M. Wilson
92
93
### maps of mod09 and NDP
94
## map of stations
95
p_mac=levelplot(mod09a,col.regions=colr(100),cuts=100,at=seq(0,100,len=100),colorkey=list(space="bottom",adj=1),
96
  margin=F,maxpixels=1e6,ylab="Latitude",xlab="Longitude",useRaster=T)+
97
  layer(panel.xyplot(lon,lat,pch=16,cex=.3,col="black"),data=data.frame(coordinates(st)))+
98
  layer(sp.lines(coast,col="black"),under=F)
99
100
p_mace=xyplot(lat~dif,data=cldm[cldm$lat>-60,],panel=function(x,y,subscripts=T){
101
  x2=x[order(y)]
102
  y2=y[order(y)]
103
  win=8000
104
  Q50=runquantile(x2,win,probs=c(.25,.5,.75))
105
  ## polygon
106
  panel.polygon(c(Q50[,1],rev(Q50[,3])),c(y2,rev(y2)),type="l",col=grey(.8))
107
### hist
108
  n=150
109
  xbins=seq(-70,50,len=n)
110
  ybins=seq(-60,90,len=n)
111
  tb=melt(as.matrix(table(
112
    x=cut(x,xbins,labels=xbins[-1]),
113
    y=cut(y,ybins,labels=ybins[-1]))))
114
  qat=unique(tb$value)
115
  print(qat)
116
  panel.levelplot(tb$x,tb$y,tb$value,at=qat,col.regions=c("transparent",hmcols(length(qat))),subscripts=1:nrow(tb))
117
###
118
#  panel.xyplot(x,y,pch=16,cex=.1,col=)
119
#  cuts=cut(y,lats,labels=lats[-10])
120
#  qs=do.call(rbind,tapply(x,cuts,quantile,c(.25,.50,.75),na.rm=T))
121
  colnames(qs)=c("Q25","Q50","Q75")
122
  panel.lines(Q50[,1],y2,type="l",col=grey(.5))
123
  panel.lines(Q50[,2],y2,type="l",col="black")
124
  panel.lines(Q50[,3],y2,type="l",col=grey(.5))
125
},asp=1,xlab="Difference (MOD09-Observed)")+layer(panel.abline(v=0,lty="dashed",col="red"))
126
127
print(p_mac,position=c(0,0,.75,1),more=T)
128
print(p_mace,position=c(0.75,0,1,1))
129
130
131
p1=c("MODIS Cloud Frequency and NDP-026D Validation Stations"=p_mac,"Difference (MOD09-NDP026D)"=p_mace,x.same=F,y.same=T,layout=c(2,1))
132
resizePanels(p1,w=c(.75,.25))
133
134
quantile(cldm$dif,seq(0,1,len=6),na.rm=T)
135
at=c(-70,-50,-25,-10,-5,0,5,10,25,50,70)
136
bwr=colorRampPalette(c("blue","grey","red"))
137
xyplot(lat~lon|month2,data=cldm,groups=cut(cldm$dif,at),
138
       par.settings=list(superpose.symbol=list(col=bwr(length(at)-1))),pch=16,cex=.25,
139
       auto.key=list(space="right",title="Difference\n(MOD09-NDP026D)",cex.title=1),asp=1,
140
       main="NDP-026D Cloud Climatology Stations",ylab="Latitude",xlab="Longitude")+
141
  layer(sp.lines(coast,col="black",lwd=.1),under=F)
142
143
144
145
### heatmap of mod09 vs. NDP for all months
146
hmcols=colorRampPalette(c("grey","blue","red"))
147
#hmcols=colorRampPalette(c(grey(.8),grey(.3),grey(.2)))
148
tr=c(0,120)
149
colkey <- draw.colorkey(list(col = hmcols(tr[2]), at = tr[1]:tr[2],height=.25))
150
151
xyplot(cld~mod09,data=cld[cld$Nobs>10,],panel=function(x,y,subscripts){
152
  n=150
153
  bins=seq(0,100,len=n)
154
  tb=melt(as.matrix(table(
155
    x=cut(x,bins,labels=bins[-1]),
156
    y=cut(y,bins,labels=bins[-1]))))
157
  qat=tr[1]:tr[2]#unique(tb$value)
158
  print(qat)
159
  panel.levelplot(tb$x,tb$y,tb$value,at=qat,col.regions=c("transparent",hmcols(length(qat))),subscripts=subscripts)
160
  },asp=1,scales=list(at=seq(0,100,len=6)),ylab="NDP Mean Cloud Amount (%)",xlab="MOD09 Cloud Frequency (%)",
161
       legend= list(right = list(fun = colkey,title="Station Count")))+
162
  layer(panel.abline(0,1,col="black",lwd=2))
163
#  layer(panel.ablineq(lm(y ~ x), r.sq = TRUE,at = 0.6,pos=1, offset=22,digits=2,col="blue"), style = 1)
164
165
166
xyplot(cld~mod09|month2,data=cld[cld$Nobs>50,],panel=function(x,y,subscripts){
167
  n=50
168
  bins=seq(0,100,len=n)
169
  tb=melt(as.matrix(table(
170
    x=cut(x,bins,labels=bins[-1]),
171
    y=cut(y,bins,labels=bins[-1]))))
172
  qat=unique(tb$value)
173
  qat=0:78
174
  qat=tr[1]:tr[2]#unique(tb$value)
175
  panel.levelplot(tb$x,tb$y,tb$value,at=qat,col.regions=c("transparent",hmcols(length(qat))),subscripts=1:nrow(tb))
176
  panel.abline(0,1,col="black",lwd=2)
177
#  panel.ablineq(lm(y ~ x), r.sq = TRUE,at = 0.6,pos=1, offset=0,digits=2,col="blue")
178
  panel.text(70,10,bquote(paste(R^2,"=",.(round(summary(lm(y ~ x))$r.squared,2)))),cex=1.2)
179
},asp=1,scales=list(at=seq(0,100,len=6),useRaster=T,colorkey=list(width=.5,title="Number of Stations")),
180
          ylab="NDP Mean Cloud Amount (%)",xlab="MOD09 Cloud Frequency (%)",
181
              legend= list(right = list(fun = colkey)))+ layer(panel.abline(0,1,col="black",lwd=2))
182
183
184
## Monthly Climatologies
185
for(i in 1:2){
186
 p1=xyplot(cld~mod09|month2,data=cldm,cex=.2,pch=16,subscripts=T,ylab="NDP Mean Cloud Amount",xlab="MOD09 Cloud Frequency (%)")+
187
  layer(panel.lines(1:100,predict(lm(y~x),newdata=data.frame(x=1:100)),col="green"))+
188
  layer(panel.lines(1:100,predict(lm(y~x+I(x^2)),newdata=data.frame(x=1:100)),col="blue"))+
189
  layer(panel.abline(0,1,col="red"))
190
    if(i==2){
191
     p1=p1+layer(panel.segments(mod09[subscripts],cld[subscripts]-cldsd[subscripts],mod09[subscripts],cld[subscripts]+cldsd[subscripts],subscripts=subscripts,col="grey"),data=cldm,under=T,magicdots=T)
192
     p1=p1+layer(panel.segments(mod09[subscripts]-mod09sd[subscripts],cld[subscripts],mod09[subscripts]+mod09sd[subscripts],cld[subscripts],subscripts=subscripts,col="grey"),data=cldm,under=T,magicdots=T) 
193
       }
194
print(p1)
195
}
196
197
bwplot(lulcc~dif,data=cldm,horiz=T,xlab="Difference (MOD09-Observed)",varwidth=T,notch=T)+layer(panel.abline(v=0))
198
199
200
dev.off()
201
202
203
summary(lm(cld~mod09,data=cld))
204
205
## explore validation error
206
cldm$lulcc=as.factor(IGBP$class[match(cldm$lulc,IGBP$ID)])
207
208
## Table of RMSE's by lulc by month
209
lulcrmsel=ddply(cldm,c("month","lulc"),function(x) c(count=nrow(x),rmse=sqrt(mean((x$mod09-x$cld)^2,na.rm=T))))
210
lulcrmsel=lulcrmsel[!is.na(lulcrmsel$lulc),]
211
lulcrmsel$lulcc=as.factor(IGBP$class[match(lulcrmsel$lulc,IGBP$ID)])
212
213
lulcrmse=cast(lulcrmsel,lulcc~month,value="rmse")
214
lulcrmse
215
print(xtable(lulcrmse,digits=1),"html")
216
  
217
levelplot(rmse~lulc*month,data=lulcrmsel,col.regions=heat.colors(20))
218
219
220
### Linear models
221
summary(lm(dif~as.factor(lulc)+lat+month2,data=cldm))