Project

General

Profile

« Previous | Next » 

Revision 21d7e7c5

Added by Adam Wilson almost 11 years ago

Fixed month-sorting bug in ee_compile that resulted in data being attributed to the wrong month

View differences:

climate/research/cloud/MOD09/2_bias.R
5 5
library(foreach)
6 6
library(RcppOctave)
7 7
library(rgdal)
8
registerDoMC(15)
8
registerDoMC(12)
9 9

  
10 10

  
11 11
# final output will be written to data directory here:
......
141 141
    writeRaster(res2,file=toutfile,overwrite=T,datatype='INT2S',options=c("COMPRESS=LZW", "PREDICTOR=2"),NAvalue=-32768)
142 142
    ## remove temporary files
143 143
    rmr(d);rmr(d2);rmr(psi);rmr(res);rmr(res2)
144
    ## remove old temporary files older than x hours
145
    removeTmpFiles(1)
144 146
    print(paste("Finished Temporary File: ",toutfile))
145 147
}
146 148

  
......
153 155
foreach( i=1:nrow(df2)) %dopar% {
154 156
    ifile=df2$path[i]
155 157
    outfile=paste(datadir,"/mcd09ctif/",df2$sensor[i],"_",df2$month[i],".tif",sep="")
156
    if(file.exists(outfile)) next
158
    if(file.exists(outfile)) {print(paste(outfile," exists, moving on...")); return(NULL)}
157 159
    ## create VRT of first band of the full image 
158 160
    fvrt=sub("[.]tif",".vrt",ifile)
159 161
    system(paste("gdalbuildvrt -b 1 ",fvrt," ",ifile))
......
163 165
    system(paste("gdal_merge.py --config GDAL_CACHEMAX 10000 -init -32768 -n -32768 -co COMPRESS=LZW -co PREDICTOR=2 -co BIGTIFF=yes  -o ",outfile," ",tfiles,sep="")) 
164 166
    writeLines(paste("Finished ",outfile))
165 167
}
166

  
168
 
167 169

  
168 170

  
climate/research/cloud/MOD09/3_combine.R
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

  
climate/research/cloud/MOD09/ee/ee_compile.R
20 20
### Download files from google drive
21 21
## This only works if google-cli is installed and has already been authenticated 
22 22
download=T
23
if(download) system(paste("google docs get 2014*_g3_* ",datadir,"/mcd09ee",sep=""))
23
if(download) system(paste("google docs get 2014043_g3_* ",datadir,"/mcd09ee",sep=""))
24 24

  
25 25

  
26 26
##  Get list of available files
......
47 47
rerun=T  # set to true to recalculate all dates even if file already exists
48 48

  
49 49
## define month-sensors to process
50
jobs=unique(data.frame(month=df$month,sensor=df$sensor))
50
jobs=unique(data.frame(month=as.numeric(df$month),sensor=df$sensor))
51 51

  
52 52
i=1
53 53
#jobs=jobs[jobs$sensor=="MYD09",]
......
64 64
        s2=sub("GA","",s)
65 65

  
66 66
        ## Define output and check if it already exists
67
        tvrt=paste(tmpfs,"/",s2,"_",sprintf("%02d", m),".vrt",sep="")
68
        ttif1=paste(tmpfs,"/",s2,"_",sprintf("%02d", m),".tif",sep="")
67
        tvrt=paste(datadir,"/mcd09tif/",s2,"_",sprintf("%02d", m),".vrt",sep="")
68
        ttif1=paste(datadir,"/mcd09tif/",s2,"_",sprintf("%02d", m),"_uncompressed.tif",sep="")
69 69
        ttif2=paste(datadir,"/mcd09tif/",s2,"_",sprintf("%02d", m),".tif",sep="")
70 70

  
71 71
        ## check if output already exists
......
76 76
        ## Merge to geotif in temporary directory
77 77
        ## specify sourc projection because it gts it slightly wrong by default #-ot Int16 -dstnodata -32768
78 78
        ops=paste("-s_srs ",proj,"  -t_srs 'EPSG:4326' -multi -srcnodata -32768  -ot Int16 -dstnodata -32768 -r bilinear -te -180 -90 180 90 -tr 0.008333333333333 -0.008333333333333",
79
            "-co BIGTIFF=YES --config GDAL_CACHEMAX 500 -wm 500 -wo NUM_THREADS:10 -co COMPRESS=LZW -co PREDICTOR=2")
79
            "-co BIGTIFF=YES --config GDAL_CACHEMAX 2000 -wm 2000 -wo NUM_THREADS:10 -co COMPRESS=LZW -co PREDICTOR=2")
80
        ## if file exists, remove it avoid warping into existing file
81
        if(file.exists(ttif1)) file.remove(ttif1)
82
        ## run the warp
80 83
        system(paste("gdalwarp -overwrite ",ops," ",tvrt," ",ttif1))
81 84

  
82 85
        ## Compress file and add metadata tags

Also available in: Unified diff