Project

General

Profile

« Previous | Next » 

Revision 079bc1c5

Added by Benoit Parmentier over 11 years ago

GAM CAI added to script and modification of function for daily deviation

View differences:

climate/research/oregon/interpolation/GAM_fusion_function_multisampling.R
3 3
#This script contains 5 functions used in the interpolation of temperature in the specfied study/processing area:                             
4 4
# 1)predict_raster_model<-function(in_models,r_stack,out_filename)                                                             
5 5
# 2)fit_models<-function(list_formulas,data_training)           
6
# 3)runClimCAI<-function(j) : not working yet
6
# 3)runClim_KGCAI<-function(j,list_param) : function that peforms GAM CAI method
7 7
# 4)runClim_KGFusion<-function(j,list_param) function for monthly step (climatology) in the fusion method
8 8
# 5)runGAMFusion <- function(i,list_param) : daily step for fusion method, perform daily prediction
9 9
#
10 10
#AUTHOR: Benoit Parmentier                                                                       
11
#DATE: 04/02/2013                                                                                 
11
#DATE: 04/20/2013                                                                                 
12 12
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363--   
13 13

  
14 14
##Comments and TODO:
......
60 60
####
61 61
#TODO:
62 62
#Add log file and calculate time and sizes for processes-outputs
63
runClimCAI<-function(j,list_param){
63
runClim_KGCAI <-function(j,list_param){
64 64

  
65 65
  #Make this a function with multiple argument that can be used by mcmapply??
66 66
  #Arguments: 
......
103 103
  LST_name<-lst_avg[j] # name of LST month to be matched
104 104
  data_month$LST<-data_month[[LST_name]]
105 105
  
106
  #Create formulas object from list of characters...
106 107
  list_formulas<-lapply(list_models,as.formula,env=.GlobalEnv) #mulitple arguments passed to lapply!!  
107 108

  
108 109
  #TMax to model...
......
112 113
  if (var=="TMIN"){   
113 114
    data_month$y_var<-data_month$TMin #Adding TMin as the variable modeled
114 115
  }
115

  
116
  #Fit gam models using data and list of formulas
116 117
  mod_list<-fit_models(list_formulas,data_month) #only gam at this stage
117 118
  cname<-paste("mod",1:length(mod_list),sep="") #change to more meaningful name?
118 119
  names(mod_list)<-cname
......
122 123
  s_raster<-dropLayer(s_raster,pos)      # If it exists drop layer
123 124
  LST<-subset(s_raster,LST_name)
124 125
  names(LST)<-"LST"
125
  #Screen for extreme values": this needs more thought, min and max val vary with regions
126
  #min_val<-(-15+273.16) #if values less than -15C then screen out (note the Kelvin units that will need to be changed later in all datasets)
127
  #r1[r1 < (min_val)]<-NA
128 126
  s_raster<-addLayer(s_raster,LST)            #Adding current month
129 127
  
130 128
  #Now generate file names for the predictions...
......
135 133
    #j indicate which month is predicted
136 134
    data_name<-paste(var,"_clim_month_",j,"_",cname[k],"_",prop_month,
137 135
                     "_",run_samp,sep="")
138
    raster_name<-paste("fusion_",data_name,out_prefix,".tif", sep="")
136
    raster_name<-paste("CAI_",data_name,out_prefix,".tif", sep="")
139 137
    list_out_filename[[k]]<-raster_name
140 138
  }
141 139
  
142 140
  #now predict values for raster image...
143 141
  rast_clim_list<-predict_raster_model(mod_list,s_raster,list_out_filename)
144 142
  names(rast_clim_list)<-cname
145
  #Some modles will not be predicted...remove them
143
  #Some models will not be predicted because of the lack of training data...remove empty string from list of models
146 144
  rast_clim_list<-rast_clim_list[!sapply(rast_clim_list,is.null)] #remove NULL elements in list
147 145
  
148 146
  #Adding Kriging for Climatology options
......
164 162
  #clim_rast<-LST-bias_rast
165 163
  data_name<-paste(var,"_clim_month_",j,"_",model_name,"_",prop_month,
166 164
                   "_",run_samp,sep="")
167
  raster_name_clim<-paste("fusion_",data_name,out_prefix,".tif", sep="")
165
  raster_name_clim<-paste("CAI_",data_name,out_prefix,".tif", sep="")
168 166
  writeRaster(clim_rast, filename=raster_name_clim,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
169 167
  
170 168
  #Adding to current objects
......
176 174
  clim_obj<-list(rast_clim_list,data_month,mod_list,list_formulas)
177 175
  names(clim_obj)<-c("clim","data_month","mod","formulas")
178 176
  
179
  save(clim_obj,file= paste("clim_obj_month_",j,"_",var,"_",out_prefix,".RData",sep=""))
177
  save(clim_obj,file= paste("clim_obj_CAI_month_",j,"_",var,"_",out_prefix,".RData",sep=""))
180 178
  
181 179
  return(clim_obj) 
182 180
}
......
321 319

  
322 320
## Run function for kriging...?
323 321

  
324
#runGAMFusion <- function(i) {            # loop over dates
325
runGAMFusion <- function(i,list_param) {            # loop over dates
326
    #### Change this to allow explicitly arguments...
322
#runGAMFusion <- function(i,list_param) {            # loop over dates
323
run_prediction_daily_deviation <- function(i,list_param) {            # loop over dates
324
  #This function produce daily prediction using monthly predicted clim surface.
325
  #The output is both daily prediction and daily deviation from monthly steps.
326
  
327
  #### Change this to allow explicitly arguments...
327 328
  #Arguments: 
328 329
  #1)index: loop list index for individual run/fit
329 330
  #2)clim_year_list: list of climatology files for all models...(12*nb of models)
......
332 333
  #5)var: variable predicted -TMAX or TMIN
333 334
  #6)y_var_name: name of the variable predicted - dailyTMax, dailyTMin
334 335
  #7)out_prefix
336
  #8)
335 337
  #
336 338
  #The output is a list of four shapefile names produced by the function:
337 339
  #1) list_temp: y_var_name
......
482 484
    data_name<-paste(y_var_name,"_predicted_",names(rast_clim_list)[j],"_",
483 485
                     sampling_dat$date[i],"_",sampling_dat$prop[i],
484 486
                     "_",sampling_dat$run_samp[i],sep="")
485
    raster_name<-paste("fusion_",data_name,out_prefix,".tif", sep="")
487
    raster_name<-paste(interpolation_method,"_",data_name,out_prefix,".tif", sep="")
486 488
    writeRaster(temp_predicted, filename=raster_name,overwrite=TRUE) 
487 489
    temp_list[[j]]<-raster_name
488 490
  }

Also available in: Unified diff