Project

General

Profile

« Previous | Next » 

Revision 35ea6570

Added by Adam Wilson almost 11 years ago

adding initial bias correction function

View differences:

climate/research/cloud/MOD09/ee/ee_compile.R
9 9
setwd("~/acrobates/adamw/projects/cloud")
10 10

  
11 11
##  Get list of available files
12
df=data.frame(path=list.files("data/mod09cloud",pattern="*.tif$",full=T,recur=T),stringsAsFactors=F)
13
df[,c("month")]=do.call(rbind,strsplit(basename(df$path),"_|[.]|-"))[,c(6)]
12
df=data.frame(path=list.files("data/mcd09",pattern="*.tif$",full=T,recur=T),stringsAsFactors=F)
13
df[,c("month","sensor")]=do.call(rbind,strsplit(basename(df$path),"_|[.]|-"))[,c(5,4)]
14 14
df$date=as.Date(paste(2013,"_",df$month,"_15",sep=""),"%Y_%m_%d")
15 15

  
16

  
16 17
## use ramdisk?
17 18
tmpfs=tempdir()
18 19

  
......
24 25
    tmpfs="/mnt/ram"
25 26
}
26 27

  
27
#i=2
28
for(i in 1:nrow(df)){
29
## Define output and check if it already exists
30
    temptffile=paste(tmpfs,"/modcf_",df$month[i],".tif",sep="")
31
    tffile=paste("data/mod09cloud2/modcf_",df$month[i],".tif",sep="")
32
    ncfile=paste("data/mod09cloud2/modcf_",df$month[i],".nc",sep="")
33

  
34
                                        #    if(file.exists(tffile)) next
35
    ## warp to wgs84
36
    ops=paste("-t_srs 'EPSG:4326' -multi -r bilinear -te -180 -90 180 90 -tr 0.008333333333333 -0.008333333333333",
37
        "-co BIGTIFF=YES -ot Byte --config GDAL_CACHEMAX 300000 -wm 300000 -wo NUM_THREADS:10 -wo SOURCE_EXTRA=5")
38
    system(paste("gdalwarp -overwrite ",ops," ",df$path[i]," ",temptffile))
39
    ## update metadata
40
    tags=c(paste("TIFFTAG_IMAGEDESCRIPTION='Cloud Frequency extracted from Collection 5 MYD09GA and MOD09GA (PGE11) internal cloud mask algorithm (embedded in M*D09GA state_1km bit 10).",
41
        " Band 1 represents the overall 2000-2013 mean cloud frequency for month ",df$month[i],
42
        ".  Band 2 is the four times the standard deviation of the mean monthly cloud frequencies over 2000-2013.'",sep=""),
43
        "TIFFTAG_DOCUMENTNAME='MODCF: MODIS Cloud Frequency'",
44
        paste("TIFFTAG_DATETIME='2013-",df$month[i],"-15'",sep=""),
45
        "TIFFTAG_ARTIST='Adam M. Wilson (adam.wilson@yale.edu)'")
46
    ## compress
47
#    trans_ops=paste(" -co COMPRESS=LZW -stats -co BLOCKXSIZE=256 -co BLOCKYSIZE=256 -co PREDICTOR=2 -co FORMAT=NC4C")
48
#    system(paste("gdal_translate ",trans_ops," ",paste("-mo ",tags,sep="",collapse=" ")," ",temptffile," ",tffile," ",sep=""))
49

  
50
    trans_ops=paste(" -co COMPRESS=DEFLATE -stats -co PREDICTOR=2 -co FORMAT=NC4C -co ZLEVEL=9")
51
    system(paste("gdal_translate ",trans_ops," ",temptffile," ",ncfile," ",sep=""))
52
file.remove(temptffile)
53
}
54

  
55

  
56
if(ramdisk) {
57
    ## unmount the ram disk
58
    system(paste("sudo umount ",tmpfs)
59
}
60

  
61 28

  
62
rerun=F  # set to true to recalculate all dates even if file already exists
29
rerun=T  # set to true to recalculate all dates even if file already exists
63 30

  
64 31
    ## Loop over existing months to build composite netcdf files
65
    foreach(date=unique(df$date)) %dopar% {
32
    foreach(i=1:nrow(df)) %dopar% {
66 33
        ## get date
34
        date=df$date[i]
67 35
        print(date)
68 36
        ## Define output and check if it already exists
37
#        vrtfile=paste(tmpfs,"/modcf_",df$month[i],".vrt",sep="")
69 38
        temptffile=paste(tmpfs,"/modcf_",df$month[i],".tif",sep="")
39
        temptffile2=paste(tmpfs,"/modcf_",df$month[i],".tif",sep="")
70 40
        ncfile=paste("data/mod09cloud2/modcf_",df$month[i],".nc",sep="")
71 41
        ## check if output already exists
72 42
        if(!rerun&file.exists(ncfile)) return(NA)
43
        ## Develop mask
44
#        nObs=
45
#        system(paste("pksetmask -i ",temptffile," -m ",nObs," --operator='<' --msknodata 1 --nodata 255 --operator='>' --msknodata 10 --nodata 10 -o ",temptiffile2,sep=""))
73 46
        
74 47
        ## warp to wgs84
75 48
        ops=paste("-t_srs 'EPSG:4326' -multi -r bilinear -te -180 -90 180 90 -tr 0.008333333333333 -0.008333333333333",
......
136 109
        
137 110
    }
138 111

  
112
if(ramdisk) {
113
    ## unmount the ram disk
114
    system(paste("sudo umount ",tmpfs)
115
}
116

  
139 117

  
140 118
### merge all the tiles to a single global composite
141 119
#system(paste("ncdump -h ",list.files(tempdir,pattern="mod09.*.nc$",full=T)[10]))
climate/research/cloud/MOD09/ee_biastest.R
1
#### Evaluate the feasibility of correcting for orbital bias in cloud cover
2
library(raster)
3
library(rasterVis)
4
library(doMC)
5
library(multicore)
6
library(foreach)
7
library(rgdal)
8
registerDoMC(4)
9
library(plyr)
10
library(mgcv)
11
library(sampling)
12

  
13
setwd("~/acrobates/adamw/projects/cloud")
14

  
15
## set raster options
16
rasterOptions(maxmemory=1e8) 
17

  
18
## get CF data
19
##  Get list of available files
20
#df=data.frame(path=list.files("data/mod09cloud",pattern="*.tif$",full=T,recur=T),stringsAsFactors=F)
21
#df[,c("month")]=do.call(rbind,strsplit(basename(df$path),"_|[.]|-"))[,c(6)]
22
#df$date=as.Date(paste(2013,"_",df$month,"_15",sep=""),"%Y_%m_%d")
23
#df=df[order(df$date),]
24

  
25
#d=stack(df$path,bands=1)
26
#names(d)=df$month
27

  
28
reg=extent(c(-1434564.00523,1784892.95369, 564861.173869, 1880991.3772))
29
reg=extent(c(-2187230.72881, 4017838.07688,  -339907.592509, 3589340.63805))  #all sahara
30
reg=extent(c(-12020769.9608, 10201058.231,  -3682105.25271, 3649806.08382))  #large equitorial
31
#reg=extent(c(-151270.082307, 557321.958761, 1246351.8356, 1733123.75947))
32

  
33
d=crop(stack("data/mcd09/2014113_combined_20002013_MOD09_1-img-0000000000-0000000000.tif"),reg)
34
names(d)=c("cf","cfsd","nobs","pobs")
35
#obs=crop(raster("data/mcd09/2014113_combined_20002013_MOD09_1-img-0000000000-0000000000.tif",band=4),reg)
36
#levelplot(stack(obs,d))
37

  
38
#plot(obs,d)
39

  
40
#pts=sampleStratified(d[["pobs"]], size=10000, exp=10, na.rm=TRUE, xy=FALSE, ext=NULL, sp=FALSE)
41
pts=sampleRandom(d, size=10000, na.rm=TRUE, xy=T, ext=NULL, sp=T)
42
pts=pts[pts$nobs>0,]  #drop data from missing tiles
43

  
44
#dt=data.frame(cf=as.numeric(values(d[["cf"]])),obs=as.numeric(values(d[["pobs"]])))
45
#dt[,c("x","y")]=coordinates(d)
46
#dt=dt[dt$obs>0,]  #drop data from missing tiles
47

  
48
#s=1:nrow(dt)
49
#s=sample(1:nrow(dt),10000)
50
#s=strata(dt, stratanames=dt$obs, size=1000)
51

  
52
lm1=bam(cf~s(x,y,pobs),data=pts@data)
53
summary(lm1)
54
beta=coef(lm1)["pobs"]; beta
55

  
56
#lm1=bam(as.vector(d[["cf"]]) ~ as.vector(d[["pobs"]]))
57
#fun <- function(x) { bam(x[["cf"]] ~ x[["pobs"]])$coefficients["obs"] }
58
#bias <- calc(d, fun)
59

  
60
#pred=interpolate(d,lm1)
61

  
62
getbias=function(x) return((100-x)*beta)
63

  
64

  
65
getbias(beta,87)
66

  
67

  
68
bias=calc(d[["pobs"]],getbias,file=paste("data/bias/bias.tif",sep=""),format="GTiff",dataType="INT1U",overwrite=T,NAflag=255)
69

  
70
dc=overlay(d[["cf"]],bias,fun=function(x,y) x+y,
71
    file=paste("data/bias/biasc.tif",sep=""),
72
    format="GTiff",dataType="INT1U",overwrite=T,NAflag=255) #,options=c("COMPRESS=LZW","ZLEVEL=9"))
73

  
74

  
75
#levelplot(stack(d,dc))
76

  
77

  
78
library(multicore)
79

  
80
Mode <- function(x) {
81
      ux <- unique(x)
82
        ux[which.max(tabulate(match(x, ux)))]
83
  }
84
#rasterOptions(maxmemory)#=50GB) 
85
## set up processing chunks
86
nrw=nrow(d)
87
nby=20
88
nrwg=seq(1,nrw,by=nby)
89
writeLines(paste("Processing ",length(nrwg)," groups and",nrw,"lines"))
90

  
91
## Parallel loop to conduct moving window analysis and quantify pixels with significant shifts across pp or lulc boundaries
92
output=mclapply(nrwg,function(ti){
93
      ## Extract focal areas
94
      ngb=c(51,101)
95
      vals_cf=getValuesFocal(d,ngb=ngb,row=ti,nrows=nby)
96
      vals_obs=getValuesFocal(obs,ngb=ngb,row=ti,nrows=nby)
97
      ## extract bias
98
      bias=raster(matrix(do.call(rbind,lapply(1:nrow(vals_cf),function(i) {
99
#          if(i%in%round(seq(1,nrow(vals_cf),len=100))) print(i)
100
          tobs=vals_obs[i,]  #vector of indices
101
          tval=vals_cf[i,]    # vector of values
102
          lm1=lm(tval~tobs,na.rm=T)
103
          dif=round(predict(lm1)-predict(lm1,newdata=data.frame(tobs=median(tobs,na.rm=T))))
104
          return(dif)  
105
            })),nrow=nby,ncol=ncol(d),byrow=T))     # turn it back into a raster
106
        ## update raster and write it
107
        extent(bias)=extent(d[ti:(ti+nby-1),1:ncol(d),drop=F])
108
        projection(bias)=projection(d)
109
        NAvalue(bias) <- 255
110
        writeRaster(bias,file=paste("data/bias/tiles/bias_",ti,".tif",sep=""),
111
                                  format="GTiff",dataType="INT1U",overwrite=T,NAflag=255) #,options=c("COMPRESS=LZW","ZLEVEL=9")
112
    print(ti)
113
  }
114
)
115

  
116
#levelplot(d)
117
#plot(obs)
118

  
119
  system("gdalbuildvrt -srcnodata 255 -vrtnodata 255 data/bias/bias.vrt `find data/bias/tiles -name 'bias*tif'` ")
120
  system("gdalwarp -srcnodata 255 `find data/bias/tiles -name 'bias*tif'` data/bias/bias_movingwindow.tif ")
121

  
122
n=5000
123
ps=SpatialPoints(cbind(lon=runif(n,-180,10),lat=runif(n,-30,30)))
124
ps=SpatialPoints(cbind(lon=runif(n,-10,10),lat=runif(n,-5,30)))
125

  
126
projection(ps)=projection(d)
127

  
128
psd=extract(d,ps,sp=F)[,12]
129
psd=cbind.data.frame(cf=psd,pobs=extract(obs,ps,sp=F))
130
psd$cf[psd$cf<0]=NA
131
ps2=SpatialPointsDataFrame(ps,data=psd)
132

  
133
writeOGR(ps2,"output","MODIS_ObservationValidation",driver="ESRI Shapefile")
134

  
135

  
136
ps=readOGR("output","MODIS_ObservationValidation")
137

  
138
xyplot(cf~pobs,data=ps@data[!is.na(ps$cf)&ps$pobs>50,])
139

  
140
keep=T
141
keep=ps$cf<12&ps$pobs>80
142
lm1=lm(cf~pobs,data=ps@data[keep,])
143
summary(lm1)
144

  
145

  
146
pred=predict(lm1,newdata=ps@data[keep,],type="response")
147

  
148
plot(cf~pobs,data=ps@data[keep,],xlim=c(78,100))
149
points(pred~ps$pobs[keep],col="red")
150

  
151

  
152

  
153
#### fiddle with binomial
154
n=11
155
p=seq(0,1,len=n)
156
obs=1:30
157

  
158
mat=expand.grid(p=p,obs=obs)
159
mat$obsh=apply(sapply(1:14,function(x) rbinom(nrow(mat),size=mat$obs,prob=mat$p)),1,mean)
160
mat$ph=mat$obsh/mat$obs
161
mat$bias=mat$ph-mat$p
162

  
163
####  Simulate clouds
164
n=10
165
p=seq(0,1,len=n)
166
obs=10:30
167

  
168
## true clouds
169
mat=expand.grid(p=p,obs=obs)
170

  
171
res=matrix(nrow=nrow(mat),ncol=1000)
172
for(r in 1:1000){
173
    clouds=sapply(1:30,function(x) rbinom(nrow(mat),size=1,prob=mat$p))
174
    for(i in 1:nrow(mat)){
175
        res[i,r]=
176
            mean(sample(clouds[i,],mat$obs[i]))
177
    }
178
    
179
}
180

  
181
mat$ph=apply(res,1,mean)
182
                                        #mat$ph=mat$obsh/mat$obs
183
mat$bias=mat$ph-mat$p
184

  
185

  
186
levelplot(bias~p*obs,data=mat,col.regions=rainbow(100,end=0.8),at=seq(min(mat$bias),max(mat$bias),len=n),ylab="Observations in a 30-day month",xlab="'True'  Cloud Frequency (%) ")
187

  
188
hist(mat$bias)
189
plot(mat$p~mat$ph)
190
summary(mat)

Also available in: Unified diff