Project

General

Profile

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