1
|
### Script to compile the monthly cloud data from earth engine into a netcdf file for further processing
|
2
|
|
3
|
library(raster)
|
4
|
library(doMC)
|
5
|
library(multicore)
|
6
|
library(foreach)
|
7
|
#library(doMPI)
|
8
|
registerDoMC(4)
|
9
|
#beginCluster(4)
|
10
|
|
11
|
wd="~/acrobates/adamw/projects/cloud"
|
12
|
setwd(wd)
|
13
|
|
14
|
tempdir="tmp"
|
15
|
if(!file.exists(tempdir)) dir.create(tempdir)
|
16
|
|
17
|
## Get list of available files
|
18
|
df=data.frame(path=list.files("/mnt/data2/projects/cloud/mod09",pattern="*.tif$",full=T,recur=T),stringsAsFactors=F)
|
19
|
df[,c("region","year","month")]=do.call(rbind,strsplit(basename(df$path),"_|[.]"))[,c(1,2,3)]
|
20
|
df$date=as.Date(paste(df$year,"_",df$month,"_15",sep=""),"%Y_%m_%d")
|
21
|
|
22
|
## add stats to test for missing data
|
23
|
addstats=F
|
24
|
if(addstats){
|
25
|
df[,c("max","min","mean","sd")]=do.call(rbind.data.frame,mclapply(1:nrow(df),function(i) as.numeric(sub("^.*[=]","",grep("STATISTICS",system(paste("gdalinfo -stats",df$path[i]),inter=T),value=T)))))
|
26
|
table(df$sd==0)
|
27
|
}
|
28
|
|
29
|
## subset to testtiles?
|
30
|
#df=df[df$region%in%testtiles,]
|
31
|
#df=df[df$month==1,]
|
32
|
table(df$year,df$month)
|
33
|
|
34
|
writeLines(paste("Tiling options will produce",nrow(tiles),"tiles and ",nrow(jobs),"tile-months. Current todo list is ",length(todo)))
|
35
|
|
36
|
|
37
|
## drop some if not complete
|
38
|
#df=df[df$month%in%1:9&df$year%in%c(2001:2012),]
|
39
|
rerun=F # set to true to recalculate all dates even if file already exists
|
40
|
|
41
|
## Loop over existing months to build composite netcdf files
|
42
|
foreach(date=unique(df$date)) %dopar% {
|
43
|
## get date
|
44
|
print(date)
|
45
|
## Define output and check if it already exists
|
46
|
vrtfile=paste(tempdir,"/mod09_",date,".vrt",sep="")
|
47
|
ncfile=paste(tempdir,"/mod09_",date,".nc",sep="")
|
48
|
tffile=paste(tempdir,"/mod09_",date,".tif",sep="")
|
49
|
|
50
|
if(!rerun&file.exists(ncfile)) return(NA)
|
51
|
## merge regions to a new netcdf file
|
52
|
# system(paste("gdal_merge.py -o ",tffile," -init -32768 -n -32768.000 -ot Int16 ",paste(df$path[df$date==date],collapse=" ")))
|
53
|
system(paste("gdalbuildvrt -overwrite -srcnodata -32768 -vrtnodata -32768 ",vrtfile," ",paste(df$path[df$date==date],collapse=" ")))
|
54
|
## Warp to WGS84 grid and convert to netcdf
|
55
|
##ops="-t_srs 'EPSG:4326' -multi -r cubic -te -180 0 180 10 -tr 0.008333333333333 -0.008333333333333 -wo SOURCE_EXTRA=50" #-wo SAMPLE_GRID=YES -wo SAMPLE_STEPS=100
|
56
|
ops="-t_srs 'EPSG:4326' -multi -r cubic -te -180 -90 180 90 -tr 0.008333333333333 -0.008333333333333 -wo SOURCE_EXTRA=50"
|
57
|
|
58
|
system(paste("gdalwarp -overwrite ",ops," -et 0 -srcnodata -32768 -dstnodata -32768 ",vrtfile," ",tffile," -ot Int16"))
|
59
|
system(paste("gdal_translate -of netCDF ",tffile," ",ncfile))
|
60
|
# system(paste("gdalwarp -overwrite ",ops," -srcnodata -32768 -dstnodata -32768 -of netCDF ",vrtfile," ",tffile," -ot Int16"))
|
61
|
file.remove(tffile)
|
62
|
setwd(wd)
|
63
|
system(paste("ncecat -O -u time ",ncfile," ",ncfile,sep=""))
|
64
|
## create temporary nc file with time information to append to MOD06 data
|
65
|
cat(paste("
|
66
|
netcdf time {
|
67
|
dimensions:
|
68
|
time = 1 ;
|
69
|
variables:
|
70
|
int time(time) ;
|
71
|
time:units = \"days since 2000-01-01 00:00:00\" ;
|
72
|
time:calendar = \"gregorian\";
|
73
|
time:long_name = \"time of observation\";
|
74
|
data:
|
75
|
time=",as.integer(date-as.Date("2000-01-01")),";
|
76
|
}"),file=paste(tempdir,"/",date,"_time.cdl",sep=""))
|
77
|
system(paste("ncgen -o ",tempdir,"/",date,"_time.nc ",tempdir,"/",date,"_time.cdl",sep=""))
|
78
|
system(paste("ncks -A ",tempdir,"/",date,"_time.nc ",ncfile,sep=""))
|
79
|
## add other attributes
|
80
|
system(paste("ncrename -v Band1,CF ",ncfile,sep=""))
|
81
|
system(paste("ncatted ",
|
82
|
" -a units,CF,o,c,\"%\" ",
|
83
|
# " -a valid_range,CF,o,b,\"0,100\" ",
|
84
|
" -a scale_factor,CF,o,f,\"0.1\" ",
|
85
|
" -a _FillValue,CF,o,f,\"-32768\" ",
|
86
|
" -a long_name,CF,o,c,\"Cloud Frequency(%)\" ",
|
87
|
" -a NETCDF_VARNAME,CF,o,c,\"Cloud Frequency(%)\" ",
|
88
|
" -a title,global,o,c,\"Cloud Climatology from MOD09 Cloud Mask\" ",
|
89
|
" -a institution,global,o,c,\"Jetz Lab, EEB, Yale University, New Haven, CT\" ",
|
90
|
" -a source,global,o,c,\"Derived from MOD09GA Daily Data\" ",
|
91
|
" -a comment,global,o,c,\"Developed by Adam M. Wilson (adam.wilson@yale.edu / http://adamwilson.us)\" ",
|
92
|
ncfile,sep=""))
|
93
|
|
94
|
## add the fillvalue attribute back (without changing the actual values)
|
95
|
#system(paste("ncatted -a _FillValue,CF,o,b,-32768 ",ncfile,sep=""))
|
96
|
|
97
|
if(as.numeric(system(paste("cdo -s ntime ",ncfile),intern=T))<1) {
|
98
|
print(paste(ncfile," has no time, deleting"))
|
99
|
file.remove(ncfile)
|
100
|
}
|
101
|
print(paste(basename(ncfile)," Finished"))
|
102
|
|
103
|
|
104
|
}
|
105
|
|
106
|
|
107
|
### merge all the tiles to a single global composite
|
108
|
#system(paste("ncdump -h ",list.files(tempdir,pattern="mod09.*.nc$",full=T)[10]))
|
109
|
file.remove("tmp/mod09_2000-01-15.nc")
|
110
|
system(paste("cdo -O mergetime -setrtomiss,-32768,-1 ",paste(list.files(tempdir,pattern="mod09.*.nc$",full=T),collapse=" ")," data/cloud_monthly.nc"))
|
111
|
|
112
|
# Overall mean
|
113
|
system(paste("cdo -O timmean data/cloud_monthly.nc data/cloud_mean.nc"))
|
114
|
|
115
|
### generate the monthly mean and sd
|
116
|
#system(paste("cdo -P 10 -O merge -ymonmean data/mod09.nc -chname,CF,CF_sd -ymonstd data/mod09.nc data/mod09_clim.nc"))
|
117
|
system(paste("cdo -f nc4c -O -ymonmean data/cloud_monthly.nc data/cloud_ymonmean.nc"))
|
118
|
|
119
|
|
120
|
## Seasonal Means
|
121
|
system(paste("cdo -f nc4c -O -yseasmean data/cloud_monthly.nc data/cloud_yseasmean.nc"))
|
122
|
system(paste("cdo -f nc4c -O -yseasstd data/cloud_monthly.nc data/cloud_yseasstd.nc"))
|
123
|
|
124
|
## standard deviations, had to break to limit memory usage
|
125
|
system(paste("cdo -f nc4c -O -chname,CF,CF_sd -ymonstd -selmon,1,2,3,4,5,6 data/cloud_monthly.nc data/cloud_ymonsd_1-6.nc"))
|
126
|
system(paste("cdo -f nc4c -O -chname,CF,CF_sd -ymonstd -selmon,7,8,9,10,11,12 data/cloud_monthly.nc data/cloud_ymonsd_7-12.nc"))
|
127
|
system(paste("cdo -f nc4c -O mergetime data/cloud_ymonsd_1-6.nc data/cloud_ymonsd_7-12.nc data/cloud_ymonstd.nc"))
|
128
|
|
129
|
|
130
|
#if(!file.exists("data/mod09_metrics.nc")) {
|
131
|
system("cdo -f nc4c timmin data/cloud_ymonmean.nc data/cloud_min.nc")
|
132
|
system("cdo -f nc4c timmax data/cloud_ymonmean.nc data/cloud_max.nc")
|
133
|
system("cdo -f nc4c -chname,CF,CFsd -timstd data/cloud_ymonmean.nc data/cloud_std.nc")
|
134
|
# system("cdo -f nc2 merge data/mod09_std.nc data/mod09_min.nc data/cloud_max.nc data/cloud_metrics.nc")
|
135
|
# system("cdo merge -chname,CF,CFmin -timmin data/cloud_ymonmean.nc -chname,CF,CFmax -timmax data/cloud_ymonmean.nc -chname,CF,CFsd -timstd data/cloud_ymonmean.nc data/cloud_metrics.nc")
|
136
|
#}
|
137
|
|
138
|
|
139
|
# Regressions through time by season
|
140
|
s=c("DJF","MAM","JJA","SON")
|
141
|
|
142
|
system(paste("cdo -f nc4c -O regres -selseas,",s[1]," data/cloud_monthly.nc data/slope_",s[1],".nc &",sep=""))
|
143
|
system(paste("cdo -f nc4c -O regres -selseas,",s[2]," data/cloud_monthly.nc data/slope_",s[2],".nc &",sep=""))
|
144
|
system(paste("cdo -f nc4c -O regres -selseas,",s[3]," data/cloud_monthly.nc data/slope_",s[3],".nc &",sep=""))
|
145
|
system(paste("cdo -f nc4c -O regres -selseas,",s[4]," data/cloud_monthly.nc data/slope_",s[4],".nc &",sep=""))
|
146
|
|
147
|
|
148
|
|
149
|
|
150
|
|
151
|
### Long term summaries
|
152
|
seasconc <- function(x,return.Pc=T,return.thetat=F) {
|
153
|
#################################################################################################
|
154
|
## Precipitation Concentration function
|
155
|
## This function calculates Precipitation Concentration based on Markham's (1970) technique as described in Schulze (1997)
|
156
|
## South Africa Atlas of Agrohydology and Climatology - R E Schulze, M Maharaj, S D Lynch, B J Howe, and B Melvile-Thomson
|
157
|
## Pages 37-38
|
158
|
#################################################################################################
|
159
|
## x is a vector of precipitation quantities - the mean for each factor in "months" will be taken,
|
160
|
## so it does not matter if the data are daily or monthly, as long as the "months" factor correctly
|
161
|
## identifies them into 12 monthly bins, collapse indicates whether the data are already summarized as monthly means.
|
162
|
#################################################################################################
|
163
|
theta=seq(30,360,30)*(pi/180) # set up angles for each month & convert to radians
|
164
|
if(sum(is.na(x))==12) { return(cbind(Pc=NA,thetat=NA)) ; stop}
|
165
|
if(return.Pc) {
|
166
|
rt=sqrt(sum(x * cos(theta))^2 + sum(x * sin(theta))^2) # the magnitude of the summation
|
167
|
Pc=as.integer(round((rt/sum(x))*100))}
|
168
|
if(return.thetat){
|
169
|
s1=sum(x*sin(theta),na.rm=T); s2=sum(x*cos(theta),na.rm=T)
|
170
|
if(s1>=0 & s2>=0) {thetat=abs((180/pi)*(atan(sum(x*sin(theta),na.rm=T)/sum(x*cos(theta),na.rm=T))))}
|
171
|
if(s1>0 & s2<0) {thetat=180-abs((180/pi)*(atan(sum(x*sin(theta),na.rm=T)/sum(x*cos(theta),na.rm=T))))}
|
172
|
if(s1<0 & s2<0) {thetat=180+abs((180/pi)*(atan(sum(x*sin(theta),na.rm=T)/sum(x*cos(theta),na.rm=T))))}
|
173
|
if(s1<0 & s2>0) {thetat=360-abs((180/pi)*(atan(sum(x*sin(theta),na.rm=T)/sum(x*cos(theta),na.rm=T))))}
|
174
|
thetat=as.integer(round(thetat))
|
175
|
}
|
176
|
if(return.thetat&return.Pc) return(c(conc=Pc,theta=thetat))
|
177
|
if(return.Pc) return(Pc)
|
178
|
if(return.thetat) return(thetat)
|
179
|
}
|
180
|
|
181
|
|
182
|
|
183
|
## read in monthly dataset
|
184
|
mod09=brick("data/cloud_ymonmean.nc",varname="CF")
|
185
|
plot(mod09[1])
|
186
|
|
187
|
mod09_seas=calc(mod09,seasconc,return.Pc=T,return.thetat=F,overwrite=T,filename="data/mod09_seas.nc",NAflag=255,datatype="INT1U")
|
188
|
mod09_seas2=calc(mod09,seasconc,return.Pc=F,return.thetat=T,overwrite=T,filename="data/mod09_seas_theta.nc",datatype="INT1U")
|
189
|
|
190
|
plot(mod09_seas)
|