1 |
99b3508d
|
Adam M. Wilson
|
############################
|
2 |
|
|
#### Extract MOD35 C6 processing path
|
3 |
|
|
|
4 |
d39ab57e
|
Adam M. Wilson
|
setwd("~/acrobates/adamw/projects/interp/data/modis/mod35/processpath")
|
5 |
99b3508d
|
Adam M. Wilson
|
library(multicore)
|
6 |
|
|
library(raster)
|
7 |
|
|
library(spgrass6)
|
8 |
ddd9a810
|
Adam M. Wilson
|
library(rgeos)
|
9 |
99b3508d
|
Adam M. Wilson
|
|
10 |
555815c9
|
Adam M. Wilson
|
#### Set up command for running swtif to grid and mosaic the swath data
|
11 |
|
|
stitch=paste("sudo MRTDATADIR=\"/usr/local/heg/2.12/data\" PGSHOME=/usr/local/heg/2.12/TOOLKIT_MTD PWD=/home/adamw /usr/local/heg/2.12b/bin/swtif")
|
12 |
|
|
|
13 |
|
|
## Link to MOD35 Swath data
|
14 |
99b3508d
|
Adam M. Wilson
|
url="ftp://ladsweb.nascom.nasa.gov/allData/51/MOD35_L2/2012/"
|
15 |
|
|
dir.create("swath")
|
16 |
|
|
|
17 |
58d05081
|
Adam M. Wilson
|
##download 3 days of modis swath data:
|
18 |
d39ab57e
|
Adam M. Wilson
|
getdata=F
|
19 |
|
|
if(getdata)
|
20 |
|
|
system(paste("wget -S --recursive --no-parent --no-directories -N -P swath --accept \"hdf\" --accept \"002|003|004\" ",url))
|
21 |
99b3508d
|
Adam M. Wilson
|
|
22 |
555815c9
|
Adam M. Wilson
|
### make global raster that aligns with MOD17 NPP raster
|
23 |
d39ab57e
|
Adam M. Wilson
|
t=raster(paste("../../MOD17/MOD17A3_Science_NPP_mean_00_12.tif",sep=""))
|
24 |
ddd9a810
|
Adam M. Wilson
|
projection(t)
|
25 |
99b3508d
|
Adam M. Wilson
|
|
26 |
|
|
## make global extent
|
27 |
|
|
glb=t
|
28 |
|
|
glb=extend(glb,extent(-180,180,-90,90))
|
29 |
|
|
|
30 |
555815c9
|
Adam M. Wilson
|
### list of swath files
|
31 |
|
|
files=paste(getwd(),"/",list.files("swath",pattern="hdf$",full=T),sep="")[1:5000]
|
32 |
99b3508d
|
Adam M. Wilson
|
|
33 |
|
|
## vars to process
|
34 |
|
|
vars=as.data.frame(matrix(c(
|
35 |
5f212fe8
|
Adam M. Wilson
|
"Cloud_Mask", "CM", "NN", 1,
|
36 |
|
|
"Sensor_Zenith", "SZ", "CUBIC", 1),
|
37 |
|
|
byrow=T,ncol=4,dimnames=list(1:2,c("variable","varid","method","band"))),stringsAsFactors=F)
|
38 |
ddd9a810
|
Adam M. Wilson
|
|
39 |
|
|
## global bounding box
|
40 |
5f212fe8
|
Adam M. Wilson
|
gbb=cbind(c(-180,-180,180,180,-180),c(-90,90,90,-90,-90))
|
41 |
ddd9a810
|
Adam M. Wilson
|
gpp = SpatialPolygons(list(Polygons(list(Polygon(gbb)),1)))
|
42 |
|
|
proj4string(gpp)=projection(glb)
|
43 |
|
|
|
44 |
555815c9
|
Adam M. Wilson
|
outdir="/gridded/"
|
45 |
ddd9a810
|
Adam M. Wilson
|
|
46 |
5f212fe8
|
Adam M. Wilson
|
swtif<-function(file,var){
|
47 |
|
|
outfile=paste(tempdir(),"/",var$varid,"_",basename(file),sep="") #gridded path
|
48 |
ddd9a810
|
Adam M. Wilson
|
## First write the parameter file (careful, heg is very finicky!)
|
49 |
5f212fe8
|
Adam M. Wilson
|
hdr=paste("NUM_RUNS = 1")
|
50 |
ddd9a810
|
Adam M. Wilson
|
grp=paste("
|
51 |
99b3508d
|
Adam M. Wilson
|
BEGIN
|
52 |
|
|
INPUT_FILENAME=",file,"
|
53 |
|
|
OBJECT_NAME=mod35
|
54 |
5f212fe8
|
Adam M. Wilson
|
FIELD_NAME=",var$variable,"|
|
55 |
|
|
BAND_NUMBER = ",var$band,"
|
56 |
99b3508d
|
Adam M. Wilson
|
OUTPUT_PIXEL_SIZE_X=0.008333333
|
57 |
|
|
OUTPUT_PIXEL_SIZE_Y=0.008333333
|
58 |
5f212fe8
|
Adam M. Wilson
|
SPATIAL_SUBSET_UL_CORNER = ( ",bbox(gpp)[2,2]," ",bbox(gpp)[1,1]," )
|
59 |
|
|
SPATIAL_SUBSET_LR_CORNER = ( ",bbox(gpp)[2,1]," ",bbox(gpp)[1,2]," )
|
60 |
|
|
RESAMPLING_TYPE =",var$method,"
|
61 |
99b3508d
|
Adam M. Wilson
|
OUTPUT_PROJECTION_TYPE = GEO
|
62 |
|
|
OUTPUT_PROJECTION_PARAMETERS = ( 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 )
|
63 |
|
|
ELLIPSOID_CODE = WGS84
|
64 |
|
|
OUTPUT_TYPE = HDFEOS
|
65 |
5f212fe8
|
Adam M. Wilson
|
OUTPUT_FILENAME= ",outfile,"
|
66 |
99b3508d
|
Adam M. Wilson
|
END
|
67 |
|
|
|
68 |
|
|
",sep="")
|
69 |
ddd9a810
|
Adam M. Wilson
|
## write it to a file
|
70 |
|
|
cat( c(hdr,grp) , file=paste(tempdir(),"/",basename(file),"_MODparms.txt",sep=""))
|
71 |
|
|
## now run the swath2grid tool
|
72 |
|
|
## write the gridded file
|
73 |
|
|
print(paste("Starting",file))
|
74 |
555815c9
|
Adam M. Wilson
|
system(paste("sudo ",stitch," -p ",tempdir(),"/",basename(file),"_MODparms.txt -d -log /dev/null -tmpLatLondir ",tempdir(),sep=""),intern=F)#,ignore.stderr=F)
|
75 |
5f212fe8
|
Adam M. Wilson
|
print(paste("Finished processing variable",var$variable,"from ",basename(file),"to",outfile))
|
76 |
|
|
}
|
77 |
ddd9a810
|
Adam M. Wilson
|
|
78 |
5f212fe8
|
Adam M. Wilson
|
getpath<- function(file){
|
79 |
|
|
setwd(tempdir())
|
80 |
|
|
bfile=sub(".hdf","",basename(file))
|
81 |
|
|
tempfile2_path=paste(tempdir(),"/",bfile,".tif",sep="") #gridded/masked/processed path
|
82 |
|
|
outfile=paste(outdir,"/",bfile,".tif",sep="") #final file
|
83 |
d39ab57e
|
Adam M. Wilson
|
if(file.exists(outfile)) return(paste(file," already finished"))
|
84 |
5f212fe8
|
Adam M. Wilson
|
ppc=gpp
|
85 |
|
|
#######
|
86 |
|
|
## run swtif for each band
|
87 |
|
|
lapply(1:nrow(vars),function(i) swtif(file,vars[i,]))
|
88 |
ddd9a810
|
Adam M. Wilson
|
####### import to R for processing
|
89 |
5f212fe8
|
Adam M. Wilson
|
|
90 |
|
|
if(!file.exists(paste(tempdir(),"/CM_",basename(file),sep=""))) {
|
91 |
ddd9a810
|
Adam M. Wilson
|
file.remove(list.files(tempdir(),pattern=bfile,full=T))
|
92 |
|
|
return(c(file,0))
|
93 |
|
|
}
|
94 |
|
|
## convert to land path
|
95 |
5f212fe8
|
Adam M. Wilson
|
d=raster(paste(tempdir(),"/CM_",basename(file),sep=""))
|
96 |
|
|
sz=raster(paste(tempdir(),"/SZ_",basename(file),sep=""))
|
97 |
|
|
NAvalue(sz)=-9999
|
98 |
d39ab57e
|
Adam M. Wilson
|
getlc=function(x,y) {ifelse(y<0|y>6000,NA,((x%/%2^6) %% 2^2))}
|
99 |
ddd9a810
|
Adam M. Wilson
|
path= overlay(d,sz,fun=getlc,filename=tempfile2_path,options=c("COMPRESS=LZW", "LEVEL=9","PREDICTOR=2"),datatype="INT1U",overwrite=T)
|
100 |
|
|
### warp them to align all pixels
|
101 |
|
|
system(paste("gdalwarp -overwrite -srcnodata 255 -dstnodata 255 -tap -tr 0.008333333 0.008333333 -co COMPRESS=LZW -co ZLEVEL=9 -co PREDICTOR=2 -s_srs \"",projection(t),"\" ",tempfile2_path," ",outfile,sep=""))
|
102 |
|
|
## delete temporary files
|
103 |
|
|
file.remove(list.files(tempdir(),pattern=bfile,full=T))
|
104 |
|
|
return(c(file,1))
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
|
108 |
5f212fe8
|
Adam M. Wilson
|
### run it
|
109 |
905db3dc
|
Adam M. Wilson
|
mclapply(files,getpath,mc.cores=10)
|
110 |
99b3508d
|
Adam M. Wilson
|
|
111 |
|
|
## check gdal can read all of them
|
112 |
5f212fe8
|
Adam M. Wilson
|
gfiles=list.files(outdir,pattern="tif$",full=T)
|
113 |
99b3508d
|
Adam M. Wilson
|
length(gfiles)
|
114 |
|
|
|
115 |
|
|
check=do.call(rbind,mclapply(gfiles,function(file){
|
116 |
|
|
gd=system(paste("gdalinfo ",file,sep=""),intern=T)
|
117 |
|
|
if(any(grepl("Corner",gd))) return(1)
|
118 |
|
|
else return(0)
|
119 |
|
|
}))
|
120 |
|
|
|
121 |
555815c9
|
Adam M. Wilson
|
## report on any bad files
|
122 |
99b3508d
|
Adam M. Wilson
|
table(check)
|
123 |
|
|
|
124 |
555815c9
|
Adam M. Wilson
|
## remove any fail the check
|
125 |
99b3508d
|
Adam M. Wilson
|
file.remove(gfiles[check==0])
|
126 |
555815c9
|
Adam M. Wilson
|
gfiles=gfiles[check==1]
|
127 |
99b3508d
|
Adam M. Wilson
|
|
128 |
|
|
|
129 |
|
|
###########
|
130 |
555815c9
|
Adam M. Wilson
|
### Use GRASS to import all the tifs and calculate the mode
|
131 |
99b3508d
|
Adam M. Wilson
|
## make temporary working directory
|
132 |
|
|
tf=paste(tempdir(),"/grass", Sys.getpid(),"/", sep="") #temporar
|
133 |
|
|
if(!file.exists(tf)) dir.create(tf)
|
134 |
|
|
|
135 |
|
|
## set up temporary grass instance for this PID
|
136 |
|
|
gisBase="/usr/lib/grass64"
|
137 |
|
|
print(paste("Set up temporary grass session in",tf))
|
138 |
|
|
initGRASS(gisBase=gisBase,gisDbase=tf,SG=as(glb,"SpatialGridDataFrame"),override=T,location="mod35",mapset="PERMANENT",home=tf,pid=Sys.getpid())
|
139 |
|
|
system(paste("g.proj -c proj4=\"",projection(glb),"\"",sep=""),ignore.stdout=T,ignore.stderr=T)
|
140 |
|
|
|
141 |
|
|
## read in NPP grid to serve as grid
|
142 |
|
|
execGRASS("r.in.gdal",input=t@file@name,output="grid")
|
143 |
|
|
system("g.region rast=grid n=90 s=-90 save=roi --overwrite",ignore.stdout=F,ignore.stderr=F)
|
144 |
|
|
|
145 |
|
|
## get files already imported - only important if more tifs were written after an initial import
|
146 |
|
|
imported=basename(gfiles)%in%system("g.mlist type=rast pattern=MOD*",intern=T)
|
147 |
|
|
table(imported)
|
148 |
|
|
|
149 |
|
|
## read in all tifs
|
150 |
d39ab57e
|
Adam M. Wilson
|
for(f in gfiles[!imported]) {
|
151 |
|
|
print(f)
|
152 |
555815c9
|
Adam M. Wilson
|
execGRASS("r.external",input=f,output=basename(f),flags=c("overwrite"))
|
153 |
d39ab57e
|
Adam M. Wilson
|
}
|
154 |
|
|
|
155 |
555815c9
|
Adam M. Wilson
|
## calculate mode in chunks. This first bins several individual swaths together into more or less complete global coverages taking the mode of each chunk
|
156 |
|
|
nbreaks=100
|
157 |
|
|
bins=cut(1:5000,nbreaks)
|
158 |
|
|
ts=system("g.mlist type=rast pattern=MOD*.tif",intern=T) #files to process
|
159 |
d39ab57e
|
Adam M. Wilson
|
|
160 |
555815c9
|
Adam M. Wilson
|
for(i in 1:nbreaks) #loop over breaks
|
161 |
|
|
execGRASS("r.series",input=paste(ts[bins==levels(bins)[i]],sep="",collapse=","),output=paste("path",i,sep="_"),method="mode",range=c(0,5),flags=c("verbose","overwrite"),Sys_wait=T)
|
162 |
|
|
|
163 |
|
|
## Get mode of each chunk
|
164 |
|
|
execGRASS("r.series",input=paste(system("g.mlist type=rast pattern=path*",intern=T),sep="",collapse=","),output="MOD35_path",method="mode",range=c(0,5),flags=c("verbose","overwrite"))
|
165 |
|
|
|
166 |
|
|
## fill in missing data (due to gridding artifacts) very near poles with water (north) and land (south)
|
167 |
|
|
system("r.mapcalc \"MOD35_patha=if(isnull(MOD35_path)&y()>-84.31,0,MOD35_path)\"")
|
168 |
|
|
system("r.mapcalc \"MOD35_pathb=if(isnull(MOD35_patha)&y()<-84.31,3,MOD35_patha)\"")
|
169 |
99b3508d
|
Adam M. Wilson
|
|
170 |
|
|
## add colors
|
171 |
555815c9
|
Adam M. Wilson
|
execGRASS("r.colors",map="MOD35_pathb",rules="MOD35_path_grasscolors.txt")
|
172 |
|
|
|
173 |
99b3508d
|
Adam M. Wilson
|
## write to disk
|
174 |
555815c9
|
Adam M. Wilson
|
execGRASS("r.out.gdal",input="MOD35_pathb",output=paste(getwd(),"/C5MOD35_ProcessPath.tif",sep=""),type="Byte",createopt="COMPRESS=LZW,LEVEL=9,PREDICTOR=2")
|
175 |
|
|
|
176 |
|
|
## update metadata
|
177 |
|
|
tags=c("TIFFTAG_IMAGEDESCRIPTION='Collection 5 MOD35 Processing Path (0=Water,1=Coast,2=Desert,3=Land)'",
|
178 |
|
|
"TIFFTAG_DOCUMENTNAME='Collection 5 MOD35 Processing Path'",
|
179 |
|
|
"TIFFTAG_DATETIME='20130901'",
|
180 |
|
|
"TIFFTAG_ARTIST='Adam M. Wilson (adam.wilson@yale.edu)'")
|
181 |
|
|
system(paste("/usr/local/src/gdal-1.10.0/swig/python/scripts/gdal_edit.py ",getwd(),"/C5MOD35_ProcessPath.tif ",paste("-mo ",tags,sep="",collapse=" "),sep=""))
|
182 |
99b3508d
|
Adam M. Wilson
|
|
183 |
|
|
### delete the temporary files
|
184 |
|
|
unlink_.gislock()
|
185 |
|
|
system(paste("rm -frR ",tf,sep=""))
|