Project

General

Profile

Download (8.44 KB) Statistics
| Branch: | Revision:
1 4ef959c2 Adam M. Wilson
#! /bin/R
2
### Script to download and process the NDP-026D station cloud dataset
3 710f7456 Adam M. Wilson
setwd("~/acrobates/adamw/projects/interp/data/NDP026D")
4
5
library(multicore)
6
library(latticeExtra)
7
library(doMC)
8 ddd9a810 Adam M. Wilson
library(rasterVis)
9 710f7456 Adam M. Wilson
library(rgdal)
10 9a19743f Adam M. Wilson
library(reshape)
11
library(hexbin)
12 710f7456 Adam M. Wilson
## register parallel processing
13 2bca6aaa Adam M. Wilson
registerDoMC(10)
14 710f7456 Adam M. Wilson
15 4ef959c2 Adam M. Wilson
16
## available here http://cdiac.ornl.gov/epubs/ndp/ndp026d/ndp026d.html
17
18
## Get station locations
19 a6d44f2d Adam M. Wilson
system("wget -N -nd http://cdiac.ornl.gov/ftp/ndp026d/cat01/01_STID -P data/")
20 4ef959c2 Adam M. Wilson
st=read.table("data/01_STID",skip=1)
21
colnames(st)=c("StaID","LAT","LON","ELEV","ny1","fy1","ly1","ny7","fy7","ly7","SDC","b5c")
22
st$lat=st$LAT/100
23
st$lon=st$LON/100
24
st$lon[st$lon>180]=st$lon[st$lon>180]-360
25 9a19743f Adam M. Wilson
st=st[,c("StaID","ELEV","lat","lon")]
26
colnames(st)=c("id","elev","lat","lon")
27
write.csv(st,"stations.csv",row.names=F)
28 4ef959c2 Adam M. Wilson
29 710f7456 Adam M. Wilson
## download data
30 4ef959c2 Adam M. Wilson
system("wget -N -nd ftp://cdiac.ornl.gov/pub/ndp026d/cat67_78/* -A '.tc.Z' -P data/")
31
32 9a19743f Adam M. Wilson
system("gunzip data/*.Z")
33 710f7456 Adam M. Wilson
34
## define FWF widths
35
f162=c(5,5,4,7,7,7,4) #format 162
36 4ef959c2 Adam M. Wilson
c162=c("StaID","YR","Nobs","Amt","Fq","AWP","NC")
37
38 710f7456 Adam M. Wilson
## use monthly timeseries
39
cld=do.call(rbind.data.frame,mclapply(sprintf("%02d",1:12),function(m) {
40 4ef959c2 Adam M. Wilson
  d=read.fwf(list.files("data",pattern=paste("MNYDC.",m,".tc",sep=""),full=T),skip=1,widths=f162)
41
  colnames(d)=c162
42
  d$month=as.numeric(m)
43 710f7456 Adam M. Wilson
  print(m)
44 4ef959c2 Adam M. Wilson
  return(d)}
45
  ))
46
47 710f7456 Adam M. Wilson
## add lat/lon
48
cld[,c("lat","lon")]=st[match(cld$StaID,st$StaID),c("lat","lon")]
49 4ef959c2 Adam M. Wilson
50 a6d44f2d Adam M. Wilson
## drop missing values
51 4ef959c2 Adam M. Wilson
cld$Amt[cld$Amt<0]=NA
52
cld$Fq[cld$Fq<0]=NA
53
cld$AWP[cld$AWP<0]=NA
54
cld$NC[cld$NC<0]=NA
55 710f7456 Adam M. Wilson
cld=cld[cld$Nobs>0,]
56 4ef959c2 Adam M. Wilson
57 710f7456 Adam M. Wilson
## calculate means and sds
58 a6d44f2d Adam M. Wilson
cldm=do.call(rbind.data.frame,by(cld,list(month=as.factor(cld$month),StaID=as.factor(cld$StaID)),function(x){
59 710f7456 Adam M. Wilson
  data.frame(
60
             month=x$month[1],
61
             StaID=x$StaID[1],
62
             cld=mean(x$Amt[x$Nobs>10]/100,na.rm=T),
63
             cldsd=sd(x$Amt[x$Nobs>10]/100,na.rm=T))}))
64
cldm[,c("lat","lon")]=st[match(cldm$StaID,st$StaID),c("lat","lon")]
65
66 99b3508d Adam M. Wilson
## means by year
67
cldy=do.call(rbind.data.frame,by(cld,list(year=as.factor(cld$YR),StaID=as.factor(cld$StaID)),function(x){
68
  data.frame(
69
             year=x$YR[1],
70
             StaID=x$StaID[1],
71
             cld=mean(x$Amt[x$Nobs>10]/100,na.rm=T),
72
             cldsd=sd(x$Amt[x$Nobs>10]/100,na.rm=T))}))
73
cldy[,c("lat","lon")]=st[match(cldy$StaID,st$StaID),c("lat","lon")]
74
75 9a19743f Adam M. Wilson
## add the MOD09 data to cld
76
#### Evaluate MOD35 Cloud data
77
mod09=brick("~/acrobates/adamw/projects/cloud/data/mod09.nc")
78 99b3508d Adam M. Wilson
79 9a19743f Adam M. Wilson
## overlay the data with 5km radius buffer
80
mod09st=extract(mod09,st,buffer=5000,fun=mean,na.rm=T,df=T)
81
mod09st$id=st$id
82
mod09stl=melt(mod09st[,-1],id.vars="id")
83
mod09stl[,c("year","month")]=do.call(rbind,strsplit(sub("X","",mod09stl$variable),"[.]"))[,1:2]
84
## add it to cld
85
cld$mod09=mod09stl$value[match(paste(cld$StaID,cld$YR,cld$month),paste(mod09stl$id,mod09stl$year,as.numeric(mod09stl$month)))]
86 a6d44f2d Adam M. Wilson
87 99b3508d Adam M. Wilson
## write out the tables
88 9a19743f Adam M. Wilson
write.csv(cld,file="cld.csv",row.names=F)
89 99b3508d Adam M. Wilson
write.csv(cldy,file="cldy.csv")
90 a6d44f2d Adam M. Wilson
write.csv(cldm,file="cldm.csv")
91
92 555815c9 Adam M. Wilson
#########################################################################
93 a6d44f2d Adam M. Wilson
##################
94
###
95 9a19743f Adam M. Wilson
cld=read.csv("cld.csv")
96 a6d44f2d Adam M. Wilson
cldm=read.csv("cldm.csv")
97 99b3508d Adam M. Wilson
cldy=read.csv("cldy.csv")
98 9a19743f Adam M. Wilson
st=read.csv("stations.csv")
99 a6d44f2d Adam M. Wilson
100 9a19743f Adam M. Wilson
coordinates(st)=c("lon","lat")
101
projection(st)=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
102 4ef959c2 Adam M. Wilson
103 710f7456 Adam M. Wilson
##make spatial object
104
cldms=cldm
105
coordinates(cldms)=c("lon","lat")
106
projection(cldms)=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
107
108 ddd9a810 Adam M. Wilson
##make spatial object
109
cldys=cldy
110
coordinates(cldys)=c("lon","lat")
111
projection(cldys)=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
112
113 710f7456 Adam M. Wilson
#### Evaluate MOD35 Cloud data
114 9a19743f Adam M. Wilson
mod09=brick("~/acrobates/adamw/projects/cloud/data/mod09.nc")
115 99b3508d Adam M. Wilson
116
117 ddd9a810 Adam M. Wilson
## LULC
118 2bca6aaa Adam M. Wilson
#system(paste("gdalwarp -r near -co \"COMPRESS=LZW\" -tr ",paste(res(mod09),collapse=" ",sep=""),
119
#             "-tap -multi -t_srs \"",   projection(mod09),"\" /mnt/data/jetzlab/Data/environ/global/landcover/MODIS/MCD12Q1_IGBP_2005_v51.tif ../modis/mod12/MCD12Q1_IGBP_2005_v51.tif"))
120 ddd9a810 Adam M. Wilson
lulc=raster("../modis/mod12/MCD12Q1_IGBP_2005_v51.tif")
121 9a19743f Adam M. Wilson
#lulc=ratify(lulc)
122 ddd9a810 Adam M. Wilson
require(plotKML); data(worldgrids_pal)  #load IGBP palette
123
IGBP=data.frame(ID=0:16,col=worldgrids_pal$IGBP[-c(18,19)],stringsAsFactors=F)
124
IGBP$class=rownames(IGBP);rownames(IGBP)=1:nrow(IGBP)
125
levels(lulc)=list(IGBP)
126 9a19743f Adam M. Wilson
#lulc=crop(lulc,mod09)
127 ddd9a810 Adam M. Wilson
128 99b3508d Adam M. Wilson
n=100
129
at=seq(0,100,length=n)
130
colr=colorRampPalette(c("black","green","red"))
131
cols=colr(n)
132
133
134 9a19743f Adam M. Wilson
hexbinplot(Amt/100~mod09,data=cld[cld$Nobs>100,])+
135
  layer(panel.abline(lm(y~x),col="blue"))+
136
  layer(panel.abline(0,1,col="red"))
137 a6d44f2d Adam M. Wilson
138 9a19743f Adam M. Wilson
xyplot(Amt/100~mod09,grpups="month",data=cld[cld$Nobs>75,],cex=.2,pch=16)+
139
  layer(panel.abline(lm(y~x),col="blue"))+
140
#  layer(panel.lines(x,predict(lm(y~x),type="prediction")))+
141
  layer(panel.abline(0,1,col="red"))
142 710f7456 Adam M. Wilson
143 9a19743f Adam M. Wilson
xyplot(Amt/100~mod09|month,data=cld[cld$Nobs>75,],cex=.2,pch=16)+
144
  layer(panel.abline(lm(y~x),col="blue"))+
145
#  layer(panel.lines(x,predict(lm(y~x),type="prediction")))+
146
  layer(panel.abline(0,1,col="red"))
147 710f7456 Adam M. Wilson
148 ddd9a810 Adam M. Wilson
149
d$lulc=unlist(extract(lulc,d))
150
d$lulc_10=unlist(extract(lulc,d,buffer=10000,fun=mode,na.rm=T))
151
d$lulc=factor(d$lulc,labels=IGBP$class)
152
153
save(d,file="annualsummary.Rdata")
154
155 9a19743f Adam M. Wilson
156
157
load("annualsummary.Rdata")
158
159 ddd9a810 Adam M. Wilson
## quick model to explore fit
160 9a19743f Adam M. Wilson
xyplot(cld~mod35c5_10,groups=lulc,data=d@data)
161
summary(lm(cld~mod35c5_10+as.factor(lulc),data=d@data))
162
summary(lm(Amt~mod09,data=cld))
163 ddd9a810 Adam M. Wilson
summary(lm(cld~mod09_10+as.factor(lulc),data=d))
164
summary(lm(cld~mod09_10+as.factor(lulc),data=d))
165
166
### exploratory plots
167
xyplot(cld~mod09_10,groups=lulc,data=d@data,pch=16,cex=.5)+layer(panel.abline(0,1,col="red"))
168 2bca6aaa Adam M. Wilson
xyplot(cld~mod09_10+mod35c5_10|as.factor(lulc),data=d@data,type=c("p","r"),pch=16,cex=.25,auto.key=T)+layer(panel.abline(0,1,col="green"))
169 ddd9a810 Adam M. Wilson
xyplot(cld~mod35_10|as.factor(lulc),data=d@data,pch=16,cex=.5)+layer(panel.abline(0,1,col="red"))
170
xyplot(mod35_10~mod09_10|as.factor(lulc),data=d@data,pch=16,cex=.5)+layer(panel.abline(0,1,col="red"))
171
172
densityplot(stack(mod35,mod09))
173
boxplot(mod35,lulc)
174
175
bwplot(mod09~mod35|cut(y,5),data=stack(mod09,mod35))
176
177 710f7456 Adam M. Wilson
## month factors
178
cldm$month2=factor(cldm$month,labels=month.name)
179
## add a color key
180
breaks=seq(0,100,by=25)
181
cldm$cut=cut(cldm$cld,breaks)
182
cp=colorRampPalette(c("blue","orange","red"))
183
cols=cp(length(at))
184
185
## read in global coasts for nice plotting
186
library(maptools)
187
188
data(wrld_simpl)
189
coast <- unionSpatialPolygons(wrld_simpl, rep("land",nrow(wrld_simpl)), threshold=5)
190
coast=as(coast,"SpatialLines")
191
#coast=spTransform(coast,CRS(projection(mod35)))
192
193
194
## write a pdf
195
#dir.create("output")
196
pdf("output/NDP026d.pdf",width=11,height=8.5)
197
198
## map of stations
199
 xyplot(lat~lon,data=st,pch=16,cex=.5,col="black",auto.key=T,
200
       main="NDP-026D Cloud Climatology Stations",ylab="Latitude",xlab="Longitude")+
201
  layer(sp.lines(coast,col="grey"),under=T)
202
203
xyplot(lat~lon|month2,groups=cut,data=cldm,pch=".",cex=.2,auto.key=T,
204
       main="Mean Monthly Cloud Coverage",ylab="Latitude",xlab="Longitude",
205
        par.settings = list(superpose.symbol= list(pch=16,col=c("blue","green","yellow","red"))))+
206
  layer(sp.lines(coast,col="grey"),under=T)
207
208
209
## Validation
210
m=10
211
zlim=c(40,100)
212
dr=subset(mod35,subset=m);projection(dr)=projection(mod35)
213
ds=cldms[cldms$month==m,]
214
plot(dr,col=cp(100),zlim=zlim,main="Comparison of MOD35 Cloud Frequency and NDP-026D Station Cloud Climatologies",
215
     ylab="Northing (m)",xlab="Easting (m)",sub="MOD35 is proportion of cloudy days, while NDP-026D is Mean Cloud Coverage")
216
plot(ds,add=T,pch=21,cex=3,lwd=2,fg="black",bg=as.character(cut(ds$cld,breaks=seq(zlim[1],zlim[2],len=5),labels=cp(4))))
217
#legend("topright",legend=seq(zlim[1],zlim[2],len=5),pch=16,col=cp(length(breaks)))
218
219
220
xyplot(mod35~cld,data=mod35v,subscripts=T,auto.key=T,panel=function(x,y,subscripts){
221
   td=mod35v[subscripts,]
222
#   panel.segments(x-td$cldsd[subscripts],y,x+td$cldsd[subscripts],y,subscripts=subscripts)
223
   panel.xyplot(x,y,subscripts=subscripts,type=c("p","smooth"),pch=16,col="black")
224
#   panel.segments(x-td$cldsd[subscripts],y,x+td$cldsd[subscripts],y,subscripts=subscripts)
225
 },ylab="MOD35 Proportion Cloudy Days",xlab="NDP-026D Mean Monthly Cloud Amount",
226
        main="Comparison of MOD35 Cloud Mask and Station Cloud Climatologies")
227
228
#xyplot(mod35~cld|month,data=mod35v,subscripts=T,auto.key=T,panel=function(x,y,subscripts){
229
#   td=mod35v[subscripts,]
230
#   panel.segments(x-td$cldsd[subscripts],y,x+td$cldsd[subscripts],y,subscripts=subscripts)
231
#   panel.xyplot(x,y,subscripts=subscripts,type=c("p","smooth"),pch=16,col="black")
232
#   panel.segments(x-td$cldsd[subscripts],y,x+td$cldsd[subscripts],y,subscripts=subscripts)
233
# },ylab="MOD35 Proportion Cloudy Days",xlab="NDP-026D Mean Monthly Cloud Amount",
234
#        main="Comparison of MOD35 Cloud Mask and Station Cloud Climatologies")
235
236
237
dev.off()
238
239
graphics.off()