Project

General

Profile

Download (5.29 KB) Statistics
| Branch: | Revision:
1
### Script to download and process the NDP-026D station cloud dataset
2

    
3
setwd("~/acrobates/adamw/projects/cloud/data/NDP026D")
4

    
5
library(multicore)
6
library(doMC)
7
library(rasterVis)
8
library(rgdal)
9
library(reshape)
10

    
11

    
12
## Data available here http://cdiac.ornl.gov/epubs/ndp/ndp026d/ndp026d.html
13

    
14
## Get station locations
15
system("wget -N -nd http://cdiac.ornl.gov/ftp/ndp026d/cat01/01_STID -P data/")
16
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
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
coordinates(st)=c("lon","lat")
25
projection(st)="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
26

    
27
## download data
28
system("wget -N -nd ftp://cdiac.ornl.gov/pub/ndp026d/cat67_78/* -A '.tc.Z' -P data/")
29

    
30
system("gunzip data/*.Z")
31

    
32
## define FWF widths
33
f162=c(5,5,4,7,7,7,4) #format 162
34
c162=c("StaID","YR","Nobs","Amt","Fq","AWP","NC")
35

    
36
## use monthly timeseries
37
cld=do.call(rbind.data.frame,mclapply(sprintf("%02d",1:12),function(m) {
38
  d=read.fwf(list.files("data",pattern=paste("MNYDC.",m,".tc$",sep=""),full=T),skip=1,widths=f162)
39
  colnames(d)=c162
40
  d$month=as.numeric(m)
41
  print(m)
42
  return(d)}
43
  ))
44

    
45
## add lat/lon
46
cld[,c("lat","lon")]=coordinates(st)[match(cld$StaID,st$id),]
47

    
48
## drop missing values
49
cld=cld[,!grepl("Fq|AWP|NC",colnames(cld))]
50
cld$Amt[cld$Amt<0]=NA
51
#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

    
56
## add the MOD09 data to cld
57
#### Evaluate MOD35 Cloud data
58
mod09=brick("~/acrobates/adamw/projects/cloud/data/cloud_ymonmean.nc")
59

    
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
bins=cut(1:nrow(st),100)
64
if(file.exists("valid.csv")) file.remove("valid.csv")
65
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
## read it back in
75
mod09st=read.csv("valid.csv",header=F)[,-c(1,2)]
76

    
77
colnames(mod09st)=c(names(mod09)[-1],"id")
78
mod09stl=melt(mod09st,id.vars="id")
79
mod09stl[,c("year","month")]=do.call(rbind,strsplit(sub("X","",mod09stl$variable),"[.]"))[,1:2]
80

    
81
## 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
lulc=raster("~/acrobates/adamw/projects/interp/data/modis/mod12/MCD12Q1_IGBP_2005_v51.tif")
89
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
## function to get modal lulc value
94
Mode <- function(x) {
95
      ux <- na.omit(unique(x))
96
        ux[which.max(tabulate(match(x, ux)))]
97
      }
98
lulcst=extract(lulc,st,fun=Mode,buffer=buf,df=T)
99
colnames(lulcst)=c("id","lulc")
100
## add it to cld
101
cld$lulc=lulcst$lulc[match(cld$StaID,lulcst$id)]
102
cld$lulcc=IGBP$class[match(cld$lulc,IGBP$ID)]
103

    
104
## update cld column names
105
colnames(cld)[grep("Amt",colnames(cld))]="cld"
106
cld$cld=cld$cld/100
107
cld[,c("lat","lon")]=coordinates(st)[match(cld$StaID,st$id),c("lat","lon")]
108

    
109
## calculate means and sds
110
cldm=do.call(rbind.data.frame,by(cld,list(month=as.factor(cld$month),StaID=as.factor(cld$StaID)),function(x){
111
  data.frame(
112
             month=x$month[1],
113
             lulc=x$lulc[1],
114
             StaID=x$StaID[1],
115
             mod09=mean(x$mod09,na.rm=T),
116
             mod09sd=sd(x$mod09,na.rm=T),
117
             cld=mean(x$cld[x$Nobs>50],na.rm=T),
118
             cldsd=sd(x$cld[x$Nobs>50],na.rm=T))}))
119
cldm[,c("lat","lon")]=coordinates(st)[match(cldm$StaID,st$id),c("lat","lon")]
120

    
121
## 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
             lulc=x$lulc[1],
127
             mod09=mean(x$mod09,na.rm=T),
128
             mod09sd=sd(x$mod09,na.rm=T),
129
             cld=mean(x$cld[x$Nobs>50]/100,na.rm=T),
130
             cldsd=sd(x$cld[x$Nobs>50]/100,na.rm=T))}))
131
cldy[,c("lat","lon")]=coordinates(st)[match(cldy$StaID,st$id),c("lat","lon")]
132

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

    
144

    
145
## write out the tables
146
write.csv(cld,file="cld.csv",row.names=F)
147
write.csv(cldy,file="cldy.csv",row.names=F)
148
write.csv(cldm,file="cldm.csv",row.names=F)
149
write.csv(clda,file="clda.csv",row.names=F)
150

    
151
#########################################################################
152

    
(36-36/51)