Project

General

Profile

Download (6.88 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
## 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 9a19743f Adam M. Wilson
##  Get list of available files
33 d7dc526e Adam M. Wilson
df=data.frame(path=list.files("/mnt/data2/projects/cloud/mod09",pattern="*.tif$",full=T),stringsAsFactors=F)
34 f5ef0596 Adam M. Wilson
df[,c("region","year","month")]=do.call(rbind,strsplit(basename(df$path),"_|[.]"))[,c(1,2,3)]
35 9a19743f Adam M. Wilson
df$date=as.Date(paste(df$year,"_",df$month,"_15",sep=""),"%Y_%m_%d")
36
37 f5ef0596 Adam M. Wilson
## subset to testtiles?
38
df=df[df$region%in%testtiles,]
39
40
table(df$year,df$month)
41 d7dc526e Adam M. Wilson
42 9a19743f Adam M. Wilson
## drop some if not complete
43 d7dc526e Adam M. Wilson
#df=df[df$year<=2009,]
44
rerun=T  # set to true to recalculate all dates even if file already exists
45 9a19743f Adam M. Wilson
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 d7dc526e Adam M. Wilson
  if(!rerun&file.exists(ncfile)) next
53 9a19743f Adam M. Wilson
  ## merge regions to a new netcdf file
54 f5ef0596 Adam M. Wilson
  system(paste("gdal_merge.py -o ",ncfile," -n -32768 -of netCDF -ot Int16 ",paste(df$path[df$date==date],collapse=" ")))
55 9a19743f Adam M. Wilson
  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 f5ef0596 Adam M. Wilson
               " -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 9a19743f Adam M. Wilson
83
if(as.numeric(system(paste("cdo -s ntime ",ncfile),intern=T))<1) {
84
  print(paste(ncfile," has no time, deleting"))
85 d7dc526e Adam M. Wilson
  file.remove(ncfile)
86 9a19743f Adam M. Wilson
}
87
  print(paste(basename(ncfile)," Finished"))
88
89
}
90
91 d7dc526e Adam M. Wilson
92
93
94
95 9a19743f Adam M. Wilson
### 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 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"))
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 d7dc526e Adam M. Wilson
105
#  Overall mean
106 d86b0a4a Adam M. Wilson
system(paste("cdo -O  -chname,CF,CF_annual -timmean data/mod09.nc  data/mod09_clim_mac.nc"))
107 d7dc526e Adam M. Wilson
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 d86b0a4a Adam M. Wilson
mod09=brick("data/mod09_clim_mean.nc",varname="CF")
141 d7dc526e Adam M. Wilson
plot(mod09[1])
142 9a19743f Adam M. Wilson
143 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")
144 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")
145 9a19743f Adam M. Wilson
146 d7dc526e Adam M. Wilson
plot(mod09_seas)