1
|
###################################################################################
|
2
|
### R code to aquire and process MOD06_L2 cloud data from the MODIS platform
|
3
|
|
4
|
|
5
|
## connect to server of choice
|
6
|
#system("ssh litoria")
|
7
|
#R
|
8
|
|
9
|
library(sp)
|
10
|
library(spgrass6)
|
11
|
library(rgdal)
|
12
|
library(reshape2)
|
13
|
library(ncdf4)
|
14
|
library(geosphere)
|
15
|
library(rgeos)
|
16
|
library(multicore)
|
17
|
library(raster)
|
18
|
library(lattice)
|
19
|
library(rgl)
|
20
|
library(hdf5)
|
21
|
library(rasterVis)
|
22
|
library(heR.Misc)
|
23
|
|
24
|
X11.options(type="Xlib")
|
25
|
ncores=20 #number of threads to use
|
26
|
|
27
|
setwd("/home/adamw/personal/projects/interp")
|
28
|
setwd("/home/adamw/acrobates/projects/interp")
|
29
|
|
30
|
roi=readOGR("data/regions/Test_sites/Oregon.shp","Oregon")
|
31
|
roi_geo=as(roi,"SpatialLines")
|
32
|
roi=spTransform(roi,CRS(" +proj=sinu +lon_0=0 +x_0=0 +y_0=0"))
|
33
|
roil=as(roi,"SpatialLines")
|
34
|
|
35
|
summarydatadir="data/modis/MOD06_climatologies"
|
36
|
|
37
|
|
38
|
|
39
|
##########################
|
40
|
#### explore the data
|
41
|
|
42
|
## load data
|
43
|
months=seq(as.Date("2000-01-15"),as.Date("2000-12-15"),by="month")
|
44
|
cerfiles=list.files(summarydatadir,pattern="CER_mean_.*tif$",full=T); cerfiles
|
45
|
cer=brick(stack(cerfiles))
|
46
|
setZ(cer,months,name="time")
|
47
|
cer@z=list(months)
|
48
|
cer@zname="time"
|
49
|
layerNames(cer) <- as.character(format(months,"%b"))
|
50
|
#cer=projectRaster(from=cer,crs="+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0",method="ngb")
|
51
|
### TODO: change to bilinear!
|
52
|
|
53
|
cotfiles=list.files(summarydatadir,pattern="COT_mean_.*tif$",full=T); cotfiles
|
54
|
cot=brick(stack(cotfiles))
|
55
|
setZ(cot,months,name="time")
|
56
|
cot@z=list(months)
|
57
|
cot@zname="time"
|
58
|
layerNames(cot) <- as.character(format(months,"%b"))
|
59
|
#cot=projectRaster(from=cot,crs="+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0",method="ngb")
|
60
|
cotm=mean(cot,na.rm=T)
|
61
|
### TODO: change to bilinear!
|
62
|
|
63
|
cldfiles=list.files(summarydatadir,pattern="CLD_mean_.*tif$",full=T); cldfiles
|
64
|
cld=brick(stack(cldfiles))
|
65
|
cld[cld==0]=NA
|
66
|
setZ(cld,months,name="time")
|
67
|
cld@z=list(months)
|
68
|
cld@zname="time"
|
69
|
layerNames(cld) <- as.character(format(months,"%b"))
|
70
|
#cot=projectRaster(from=cot,crs="+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0",method="ngb")
|
71
|
cldm=mean(cld,na.rm=T)
|
72
|
### TODO: change to bilinear!
|
73
|
|
74
|
|
75
|
### get station data, subset to stations in region, and transform to sinusoidal
|
76
|
load("data/ghcn/roi_ghcn.Rdata")
|
77
|
load("data/allstations.Rdata")
|
78
|
|
79
|
d2=d[d$variable=="ppt"&d$date>=as.Date("2000-01-01"),]
|
80
|
d2=d2[,-grep("variable",colnames(d2)),]
|
81
|
st2=st[st$id%in%d$id,]
|
82
|
#st2=spTransform(st2,CRS(" +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +towgs84=0,0,0"))
|
83
|
d2[,c("lon","lat")]=coordinates(st2)[match(d2$id,st2$id),]
|
84
|
d2$month=format(d2$date,"%m")
|
85
|
d2$value=d2$value/10 #convert to mm
|
86
|
|
87
|
### extract MOD06 data for each station
|
88
|
stcer=extract(cer,st2)#;colnames(stcer)=paste("cer_mean_",1:12,sep="")
|
89
|
stcot=extract(cot,st2)#;colnames(stcot)=paste("cot_mean_",1:12,sep="")
|
90
|
stcld=extract(cld,st2)#;colnames(stcld)=paste("cld_mean_",1:12,sep="")
|
91
|
mod06=cbind.data.frame(id=st2$id,lat=st2$lat,lon=st2$lon,stcer,stcot,stcld)
|
92
|
mod06l=melt(mod06,id.vars=c("id","lon","lat"))
|
93
|
mod06l[,c("variable","moment","month")]=do.call(rbind,strsplit(as.character(mod06l$variable),"_"))
|
94
|
mod06l=as.data.frame(cast(mod06l,id+lon+lat+month~variable+moment,value="value"))
|
95
|
|
96
|
### Identify stations that have < 10 years of data
|
97
|
cnts=cast(d2,id~.,fun=function(x) length(x[!is.na(x)]),value="count");colnames(cnts)[colnames(cnts)=="(all)"]="count"
|
98
|
summary(cnts)
|
99
|
## drop them
|
100
|
d2=d2[d2$id%in%cnts$id[cnts$count>=365*10],]
|
101
|
|
102
|
|
103
|
### generate monthly means of station data
|
104
|
dc=cast(d2,id+lon+lat~month,value="value",fun=function(x) mean(x,na.rm=T)*30)
|
105
|
dcl=melt(dc,id.vars=c("id","lon","lat"),value="ppt")
|
106
|
|
107
|
|
108
|
## merge station data with mod06
|
109
|
mod06s=merge(dcl,mod06l)
|
110
|
mod06s$lvalue=log(mod06s$value+1)
|
111
|
colnames(mod06s)[colnames(mod06s)=="value"]="ppt"
|
112
|
|
113
|
|
114
|
###################################################################
|
115
|
###################################################################
|
116
|
### draw some plots
|
117
|
gq=function(x,n=10,cut=F) {
|
118
|
if(!cut) unique(quantile(x,seq(0,1,len=n+1)))
|
119
|
if(cut) cut(x,unique(quantile(x,seq(0,1,len=n+1))))
|
120
|
}
|
121
|
|
122
|
bgyr=colorRampPalette(c("blue","green","yellow","red"))
|
123
|
|
124
|
pdf("output/MOD06_summary.pdf",width=11,height=8.5)
|
125
|
|
126
|
# % cloudy maps
|
127
|
title="Cloudiness (% cloudy days) "
|
128
|
at=unique(quantile(as.matrix(cld),seq(0,1,len=100),na.rm=T))
|
129
|
p=levelplot(cld,xlab.top=title,at=at,col.regions=bgyr(length(at)))+layer(sp.lines(roil, lwd=1.2, col='black'))
|
130
|
print(p)
|
131
|
bwplot(cer,main=title,ylab="Cloud Effective Radius (microns)")
|
132
|
|
133
|
# CER maps
|
134
|
title="Cloud Effective Radius (microns)"
|
135
|
at=quantile(as.matrix(cer),seq(0,1,len=100),na.rm=T)
|
136
|
p=levelplot(cer,xlab.top=title,at=at,col.regions=bgyr(length(at)))+layer(sp.lines(roil, lwd=1.2, col='black'))
|
137
|
print(p)
|
138
|
bwplot(cer,main=title,ylab="Cloud Effective Radius (microns)")
|
139
|
|
140
|
# COT maps
|
141
|
title="Cloud Optical Thickness (%)"
|
142
|
at=quantile(as.matrix(cot),seq(0,1,len=100),na.rm=T)
|
143
|
p=levelplot(cot,xlab.top=title,at=at,col.regions=bgyr(length(at)))+layer(sp.lines(roil, lwd=0.8, col='black'))
|
144
|
print(p)
|
145
|
bwplot(cot,xlab.top=title,ylab="Cloud Optical Thickness (%)")
|
146
|
|
147
|
|
148
|
### Comparison at station values
|
149
|
at=quantile(as.matrix(cotm),seq(0,1,len=100),na.rm=T)
|
150
|
p=levelplot(cotm, layers=1,at=at,col.regions=bgyr(length(at)),main="Mean Annual Cloud Optical Thickness",FUN.margin=function(x) 0)+
|
151
|
layer(sp.lines(roil, lwd=1.2, col='black'))+layer(sp.points(st2, pch=16, col='black'))
|
152
|
print(p)
|
153
|
|
154
|
### monthly comparisons of variables
|
155
|
mod06sl=melt(mod06s,measure.vars=c("value","COT_mean","CER_mean"))
|
156
|
bwplot(value~month|variable,data=mod06sl,cex=.5,pch=16,col="black",scales=list(y=list(relation="free")),layout=c(1,3))
|
157
|
splom(mod06s[grep("CER|COT|CLD",colnames(mod06s))],cex=.2,pch=16)
|
158
|
|
159
|
### run some regressions
|
160
|
summary(lm(log(ppt)~CER_mean*month,data=mod06s))
|
161
|
|
162
|
xyplot(ppt~CLD_mean,data=mod06s,cex=.5,pch=16,col="black",scales=list(y=list(log=F)),main="Comparison of monthly mean CLD and precipitation",ylab="Precipitation (log axis)",xlab="% Days Cloudy")
|
163
|
xyplot(ppt~CER_mean,data=mod06s,cex=.5,pch=16,col="black",scales=list(y=list(log=T)),main="Comparison of monthly mean CER and precipitation",ylab="Precipitation (log axis)")
|
164
|
xyplot(ppt~COT_mean,data=mod06s,cex=.5,pch=16,col="black",scales=list(y=list(log=T)),main="Comparison of monthly mean COT and precipitation",ylab="Precipitation (log axis)")
|
165
|
|
166
|
xyplot(ppt~CER_mean|month,data=mod06s,cex=.5,pch=16,col="black",scales=list(log=T,relation="free"))
|
167
|
xyplot(ppt~COT_mean|month,data=mod06s,cex=.5,pch=16,col="black",scales=list(log=T,relation="free"))
|
168
|
|
169
|
xyplot(ppt~COT_mean|id,data=mod06s,panel=function(x,y,group){
|
170
|
panel.xyplot(x,y,type=c("r"),cex=.5,pch=16,col="red")
|
171
|
panel.xyplot(x,y,type=c("p"),cex=.5,pch=16,col="black")
|
172
|
} ,scales=list(y=list(log=T)),strip=F,main="Monthly Mean Precipitation and Cloud Optical Thickness by station",sub="Each panel is a station, each point is a monthly mean",ylab="Precipitation (mm, log axis)",xlab="Mean Monthly Cloud Optical Thickness")
|
173
|
|
174
|
### Calculate the slope of each line and plot it on a map
|
175
|
mod06s.sl=dapply(mod06s,list(id=mod06s$id),function(x){
|
176
|
lm1=lm(log(x$ppt)~x$CER_mean,)
|
177
|
data.frame(lat=x$lat[1],lon=x$lon[1],intcpt=coefficients(lm1)[1],cer=coefficients(lm1)[2],r2=summary(lm1)$r.squared)
|
178
|
})
|
179
|
mod06s.sl$cex=gq(mod06s.sl$r2,n=5,cut=T)
|
180
|
mod06s.sl$cer.s=gq(mod06s.sl$cer,n=5,cut=T)
|
181
|
|
182
|
xyplot(lat~lon,group=cer.s,data=mod06s.sl,par.settings = list(superpose.symbol = list(pch =16, col=bgyr(5),cex=1)),auto.key=list(space="right",title="Slope Coefficient"),asp=1,
|
183
|
main="Slopes of linear regressions {log(ppt)~CloudEffectiveRadius}")+
|
184
|
layer(sp.lines(roi_geo, lwd=1.2, col='black'))
|
185
|
|
186
|
|
187
|
############################################################
|
188
|
### simple regression to get spatial residuals
|
189
|
m="01"
|
190
|
mod06s2=mod06s#[mod06s$month==m,]
|
191
|
|
192
|
lm1=lm(ppt~CER_mean*month,data=mod06s2)
|
193
|
summary(lm1)
|
194
|
mod06s2$pred=predict(lm1,mod06s2)
|
195
|
mod06s2$resid=mod06s2$pred-mod06s2$ppt
|
196
|
|
197
|
mod06sr=cast(mod06s2,id+lon+lat~month,value="resid",fun=function(x) mean(x,na.rm=T))
|
198
|
mod06sr=melt(mod06sr,id.vars=c("id","lon","lat"),value="resid")
|
199
|
mod06sr$residg=cut(mod06sr$value,quantile(mod06sr$value,seq(0,1,len=11),na.rm=T))
|
200
|
|
201
|
xyplot(lat~lon|month,group=residg,data=mod06sr,
|
202
|
par.settings = list(superpose.symbol = list(pch =16, col=terrain.colors(10),cex=.5)),
|
203
|
auto.key=T)
|
204
|
|
205
|
|
206
|
|
207
|
plot(pred~value,data=mod06s,log="xy")
|
208
|
|
209
|
|
210
|
|
211
|
dev.off()
|
212
|
|
213
|
|
214
|
|
215
|
|
216
|
|
217
|
|
218
|
|
219
|
|
220
|
|
221
|
|
222
|
|
223
|
|
224
|
load("data/modis/pointsummary.Rdata")
|
225
|
|
226
|
|
227
|
dsl=melt(ds,id.vars=c("id","date","ppt","lon","lat"),measure.vars= c("Cloud_Water_Path","Cloud_Effective_Radius","Cloud_Optical_Thickness"))
|
228
|
|
229
|
dsl=dsl[!is.nan(dsl$value),]
|
230
|
|
231
|
|
232
|
summary(lm(ppt~Cloud_Effective_Radius,data=ds))
|
233
|
summary(lm(ppt~Cloud_Water_Path,data=ds))
|
234
|
summary(lm(ppt~Cloud_Optical_Thickness,data=ds))
|
235
|
summary(lm(ppt~Cloud_Effective_Radius+Cloud_Water_Path+Cloud_Optical_Thickness,data=ds))
|
236
|
|
237
|
|
238
|
####
|
239
|
## mean annual precip
|
240
|
dp=d[d$variable=="ppt",]
|
241
|
dp$year=format(dp$date,"%Y")
|
242
|
dm=tapply(dp$value,list(id=dp$id,year=dp$year),sum,na.rm=T)
|
243
|
dms=apply(dm,1,mean,na.rm=T)
|
244
|
dms=data.frame(id=names(dms),ppt=dms/10)
|
245
|
|
246
|
dslm=tapply(dsl$value,list(id=dsl$id,variable=dsl$variable),mean,na.rm=T)
|
247
|
dslm=data.frame(id=rownames(dslm),dslm)
|
248
|
|
249
|
dms=merge(dms,dslm)
|
250
|
dmsl=melt(dms,id.vars=c("id","ppt"))
|
251
|
|
252
|
summary(lm(ppt~Cloud_Effective_Radius,data=dms))
|
253
|
summary(lm(ppt~Cloud_Water_Path,data=dms))
|
254
|
summary(lm(ppt~Cloud_Optical_Thickness,data=dms))
|
255
|
summary(lm(ppt~Cloud_Effective_Radius+Cloud_Water_Path+Cloud_Optical_Thickness,data=dms))
|
256
|
|
257
|
|
258
|
#### draw some plots
|
259
|
#pdf("output/MOD06_summary.pdf",width=11,height=8.5)
|
260
|
png("output/MOD06_summary_%d.png",width=1024,height=780)
|
261
|
|
262
|
## daily data
|
263
|
xyplot(value~ppt/10|variable,data=dsl,
|
264
|
scales=list(relation="free"),type=c("p","r"),
|
265
|
pch=16,cex=.5,layout=c(3,1))
|
266
|
|
267
|
|
268
|
densityplot(~value|variable,groups=cut(dsl$ppt,c(0,50,100,500)),data=dsl,auto.key=T,
|
269
|
scales=list(relation="free"),plot.points=F)
|
270
|
|
271
|
## annual means
|
272
|
|
273
|
xyplot(value~ppt|variable,data=dmsl,
|
274
|
scales=list(relation="free"),type=c("p","r"),pch=16,cex=0.5,layout=c(3,1),
|
275
|
xlab="Mean Annual Precipitation (mm)",ylab="Mean value")
|
276
|
|
277
|
densityplot(~value|variable,groups=cut(dsl$ppt,c(0,50,100,500)),data=dmsl,auto.key=T,
|
278
|
scales=list(relation="free"),plot.points=F)
|
279
|
|
280
|
|
281
|
## plot some swaths
|
282
|
|
283
|
nc1=raster(fs$path[3],varname="Cloud_Effective_Radius")
|
284
|
nc2=raster(fs$path[4],varname="Cloud_Effective_Radius")
|
285
|
nc3=raster(fs$path[5],varname="Cloud_Effective_Radius")
|
286
|
|
287
|
nc1[nc1<=0]=NA
|
288
|
nc2[nc2<=0]=NA
|
289
|
nc3[nc3<=0]=NA
|
290
|
|
291
|
plot(roi)
|
292
|
plot(nc3)
|
293
|
|
294
|
plot(nc1,add=T)
|
295
|
plot(nc2,add=T)
|
296
|
|
297
|
|
298
|
dev.off()
|
299
|
|
300
|
|