Project

General

Profile

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