Project

General

Profile

Download (5.93 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
##  Get list of available files
14
df=data.frame(path=list.files("/mnt/data2/projects/cloud/mod09",pattern="*.tif$",full=T),stringsAsFactors=F)
15
df[,c("region","year","month")]=do.call(rbind,strsplit(basename(df$path),"_|[.]"))[,c(2,3,4)]
16
df$date=as.Date(paste(df$year,"_",df$month,"_15",sep=""),"%Y_%m_%d")
17

    
18
table(df$year,df$month)#,df$region)
19

    
20
## drop some if not complete
21
#df=df[df$year<=2009,]
22
rerun=T  # set to true to recalculate all dates even if file already exists
23

    
24
## Loop over existing months to build composite netcdf files
25
foreach(date=unique(df$date)) %dopar% {
26
## get date
27
  print(date)
28
  ## Define output and check if it already exists
29
  ncfile=paste(tempdir,"/mod09_",date,".nc",sep="")
30
  if(!rerun&file.exists(ncfile)) next
31
  ## merge regions to a new netcdf file
32
  system(paste("gdal_merge.py -o ",ncfile," -of netCDF -ot Byte ",paste(df$path[df$date==date],collapse=" ")))
33
  system(paste("ncecat -O -u time ",ncfile," ",ncfile,sep=""))
34
## create temporary nc file with time information to append to MOD06 data
35
  cat(paste("
36
    netcdf time {
37
      dimensions:
38
        time = 1 ;
39
      variables:
40
        int time(time) ;
41
      time:units = \"days since 2000-01-01 00:00:00\" ;
42
      time:calendar = \"gregorian\";
43
      time:long_name = \"time of observation\"; 
44
    data:
45
      time=",as.integer(date-as.Date("2000-01-01")),";
46
    }"),file=paste(tempdir,"/",date,"_time.cdl",sep=""))
47
system(paste("ncgen -o ",tempdir,"/",date,"_time.nc ",tempdir,"/",date,"_time.cdl",sep=""))
48
system(paste("ncks -A ",tempdir,"/",date,"_time.nc ",ncfile,sep=""))
49
## add other attributes
50
  system(paste("ncrename -v Band1,CF ",ncfile,sep=""))
51
  system(paste("ncatted ",
52
" -a units,CF,o,c,\"Proportion Days Cloudy\" ",
53
" -a valid_range,CF,o,b,\"0,100\" ",
54
" -a long_name,CF,o,c,\"Proportion cloudy days (%)\" ",
55
ncfile,sep=""))
56
#" -a missing_value,CF,o,b,0 ",
57
#" -a _FillValue,CF,o,b,0 ", 
58
## add the fillvalue attribute back (without changing the actual values)
59
#system(paste("ncatted -a _FillValue,CF,o,b,255 ",ncfile,sep=""))
60

    
61
if(as.numeric(system(paste("cdo -s ntime ",ncfile),intern=T))<1) {
62
  print(paste(ncfile," has no time, deleting"))
63
  file.remove(ncfile)
64
}
65
  print(paste(basename(ncfile)," Finished"))
66

    
67
}
68

    
69

    
70

    
71

    
72

    
73
### merge all the tiles to a single global composite
74
#system(paste("ncdump -h ",list.files(tempdir,pattern="mod09.*.nc$",full=T)[10]))
75
system(paste("cdo -O mergetime ",paste(list.files(tempdir,pattern="mod09.*.nc$",full=T),collapse=" ")," data/mod09.nc"))
76

    
77

    
78
### generate the monthly mean and sd
79
#system(paste("cdo -P 10 -O merge -ymonmean data/mod09.nc -chname,CF,CF_sd -ymonstd data/mod09.nc data/mod09_clim.nc"))
80
system(paste("cdo  -O -ymonmean data/mod09.nc data/mod09_clim_mean.nc"))
81
system(paste("cdo  -O -chname,CF,CF_sd -ymonstd data/mod09.nc data/mod09_clim_sd.nc"))
82

    
83
#  Overall mean
84
system(paste("cdo -O  -chname,CF,CF_annual -timmean data/mod09.nc  data/mod09_clim_mac.nc"))
85

    
86
### Long term summaries
87
seasconc <- function(x,return.Pc=T,return.thetat=F) {
88
          #################################################################################################
89
          ## Precipitation Concentration function
90
          ## This function calculates Precipitation Concentration based on Markham's (1970) technique as described in Schulze (1997)
91
          ## South Africa Atlas of Agrohydology and Climatology - R E Schulze, M Maharaj, S D Lynch, B J Howe, and B Melvile-Thomson
92
          ## Pages 37-38
93
          #################################################################################################
94
          ## x is a vector of precipitation quantities - the mean for each factor in "months" will be taken,
95
          ## so it does not matter if the data are daily or monthly, as long as the "months" factor correctly
96
          ## identifies them into 12 monthly bins, collapse indicates whether the data are already summarized as monthly means.
97
          #################################################################################################
98
          theta=seq(30,360,30)*(pi/180)                                       # set up angles for each month & convert to radians
99
                  if(sum(is.na(x))==12) { return(cbind(Pc=NA,thetat=NA)) ; stop}
100
                  if(return.Pc) {
101
                              rt=sqrt(sum(x * cos(theta))^2 + sum(x * sin(theta))^2)    # the magnitude of the summation
102
                                        Pc=as.integer(round((rt/sum(x))*100))}
103
                  if(return.thetat){
104
                              s1=sum(x*sin(theta),na.rm=T); s2=sum(x*cos(theta),na.rm=T)
105
                                        if(s1>=0 & s2>=0)  {thetat=abs((180/pi)*(atan(sum(x*sin(theta),na.rm=T)/sum(x*cos(theta),na.rm=T))))}
106
                                        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))))}
107
                                        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))))}
108
                                        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))))}
109
                             thetat=as.integer(round(thetat))
110
                            }
111
                  if(return.thetat&return.Pc) return(c(conc=Pc,theta=thetat))
112
                  if(return.Pc)          return(Pc)
113
                  if(return.thetat)  return(thetat)
114
        }
115

    
116

    
117
## read in monthly dataset
118
mod09=brick("data/mod09_clim_mean.nc",varname="CF")
119
plot(mod09[1])
120

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

    
124
plot(mod09_seas)
(44-44/47)