Project

General

Profile

Download (8.96 KB) Statistics
| Branch: | Revision:
1
#! /bin/R
2
### Script to download and process the NDP-026D station cloud dataset
3
setwd("~/acrobates/adamw/projects/interp/data/NDP026D")
4

    
5
library(multicore)
6
library(latticeExtra)
7
library(doMC)
8
library(rasterVis)
9
library(rgdal)
10
## register parallel processing
11
registerDoMC(10)
12

    
13

    
14
## available here http://cdiac.ornl.gov/epubs/ndp/ndp026d/ndp026d.html
15

    
16
## Get station locations
17
system("wget -N -nd http://cdiac.ornl.gov/ftp/ndp026d/cat01/01_STID -P data/")
18
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
## download data
25
system("wget -N -nd ftp://cdiac.ornl.gov/pub/ndp026d/cat67_78/* -A '.tc.Z' -P data/")
26
system("gunzip data/*.Z")
27

    
28
## 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
#f121=c(6,6,6,7,6,7,6,2) #format 121
32
#c121=c("StaID","NobD","AvgDy","NobN","AvgNt","NobDN","AvgDN","Acode")
33
#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
c162=c("StaID","YR","Nobs","Amt","Fq","AWP","NC")
43

    
44
## use monthly timeseries
45
cld=do.call(rbind.data.frame,mclapply(sprintf("%02d",1:12),function(m) {
46
  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
  print(m)
50
  return(d)}
51
  ))
52

    
53
## add lat/lon
54
cld[,c("lat","lon")]=st[match(cld$StaID,st$StaID),c("lat","lon")]
55

    
56
## drop missing values
57
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
cld=cld[cld$Nobs>0,]
62

    
63
## calculate means and sds
64
cldm=do.call(rbind.data.frame,by(cld,list(month=as.factor(cld$month),StaID=as.factor(cld$StaID)),function(x){
65
  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
## 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
#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

    
91
## write out the tables
92
write.csv(cldy,file="cldy.csv")
93
write.csv(cldm,file="cldm.csv")
94

    
95
#########################################################################
96
##################
97
###
98
cldm=read.csv("cldm.csv")
99
cldy=read.csv("cldy.csv")
100

    
101

    
102
##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
##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
#### Evaluate MOD35 Cloud data
113
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

    
116

    
117
### use data from google earth engine
118
mod35c5=raster("../modis/mod09/global_2009/MOD35_2009.tif")
119
mod09=raster("../modis/mod09/global_2009/MOD09_2009.tif")
120

    
121
## LULC
122
#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
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
n=100
133
at=seq(0,100,length=n)
134
colr=colorRampPalette(c("black","green","red"))
135
cols=colr(n)
136

    
137
#dif=mod35-mod09
138
#bwplot(dif~as.factor(lulc))
139

    
140
#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

    
143
#cldys=spTransform(cldys,CRS(projection(mod35)))
144

    
145
#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
#  ds$mod35sd=extract(dr2,ds,buffer=10)
151
#  print(m)
152
#  return(ds@data[!is.na(ds$mod35),])}
153

    
154
y=2009
155
d=cldys[cldys$year==y,]
156

    
157
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
d$mod09_10=unlist(extract(mod09,d,buffer=10000,fun=mean,na.rm=T))
160
#d$dif=d$mod35_10-d$mod09_10
161
#d$dif2=d$mod35_10-d$cld
162

    
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
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
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
## 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()
(33-33/45)