1
|
### Script to compile the monthly cloud data from earth engine into a netcdf file for further processing
|
2
|
|
3
|
setwd("~/acrobates/adamw/projects/cloud")
|
4
|
library(raster)
|
5
|
library(doMC)
|
6
|
|
7
|
registerDoMC(4)
|
8
|
beginCluster(4)
|
9
|
|
10
|
tempdir="tmp"
|
11
|
if(!file.exists(tempdir)) dir.create(tempdir)
|
12
|
|
13
|
|
14
|
## Load list of tiles
|
15
|
tiles=read.table("tile_lat_long_10d.txt",header=T)
|
16
|
|
17
|
jobs=expand.grid(tile=tiles$Tile,year=2000:2012,month=1:12)
|
18
|
jobs[,c("ULX","ULY","LRX","LRY")]=tiles[match(jobs$tile,tiles$Tile),c("ULX","ULY","LRX","LRY")]
|
19
|
|
20
|
## Run the python downloading script
|
21
|
#system("~/acrobates/adamw/projects/environmental-layers/climate/procedures/ee.MOD09.py -projwin -159 20 -154.5 18.5 -year 2001 -month 6 -region test")
|
22
|
i=6715
|
23
|
testtiles=c("h02v07","h02v06","h02v08","h03v07","h03v06")
|
24
|
todo=which(jobs$tile%in%testtiles)
|
25
|
#todo=todo[1:3]
|
26
|
#todo=1:nrow(jobs)
|
27
|
lapply(todo,function(i)
|
28
|
system(paste("~/acrobates/adamw/projects/environmental-layers/climate/procedures/ee.MOD09.py -projwin ",jobs$ULX[i]," ",jobs$ULY[i]," ",jobs$LRX[i]," ",jobs$LRY[i],
|
29
|
" -year ",jobs$year[i]," -month ",jobs$month[i]," -region ",jobs$tile[i],sep="")))
|
30
|
|
31
|
|
32
|
## Get list of available files
|
33
|
df=data.frame(path=list.files("/mnt/data2/projects/cloud/mod09",pattern="*.tif$",full=T),stringsAsFactors=F)
|
34
|
df[,c("region","year","month")]=do.call(rbind,strsplit(basename(df$path),"_|[.]"))[,c(1,2,3)]
|
35
|
df$date=as.Date(paste(df$year,"_",df$month,"_15",sep=""),"%Y_%m_%d")
|
36
|
|
37
|
## subset to testtiles?
|
38
|
df=df[df$region%in%testtiles,]
|
39
|
|
40
|
table(df$year,df$month)
|
41
|
|
42
|
## drop some if not complete
|
43
|
#df=df[df$year<=2009,]
|
44
|
rerun=T # set to true to recalculate all dates even if file already exists
|
45
|
|
46
|
## Loop over existing months to build composite netcdf files
|
47
|
foreach(date=unique(df$date)) %dopar% {
|
48
|
## get date
|
49
|
print(date)
|
50
|
## Define output and check if it already exists
|
51
|
ncfile=paste(tempdir,"/mod09_",date,".nc",sep="")
|
52
|
if(!rerun&file.exists(ncfile)) next
|
53
|
## merge regions to a new netcdf file
|
54
|
system(paste("gdal_merge.py -o ",ncfile," -n -32768 -of netCDF -ot Int16 ",paste(df$path[df$date==date],collapse=" ")))
|
55
|
system(paste("ncecat -O -u time ",ncfile," ",ncfile,sep=""))
|
56
|
## create temporary nc file with time information to append to MOD06 data
|
57
|
cat(paste("
|
58
|
netcdf time {
|
59
|
dimensions:
|
60
|
time = 1 ;
|
61
|
variables:
|
62
|
int time(time) ;
|
63
|
time:units = \"days since 2000-01-01 00:00:00\" ;
|
64
|
time:calendar = \"gregorian\";
|
65
|
time:long_name = \"time of observation\";
|
66
|
data:
|
67
|
time=",as.integer(date-as.Date("2000-01-01")),";
|
68
|
}"),file=paste(tempdir,"/",date,"_time.cdl",sep=""))
|
69
|
system(paste("ncgen -o ",tempdir,"/",date,"_time.nc ",tempdir,"/",date,"_time.cdl",sep=""))
|
70
|
system(paste("ncks -A ",tempdir,"/",date,"_time.nc ",ncfile,sep=""))
|
71
|
## add other attributes
|
72
|
system(paste("ncrename -v Band1,CF ",ncfile,sep=""))
|
73
|
system(paste("ncatted ",
|
74
|
" -a units,CF,o,c,\"Proportion Days Cloudy\" ",
|
75
|
# " -a valid_range,CF,o,b,\"0,100\" ",
|
76
|
" -a scale_factor,CF,o,f,\"0.1\" ",
|
77
|
" -a long_name,CF,o,c,\"Proportion cloudy days (%)\" ",
|
78
|
ncfile,sep=""))
|
79
|
|
80
|
## add the fillvalue attribute back (without changing the actual values)
|
81
|
#system(paste("ncatted -a _FillValue,CF,o,b,-32768 ",ncfile,sep=""))
|
82
|
|
83
|
if(as.numeric(system(paste("cdo -s ntime ",ncfile),intern=T))<1) {
|
84
|
print(paste(ncfile," has no time, deleting"))
|
85
|
file.remove(ncfile)
|
86
|
}
|
87
|
print(paste(basename(ncfile)," Finished"))
|
88
|
|
89
|
}
|
90
|
|
91
|
|
92
|
|
93
|
|
94
|
|
95
|
### merge all the tiles to a single global composite
|
96
|
#system(paste("ncdump -h ",list.files(tempdir,pattern="mod09.*.nc$",full=T)[10]))
|
97
|
system(paste("cdo -O mergetime ",paste(list.files(tempdir,pattern="mod09.*.nc$",full=T),collapse=" ")," data/mod09.nc"))
|
98
|
|
99
|
|
100
|
### generate the monthly mean and sd
|
101
|
#system(paste("cdo -P 10 -O merge -ymonmean data/mod09.nc -chname,CF,CF_sd -ymonstd data/mod09.nc data/mod09_clim.nc"))
|
102
|
system(paste("cdo -O -ymonmean data/mod09.nc data/mod09_clim_mean.nc"))
|
103
|
system(paste("cdo -O -chname,CF,CF_sd -ymonstd data/mod09.nc data/mod09_clim_sd.nc"))
|
104
|
|
105
|
# Overall mean
|
106
|
system(paste("cdo -O -chname,CF,CF_annual -timmean data/mod09.nc data/mod09_clim_mac.nc"))
|
107
|
|
108
|
### Long term summaries
|
109
|
seasconc <- function(x,return.Pc=T,return.thetat=F) {
|
110
|
#################################################################################################
|
111
|
## Precipitation Concentration function
|
112
|
## This function calculates Precipitation Concentration based on Markham's (1970) technique as described in Schulze (1997)
|
113
|
## South Africa Atlas of Agrohydology and Climatology - R E Schulze, M Maharaj, S D Lynch, B J Howe, and B Melvile-Thomson
|
114
|
## Pages 37-38
|
115
|
#################################################################################################
|
116
|
## x is a vector of precipitation quantities - the mean for each factor in "months" will be taken,
|
117
|
## so it does not matter if the data are daily or monthly, as long as the "months" factor correctly
|
118
|
## identifies them into 12 monthly bins, collapse indicates whether the data are already summarized as monthly means.
|
119
|
#################################################################################################
|
120
|
theta=seq(30,360,30)*(pi/180) # set up angles for each month & convert to radians
|
121
|
if(sum(is.na(x))==12) { return(cbind(Pc=NA,thetat=NA)) ; stop}
|
122
|
if(return.Pc) {
|
123
|
rt=sqrt(sum(x * cos(theta))^2 + sum(x * sin(theta))^2) # the magnitude of the summation
|
124
|
Pc=as.integer(round((rt/sum(x))*100))}
|
125
|
if(return.thetat){
|
126
|
s1=sum(x*sin(theta),na.rm=T); s2=sum(x*cos(theta),na.rm=T)
|
127
|
if(s1>=0 & s2>=0) {thetat=abs((180/pi)*(atan(sum(x*sin(theta),na.rm=T)/sum(x*cos(theta),na.rm=T))))}
|
128
|
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))))}
|
129
|
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))))}
|
130
|
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))))}
|
131
|
thetat=as.integer(round(thetat))
|
132
|
}
|
133
|
if(return.thetat&return.Pc) return(c(conc=Pc,theta=thetat))
|
134
|
if(return.Pc) return(Pc)
|
135
|
if(return.thetat) return(thetat)
|
136
|
}
|
137
|
|
138
|
|
139
|
## read in monthly dataset
|
140
|
mod09=brick("data/mod09_clim_mean.nc",varname="CF")
|
141
|
plot(mod09[1])
|
142
|
|
143
|
mod09_seas=calc(mod09,seasconc,return.Pc=T,return.thetat=F,overwrite=T,filename="data/mod09_seas.nc",NAflag=255,datatype="INT1U")
|
144
|
mod09_seas2=calc(mod09,seasconc,return.Pc=F,return.thetat=T,overwrite=T,filename="data/mod09_seas_theta.nc",datatype="INT1U")
|
145
|
|
146
|
plot(mod09_seas)
|