1
|
#### Script to facilitate processing of MOD06 data
|
2
|
|
3
|
setwd("/nobackupp1/awilso10/mod06")
|
4
|
library(rgdal)
|
5
|
library(raster)
|
6
|
|
7
|
## get MODLAND tile information
|
8
|
tb=read.table("http://landweb.nascom.nasa.gov/developers/sn_tiles/sn_bound_10deg.txt",skip=6,nrows=648,header=T)
|
9
|
tb$tile=paste("h",sprintf("%02d",tb$ih),"v",sprintf("%02d",tb$iv),sep="")
|
10
|
save(tb,file="modlandTiles.Rdata")
|
11
|
|
12
|
outdir="2_daily" #directory for separate daily files
|
13
|
outdir2="3_summary" #directory for combined daily files and summarized files
|
14
|
|
15
|
## load a MOD11A1 file to define grid
|
16
|
gridfile=list.files("/nobackupp4/datapool/modis/MOD11A1.005/2006.01.27/",pattern=paste(tile,".*hdf$",sep=""),full=T)[1]
|
17
|
td=readGDAL(paste("HDF4_EOS:EOS_GRID:\"",gridfile,"\":MODIS_Grid_Daily_1km_LST:Night_view_angl",sep=""))
|
18
|
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 "
|
19
|
|
20
|
|
21
|
### get list of files to process
|
22
|
datadir="/nobackupp4/datapool/modis/MOD06_L2.005/"
|
23
|
|
24
|
fs=data.frame(path=list.files(datadir,full=T,recursive=T,pattern="hdf"),stringsAsFactors=F)
|
25
|
fs$file=basename(fs$path)
|
26
|
fs$date=as.Date(substr(fs$file,11,17),"%Y%j")
|
27
|
fs$month=format(fs$date,"%m")
|
28
|
fs$year=format(fs$date,"%Y")
|
29
|
fs$time=substr(fs$file,19,22)
|
30
|
fs$datetime=as.POSIXct(strptime(paste(substr(fs$file,11,17),substr(fs$file,19,22)), '%Y%j %H%M'))
|
31
|
fs$dateid=format(fs$date,"%Y%m%d")
|
32
|
fs$path=as.character(fs$path)
|
33
|
fs$file=as.character(fs$file)
|
34
|
|
35
|
## get all unique dates
|
36
|
alldates=unique(fs$dateid)
|
37
|
|
38
|
|
39
|
#### Generate submission file
|
40
|
## identify which have been completed
|
41
|
done=alldates%in%substr(list.files(outdir),7,14)
|
42
|
table(done)
|
43
|
notdone=alldates[!done] #these are the dates that still need to be processed
|
44
|
|
45
|
tile="h11v08" #can move this to submit script if needed
|
46
|
script="/u/awilso10/environmental-layers/climate/procedures/MOD06_L2_process.r"
|
47
|
#write.table(paste("--verbose ",script," date=",notdone," tile=\"",tile,"\"",sep=""),file="notdone.txt",row.names=F,col.names=F,quote=F)
|
48
|
#write.table(paste("--verbose ",script," date=",notdone[1:30],sep=""),file="notdone.txt",row.names=F,col.names=F,quote=F)
|
49
|
write.table(notdone[1:30],file="notdone.txt",row.names=F,col.names=F,quote=F)
|
50
|
|
51
|
save(fs,alldates,gridfile,td,file="allfiles.Rdata")
|
52
|
|
53
|
## run script
|
54
|
cat(paste("
|
55
|
#! /bin/bash
|
56
|
source ~/moduleload
|
57
|
source ~/.bashrc
|
58
|
Rscript --verbose --vanilla /u/awilso10/environmental-layers/climate/procedures/MOD06_L2_process.r date=$1
|
59
|
#Rscript --verbose --vanilla rtest
|
60
|
",sep=""),file="MOD06_process2")
|
61
|
system("chmod +x MOD06_process2")
|
62
|
|
63
|
cat(paste("
|
64
|
library(rgdal)
|
65
|
GDALinfo
|
66
|
",sep=""),file="rtest")
|
67
|
|
68
|
|
69
|
## Submission script
|
70
|
|
71
|
cat(paste("
|
72
|
#PBS -S /bin/bash
|
73
|
#PBS -l select=2:ncpus=16:model=san
|
74
|
###PBS -l select=4:ncpus=8:model=neh
|
75
|
##PBS -l select=1:ncpus=12:model=wes
|
76
|
####### old: select=48:ncpus=8:mpiprocs=8:model=neh
|
77
|
#PBS -l walltime=2:00:00
|
78
|
#PBS -j oe
|
79
|
#PBS -m e
|
80
|
#PBS -V
|
81
|
#PBS -q devel
|
82
|
#PBS -o log/log_^array_index^
|
83
|
#PBS -o log/log_DataCompile.log
|
84
|
#PBS -M adam.wilson@yale.edu
|
85
|
#PBS -N MOD06
|
86
|
|
87
|
## cd to working directory
|
88
|
cd /nobackupp1/awilso10/mod06
|
89
|
|
90
|
## set some memory limits
|
91
|
# ulimit -d 1500000 -m 1500000 -v 1500000 #limit memory usage
|
92
|
source /usr/local/lib/global.profile
|
93
|
source /u/awilso10/.bashrc
|
94
|
source /u/awilso10/moduleload
|
95
|
## export a few important variables
|
96
|
export NNODES=32
|
97
|
export R_LIBS=\"/u/awilso10/R/x86_64-unknown-linux-gnu-library/2.15/\"
|
98
|
## Run the script!
|
99
|
## current version not parallelizing across nodes!
|
100
|
# TMPDIR=$TMPDIR Rscript --verbose --vanilla /u/awilso10/environmental-layers/climate/procedures/MOD06_L2_process.r date=20000403
|
101
|
|
102
|
WORKLIST=notdone.txt
|
103
|
#EXE=\"Rscript\"
|
104
|
EXE="./MOD06_process2"
|
105
|
LOG=log/log_DataCompile.log
|
106
|
MQUEUE=/nobackupp4/pvotava/software/share/mqueue-eg/mqueue/mqueue
|
107
|
|
108
|
TMPDIR=$TMPDIR mpiexec -np $NNODES $MQUEUE -l $WORKLIST -p $EXE -v -v -v --random-starts 2-4 --work-analyze #> $LOG
|
109
|
exit 0
|
110
|
",sep=""),file="MOD06_process")
|
111
|
|
112
|
### Check the file
|
113
|
system("cat MOD06_process")
|
114
|
#system("cat ~/environmental-layers/climate/procedures/MOD06_L2_process.r")
|
115
|
|
116
|
## check queue status
|
117
|
system("/u/scicon/tools/bin/node_stats.sh")
|
118
|
system("/u/scicon/tools/bin/qtop.pl 492352")
|
119
|
|
120
|
## Submit it (and keep the pid)!
|
121
|
system("qsub MOD06_process")
|
122
|
system("/u/scicon/tools/bin/pdsh_gdb -j 493281 -d tmp -s -u awilso10")
|
123
|
|
124
|
## work in interactive mode
|
125
|
# system("qsub -I -l walltime=2:00:00 -lselect=2:ncpus=16:model=san -q devel")
|
126
|
# mpirun -np 1 -r ssh R --no-save
|
127
|
|
128
|
## check progress
|
129
|
system("qstat -u awilso10")
|
130
|
system(paste("/u/scicon/tools/bin/qps ",pid))
|
131
|
system(paste("qstat -t -x",pid))
|
132
|
|
133
|
system("qstat devel ")
|
134
|
#system("qstat | grep awilso10")
|
135
|
|
136
|
####################################
|
137
|
|
138
|
|
139
|
################################################################################
|
140
|
## now generate the climatologies
|
141
|
fdly=data.frame(
|
142
|
path=list.files(outdir,pattern="nc$",full=T),
|
143
|
file=list.files(outdir,pattern="nc$"))
|
144
|
fdly$date=as.Date(substr(fdly$file,7,14),"%Y%m%d")
|
145
|
fdly$month=format(fdly$date,"%m")
|
146
|
fdly$year=format(fdly$date,"%Y")
|
147
|
|
148
|
## check validity (via npar and ntime) of nc files
|
149
|
for(i in 1:nrow(fdly)){
|
150
|
fdly$ntime[i]=as.numeric(system(paste("cdo sinfo ",fdly$path[i]),intern=T))
|
151
|
fdly$npar[i]=as.numeric(system(paste("cdo -s npar ",fdly$path[i]),intern=T))
|
152
|
print(i)
|
153
|
}
|
154
|
|
155
|
## Combine all days within years into a single file (can't mergetime all days at once because this opens too many files)
|
156
|
tsdir=paste(tempdir(),"/summary",sep="")
|
157
|
dir.create(tsdir)
|
158
|
lapply(unique(fdly$year),function(y){
|
159
|
system(paste("cdo -O mergetime ",paste(fdly$path[fdly$year==y],collapse=" ")," ",tsdir,"/MOD09_",tile,"_",y,"_daily.nc",sep=""))
|
160
|
print(paste("Finished merging daily files for year",y))
|
161
|
})
|
162
|
## Combine the year-by-year files into a single daily file
|
163
|
system(paste("cdo -O mergetime ",paste(list.files(tsdir,full=T,pattern="daily[.]nc$"),collapse=" ")," ",outdir2,"/MOD09_",tile,"_daily.nc",sep=""))
|
164
|
|
165
|
system(paste("cdo -O monmean ",outdir2,"/MOD09_",tile,"_daily.nc ",outdir2,"/",tile,"_monmean.nc",sep=""))
|
166
|
system(paste("cdo -O ymonmean ",outdir2,"/MOD09_",tile,"_daily.nc ",outdir2,"/",tile,"_ymonmean.nc",sep=""))
|
167
|
system(paste("cdo -O ymonstd ",outdir2,"/MOD09_",tile,"_daily.nc ",outdir2,"/",tile,"_ymonstd.nc",sep=""))
|
168
|
|
169
|
print("Finished! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
|
170
|
## quit R
|
171
|
q("no")
|
172
|
|
173
|
|
174
|
#################################################################
|
175
|
|
176
|
### copy the files back to Yale
|
177
|
list.files("2_daily")
|
178
|
system("scp 2_daily/* adamw@acrobates.eeb.yale.edu:/data/personal/adamw/projects/interp/data/modis/Venezuela")
|
179
|
|
180
|
system("scp /tmp/Rtmp6I6tFn/MOD06_L2.A2000061.1615.051.2010273184629.hdf adamw@acrobates.eeb.yale.edu:/data/personal/adamw/projects/interp/data/modis/Venezuela")
|
181
|
system("scp 2_daily/MOD06_20000410.nc adamw@acrobates.eeb.yale.edu:/data/personal/adamw/projects/interp/data/modis/Venezuela")
|
182
|
|
183
|
|
184
|
list.files(" /tmp/Rtmp6I6tFn")
|
185
|
|
186
|
|
187
|
|
188
|
|