Project

General

Profile

Download (7.73 KB) Statistics
| Branch: | Revision:
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
## delete files that fail check?
60
delete=T
61
if(delete) {
62
  fdly$drop=is.na(fdly$npar)|fdly$fvar!=finalvars
63
  print(paste(sum(fdly$drop),"files will be deleted"))
64
  file.remove(as.character(fdly$path[fdly$drop]))
65
  fdly=fdly[!fdly$drop,]
66
}
67

    
68
#################################################################################
69
## Combine the year-by-year files into a single daily file in the summary directory (for archiving)
70
if(verbose) print("Merging daily files into single file output")
71

    
72
## create temporary directory to put intermediate files (will be deleted when R quits)
73
tsdir=paste(tempdir(),"/summary",sep="")
74
if(!file.exists(tsdir)) dir.create(tsdir,recursive=T)
75

    
76
## merge all daily files to create a single file with all dates
77
system(paste(ncopath,"ncrcat -O ",outdir,"/*nc ",outdir2,"/MOD06_",tile,"_daily.nc",sep=""))
78

    
79
## Update attributes
80
system(paste(ncopath,"ncatted ",
81
" -a units,time,o,c,\"days since 2000-1-1 0:0:0\" ",
82
" -a title,global,o,c,\"MODIS Cloud Product (MOD06) Daily Timeseries\" ",
83
" -a institution,global,o,c,\"Yale University\" ",
84
" -a source,global,o,c,\"MODIS Cloud Product (MOD06)\" ",
85
" -a comment,global,o,c,\"Compiled by Adam M. Wilson (adam.wilson@yale.edu)\" ",
86
outdir2,"/MOD06_",tile,"_daily.nc",sep=""))
87

    
88
### produce a monthly timeseries?
89
#system(paste("cdo -O monmean ",outdir2,"/MOD06_",tile,"_daily.nc ",tsdir,"/MOD06_",tile,"_monmean.nc",sep=""))
90

    
91
#############################
92
##  Generate the Climatologies
93
if(verbose) print("Generate monthly climatologies")
94

    
95
myear=as.integer(max(fdly$year))  #this year will be used in all dates of monthly climatologies (and day will = 15)
96

    
97
## Monthly means
98
system(paste("cdo -O sorttimestamp -setyear,",myear," -setday,15 -ymonmean ",outdir2,"/MOD06_",tile,"_daily.nc ",tsdir,"/MOD06_",tile,"_ymonmean.nc",sep=""),wait=T)
99

    
100
## Monthly standard deviation
101
system(paste("cdo -O sorttimestamp -setyear,",myear," -setday,15 -ymonstd ",outdir2,"/MOD06_",tile,"_daily.nc ",tsdir,"/MOD06_",tile,"_ymonstd.nc",sep=""))
102
system(paste(ncopath,"ncrename -v CER,CER_sd -v CLD,CLD_sd -v COT,COT_sd ",tsdir,"/MOD06_",tile,"_ymonstd.nc",sep=""))
103
system(paste(ncopath,"ncatted ",
104
" -a long_name,CER_sd,o,c,\"Cloud Particle Effective Radius (standard deviation of daily observations)\" ",
105
" -a long_name,CLD_sd,o,c,\"Cloud Mask (standard deviation of daily observations)\" ",
106
" -a long_name,COT_sd,o,c,\"Cloud Optical Thickness (standard deviation of daily observations)\" ",
107
tsdir,"/MOD06_",tile,"_ymonstd.nc",sep=""))
108

    
109
## cer > 20
110
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=""))
111
system(paste(ncopath,"ncrename -v CER,CER20 ",tsdir,"/MOD06_",tile,"_ymoncer20.nc",sep=""))
112
system(paste(ncopath,"ncatted ",
113
" -a long_name,CER20,o,c,\"Proportion of Days with Cloud Particle Effective Radius > 20um\" ",
114
" -a units,CER20,o,c,\"Proportion\" ",
115
tsdir,"/MOD06_",tile,"_ymoncer20.nc",sep=""))
116

    
117
## number of observations
118
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=""))
119
system(paste(ncopath,"ncrename -v CER,CER_pmiss -v CLD,CLD_pmiss ",tsdir,"/MOD06_",tile,"_ymonmiss.nc",sep=""))
120
system(paste(ncopath,"ncatted ",
121
" -a long_name,CER_pmiss,o,c,\"Proportion of Days with missing data for CER\" ",
122
" -a long_name,CLD_pmiss,o,c,\"Proportion of Days with missing data for CLD\" ",
123
" -a scale_factor,CER_pmiss,o,d,0.01 ",
124
" -a units,CER_pmiss,o,c,\"Proportion\" ",
125
" -a scale_factor,CLD_pmiss,o,d,0.01 ",
126
" -a units,CLD_pmiss,o,c,\"Proportion\" ",
127
tsdir,"/MOD06_",tile,"_ymonmiss.nc",sep=""))
128

    
129
## append variables to a single file
130
if(verbose) print("Append all monthly climatologies into a single file")
131
system(paste(ncopath,"ncks -O ",tsdir,"/MOD06_",tile,"_ymonmean.nc  ",tsdir,"/MOD06_",tile,"_ymon.nc",sep=""))
132
system(paste(ncopath,"ncks -A ",tsdir,"/MOD06_",tile,"_ymonstd.nc  ",tsdir,"/MOD06_",tile,"_ymon.nc",sep=""))
133
system(paste(ncopath,"ncks -A ",tsdir,"/MOD06_",tile,"_ymoncer20.nc  ",tsdir,"/MOD06_",tile,"_ymon.nc",sep=""))
134
system(paste(ncopath,"ncks -A ",tsdir,"/MOD06_",tile,"_ymonmiss.nc  ",tsdir,"/MOD06_",tile,"_ymon.nc",sep=""))
135

    
136
## append sinusoidal grid from one of input files as CDO doesn't transfer all attributes
137
if(verbose) print("Clean up file (update attributes, flip latitudes, add grid description")
138

    
139
#system(paste(ncopath,"ncea -d time,0,1 -v sinusoidal ",list.files(outdir,full=T,pattern="[.]nc$")[1],"  ",tsdir,"/sinusoidal.nc",sep=""))
140
#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=""))
141

    
142
## invert latitude so it plays nicely with gdal
143
system(paste(ncopath,"ncpdq -O -a -y ",tsdir,"/MOD06_",tile,"_ymon.nc ",outdir2,"/MOD06_",tile,".nc",sep=""))
144

    
145
## update attributes
146
system(paste(ncopath,"ncatted ",
147
#" -a standard_parallel,sinusoidal,o,c,\"0\" ",
148
#" -a longitude_of_central_meridian,sinusoidal,o,c,\"0\" ",
149
#" -a latitude_of_central_meridian,sinusoidal,o,c,\"0\" ",
150
" -a units,time,o,c,\"days since 2000-1-1 0:0:0\" ",
151
" -a title,global,o,c,\"MODIS Cloud Product (MOD06) Climatology\" ",
152
" -a institution,global,o,c,\"Yale University\" ",
153
" -a source,global,o,c,\"MODIS Cloud Product (MOD06)\" ",
154
" -a comment,global,o,c,\"Compiled by Adam M. Wilson (adam.wilson@yale.edu)\" ",
155
outdir2,"/MOD06_",tile,".nc",sep=""))
156

    
157

    
158
print(paste("###############################  Processed ",nrow(fdly),"days for tile:",tile," ###################################################"))
159
print("Years:")
160
print(table(fdly$fyear))
161
print("Months:")
162
print(table(fdly$fmonth))
163

    
164
## quit R
165
q("no")
166
 
(4-4/23)