Project

General

Profile

« Previous | Next » 

Revision e7295f5b

Added by Benoit Parmentier almost 11 years ago

initial commit for split of source script containing functions for multi-timescales analyses paper

View differences:

climate/research/oregon/interpolation/multi_timescales_paper_interpolation_functions.R
1
####################################  INTERPOLATION OF TEMPERATURES  #######################################
2
############################  Script for manuscript analyses,tables and figures #######################################
3
#This script reads information concerning the Oregon case study to adapt data for the revised 
4
# interpolation code.
5
#Functions used in the production of figures and data for the multi timescale paper are recorded.
6
#AUTHOR: Benoit Parmentier                                                                      #
7
#DATE: 11/25/2013            
8
#Version: 1
9
#PROJECT: Environmental Layers project                                       #
10
#################################################################################################
11

  
12
###Loading R library and packages                                                      
13
library(gtools)                              # loading some useful tools 
14
library(mgcv)                                # GAM package by Simon Wood
15
library(sp)                                  # Spatial pacakge with class definition by Bivand et al.
16
library(spdep)                               # Spatial pacakge with methods and spatial stat. by Bivand et al.
17
library(rgdal)                               # GDAL wrapper for R, spatial utilities
18
library(gstat)                               # Kriging and co-kriging by Pebesma et al.
19
library(fields)                              # NCAR Spatial Interpolation methods such as kriging, splines
20
library(raster)                              # Hijmans et al. package for raster processing
21
library(gdata)                               # various tools with xls reading
22
library(rasterVis)
23
library(parallel)
24
library(maptools)
25
library(maps)
26
library(reshape)
27
library(plotrix)
28
library(plyr)
29

  
30
#### FUNCTION USED IN SCRIPT
31

  
32
function_analyses_paper <-"multi_timescales_paper_interpolation_functions_11252013.R"
33

  
34
plot_transect_m2<-function(list_trans,r_stack,title_plot,disp=FALSE,m_layers){
35
  #This function creates plot of transects for stack of raster images.
36
  #Arguments:
37
  #list_trans: list of files containing the transects lines in shapefile format
38
  #r_stack: raster stack containing the information to extect
39
  #title_plot: plot title
40
  #disp: display and save from X11 if TRUE or plot to png file if FALSE
41
  #m_layers: index for layerers containing alternate units to be drawned on a differnt scale
42
  #RETURN:
43
  #list containing transect information
44
  
45
  nb<-length(list_trans) #number of transects
46
  t_col<-rainbow(nb)
47
  t_col<-c("red","green","black")
48
  lty_list<-c("dashed","solid","dotted")
49
  list_trans_data<-vector("list",nb)
50
  
51
  #For scale 1
52
  for (i in 1:nb){
53
    trans_file<-list_trans[[i]][1]
54
    filename<-sub(".shp","",trans_file)             #Removing the extension from file.
55
    transect<-readOGR(dirname(filename), basename(filename))                 #reading shapefile 
56
    trans_data<-extract(r_stack, transect)
57
    if (disp==FALSE){
58
      png(file=paste(list_trans[[i]][2],".png",sep=""),
59
          height=480*1,width=480*2)
60
    }
61
    #Plot layer values for specific transect
62
    for (k in 1:ncol(trans_data[[1]])){
63
      y<-trans_data[[1]][,k]
64
      x<-1:length(y)
65
      m<-match(k,m_layers)
66
      
67
      if (k==1 & is.na(m)){
68
        plot(x,y,type="l",xlab="transect distance from coastal origin (km)", ylab=" maximum temperature (degree C)",
69
             ,cex=1.2,col=t_col[k])
70
        #axis(2)
71
      }
72
      if (k==1 & !is.na(m)){
73
        plot(x,y,type="l",col=t_col[k],lty="dotted",axes=F) #plotting fusion profile
74
        #axis(4,xlab="",ylab="elevation(m)")  
75
        axis(4,cex=1.2)
76
      }
77
      if (k!=1 & is.na(m)){
78
        #par(new=TRUE)              # new plot without erasing old
79
        lines(x,y,type="l",xlab="",ylab="",col=t_col[k],axes=F) #plotting fusion profile
80
        #axis(2,xlab="",ylab="tmax (in degree C)")
81
      }
82
      if (k!=1 & !is.na(m)){
83
        par(new=TRUE)              # key: ask for new plot without erasing old
84
        plot(x,y,type="l",col=t_col[k],xlab="",ylab="",lty="dotted",axes=F) #plotting fusion profile
85
        #axis(4,xlab="",ylab="elevation(m)")  
86
        axis(4,cex=1.2)
87
      } 
88
    }
89
    title(title_plot[i])
90
    legend("topleft",legend=names(r_stack)[1:2], 
91
           cex=1.2, col=t_col,lty=1,bty="n")
92
    legend("topright",legend=names(r_stack)[3], 
93
           cex=1.2, col=t_col[3],lty="dotted",bty="n")
94
    if (disp==TRUE){
95
      savePlot(file=paste(list_trans[[i]][2],".png",sep=""),type="png")
96
    }
97
    if (disp==FALSE){
98
      dev.off()
99
    }
100
    list_trans_data[[i]]<-trans_data
101
  }
102
  names(list_trans_data)<-names(list_trans)
103
  return(list_trans_data)
104
}
105

  
106

  
107
### generate filter for Moran's I function in raster package
108
autocor_filter_fun <-function(no_lag=1,f_type="queen"){
109
  if(f_type=="queen"){
110
    no_rows <- 2*no_lag +1
111
    border_row <-rep(1,no_rows)
112
    other_row <- c(1,rep(0,no_rows-2),1)
113
    other_rows <- rep(other_row,no_rows-2)
114
    mat_data<- c(border_row,other_rows,border_row)
115
    autocor_filter<-matrix(mat_data,nrow=no_rows)
116
  }
117
  #if(f_type=="rook){} #add later
118
  return(autocor_filter)
119
}
120
#MODIFY: calculate for multiple dates and create averages...
121
#Now run Moran's I for raster image given a list of  filters for different lags and raster stack
122
moran_multiple_fun<-function(i,list_param){
123
  #Parameters:
124
  #list_filters: list of filters with different lags in the image
125
  #r_stack: stack of raster image, only the selected layer is used...
126
  list_filters <-list_param$list_filters
127
  r <- subset(list_param$r_stack,i)
128
  moran_list <- lapply(list_filters,FUN=Moran,x=r)
129
  moran_v <-as.data.frame(unlist(moran_list))
130
  names(moran_v)<-names(r)
131
  return(moran_v)
132
}
133

  
134
#Modfiy...temporal plot for 1,10,20
135
stat_moran_std_raster_fun<-function(i){
136
  list_var_stat<-vector("list",ncol(lf_list))
137
  for (k in 1:length(lf_list)){
138
    
139
    raster_pred<-raster(lf_list[i,k]) 
140
    tmp_rast<-mask(raster_pred,mask_rast)
141
    #tmp_rast<-raster_pred
142
    raster_pred2<-tmp_rast
143
    
144
    t1<-cellStats(raster_pred,na.rm=TRUE,stat=sd)    #Calculating the standard deviation for the 
145
    m1<-Moran(raster_pred,w=3) #Calculating Moran's I with window of 3 an default Queen's case
146
    stat<-as.data.frame(t(c(m1,t1)))
147
    names(stat)<-c("moranI","std")
148
    list_var_stat[[k]]<-stat
149
  }
150
  dat_var_stat<-do.call(rbind,list_var_stat)
151
  dat_var_stat$lf_names<-names(lf_list)
152
  dat_var_stat$dates<-dates[i]
153
  return(dat_var_stat)
154
}
155

  
156

  
157
################### END OF SCRIPT ###################
158

  
159

  

Also available in: Unified diff