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(raster)
|
9
|
library(rgdal)
|
10
|
## register parallel processing
|
11
|
registerDoMC(20)
|
12
|
|
13
|
|
14
|
## available here http://cdiac.ornl.gov/epubs/ndp/ndp026d/ndp026d.html
|
15
|
|
16
|
|
17
|
## Get station locations
|
18
|
system("wget -N -nd http://cdiac.ornl.gov/ftp/ndp026d/cat01/01_STID -P data/")
|
19
|
st=read.table("data/01_STID",skip=1)
|
20
|
colnames(st)=c("StaID","LAT","LON","ELEV","ny1","fy1","ly1","ny7","fy7","ly7","SDC","b5c")
|
21
|
st$lat=st$LAT/100
|
22
|
st$lon=st$LON/100
|
23
|
st$lon[st$lon>180]=st$lon[st$lon>180]-360
|
24
|
|
25
|
## download data
|
26
|
system("wget -N -nd ftp://cdiac.ornl.gov/pub/ndp026d/cat67_78/* -A '.tc.Z' -P data/")
|
27
|
system("gunzip data/*.Z")
|
28
|
|
29
|
## get monthly mean cloud amount MMCF
|
30
|
#system("wget -N -nd ftp://cdiac.ornl.gov/pub/ndp026d/cat08_09/* -A '.tc.Z' -P data/")
|
31
|
#system("gunzip data/*.Z")
|
32
|
#f121=c(6,6,6,7,6,7,6,2) #format 121
|
33
|
#c121=c("StaID","NobD","AvgDy","NobN","AvgNt","NobDN","AvgDN","Acode")
|
34
|
#cld=do.call(rbind.data.frame,lapply(sprintf("%02d",1:12),function(m) {
|
35
|
# d=read.fwf(list.files("data",pattern=paste("MMCA.",m,".tc",sep=""),full=T),skip=1,widths=f162)
|
36
|
# colnames(d)=c121
|
37
|
# d$month=as.numeric(m)
|
38
|
# return(d)}
|
39
|
# ))
|
40
|
|
41
|
## define FWF widths
|
42
|
f162=c(5,5,4,7,7,7,4) #format 162
|
43
|
c162=c("StaID","YR","Nobs","Amt","Fq","AWP","NC")
|
44
|
|
45
|
## use monthly timeseries
|
46
|
cld=do.call(rbind.data.frame,mclapply(sprintf("%02d",1:12),function(m) {
|
47
|
d=read.fwf(list.files("data",pattern=paste("MNYDC.",m,".tc",sep=""),full=T),skip=1,widths=f162)
|
48
|
colnames(d)=c162
|
49
|
d$month=as.numeric(m)
|
50
|
print(m)
|
51
|
return(d)}
|
52
|
))
|
53
|
|
54
|
## add lat/lon
|
55
|
cld[,c("lat","lon")]=st[match(cld$StaID,st$StaID),c("lat","lon")]
|
56
|
|
57
|
## drop missing values
|
58
|
cld$Amt[cld$Amt<0]=NA
|
59
|
cld$Fq[cld$Fq<0]=NA
|
60
|
cld$AWP[cld$AWP<0]=NA
|
61
|
cld$NC[cld$NC<0]=NA
|
62
|
cld=cld[cld$Nobs>0,]
|
63
|
|
64
|
## calculate means and sds
|
65
|
cldm=do.call(rbind.data.frame,by(cld,list(month=as.factor(cld$month),StaID=as.factor(cld$StaID)),function(x){
|
66
|
data.frame(
|
67
|
month=x$month[1],
|
68
|
StaID=x$StaID[1],
|
69
|
cld=mean(x$Amt[x$Nobs>10]/100,na.rm=T),
|
70
|
cldsd=sd(x$Amt[x$Nobs>10]/100,na.rm=T))}))
|
71
|
cldm[,c("lat","lon")]=st[match(cldm$StaID,st$StaID),c("lat","lon")]
|
72
|
|
73
|
## means by year
|
74
|
cldy=do.call(rbind.data.frame,by(cld,list(year=as.factor(cld$YR),StaID=as.factor(cld$StaID)),function(x){
|
75
|
data.frame(
|
76
|
year=x$YR[1],
|
77
|
StaID=x$StaID[1],
|
78
|
cld=mean(x$Amt[x$Nobs>10]/100,na.rm=T),
|
79
|
cldsd=sd(x$Amt[x$Nobs>10]/100,na.rm=T))}))
|
80
|
cldy[,c("lat","lon")]=st[match(cldy$StaID,st$StaID),c("lat","lon")]
|
81
|
|
82
|
|
83
|
#cldm=foreach(m=unique(cld$month),.combine='rbind')%:%
|
84
|
# foreach(s=unique(cld$StaID),.combine="rbind") %dopar% {
|
85
|
# x=cld[cld$month==m&cld$StaID==s,]
|
86
|
# data.frame(
|
87
|
# month=x$month[1],
|
88
|
# StaID=x$StaID[1],
|
89
|
# Amt=mean(x$Amt[x$Nobs>10],na.rm=T)/100)}
|
90
|
|
91
|
|
92
|
## write out the tables
|
93
|
write.csv(cldy,file="cldy.csv")
|
94
|
write.csv(cldm,file="cldm.csv")
|
95
|
|
96
|
|
97
|
##################
|
98
|
###
|
99
|
cldm=read.csv("cldm.csv")
|
100
|
cldy=read.csv("cldy.csv")
|
101
|
|
102
|
|
103
|
##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
|
#### Evaluate MOD35 Cloud data
|
109
|
mod35=brick("../modis/mod35/MOD35_h11v08.nc",varname="CLD01")
|
110
|
mod35sd=brick("../modis/mod35/MOD35_h11v08.nc",varname="CLD_sd")
|
111
|
projection(mod35)="+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs"
|
112
|
|
113
|
|
114
|
### use data from google earth engine
|
115
|
mod35=brick("../modis/mod09/global_2009/")
|
116
|
mod09=raster("../modis/mod09/global_2009/MOD09_2009.tif")
|
117
|
|
118
|
n=100
|
119
|
at=seq(0,100,length=n)
|
120
|
colr=colorRampPalette(c("black","green","red"))
|
121
|
cols=colr(n)
|
122
|
|
123
|
levelplot(mod09,col.regions=cols,at=at)
|
124
|
|
125
|
|
126
|
cldms=spTransform(cldms,CRS(projection(mod35)))
|
127
|
|
128
|
mod35v=foreach(m=unique(cldm$month),.combine="rbind") %do% {
|
129
|
dr=subset(mod35,subset=m);projection(dr)=projection(mod35)
|
130
|
dr2=subset(mod35sd,subset=m);projection(dr2)=projection(mod35)
|
131
|
ds=cldms[cldms$month==m,]
|
132
|
ds$mod35=unlist(extract(dr,ds,buffer=10,fun=mean,na.rm=T))
|
133
|
# ds$mod35sd=extract(dr2,ds,buffer=10)
|
134
|
print(m)
|
135
|
return(ds@data[!is.na(ds$mod35),])}
|
136
|
|
137
|
## month factors
|
138
|
cldm$month2=factor(cldm$month,labels=month.name)
|
139
|
## add a color key
|
140
|
breaks=seq(0,100,by=25)
|
141
|
cldm$cut=cut(cldm$cld,breaks)
|
142
|
cp=colorRampPalette(c("blue","orange","red"))
|
143
|
cols=cp(length(at))
|
144
|
|
145
|
## read in global coasts for nice plotting
|
146
|
library(maptools)
|
147
|
|
148
|
data(wrld_simpl)
|
149
|
coast <- unionSpatialPolygons(wrld_simpl, rep("land",nrow(wrld_simpl)), threshold=5)
|
150
|
coast=as(coast,"SpatialLines")
|
151
|
#coast=spTransform(coast,CRS(projection(mod35)))
|
152
|
|
153
|
|
154
|
## write a pdf
|
155
|
#dir.create("output")
|
156
|
pdf("output/NDP026d.pdf",width=11,height=8.5)
|
157
|
|
158
|
## map of stations
|
159
|
xyplot(lat~lon,data=st,pch=16,cex=.5,col="black",auto.key=T,
|
160
|
main="NDP-026D Cloud Climatology Stations",ylab="Latitude",xlab="Longitude")+
|
161
|
layer(sp.lines(coast,col="grey"),under=T)
|
162
|
|
163
|
xyplot(lat~lon|month2,groups=cut,data=cldm,pch=".",cex=.2,auto.key=T,
|
164
|
main="Mean Monthly Cloud Coverage",ylab="Latitude",xlab="Longitude",
|
165
|
par.settings = list(superpose.symbol= list(pch=16,col=c("blue","green","yellow","red"))))+
|
166
|
layer(sp.lines(coast,col="grey"),under=T)
|
167
|
|
168
|
|
169
|
## Validation
|
170
|
m=10
|
171
|
zlim=c(40,100)
|
172
|
dr=subset(mod35,subset=m);projection(dr)=projection(mod35)
|
173
|
ds=cldms[cldms$month==m,]
|
174
|
plot(dr,col=cp(100),zlim=zlim,main="Comparison of MOD35 Cloud Frequency and NDP-026D Station Cloud Climatologies",
|
175
|
ylab="Northing (m)",xlab="Easting (m)",sub="MOD35 is proportion of cloudy days, while NDP-026D is Mean Cloud Coverage")
|
176
|
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))))
|
177
|
#legend("topright",legend=seq(zlim[1],zlim[2],len=5),pch=16,col=cp(length(breaks)))
|
178
|
|
179
|
|
180
|
xyplot(mod35~cld,data=mod35v,subscripts=T,auto.key=T,panel=function(x,y,subscripts){
|
181
|
td=mod35v[subscripts,]
|
182
|
# panel.segments(x-td$cldsd[subscripts],y,x+td$cldsd[subscripts],y,subscripts=subscripts)
|
183
|
panel.xyplot(x,y,subscripts=subscripts,type=c("p","smooth"),pch=16,col="black")
|
184
|
# panel.segments(x-td$cldsd[subscripts],y,x+td$cldsd[subscripts],y,subscripts=subscripts)
|
185
|
},ylab="MOD35 Proportion Cloudy Days",xlab="NDP-026D Mean Monthly Cloud Amount",
|
186
|
main="Comparison of MOD35 Cloud Mask and Station Cloud Climatologies")
|
187
|
|
188
|
#xyplot(mod35~cld|month,data=mod35v,subscripts=T,auto.key=T,panel=function(x,y,subscripts){
|
189
|
# td=mod35v[subscripts,]
|
190
|
# panel.segments(x-td$cldsd[subscripts],y,x+td$cldsd[subscripts],y,subscripts=subscripts)
|
191
|
# panel.xyplot(x,y,subscripts=subscripts,type=c("p","smooth"),pch=16,col="black")
|
192
|
# panel.segments(x-td$cldsd[subscripts],y,x+td$cldsd[subscripts],y,subscripts=subscripts)
|
193
|
# },ylab="MOD35 Proportion Cloudy Days",xlab="NDP-026D Mean Monthly Cloud Amount",
|
194
|
# main="Comparison of MOD35 Cloud Mask and Station Cloud Climatologies")
|
195
|
|
196
|
|
197
|
dev.off()
|
198
|
|
199
|
graphics.off()
|