Project

General

Profile

Download (6.95 KB) Statistics
| Branch: | Revision:
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

    
21
jobs=jobs[jobs$month==1,]
22
## 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
todo=todo[1:3]
28
todo=1:nrow(jobs)
29
#todo=todo[500:503]
30
mclapply(todo,function(i)
31
         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
       "  -year ",jobs$year[i]," -month ",jobs$month[i]," -region ",jobs$tile[i],sep="")),mc.cores=1)
33

    
34

    
35
##  Get list of available files
36
df=data.frame(path=list.files("/mnt/data2/projects/cloud/mod09",pattern="*.tif$",full=T),stringsAsFactors=F)
37
df[,c("region","year","month")]=do.call(rbind,strsplit(basename(df$path),"_|[.]"))[,c(1,2,3)]
38
df$date=as.Date(paste(df$year,"_",df$month,"_15",sep=""),"%Y_%m_%d")
39

    
40
## subset to testtiles?
41
#df=df[df$region%in%testtiles,]
42
df=df[df$month==1,]
43
table(df$year,df$month)
44

    
45
## drop some if not complete
46
#df=df[df$year<=2009,]
47
rerun=T  # set to true to recalculate all dates even if file already exists
48

    
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
  if(!rerun&file.exists(ncfile)) next
56
  ## merge regions to a new netcdf file
57
  system(paste("gdal_merge.py -o ",ncfile," -n -32768 -of netCDF -ot Int16 ",paste(df$path[df$date==date],collapse=" ")))
58
  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
               " -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

    
86
if(as.numeric(system(paste("cdo -s ntime ",ncfile),intern=T))<1) {
87
  print(paste(ncfile," has no time, deleting"))
88
  file.remove(ncfile)
89
}
90
  print(paste(basename(ncfile)," Finished"))
91

    
92
}
93

    
94

    
95

    
96

    
97

    
98
### 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
#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

    
108
#  Overall mean
109
system(paste("cdo -O  -chname,CF,CF_annual -timmean data/mod09.nc  data/mod09_clim_mac.nc"))
110

    
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
mod09=brick("data/mod09_clim_mean.nc",varname="CF")
144
plot(mod09[1])
145

    
146
mod09_seas=calc(mod09,seasconc,return.Pc=T,return.thetat=F,overwrite=T,filename="data/mod09_seas.nc",NAflag=255,datatype="INT1U")
147
mod09_seas2=calc(mod09,seasconc,return.Pc=F,return.thetat=T,overwrite=T,filename="data/mod09_seas_theta.nc",datatype="INT1U")
148

    
149
plot(mod09_seas)
(47-47/50)