4 |
4 |
datadir="/mnt/data2/projects/cloud/"
|
5 |
5 |
|
6 |
6 |
# Create combined (MOD+MYD) corrected mean CF
|
7 |
|
foreach(i=1:2) %dopar% {
|
8 |
|
|
|
7 |
foreach(i=1:12) %dopar% {
|
9 |
8 |
f=list.files(paste(datadir,"/mcd09ctif",sep=""),pattern=paste(".*[O|Y].*_",sprintf("%02d",i),"[.]tif$",sep=""),full=T)
|
10 |
9 |
## Define output and check if it already exists
|
11 |
10 |
tmcd=paste(datadir,"/mcd09ctif/MCD09_",sprintf("%02d", i),"_uncompressed.tif",sep="")
|
12 |
11 |
tmcd2=paste(datadir,"/mcd09ctif/MCD09_",sprintf("%02d", i),".tif",sep="")
|
13 |
12 |
## check if output already exists
|
14 |
|
ops=paste("-t_srs 'EPSG:4326' -multi -srcnodata -32768 -dstnodata -32768 -r bilinear -te -180 -90 180 90 -tr 0.008333333333333 -0.008333333333333",
|
15 |
|
"-co BIGTIFF=YES --config GDAL_CACHEMAX 500 -wm 500 -wo NUM_THREADS:10 -wo SOURCE_EXTRA=5")
|
|
13 |
# if(file.exists(tmcd2)){print(paste(tmcd2,"Exists, moving on..."));return(NULL)}
|
|
14 |
## Take average between images
|
|
15 |
## switch NA values to 32768 to facilitate recasting to 8-bit below, otherwise they are confounded with 0 cloud values
|
|
16 |
ops=paste("-t_srs 'EPSG:4326' -multi -srcnodata -32768 -dstnodata 32767 -r bilinear -te -180 -90 180 90 -tr 0.008333333333333 -0.008333333333333",
|
|
17 |
"-co BIGTIFF=YES --config GDAL_CACHEMAX 20000 -wm 2000 -wo NUM_THREADS:10 -wo SOURCE_EXTRA=5")
|
16 |
18 |
system(paste("gdalwarp -overwrite -r average -co COMPRESS=LZW -co ZLEVEL=9 ",ops," ",paste(f,collapse=" ")," ",tmcd))
|
17 |
19 |
## update metadata
|
18 |
20 |
tags=c(paste("TIFFTAG_IMAGEDESCRIPTION='Monthly Cloud Frequency for 2000-2013 extracted from C5 MODIS MOD09GA and MYD09GA PGE11 internal cloud mask algorithm (embedded in state_1km bit 10).",
|
19 |
|
"The daily cloud mask time series were summarized to mean cloud frequency (CF) by calculating the proportion of cloudy days. ",
|
20 |
|
"Band Descriptions: 1) Mean Monthly Cloud Frequency 2) Four Times the Standard Deviation of Mean Monthly Cloud Frequency",
|
21 |
|
" 3) Mean number of daily observations for each pixel 4) Proportion of days with at least one observation '"),
|
|
21 |
"The daily cloud mask time series were summarized to mean cloud frequency (CF) by calculating the proportion of cloudy days.'"),
|
22 |
22 |
"TIFFTAG_DOCUMENTNAME='Collection 5 MCD09 Cloud Frequency'",
|
23 |
23 |
paste("TIFFTAG_DATETIME='2013",sprintf("%02d", i),"15'",sep=""),
|
24 |
24 |
"TIFFTAG_ARTIST='Adam M. Wilson (adam.wilson@yale.edu)'")
|
25 |
25 |
system(paste("/usr/local/src/gdal-1.10.0/swig/python/scripts/gdal_edit.py ",tmcd," ",paste("-mo ",tags,sep="",collapse=" "),sep=""))
|
26 |
26 |
# create final fixed image
|
27 |
|
system(paste("gdal_translate -a_nodata -32768 -co COMPRESS=LZW -co ZLEVEL=9 -co PREDICTOR=2 ",tmcd," ",tmcd2,sep=""))
|
|
27 |
system(paste("gdal_translate -co COMPRESS=LZW -co ZLEVEL=9 -co PREDICTOR=2 ",tmcd," ",tmcd2,sep=""))
|
|
28 |
writeLines(paste("Finished month",i))
|
|
29 |
}
|
|
30 |
|
|
31 |
################################################
|
|
32 |
# Create combined (MOD+MYD) corrected mean CF SD
|
|
33 |
foreach(i=1:12) %dopar% {
|
|
34 |
f=list.files(paste(datadir,"/mcd09tif",sep=""),pattern=paste(".*[O|Y].*_",sprintf("%02d",i),"[.]tif$",sep=""),full=T)
|
|
35 |
## Define output and check if it already exists
|
|
36 |
tmcd=paste(datadir,"/mcd09ctif/MCD09sd_",sprintf("%02d", i),"_uncompressed.tif",sep="")
|
|
37 |
tmcd2=paste(datadir,"/mcd09ctif/MCD09sd_",sprintf("%02d", i),".tif",sep="")
|
|
38 |
## check if output already exists
|
|
39 |
if(file.exists(tmcd2)){print(paste(tmcd2,"Exists, moving on..."));return(NULL)}
|
|
40 |
## Take average between images
|
|
41 |
ops=paste("-t_srs 'EPSG:4326' -multi -srcnodata -32768 -dstnodata -32768 -r bilinear -te -180 -90 180 90 -tr 0.008333333333333 -0.008333333333333",
|
|
42 |
"-co BIGTIFF=YES --config GDAL_CACHEMAX 20000 -wm 2000 -wo NUM_THREADS:10 -wo SOURCE_EXTRA=5")
|
|
43 |
system(paste("gdalwarp -overwrite -r average -co COMPRESS=LZW -co ZLEVEL=9 ",ops," ",paste(f,collapse=" ")," ",tmcd))
|
|
44 |
## update metadata
|
|
45 |
tags=c(paste("TIFFTAG_IMAGEDESCRIPTION='Standard Deviation of the Monthly Cloud Frequency for 2000-2013 extracted from C5 MODIS",
|
|
46 |
" MOD09GA and MYD09GA PGE11 internal cloud mask algorithm (embedded in state_1km bit 10).",
|
|
47 |
"The daily cloud mask time series were summarized to mean cloud frequency (CF) by calculating the proportion of cloudy days"),
|
|
48 |
"TIFFTAG_DOCUMENTNAME='Collection 5 MCD09 SD of Cloud Frequency'",
|
|
49 |
paste("TIFFTAG_DATETIME='2013",sprintf("%02d", i),"15'",sep=""),
|
|
50 |
"TIFFTAG_ARTIST='Adam M. Wilson (adam.wilson@yale.edu)'")
|
|
51 |
system(paste("/usr/local/src/gdal-1.10.0/swig/python/scripts/gdal_edit.py ",tmcd," ",paste("-mo ",tags,sep="",collapse=" "),sep=""))
|
|
52 |
# create final fixed image
|
|
53 |
system(paste("gdal_translate -b 2 -a_nodata -32768 -co COMPRESS=LZW -co ZLEVEL=9 -co PREDICTOR=2 ",tmcd," ",tmcd2,sep=""))
|
28 |
54 |
writeLines(paste("Finished month",i))
|
29 |
55 |
}
|
30 |
56 |
|
... | ... | |
46 |
72 |
hd=c("<ColorInterp>Palette</ColorInterp>","<ColorTable>")
|
47 |
73 |
ft="</ColorTable>"
|
48 |
74 |
colR=colorRampPalette(c("#08306b","#0d57a1","#2878b8","#4997c9","#72b2d7","#a2cbe2","#c7dcef","#deebf7","#f7fbff"))
|
49 |
|
cols=data.frame(t(col2rgb(colR(101))))
|
|
75 |
cols=data.frame(t(col2rgb(colR(105))))
|
50 |
76 |
ct=paste("<Entry c1=\"",cols$red,"\" c2=\"",cols$green,"\" c3=\"",cols$blue,"\" c4=\"255\"/>")
|
51 |
77 |
cti=grep("ColorInterp",vrt) # get index of current color table
|
52 |
78 |
vrt2=c(vrt[1:(cti-1)],hd,ct,ft,vrt[(cti+1):length(vrt)])
|
53 |
79 |
## update missing data flag following http://lists.osgeo.org/pipermail/gdal-dev/2010-February/023541.html
|
54 |
80 |
csi=grep("<ComplexSource>",vrt2) # get index of current color table
|
55 |
|
vrt2=c(vrt2[1:csi],"<NODATA>-327.68</NODATA>",vrt2[(csi+1):length(vrt2)])
|
|
81 |
vrt2=c(vrt2[1:csi],"<NODATA>327</NODATA>",vrt2[(csi+1):length(vrt2)])
|
56 |
82 |
write.table(vrt2,file=outfilevrt,col.names=F,row.names=F,quote=F)
|
57 |
83 |
|
58 |
84 |
#system(paste("gdal_translate -of VRT -scale 0 10000 0 100 ",outfilevrt," ",outfilevrt2))
|
59 |
|
|
60 |
|
## convert to 8-bit compressed file
|
61 |
|
## update vrt to correctly handle missing data
|
62 |
|
#vrt3=scan(outfilevrt2,what="char")
|
63 |
|
#csi=grep("<ComplexSource>",vrt3) # get index of current color table
|
64 |
|
#vrt3=c(vrt[1:csi],"<NODATA>-32768</NODATA>",vrt[(csi+1):length(vrt)])
|
65 |
|
#write.table(vrt3,file=outfilevrt2,col.names=F,row.names=F,quote=F)
|
|
85 |
|
66 |
86 |
|
67 |
87 |
# system(paste("pkreclass -i ",outfilevrt," ",paste("-mo ",tags,sep="",collapse=" ")," ",outfilevrt," ",outfile2))
|
68 |
88 |
tags=c(paste("TIFFTAG_IMAGEDESCRIPTION='Monthly Cloud Frequency for 2000-2013 extracted from C5 MODIS M*D09GA PGE11 internal cloud mask algorithm (embedded in state_1km bit 10).",
|
... | ... | |
71 |
91 |
"TIFFTAG_DOCUMENTNAME='Collection 5 Cloud Frequency'",
|
72 |
92 |
paste("TIFFTAG_DATETIME='2014'",sep=""),
|
73 |
93 |
"TIFFTAG_ARTIST='Adam M. Wilson (adam.wilson@yale.edu)'")
|
74 |
|
system(paste("gdal_translate -ot Byte -co COMPRESS=LZW -co PREDICTOR=2 ",paste("-mo ",tags,sep="",collapse=" ")," ",outfilevrt," ",outfile))
|
|
94 |
system(paste("gdal_translate -a_nodata 255 -ot Byte -co COMPRESS=LZW -co PREDICTOR=2 ",paste("-mo ",tags,sep="",collapse=" ")," ",outfilevrt," ",outfile))
|
75 |
95 |
|
76 |
|
## Convert SD to 8-bit
|
77 |
|
system(paste("gdal_translate -b 2 ",ops," ",paste("-mo ",tags,sep="",collapse=" ")," ",file," ",outfilesd))
|
78 |
96 |
}
|
79 |
97 |
|
80 |
98 |
|
Fixed month-sorting bug in ee_compile that resulted in data being attributed to the wrong month