1
|
#### Script to facilitate processing of MOD06 data
|
2
|
|
3
|
setwd("/nobackupp1/awilso10/mod35")
|
4
|
|
5
|
library(rgdal)
|
6
|
library(raster)
|
7
|
library(RSQLite)
|
8
|
|
9
|
|
10
|
verbose=T
|
11
|
|
12
|
## get MODLAND tile information
|
13
|
tb=read.table("http://landweb.nascom.nasa.gov/developers/sn_tiles/sn_bound_10deg.txt",skip=6,nrows=648,header=T)
|
14
|
tb$tile=paste("h",sprintf("%02d",tb$ih),"v",sprintf("%02d",tb$iv),sep="")
|
15
|
tb=tb[tb$lon_min!=-999,]
|
16
|
save(tb,file="modlandTiles.Rdata")
|
17
|
load("modlandTiles.Rdata")
|
18
|
|
19
|
## delete temporary log file that can grow to GB
|
20
|
system("rm /nobackupp1/awilso10/software/heg/TOOLKIT_MTD/runtime/LogStatus")
|
21
|
|
22
|
### list of tiles to process
|
23
|
tiles=c("h10v08","h11v08","h12v08","h10v07","h11v07","h12v07") # South America
|
24
|
## a northern block of tiles
|
25
|
expand.grid(paste("h",11:17,sep=""),v=c("v00","v01","v02","v03","v04"))
|
26
|
|
27
|
b## subset to MODLAND tiles
|
28
|
modlandtiles=system("ls -r /nobackupp4/datapool/modis/MOD11A1.005/2010* | grep hdf$ | cut -c18-23 | sort | uniq - ",intern=T)
|
29
|
tb$land=tb$tile%in%modlandtiles
|
30
|
tiles=tb$tile[tb$land]
|
31
|
|
32
|
## subset tile corner matrix to tiles selected above
|
33
|
tile_bb=tb[tb$tile%in%tiles,]
|
34
|
|
35
|
### get list of files to process
|
36
|
datadir="/nobackupp4/datapool/modis/MOD35_L2.006/"
|
37
|
|
38
|
outdir="daily/" #paste("daily/",tile,sep="")
|
39
|
|
40
|
##find swaths in region from sqlite database for the specified date/tile
|
41
|
## this takes a while, about 30 minutes, so only rebuild if you need to update what's available...
|
42
|
rebuildswathtable=F
|
43
|
if(rebuildswathtable){
|
44
|
## path to swath database
|
45
|
db="/nobackupp4/pvotava/DB/export/swath_geo.sql.sqlite3.db"
|
46
|
con=dbConnect("SQLite", dbname = db)
|
47
|
fs=do.call(rbind.data.frame,lapply(1:nrow(tile_bb),function(i){
|
48
|
d=dbGetQuery(con,paste("SELECT * from swath_geo6
|
49
|
WHERE east>=",tile_bb$lon_min[i]," AND
|
50
|
west<=",tile_bb$lon_max[i]," AND
|
51
|
north>=",tile_bb$lat_min[i]," AND
|
52
|
south<=",tile_bb$lat_max[i])
|
53
|
)
|
54
|
d$tile=tile_bb$tile[i]
|
55
|
print(paste("Finished tile",tile_bb$tile[i]))
|
56
|
return(d)
|
57
|
}))
|
58
|
con=dbDisconnect(con)
|
59
|
fs$id=substr(fs$id,7,19)
|
60
|
|
61
|
## Identify which swaths are available in the datapool
|
62
|
swaths=data.frame(path=list.files(datadir,pattern=paste("hdf$"),recursive=T,full=T),stringsAsFactors=F) #all swaths in data pool
|
63
|
swaths$id=substr(basename(swaths$path),10,22)
|
64
|
fs$exists=fs$id%in%swaths$id
|
65
|
fs$path=swaths$path[match(fs$id,swaths$id)]
|
66
|
|
67
|
## write tile-swath list to disk
|
68
|
save(fs,swaths,file="swathtile.Rdata")
|
69
|
}
|
70
|
|
71
|
load("swathtile.Rdata")
|
72
|
|
73
|
if(verbose) print(paste("###############",nrow(fs)," swath IDs recieved from database"))
|
74
|
|
75
|
## get all unique dates
|
76
|
fs$dateid=format(as.Date(paste(fs$year,fs$day,sep=""),"%Y%j"),"%Y%m%d")
|
77
|
#alldates=unique(fs$dateid[fs$exists])
|
78
|
|
79
|
#### Generate submission file
|
80
|
startdate="2000-03-01"
|
81
|
stopdate="2011-12-31"
|
82
|
## just 2005-2010
|
83
|
startdate="2009-01-01"
|
84
|
stopdate="2009-12-31"
|
85
|
|
86
|
alldates=format(seq(as.Date(startdate),as.Date(stopdate),1),"%Y%m%d")
|
87
|
|
88
|
proclist=expand.grid(date=alldates,tile=tiles)
|
89
|
proclist$year=substr(proclist$date,1,4)
|
90
|
|
91
|
## identify tile-dates with no available swaths
|
92
|
avail=unique(cbind.data.frame(tile=fs$tile,date=fs$dateid)[fs$exists, ])
|
93
|
proclist$avail=paste(proclist$tile,proclist$date,sep="_")%in%paste(avail$tile,avail$date,sep="_")
|
94
|
|
95
|
## identify which have been completed
|
96
|
#fdone=data.frame(path=system("ssh lou 'find MOD35/daily -name \"*.nc\"' ",intern=T))
|
97
|
fdone=data.frame(path=list.files(outdir,pattern="nc$",recursive=T))
|
98
|
fdone$date=substr(basename(as.character(fdone$path)),14,21)
|
99
|
fdone$tile=substr(basename(as.character(fdone$path)),7,12)
|
100
|
proclist$done=paste(proclist$tile,proclist$date,sep="_")%in%substr(basename(as.character(fdone$path)),7,21)
|
101
|
|
102
|
### report on what has already been processed
|
103
|
print(paste(sum(!proclist$done)," out of ",nrow(proclist)," (",round(100*sum(!proclist$done)/nrow(proclist),2),"%) remain"))
|
104
|
table(tile=proclist$tile[proclist$done],year=proclist$year[proclist$done])
|
105
|
table(table(tile=proclist$tile[!proclist$done],year=proclist$year[!proclist$done]))
|
106
|
|
107
|
### explore tile counts
|
108
|
#x=table(tile=proclist$tile[proclist$done],year=proclist$year[proclist$done])
|
109
|
#x=x[order(rownames(x)),]
|
110
|
|
111
|
script="/u/awilso10/environmental-layers/climate/procedures/MOD35_L2_process.r"
|
112
|
|
113
|
## write the table processed by mpiexec
|
114
|
tp=((!proclist$done)&proclist$avail) #date-tiles to process
|
115
|
table(Available=proclist$avail,Completed=proclist$done)
|
116
|
|
117
|
write.table(paste("--verbose ",script," --date ",proclist$date[tp]," --verbose T --tile ",proclist$tile[tp],sep=""),
|
118
|
file=paste("notdone.txt",sep=""),row.names=F,col.names=F,quote=F)
|
119
|
|
120
|
### qsub script
|
121
|
cat(paste("
|
122
|
#PBS -S /bin/bash
|
123
|
##PBS -l select=1:ncpus=8:mpiprocs=8
|
124
|
#PBS -l select=100:ncpus=8:mpiprocs=8
|
125
|
#PBS -l walltime=8:00:00
|
126
|
##PBS -l walltime=2:00:00
|
127
|
#PBS -j n
|
128
|
#PBS -m be
|
129
|
#PBS -N mod35
|
130
|
#PBS -q normal
|
131
|
##PBS -q devel
|
132
|
#PBS -V
|
133
|
|
134
|
CORES=800
|
135
|
#CORES=160
|
136
|
|
137
|
HDIR=/u/armichae/pr/
|
138
|
source $HDIR/etc/environ.sh
|
139
|
source /u/awilso10/environ.sh
|
140
|
source /u/awilso10/.bashrc
|
141
|
IDIR=/nobackupp1/awilso10/mod35/
|
142
|
##WORKLIST=$HDIR/var/run/pxrRgrs/work.txt
|
143
|
WORKLIST=$IDIR/notdone.txt
|
144
|
EXE=Rscript
|
145
|
LOGSTDOUT=$IDIR/log/mod35_stdout
|
146
|
LOGSTDERR=$IDIR/log/mod35_stderr
|
147
|
### use mpiexec to parallelize across days
|
148
|
mpiexec -np $CORES pxargs -a $WORKLIST -p $EXE -v -v -v --work-analyze 1> $LOGSTDOUT 2> $LOGSTDERR
|
149
|
",sep=""),file=paste("mod35_qsub",sep=""))
|
150
|
|
151
|
### Check the files
|
152
|
system(paste("cat mod35_qsub",sep=""))
|
153
|
system(paste("cat notdone.txt | head",sep=""))
|
154
|
system(paste("cat notdone.txt | wc -l ",sep=""))
|
155
|
|
156
|
## Submit it
|
157
|
system(paste("qsub mod35_qsub",sep=""))
|
158
|
|
159
|
system("qstat -u awilso10")
|
160
|
|
161
|
#######################################################
|
162
|
### Now submit the script to generate the climatologies
|
163
|
|
164
|
|
165
|
tiles
|
166
|
ctiles=c("h10v08","h11v08","h12v08","h10v07","h11v07","h12v07") # South America
|
167
|
|
168
|
ctiles=tiles#[c(1:3)] #subset to only some tiles (for example if some aren't finished yet)?
|
169
|
climatescript="/pleiades/u/awilso10/environmental-layers/climate/procedures/MOD35_Climatology.r"
|
170
|
|
171
|
## check which tiles have been processed and are on lou with a filename "MOD35_[tile].nc"
|
172
|
cdone=data.frame(path="",tile="") #use this if you want to re-run everything
|
173
|
cdone=data.frame(path=sapply(strsplit(basename(
|
174
|
system("ssh lou 'find MOD35/summary -name \"MOD35_h[0-9][0-9]v[0-9][0-9].nc\"' ",intern=T)),split="_"),function(x) x[2]))
|
175
|
cdone=data.frame(path=sapply(strsplit(basename(
|
176
|
system("find summary -name \"MOD35_h[0-9][0-9]v[0-9][0-9].nc\"",intern=T)),split="_"),function(x) x[2]))
|
177
|
cdone$tile=substr(basename(as.character(cdone$path)),1,6)
|
178
|
print(paste(length(ctiles[!ctiles%in%cdone$tile]),"Tiles still need to be processed"))
|
179
|
|
180
|
## write the table processed by mpiexec
|
181
|
write.table(paste("--verbose ",climatescript," --verbose T --tile ",ctiles[!ctiles%in%cdone$tile],sep=""),
|
182
|
file=paste("notdone_climate.txt",sep=""),row.names=F,col.names=F,quote=F)
|
183
|
|
184
|
## delay start until previous jobs have finished?
|
185
|
delay=F
|
186
|
## check running jobs to get JobID of job you want to wait for
|
187
|
system("qstat -u awilso10")
|
188
|
## enter JobID here:
|
189
|
job="881394.pbspl1.nas.nasa.gov"
|
190
|
|
191
|
### qsub script
|
192
|
cat(paste("
|
193
|
#PBS -S /bin/bash
|
194
|
#PBS -l select=40:ncpus=8:mem=94
|
195
|
#PBS -l walltime=2:00:00
|
196
|
#PBS -j n
|
197
|
#PBS -m be
|
198
|
#PBS -N mod35_climate
|
199
|
#PBS -q devel
|
200
|
##PBS -q normal
|
201
|
##PBS -q ldan
|
202
|
#PBS -V
|
203
|
",if(delay) paste("#PBS -W depend=afterany:",job,sep=""),"
|
204
|
|
205
|
CORES=320
|
206
|
HDIR=/u/armichae/pr/
|
207
|
source $HDIR/etc/environ.sh
|
208
|
source /pleiades/u/awilso10/environ.sh
|
209
|
source /pleiades/u/awilso10/.bashrc
|
210
|
IDIR=/nobackupp1/awilso10/mod35/
|
211
|
##WORKLIST=$HDIR/var/run/pxrRgrs/work.txt
|
212
|
WORKLIST=$IDIR/notdone_climate.txt
|
213
|
EXE=Rscript
|
214
|
LOGSTDOUT=$IDIR/log/climatology_stdout
|
215
|
LOGSTDERR=$IDIR/log/climatology_stderr
|
216
|
### use mpiexec to parallelize across tiles
|
217
|
mpiexec -np $CORES pxargs -a $WORKLIST -p $EXE -v -v -v --work-analyze 1> $LOGSTDOUT 2> $LOGSTDERR
|
218
|
",sep=""),file=paste("mod35_climatology_qsub",sep=""))
|
219
|
|
220
|
## check files
|
221
|
system(paste("cat mod35_climatology_qsub",sep="")) #qsub submission script
|
222
|
system(paste("cat notdone_climate.txt | head",sep="")) #top of job file
|
223
|
system(paste("cat notdone_climate.txt | wc -l ",sep="")) #number of jobs to be run
|
224
|
|
225
|
## Submit it
|
226
|
system(paste("qsub mod35_climatology_qsub",sep=""))
|
227
|
|
228
|
## check progress
|
229
|
system("qstat -u awilso10")
|
230
|
|
231
|
## start interactive job on compute node for debugging
|
232
|
# system("qsub -I -l walltime=2:00:00 -lselect=2:ncpus=16:model=san -q devel")
|
233
|
|
234
|
|
235
|
#################################################################
|
236
|
### copy the files back to Yale
|
237
|
|
238
|
|
239
|
system("ssh lou")
|
240
|
#scp `find MOD35/summary -name "MOD35_h[0-9][0-9]v[0-9][0-9].nc"` adamw@acrobates.eeb.yale.edu:/data/personal/adamw/projects/interp/data/modis/mod35/summary/
|
241
|
system("rsync -cavv `find summary -name \"MOD35_h[0-9][0-9]v[0-9][0-9]_2009mean.nc\"` adamw@acrobates.eeb.yale.edu:/data/personal/adamw/projects/interp/data/modis/mod35/summary/")
|
242
|
system("rsync -cavv `find summary -name \"MOD35_h[0-9][0-9]v[0-9][0-9].nc\"` adamw@acrobates.eeb.yale.edu:/data/personal/adamw/projects/interp/data/modis/mod35/summary/")
|
243
|
|
244
|
|
245
|
system("gdalbuildvrt MOD35C6_2009.vrt summary/*2009mean.nc ")
|
246
|
system("gdal_translate -stats -co \"COMPRESS=LZW\" -of GTiff MOD35C6_2009.vrt MOD35C6_2009.tif ")
|
247
|
system("scp MOD35C6_2009.tif adamw@acrobates.eeb.24.177.10.190:/Users/adamw/Downloads/")
|
248
|
exit
|
249
|
|
250
|
|