35 |
35 |
|
36 |
36 |
|
37 |
37 |
## default date and tile to play with (will be overwritten below when running in batch)
|
38 |
|
#date="20000410"
|
|
38 |
#date="20090129"
|
39 |
39 |
#tile="h11v08"
|
40 |
40 |
platform="pleiades"
|
41 |
41 |
verbose=T
|
... | ... | |
57 |
57 |
## path to swath database
|
58 |
58 |
db="/nobackupp4/pvotava/DB/export/swath_geo.sql.sqlite3.db"
|
59 |
59 |
## specify working directory
|
60 |
|
outdir=paste("daily/",tile,"/",sep="") #directory for separate daily files
|
61 |
|
basedir="/nobackupp1/awilso10/mod35/daily/" #directory to hold files temporarily before transferring to lou
|
|
60 |
outdir=paste("/nobackupp1/awilso10/mod35/daily/",tile,"/",sep="") #directory for separate daily files
|
|
61 |
basedir="/nobackupp1/awilso10/mod35/" #directory to hold files temporarily before transferring to lou
|
62 |
62 |
setwd(tempdir())
|
63 |
63 |
## grass database
|
64 |
64 |
gisBase="/u/armichae/pr/grass-6.4.2/"
|
65 |
65 |
## path to MOD11A1 file for this tile to align grid/extent
|
66 |
|
gridfile=list.files("/nobackupp4/datapool/modis/MOD11A1.005/2006.01.27",pattern=paste(tile,".*[.]hdf$",sep=""),recursive=T,full=T)[1]
|
67 |
|
td=readGDAL(paste("HDF4_EOS:EOS_GRID:\"",gridfile,"\":MODIS_Grid_Daily_1km_LST:Night_view_angl",sep=""))
|
|
66 |
gridfile=list.files("/nobackupp4/datapool/modis/MOD11A2.005/2009.01.01",pattern=paste(tile,".*[.]hdf$",sep=""),recursive=T,full=T)[1]
|
|
67 |
td=readGDAL(paste("HDF4_EOS:EOS_GRID:\"",gridfile,"\":MODIS_Grid_8Day_1km_LST:LST_Day_1km",sep=""))
|
68 |
68 |
projection(td)="+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs +datum=WGS84 +ellps=WGS84 "
|
69 |
69 |
}
|
70 |
70 |
|
... | ... | |
97 |
97 |
## get bounds of swath to keep and feed into grass when generating tile
|
98 |
98 |
## expand a little (0.5 deg) to ensure that there is no clipping of pixels on the edges
|
99 |
99 |
## tile will later be aligned with MODLAND tile so the extra will eventually be trimmed
|
100 |
|
upleft=paste(min(90,tile_bb$lat_max+.5),max(-180,tile_bb$lon_min-.5)) #northwest corner
|
|
100 |
upleft=paste(min(90,tile_bb$lat_max+0.5),max(-180,tile_bb$lon_min-0.5)) #northwest corner
|
101 |
101 |
lowright=paste(max(-90,tile_bb$lat_min-0.5),min(180,tile_bb$lon_max+0.5)) #southeast corner
|
102 |
102 |
|
103 |
|
|
104 |
103 |
## vars to process
|
105 |
104 |
vars=as.data.frame(matrix(c(
|
106 |
105 |
"Cloud_Mask", "CM",
|
... | ... | |
108 |
107 |
byrow=T,ncol=2,dimnames=list(1:2,c("variable","varid"))),stringsAsFactors=F)
|
109 |
108 |
|
110 |
109 |
## vector of variables expected to be in final netcdf file. If these are not present, the file will be deleted at the end.
|
111 |
|
finalvars=c("PClear")
|
|
110 |
finalvars=c("CMday","CMnight")
|
112 |
111 |
|
113 |
112 |
|
114 |
113 |
#####################################################
|
... | ... | |
146 |
145 |
OBJECT_NAME=mod35
|
147 |
146 |
FIELD_NAME=",vars$variable,"|
|
148 |
147 |
BAND_NUMBER = 1
|
149 |
|
OUTPUT_PIXEL_SIZE_X=1000
|
150 |
|
OUTPUT_PIXEL_SIZE_Y=1000
|
|
148 |
OUTPUT_PIXEL_SIZE_X=926.6
|
|
149 |
OUTPUT_PIXEL_SIZE_Y=926.6
|
|
150 |
# MODIS 1km Resolution
|
151 |
151 |
SPATIAL_SUBSET_UL_CORNER = ( ",upleft," )
|
152 |
152 |
SPATIAL_SUBSET_LR_CORNER = ( ",lowright," )
|
153 |
153 |
#RESAMPLING_TYPE =",ifelse(grepl("Flag|Mask|Quality",vars),"NN","CUBIC"),"
|
... | ... | |
169 |
169 |
cat(c(hdr,grp) , file=paste(tempdir(),"/",basename(file),"_MODparms.txt",sep=""))
|
170 |
170 |
## now run the swath2grid tool
|
171 |
171 |
## write the gridded file
|
172 |
|
log=system(paste("(",swtifpath," -p ",tempdir(),"/",basename(file),"_MODparms.txt -d ; echo $$)",sep=""),intern=T,ignore.stderr=T)
|
173 |
|
## clean up temporary files in working directory
|
174 |
|
# file.remove(list.files(pattern=
|
175 |
|
# paste("filetable.temp_",
|
176 |
|
# as.numeric(log[length(log)]):(as.numeric(log[length(log)])+3),sep="",collapse="|"))) #Look for files with PID within 3 of parent process
|
177 |
|
if(verbose) print(log)
|
178 |
|
print(paste("Finished gridding ", file))
|
|
172 |
system(paste("(",swtifpath," -p ",tempdir(),"/",basename(file),"_MODparms.txt -d)",sep=""),intern=F,ignore.stderr=F)
|
|
173 |
print(paste("Finished gridding ", file," for tile ",tile))
|
179 |
174 |
} #end looping over swaths
|
180 |
175 |
|
181 |
176 |
########################
|
... | ... | |
206 |
201 |
if(!file.exists(tf)) dir.create(tf)
|
207 |
202 |
## create output directory if needed
|
208 |
203 |
## Identify output file
|
209 |
|
ncfile=paste(basedir,"MOD35_",tile,"_",date,".nc",sep="") #this is the 'final' daily output file
|
210 |
|
if(!file.exists(dirname(ncfile))) dir.create(dirname(ncfile,recursive=T))
|
|
204 |
ncfile=paste(outdir,"MOD35_",tile,"_",date,".nc",sep="") #this is the 'final' daily output file
|
|
205 |
if(!file.exists(dirname(ncfile))) dir.create(dirname(ncfile),recursive=T)
|
211 |
206 |
|
212 |
207 |
## set up temporary grass instance for this PID
|
213 |
208 |
if(verbose) print(paste("Set up temporary grass session in",tf))
|
... | ... | |
217 |
212 |
## Define region by importing one MOD11A1 raster.
|
218 |
213 |
print("Import one MOD11A1 raster to define grid")
|
219 |
214 |
if(platform=="pleiades") {
|
220 |
|
execGRASS("r.in.gdal",input=paste("HDF4_EOS:EOS_GRID:\"",gridfile,"\":MODIS_Grid_Daily_1km_LST:Night_view_angl",sep=""),
|
|
215 |
execGRASS("r.in.gdal",input=paste("HDF4_EOS:EOS_GRID:\"",gridfile,"\":MODIS_Grid_8Day_1km_LST:LST_Day_1km",sep=""),
|
221 |
216 |
output="modisgrid",flags=c("quiet","overwrite","o"))
|
222 |
217 |
system("g.region rast=modisgrid save=roi --overwrite",ignore.stdout=F,ignore.stderr=F)
|
223 |
218 |
}
|
... | ... | |
248 |
243 |
system(paste("r.mapcalc <<EOF
|
249 |
244 |
QA_useful_",i," = if((QA_",i," / 2^0) % 2==1,1,0)
|
250 |
245 |
CM_cloud_",i," = if((CM1_",i," / 2^0) % 2==1,(CM1_",i," / 2^1) % 2^2,-9999)
|
251 |
|
Pclear1_",i," = if(CM_cloud_",i,"==0,0,if(CM_cloud_",i,"==1,66,if(CM_cloud_",i,"==2,95,if(CM_cloud_",i,"==3,99,-9999))))
|
252 |
|
Pclear_",i," = if(QA_useful_",i,"==1,Pclear1_",i,",-9999)
|
|
246 |
CM_dayflag_",i," = if((CM1_",i," / 2^3) % 2==1,1,0)
|
|
247 |
CMday_",i," = if(QA_useful_",i,"==1&CM_dayflag_",i,"==1,CM_cloud_",i,",-9999)
|
|
248 |
CMnight_",i," = if(QA_useful_",i,"==1&CM_dayflag_",i,"==0,CM_cloud_",i,",-9999)
|
253 |
249 |
EOF",sep=""))
|
|
250 |
#Pclear1_",i," = if(CM_cloud_",i,"==0,0,if(CM_cloud_",i,"==1,66,if(CM_cloud_",i,"==2,95,if(CM_cloud_",i,"==3,99,-9999))))
|
|
251 |
#CM_path_",i," = ((CM1_",i," / 2^6) % 2^2)
|
254 |
252 |
|
255 |
|
# CM_path_",i," = ((CM1_",i," / 2^6) % 2^2)
|
|
253 |
# execGRASS("r.null",map=paste("Pclearday_",i,sep=""),setnull="-9999")
|
|
254 |
# execGRASS("r.null",map=paste("Pclearnight_",i,sep=""),setnull="-9999")
|
256 |
255 |
|
257 |
|
execGRASS("r.null",map=paste("Pclear_",i,sep=""),setnull="-9999")
|
258 |
|
execGRASS("r.null",map=paste("CM_cloud_",i,sep=""),setnull="-9999")
|
|
256 |
execGRASS("r.null",map=paste("CMday_",i,sep=""),setnull="-9999")
|
|
257 |
execGRASS("r.null",map=paste("CMnight_",i,sep=""),setnull="-9999")
|
259 |
258 |
|
260 |
259 |
|
261 |
260 |
} #end loop through sub daily files
|
262 |
261 |
|
263 |
262 |
#### Now generate daily minimum p(clear)
|
264 |
263 |
system(paste("r.mapcalc <<EOF
|
265 |
|
Pclear_daily=int((min(",paste("if(isnull(Pclear_",1:nfs,"),9999,Pclear_",1:nfs,")",sep="",collapse=","),")))
|
|
264 |
CMday_daily=int((min(",paste("if(isnull(CMday_",1:nfs,"),9999,CMday_",1:nfs,")",sep="",collapse=","),")))
|
|
265 |
CMnight_daily=int((min(",paste("if(isnull(CMnight_",1:nfs,"),9999,CMnight_",1:nfs,")",sep="",collapse=","),")))
|
266 |
266 |
EOF",sep=""))
|
267 |
267 |
|
268 |
268 |
|
269 |
269 |
## reset null values
|
270 |
|
execGRASS("r.null",map="Pclear_daily",setnull="9999")
|
271 |
|
|
|
270 |
execGRASS("r.null",map="CMday_daily",setnull="9999")
|
|
271 |
execGRASS("r.null",map="CMnight_daily",setnull="9999")
|
272 |
272 |
|
273 |
273 |
### Write the files to a netcdf file
|
274 |
274 |
## create image group to facilitate export as multiband netcdf
|
275 |
|
execGRASS("i.group",group="mod35",input=c("Pclear_daily")) ; print("")
|
|
275 |
execGRASS("i.group",group="mod35",input=c("CMday_daily","CMnight_daily")) ; print("")
|
276 |
276 |
|
277 |
277 |
if(file.exists(ncfile)) file.remove(ncfile) #if it exists already, delete it
|
278 |
278 |
execGRASS("r.out.gdal",input="mod35",output=ncfile,type="Byte",nodata=255,flags=c("quiet"),
|
... | ... | |
296 |
296 |
system(paste("ncgen -o ",tempdir(),"/time.nc ",tempdir(),"/time.cdl",sep=""))
|
297 |
297 |
system(paste(ncopath,"ncks -A ",tempdir(),"/time.nc ",ncfile,sep=""))
|
298 |
298 |
## add other attributes
|
299 |
|
system(paste(ncopath,"ncrename -v Band1,PClear ",ncfile,sep=""))
|
|
299 |
system(paste(ncopath,"ncrename -v Band1,CMday -v Band2,CMnight ",ncfile,sep=""))
|
300 |
300 |
system(paste(ncopath,"ncatted ",
|
301 |
|
" -a units,PClear,o,c,\"Probability (%)\" ",
|
302 |
|
" -a missing_value,PClear,o,b,255 ",
|
303 |
|
" -a _FillValue,PClear,o,b,255 ",
|
304 |
|
" -a valid_range,PClear,o,b,\"0,100\" ",
|
305 |
|
" -a long_name,PClear,o,c,\"Probability of Clear Sky\" ",
|
|
301 |
" -a units,CMday,o,c,\"Cloud Flag (0-3)\" ",
|
|
302 |
" -a missing_value,CMday,o,b,255 ",
|
|
303 |
" -a _FillValue,CMday,o,b,255 ",
|
|
304 |
" -a valid_range,CMday,o,b,\"0,3\" ",
|
|
305 |
" -a long_name,CMday,o,c,\"Cloud Flag from 'day' pixels\" ",
|
|
306 |
" -a units,CMnight,o,c,\"Cloud Flag (0-3)\" ",
|
|
307 |
" -a missing_value,CMnight,o,b,255 ",
|
|
308 |
" -a _FillValue,CMnight,o,b,255 ",
|
|
309 |
" -a valid_range,CMnight,o,b,\"0,3\" ",
|
|
310 |
" -a long_name,CMnight,o,c,\"Cloud Flag from 'night' pixels\" ",
|
306 |
311 |
ncfile,sep=""))
|
307 |
312 |
#system(paste(ncopath,"ncatted -a sourcecode,global,o,c,",script," ",ncfile,sep=""))
|
308 |
313 |
|
Separated daily products into a 'day' and 'night' cloudiness based on day flag