1 |
4ef959c2
|
Adam M. Wilson
|
### Script to download and process the NDP-026D station cloud dataset
|
2 |
f5ef0596
|
Adam M. Wilson
|
|
3 |
|
|
setwd("~/acrobates/adamw/projects/cloud/data/NDP026D")
|
4 |
710f7456
|
Adam M. Wilson
|
|
5 |
|
|
library(multicore)
|
6 |
|
|
library(doMC)
|
7 |
ddd9a810
|
Adam M. Wilson
|
library(rasterVis)
|
8 |
710f7456
|
Adam M. Wilson
|
library(rgdal)
|
9 |
8009a075
|
Adam M. Wilson
|
library(reshape)
|
10 |
f5ef0596
|
Adam M. Wilson
|
|
11 |
|
|
|
12 |
|
|
## Data available here http://cdiac.ornl.gov/epubs/ndp/ndp026d/ndp026d.html
|
13 |
4ef959c2
|
Adam M. Wilson
|
|
14 |
|
|
## Get station locations
|
15 |
a6d44f2d
|
Adam M. Wilson
|
system("wget -N -nd http://cdiac.ornl.gov/ftp/ndp026d/cat01/01_STID -P data/")
|
16 |
4ef959c2
|
Adam M. Wilson
|
st=read.table("data/01_STID",skip=1)
|
17 |
|
|
colnames(st)=c("StaID","LAT","LON","ELEV","ny1","fy1","ly1","ny7","fy7","ly7","SDC","b5c")
|
18 |
|
|
st$lat=st$LAT/100
|
19 |
|
|
st$lon=st$LON/100
|
20 |
|
|
st$lon[st$lon>180]=st$lon[st$lon>180]-360
|
21 |
9a19743f
|
Adam M. Wilson
|
st=st[,c("StaID","ELEV","lat","lon")]
|
22 |
|
|
colnames(st)=c("id","elev","lat","lon")
|
23 |
|
|
write.csv(st,"stations.csv",row.names=F)
|
24 |
d86b0a4a
|
Adam M. Wilson
|
coordinates(st)=c("lon","lat")
|
25 |
8009a075
|
Adam M. Wilson
|
projection(st)="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
|
26 |
1dc46eb9
|
Adam M. Wilson
|
st@data[,c("lon","lat")]=coordinates(st)
|
27 |
8009a075
|
Adam M. Wilson
|
|
28 |
710f7456
|
Adam M. Wilson
|
## download data
|
29 |
4ef959c2
|
Adam M. Wilson
|
system("wget -N -nd ftp://cdiac.ornl.gov/pub/ndp026d/cat67_78/* -A '.tc.Z' -P data/")
|
30 |
|
|
|
31 |
9a19743f
|
Adam M. Wilson
|
system("gunzip data/*.Z")
|
32 |
710f7456
|
Adam M. Wilson
|
|
33 |
|
|
## define FWF widths
|
34 |
|
|
f162=c(5,5,4,7,7,7,4) #format 162
|
35 |
4ef959c2
|
Adam M. Wilson
|
c162=c("StaID","YR","Nobs","Amt","Fq","AWP","NC")
|
36 |
|
|
|
37 |
710f7456
|
Adam M. Wilson
|
## use monthly timeseries
|
38 |
|
|
cld=do.call(rbind.data.frame,mclapply(sprintf("%02d",1:12),function(m) {
|
39 |
8009a075
|
Adam M. Wilson
|
d=read.fwf(list.files("data",pattern=paste("MNYDC.",m,".tc$",sep=""),full=T),skip=1,widths=f162)
|
40 |
4ef959c2
|
Adam M. Wilson
|
colnames(d)=c162
|
41 |
|
|
d$month=as.numeric(m)
|
42 |
710f7456
|
Adam M. Wilson
|
print(m)
|
43 |
4ef959c2
|
Adam M. Wilson
|
return(d)}
|
44 |
|
|
))
|
45 |
|
|
|
46 |
710f7456
|
Adam M. Wilson
|
## add lat/lon
|
47 |
f5ef0596
|
Adam M. Wilson
|
cld[,c("lat","lon")]=coordinates(st)[match(cld$StaID,st$id),]
|
48 |
4ef959c2
|
Adam M. Wilson
|
|
49 |
a6d44f2d
|
Adam M. Wilson
|
## drop missing values
|
50 |
d86b0a4a
|
Adam M. Wilson
|
cld=cld[,!grepl("Fq|AWP|NC",colnames(cld))]
|
51 |
4ef959c2
|
Adam M. Wilson
|
cld$Amt[cld$Amt<0]=NA
|
52 |
d86b0a4a
|
Adam M. Wilson
|
#cld$Fq[cld$Fq<0]=NA
|
53 |
|
|
#cld$AWP[cld$AWP<0]=NA
|
54 |
|
|
#cld$NC[cld$NC<0]=NA
|
55 |
|
|
#cld=cld[cld$Nobs>0,]
|
56 |
4ef959c2
|
Adam M. Wilson
|
|
57 |
1dc46eb9
|
Adam M. Wilson
|
## calculate means and sds
|
58 |
|
|
cldm=do.call(rbind.data.frame,by(cld,list(month=as.factor(cld$month),StaID=as.factor(cld$StaID)),function(x){
|
59 |
|
|
data.frame(
|
60 |
|
|
month=x$month[1],
|
61 |
|
|
StaID=x$StaID[1],
|
62 |
|
|
cld=mean(x$cld[x$Nobs>60],na.rm=T),
|
63 |
|
|
cldsd=sd(x$cld[x$Nobs>60],na.rm=T))}))
|
64 |
|
|
cldm[,c("lat","lon")]=coordinates(st)[match(cldm$StaID,st$id),c("lat","lon")]
|
65 |
|
|
|
66 |
|
|
|
67 |
d7dc526e
|
Adam M. Wilson
|
## add the MOD09 data to cld
|
68 |
|
|
#### Evaluate MOD35 Cloud data
|
69 |
8009a075
|
Adam M. Wilson
|
mod09=brick("~/acrobates/adamw/projects/cloud/data/cloud_ymonmean.nc")
|
70 |
1dc46eb9
|
Adam M. Wilson
|
mod09std=brick("~/acrobates/adamw/projects/cloud/data/cloud_ymonstd.nc")
|
71 |
d7dc526e
|
Adam M. Wilson
|
|
72 |
|
|
## overlay the data with 32km diameter (16km radius) buffer
|
73 |
|
|
## buffer size from Dybbroe, et al. (2005) doi:10.1175/JAM-2189.1.
|
74 |
|
|
buf=16000
|
75 |
1dc46eb9
|
Adam M. Wilson
|
bins=cut(st$lat,10)
|
76 |
|
|
rerun=F
|
77 |
|
|
if(rerun&file.exists("valid.csv")) file.remove("valid.csv")
|
78 |
d6ddb3e2
|
Adam M. Wilson
|
mod09sta=lapply(levels(bins),function(lb) {
|
79 |
|
|
l=which(bins==lb)
|
80 |
1dc46eb9
|
Adam M. Wilson
|
## mean
|
81 |
d6ddb3e2
|
Adam M. Wilson
|
td=extract(mod09,st[l,],buffer=buf,fun=mean,na.rm=T,df=T)
|
82 |
|
|
td$id=st$id[l]
|
83 |
1dc46eb9
|
Adam M. Wilson
|
td$type="mean"
|
84 |
|
|
## std
|
85 |
|
|
td2=extract(mod09std,st[l,],buffer=buf,fun=mean,na.rm=T,df=T)
|
86 |
|
|
td2$id=st$id[l]
|
87 |
|
|
td2$type="sd"
|
88 |
d6ddb3e2
|
Adam M. Wilson
|
print(lb)#as.vector(c(l,td[,1:4])))
|
89 |
1dc46eb9
|
Adam M. Wilson
|
write.table(rbind(td,td2),"valid.csv",append=T,col.names=F,quote=F,sep=",",row.names=F)
|
90 |
d6ddb3e2
|
Adam M. Wilson
|
td
|
91 |
|
|
})#,mc.cores=3)
|
92 |
|
|
|
93 |
f5ef0596
|
Adam M. Wilson
|
## read it back in
|
94 |
d6ddb3e2
|
Adam M. Wilson
|
mod09st=read.csv("valid.csv",header=F)[,-c(1,2)]
|
95 |
|
|
|
96 |
f5ef0596
|
Adam M. Wilson
|
colnames(mod09st)=c(names(mod09)[-1],"id")
|
97 |
1dc46eb9
|
Adam M. Wilson
|
mod09stl=melt(mod09st,id.vars=c("id","sd"))
|
98 |
d7dc526e
|
Adam M. Wilson
|
mod09stl[,c("year","month")]=do.call(rbind,strsplit(sub("X","",mod09stl$variable),"[.]"))[,1:2]
|
99 |
1dc46eb9
|
Adam M. Wilson
|
mod09stl$value[mod09stl$value<0]=NA
|
100 |
f5ef0596
|
Adam M. Wilson
|
|
101 |
d7dc526e
|
Adam M. Wilson
|
## add it to cld
|
102 |
1dc46eb9
|
Adam M. Wilson
|
cldm$mod09=mod09stl$value[match(paste(cldm$StaID,cldm$month),paste(mod09stl$id,as.numeric(mod09stl$month)))]
|
103 |
d7dc526e
|
Adam M. Wilson
|
|
104 |
|
|
|
105 |
|
|
## LULC
|
106 |
|
|
#system(paste("gdalwarp -r near -co \"COMPRESS=LZW\" -tr ",paste(res(mod09),collapse=" ",sep=""),
|
107 |
|
|
# "-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"))
|
108 |
f5ef0596
|
Adam M. Wilson
|
lulc=raster("~/acrobates/adamw/projects/interp/data/modis/mod12/MCD12Q1_IGBP_2005_v51.tif")
|
109 |
d7dc526e
|
Adam M. Wilson
|
require(plotKML); data(worldgrids_pal) #load IGBP palette
|
110 |
|
|
IGBP=data.frame(ID=0:16,col=worldgrids_pal$IGBP[-c(18,19)],stringsAsFactors=F)
|
111 |
|
|
IGBP$class=rownames(IGBP);rownames(IGBP)=1:nrow(IGBP)
|
112 |
|
|
levels(lulc)=list(IGBP)
|
113 |
f5ef0596
|
Adam M. Wilson
|
## function to get modal lulc value
|
114 |
|
|
Mode <- function(x) {
|
115 |
d86b0a4a
|
Adam M. Wilson
|
ux <- na.omit(unique(x))
|
116 |
d6ddb3e2
|
Adam M. Wilson
|
ux[which.max(tabulate(match(x, ux)))]
|
117 |
d7dc526e
|
Adam M. Wilson
|
}
|
118 |
d6ddb3e2
|
Adam M. Wilson
|
lulcst=extract(lulc,st,fun=Mode,buffer=buf,df=T)
|
119 |
d7dc526e
|
Adam M. Wilson
|
colnames(lulcst)=c("id","lulc")
|
120 |
|
|
## add it to cld
|
121 |
1dc46eb9
|
Adam M. Wilson
|
cldm$lulc=lulcst$lulc[match(cldm$StaID,lulcst$id)]
|
122 |
|
|
cldm$lulcc=IGBP$class[match(cldm$lulc,IGBP$ID)]
|
123 |
d7dc526e
|
Adam M. Wilson
|
|
124 |
|
|
## update cld column names
|
125 |
1dc46eb9
|
Adam M. Wilson
|
colnames(cldm)[grep("Amt",colnames(cldm))]="cld"
|
126 |
|
|
cldm$cld=cldm$cld/100
|
127 |
|
|
cldm[,c("lat","lon")]=coordinates(st)[match(cldm$StaID,st$id),c("lat","lon")]
|
128 |
d7dc526e
|
Adam M. Wilson
|
|
129 |
710f7456
|
Adam M. Wilson
|
## calculate means and sds
|
130 |
1dc46eb9
|
Adam M. Wilson
|
#cldm=do.call(rbind.data.frame,by(cld,list(month=as.factor(cld$month),StaID=as.factor(cld$StaID)),function(x){
|
131 |
|
|
# data.frame(
|
132 |
|
|
# month=x$month[1],
|
133 |
|
|
# lulc=x$lulc[1],
|
134 |
|
|
# StaID=x$StaID[1],
|
135 |
|
|
# mod09=mean(x$mod09,na.rm=T),
|
136 |
|
|
# mod09sd=sd(x$mod09,na.rm=T),
|
137 |
|
|
# cld=mean(x$cld[x$Nobs>50],na.rm=T),
|
138 |
|
|
# cldsd=sd(x$cld[x$Nobs>50],na.rm=T))}))
|
139 |
|
|
#cldm[,c("lat","lon")]=coordinates(st)[match(cldm$StaID,st$id),c("lat","lon")]
|
140 |
710f7456
|
Adam M. Wilson
|
|
141 |
99b3508d
|
Adam M. Wilson
|
## means by year
|
142 |
1dc46eb9
|
Adam M. Wilson
|
#cldy=do.call(rbind.data.frame,by(cld,list(year=as.factor(cld$YR),StaID=as.factor(cld$StaID)),function(x){
|
143 |
|
|
# data.frame(
|
144 |
|
|
# year=x$YR[1],
|
145 |
|
|
# StaID=x$StaID[1],
|
146 |
|
|
# lulc=x$lulc[1],
|
147 |
|
|
# mod09=mean(x$mod09,na.rm=T),
|
148 |
|
|
# mod09sd=sd(x$mod09,na.rm=T),
|
149 |
|
|
# cld=mean(x$cld[x$Nobs>50]/100,na.rm=T),
|
150 |
|
|
# cldsd=sd(x$cld[x$Nobs>50]/100,na.rm=T))}))
|
151 |
|
|
#cldy[,c("lat","lon")]=coordinates(st)[match(cldy$StaID,st$id),c("lat","lon")]
|
152 |
99b3508d
|
Adam M. Wilson
|
|
153 |
d7dc526e
|
Adam M. Wilson
|
## overall mean
|
154 |
1dc46eb9
|
Adam M. Wilson
|
clda=do.call(rbind.data.frame,by(cldm,list(StaID=as.factor(cldm$StaID)),function(x){
|
155 |
d7dc526e
|
Adam M. Wilson
|
data.frame(
|
156 |
|
|
StaID=x$StaID[1],
|
157 |
|
|
lulc=x$lulc[1],
|
158 |
|
|
mod09=mean(x$mod09,na.rm=T),
|
159 |
|
|
mod09sd=sd(x$mod09,na.rm=T),
|
160 |
1dc46eb9
|
Adam M. Wilson
|
cld=mean(x$cld,na.rm=T),
|
161 |
|
|
cldsd=sd(x$cld,na.rm=T))}))
|
162 |
d7dc526e
|
Adam M. Wilson
|
clda[,c("lat","lon")]=coordinates(st)[match(clda$StaID,st$id),c("lat","lon")]
|
163 |
99b3508d
|
Adam M. Wilson
|
|
164 |
a6d44f2d
|
Adam M. Wilson
|
|
165 |
99b3508d
|
Adam M. Wilson
|
## write out the tables
|
166 |
9a19743f
|
Adam M. Wilson
|
write.csv(cld,file="cld.csv",row.names=F)
|
167 |
1dc46eb9
|
Adam M. Wilson
|
#write.csv(cldy,file="cldy.csv",row.names=F)
|
168 |
d7dc526e
|
Adam M. Wilson
|
write.csv(cldm,file="cldm.csv",row.names=F)
|
169 |
f5ef0596
|
Adam M. Wilson
|
write.csv(clda,file="clda.csv",row.names=F)
|
170 |
d7dc526e
|
Adam M. Wilson
|
|
171 |
f5ef0596
|
Adam M. Wilson
|
#########################################################################
|