Project

General

Profile

« Previous | Next » 

Revision 710f7456

Added by Adam Wilson over 11 years ago

Added initial validation via NDP-026D dataset

View differences:

climate/procedures/MOD35_Explore.r
2 2
setwd("~/acrobates/projects/interp/data/modis/mod35")
3 3

  
4 4
library(raster)
5
library(rasterVis)
5 6
library(rgdal)
6 7

  
7
f=list.files(pattern="*.hdf")
8
#f=list.files(pattern="*.hdf")
8 9

  
9
Sys.setenv(GEOL_AS_GCPS = "PARTIAL")
10
#Sys.setenv(GEOL_AS_GCPS = "PARTIAL")
11
## try swath-grid with gdal
12
#GDALinfo(f[1])
13
#system(paste("gdalinfo",f[2]))
14
#GDALinfo("HDF4_EOS:EOS_SWATH:\"MOD35_L2.A2000100.1445.006.2012252024758.hdf\":mod35:Cloud_Mask")
15
#system("gdalinfo HDF4_EOS:EOS_SWATH:\"data/modis/mod35/MOD35_L2.A2000100.1445.006.2012252024758.hdf\":mod35:Cloud_Mask")
16
#system("gdalwarp -overwrite -geoloc -order 2 -r near -s_srs \"EPSG:4326\" HDF4_EOS:EOS_SWATH:\"MOD35_L2.A2000100.1445.006.2012252024758.hdf\":mod35:Cloud_Mask cloudmask.tif")
17
#system("gdalwarp -overwrite -r near -s_srs \"EPSG:4326\" HDF4_EOS:EOS_SWATH:\"MOD35_L2.A2000100.1445.006.2012252024758.hdf\":mod35:Cloud_Mask:1 cloudmask2.tif")
18
#r=raster(f[1])
19
#extent(r)
20
#st=lapply(f[1:10],raster)
21
#str=lapply(2:length(st),function(i) union(extent(st[[i-1]]),extent(st[[i]])))[[length(st)-1]]
22
#str=union(extent(h11v08),str)
23
#b1=brick(lapply(st,function(stt) {
24
#  x=crop(alignExtent(stt,str),h11v08)
25
#  return(x)
26
#}))
27
#c=brick(f[1:10])
10 28

  
11
GDALinfo(f[1])
12
system(paste("gdalinfo",f[1]))
13
GDALinfo("HDF4_EOS:EOS_SWATH:\"MOD35_L2.A2000100.1445.006.2012252024758.hdf\":mod35:Cloud_Mask")
14
system("gdalinfo HDF4_EOS:EOS_SWATH:\"MOD35_L2.A2000100.1445.006.2012252024758.hdf\":mod35:Cloud_Mask | tail -n 200")
29
## get % cloudy
30
v5=stack(brick("../mod06/summary/MOD06_h11v08_ymoncld01.nc",varname="CLD01"))
31
projection(v5)="+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs"
32
v6=stack(brick("MOD35_h11v08.nc",varname="CLD01"))
33
projection(v6)="+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs"
15 34

  
16
system("gdalwarp -overwrite -geoloc -order 2 -r near -s_srs \"EPSG:4326\" HDF4_EOS:EOS_SWATH:\"MOD35_L2.A2000100.1445.006.2012252024758.hdf\":mod35:Cloud_Mask cloudmask.tif")
17
system("gdalwarp -overwrite -r near -s_srs \"EPSG:4326\" HDF4_EOS:EOS_SWATH:\"MOD35_L2.A2000100.1445.006.2012252024758.hdf\":mod35:Cloud_Mask:1 cloudmask2.tif")
35
## generate means
36
v6m=mean(v6)
37
v5m=mean(v5)
18 38

  
19 39

  
20
## get tile
21
tile=raster("~/acrobates/projects/interp/data/modis/mod06/summary/MOD06_h09v04.nc",varname="CER")
22
h11v08=extent(tile)
40
## landcover
41
lulc=raster("~/acrobatesroot/Data/environ/global/landcover/MODIS/MCD12Q1_IGBP_2005_v51.tif")
42
projection(lulc)="+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs"
43
lulc=crop(lulc,v6)
23 44

  
24
r=raster(f[1])
25
extent(r)
45
Mode <- function(x,na.rm=T) {  #get MODE
46
  x=na.omit(x)
47
  ux <- unique(x)
48
      ux[which.max(tabulate(match(x, ux)))]
49
  }
50
## aggregate to 1km resolution
51
lulc2=aggregate(lulc,2,fun=function(x,na.rm=T) Mode(x))
52
## convert to factor table
53
lulcf=lulc2
54
ratify(lulcf)
55
levels(lulcf)[[1]]
56
lulc_levels=c("Water","Evergreen Needleleaf forest","Evergreen Broadleaf forest","Deciduous Needleleaf forest","Deciduous Broadleaf forest","Mixed forest","Closed shrublands","Open shrublands","Woody savannas","Savannas","Grasslands","Permanent wetlands","Croplands","Urban and built-up","Cropland/Natural vegetation mosaic","Snow and ice","Barren or sparsely vegetated")
57
lulc_levels2=c("Water","Forest","Forest","Forest","Forest","Forest","Shrublands","Shrublands","Savannas","Savannas","Grasslands","Permanent wetlands","Croplands","Urban and built-up","Cropland/Natural vegetation mosaic","Snow and ice","Barren or sparsely vegetated")
26 58

  
59
levels(lulcf)=list(data.frame(ID=0:16,LULC=lulc_levels,LULC2=lulc_levels2))
27 60

  
28
st=lapply(f[1:10],raster)
29
str=lapply(2:length(st),function(i) union(extent(st[[i-1]]),extent(st[[i]])))[[length(st)-1]]
30
str=union(extent(h11v08),str)
31 61

  
32
b1=brick(lapply(st,function(stt) {
33
  x=crop(alignExtent(stt,str),h11v08)
34
  return(x)
35
}))
36 62

  
63
### load WORLDCLIM elevation 
64
dem=raster("../../tiles/h11v08/dem_h11v08.tif",format="GTiff")
65
projection(dem)="+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs"
37 66

  
67
dif=v6-v5
68
names(dif)=month.name
38 69

  
39
c=brick(f[1:10])
70
difm=v6m-v5m
71

  
72
tile=extent(v6)
73

  
74
### compare differences between v5 and v6 by landcover type
75
lulcm=as.matrix(lulc)
76
forest=lulcm>=1&lulcm<=5
77

  
78

  
79
boxplot(cld)
80
splom(cld)
81

  
82

  
83
#####################################
84
### compare MOD43 and MOD17 products
85

  
86
## MOD17
87
mod17=raster("../MOD17/Npp_1km_C5.1_mean_00_to_06.tif",format="GTiff")
88
mod17=crop(projectRaster(mod17,v6,method="bilinear"),v6)
89
NAvalue(mod17)=32767
90

  
91
mod17qc=raster("../MOD17/Npp_QC_1km_C5.1_mean_00_to_06.tif",format="GTiff")
92
mod17qc=crop(projectRaster(mod17qc,v6,method="bilinear"),v6)
93
mod17qc[mod17qc<0|mod17qc>100]=NA
94

  
95
## MOD43 via earth engine
96
mod43=raster("../mod43/3b21aa90cc657523ff31e9559f36fb12.EVI_MEAN.tif",format="GTiff")
97
mod43=crop(projectRaster(mod43,v6,method="bilinear"),v6)
98

  
99
mod43qc=raster("../mod43/3b21aa90cc657523ff31e9559f36fb12.Percent_Cloudy.tif",format="GTiff")
100
mod43qc=crop(projectRaster(mod43qc,v6,method="bilinear"),v6)
101
mod43qc[mod43qc<0|mod43qc>100]=NA
102

  
103
## Summary plot of mod17 and mod43
104
modprod=stack(mod17qc,mod43qc)
105
names(modprod)=c("MOD17","MOD43")
106

  
107
n=100
108
at=seq(0,100,len=n)
109
cols=grey(seq(0,1,len=n))
110
cols=rainbow(n)
111
bgyr=colorRampPalette(c("blue","green","yellow","red"))
112
cols=bgyr(n)
113

  
114
#levelplot(lulcf,margin=F,layers="LULC")
115

  
116
m=3
117
mcompare=stack(subset(v5,m),subset(v6,m))
118

  
119
mdiff=subset(v5,m)-subset(v6,m)
120
names(mcompare)=c("Collection_5","Collection_6")
121
names(mdiff)=c("Collection_5-Collection_6")
122

  
123

  
124
pdf("output/mod35compare.pdf",width=11,height=8.5)
125

  
126
levelplot(mcompare,col.regions=cols,at=at,margin=F,sub="Frequency of MOD35 Clouds in March")
127
levelplot(dif,col.regions=bgyr(20),margin=F)
128
levelplot(mdiff,col.regions=bgyr(20),margin=F)
129

  
130

  
131
boxplot(as.matrix(subset(dif,subset=1))~forest,varwidth=T,notch=T);abline(h=0)
132

  
133
dev.off()
134

  
135

  
136
levelplot(modprod,main="Missing Data (%) in MOD17 (NPP) and MOD43 (BRDF Reflectance)",
137
          sub="Tile H11v08 (Venezuela)",col.regions=cols,at=at)
138

  
139
levelplot(modprod,main="Missing Data (%) in MOD17 (NPP) and MOD43 (BRDF Reflectance)",
140
          sub="Tile H11v08 (Venezuela)",col.regions=cols,at=at,
141
          xlim=c(-7300000,-6670000),ylim=c(0,600000))
142

  
143
levelplot(v5m,main="Missing Data (%) in MOD17 (NPP) and MOD43 (BRDF Reflectance)",
144
          sub="Tile H11v08 (Venezuela)",col.regions=cols,at=at,
145
          xlim=c(-7200000,-6670000),ylim=c(0,400000),margin=F)
146

  
147
levelplot(stack(v5m,v6m),main="Missing Data (%) in MOD17 (NPP) and MOD43 (BRDF Reflectance)",
148
          sub="Tile H11v08 (Venezuela)",col.regions=cols,at=at,
149
          xlim=c(-7200000,-6670000),ylim=c(0,400000),margin=F)
150

  
151
### smoothing plots
152
## explore smoothed version
153
td=subset(v6,m)
154
## build weight matrix
155
s=3
156
w=matrix(1/(s*s),nrow=s,ncol=s)
157
#w[s-1,s-1]=4/12; w
158
td2=focal(td,w=w)
159
td3=stack(td,td2)
160

  
161
levelplot(td3,col.regions=cols,at=at,margin=F)
162

  
163
dev.off()
164
plot(stack(difm,lulc))
165

  
166
### ROI
167
tile_ll=projectExtent(v6, "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
168

  
169
62,59
170
0,3
climate/procedures/NDP-026D.R
1 1
#! /bin/R
2 2
### Script to download and process the NDP-026D station cloud dataset
3
setwd("~/acrobates/projects/interp/data/NDP026D")
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

  
4 13

  
5 14
## available here http://cdiac.ornl.gov/epubs/ndp/ndp026d/ndp026d.html
6 15

  
......
13 22
st$lon=st$LON/100
14 23
st$lon[st$lon>180]=st$lon[st$lon>180]-360
15 24

  
16
## check a plot
17
plot(lat~lon,data=st,pch=16,cex=.5)
18

  
19

  
20
## get monthly mean cloud amount MMCA
25
## download data
21 26
system("wget -N -nd ftp://cdiac.ornl.gov/pub/ndp026d/cat67_78/* -A '.tc.Z' -P data/")
22 27
system("gunzip data/*.Z")
23 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")
24 32
#f121=c(6,6,6,7,6,7,6,2) #format 121
25 33
#c121=c("StaID","NobD","AvgDy","NobN","AvgNt","NobDN","AvgDN","Acode")
26
f162=c(5,5,4,7,7,7,4) #format 121
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
27 43
c162=c("StaID","YR","Nobs","Amt","Fq","AWP","NC")
28 44

  
29
cld=do.call(rbind.data.frame,lapply(sprintf("%02d",1:12),function(m) {
45
## use monthly timeseries
46
cld=do.call(rbind.data.frame,mclapply(sprintf("%02d",1:12),function(m) {
30 47
  d=read.fwf(list.files("data",pattern=paste("MNYDC.",m,".tc",sep=""),full=T),skip=1,widths=f162)
31 48
  colnames(d)=c162
32 49
  d$month=as.numeric(m)
50
  print(m)
33 51
  return(d)}
34 52
  ))
35 53

  
36
cld[,c("lat","lon")]=st[match(st$StaID,cld$StaID),c("lat","lon")]
54
## add lat/lon
55
cld[,c("lat","lon")]=st[match(cld$StaID,st$StaID),c("lat","lon")]
37 56

  
38 57
## drop missing values
39 58
cld$Amt[cld$Amt<0]=NA
40 59
cld$Fq[cld$Fq<0]=NA
41 60
cld$AWP[cld$AWP<0]=NA
42 61
cld$NC[cld$NC<0]=NA
62
cld=cld[cld$Nobs>0,]
43 63

  
44
## calculate means
64
## calculate means and sds
45 65
cldm=do.call(rbind.data.frame,by(cld,list(month=as.factor(cld$month),StaID=as.factor(cld$StaID)),function(x){
46
  data.frame(month=x$month[1],StaID=x$StaID[1],Amt=mean(x$Amt[x$Nobs>20],na.rm=T))}))
47
cldm[,c("lat","lon")]=st[match(st$StaID,cldm$StaID),c("lat","lon")]
48

  
49

  
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
#cldm=foreach(m=unique(cld$month),.combine='rbind')%:%
74
#  foreach(s=unique(cld$StaID),.combine="rbind") %dopar% {
75
#    x=cld[cld$month==m&cld$StaID==s,]
76
#    data.frame(
77
#               month=x$month[1],
78
#               StaID=x$StaID[1],
79
#               Amt=mean(x$Amt[x$Nobs>10],na.rm=T)/100)}
80
 
50 81

  
51 82
## write out the table
52 83
write.csv(cldm,file="cldm.csv")
......
56 87
###
57 88
cldm=read.csv("cldm.csv")
58 89

  
59
## add a color key
60
cldm$col=cut(cldm$Amt/100,quantile(cldm$Amt/100,seq(0,1,len=5),na.rm=T))
61 90

  
62
library(lattice)
63
xyplot(lat~lon|+month,groups=col,data=cldm,pch=16,cex=.2,auto.key=T)
91
##make spatial object
92
cldms=cldm
93
coordinates(cldms)=c("lon","lat")
94
projection(cldms)=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
95

  
96
#### Evaluate MOD35 Cloud data
97
mod35=brick("../modis/mod35/MOD35_h11v08.nc",varname="CLD01")
98
mod35sd=brick("../modis/mod35/MOD35_h11v08.nc",varname="CLD_sd")
99

  
100
projection(mod35)="+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs"
101
projection(mod35sd)="+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs"
64 102

  
103
cldms=spTransform(cldms,CRS(projection(mod35)))
104

  
105
mod35v=foreach(m=unique(cldm$month),.combine="rbind") %do% {
106
  dr=subset(mod35,subset=m);projection(dr)=projection(mod35)
107
  dr2=subset(mod35sd,subset=m);projection(dr2)=projection(mod35)
108
  ds=cldms[cldms$month==m,]
109
  ds$mod35=unlist(extract(dr,ds,buffer=10,fun=mean,na.rm=T))
110
#  ds$mod35sd=extract(dr2,ds,buffer=10)
111
  print(m)
112
  return(ds@data[!is.na(ds$mod35),])}
113

  
114
## month factors
115
cldm$month2=factor(cldm$month,labels=month.name)
116
## add a color key
117
breaks=seq(0,100,by=25)
118
cldm$cut=cut(cldm$cld,breaks)
119
cp=colorRampPalette(c("blue","orange","red"))
120
cols=cp(length(at))
121

  
122
## read in global coasts for nice plotting
123
library(maptools)
124

  
125
data(wrld_simpl)
126
coast <- unionSpatialPolygons(wrld_simpl, rep("land",nrow(wrld_simpl)), threshold=5)
127
coast=as(coast,"SpatialLines")
128
#coast=spTransform(coast,CRS(projection(mod35)))
129

  
130

  
131
## write a pdf
132
#dir.create("output")
133
pdf("output/NDP026d.pdf",width=11,height=8.5)
134

  
135
## map of stations
136
 xyplot(lat~lon,data=st,pch=16,cex=.5,col="black",auto.key=T,
137
       main="NDP-026D Cloud Climatology Stations",ylab="Latitude",xlab="Longitude")+
138
  layer(sp.lines(coast,col="grey"),under=T)
139

  
140
xyplot(lat~lon|month2,groups=cut,data=cldm,pch=".",cex=.2,auto.key=T,
141
       main="Mean Monthly Cloud Coverage",ylab="Latitude",xlab="Longitude",
142
        par.settings = list(superpose.symbol= list(pch=16,col=c("blue","green","yellow","red"))))+
143
  layer(sp.lines(coast,col="grey"),under=T)
144

  
145

  
146
## Validation
147
m=10
148
zlim=c(40,100)
149
dr=subset(mod35,subset=m);projection(dr)=projection(mod35)
150
ds=cldms[cldms$month==m,]
151
plot(dr,col=cp(100),zlim=zlim,main="Comparison of MOD35 Cloud Frequency and NDP-026D Station Cloud Climatologies",
152
     ylab="Northing (m)",xlab="Easting (m)",sub="MOD35 is proportion of cloudy days, while NDP-026D is Mean Cloud Coverage")
153
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))))
154
#legend("topright",legend=seq(zlim[1],zlim[2],len=5),pch=16,col=cp(length(breaks)))
155

  
156

  
157
xyplot(mod35~cld,data=mod35v,subscripts=T,auto.key=T,panel=function(x,y,subscripts){
158
   td=mod35v[subscripts,]
159
#   panel.segments(x-td$cldsd[subscripts],y,x+td$cldsd[subscripts],y,subscripts=subscripts)
160
   panel.xyplot(x,y,subscripts=subscripts,type=c("p","smooth"),pch=16,col="black")
161
#   panel.segments(x-td$cldsd[subscripts],y,x+td$cldsd[subscripts],y,subscripts=subscripts)
162
 },ylab="MOD35 Proportion Cloudy Days",xlab="NDP-026D Mean Monthly Cloud Amount",
163
        main="Comparison of MOD35 Cloud Mask and Station Cloud Climatologies")
164

  
165
#xyplot(mod35~cld|month,data=mod35v,subscripts=T,auto.key=T,panel=function(x,y,subscripts){
166
#   td=mod35v[subscripts,]
167
#   panel.segments(x-td$cldsd[subscripts],y,x+td$cldsd[subscripts],y,subscripts=subscripts)
168
#   panel.xyplot(x,y,subscripts=subscripts,type=c("p","smooth"),pch=16,col="black")
169
#   panel.segments(x-td$cldsd[subscripts],y,x+td$cldsd[subscripts],y,subscripts=subscripts)
170
# },ylab="MOD35 Proportion Cloudy Days",xlab="NDP-026D Mean Monthly Cloud Amount",
171
#        main="Comparison of MOD35 Cloud Mask and Station Cloud Climatologies")
172

  
173

  
174
dev.off()
175

  
176
graphics.off()

Also available in: Unified diff