Project

General

Profile

« Previous | Next » 

Revision 5af36cdd

Added by Adam Wilson over 11 years ago

Separated daily products into a 'day' and 'night' cloudiness based on day flag

View differences:

climate/procedures/MOD35_L2_process.r
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
   
climate/procedures/Pleiades_MOD35.R
118 118
### qsub script
119 119
cat(paste("
120 120
#PBS -S /bin/bash
121
##PBS -l select=100:ncpus=8:mpiprocs=8
122
#PBS -l select=10:ncpus=8:mpiprocs=8
121
#PBS -l select=100:ncpus=8:mpiprocs=8
122
##PBS -l select=10:ncpus=8:mpiprocs=8
123 123
##PBS -l walltime=8:00:00
124 124
#PBS -l walltime=2:00:00
125 125
#PBS -j n
......
129 129
#PBS -q devel
130 130
#PBS -V
131 131

  
132
CORES=80
132
CORES=800
133 133
#CORES=160
134 134

  
135 135
HDIR=/u/armichae/pr/
136
#  source $HDIR/etc/environ.sh
136
  source $HDIR/etc/environ.sh
137 137
  source /u/awilso10/environ.sh
138 138
  source /u/awilso10/.bashrc
139 139
IDIR=/nobackupp1/awilso10/mod35/
......
146 146
mpiexec -np $CORES pxargs -a $WORKLIST -p $EXE -v -v -v --work-analyze 1> $LOGSTDOUT 2> $LOGSTDERR
147 147
",sep=""),file=paste("mod35_qsub",sep=""))
148 148

  
149

  
150 149
### Check the files
151 150
system(paste("cat mod35_qsub",sep=""))
152 151
system(paste("cat notdone.txt | head",sep=""))
......
160 159
#######################################################
161 160
### Now submit the script to generate the climatologies
162 161

  
162

  
163 163
tiles
164 164
ctiles=c("h10v08","h11v08","h12v08","h10v07","h11v07","h12v07")  # South America
165 165

  
......
170 170
cdone=data.frame(path="",tile="")  #use this if you want to re-run everything
171 171
cdone=data.frame(path=sapply(strsplit(basename(
172 172
                   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]))
173
cdone=data.frame(path=sapply(strsplit(basename(
174
                   system("find summary -name \"MOD35_h[0-9][0-9]v[0-9][0-9].nc\"",intern=T)),split="_"),function(x) x[2]))
173 175
cdone$tile=substr(basename(as.character(cdone$path)),1,6)
174 176
print(paste(length(ctiles[!ctiles%in%cdone$tile]),"Tiles still need to be processed"))
175 177

  
......
187 189
### qsub script
188 190
cat(paste("
189 191
#PBS -S /bin/bash
190
#PBS -l select=20:ncpus=8:mem=94
191
#PBS -l walltime=3:00:00
192
#PBS -l select=40:ncpus=8:mem=94
193
#PBS -l walltime=2:00:00
192 194
#PBS -j n
193 195
#PBS -m be
194 196
#PBS -N mod35_climate
195
#PBS -q normal
197
#PBS -q devel
198
##PBS -q normal
196 199
##PBS -q ldan
197 200
#PBS -V
198 201
",if(delay) paste("#PBS -W depend=afterany:",job,sep="")," 
199 202

  
200
CORES=160
203
CORES=320
201 204
HDIR=/u/armichae/pr/
202 205
  source $HDIR/etc/environ.sh
203 206
  source /pleiades/u/awilso10/environ.sh
......
230 233
#################################################################
231 234
### copy the files back to Yale
232 235

  
236

  
233 237
system("ssh lou")
234 238
#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/
235
system("rsync -cavv `find summary -name \"MOD35_h[0-9][0-9]v[0-9][0-9]_mean.nc\"` adamw@acrobates.eeb.yale.edu:/data/personal/adamw/projects/interp/data/modis/mod35/summary/")
239
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/")
240
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/")
241

  
242

  
243
system("gdalbuildvrt MOD35C6_2009.vrt summary/*2009mean.nc ") 
244
system("gdal_translate -stats -co \"COMPRESS=LZW\" -of GTiff MOD35C6_2009.vrt MOD35C6_2009.tif ")              
245
system("scp MOD35C6_2009.tif adamw@acrobates.eeb.24.177.10.190:/Users/adamw/Downloads/")
236 246
exit
237 247

  
238 248

  

Also available in: Unified diff