Project

General

Profile

Download (7.25 KB) Statistics
| Branch: | Revision:
1
############################
2
####  Extract MOD35 C6 processing path
3

    
4
setwd("~/acrobates/adamw/projects/interp/data/modis/mod35/processpath")
5
library(multicore)
6
library(raster)
7
library(spgrass6)
8
library(rgeos)
9

    
10
#### 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
url="ftp://ladsweb.nascom.nasa.gov/allData/51/MOD35_L2/2012/"
15
dir.create("swath")
16

    
17
##download 3 days of modis swath data:
18
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

    
22
### make global raster that aligns with MOD17 NPP raster
23
t=raster(paste("../../MOD17/MOD17A3_Science_NPP_mean_00_12.tif",sep=""))
24
projection(t)
25

    
26
## make global extent
27
glb=t
28
glb=extend(glb,extent(-180,180,-90,90))
29

    
30
### list of swath files
31
files=paste(getwd(),"/",list.files("swath",pattern="hdf$",full=T),sep="")[1:5000]
32

    
33
## vars to process
34
vars=as.data.frame(matrix(c(
35
  "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

    
39
## global bounding box
40
   gbb=cbind(c(-180,-180,180,180,-180),c(-90,90,90,-90,-90))
41
   gpp = SpatialPolygons(list(Polygons(list(Polygon(gbb)),1)))
42
   proj4string(gpp)=projection(glb)
43

    
44
outdir="/gridded/"
45

    
46
swtif<-function(file,var){
47
  outfile=paste(tempdir(),"/",var$varid,"_",basename(file),sep="")  #gridded path
48
   ## First write the parameter file (careful, heg is very finicky!)
49
   hdr=paste("NUM_RUNS = 1")
50
   grp=paste("
51
BEGIN
52
INPUT_FILENAME=",file,"
53
OBJECT_NAME=mod35
54
FIELD_NAME=",var$variable,"|
55
BAND_NUMBER = ",var$band,"
56
OUTPUT_PIXEL_SIZE_X=0.008333333
57
OUTPUT_PIXEL_SIZE_Y=0.008333333
58
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
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
OUTPUT_FILENAME= ",outfile,"
66
END
67

    
68
",sep="")
69
   ## 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
   system(paste("sudo ",stitch," -p ",tempdir(),"/",basename(file),"_MODparms.txt -d -log /dev/null -tmpLatLondir ",tempdir(),sep=""),intern=F)#,ignore.stderr=F)
75
   print(paste("Finished processing variable",var$variable,"from ",basename(file),"to",outfile))
76
}  
77

    
78
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
   if(file.exists(outfile)) return(paste(file," already finished"))
84
   ppc=gpp
85
#######
86
## run swtif for each band
87
   lapply(1:nrow(vars),function(i) swtif(file,vars[i,]))
88
####### import to R for processing
89
  
90
   if(!file.exists(paste(tempdir(),"/CM_",basename(file),sep=""))) {
91
     file.remove(list.files(tempdir(),pattern=bfile,full=T))
92
     return(c(file,0))
93
   }
94
   ## convert to land path
95
   d=raster(paste(tempdir(),"/CM_",basename(file),sep=""))
96
   sz=raster(paste(tempdir(),"/SZ_",basename(file),sep=""))
97
   NAvalue(sz)=-9999
98
   getlc=function(x,y) {ifelse(y<0|y>6000,NA,((x%/%2^6) %% 2^2))}
99
   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
### run it
109
mclapply(files,getpath,mc.cores=10)
110

    
111
## check gdal can read all of them
112
gfiles=list.files(outdir,pattern="tif$",full=T)
113
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
## report on any bad files
122
table(check)
123

    
124
## remove any fail the check
125
file.remove(gfiles[check==0])
126
gfiles=gfiles[check==1]
127

    
128

    
129
###########
130
### Use GRASS to import all the tifs and calculate the mode
131
## 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
  for(f in gfiles[!imported])  {
151
    print(f)
152
    execGRASS("r.external",input=f,output=basename(f),flags=c("overwrite"))
153
  }
154

    
155
## 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

    
160
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

    
170
## add colors
171
execGRASS("r.colors",map="MOD35_pathb",rules="MOD35_path_grasscolors.txt")
172

    
173
## write to disk
174
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

    
183
### delete the temporary files 
184
  unlink_.gislock()
185
  system(paste("rm -frR ",tf,sep=""))
(29-29/52)