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