Project

General

Profile

Download (15.4 KB) Statistics
| Branch: | Revision:
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(reshape)
13
library(ncdf4)
14
library(geosphere)
15
library(rgeos)
16
library(multicore)
17
library(raster)
18
library(lattice)
19
library(latticeExtra)
20
library(rgl)
21
library(hdf5)
22
library(rasterVis)
23
library(heR.Misc)
24
library(car)
25
library(mgcv)
26
library(sampling)
27

    
28
X11.options(type="Xlib")
29
ncores=20  #number of threads to use
30

    
31
setwd("/home/adamw/acrobates/projects/interp")
32

    
33
## get MODLAND tile information
34
tb=read.table("http://landweb.nascom.nasa.gov/developers/sn_tiles/sn_bound_10deg.txt",skip=6,nrows=648,header=T)
35
tb$tile=paste("h",sprintf("%02d",tb$ih),"v",sprintf("%02d",tb$iv),sep="")
36
save(tb,file="modlandTiles.Rdata")
37

    
38
tile="h11v08"   #can move this to submit script if needed
39
#tile="h09v04"   #oregon
40

    
41
psin=CRS("+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs")
42

    
43
tile_bb=tb[tb$tile==tile,] ## identify tile of interest
44
roi_ll=extent(tile_bb$lon_min,tile_bb$lon_max,tile_bb$lat_min,tile_bb$lat_max) 
45
#roi=spTransform(roi,psin)
46
#roil=as(roi,"SpatialLines")
47

    
48
dmod06="data/modis/mod06/summary"
49

    
50

    
51
##########################
52
#### Organize the data
53
months=seq(as.Date("2000-01-15"),as.Date("2000-12-15"),by="month")
54

    
55
getmod06<-function(variable){
56
  d=brick(list.files(dmod06,pattern=paste("MOD06_",tile,".nc",sep=""),full=T),varname=toupper(variable))
57
#  d=dropLayer(d,1)
58
  projection(d)=psin
59
  setZ(d,format(as.Date(d@z$Date),"%m"),name="time")
60
#  d@z=as.Date(d@z$Date)
61
  layerNames(d) <- as.character(format(as.Date(d@z$Date),"%b")) #paste(variable,format(as.Date(d@z$Date),"%m"))
62
  return(d)
63
}
64

    
65
# drop #1?
66

    
67
cer=getmod06("cer")
68
cld=getmod06("cld")
69
cot=getmod06("cot")
70
cer20=getmod06("cer20")
71

    
72
pcol=colorRampPalette(c("brown","red","yellow","darkgreen"))
73
#levelplot(cer,col.regions=pcol(20))
74

    
75
## load WorldClim data for comparison (download then uncompress)
76
#system("wget -P data/worldclim/ http://biogeo.ucdavis.edu/data/climate/worldclim/1_4/grid/cur/prec_30s_bil.zip",wait=F)
77
#system("wget -P data/worldclim/ http://biogeo.ucdavis.edu/data/climate/worldclim/1_4/grid/cur/alt_30s_bil.zip",wait=F)
78

    
79
### load WORLDCLIM elevation 
80
#dem=raster(list.files("data/worldclim/alt_30s_bil/",pattern="bil$",full=T))
81
#projection(dem)=CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
82
#dem=crop(dem,roi_ll)
83
#dem[dem>60000]=NA
84
#dem=projectRaster(dem,cer)
85
#writeRaster(dem,file=paste("data/tiles/",tile,"/dem_",tile,".tif",sep=""),format="GTiff")
86
dem=raster(paste("data/tiles/",tile,"/dem_",tile,".tif",sep=""))
87

    
88
### get station data, subset to stations in region, and transform to sinusoidal
89
dm=readOGR(paste("data/tiles/",tile,sep=""),paste("station_monthly_",tile,"_PRCP",sep=""))
90
colnames(dm@data)[grep("elevation",colnames(dm@data))]="dem"
91
colnames(dm@data)[grep("value",colnames(dm@data))]="ppt"
92

    
93
                                        #xyplot(latitude~longitude|month,data=dm@data)
94
dm2=spTransform(dm,CRS(projection(cer)))
95
dm2@data[,c("x","y")]=coordinates(dm2)
96

    
97
### extract MOD06 data for each station
98
stcer=extract(cer,dm2,fun=mean);colnames(stcer)=paste("cer_mean_",as.numeric(format(as.Date(cer@z$Date),"%m")),sep="")
99
stcer20=extract(cer20,dm2,fun=mean);colnames(stcer20)=paste("cer20_mean_",as.numeric(format(as.Date(cer20@z$Date),"%m")),sep="")
100
stcot=extract(cot,dm2);colnames(stcot)=paste("cot_mean_",as.numeric(format(as.Date(cot@z$Date),"%m")),sep="")
101
stcld=extract(cld,dm2);colnames(stcld)=paste("cld_mean_",as.numeric(format(as.Date(cld@z$Date),"%m")),sep="")
102
stdem=extract(dem,dm2)
103
#mod06=cbind.data.frame(station=dm$station,stcer[,-1],stcot[,-1],stcld[,-1],stcer20[,-1])
104
mod06=cbind.data.frame(station=dm$station,stcer,stcot,stcld,stcer20)
105
mod06l=melt(mod06,id.vars=c("station"));colnames(mod06l)[grep("value",colnames(mod06l))]="mod06"
106
mod06l[,c("variable","moment","month")]=do.call(rbind,strsplit(as.character(mod06l$variable),"_"))
107
mod06l=unique(mod06l)
108
mod06l=cast(mod06l,station+moment+month~variable,value="mod06")
109
mod06l=merge(dm2@data,mod06l,by=c("station","month"))
110
mod06l=mod06l[!is.na(mod06l$cer),]
111

    
112
mod06l=mod06l[order(mod06l$month),]
113

    
114
#xyplot(value~cer|month,data=mod06l,scales=list(relation="free"),pch=16,cex=.5)
115
#xyplot(value~cer|station,data=mod06l[mod06l$count>400,],pch=16,cex=.5)
116
#xyplot(cot~month,groups=station,data=mod06l,type="l")
117

    
118
### create monthly raster bricks for prediction
119
m=2
120

    
121
pdata=stack(
122
  subset(cer,subset=m),
123
  subset(cot,subset=m),
124
  subset(cer20,subset=m),
125
  subset(cld,subset=m)
126
  )
127

    
128
## Set up models to compare
129
####################################
130
#### build table comparing various metrics
131
models=c(
132
  "ppt~s(y)+ s(x)",
133
  "ppt~s(y,x)",
134
  "ppt~s(y,x) + s(dem)",
135
  "ppt~s(y,x)+s(dem)+cer+cld+cot+cer20",
136
  "ppt~s(y,x)+s(dem)+s(cer)",
137
  "ppt~s(y,x)+s(dem)+s(cer20)",
138
  "ppt~s(y,x)+s(dem)+s(cld)",
139
  "ppt~s(y,x)+s(dem)+s(cot)",
140
  "ppt~s(y,x)+s(dem)+s(cer20,cld)",
141
  "ppt~s(y,x)+s(dem)+s(cer20,cot)",
142
  "ppt~s(y,x)+s(dem)+s(cer,cld)",
143
  "ppt~s(dem)+s(cer,cot)")
144
months=1:12
145

    
146
## build the list of models/months to process
147
mm=expand.grid(model=models,month=months)
148
mm$model=as.character(mm$model)
149
mm$mid=match(mm$model,models)
150
  
151
#  mod1<- gam(tmax~ s(lat) + s (lon) + s (ELEV_SRTM), data=data_s)
152
#  mod2<- gam(tmax~ s(lat,lon,ELEV_SRTM), data=data_s)
153
#  mod3<- gam(tmax~ s(lat) + s (lon) + s (ELEV_SRTM) +  s (Northness)+ s (Eastness) + s(DISTOC), data=data_s)
154
#  mod4<- gam(tmax~ s(lat) + s (lon) + s(ELEV_SRTM) + s(Northness) + s (Eastness) + s(DISTOC) + s(LST), data=data_s)
155
#  mod5<- gam(tmax~ s(lat,lon) +s(ELEV_SRTM) + s(Northness,Eastness) + s(DISTOC) + s(LST), data=data_s)
156
#  mod6<- gam(tmax~ s(lat,lon) +s(ELEV_SRTM) + s(Northness,Eastness) + s(DISTOC) + s(LST,LC1), data=data_s)
157
#  mod7<- gam(tmax~ s(lat,lon) +s(ELEV_SRTM) + s(Northness,Eastness) + s(DISTOC) + s(LST,LC3), data=data_s)
158
#  mod8<- gam(tmax~ s(lat,lon) +s(ELEV_SRTM) + s(Northness,Eastness) + s(DISTOC) + s(LST) + s(LC1), data=data_s)
159

    
160
### Sample validation stations
161
prop=0.1
162
mod06l$validation=F
163
mod06l$validation[as.numeric(rownames(strata(mod06l,stratanames="month",size=as.integer(prop*table(mod06l$month)),method="srswor")))]=T
164

    
165
### run it
166
mods=lapply(1:nrow(mm),function(i){
167
    mod=try(gam(as.formula(mm$model[i]),data=mod06l[mod06l$month==mm$month[i]&!mod06l$validation,]))
168
  ## add some attributes to the object to help ID it later
169
  attr(mod,"month")=mm$month[i]
170
  attr(mod,"model")=mm$model[i]
171
  attr(mod,"modelid")=mm$mid[i]
172
  print(paste("Finished model ",mm$model[i]," for month",mm$month[i]))
173
  return(mod)
174
})
175

    
176
## Get summary stats
177
sums=do.call(rbind.data.frame,lapply(mods,function(m,plot=F){
178
  if(class(m)=="try-error") {
179
    return(data.frame(model=attr(m,"model"),
180
                      modelid=attr(m,"modelid"),
181
                      month=attr(m,"month"),
182
                      r2=NA,
183
                      dev=NA,
184
                      aic=NA,
185
                      rmse=NA,
186
                      vr2=NA))
187
  }
188

    
189
  ## make validation predictions
190
  vd=mod06l[mod06l$validation&mod06l$month==attr(m,"month"),]
191
  y=predict(m,mod06l[mod06l$validation&mod06l$month==attr(m,"month"),])
192
  vlm=lm(y~vd$ppt)
193

    
194
    ## Draw some plots
195
  if(plot){
196
    ## partial residual plots
197
    var=2
198
    fv <- predict(m,type="terms") ## get term estimates
199
    ## compute partial residuals for first smooth...
200
    prsd1 <- residuals(m,type="working") + fv[,var]
201
    plot(m,select=var) ## plot first smooth
202
    points(mod06l$cot[mod06l$month==mm$month[i]&!mod06l$validation],prsd1,pch=16,col="red")
203
  }
204

    
205
  ## summarize validation
206
  s1=summary(m)
207
  data.frame(
208
             model=attr(m,"model"),
209
             modelid=attr(m,"modelid"),
210
             month=attr(m,"month"),
211
             r2=s1$r.sq,
212
             dev=s1$dev.expl,
213
             aic=AIC(m),
214
             rmse=sqrt(mean((vd$ppt-predict(vlm,vd))^2)),
215
             vr2=summary(vlm)$r.squared)
216
}))
217

    
218
### Summary Figures
219

    
220
sumsl=melt(sums,id.vars=c("model","modelid","month"))
221

    
222
sum2=cast(sumsl,model~variable, fun=function(x) c(mean=mean(x,na.rm=T),sd=sd(x,na.rm=T)))
223

    
224
#combineLimits(useOuterStrips(xyplot(value~month|model+variable,data=sumsl,scale=list(relation="free"))))
225
bwplot(model~value|variable,data=sumsl,scale=list(x=list(relation="free")))
226

    
227
xyplot(ppt~cld|station,groups=month,data=mod06l,cex=.5,pch=16)
228

    
229
round(cor(mod06l[,c("ppt","dem","cer","cer20","cld","cot")]),2)
230

    
231

    
232
### draw some plots
233
gq=function(x,n=10,cut=F) {
234
  if(!cut) return(unique(quantile(x,seq(0,1,len=n+1),na.rm=T)))
235
  if(cut)  return(cut(x,unique(quantile(x,seq(0,1,len=n+1),na.rm=T))))
236
}
237

    
238
### add some additional variables
239
mod06s$month=factor(mod06s$month,labels=format(as.Date(paste("2000",1:12,"15",sep="-")),"%b"))
240
mod06s$lppt=log(mod06s$ppt)
241
mod06s$glon=cut(mod06s$lon,gq(mod06s$lon,n=5),include.lowest=T,ordered=T)#gq(mod06s$lon,n=3))
242
mod06s$glon2=cut(mod06s$lon,breaks=c(-125,-122,-115),labels=c("Coastal","Inland"),include.lowest=T,ordered=T)#gq(mod06s$lon,n=3))
243
mod06s$gelev=cut(mod06s$elev,breaks=gq(mod06s$elev,n=3),labels=c("Low","Mid","High"),include.lowest=T,ordered=T)
244
mod06s$gbin=factor(paste(mod06s$gelev,mod06s$glon2,sep="_"),levels=c("Low_Coastal","Mid_Coastal","High_Coastal","Low_Inland","Mid_Inland","High_Inland"),ordered=T)
245
mod06s$LWP_mean=(2/3)*mod06s$CER_mean*mod06s$COT_mean
246

    
247
## melt it
248
mod06sl=melt(mod06s[,!grepl("lppt",colnames(mod06s))],id.vars=c("id","lon","lat","elev","month","ppt","glon","glon2","gelev","gbin"))
249
levels(mod06sl$variable)=c("Effective Radius (um)","Very Cloudy Days (%)","Cloudy Days (%)","Optical Thickness (%)","Liquid Water Path")
250

    
251
###################################################################
252
###################################################################
253

    
254
bgyr=colorRampPalette(c("blue","green","yellow","red","purple"))
255

    
256
X11.options(type="cairo")
257
pdf("output/MOD06_summary.pdf",width=11,height=8.5)
258

    
259
# % cloudy maps
260
title="Cloudiness (% cloudy days) "
261
at=unique(quantile(as.matrix(cld),seq(0,1,len=100),na.rm=T))
262
p=levelplot(cld,xlab.top=title,at=at,col.regions=bgyr(length(at)))#+layer(sp.lines(roil, lwd=1.2, col='black'))
263
print(p)
264
#bwplot(cer,main=title,ylab="Cloud Effective Radius (microns)")
265

    
266
# CER maps
267
title="Cloud Effective Radius (microns)"
268
at=quantile(as.matrix(cer),seq(0,1,len=100),na.rm=T)
269
p=levelplot(cer,xlab.top=title,at=at,col.regions=bgyr(length(at)))#+layer(sp.lines(roil, lwd=1.2, col='black'))
270
print(p)
271
#bwplot(cer,main=title,ylab="Cloud Effective Radius (microns)")
272

    
273
# CER20 maps
274
title="% Days with Cloud Effective Radius > 20 microns"
275
at=unique(quantile(as.matrix(cer20),seq(0,1,len=100),na.rm=T))
276
p=levelplot(cer20,xlab.top=title,at=at,col.regions=bgyr(length(at)))#+layer(sp.lines(roil, lwd=1.2, col='black'))
277
print(p)
278
#bwplot(cer,main=title,ylab="Cloud Effective Radius (microns)")
279

    
280
# COT maps
281
title="Cloud Optical Thickness (%)"
282
at=quantile(as.matrix(cot),seq(0,1,len=100),na.rm=T)
283
p=levelplot(cot,xlab.top=title,at=at,col.regions=bgyr(length(at)))#+layer(sp.lines(roil, lwd=0.8, col='black'))
284
print(p)
285
#bwplot(cot,xlab.top=title,ylab="Cloud Optical Thickness (%)")
286
dev.off()
287

    
288

    
289

    
290

    
291

    
292

    
293
### Calculate the slope of each line
294
mod06s.sl=dapply(mod06s,list(id=mod06s$id),function(x){
295
  lm1=lm(log(x$ppt)~x$CER_mean,)
296
  data.frame(lat=x$lat[1],lon=x$lon[1],elev=x$elev[1],intcpt=coefficients(lm1)[1],cer=coefficients(lm1)[2],r2=summary(lm1)$r.squared)
297
})
298
mod06s.sl$cex=gq(mod06s.sl$r2,n=5,cut=T)
299
mod06s.sl$cer.s=gq(mod06s.sl$cer,n=5,cut=T)
300

    
301
###  and plot it on a map
302
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,
303
       main="Slopes of linear regressions {log(ppt)~CloudEffectiveRadius}")+
304
  layer(sp.lines(roi_geo, lwd=1.2, col='black'))
305

    
306
### look for relationships with longitude
307
xyplot(cer~lon,group=cut(mod06s.sl$elev,gq(mod06s.sl$elev,n=5)),data=mod06s.sl,
308
       par.settings = list(superpose.symbol = list(col=bgyr(5),pch=16,cex=1)),auto.key=list(space="right",title="Station Elevation"),
309
       ylab="Slope of lm(ppt~EffectiveRadius)",xlab="Longitude",main="Precipitation~Effective Radius relationship by latitude")
310

    
311

    
312
############################################################
313
### simple regression to get spatial residuals
314
m="01"
315
mod06s2=mod06s#[mod06s$month==m,]
316

    
317
lm1=lm(log(ppt)~CER_mean*month*lon,data=mod06s2); summary(lm1)
318
mod06s2$pred=exp(predict(lm1,mod06s2))
319
mod06s2$resid=mod06s2$pred-mod06s2$ppt
320
mod06s2$residg=gq(mod06s2$resid,n=5,cut=T)
321
mod06s2$presid=mod06s2$resid/mod06s2$ppt
322

    
323
for(l in c(F,T)){
324
## all months
325
  xyplot(pred~ppt,groups=gelev,data=mod06s2,
326
       par.settings = list(superpose.symbol = list(col=bgyr(3),pch=16,cex=.75)),auto.key=list(space="right",title="Station Elevation"),
327
       scales=list(log=l),
328
       ylab="Predicted Mean Monthly Precipitation (mm)",xlab="Observed Mean Monthly Precipitation (mm)",main="Predicted vs. Observed for Simple Model",
329
       sub="Red line is y=x")+
330
  layer(panel.abline(0,1,col="red"))
331

    
332
## month by month
333
  print(xyplot(pred~ppt|month,groups=gelev,data=mod06s2,
334
       par.settings = list(superpose.symbol = list(col=bgyr(3),pch=16,cex=.75)),auto.key=list(space="right",title="Station Elevation"),
335
       scales=list(log=l),
336
       ylab="Predicted Mean Monthly Precipitation (mm)",xlab="Observed Mean Monthly Precipitation (mm)",main="Predicted vs. Observed for Simple Model",
337
       sub="Red line is y=x")+
338
  layer(panel.abline(0,1,col="red"))
339
)}
340

    
341
## residuals by month
342
xyplot(lat~lon|month,group=residg,data=mod06s2,
343
       par.settings = list(superpose.symbol = list(pch =16, col=bgyr(5),cex=.5)),
344
       auto.key=list(space="right",title="Residuals"),
345
       main="Spatial plot of monthly residuals")+
346
    layer(sp.lines(roi_geo, lwd=1.2, col='black'))
347

    
348

    
349
dev.off()
350

    
351

    
352

    
353

    
354

    
355

    
356

    
357

    
358

    
359

    
360
load("data/modis/pointsummary.Rdata")
361

    
362

    
363
dsl=melt(ds,id.vars=c("id","date","ppt","lon","lat"),measure.vars=  c("Cloud_Water_Path","Cloud_Effective_Radius","Cloud_Optical_Thickness"))
364

    
365
dsl=dsl[!is.nan(dsl$value),]
366

    
367

    
368

    
369

    
370
####
371
## mean annual precip
372
dp=d[d$variable=="ppt",]
373
dp$year=format(dp$date,"%Y")
374
dm=tapply(dp$value,list(id=dp$id,year=dp$year),sum,na.rm=T)
375
dms=apply(dm,1,mean,na.rm=T)
376
dms=data.frame(id=names(dms),ppt=dms/10)
377

    
378
dslm=tapply(dsl$value,list(id=dsl$id,variable=dsl$variable),mean,na.rm=T)
379
dslm=data.frame(id=rownames(dslm),dslm)
380

    
381
dms=merge(dms,dslm)
382
dmsl=melt(dms,id.vars=c("id","ppt"))
383

    
384
summary(lm(ppt~Cloud_Effective_Radius,data=dms))
385
summary(lm(ppt~Cloud_Water_Path,data=dms))
386
summary(lm(ppt~Cloud_Optical_Thickness,data=dms))
387
summary(lm(ppt~Cloud_Effective_Radius+Cloud_Water_Path+Cloud_Optical_Thickness,data=dms))
388

    
389

    
390
#### draw some plots
391
#pdf("output/MOD06_summary.pdf",width=11,height=8.5)
392
png("output/MOD06_summary_%d.png",width=1024,height=780)
393

    
394
 ## daily data
395
xyplot(value~ppt/10|variable,data=dsl,
396
       scales=list(relation="free"),type=c("p","r"),
397
       pch=16,cex=.5,layout=c(3,1))
398

    
399

    
400
densityplot(~value|variable,groups=cut(dsl$ppt,c(0,50,100,500)),data=dsl,auto.key=T,
401
            scales=list(relation="free"),plot.points=F)
402

    
403
## annual means
404

    
405
xyplot(value~ppt|variable,data=dmsl,
406
       scales=list(relation="free"),type=c("p","r"),pch=16,cex=0.5,layout=c(3,1),
407
       xlab="Mean Annual Precipitation (mm)",ylab="Mean value")
408

    
409
densityplot(~value|variable,groups=cut(dsl$ppt,c(0,50,100,500)),data=dmsl,auto.key=T,
410
            scales=list(relation="free"),plot.points=F)
411

    
412

    
413
## plot some swaths
414

    
415
nc1=raster(fs$path[3],varname="Cloud_Effective_Radius")
416
nc2=raster(fs$path[4],varname="Cloud_Effective_Radius")
417
nc3=raster(fs$path[5],varname="Cloud_Effective_Radius")
418

    
419
nc1[nc1<=0]=NA
420
nc2[nc2<=0]=NA
421
nc3[nc3<=0]=NA
422

    
423
plot(roi)
424
plot(nc3)
425

    
426
plot(nc1,add=T)
427
plot(nc2,add=T)
428

    
429

    
430
dev.off()
431

    
432

    
(13-13/19)