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