1
|
### Process a folder of daily MOD06 HDF files to produce a climatology
|
2
|
|
3
|
## import commandline arguments
|
4
|
library(getopt)
|
5
|
## get options
|
6
|
opta <- getopt(matrix(c(
|
7
|
'tile', 't', 1, 'character',
|
8
|
'verbose','v',1,'logical'
|
9
|
), ncol=4, byrow=TRUE))
|
10
|
|
11
|
tile=opta$tile #tile="h11v08"
|
12
|
verbose=opta$verbose #print out extensive information for debugging?
|
13
|
|
14
|
### directory containing daily files
|
15
|
outdir=paste("daily/",tile,"/",sep="") #directory for separate daily files
|
16
|
if(!file.exists(outdir)) dir.create(outdir)
|
17
|
|
18
|
### directory to hold climatology
|
19
|
outdir2="summary" #directory for combined daily files and summarized files
|
20
|
if(!file.exists(outdir2)) dir.create(outdir2)
|
21
|
|
22
|
### path to NCO
|
23
|
ncopath="/nasa/sles11/nco/4.0.8/gcc/mpt/bin/"
|
24
|
|
25
|
### Vector of variables that must be in file or they will be deleted.
|
26
|
### Formated as output from system(paste("cdo -s showvar ",fdly$path[i]),intern=T)
|
27
|
finalvars=" CER COT CLD"
|
28
|
|
29
|
|
30
|
################################################################################
|
31
|
## Get list of all daily files
|
32
|
if(verbose) print("Checking daily output in preparation for generating climatology")
|
33
|
|
34
|
fdly=data.frame(
|
35
|
path=list.files(outdir,pattern="nc$",full=T),
|
36
|
file=list.files(outdir,pattern="nc$"))
|
37
|
fdly$dateid=substr(fdly$file,14,21)
|
38
|
fdly$date=as.Date(substr(fdly$file,14,21),"%Y%m%d")
|
39
|
fdly$month=format(fdly$date,"%m")
|
40
|
fdly$year=format(fdly$date,"%Y")
|
41
|
nrow(fdly)
|
42
|
|
43
|
## check validity (via npar and ntime) of nc files
|
44
|
for(i in 1:nrow(fdly)){
|
45
|
fdly$ntime[i]<-as.numeric(system(paste("cdo -s ntime ",fdly$path[i]),intern=T))
|
46
|
fdly$npar[i]<-as.numeric(system(paste("cdo -s npar ",fdly$path[i]),intern=T))
|
47
|
fdly$fyear[i]<-as.numeric(system(paste("cdo -s showyear ",fdly$path[i]),intern=T))
|
48
|
fdly$fmonth[i]<-as.numeric(system(paste("cdo -s showmon ",fdly$path[i]),intern=T))
|
49
|
fdly$fvar[i]<-system(paste("cdo -s showvar ",fdly$path[i]),intern=T)
|
50
|
print(paste(i," out of ",nrow(fdly)," for year ", fdly$fyear[i]))
|
51
|
}
|
52
|
|
53
|
## print some summaries
|
54
|
if(verbose) print("Summary of available daily files")
|
55
|
print(table(fdly$fyear))
|
56
|
print(table(fdly$fmonth))
|
57
|
print(table(fdly$fvar))
|
58
|
|
59
|
## Identify which files failed test
|
60
|
fdly$drop=is.na(fdly$npar)|fdly$fvar!=finalvars
|
61
|
|
62
|
## delete files that fail check?
|
63
|
delete=T
|
64
|
if(delete) {
|
65
|
print(paste(sum(fdly$drop),"files will be deleted"))
|
66
|
file.remove(as.character(fdly$path[fdly$drop]))
|
67
|
}
|
68
|
## remove dropped files from list
|
69
|
fdly=fdly[!fdly$drop,]
|
70
|
|
71
|
#################################################################################
|
72
|
## Combine the year-by-year files into a single daily file in the summary directory (for archiving)
|
73
|
if(verbose) print("Merging daily files into single file output")
|
74
|
|
75
|
## create temporary directory to put intermediate files (will be deleted when R quits)
|
76
|
tsdir=paste(tempdir(),"/summary",sep="")
|
77
|
if(!file.exists(tsdir)) dir.create(tsdir,recursive=T)
|
78
|
|
79
|
## merge all daily files to create a single file with all dates
|
80
|
system(paste(ncopath,"ncrcat -O ",outdir,"/*nc ",outdir2,"/MOD06_",tile,"_daily.nc",sep=""))
|
81
|
|
82
|
## Update attributes
|
83
|
system(paste(ncopath,"ncatted ",
|
84
|
" -a units,time,o,c,\"days since 2000-1-1 0:0:0\" ",
|
85
|
" -a title,global,o,c,\"MODIS Cloud Product (MOD06) Daily Timeseries\" ",
|
86
|
" -a institution,global,o,c,\"Yale University\" ",
|
87
|
" -a source,global,o,c,\"MODIS Cloud Product (MOD06)\" ",
|
88
|
" -a comment,global,o,c,\"Compiled by Adam M. Wilson (adam.wilson@yale.edu)\" ",
|
89
|
outdir2,"/MOD06_",tile,"_daily.nc",sep=""))
|
90
|
|
91
|
### produce a monthly timeseries?
|
92
|
#system(paste("cdo -O monmean ",outdir2,"/MOD06_",tile,"_daily.nc ",tsdir,"/MOD06_",tile,"_monmean.nc",sep=""))
|
93
|
|
94
|
#############################
|
95
|
## Generate the Climatologies
|
96
|
if(verbose) print("Generate monthly climatologies")
|
97
|
|
98
|
myear=as.integer(max(fdly$year)) #this year will be used in all dates of monthly climatologies (and day will = 15)
|
99
|
|
100
|
## Monthly means
|
101
|
system(paste("cdo -O sorttimestamp -setyear,",myear," -setday,15 -ymonmean ",outdir2,"/MOD06_",tile,"_daily.nc ",tsdir,"/MOD06_",tile,"_ymonmean.nc",sep=""),wait=T)
|
102
|
|
103
|
## Monthly standard deviation
|
104
|
system(paste("cdo -O sorttimestamp -setyear,",myear," -setday,15 -ymonstd ",outdir2,"/MOD06_",tile,"_daily.nc ",tsdir,"/MOD06_",tile,"_ymonstd.nc",sep=""))
|
105
|
system(paste(ncopath,"ncrename -v CER,CER_sd -v CLD,CLD_sd -v COT,COT_sd ",tsdir,"/MOD06_",tile,"_ymonstd.nc",sep=""))
|
106
|
system(paste(ncopath,"ncatted ",
|
107
|
" -a long_name,CER_sd,o,c,\"Cloud Particle Effective Radius (standard deviation of daily observations)\" ",
|
108
|
" -a long_name,CLD_sd,o,c,\"Cloud Mask (standard deviation of daily observations)\" ",
|
109
|
" -a long_name,COT_sd,o,c,\"Cloud Optical Thickness (standard deviation of daily observations)\" ",
|
110
|
tsdir,"/MOD06_",tile,"_ymonstd.nc",sep=""))
|
111
|
|
112
|
## cer > 20
|
113
|
system(paste("cdo -O sorttimestamp -setyear,",myear," -setday,15 -ymonmean -gtc,20 -selvar,CER ",outdir2,"/MOD06_",tile,"_daily.nc ",tsdir,"/MOD06_",tile,"_ymoncer20.nc",sep=""))
|
114
|
system(paste(ncopath,"ncrename -v CER,CER20 ",tsdir,"/MOD06_",tile,"_ymoncer20.nc",sep=""))
|
115
|
system(paste(ncopath,"ncatted ",
|
116
|
" -a long_name,CER20,o,c,\"Proportion of Days with Cloud Particle Effective Radius > 20um\" ",
|
117
|
" -a units,CER20,o,c,\"Proportion\" ",
|
118
|
tsdir,"/MOD06_",tile,"_ymoncer20.nc",sep=""))
|
119
|
|
120
|
## number of observations
|
121
|
system(paste("cdo -O sorttimestamp -setyear,",myear," -setday,15 -nint -mulc,100 -ymonmean -eqc,9999 -setmisstoc,9999 -selvar,CER,CLD ",outdir2,"/MOD06_",tile,"_daily.nc ",tsdir,"/MOD06_",tile,"_ymonmiss.nc",sep=""))
|
122
|
system(paste(ncopath,"ncrename -v CER,CER_pmiss -v CLD,CLD_pmiss ",tsdir,"/MOD06_",tile,"_ymonmiss.nc",sep=""))
|
123
|
system(paste(ncopath,"ncatted ",
|
124
|
" -a long_name,CER_pmiss,o,c,\"Proportion of Days with missing data for CER\" ",
|
125
|
" -a long_name,CLD_pmiss,o,c,\"Proportion of Days with missing data for CLD\" ",
|
126
|
" -a scale_factor,CER_pmiss,o,d,0.01 ",
|
127
|
" -a units,CER_pmiss,o,c,\"Proportion\" ",
|
128
|
" -a scale_factor,CLD_pmiss,o,d,0.01 ",
|
129
|
" -a units,CLD_pmiss,o,c,\"Proportion\" ",
|
130
|
tsdir,"/MOD06_",tile,"_ymonmiss.nc",sep=""))
|
131
|
|
132
|
## append variables to a single file
|
133
|
if(verbose) print("Append all monthly climatologies into a single file")
|
134
|
system(paste(ncopath,"ncks -O ",tsdir,"/MOD06_",tile,"_ymonmean.nc ",tsdir,"/MOD06_",tile,"_ymon.nc",sep=""))
|
135
|
system(paste(ncopath,"ncks -A ",tsdir,"/MOD06_",tile,"_ymonstd.nc ",tsdir,"/MOD06_",tile,"_ymon.nc",sep=""))
|
136
|
system(paste(ncopath,"ncks -A ",tsdir,"/MOD06_",tile,"_ymoncer20.nc ",tsdir,"/MOD06_",tile,"_ymon.nc",sep=""))
|
137
|
system(paste(ncopath,"ncks -A ",tsdir,"/MOD06_",tile,"_ymonmiss.nc ",tsdir,"/MOD06_",tile,"_ymon.nc",sep=""))
|
138
|
|
139
|
## append sinusoidal grid from one of input files as CDO doesn't transfer all attributes
|
140
|
if(verbose) print("Clean up file (update attributes, flip latitudes, add grid description")
|
141
|
|
142
|
#system(paste(ncopath,"ncea -d time,0,1 -v sinusoidal ",list.files(outdir,full=T,pattern="[.]nc$")[1]," ",tsdir,"/sinusoidal.nc",sep=""))
|
143
|
#system(paste(ncopath,"ncks -A -d time,0,1 -v sinusoidal ",list.files(outdir,full=T,pattern="[.]nc$")[1]," ",tsdir,"/MOD06_",tile,"_ymon.nc",sep=""))
|
144
|
|
145
|
## invert latitude so it plays nicely with gdal
|
146
|
system(paste(ncopath,"ncpdq -O -a -y ",tsdir,"/MOD06_",tile,"_ymon.nc ",outdir2,"/MOD06_",tile,".nc",sep=""))
|
147
|
|
148
|
## update attributes
|
149
|
system(paste(ncopath,"ncatted ",
|
150
|
#" -a standard_parallel,sinusoidal,o,c,\"0\" ",
|
151
|
#" -a longitude_of_central_meridian,sinusoidal,o,c,\"0\" ",
|
152
|
#" -a latitude_of_central_meridian,sinusoidal,o,c,\"0\" ",
|
153
|
" -a units,time,o,c,\"days since 2000-1-1 0:0:0\" ",
|
154
|
" -a title,global,o,c,\"MODIS Cloud Product (MOD06) Climatology\" ",
|
155
|
" -a institution,global,o,c,\"Yale University\" ",
|
156
|
" -a source,global,o,c,\"MODIS Cloud Product (MOD06)\" ",
|
157
|
" -a comment,global,o,c,\"Compiled by Adam M. Wilson (adam.wilson@yale.edu)\" ",
|
158
|
outdir2,"/MOD06_",tile,".nc",sep=""))
|
159
|
|
160
|
|
161
|
print(paste("############################### Processed ",nrow(fdly),"days for tile:",tile," ###################################################"))
|
162
|
print("Years:")
|
163
|
print(table(fdly$fyear))
|
164
|
print("Months:")
|
165
|
print(table(fdly$fmonth))
|
166
|
|
167
|
## quit R
|
168
|
q("no")
|
169
|
|