Revision f5ef0596
Added by Adam Wilson about 11 years ago
climate/procedures/MOD09_CloudFigures.R | ||
---|---|---|
1 |
### Figures and tables for MOD09 Cloud Manuscript |
|
2 |
|
|
3 |
setwd("~/acrobates/adamw/projects/cloud/") |
|
4 |
|
|
5 |
|
|
6 |
## libraries |
|
7 |
library(rasterVis) |
|
8 |
library(latticeExtra) |
|
9 |
library(xtable) |
|
10 |
library(reshape) |
|
11 |
library(caTools) |
|
12 |
|
|
13 |
## read in data |
|
14 |
cld=read.csv("data/NDP026D/cld.csv") |
|
15 |
cldm=read.csv("data/NDP026D/cldm.csv") |
|
16 |
cldy=read.csv("data/NDP026D/cldy.csv") |
|
17 |
clda=read.csv("data/NDP026D/clda.csv") |
|
18 |
st=read.csv("data/NDP026D/stations.csv") |
|
19 |
|
|
20 |
|
|
21 |
## add lulc factor information |
|
22 |
require(plotKML); data(worldgrids_pal) #load IGBP palette |
|
23 |
IGBP=data.frame(ID=0:16,col=worldgrids_pal$IGBP[-c(18,19)],stringsAsFactors=F) |
|
24 |
IGBP$class=rownames(IGBP);rownames(IGBP)=1:nrow(IGBP) |
|
25 |
|
|
26 |
## month factors |
|
27 |
cld$month2=factor(cld$month,labels=month.name) |
|
28 |
cldm$month2=factor(cldm$month,labels=month.name) |
|
29 |
|
|
30 |
coordinates(st)=c("lon","lat") |
|
31 |
projection(st)=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") |
|
32 |
|
|
33 |
##make spatial object |
|
34 |
cldms=cldm |
|
35 |
coordinates(cldms)=c("lon","lat") |
|
36 |
projection(cldms)=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") |
|
37 |
|
|
38 |
##make spatial object |
|
39 |
cldys=cldy |
|
40 |
coordinates(cldys)=c("lon","lat") |
|
41 |
projection(cldys)=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") |
|
42 |
|
|
43 |
#### Evaluate MOD35 Cloud data |
|
44 |
mod09=brick("data/mod09.nc") |
|
45 |
mod09c=brick("data/mod09_clim_mean.nc",varname="CF");names(mod09c)=month.name |
|
46 |
mod09a=brick("data/mod09_clim_mac.nc",varname="CF_annual")#;names(mod09c)=month.name |
|
47 |
|
|
48 |
## derivatives |
|
49 |
if(!file.exists("data/mod09_std.nc")) { |
|
50 |
system("cdo -chname,CF,CFmin -timmin data/mod09_clim_mean.nc data/mod09_min.nc") |
|
51 |
system("cdo -chname,CF,CFmax -timmax data/mod09_clim_mean.nc data/mod09_max.nc") |
|
52 |
system("cdo -chname,CF,CFsd -timstd data/mod09_clim_mean.nc data/mod09_std.nc") |
|
53 |
system("cdo -f nc2 merge data/mod09_std.nc data/mod09_min.nc data/mod09_max.nc data/mod09_metrics.nc") |
|
54 |
} |
|
55 |
|
|
56 |
mod09min=raster("data/mod09_metrics.nc",varname="CFmin") |
|
57 |
mod09max=raster("data/mod09_metrics.nc",varname="CFmax") |
|
58 |
mod09sd=raster("data/mod09_metrics.nc",varname="CFsd") |
|
59 |
mod09mean=raster("data/mod09_clim_mac.nc") |
|
60 |
|
|
61 |
|
|
62 |
names(mod09d)=c("Mean","Minimum","Maximum","Standard Deviation") |
|
63 |
|
|
64 |
plot(mod09a,layers=1,margin=F,maxpixels=100) |
|
65 |
|
|
66 |
## calculated differences |
|
67 |
cldm$dif=cldm$mod09-cldm$cld |
|
68 |
clda$dif=clda$mod09-clda$cld |
|
69 |
|
|
70 |
## read in global coasts for nice plotting |
|
71 |
library(maptools) |
|
72 |
|
|
73 |
data(wrld_simpl) |
|
74 |
coast <- unionSpatialPolygons(wrld_simpl, rep("land",nrow(wrld_simpl)), threshold=5) |
|
75 |
coast=as(coast,"SpatialLines") |
|
76 |
|
|
77 |
|
|
78 |
## Figures |
|
79 |
n=100 |
|
80 |
at=seq(0,100,length=n) |
|
81 |
colr=colorRampPalette(c("black","green","red")) |
|
82 |
cols=colr(n) |
|
83 |
|
|
84 |
|
|
85 |
pdf("output/validation.pdf",width=11,height=8.5) |
|
86 |
|
|
87 |
## 4-panel maps |
|
88 |
#- Annual average |
|
89 |
levelplot(mod09a,col.regions=colr(100),cuts=100,at=seq(0,100,len=100),colorkey=list(space="bottom",adj=1), |
|
90 |
margin=F,maxpixels=1e6,ylab="Latitude",xlab="Longitude",useRaster=T)+ |
|
91 |
layer(sp.lines(coast,col="black"),under=F) |
|
92 |
#- Monthly minimum |
|
93 |
#- Monthly maximum |
|
94 |
#- STDEV or Min-Max |
|
95 |
|
|
96 |
|
|
97 |
### maps of mod09 and NDP |
|
98 |
## map of stations |
|
99 |
p_mac=levelplot(mod09a,col.regions=colr(100),cuts=100,at=seq(0,100,len=100),colorkey=list(space="bottom",adj=1), |
|
100 |
margin=F,maxpixels=1e6,ylab="Latitude",xlab="Longitude",useRaster=T)+ |
|
101 |
layer(panel.xyplot(lon,lat,pch=16,cex=.3,col="black"),data=data.frame(coordinates(st)))+ |
|
102 |
layer(sp.lines(coast,col="black"),under=F) |
|
103 |
|
|
104 |
p_mace=xyplot(lat~dif,data=cldm[cldm$lat>-60,],panel=function(x,y,subscripts=T){ |
|
105 |
x2=x[order(y)] |
|
106 |
y2=y[order(y)] |
|
107 |
win=8000 |
|
108 |
Q50=runquantile(x2,win,probs=c(.25,.5,.75)) |
|
109 |
## polygon |
|
110 |
panel.polygon(c(Q50[,1],rev(Q50[,3])),c(y2,rev(y2)),type="l",col=grey(.8)) |
|
111 |
### hist |
|
112 |
n=150 |
|
113 |
xbins=seq(-70,50,len=n) |
|
114 |
ybins=seq(-60,90,len=n) |
|
115 |
tb=melt(as.matrix(table( |
|
116 |
x=cut(x,xbins,labels=xbins[-1]), |
|
117 |
y=cut(y,ybins,labels=ybins[-1])))) |
|
118 |
qat=unique(tb$value) |
|
119 |
print(qat) |
|
120 |
panel.levelplot(tb$x,tb$y,tb$value,at=qat,col.regions=c("transparent",hmcols(length(qat))),subscripts=1:nrow(tb)) |
|
121 |
### |
|
122 |
# panel.xyplot(x,y,pch=16,cex=.1,col=) |
|
123 |
# cuts=cut(y,lats,labels=lats[-10]) |
|
124 |
# qs=do.call(rbind,tapply(x,cuts,quantile,c(.25,.50,.75),na.rm=T)) |
|
125 |
colnames(qs)=c("Q25","Q50","Q75") |
|
126 |
panel.lines(Q50[,1],y2,type="l",col=grey(.5)) |
|
127 |
panel.lines(Q50[,2],y2,type="l",col="black") |
|
128 |
panel.lines(Q50[,3],y2,type="l",col=grey(.5)) |
|
129 |
},asp=1,xlab="Difference (MOD09-Observed)")+layer(panel.abline(v=0,lty="dashed",col="red")) |
|
130 |
|
|
131 |
print(p_mac,position=c(0,0,.75,1),more=T) |
|
132 |
print(p_mace,position=c(0.75,0,1,1)) |
|
133 |
|
|
134 |
|
|
135 |
p1=c("MODIS Cloud Frequency and NDP-026D Validation Stations"=p_mac,"Difference (MOD09-NDP026D)"=p_mace,x.same=F,y.same=T,layout=c(2,1)) |
|
136 |
resizePanels(p1,w=c(.75,.25)) |
|
137 |
|
|
138 |
quantile(cldm$dif,seq(0,1,len=6),na.rm=T) |
|
139 |
at=c(-70,-50,-25,-10,-5,0,5,10,25,50,70) |
|
140 |
bwr=colorRampPalette(c("blue","grey","red")) |
|
141 |
xyplot(lat~lon|month2,data=cldm,groups=cut(cldm$dif,at), |
|
142 |
par.settings=list(superpose.symbol=list(col=bwr(length(at)-1))),pch=16,cex=.25, |
|
143 |
auto.key=list(space="right",title="Difference\n(MOD09-NDP026D)",cex.title=1),asp=1, |
|
144 |
main="NDP-026D Cloud Climatology Stations",ylab="Latitude",xlab="Longitude")+ |
|
145 |
layer(sp.lines(coast,col="black",lwd=.1),under=F) |
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
### heatmap of mod09 vs. NDP for all months |
|
150 |
hmcols=colorRampPalette(c("grey","blue","red")) |
|
151 |
#hmcols=colorRampPalette(c(grey(.8),grey(.3),grey(.2))) |
|
152 |
tr=c(0,120) |
|
153 |
colkey <- draw.colorkey(list(col = hmcols(tr[2]), at = tr[1]:tr[2],height=.25)) |
|
154 |
|
|
155 |
xyplot(cld~mod09,data=cld[cld$Nobs>10,],panel=function(x,y,subscripts){ |
|
156 |
n=150 |
|
157 |
bins=seq(0,100,len=n) |
|
158 |
tb=melt(as.matrix(table( |
|
159 |
x=cut(x,bins,labels=bins[-1]), |
|
160 |
y=cut(y,bins,labels=bins[-1])))) |
|
161 |
qat=tr[1]:tr[2]#unique(tb$value) |
|
162 |
print(qat) |
|
163 |
panel.levelplot(tb$x,tb$y,tb$value,at=qat,col.regions=c("transparent",hmcols(length(qat))),subscripts=subscripts) |
|
164 |
},asp=1,scales=list(at=seq(0,100,len=6)),ylab="NDP Mean Cloud Amount (%)",xlab="MOD09 Cloud Frequency (%)", |
|
165 |
legend= list(right = list(fun = colkey,title="Station Count")))+ |
|
166 |
layer(panel.abline(0,1,col="black",lwd=2)) |
|
167 |
# layer(panel.ablineq(lm(y ~ x), r.sq = TRUE,at = 0.6,pos=1, offset=22,digits=2,col="blue"), style = 1) |
|
168 |
|
|
169 |
|
|
170 |
xyplot(cld~mod09|month2,data=cld[cld$Nobs>50,],panel=function(x,y,subscripts){ |
|
171 |
n=50 |
|
172 |
bins=seq(0,100,len=n) |
|
173 |
tb=melt(as.matrix(table( |
|
174 |
x=cut(x,bins,labels=bins[-1]), |
|
175 |
y=cut(y,bins,labels=bins[-1])))) |
|
176 |
qat=unique(tb$value) |
|
177 |
qat=0:78 |
|
178 |
qat=tr[1]:tr[2]#unique(tb$value) |
|
179 |
panel.levelplot(tb$x,tb$y,tb$value,at=qat,col.regions=c("transparent",hmcols(length(qat))),subscripts=1:nrow(tb)) |
|
180 |
panel.abline(0,1,col="black",lwd=2) |
|
181 |
# panel.ablineq(lm(y ~ x), r.sq = TRUE,at = 0.6,pos=1, offset=0,digits=2,col="blue") |
|
182 |
panel.text(70,10,bquote(paste(R^2,"=",.(round(summary(lm(y ~ x))$r.squared,2)))),cex=1.2) |
|
183 |
},asp=1,scales=list(at=seq(0,100,len=6),useRaster=T,colorkey=list(width=.5,title="Number of Stations")), |
|
184 |
ylab="NDP Mean Cloud Amount (%)",xlab="MOD09 Cloud Frequency (%)", |
|
185 |
legend= list(right = list(fun = colkey)))+ layer(panel.abline(0,1,col="black",lwd=2)) |
|
186 |
|
|
187 |
|
|
188 |
## Monthly Climatologies |
|
189 |
for(i in 1:2){ |
|
190 |
p1=xyplot(cld~mod09|month2,data=cldm,cex=.2,pch=16,subscripts=T,ylab="NDP Mean Cloud Amount",xlab="MOD09 Cloud Frequency (%)")+ |
|
191 |
layer(panel.lines(1:100,predict(lm(y~x),newdata=data.frame(x=1:100)),col="green"))+ |
|
192 |
layer(panel.lines(1:100,predict(lm(y~x+I(x^2)),newdata=data.frame(x=1:100)),col="blue"))+ |
|
193 |
layer(panel.abline(0,1,col="red")) |
|
194 |
if(i==2){ |
|
195 |
p1=p1+layer(panel.segments(mod09[subscripts],cld[subscripts]-cldsd[subscripts],mod09[subscripts],cld[subscripts]+cldsd[subscripts],subscripts=subscripts,col="grey"),data=cldm,under=T,magicdots=T) |
|
196 |
p1=p1+layer(panel.segments(mod09[subscripts]-mod09sd[subscripts],cld[subscripts],mod09[subscripts]+mod09sd[subscripts],cld[subscripts],subscripts=subscripts,col="grey"),data=cldm,under=T,magicdots=T) |
|
197 |
} |
|
198 |
print(p1) |
|
199 |
} |
|
200 |
|
|
201 |
bwplot(lulcc~dif,data=cldm,horiz=T,xlab="Difference (MOD09-Observed)",varwidth=T,notch=T)+layer(panel.abline(v=0)) |
|
202 |
|
|
203 |
|
|
204 |
dev.off() |
|
205 |
|
|
206 |
|
|
207 |
summary(lm(cld~mod09,data=cld)) |
|
208 |
|
|
209 |
## explore validation error |
|
210 |
cldm$lulcc=as.factor(IGBP$class[match(cldm$lulc,IGBP$ID)]) |
|
211 |
|
|
212 |
## Table of RMSE's by lulc by month |
|
213 |
lulcrmsel=ddply(cldm,c("month","lulc"),function(x) c(count=nrow(x),rmse=sqrt(mean((x$mod09-x$cld)^2,na.rm=T)))) |
|
214 |
lulcrmsel=lulcrmsel[!is.na(lulcrmsel$lulc),] |
|
215 |
lulcrmsel$lulcc=as.factor(IGBP$class[match(lulcrmsel$lulc,IGBP$ID)]) |
|
216 |
|
|
217 |
lulcrmse=cast(lulcrmsel,lulcc~month,value="rmse") |
|
218 |
lulcrmse |
|
219 |
print(xtable(lulcrmse,digits=1),"html") |
|
220 |
|
|
221 |
levelplot(rmse~lulc*month,data=lulcrmsel,col.regions=heat.colors(20)) |
|
222 |
|
|
223 |
|
|
224 |
### Linear models |
|
225 |
summary(lm(dif~as.factor(lulc)+lat+month2,data=cldm)) |
|
226 |
|
climate/procedures/NDP-026D.R | ||
---|---|---|
1 |
#! /bin/R |
|
2 | 1 |
### Script to download and process the NDP-026D station cloud dataset |
3 |
setwd("~/acrobates/adamw/projects/interp/data/NDP026D") |
|
2 |
|
|
3 |
setwd("~/acrobates/adamw/projects/cloud/data/NDP026D") |
|
4 | 4 |
|
5 | 5 |
library(multicore) |
6 |
library(latticeExtra) |
|
7 | 6 |
library(doMC) |
8 | 7 |
library(rasterVis) |
9 | 8 |
library(rgdal) |
10 |
library(reshape) |
|
11 |
library(hexbin) |
|
12 |
## register parallel processing |
|
13 |
#registerDoMC(10) |
|
14 |
#beginCluster(10) |
|
15 | 9 |
|
16 |
## available here http://cdiac.ornl.gov/epubs/ndp/ndp026d/ndp026d.html |
|
10 |
|
|
11 |
|
|
12 |
## Data available here http://cdiac.ornl.gov/epubs/ndp/ndp026d/ndp026d.html |
|
17 | 13 |
|
18 | 14 |
## Get station locations |
19 | 15 |
system("wget -N -nd http://cdiac.ornl.gov/ftp/ndp026d/cat01/01_STID -P data/") |
... | ... | |
45 | 41 |
)) |
46 | 42 |
|
47 | 43 |
## add lat/lon |
48 |
cld[,c("lat","lon")]=st[match(cld$StaID,st$id),c("lat","lon")]
|
|
44 |
cld[,c("lat","lon")]=coordinates(st)[match(cld$StaID,st$id),]
|
|
49 | 45 |
|
50 | 46 |
## drop missing values |
51 | 47 |
cld=cld[,!grepl("Fq|AWP|NC",colnames(cld))] |
... | ... | |
62 | 58 |
## overlay the data with 32km diameter (16km radius) buffer |
63 | 59 |
## buffer size from Dybbroe, et al. (2005) doi:10.1175/JAM-2189.1. |
64 | 60 |
buf=16000 |
65 |
#mod09sta=lapply(1:nlayers(mod09),function(l) {print(l); extract(mod09[[l]],st,buffer=buf,fun=mean,na.rm=T,df=T)[,2]}) |
|
66 | 61 |
bins=cut(1:nrow(st),100) |
67 | 62 |
mod09sta=lapply(levels(bins),function(lb) { |
68 | 63 |
l=which(bins==lb) |
... | ... | |
73 | 68 |
td |
74 | 69 |
})#,mc.cores=3) |
75 | 70 |
|
76 |
#mod09sta=extract(mod09,st,buffer=buf,fun=mean,na.rm=T,df=T)
|
|
71 |
## read it back in
|
|
77 | 72 |
mod09st=read.csv("valid.csv",header=F)[,-c(1,2)] |
78 | 73 |
|
79 |
#mod09st=do.call(rbind.data.frame,mod09sta) |
|
80 |
#mod09st=mod09st[,!is.na(colnames(mod09st))] |
|
81 |
colnames(mod09st)=c(names(mod09),"id") |
|
82 |
#mod09st$id=st$id |
|
74 |
colnames(mod09st)=c(names(mod09)[-1],"id") |
|
83 | 75 |
mod09stl=melt(mod09st,id.vars="id") |
84 | 76 |
mod09stl[,c("year","month")]=do.call(rbind,strsplit(sub("X","",mod09stl$variable),"[.]"))[,1:2] |
77 |
|
|
85 | 78 |
## add it to cld |
86 | 79 |
cld$mod09=mod09stl$value[match(paste(cld$StaID,cld$YR,cld$month),paste(mod09stl$id,mod09stl$year,as.numeric(mod09stl$month)))] |
87 | 80 |
|
... | ... | |
89 | 82 |
## LULC |
90 | 83 |
#system(paste("gdalwarp -r near -co \"COMPRESS=LZW\" -tr ",paste(res(mod09),collapse=" ",sep=""), |
91 | 84 |
# "-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")) |
92 |
lulc=raster("../modis/mod12/MCD12Q1_IGBP_2005_v51.tif") |
|
93 |
#lulc=ratify(lulc) |
|
85 |
lulc=raster("~/acrobates/adamw/projects/interp/data/modis/mod12/MCD12Q1_IGBP_2005_v51.tif") |
|
94 | 86 |
require(plotKML); data(worldgrids_pal) #load IGBP palette |
95 | 87 |
IGBP=data.frame(ID=0:16,col=worldgrids_pal$IGBP[-c(18,19)],stringsAsFactors=F) |
96 | 88 |
IGBP$class=rownames(IGBP);rownames(IGBP)=1:nrow(IGBP) |
97 | 89 |
levels(lulc)=list(IGBP) |
98 |
#lulc=crop(lulc,mod09)
|
|
99 |
Mode <- function(x) {
|
|
90 |
## function to get modal lulc value
|
|
91 |
Mode <- function(x) { |
|
100 | 92 |
ux <- na.omit(unique(x)) |
101 | 93 |
ux[which.max(tabulate(match(x, ux)))] |
102 | 94 |
} |
... | ... | |
104 | 96 |
colnames(lulcst)=c("id","lulc") |
105 | 97 |
## add it to cld |
106 | 98 |
cld$lulc=lulcst$lulc[match(cld$StaID,lulcst$id)] |
107 |
#cld$lulc=factor(as.integer(cld$lulc),labels=IGBP$class[sort(unique(cld$lulc))])
|
|
99 |
cld$lulcc=IGBP$class[match(cld$lulc,IGBP$ID)]
|
|
108 | 100 |
|
109 | 101 |
## update cld column names |
110 | 102 |
colnames(cld)[grep("Amt",colnames(cld))]="cld" |
111 | 103 |
cld$cld=cld$cld/100 |
104 |
cld[,c("lat","lon")]=coordinates(st)[match(cld$StaID,st$id),c("lat","lon")] |
|
112 | 105 |
|
113 | 106 |
## calculate means and sds |
114 | 107 |
cldm=do.call(rbind.data.frame,by(cld,list(month=as.factor(cld$month),StaID=as.factor(cld$StaID)),function(x){ |
... | ... | |
118 | 111 |
StaID=x$StaID[1], |
119 | 112 |
mod09=mean(x$mod09,na.rm=T), |
120 | 113 |
mod09sd=sd(x$mod09,na.rm=T), |
121 |
cld=mean(x$cld[x$Nobs>10],na.rm=T),
|
|
122 |
cldsd=sd(x$cld[x$Nobs>10],na.rm=T))}))
|
|
114 |
cld=mean(x$cld[x$Nobs>50],na.rm=T),
|
|
115 |
cldsd=sd(x$cld[x$Nobs>50],na.rm=T))}))
|
|
123 | 116 |
cldm[,c("lat","lon")]=coordinates(st)[match(cldm$StaID,st$id),c("lat","lon")] |
124 | 117 |
|
125 | 118 |
## means by year |
... | ... | |
130 | 123 |
lulc=x$lulc[1], |
131 | 124 |
mod09=mean(x$mod09,na.rm=T), |
132 | 125 |
mod09sd=sd(x$mod09,na.rm=T), |
133 |
cld=mean(x$Amt[x$Nobs>10]/100,na.rm=T),
|
|
134 |
cldsd=sd(x$Amt[x$Nobs>10]/100,na.rm=T))}))
|
|
126 |
cld=mean(x$cld[x$Nobs>50]/100,na.rm=T),
|
|
127 |
cldsd=sd(x$cld[x$Nobs>50]/100,na.rm=T))}))
|
|
135 | 128 |
cldy[,c("lat","lon")]=coordinates(st)[match(cldy$StaID,st$id),c("lat","lon")] |
136 | 129 |
|
137 | 130 |
## overall mean |
... | ... | |
150 | 143 |
write.csv(cld,file="cld.csv",row.names=F) |
151 | 144 |
write.csv(cldy,file="cldy.csv",row.names=F) |
152 | 145 |
write.csv(cldm,file="cldm.csv",row.names=F) |
153 |
write.csv(clda,file="clda.csv",row.names=F |
|
154 |
) |
|
155 |
######################################################################### |
|
156 |
################## |
|
157 |
### |
|
158 |
cld=read.csv("cld.csv") |
|
159 |
cldm=read.csv("cldm.csv") |
|
160 |
cldy=read.csv("cldy.csv") |
|
161 |
clda=read.csv("clda.csv") |
|
162 |
st=read.csv("stations.csv") |
|
163 |
|
|
164 |
### remove mod09==0 due to mosaic problem (remove when fixed) |
|
165 |
cld=cld[!is.na(cld$lat)&cld$mod09!=0,] |
|
166 |
cldm=cldm[!is.na(cldm$lat)&cldm$mod09!=0,] |
|
167 |
cldy=cldy[!is.na(cldy$lat)&cldy$mod09!=0,] |
|
168 |
|
|
169 |
## month factors |
|
170 |
cld$month2=factor(cld$month,labels=month.name) |
|
171 |
cldm$month2=factor(cldm$month,labels=month.name) |
|
172 |
|
|
173 |
coordinates(st)=c("lon","lat") |
|
174 |
projection(st)=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") |
|
175 |
|
|
176 |
##make spatial object |
|
177 |
cldms=cldm |
|
178 |
coordinates(cldms)=c("lon","lat") |
|
179 |
projection(cldms)=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") |
|
180 |
|
|
181 |
##make spatial object |
|
182 |
cldys=cldy |
|
183 |
coordinates(cldys)=c("lon","lat") |
|
184 |
projection(cldys)=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") |
|
185 |
|
|
186 |
#### Evaluate MOD35 Cloud data |
|
187 |
mod09=brick("~/acrobates/adamw/projects/cloud/data/mod09.nc") |
|
188 |
mod09c=brick("~/acrobates/adamw/projects/cloud/data/mod09_clim.nc",varname="CF");names(mod09c)=month.name |
|
189 |
mod09c2=raster("~/acrobates/adamw/projects/cloud/data/mod09_clim.nc",varname="CF",nl=1) |
|
190 |
|
|
191 |
### get monthly climatologies for each station |
|
192 |
#cldc=do.call(rbind.data.frame,by(cld,list(id=cld$StaID,month=cld$month),function(x){ |
|
193 |
# x$mod09[x$mod09==0]=NA |
|
194 |
# data.frame(id=x$StaID[1],month=x$month[1],Nobs=sum(x$Nobs,na.rm=T),Amt=mean(x$Amt,na.rm=T),mod09=mean(x$mod09,na.rm=T)) |
|
195 |
# })) |
|
196 |
|
|
197 |
## read in global coasts for nice plotting |
|
198 |
library(maptools) |
|
199 |
|
|
200 |
data(wrld_simpl) |
|
201 |
coast <- unionSpatialPolygons(wrld_simpl, rep("land",nrow(wrld_simpl)), threshold=5) |
|
202 |
coast=as(coast,"SpatialLines") |
|
203 |
#coast=spTransform(coast,CRS(projection(mod35))) |
|
204 |
|
|
205 |
|
|
206 |
n=100 |
|
207 |
at=seq(0,100,length=n) |
|
208 |
colr=colorRampPalette(c("black","green","red")) |
|
209 |
cols=colr(n) |
|
210 |
|
|
211 |
|
|
212 |
pdf("/home/adamw/acrobates/adamw/projects/cloud/output/validation.pdf",width=11,height=8.5) |
|
213 |
|
|
214 |
### maps of mod09 and NDP |
|
215 |
## map of stations |
|
216 |
xyplot(lat~lon,data=data.frame(coordinates(st)),pch=16,cex=.5, main="NDP-026D Cloud Climatology Stations",ylab="Latitude",xlab="Longitude")+ |
|
217 |
layer(sp.lines(coast,col="grey"),under=T) |
|
218 |
|
|
219 |
levelplot(mod09c,col.regions=colr(100),at=seq(0,100,len=100),margin=F,maxpixels=1e5,main="MOD09 Cloud Frequency",ylab="Latitude",xlab="Longitude") |
|
220 |
|
|
221 |
#p2=xyplot(lat~lon|month2,data=cldm,col=as.character(cut(cldm$cld,seq(0,100,len=100),labels=colr(99))),pch=16,cex=.1,auto.key=T,asp=1, |
|
222 |
# main="NDP-026D Cloud Climatology Stations",ylab="Latitude",xlab="Longitude",layout=c(12,1))+ |
|
223 |
# layer(sp.lines(coast,col="black",lwd=.1),under=F) |
|
224 |
#v_month=c(p1,p2,layout=c(12,2),x.same=T,y.same=T,merge.legends=T) |
|
225 |
#print(v_month) |
|
226 |
|
|
227 |
|
|
228 |
#xyplot(lat~lon|month2,groups=cut(cldm$cld,seq(0,100,len=5)),data=cldm,pch=".",cex=.2,auto.key=T, |
|
229 |
# main="Mean Monthly Cloud Coverage",ylab="Latitude",xlab="Longitude", |
|
230 |
# par.settings = list(superpose.symbol= list(pch=16,col=c("blue","green","yellow","red"))))+ |
|
231 |
# layer(sp.lines(coast,col="grey"),under=T) |
|
146 |
write.csv(clda,file="clda.csv",row.names=F) |
|
232 | 147 |
|
233 |
### heatmap of mod09 vs. NDP for all months |
|
234 |
hmcols=colorRampPalette(c("grey","blue","red")) |
|
235 |
tr=c(0,27) |
|
236 |
colkey <- draw.colorkey(list(col = hmcols(tr[2]), at = tr[1]:tr[2],height=.25)) |
|
237 |
|
|
238 |
xyplot(cld~mod09,data=cld[cld$Nobs>10,],panel=function(x,y,subscripts){ |
|
239 |
n=150 |
|
240 |
bins=seq(0,100,len=n) |
|
241 |
tb=melt(as.matrix(table( |
|
242 |
x=cut(x,bins,labels=bins[-1]), |
|
243 |
y=cut(y,bins,labels=bins[-1])))) |
|
244 |
qat=tr[1]:tr[2]#unique(tb$value) |
|
245 |
print(qat) |
|
246 |
panel.levelplot(tb$x,tb$y,tb$value,at=qat,col.regions=c("transparent",hmcols(length(qat))),subscripts=subscripts) |
|
247 |
},asp=1,scales=list(at=seq(0,100,len=6)),ylab="NDP Mean Cloud Amount (%)",xlab="MOD09 Cloud Frequency (%)", |
|
248 |
legend= list(right = list(fun = colkey,title="Station Count")))+ |
|
249 |
layer(panel.abline(0,1,col="black",lwd=2))+ |
|
250 |
layer(panel.ablineq(lm(y ~ x), r.sq = TRUE,at = 0.6,pos=1, offset=22,digits=2,col="blue"), style = 1) |
|
251 |
|
|
252 |
|
|
253 |
|
|
254 |
xyplot(cld~mod09|month2,data=cld[cld$Nobs>10,],panel=function(x,y,subscripts){ |
|
255 |
n=50 |
|
256 |
bins=seq(0,100,len=n) |
|
257 |
tb=melt(as.matrix(table( |
|
258 |
x=cut(x,bins,labels=bins[-1]), |
|
259 |
y=cut(y,bins,labels=bins[-1])))) |
|
260 |
qat=unique(tb$value) |
|
261 |
print(qat) |
|
262 |
qat=0:26 |
|
263 |
qat=tr[1]:tr[2]#unique(tb$value) |
|
264 |
panel.levelplot(tb$x,tb$y,tb$value,at=qat,col.regions=c("transparent",hmcols(length(qat))),subscripts=1:nrow(tb)) |
|
265 |
layer(panel.abline(0,1,col="black",lwd=2))+ |
|
266 |
layer(panel.ablineq(lm(y ~ x), r.sq = TRUE,at = 0.6,pos=1, offset=0,digits=2,col="blue"), style = 1) |
|
267 |
},asp=1,scales=list(at=seq(0,100,len=6),useRaster=T,colorkey=list(width=.5,title="Number of Stations")), |
|
268 |
ylab="NDP Mean Cloud Amount (%)",xlab="MOD09 Cloud Frequency (%)", |
|
269 |
legend= list(right = list(fun = colkey)))+ layer(panel.abline(0,1,col="black",lwd=2)) |
|
270 |
|
|
271 |
|
|
272 |
xyplot(cld~mod09,data=clda,cex=0.5,pch=16)+ |
|
273 |
layer(panel.abline(lm(y~x),col="blue"))+ |
|
274 |
# layer(panel.lines(x,predict(lm(y~x),type="prediction")))+ |
|
275 |
layer(panel.loess(x,y,col="blue",span=.2))+ |
|
276 |
layer(panel.abline(0,1,col="red"))+ |
|
277 |
layer(panel.segments(mod09,cld-cldsd,mod09,cld+cldsd,col="grey"),data=clda,under=T,magicdots=T) |
|
278 |
|
|
279 |
## all monthly values |
|
280 |
#xyplot(cld~mod09|as.factor(month),data=cld[cld$Nobs>75,],cex=.2,pch=16,subscripts=T)+ |
|
281 |
# layer(panel.abline(lm(y~x),col="blue"))+ |
|
282 |
# layer(panel.abline(0,1,col="red")) |
|
283 |
|
|
284 |
## Monthly Climatologies |
|
285 |
for(i in 1:2){ |
|
286 |
p1=xyplot(cld~mod09|month2,data=cldm,cex=.2,pch=16,subscripts=T,ylab="NDP Mean Cloud Amount",xlab="MOD09 Cloud Frequency (%)")+ |
|
287 |
layer(panel.lines(1:100,predict(lm(y~x),newdata=data.frame(x=1:100)),col="green"))+ |
|
288 |
layer(panel.lines(1:100,predict(lm(y~x+I(x^3)),newdata=data.frame(x=1:100)),col="blue"))+ |
|
289 |
layer(panel.abline(0,1,col="red")) |
|
290 |
if(i==2){ |
|
291 |
p1=p1+layer(panel.segments(mod09[subscripts],cld[subscripts]-cldsd[subscripts],mod09[subscripts],cld[subscripts]+cldsd[subscripts],subscripts=subscripts,col="grey"),data=cldm,under=T,magicdots=T) |
|
292 |
p1=p1+layer(panel.segments(mod09[subscripts]-mod09sd[subscripts],cld[subscripts],mod09[subscripts]+mod09sd[subscripts],cld[subscripts],subscripts=subscripts,col="grey"),data=cldm,under=T,magicdots=T) |
|
293 |
} |
|
294 |
print(p1) |
|
295 |
} |
|
296 |
|
|
297 |
dev.off() |
|
298 |
|
|
299 |
|
|
300 |
summary(lm(Amt~mod09,data=cld)) |
|
301 |
summary(lm(cld~mod09_10+as.factor(lulc),data=d)) |
|
302 |
summary(lm(cld~mod09_10+as.factor(lulc),data=d)) |
|
303 |
|
|
304 |
### exploratory plots |
|
305 |
xyplot(cld~mod09_10,groups=lulc,data=d@data,pch=16,cex=.5)+layer(panel.abline(0,1,col="red")) |
|
306 |
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")) |
|
307 |
xyplot(cld~mod35_10|as.factor(lulc),data=d@data,pch=16,cex=.5)+layer(panel.abline(0,1,col="red")) |
|
308 |
xyplot(mod35_10~mod09_10|as.factor(lulc),data=d@data,pch=16,cex=.5)+layer(panel.abline(0,1,col="red")) |
|
309 |
|
|
310 |
densityplot(stack(mod35,mod09)) |
|
311 |
boxplot(mod35,lulc) |
|
312 |
|
|
313 |
bwplot(mod09~mod35|cut(y,5),data=stack(mod09,mod35)) |
|
314 |
|
|
315 |
## add a color key |
|
316 |
breaks=seq(0,100,by=25) |
|
317 |
cldm$cut=cut(cldm$cld,breaks) |
|
318 |
cp=colorRampPalette(c("blue","orange","red")) |
|
319 |
cols=cp(length(at)) |
|
320 |
|
|
321 |
|
|
322 |
|
|
323 |
## write a pdf |
|
324 |
#dir.create("output") |
|
325 |
pdf("output/NDP026d.pdf",width=11,height=8.5) |
|
326 |
|
|
327 |
|
|
328 |
|
|
329 |
## Validation |
|
330 |
m=10 |
|
331 |
zlim=c(40,100) |
|
332 |
dr=subset(mod35,subset=m);projection(dr)=projection(mod35) |
|
333 |
ds=cldms[cldms$month==m,] |
|
334 |
plot(dr,col=cp(100),zlim=zlim,main="Comparison of MOD35 Cloud Frequency and NDP-026D Station Cloud Climatologies", |
|
335 |
ylab="Northing (m)",xlab="Easting (m)",sub="MOD35 is proportion of cloudy days, while NDP-026D is Mean Cloud Coverage") |
|
336 |
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)))) |
|
337 |
#legend("topright",legend=seq(zlim[1],zlim[2],len=5),pch=16,col=cp(length(breaks))) |
|
338 |
|
|
339 |
|
|
340 |
xyplot(mod35~cld,data=mod35v,subscripts=T,auto.key=T,panel=function(x,y,subscripts){ |
|
341 |
td=mod35v[subscripts,] |
|
342 |
# panel.segments(x-td$cldsd[subscripts],y,x+td$cldsd[subscripts],y,subscripts=subscripts) |
|
343 |
panel.xyplot(x,y,subscripts=subscripts,type=c("p","smooth"),pch=16,col="black") |
|
344 |
# panel.segments(x-td$cldsd[subscripts],y,x+td$cldsd[subscripts],y,subscripts=subscripts) |
|
345 |
},ylab="MOD35 Proportion Cloudy Days",xlab="NDP-026D Mean Monthly Cloud Amount", |
|
346 |
main="Comparison of MOD35 Cloud Mask and Station Cloud Climatologies") |
|
347 |
|
|
348 |
#xyplot(mod35~cld|month,data=mod35v,subscripts=T,auto.key=T,panel=function(x,y,subscripts){ |
|
349 |
# td=mod35v[subscripts,] |
|
350 |
# panel.segments(x-td$cldsd[subscripts],y,x+td$cldsd[subscripts],y,subscripts=subscripts) |
|
351 |
# panel.xyplot(x,y,subscripts=subscripts,type=c("p","smooth"),pch=16,col="black") |
|
352 |
# panel.segments(x-td$cldsd[subscripts],y,x+td$cldsd[subscripts],y,subscripts=subscripts) |
|
353 |
# },ylab="MOD35 Proportion Cloudy Days",xlab="NDP-026D Mean Monthly Cloud Amount", |
|
354 |
# main="Comparison of MOD35 Cloud Mask and Station Cloud Climatologies") |
|
355 |
|
|
356 |
|
|
357 |
dev.off() |
|
148 |
######################################################################### |
|
358 | 149 |
|
359 |
graphics.off() |
climate/procedures/ee.MOD09.py | ||
---|---|---|
1 |
#!/usr/bin/env python |
|
2 |
|
|
1 | 3 |
## Example script that downloads data from Google Earth Engine using the python API |
2 | 4 |
## MODIS MOD09GA data is processed to extract the MOD09 cloud flag and calculate monthly cloud frequency |
3 | 5 |
|
... | ... | |
9 | 11 |
import datetime |
10 | 12 |
import wget |
11 | 13 |
import os |
14 |
import sys |
|
12 | 15 |
from subprocess import call |
13 | 16 |
|
14 |
#import logging |
|
15 |
#logging.basicConfig() |
|
17 |
import logging |
|
18 |
logging.basicConfig(filename='error.log',level=logging.DEBUG) |
|
19 |
|
|
20 |
def Usage(): |
|
21 |
print('Usage: ee.MOD9.py -projwin ulx uly lrx lry -year year -month month -regionname 1') |
|
22 |
sys.exit( 1 ) |
|
23 |
|
|
24 |
ulx = float(sys.argv[2]) |
|
25 |
uly = float(sys.argv[3]) |
|
26 |
lrx = float(sys.argv[4]) |
|
27 |
lry = float(sys.argv[5]) |
|
28 |
year = int(sys.argv[7]) |
|
29 |
month = int(sys.argv[9]) |
|
30 |
regionname = str(sys.argv[11]) |
|
31 |
|
|
32 |
#``` |
|
33 |
#ulx=-159 |
|
34 |
#uly=20 |
|
35 |
#lrx=-154.5 |
|
36 |
#lry=18.5 |
|
37 |
#year=2001 |
|
38 |
#month=6 |
|
39 |
#``` |
|
40 |
## Define output filename |
|
41 |
output=regionname+'_'+str(year)+'_'+str(month) |
|
16 | 42 |
|
17 | 43 |
## set working directory (where files will be downloaded) |
18 | 44 |
os.chdir('/mnt/data2/projects/cloud/mod09') |
... | ... | |
20 | 46 |
MY_SERVICE_ACCOUNT = '511722844190@developer.gserviceaccount.com' # replace with your service account |
21 | 47 |
MY_PRIVATE_KEY_FILE = '/home/adamw/EarthEngine-privatekey.p12' # replace with you private key file path |
22 | 48 |
|
23 |
#MY_SERVICE_ACCOUNT = '205878743334-4mrtqgu0n5rnsv1vanrvv6atqk6vu8am@developer.gserviceaccount.com' |
|
24 |
#MY_PRIVATE_KEY_FILE = '/home/adamw/EarthEngine_Jeremy-privatekey.p12' |
|
25 |
|
|
26 | 49 |
ee.Initialize(ee.ServiceAccountCredentials(MY_SERVICE_ACCOUNT, MY_PRIVATE_KEY_FILE)) |
27 | 50 |
|
28 | 51 |
## set map center to speed up viewing |
... | ... | |
33 | 56 |
def getmod09(img): return(img.select(['state_1km']).expression("((b(0)/1024)%2)>0.5")); |
34 | 57 |
# added the >0.5 because some values are coming out >1. Need to look into this further as they should be bounded 0-1... |
35 | 58 |
|
36 |
#// Date ranges |
|
37 |
yearstart=2000 |
|
38 |
yearstop=2012 |
|
39 |
monthstart=1 |
|
40 |
monthstop=12 |
|
41 |
|
|
42 | 59 |
#//////////////////////////////////////////////////// |
43 |
# Loop through months and get monthly % missing data |
|
44 |
|
|
45 |
## set a year-month if you don't want to run the loop (for testing) |
|
46 |
#year=2001 |
|
47 |
#month=2 |
|
48 |
#r=1 |
|
49 |
|
|
50 |
## define the regions to be processed |
|
51 |
regions=['[[-180, -60], [-180, 0], [0, 0], [0, -60]]', # SW |
|
52 |
'[[-180, 0], [-180, 90], [0, 90], [0, 0]]', # NW |
|
53 |
'[[0, 0], [0, 90], [180, 90], [180, 0]]', # NE |
|
54 |
'[[0, 0], [0, -60], [180, -60], [180, 0]]'] # SE |
|
55 |
## name the regions (these names will be used in the file names |
|
56 |
## must be same length as regions list above |
|
57 |
rnames=['SW','NW','NE','SE'] |
|
58 |
|
|
59 |
## Loop over regions, years, months to generate monthly timeseries |
|
60 |
for r in range(0,len(regions)): # loop over regions |
|
61 |
for year in range(yearstart,yearstop+1): # loop over years |
|
62 |
for month in range(monthstart,monthstop+1): # loop over months |
|
63 |
print('Processing '+rnames[r]+"_"+str(year)+'_'+str(month)) |
|
64 | 60 |
|
65 | 61 |
# output filename |
66 |
filename='mod09_'+rnames[r]+"_"+str(year)+"_"+str(month) |
|
67 |
unzippedfilename='mod09_'+rnames[r]+"_"+str(year)+"_"+str(month)+".MOD09_"+str(year)+"_"+str(month)+".tif" |
|
62 |
unzippedfilename=output+".mod09.tif" |
|
68 | 63 |
|
69 | 64 |
# Check if file already exists and continue if so... |
70 |
if(os.path.exists(unzippedfilename)): |
|
71 |
print("File exists:"+filename) |
|
72 |
continue |
|
65 |
if(os.path.exists(unzippedfilename)): |
|
66 |
sys.exit("File exists:"+output) |
|
73 | 67 |
|
74 |
# MOD09 internal cloud flag for this year-month |
|
68 |
|
|
69 |
##################################################### |
|
70 |
# Processing Function |
|
71 |
# MOD09 internal cloud flag for this year-month |
|
75 | 72 |
# to filter by a date range: filterDate(datetime.datetime(yearstart,monthstart,1),datetime.datetime(yearstop,monthstop,31)) |
76 |
mod09 = ee.ImageCollection("MOD09GA").filter(ee.Filter.calendarRange(year,year,"year")).filter(ee.Filter.calendarRange(month,month,"month")).map(getmod09);
|
|
73 |
mod09 = ee.ImageCollection("MOD09GA").filter(ee.Filter.calendarRange(year,year,"year")).filter(ee.Filter.calendarRange(month,month,"month")).map(getmod09); |
|
77 | 74 |
# myd09 = ee.ImageCollection("MYD09GA").filter(ee.Filter.calendarRange(year,year,"year")).filter(ee.Filter.calendarRange(month,month,"month")).map(getmod09); |
78 | 75 |
# calculate mean cloudiness (%), rename band, multiply by 100, and convert to integer |
79 |
mod09a=mod09.mean().select([0], ['MOD09_'+str(year)+'_'+str(month)]).multiply(ee.Image(100)).byte(); |
|
80 |
# myd09a=myd09.mean().select([0], ['MYD09_'+str(year)+'_'+str(month)]).multiply(ee.Image(100)).byte(); |
|
81 |
|
|
82 |
``` |
|
76 |
mod09a=mod09.mean().select([0], ['mod09']).multiply(ee.Image(1000)).int16(); |
|
77 |
# myd09a=myd09.mean().select([0], ['MYD09_'+str(year)+'_'+str(month)]).multiply(ee.Image(100)).int8(); |
|
78 |
## Set data equal to whatver you want downloaded |
|
79 |
data=mod09a |
|
80 |
###################################################### |
|
81 |
|
|
82 |
## define region for download |
|
83 |
region=[ulx,lry], [ulx, uly], [lrx, uly], [lrx, lry] #h11v08 |
|
84 |
strregion=str(list(region)) |
|
83 | 85 |
# Next few lines for testing only |
84 | 86 |
# print info to confirm there is data |
85 |
mod09a.getInfo() |
|
86 |
myd09a.getInfo() |
|
87 |
#data.getInfo() |
|
88 |
|
|
89 |
## print a status update |
|
90 |
print(output+' Processing.... Coords:'+strregion) |
|
91 |
|
|
87 | 92 |
|
88 | 93 |
# add to plot to confirm it's working |
89 |
ee.mapclient.addToMap(mod09a, {'range': '0,100'}, 'MOD09') |
|
90 |
ee.mapclient.addToMap(myd09a, {'range': '0,100'}, 'MOD09') |
|
91 |
``` |
|
94 |
#ee.mapclient.addToMap(data, {'range': '0,100'}, 'MOD09') |
|
95 |
#``` |
|
96 |
|
|
97 |
# TODO: |
|
98 |
# use MODIS projection |
|
92 | 99 |
|
93 | 100 |
# build the URL and name the object (so that when it's unzipped we know what it is!) |
94 |
path =mod09a.getDownloadUrl({
|
|
95 |
'name': filename, # name the file (otherwise it will be a uninterpretable hash)
|
|
96 |
'scale': 1000, # resolution in meters
|
|
97 |
'crs': 'EPSG:4326', # MODIS sinusoidal
|
|
98 |
'region': regions[r] # region defined above
|
|
99 |
});
|
|
101 |
path =mod09a.getDownloadUrl({ |
|
102 |
'name': output, # name the file (otherwise it will be a uninterpretable hash)
|
|
103 |
'scale': 926, # resolution in meters
|
|
104 |
'crs': 'EPSG:4326', # projection
|
|
105 |
'region': strregion # region defined above
|
|
106 |
}); |
|
100 | 107 |
|
101 | 108 |
# Sometimes EE will serve a corrupt zipped file with no error |
102 | 109 |
# to check this, use a while loop that keeps going till there is an unzippable file. |
103 | 110 |
# This has the potential for an infinite loop... |
104 | 111 |
|
105 |
while(not(os.path.exists(filename+".zip"))): |
|
106 |
# download with wget |
|
107 |
print("Downloading "+filename) |
|
108 |
wget.download(path) |
|
112 |
#if(not(os.path.exists(output+".tif"))): |
|
113 |
# download with wget |
|
114 |
print("Downloading "+output) |
|
115 |
wget.download(path) |
|
116 |
#call(["wget"+path,shell=T]) |
|
109 | 117 |
# try to unzip it |
110 |
print("Unzipping "+filename)
|
|
111 |
zipstatus=call("unzip "+filename+".zip",shell=True)
|
|
118 |
print("Unzipping "+output)
|
|
119 |
zipstatus=call("unzip "+output+".zip",shell=True)
|
|
112 | 120 |
# if file doesn't exists or it didn't unzip, remove it and try again |
113 |
if(zipstatus==9): |
|
114 |
print("ERROR: "+filename+" unzip-able") |
|
115 |
os.remove(filename) |
|
116 |
|
|
117 |
print 'Finished' |
|
121 |
if(zipstatus==9): |
|
122 |
sys.exit("File exists:"+output) |
|
123 |
# print("ERROR: "+output+" unzip-able") |
|
124 |
# os.remove(output+".zip") |
|
125 |
|
|
126 |
## delete the zipped file (the unzipped version is kept) |
|
127 |
os.remove(output+".zip") |
|
128 |
|
|
129 |
print(output+' Finished!') |
|
118 | 130 |
|
119 | 131 |
|
climate/procedures/ee.MOD09_parallel.py | ||
---|---|---|
1 |
#!/usr/bin/env python |
|
2 |
|
|
3 |
## Example script that downloads data from Google Earth Engine using the python API |
|
4 |
## MODIS MOD09GA data is processed to extract the MOD09 cloud flag and calculate monthly cloud frequency |
|
5 |
|
|
6 |
## import some libraries |
|
7 |
import ee |
|
8 |
from ee import mapclient |
|
9 |
import ee.mapclient |
|
10 |
import datetime |
|
11 |
import wget |
|
12 |
import os |
|
13 |
import sys |
|
14 |
|
|
15 |
def Usage(): |
|
16 |
print('Usage: ee.MOD9.py -projwin ulx uly lrx lry -o output ') |
|
17 |
sys.exit( 1 ) |
|
18 |
|
|
19 |
ulx = float(sys.argv[2]) |
|
20 |
uly = float(sys.argv[3]) |
|
21 |
lrx = float(sys.argv[4]) |
|
22 |
lry = float(sys.argv[5]) |
|
23 |
output = sys.argv[7] |
|
24 |
|
|
25 |
print ( output , ulx ,uly ,lrx , lry ) |
|
26 |
|
|
27 |
#import logging |
|
28 |
|
|
29 |
MY_SERVICE_ACCOUNT = '364044830827-ubb6ja607b8j7t8m9uooi4c01vgah4ms@developer.gserviceaccount.com' # replace with your service account |
|
30 |
MY_PRIVATE_KEY_FILE = '/home/selv/GEE/fe3f13d90031e3eedaa9974baa6994e467b828f7-privatekey.p12' # replace with you private key file path |
|
31 |
ee.Initialize(ee.ServiceAccountCredentials(MY_SERVICE_ACCOUNT, MY_PRIVATE_KEY_FILE)) |
|
32 |
|
|
33 |
#/////////////////////////////////// |
|
34 |
#// Function to extract cloud flags |
|
35 |
def getmod09(img): return(img.select(['state_1km']).expression("((b(0)/1024)%2)")); |
|
36 |
|
|
37 |
## set a year-month if you don't want to run the loop (for testing) |
|
38 |
year=2001 |
|
39 |
month=1 |
|
40 |
|
|
41 |
print('Processing '+str(year)+'_'+str(month)) |
|
42 |
|
|
43 |
## MOD09 internal cloud flag for this year-month |
|
44 |
## to filter by a date range: filterDate(datetime.datetime(yearstart,monthstart,1),datetime.datetime(yearstop,monthstop,31)) |
|
45 |
mod09 = ee.ImageCollection("MOD09GA").filter(ee.Filter.calendarRange(year,year,"year")).filter(ee.Filter.calendarRange(month,month,"month")).map(getmod09); |
|
46 |
|
|
47 |
## calculate mean cloudiness (%), rename band, multiply by 100, and convert to integer |
|
48 |
mod09a=mod09.mean().select([0], ['MOD09_'+str(year)+'_'+str(month)]).multiply(ee.Image(100)).byte(); |
|
49 |
|
|
50 |
## print info to confirm there is data |
|
51 |
# mod09a.getInfo() |
|
52 |
|
|
53 |
## define region for download |
|
54 |
region=[ulx,lry ], [ulx, uly], [lrx, uly], [lrx, lry] #h11v08 |
|
55 |
strregion=str(list(region)) |
|
56 |
|
|
57 |
## Define tiles |
|
58 |
region='[[-72, -1], [-72, 11], [-59, 11], [-59, -1]]' |
|
59 |
## build the URL and name the object (so that when it's unzipped we know what it is!) |
|
60 |
path =mod09a.getDownloadUrl({ |
|
61 |
'name': output, # name the file (otherwise it will be a uninterpretable hash) |
|
62 |
'scale': 926, # resolution in meters |
|
63 |
'crs': 'EPSG:4326', # MODIS sinusoidal |
|
64 |
'region': strregion # region defined above |
|
65 |
}); |
|
66 |
|
|
67 |
## download with wget |
|
68 |
wget.download(path) |
|
69 |
|
|
70 |
|
|
71 |
|
climate/procedures/ee_compile.R | ||
---|---|---|
10 | 10 |
tempdir="tmp" |
11 | 11 |
if(!file.exists(tempdir)) dir.create(tempdir) |
12 | 12 |
|
13 |
|
|
14 |
## Load list of tiles |
|
15 |
tiles=read.table("tile_lat_long_10d.txt",header=T) |
|
16 |
|
|
17 |
jobs=expand.grid(tile=tiles$Tile,year=2000:2012,month=1:12) |
|
18 |
jobs[,c("ULX","ULY","LRX","LRY")]=tiles[match(jobs$tile,tiles$Tile),c("ULX","ULY","LRX","LRY")] |
|
19 |
|
|
20 |
## Run the python downloading script |
|
21 |
#system("~/acrobates/adamw/projects/environmental-layers/climate/procedures/ee.MOD09.py -projwin -159 20 -154.5 18.5 -year 2001 -month 6 -region test") |
|
22 |
i=6715 |
|
23 |
testtiles=c("h02v07","h02v06","h02v08","h03v07","h03v06") |
|
24 |
todo=which(jobs$tile%in%testtiles) |
|
25 |
#todo=todo[1:3] |
|
26 |
#todo=1:nrow(jobs) |
|
27 |
lapply(todo,function(i) |
|
28 |
system(paste("~/acrobates/adamw/projects/environmental-layers/climate/procedures/ee.MOD09.py -projwin ",jobs$ULX[i]," ",jobs$ULY[i]," ",jobs$LRX[i]," ",jobs$LRY[i], |
|
29 |
" -year ",jobs$year[i]," -month ",jobs$month[i]," -region ",jobs$tile[i],sep=""))) |
|
30 |
|
|
31 |
|
|
13 | 32 |
## Get list of available files |
14 | 33 |
df=data.frame(path=list.files("/mnt/data2/projects/cloud/mod09",pattern="*.tif$",full=T),stringsAsFactors=F) |
15 |
df[,c("region","year","month")]=do.call(rbind,strsplit(basename(df$path),"_|[.]"))[,c(2,3,4)]
|
|
34 |
df[,c("region","year","month")]=do.call(rbind,strsplit(basename(df$path),"_|[.]"))[,c(1,2,3)]
|
|
16 | 35 |
df$date=as.Date(paste(df$year,"_",df$month,"_15",sep=""),"%Y_%m_%d") |
17 | 36 |
|
18 |
table(df$year,df$month)#,df$region) |
|
37 |
## subset to testtiles? |
|
38 |
df=df[df$region%in%testtiles,] |
|
39 |
|
|
40 |
table(df$year,df$month) |
|
19 | 41 |
|
20 | 42 |
## drop some if not complete |
21 | 43 |
#df=df[df$year<=2009,] |
... | ... | |
29 | 51 |
ncfile=paste(tempdir,"/mod09_",date,".nc",sep="") |
30 | 52 |
if(!rerun&file.exists(ncfile)) next |
31 | 53 |
## merge regions to a new netcdf file |
32 |
system(paste("gdal_merge.py -o ",ncfile," -of netCDF -ot Byte ",paste(df$path[df$date==date],collapse=" ")))
|
|
54 |
system(paste("gdal_merge.py -o ",ncfile," -n -32768 -of netCDF -ot Int16 ",paste(df$path[df$date==date],collapse=" ")))
|
|
33 | 55 |
system(paste("ncecat -O -u time ",ncfile," ",ncfile,sep="")) |
34 | 56 |
## create temporary nc file with time information to append to MOD06 data |
35 | 57 |
cat(paste(" |
... | ... | |
49 | 71 |
## add other attributes |
50 | 72 |
system(paste("ncrename -v Band1,CF ",ncfile,sep="")) |
51 | 73 |
system(paste("ncatted ", |
52 |
" -a units,CF,o,c,\"Proportion Days Cloudy\" ", |
|
53 |
" -a valid_range,CF,o,b,\"0,100\" ", |
|
54 |
" -a long_name,CF,o,c,\"Proportion cloudy days (%)\" ",
|
|
55 |
ncfile,sep=""))
|
|
56 |
#" -a missing_value,CF,o,b,0 ",
|
|
57 |
#" -a _FillValue,CF,o,b,0 ", |
|
58 |
## add the fillvalue attribute back (without changing the actual values) |
|
59 |
#system(paste("ncatted -a _FillValue,CF,o,b,255 ",ncfile,sep=""))
|
|
74 |
" -a units,CF,o,c,\"Proportion Days Cloudy\" ",
|
|
75 |
# " -a valid_range,CF,o,b,\"0,100\" ",
|
|
76 |
" -a scale_factor,CF,o,f,\"0.1\" ",
|
|
77 |
" -a long_name,CF,o,c,\"Proportion cloudy days (%)\" ",
|
|
78 |
ncfile,sep=""))
|
|
79 |
|
|
80 |
## add the fillvalue attribute back (without changing the actual values)
|
|
81 |
#system(paste("ncatted -a _FillValue,CF,o,b,-32768 ",ncfile,sep=""))
|
|
60 | 82 |
|
61 | 83 |
if(as.numeric(system(paste("cdo -s ntime ",ncfile),intern=T))<1) { |
62 | 84 |
print(paste(ncfile," has no time, deleting")) |
Also available in: Unified diff
Updated EE script to run in parallel and minor edits to post-processing