Project

General

Profile

« Previous | Next » 

Revision 0c74b1da

Added by Adam M. Wilson over 12 years ago

Added cloud flag to outputs

View differences:

climate/procedures/MOD06_L2_data_compile.r
36 36

  
37 37
system("wget -r --retr-symlinks ftp://ladsweb.nascom.nasa.gov/orders/500676499/ -P /home/adamw/acrobates/projects/interp/data/modis/MOD06_L2_hdf")
38 38

  
39

  
39
## specify some working directories
40 40
gdir="output/"
41 41
datadir="data/modis/MOD06_L2_hdf"
42 42
outdir="data/modis/MOD06_L2_hdf2"
43
tifdir="data/modis/MOD06_L2_tif"
43
tifdir="/media/data/MOD06_L2_tif"
44
summarydatadir="data/modis/MOD06_climatologies"
45

  
44 46

  
45 47
fs=data.frame(
46 48
  path=list.files(datadir,full=T,recursive=T,pattern="hdf"),
......
73 75
  "Cloud_Multi_Layer_Flag",              "CMLF",
74 76
  "Cloud_Mask_1km",                      "CM1",
75 77
  "Quality_Assurance_1km",               "QA"),
76
  byrow=T,ncol=2,dimnames=list(1:10,c("variable","varid"))))
78
  byrow=T,ncol=2,dimnames=list(1:10,c("variable","varid"))),stringsAsFactors=F)
77 79

  
78 80

  
79 81
### Installation of hegtool
......
83 85

  
84 86

  
85 87
#### Function to generate hegtool parameter file for multi-band HDF-EOS file
86
swath2grid=function(i=1,files,vars=vars,outdir,upleft="47 -125",lowright="41 -115"){
88
swath2grid=function(i=1,files,vars,outdir,upleft="47 -125",lowright="41 -115"){
87 89
  file=fs$path[i]
88 90
  print(paste("Starting file",basename(file)))
89 91
  outfile=paste(outdir,"/",fs$file[i],sep="")
......
101 103
OUTPUT_PIXEL_SIZE_Y=1000
102 104
SPATIAL_SUBSET_UL_CORNER = ( ",upleft," )
103 105
SPATIAL_SUBSET_LR_CORNER = ( ",lowright," )
104
#RESAMPLING_TYPE =",ifelse(grepl("Flag|Mask|Quality",vars$variable),"NN","CUBIC"),"
106
#RESAMPLING_TYPE =",ifelse(grepl("Flag|Mask|Quality",vars),"NN","CUBIC"),"
105 107
RESAMPLING_TYPE =NN
106 108
OUTPUT_PROJECTION_TYPE = SIN
107 109
ELLIPSOID_CODE = WGS84
......
110 112
OUTPUT_FILENAME = ",outfile,"
111 113
END
112 114

  
113

  
114 115
",sep="")
115 116
  ## if any remnants from previous runs remain, delete them
116 117
  if(length(list.files(tempdir(),pattern=basename(file)))>0)
......
146 147

  
147 148
#fs$grass=paste(fs$month,fs$year,fs$file,sep="_")
148 149
td=readGDAL(paste("HDF4_EOS:EOS_GRID:\"",outdir,"/",fs$file[1],"\":mod06:Cloud_Mask_1km_0",sep=""))
149
projection(td)="+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +ellps=WGS84 +units=m +no_defs"
150
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 "
150 151

  
151 152
## fucntion to convert binary to decimal to assist in identifying correct values
152 153
b2d=function(x) sum(x * 2^(rev(seq_along(x)) - 1)) #http://tolstoy.newcastle.edu.au/R/e2/help/07/02/10596.html
......
173 174
system("g.region -p")
174 175
getLocationProj()
175 176

  
176

  
177
## temporary objects to test function below
177 178
 i=1
178 179
file=paste(outdir,"/",fs$file[1],sep="")
179 180
date=as.Date("2000-03-02")
180 181

  
182

  
183
### Function to extract various SDSs from a single gridded HDF file and use QA data to throw out 'bad' observations
181 184
loadcloud<-function(date,fs){
182
  
183
  ## Identify which files to process
185
    ## Identify which files to process
184 186
  tfs=fs$file[fs$date==date]
185 187
  nfs=length(tfs)
186 188
  unlink_.gislock()
......
215 217
                 QA_COT3_",i,"=  ((QA_",i," / 2^3) % 2^2 )==0
216 218
                 QA_CER_",i,"=   ((QA_",i," / 2^5) % 2^1 )==1
217 219
                 QA_CER2_",i,"=  ((QA_",i," / 2^6) % 2^2 )==3
220
                 QA_CWP_",i,"=   ((QA_",i," / 2^8) % 2^1 )==1
221
                 QA_CWP2_",i,"=  ((QA_",i," / 2^9) % 2^2 )==3
218 222
EOF",sep="")) 
219
#                 QA_CWP_",i,"=   ((QA_",i," / 2^8) % 2^1 )==1
220
#                 QA_CWP2_",i,"=  ((QA_",i," / 2^9) % 2^2 )==3
221 223

  
222 224
   ## Optical Thickness
223 225
   execGRASS("r.in.gdal",input=paste("HDF4_EOS:EOS_GRID:\"",file,"\":mod06:Cloud_Optical_Thickness",sep=""),
......
242 244
   system(paste("r.mapcalc \"CER2_",i,"=if(CM_clear_",i,"==0,CER_",i,",0)\"",sep=""))   
243 245

  
244 246
   ## Cloud Water Path
245
#   execGRASS("r.in.gdal",input=paste("HDF4_EOS:EOS_GRID:\"",file,"\":mod06:Cloud_Water_Path",sep=""),
246
#            output="CWP",title="cloud_water_path",
247
#            flags=c("overwrite","o")) ; print("")
248
#   execGRASS("r.null",map="CWP",setnull="-9999")
249
#   ## keep only positive CWP values where quality is 'useful' and 'very good' & scale to real units
247
   execGRASS("r.in.gdal",input=paste("HDF4_EOS:EOS_GRID:\"",file,"\":mod06:Cloud_Water_Path",sep=""),
248
            output=paste("CWP_",i,sep=""),title="cloud_water_path",
249
            flags=c("overwrite","o")) ; print("")
250
   execGRASS("r.null",map=paste("CWP_",i,sep=""),setnull="-9999")
251
   ## keep only positive CWP values where quality is 'useful' and 'very good' & scale to real units
250 252
#   system(paste("r.mapcalc \"CWP=if(QA_CWP&&QA_CWP2,CWP,null())\""))   
251
   
253
   ## keep only positive CER values where quality is 'useful' and 'very good' & scale to real units
254
   system(paste("r.mapcalc \"CWP_",i,"=if(QA_CWP_",i,"&&QA_CWP2_",i,"&&CWP_",i,">=0,CWP_",i,"*0.009999999776482582,null())\"",sep=""))   
255
   ## set CER to 0 in clear-sky pixels
256
   system(paste("r.mapcalc \"CWP2_",i,"=if(CM_clear_",i,"==0,CWP_",i,",0)\"",sep=""))   
257

  
258
     
252 259
 } #end loop through sub daily files
253 260

  
254
#### Now generate daily averages
255
 
261
#### Now generate daily averages (or maximum in case of cloud flag)
262
  
256 263
  system(paste("r.mapcalc <<EOF
257 264
         COT_denom=",paste("!isnull(COT2_",1:nfs,")",sep="",collapse="+"),"
258 265
         COT_numer=",paste("if(isnull(COT2_",1:nfs,"),0,COT2_",1:nfs,")",sep="",collapse="+"),"
......
260 267
         CER_denom=",paste("!isnull(CER2_",1:nfs,")",sep="",collapse="+"),"
261 268
         CER_numer=",paste("if(isnull(CER2_",1:nfs,"),0,CER2_",1:nfs,")",sep="",collapse="+"),"
262 269
         CER_daily=CER_numer/CER_denom
270
         CLD_daily=max(",paste("if(isnull(CM_cloud_",1:nfs,"),0,CM_cloud_",1:nfs,")",sep="",collapse=","),") 
263 271
EOF",sep=""))
264 272

  
265 273
  #### Write the file to a geotiff
266 274
  execGRASS("r.out.gdal",input="CER_daily",output=paste(tifdir,"/CER_",format(date,"%Y%m%d"),".tif",sep=""),nodata=-999)
267 275
  execGRASS("r.out.gdal",input="COT_daily",output=paste(tifdir,"/COT_",format(date,"%Y%m%d"),".tif",sep=""),nodata=-999)
276
  execGRASS("r.out.gdal",input="CLD_daily",output=paste(tifdir,"/CLD_",format(date,"%Y%m%d"),".tif",sep=""),nodata=-999)
268 277

  
269 278
### delete the temporary files 
270 279
  unlink_.gislock()
......
277 286
###########################################
278 287
### Now run it
279 288

  
280
 tdates=sort(unique(fs$date))
289
tdates=sort(unique(fs$date))
281 290
done=tdates%in%as.Date(substr(list.files("data/modis/MOD06_L2_tif"),5,12),"%Y%m%d")
282 291
table(done)
283 292
tdates=tdates[!done]
284 293

  
285 294
lapply(tdates,function(date) loadcloud(date,fs=fs))
286 295

  
287

  
288

  
289

  
290
# copy all datasets back to master mapset for summaries
291

  
292
execGRASS("g.copy",rast=paste("\"COT_daily\",\"COT_",format(date,"%Y%m%d"),"@mod06\"",sep=""))
293

  
294
  
296
 
295 297
## unlock the grass database
296 298
unlink_.gislock()
297 299

  
......
302 304

  
303 305
## get list of daily files
304 306
fs2=data.frame(
305
  path=list.files(outdir,full=T,recursive=T,pattern="hdf"),
306
  file=basename(list.files(datadir,full=F,recursive=T,pattern="hdf")))
307
fs$date=as.Date(substr(fs$file,11,17),"%Y%j")
308
fs$month=format(fs$date,"%m")
309
fs$year=format(fs$date,"%Y")
310
fs$time=substr(fs$file,19,22)
311
fs$datetime=as.POSIXct(strptime(paste(substr(fs$file,11,17),substr(fs$file,19,22)), '%Y%j %H%M'))
312
fs$dateid=format(fs$date,"%Y%m%d")
313
fs$path=as.character(fs$path)
314
fs$file=as.character(fs$file)
315

  
316

  
317
# read in data as single spatialgrid
318
ms=c("01","02","03","04","05","06","07","08","09","10","11","12")
319

  
320
mclapply(ms, function(m){
321
  d=readRAST6(fs$grass[1])
322
  projection(d)=projection(td)
323
  d@data=as.data.frame(do.call(cbind,mclapply(which(fs$month==m),function(i){
324
    print(fs$date[i])
325
    readRAST6(fs$grass[i])@data[,1]
326
    })))
327
  d=brick(d)
307
  path=list.files(tifdir,full=T,recursive=T,pattern="tif$"),
308
  file=basename(list.files(tifdir,full=F,recursive=T,pattern="tif$")))
309
fs2$type=substr(fs2$file,1,3)
310
fs2$date=as.Date(substr(fs2$file,5,12),"%Y%m%d")
311
fs2$month=format(fs2$date,"%m")
312
fs2$year=format(fs2$date,"%Y")
313
fs2$path=as.character(fs2$path)
314
fs2$file=as.character(fs2$file)
315

  
316

  
317
# Define type/month products
318
vs=expand.grid(type=unique(fs2$type),month=c("01","02","03","04","05","06","07","08","09","10","11","12"))
319

  
320
## identify which have been completed
321
#done=
322
#  do.call(rbind,strsplit(list.files(summarydatadir),"_|[.]"))[,3]
323
#table(done)
324
#tdates=tdates[!done]
325

  
326

  
327
## process the summaries using the raster package
328
mclapply(1:nrow(vs),function(i){
329
  print(paste("Starting ",vs$type[i]," for month ",vs$month[i]))
330
  td=stack(fs2$path[which(fs2$month==vs$month[i]&fs2$type==vs$type[i])])
331
  calc(td,mean,na.rm=T,
332
       filename=paste(summarydatadir,"/",vs$type[i],"_mean_",vs$month[i],".tif",sep=""),
333
       format="GTiff")
334
  calc(td,sd,na.rm=T,
335
       filename=paste(summarydatadir,"/",vs$type[i],"_sd_",vs$month[i],".tif",sep=""),
336
       format="GTiff")
337
  print(paste("Processing missing data for ",vs$type[i]," for month ",vs$month[i]))
338
  calc(td,function(i)
339
       sum(!is.na(i)),filename=paste(summarydatadir,"/",vs$type[i],"_count_",vs$month[i],".tif",sep=""),
340
       format="GTiff")
341
  calc(td,function(i) sum(ifelse(i==0,0,1)),
342
       filename=paste(summarydatadir,"/",vs$type[i],"_clear_",vs$month[i],".tif",sep=""),format="GTiff")
328 343
  gc()
329
  assign(paste("m",m,sep="_"),d)
330 344
}
331

  
332
save(paste("m",m,sep="_"),file="output/MOD06.Rdata")
333

  
334
## replace missings with 0 (because they mean no clouds)
335
db2=d
336
db2[is.na(db2)]=0
337

  
338

  
339
md=mean(db,na.rm=T)
340
mn=sum(!is.na(db))
341

  
342

  
343
md2=mean(db2,na.rm=T)
344

  
345

  
346
# Histogram equalization stretch
347
eqstretch<-function(img){
348
  ecdf<-ecdf(getValues(img))
349
  return(calc(img,fun=function(x) ecdf(x)*255))
350
}
351

  
352
ncol=100
353
plot(md,col=rainbow(ncol),breaks=quantile(as.matrix(md),seq(0,1,len=ncol-1),na.rm=T))
354

  
355
str(d)
356
plot(brick(d))
357

  
358

  
359

  
360

  
361

  
362

  
363

  
364
#################################################################
365
### start grass to process the files
366

  
367
#get bounding box of region in m
368
ge=SpatialPoints(data.frame(lon=c(-125,-115),lat=c(40,47)))
369
projection(ge)=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
370
ge2=spTransform(ge, CRS(" +proj=sinu +lon_0=0 +x_0=0 +y_0=0")); ge2
371

  
372

  
373

  
374
system("grass -text /media/data/grassdata/oregon/mod06")
375

  
376
### import variables one at a time
377
basedir=/home/adamw/acrobates/projects/interp/data/modis/MOD06_L2_tif
378

  
379
## parse the file names
380
fs=`ls data/modis/MOD06_L2_tif | grep tif$`
381
echo `echo $fs | wc -w` files to process
382

  
383
## example file
384
f=MOD06_L2.A2000062.1830.051.2010273075045.gscs_000500676719.tif
385

  
386

  
387
for f in $fs
388
do
389
year=`echo $f |cut -c 11-14`
390
day=`echo $f |cut -c 15-17 |sed 's/^0*//'`
391
time=`echo $f |cut -c 19-22`
392
month=$(date -d "`date +%Y`-01-01 +$(( ${day} - 1 ))days" +%m)
393
ofile=$month\_$year\_cloud_effective_radius_$f
394
r.in.gdal --quiet -e input=$basedir/$f output=$ofile band=1 title=cloud_effective_radius --overwrite
395
r.mapcalc "$ofile=if($ofile,$ofile,-9999,-9999)"
396
r.null --q map="$ofile" setnull=-9999
397
r.mapcalc "$ofile=$ofile*0.009999999776482582"
398
r.colors --quiet -ne map=$ofile color=precipitation
399
echo Finished $f
400
done
401

  
402
## generate monthly means
403
m02=`g.mlist type=rast pattern="02*"`
404
m02n=`echo $m02 | wc -w`
405

  
406
m02p=`printf '%q+' $m02 | sed 's/\(.*\)./\1/'`
407
r.mapcalc "m02=($m02p)/$m02n"
408

  
409
#  g.mremove -f rast=02*
345
)
410 346

  
411 347

  
climate/procedures/MOD06_L2_data_summary.r
18 18
library(lattice)
19 19
library(rgl)
20 20
library(hdf5)
21
library(rasterVis)
21 22

  
22 23
X11.options(type="Xlib")
23 24
ncores=20  #number of threads to use
......
27 28

  
28 29
roi=readOGR("data/regions/Test_sites/Oregon.shp","Oregon")
29 30
roi=spTransform(roi,CRS(" +proj=sinu +lon_0=0 +x_0=0 +y_0=0"))
31
roil=as(roi,"SpatialLines")
30 32

  
33
summarydatadir="data/modis/MOD06_climatologies"
31 34

  
32
### Get processed MOD06 nc files
33
datadir="data/modis/MOD06_L2_tif"
34 35

  
35
fs=data.frame(
36
  path=list.files(datadir,full=T,recursive=T,pattern="tif$"),
37
  file=basename(list.files(datadir,full=F,recursive=T,pattern="tif$")))
38
fs$date=as.Date(substr(fs$file,11,17),"%Y%j")
39
fs$time=substr(fs$file,19,22)
40
fs$datetime=as.POSIXct(strptime(paste(substr(fs$file,11,17),substr(fs$file,19,22)), '%Y%j %H%M'))
41
fs$path=as.character(fs$path)
42
fs$file=as.character(fs$file)
43 36

  
37
##########################
38
#### explore the data
39

  
40
## load data
41
months=seq(as.Date("2000-01-15"),as.Date("2000-12-15"),by="month")
42
cerfiles=list.files(summarydatadir,pattern="CER_mean_.*tif$",full=T); cerfiles
43
cer=brick(stack(cerfiles))
44
setZ(cer,months,name="time")
45
cer@z=list(months)
46
cer@zname="time"
47
layerNames(cer) <- as.character(format(months,"%b"))
48
#cer=projectRaster(from=cer,crs="+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0",method="ngb")
49
### TODO: change to bilinear!
50

  
51
cotfiles=list.files(summarydatadir,pattern="COT_mean_.*tif$",full=T); cotfiles
52
cot=brick(stack(cotfiles))
53
setZ(cot,months,name="time")
54
cot@z=list(months)
55
cot@zname="time"
56
layerNames(cot) <- as.character(format(months,"%b"))
57
#cot=projectRaster(from=cot,crs="+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0",method="ngb")
58
cotm=mean(cot,na.rm=T)
59
### TODO: change to bilinear!
44 60

  
45 61
### get station data
46 62
load("data/ghcn/roi_ghcn.Rdata")
47 63
load("data/allstations.Rdata")
48 64

  
49

  
50
d2=d[d$variable=="ppt"&d$date%in%fs$date,]
65
d2=d[d$variable=="ppt",]
51 66
d2=d2[,-grep("variable",colnames(d2)),]
52 67
st2=st[st$id%in%d$id,]
68
st2=spTransform(st2,CRS(" +proj=sinu +lon_0=0 +x_0=0 +y_0=0"))
53 69
d2[,c("lon","lat")]=coordinates(st2)[match(d2$id,st2$id),]
70
d2$month=format(d2$date,"%m")
71

  
72
### extract MOD06 data for each station
73
stcer=extract(cer,st2)#;colnames(stcer)=paste("cer_mean_",1:12,sep="")
74
stcot=extract(cot,st2)#;colnames(stcot)=paste("cot_mean_",1:12,sep="")
75
mod06=cbind.data.frame(id=st2$id,lat=st2$lat,lon=st2$lon,stcer,stcot)
76
mod06l=melt(mod06,id.vars=c("id","lon","lat"))
77
mod06l[,c("variable","moment","month")]=do.call(rbind,strsplit(as.character(mod06l$variable),"_"))
78
mod06l=cast(mod06l,id+lon+lat+month~variable+moment,value="value")
79

  
80
### generate monthly means of station data
81
dc=cast(d2,id+lon+lat~month,value="value",fun=function(x) mean(x,na.rm=T)*30)
82
dcl=melt(dc,id.vars=c("id","lon","lat"),value="ppt")
83

  
84
## merge station data with mod06
85
mod06s=merge(dcl,mod06l)
86
mod06s$lvalue=log(mod06s$value+1)
87

  
88

  
89
###################################################################
90
###################################################################
91
### draw some plots
92

  
93
bgyr=colorRampPalette(c("blue","green","yellow","red"))
94

  
95
pdf("output/MOD06_summary.pdf",width=11,height=8.5)
96

  
97
# CER maps
98
title="Cloud Effective Radius (microns)"
99
at=quantile(as.matrix(cer),seq(0,1,len=100),na.rm=T)
100
p=levelplot(cer,xlab.top=title,at=at,col.regions=bgyr(length(at)))+layer(sp.lines(roil, lwd=1.2, col='black'))
101
print(p)
102
bwplot(cer,main=title,ylab="Cloud Effective Radius (microns)")
103

  
104
# COT maps
105
title="Cloud Optical Thickness (%)"
106
at=quantile(as.matrix(cot),seq(0,1,len=100),na.rm=T)
107
p=levelplot(cot,xlab.top=title,at=at,col.regions=bgyr(length(at)))+layer(sp.lines(roil, lwd=0.8, col='black'))
108
print(p)
109
bwplot(cot,xlab.top=title,ylab="Cloud Optical Thickness (%)")
110

  
111

  
112
### Comparison at station values
113
at=quantile(as.matrix(cotm),seq(0,1,len=100),na.rm=T)
114
p=levelplot(cotm, layers=1,at=at,col.regions=bgyr(length(at)),main="Mean Annual Cloud Optical Thickness",FUN.margin=function(x) 0)+
115
  layer(sp.lines(roil, lwd=1.2, col='black'))+layer(sp.points(st2, pch=16, col='black'))
116
print(p)
117

  
118
### monthly comparisons of variables
119
mod06sl=melt(mod06s,measure.vars=c("value","COT_mean","CER_mean"))
120
bwplot(value~month|variable,data=mod06sl,cex=.5,pch=16,col="black",scales=list(y=list(relation="free")),layout=c(1,3))
121
splom(mod06s[grep("CER|COT",colnames(mod06s))],cex=.2,pch=16)
122

  
123
xyplot(value~CER_mean,data=mod06s,cex=.5,pch=16,col="black",scales=list(y=list(log=T)),main="Comparison of monthly mean CER and precipitation",ylab="Precipitation (log axis)")
124
xyplot(value~COT_mean,data=mod06s,cex=.5,pch=16,col="black",scales=list(y=list(log=T)),main="Comparison of monthly mean COT and precipitation",ylab="Precipitation (log axis)")
125

  
126
xyplot(value~CER_mean|month,data=mod06s,cex=.5,pch=16,col="black",scales=list(log=T,relation="free"))
127
xyplot(value~COT_mean|month,data=mod06s,cex=.5,pch=16,col="black",scales=list(log=T,relation="free"))
128

  
129
#xyplot(value~CER_mean|month+cut(COT_mean,breaks=c(0,10,20)),data=mod06s,cex=.5,pch=16,col="black")#,scales=list(relation="fixed"))
130

  
131

  
132
summary(lm(lvalue~COT_mean*month,data=mod06s))
133

  
134

  
135
dev.off()
136

  
137

  
138

  
139

  
140

  
141

  
142

  
143

  
144

  
145

  
54 146

  
55
## generate list split by day to merge with MOD06 data
56
d2l=split(d2,d2$date)
57

  
58
ds=do.call(rbind.data.frame,mclapply(d2l,function(td){
59
  print(td$date[1])
60
  tfs=fs[fs$date==td$date[1],]
61
  ## make spatial object
62
  tdsp=td
63
  coordinates(tdsp)=c("lon","lat")
64
  projection(tdsp)=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
65
  tdsp=spTransform(tdsp, CRS(" +proj=sinu +lon_0=0 +x_0=0 +y_0=0"))
66

  
67
  td2=do.call(rbind.data.frame,lapply(1:nrow(tfs),function(i){
68
    tnc1=brick(
69
      raster(tfs$path[i],varname="Cloud_Water_Path"),
70
      raster(tfs$path[i],varname="Cloud_Effective_Radius"),
71
      raster(tfs$path[i],varname="Cloud_Optical_Thickness"))
72
    projection(tnc1)=CRS(" +proj=sinu +lon_0=0 +x_0=0 +y_0=0")
73
    td3=as.data.frame(extract(tnc1,tdsp))
74
    if(ncol(td3)==1) return(NULL)
75
    colnames(td3)=
76
      c("Cloud_Water_Path","Cloud_Effective_Radius","Cloud_Optical_Thickness")
77
      ## drop negative values (need to check why these exist)
78
      td3[td3<0]=NA
79
    td3[,c("date","id")]=td[,c("date","id")]
80
      return(td3)
81
    }))
82
      td2=melt(td2,id.vars=c("date","id"))
83
      td2=cast(td2,date+id~variable,fun=mean,na.rm=T)
84
      td2=merge(td,td2)
85
  td2$id=as.character(td2$id)
86
  td2$date=as.character(td2$date)
87
      return(td2)
88
}))
89

  
90
colnames(ds)[grep("value",colnames(ds))]="ppt"
91
ds$ppt=as.numeric(ds$ppt)
92
ds$Cloud_Water_Path=as.numeric(ds$Cloud_Water_Path)
93
ds$Cloud_Effective_Radius=as.numeric(ds$Cloud_Effective_Radius)
94
ds$Cloud_Optical_Thickness=as.numeric(ds$Cloud_Optical_Thickness)
95
ds$date=as.Date(ds$date)
96
ds$year=as.numeric(ds$year)
97
ds$lon=as.numeric(ds$lon)
98
ds$lat=as.numeric(ds$lat)
99
ds=ds[!is.na(ds$ppt),]
100

  
101
save(ds,file="data/modis/pointsummary.Rdata")
102 147

  
103 148
load("data/modis/pointsummary.Rdata")
104 149

  

Also available in: Unified diff