Project

General

Profile

« Previous | Next » 

Revision 7624aef9

Added by Benoit Parmentier about 11 years ago

modificatons for multi-time scale methods, adding gam,gwr,kriging options for daily deviation surfaces

View differences:

climate/research/oregon/interpolation/GAM_fusion_analysis_raster_prediction_multisampling.R
12 12
   # gam_daily, kriging_daily,gwr_daily,gam_CAI,gam_fusion,kriging_fusion,gwr_fusion and other options added.
13 13
#For multiple time scale methods, the interpolation is done first at the monthly time scale then delta surfaces are added.
14 14
#AUTHOR: Benoit Parmentier                                                                        
15
#DATE: 08/26/2013                                                                                 
15
#DATE: 10/04/2013                                                                                 
16 16
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#568--     
17 17
#
18 18
# TO DO:
......
56 56
  #26)interpolation_method: c("gam_fusion","gam_CAI") #other otpions to be added later
57 57
  #27) use_clim_image
58 58
  #28) join_daily
59
  #29)list_models2: models' formulas as string vector for daily devation
60
  #30)interp_method2: intepolation method for daily devation step
59 61
  
60 62
  ###Loading R library and packages     
61 63
  
......
113 115
  #6 additional parameters for monthly climatology and more
114 116
  list_models<-list_param_raster_prediction$list_models
115 117
  list_models2<-list_param_raster_prediction$list_models2
118
  interp_method2 <- list_param_raster_prediction$interp_method2
119
  
116 120
  lst_avg<-list_param_raster_prediction$lst_avg
117 121
  out_path<-list_param_raster_prediction$out_path
118 122
  script_path<-list_param_raster_prediction$script_path
......
271 275
  if (interpolation_method %in% c("gam_CAI","kriging_CAI","gwr_CAI","gam_fusion","kriging_fusion","gwr_fusion")){
272 276
    #input a list:note that ghcn.subsets is not sampling_obj$data_day_ghcn
273 277
    i<-1
274
    list_param_run_prediction_daily_deviation <-list(i,clim_yearlist,daily_dev_sampling_dat,sampling_month_obj,sampling_obj,dst,list_models2,
278
    list_param_run_prediction_daily_deviation <-list(i,clim_yearlist,daily_dev_sampling_dat,sampling_month_obj,sampling_obj,dst,list_models2,interp_method2,
275 279
                                                     s_raster,use_clim_image,join_daily,var,y_var_name, interpolation_method,out_prefix,out_path)
276
    names(list_param_run_prediction_daily_deviation)<-c("list_index","clim_yearlist","daily_dev_sampling_dat","sampling_month_obj","sampling_obj","dst","list_models2",
280
    names(list_param_run_prediction_daily_deviation)<-c("list_index","clim_yearlist","daily_dev_sampling_dat","sampling_month_obj","sampling_obj","dst","list_models2","interp_method2",
277 281
                                                        "s_raster","use_clim_image","join_daily","var","y_var_name","interpolation_method","out_prefix","out_path")
278 282
    #method_mod_obj<-mclapply(1:length(sampling_obj$ghcn_data),list_param=list_param_run_prediction_daily_deviation,run_prediction_daily_deviation,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement
279 283
    #debug(run_prediction_daily_deviation)
280 284
    #test <- run_prediction_daily_deviation(1,list_param=list_param_run_prediction_daily_deviation) #This is the end bracket from mclapply(...) statement
281 285
    #test <- mclapply(1:9,list_param=list_param_run_prediction_daily_deviation,run_prediction_daily_deviation,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement
282 286
    
283
    method_mod_obj<-mclapply(1:nrow(daily_dev_sampling_dat),list_param=list_param_run_prediction_daily_deviation,run_prediction_daily_deviation,mc.preschedule=FALSE,mc.cores = 9) #This is the end bracket from mclapply(...) statement
287
    method_mod_obj<-mclapply(1:nrow(daily_dev_sampling_dat),list_param=list_param_run_prediction_daily_deviation,run_prediction_daily_deviation,mc.preschedule=FALSE,mc.cores = 11) #This is the end bracket from mclapply(...) statement
284 288
    save(method_mod_obj,file= file.path(out_path,paste("method_mod_obj_",interpolation_method,"_",y_var_name,out_prefix,".RData",sep="")))
285 289
    
286 290
  }
......
392 396
    names(list_param_validation_month)<-c("list_index","rast_day_year_list",
393 397
                                    "list_data_v","list_data_s","list_sampling_dat","y_ref","multi_time_scale","out_prefix", "out_path") #same names for any method
394 398
    #debug(calculate_accuracy_metrics)    
395
    #test_val2 <-calculate_accuracy_metrics(2,list_param_validation)
399
    #test_val2 <-calculate_accuracy_metrics(1,list_param_validation_month)
396 400
    
397
    validation_mod_month_obj <- mclapply(1:length(clim_method_mod_obj), list_param=list_param_validation_month, calculate_accuracy_metrics,mc.preschedule=FALSE,mc.cores = 9) 
401
    validation_mod_month_obj <- mclapply(1:length(clim_method_mod_obj), list_param=list_param_validation_month, calculate_accuracy_metrics,mc.preschedule=FALSE,mc.cores = 6) 
398 402
    #test_val<-calculate_accuracy_metrics(1,list_param_validation)
399 403
    save(validation_mod_month_obj,file= file.path(out_path,paste(interpolation_method,"_validation_mod_month_obj_",y_var_name,out_prefix,".RData",sep="")))
400 404
  
climate/research/oregon/interpolation/GAM_fusion_function_multisampling.R
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: 09/04/2013                                                                                 
11
#DATE: 10/03/2013                                                                                 
12 12
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363--   
13 13

  
14 14
##Comments and TODO:
......
57 57
  return(list_fitted_models) 
58 58
}
59 59

  
60
#Function to glue all methods together...still need to separate fit and training for gwr and kriging, ok for now
61
interpolate_area_fun <- function(method_interp,list_models,s_raster,list_out_filename,data_df){
62
  ##Function to fit and predict an interpolation surface
63
  ##Author: Benoit Parmentier
64
  ##Function depends on other functions!!!
65
  #inpputs:
66
  #method_interp: interpolation method with value "gam","gwr","kriging"
67
  #list_models: models to fit and predict as string (i.e.vector char)
68
  #s_raster: stack with covariate variables, must match in name the data.frame input
69
  #data_df: spatial point data.frame with covariates, must be projected match names of covariates
70
  #list_out_filename: list of char containing output names for models
71
  
72
  #Conver to formula object
73
  list_formulas<-lapply(list_models,as.formula,env=.GlobalEnv) #mulitple arguments passed to lapply!!
74
  cname<-paste("mod",1:length(list_formulas),sep="") #change to more meaningful name?
75
  
76
  names(list_out_filename)<-cname  
77
  
78
  ##Now carry out prediction
79
  if(method_interp=="gam"){    
80
    
81
    #First fitting
82
    mod_list<-fit_models(list_formulas,data_df) #only gam at this stage
83
    names(mod_list)<-cname
84
    
85
    #if raster provided then predict surface
86
    if(!is.null(s_raster)){
87
      #Second predict values for raster image...by providing fitted model list, raster brick and list of output file names
88
      rast_pred_list<-predict_raster_model(mod_list,s_raster,list_out_filename)
89
      names(rast_pred_list)<-cname
90
    }
91
  }
92
  
93
  if(method_interp%in%c("gwr","kriging")){
94
    
95
    #Call funciton to fit and predict gwr and/or kriging
96
    #month_prediction_obj<-predict_auto_krige_raster_model(list_formulas,s_raster,data_month,list_out_filename)
97
    rast_prediction_obj<-predict_autokrige_gwr_raster_model(method_interp,list_formulas,s_raster,data_df,list_out_filename)
98
    
99
    mod_list <-rast_prediction_obj$list_fitted_models
100
    rast_pred_list <-rast_prediction_obj$list_rast_pred
101
    names(rast_pred_list)<-cname
102
  }
103
  
104
  #Now prepare to return object
105
  interp_area_obj <-list(mod_list,list_formulas,rast_pred_list)
106
  names(interp_area_obj) <- c("mod_list","list_formulas","rast_pred_list")
107
  return(interp_area_obj)
108
} 
109

  
60 110
####
61 111
#TODO:
62 112
#Add log file and calculate time and sizes for processes-outputs
......
502 552
  #6)y_var_name: name of the variable predicted - dailyTMax, dailyTMin
503 553
  #7)out_prefix
504 554
  #8)out_path
505
  #
555
  #9)list_models2 : interpolation model's formulas as string
556
  #10)interp_methods2: "gam","gwr","kriging"
557
  #11)s_raster: stack for covariates and toher variables
558
  
506 559
  #The output is a list of four shapefile names produced by the function:
507 560
  #1) list_temp: y_var_name
508 561
  #2) rast_clim_list: list of files for temperature climatology predictions
......
525 578
  out_prefix<-list_param$out_prefix
526 579
  dst<-list_param$dst #monthly station dataset
527 580
  out_path <-list_param$out_path
581
  list_models2 <-list_param$list_models2
582
  interp_method2 <- list_param$interp_method2
583
  s_raster <- list_param$s_raster
528 584
  
529 585
  sampling_month_obj <- list_param$sampling_month_obj
530 586
  daily_dev_sampling_dat <- list_param$daily_dev_sampling_dat
......
568 624
  day<-as.integer(strftime(date_proc, "%d"))
569 625
  year<-as.integer(strftime(date_proc, "%Y"))
570 626
  
627
  #Adding layer LST to the raster stack  
628
  #names(s_raster)<-covar_names
629
  pos<-match("LST",names(s_raster)) #Find the position of the layer with name "LST", if not present pos=NA
630
  s_raster<-dropLayer(s_raster,pos)      # If it exists drop layer
631
  LST<-subset(s_raster,LST_month)
632
  names(LST)<-"LST"
633
  s_raster<-addLayer(s_raster,LST)            #Adding current month
634
  
571 635
  #Now get monthly data...
572 636
  
573 637
  ghcn.month.subsets<-sampling_month_obj$ghcn_data
......
671 735
    #only one delta in this case!!!
672 736
    #list(mod)
673 737
    
674
    model_name<-paste("mod_stat_kr",sep="_")
675
    daily_delta_xy<-as.matrix(cbind(dmoday$x,dmoday$y))
676
    fitdelta<-Krig(daily_delta_xy,daily_delta,theta=1e5) #use TPS or krige
677
    mod_krtmp2 <- fitdelta
678
    #names(mod_krtmp2)[k] <- model_name
679
    #data_s$daily_delta<-daily_delta
680
    #rast_clim_list<-rast_clim_yearlist[[index_m]]  #select relevant monthly climatology image ...
681
    rast_clim_list<-rast_clim_yearlist[[index_m]]  #select relevant monthly climatology image ...
682
    rast_clim_mod <- stack(rast_clim_list)
683
    names(rast_clim_mod) <- names(rast_clim_list)
684
    rast_clim_month <- subset(rast_clim_mod,1) #example layer to interpolate to
685
    
686
    daily_delta_rast<-interpolate(rast_clim_month,fitdelta) #Interpolation of the bias surface...
738
    if(is.null(list_models2)){ #change here...
739
      
740
      list_daily_delta_rast <- vector("list",length=1) #only one delta surface in this case!!
741
      list_mod_krtmp2 <- vector("list",length=1) #only one delta model in this case!!
742
      
743
      model_name<-paste("mod_stat_kr",sep="_")
744
      daily_delta_xy<-as.matrix(cbind(dmoday$x,dmoday$y))
745
      fitdelta<-Krig(daily_delta_xy,daily_delta,theta=1e5) #use TPS or krige
746
      mod_krtmp2 <- fitdelta
747
      #names(mod_krtmp2)[k] <- model_name
748
      #data_s$daily_delta<-daily_delta
749
      #rast_clim_list<-rast_clim_yearlist[[index_m]]  #select relevant monthly climatology image ...
750
      rast_clim_list<-rast_clim_yearlist[[index_m]]  #select relevant monthly climatology image ...
751
      rast_clim_mod <- stack(rast_clim_list)
752
      names(rast_clim_mod) <- names(rast_clim_list)
753
      rast_clim_month <- subset(rast_clim_mod,1) #example layer to interpolate to
754
      
755
      daily_delta_rast<-interpolate(rast_clim_month,fitdelta) #Interpolation of the of the daily devation
756
      #there is only one daily devation (delta) sruface in this case
757
      
758
      #To many I/O out of swap memory on atlas
759
      #Saving kriged surface in raster images
760
      data_name<-paste("daily_delta_",y_var_name,"_",model_name,"_",sampling_month_dat$prop[index_m],"_",sampling_month_dat$run_samp[index_m],"_",
761
                       sampling_dat$date[index_d],"_",sampling_dat$prop[index_d],"_",sampling_dat$run_samp[index_d],sep="")
762
      raster_name_delta<-file.path(out_path,paste(interpolation_method,"_",var,"_",data_name,out_prefix,".tif", sep=""))
763
      writeRaster(daily_delta_rast, filename=raster_name_delta,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
764
      
765
      list_daily_delta_rast[[1]] <- raster_name_delta
766
      list_mod_krtmp2[[1]] <- mod_krtmp2
767
    }
687 768
    
688
    #To many I/O out of swap memory on atlas
689
    #Saving kriged surface in raster images
690
    data_name<-paste("daily_delta_",y_var_name,"_",model_name,"_",sampling_month_dat$prop[index_m],"_",sampling_month_dat$run_samp[index_m],"_",
691
                     sampling_dat$date[index_d],"_",sampling_dat$prop[index_d],"_",sampling_dat$run_samp[index_d],sep="")
692
    raster_name_delta<-file.path(out_path,paste(interpolation_method,"_",var,"_",data_name,out_prefix,".tif", sep=""))
693
    writeRaster(daily_delta_rast, filename=raster_name_delta,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
769
    if(!is.null(list_models2)){ #change here...
770
      list_daily_delta_rast <- vector("list",length=1) #several delta surfaces in this case but stored as one list!!
771
      list_mod_krtmp2 <- vector("list",length=1) #several delta model in this case but stored as one list!!
772
      
773
      dev_mod_name<-paste("dev_mod",1:length(list_models2),sep="") #change to more meaningful name?
774
      model_name<-paste("mod_stat_",sep="_")
775
      #Now generate file names for the predictions...
776
      list_out_filename<-vector("list",length(list_models2))
777
      names(list_out_filename)<- dev_mod_name  
778
      
779
      ##Change name...
780
      for (j in 1:length(list_out_filename)){
781
        #j indicate which month is predicted, var indicates TMIN or TMAX
782
        data_name<-paste("daily_delta_",y_var_name,"_",model_name,"_",sampling_month_dat$prop[index_m],"_",sampling_month_dat$run_samp[index_m],"_",
783
                         sampling_dat$date[index_d],"_",sampling_dat$prop[index_d],"_",sampling_dat$run_samp[index_d],
784
                         "_",interp_method2,"_",dev_mod_name[j],sep="")
785
        raster_name_delta<-file.path(out_path,paste(interpolation_method,"_",var,"_",data_name,out_prefix,".tif", sep=""))
786
        
787
        list_out_filename[[j]]<-raster_name_delta
788
      }
789
      
790
      #Now call function
791
      
792
      #for (j in 1:length(list_models2)){
793
      dmoday$y_var <- daily_delta
794
      #coordinates(data_s)<-cbind(data_s$x,data_s$y)
795
      #proj4string(data_s)<-proj_str
796
      #coordinates(data_v)<-cbind(data_v$x,data_v$y)
797
      #proj4string(data_v)<-proj_str
798
      
799
      interp_area_obj <-interpolate_area_fun(interp_method2,list_models2,s_raster,list_out_filename,dmoday)
800
      rast_pred_list <- interp_area_obj$rast_pred_list
801
      rast_pred_list <-rast_pred_list[!sapply(rast_pred_list,is.null)] #remove NULL elements in list
802
      list_daily_delta_rast[[1]] <-rast_pred_list
803
      #names(list_daily_delta_rast) <- names(daily_delta_df)
804
      list_mod_krtmp2[[1]] <-interp_area_obj$mod_list      
805
    }
694 806
  }
695 807
  
696 808
  if(use_clim_image==TRUE){
......
739 851
    
740 852
    names(extract_data_s) <- paste(names(extract_data_s),"_m",sep="") # "m" for monthly predictions...
741 853
    dmoday <-spCbind(dmoday,extract_data_s) #contains the predicted clim at locations
742
    
854
    dmoday <-spCbind(dmoday,daily_delta_df) #contains the predicted clim at locations
743 855
    #Now krige  forevery model !! loop
744 856
    list_mod_krtmp2 <- vector("list",length=nlayers(rast_clim_mod)) 
745 857
    list_daily_delta_rast <- vector("list",length=nlayers(rast_clim_mod)) 
746
    
858
    names(list_daily_delta_rast) <- names(daily_delta_df)
859
    names(list_mod_krtmp2) <- names(daily_delta_df)
747 860
    for(k in 1:nlayers(rast_clim_mod)){
748 861
      
749
      daily_delta <- daily_delta_df[[k]]
862
      daily_delta <- daily_delta_df[[k]] #Current daily deviation being process: the reference monthly prediction varies...
750 863
      #model_name<-paste("mod_kr","day",sep="_")
751 864
      model_name<- names(daily_delta_df)[k]
752 865
      
753
      daily_delta_xy<-as.matrix(cbind(dmoday$x,dmoday$y))
754
      fitdelta<-Krig(daily_delta_xy,daily_delta,theta=1e5) #use TPS or krige
755
      list_mod_krtmp2[[k]] <-fitdelta
756
      names(list_mod_krtmp2)[k] <- model_name
757
      #data_s$daily_delta<-daily_delta
758
      #rast_clim_list<-rast_clim_yearlist[[index_m]]  #select relevant monthly climatology image ...
759
      rast_clim_month <- subset(rast_clim_mod,1) #example layer to interpolate to
866
      if(is.null(list_models2)){
867
        daily_delta_xy<-as.matrix(cbind(dmoday$x,dmoday$y))
868
        fitdelta<-Krig(daily_delta_xy,daily_delta,theta=1e5) #use TPS or krige
869
        list_mod_krtmp2[[k]] <-fitdelta
870
        names(list_mod_krtmp2)[k] <- model_name
871
        #data_s$daily_delta<-daily_delta
872
        #rast_clim_list<-rast_clim_yearlist[[index_m]]  #select relevant monthly climatology image ...
873
        rast_clim_month <- subset(rast_clim_mod,1) #example layer to interpolate to
874
        
875
        daily_delta_rast<-interpolate(rast_clim_month,fitdelta) #Interpolation of the bias surface...
876
        
877
        #list_daily_delta_rast[[k]] <- raster_name_delta
878
        
879
        data_name<-paste("daily_delta_",y_var_name,"_",model_name,"_",sampling_month_dat$prop[index_m],"_",sampling_month_dat$run_samp[index_m],"_",
880
                         sampling_dat$date[index_d],"_",sampling_dat$prop[index_d],"_",sampling_dat$run_samp[index_d],sep="")
881
        raster_name_delta<-file.path(out_path,paste(interpolation_method,"_",var,"_",data_name,out_prefix,".tif", sep=""))
882
        
883
        writeRaster(delta_rast_s, filename=raster_name_delta,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
884
        #writeRaster(r_spat, NAflag=NA_flag_val,filename=raster_name,bylayer=TRUE,bandorder="BSQ",overwrite=TRUE)   
885
        
886
        #raster_name_delta <- list_daily_delta_rast
887
        #mod_krtmp2 <- list_mod_krtmp2
888
        list_daily_delta_rast[[k]] <- raster_name_delta
889
        
890
      }
760 891
      
761
      daily_delta_rast<-interpolate(rast_clim_month,fitdelta) #Interpolation of the bias surface...
892
      if (!is.null(list_models2)){
893
        
894
        #list_formulas<-lapply(list_models,as.formula,env=.GlobalEnv) #mulitple arguments passed to lapply!!
895
        dev_mod_name<-paste("dev_mod",1:length(list_models2),sep="") #change to more meaningful name?
896
        
897
        #Now generate file names for the predictions...
898
        list_out_filename<-vector("list",length(list_models2))
899
        names(list_out_filename)<- dev_mod_name  
900
        
901
        ##Change name...
902
        for (j in 1:length(list_out_filename)){
903
          #j indicate which month is predicted, var indicates TMIN or TMAX
904
          data_name<-paste("daily_delta_",y_var_name,"_",model_name,"_",sampling_month_dat$prop[index_m],"_",sampling_month_dat$run_samp[index_m],"_",
905
                           sampling_dat$date[index_d],"_",sampling_dat$prop[index_d],"_",sampling_dat$run_samp[index_d],
906
                           "_",interp_method2,"_",dev_mod_name[j],sep="")
907
          raster_name_delta<-file.path(out_path,paste(interpolation_method,"_",var,"_",data_name,out_prefix,".tif", sep=""))
908
          
909
          list_out_filename[[j]]<-raster_name_delta
910
        }
911
        
912
        #Now call function
913
        
914
        #for (j in 1:length(list_models2)){
915
        dmoday$y_var <- daily_delta
916
        #coordinates(data_s)<-cbind(data_s$x,data_s$y)
917
        #proj4string(data_s)<-proj_str
918
        #coordinates(data_v)<-cbind(data_v$x,data_v$y)
919
        #proj4string(data_v)<-proj_str
920
        
921
        interp_area_obj <-interpolate_area_fun(interp_method2,list_models2,s_raster,list_out_filename,dmoday)
922
        rast_pred_list <- interp_area_obj$rast_pred_list
923
        names(rast_pred_list) <- dev_mod_name
924
        rast_pred_list <-rast_pred_list[!sapply(rast_pred_list,is.null)] #remove NULL elements in list
925
        list_daily_delta_rast[[k]] <-rast_pred_list
926
        names(list_daily_delta_rast) <- names(daily_delta_df)
927
        mod_list <-interp_area_obj$mod_list
928
        names(mod_list) <- dev_mod_name
929
        list_mod_krtmp2[[k]] <-interp_area_obj$mod_list
930
      }
762 931
      
763
      list_daily_delta_rast[[k]] <- daily_delta_rast 
764
      #list_daily_delta_rast[[k]] <- raster_name_delta   
765 932
    }
766 933
    
767 934
    #Too many I/O out of swap memory on atlas
768 935
    #Saving kriged surface in raster images
769
    delta_rast_s <-stack(list_daily_delta_rast)
770
    names(delta_rast_s) <- names(daily_delta_df)
936
    #delta_rast_s <-stack(list_daily_delta_rast)
937
    #names(delta_rast_s) <- names(daily_delta_df)
771 938
    
772 939
    #Should check that all delta images have been created for every model!!! remove from list empty elements!!
773 940
    
......
776 943
    #raster_name_delta<-file.path(out_path,paste(interpolation_method,"_",var,"_",data_name,out_prefix,".tif", sep=""))
777 944
    #writeRaster(daily_delta_rast, filename=raster_name_delta,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
778 945
    
779
    data_name<-paste("daily_delta_",y_var_name,"_",sampling_month_dat$prop[index_m],"_",sampling_month_dat$run_samp[index_m],"_",
780
                     sampling_dat$date[index_d],"_",sampling_dat$prop[index_d],"_",sampling_dat$run_samp[index_d],sep="")
781
    raster_name_delta<-file.path(out_path,paste(interpolation_method,"_",var,"_",data_name,out_prefix,".tif", sep=""))
946
    #data_name<-paste("daily_delta_",y_var_name,"_",sampling_month_dat$prop[index_m],"_",sampling_month_dat$run_samp[index_m],"_",
947
    #                 sampling_dat$date[index_d],"_",sampling_dat$prop[index_d],"_",sampling_dat$run_samp[index_d],sep="")
948
    #raster_name_delta<-file.path(out_path,paste(interpolation_method,"_",var,"_",data_name,out_prefix,".tif", sep=""))
782 949
    
783
    writeRaster(delta_rast_s, filename=raster_name_delta,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
950
    #writeRaster(delta_rast_s, filename=raster_name_delta,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
784 951
    #writeRaster(r_spat, NAflag=NA_flag_val,filename=raster_name,bylayer=TRUE,bandorder="BSQ",overwrite=TRUE)   
785 952
    
786 953
    #raster_name_delta <- list_daily_delta_rast
787
    mod_krtmp2 <- list_mod_krtmp2
954
    #mod_krtmp2 <- list_mod_krtmp2
788 955
  }
789 956
  
790 957
  #########
......
798 965
  temp_list<-vector("list",nlayers(rast_clim_mod))  
799 966
  for (k in 1:nlayers(rast_clim_mod)){
800 967
    if(use_clim_image==TRUE){
801
      daily_delta_rast <- list_daily_delta_rast[[k]]
968
      if (is.null(list_models2)){ 
969
        daily_delta_rast <- raster(list_daily_delta_rast[[k]]) #There is only one image of deviation per model if list_models2 is NULL
970
      }
971
      if (!is.null(list_models2)){ #then possible multiple daily dev predictions
972
        daily_delta_rast <- stack(unlist(list_daily_delta_rast[[k]]))
973
      }
974
      #daily_delta_rast <- subset(delta_rast_s,k)
802 975
    }
803 976
    #if use_clim_image==FALSE then daily__delta_rast already defined earlier...
804 977
    
978
    if(use_clim_image==FALSE){
979
      if (is.null(list_models2)){ 
980
        daily_delta_rast <- raster(list_daily_delta_rast[[1]]) #There is only one image of deviation per model if list_models2 is NULL
981
      }
982
      if (!is.null(list_models2)){ #then possible multiple daily dev predictions hence use stack
983
        daily_delta_rast <- stack(unlist(list_daily_delta_rast[[1]]))
984
      }
985
      #daily_delta_rast <- subset(delta_rast_s,k)
986
    }
987
    
805 988
    #rast_clim_month<-raster(rast_clim_list[[k]])
806
    rast_clim_month <- subset(rast_clim_mod,k)
807
    temp_predicted<-rast_clim_month + daily_delta_rast
808
    
809
    data_name<-paste(y_var_name,"_predicted_",names(rast_clim_mod)[k],"_",sampling_month_dat$prop[index_m],"_",sampling_month_dat$run_samp[index_m],"_",
810
                     sampling_dat$date[index_d],"_",sampling_dat$prop[index_d],"_",sampling_dat$run_samp[index_d],sep="")
811
    raster_name<-file.path(out_path,paste(interpolation_method,"_",data_name,out_prefix,".tif", sep=""))
812
    writeRaster(temp_predicted, filename=raster_name,overwrite=TRUE) 
813
    temp_list[[k]]<-raster_name
989
    rast_clim_month <- subset(rast_clim_mod,k) #long term monthly prediction
990
    if (is.null(list_models2)){ 
991
      temp_predicted<-rast_clim_month + daily_delta_rast
992
      data_name<-paste(y_var_name,"_predicted_",names(rast_clim_mod)[k],"_",sampling_month_dat$prop[index_m],"_",sampling_month_dat$run_samp[index_m],"_",
993
                       sampling_dat$date[index_d],"_",sampling_dat$prop[index_d],"_",sampling_dat$run_samp[index_d],sep="")
994
      raster_name<-file.path(out_path,paste(interpolation_method,"_",data_name,out_prefix,".tif", sep=""))
995
      writeRaster(temp_predicted, filename=raster_name,overwrite=TRUE) 
996
      temp_list[[k]]<-raster_name
997
    }
998
    if (!is.null(list_models2)){ 
999
      dev_mod_name <- paste("dev_mod",1:length(list_models2),sep="") #change to more meaningful name?
1000
      raster_name_list <- vector("list",length(list_models2))
1001
      for(j in 1:length(list_models2)){
1002
        temp_predicted <- rast_clim_month + subset(daily_delta_rast,j)
1003
        data_name<-paste(y_var_name,"_predicted_",names(rast_clim_mod)[k],"_",sampling_month_dat$prop[index_m],"_",sampling_month_dat$run_samp[index_m],"_",
1004
                         sampling_dat$date[index_d],"_",sampling_dat$prop[index_d],"_",sampling_dat$run_samp[index_d],"_",
1005
                         interp_method2,"_",dev_mod_name[j],sep="")
1006
        raster_name<-file.path(out_path,paste(interpolation_method,"_",data_name,out_prefix,".tif", sep=""))
1007
        writeRaster(temp_predicted, filename=raster_name,overwrite=TRUE) 
1008
        raster_name_list[[j]] <- raster_name
1009
      }
1010
      names(raster_name_list) <- dev_mod_name
1011
      temp_list[[k]]<-raster_name_list #record names of the daily predictions 
1012
    }
814 1013
  }
815 1014
  
816 1015
  ##########
817 1016
  # STEP 5 - Prepare output object to return
818 1017
  ##########
819 1018
  
820
  data_s<-dmoday #put the 
1019
  data_s <- dmoday #put the 
821 1020
  #coordinates(data_s)<-cbind(data_s$x,data_s$y)
822 1021
  #proj4string(data_s)<-proj_str
823 1022
  coordinates(data_v)<-cbind(data_v$x,data_v$y)
......
826 1025
  #mod_krtmp2<-list_mod_krtmp2    
827 1026
  #mod_krtmp2<-fitdelta
828 1027

  
829
  model_name<-paste("mod_kr","day",sep="_")
1028
  model_name<-paste("mod_","day",sep="_")
830 1029
  
831 1030
  names(temp_list)<-names(rast_clim_list)
832

  
1031
  
833 1032
  #add data_month_s and data_month_v?
834
  delta_obj<-list(temp_list,rast_clim_list,raster_name_delta, data_s, data_v,
835
                 modst,data_month_v,sampling_dat[index_d,],daily_dev_sampling_dat[i,],mod_krtmp2)
1033
  delta_obj<-list(temp_list,rast_clim_list,list_daily_delta_rast, data_s, data_v,
1034
                 modst,data_month_v,sampling_dat[index_d,],daily_dev_sampling_dat[i,],list_mod_krtmp2)
836 1035
  
837 1036
  obj_names<-c(y_var_name,"clim","delta","data_s","data_v",
838 1037
               "data_month_s","data_month_v","sampling_dat","daily_dev_sampling_dat",model_name)
climate/research/oregon/interpolation/master_script_temp.R
10 10
#STAGE 5: Output analyses: assessment of results for specific dates...
11 11
#
12 12
#AUTHOR: Benoit Parmentier                                                                       
13
#DATE: 09/29/2013                                                                                 
13
#DATE: 10/04/2013                                                                                 
14 14

  
15 15
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363, TASK$568--   
16 16

  
......
58 58
#source(file.path(script_path,"download_and_produce_MODIS_LST_climatology_06112013.R"))
59 59
source(file.path(script_path,"covariates_production_temperatures_08052013.R"))
60 60
source(file.path(script_path,"Database_stations_covariates_processing_function_06112013.R"))
61
source(file.path(script_path,"GAM_fusion_analysis_raster_prediction_multisampling_09042013.R"))
61
source(file.path(script_path,"GAM_fusion_analysis_raster_prediction_multisampling_10042013.R"))
62 62
source(file.path(script_path,"results_interpolation_date_output_analyses_08052013.R"))
63 63
#source(file.path(script_path,"results_covariates_database_stations_output_analyses_04012013.R")) #to be completed
64 64

  
65 65
#FUNCTIONS CALLED FROM GAM ANALYSIS RASTER PREDICTION ARE FOUND IN...
66 66

  
67 67
source(file.path(script_path,"sampling_script_functions_08252013.R"))
68
source(file.path(script_path,"GAM_fusion_function_multisampling_09042013.R")) #Includes Fusion and CAI methods
68
source(file.path(script_path,"GAM_fusion_function_multisampling_10042013.R")) #Includes Fusion and CAI methods
69 69
source(file.path(script_path,"interpolation_method_day_function_multisampling_07052013.R")) #Include GAM_day
70
source(file.path(script_path,"GAM_fusion_function_multisampling_validation_metrics_09012013.R"))
70
source(file.path(script_path,"GAM_fusion_function_multisampling_validation_metrics_10042013.R"))
71 71

  
72 72
#stages_to_run<-c(1,2,3,4,5) #May decide on antoher strategy later on...
73 73
#stages_to_run<-c(0,2,3,4,5) #May decide on antoher strategy later on...
......
80 80
#met_stations_outfiles_obj_file<-"met_stations_outfiles_obj_gam_CAI__365d_gam_CAI_lst_comb3_08252013.RData"
81 81

  
82 82
var<-"TMAX" # variable being interpolated
83
out_prefix<-"_365d_gam_cai_lst_comb3_09292013"                #User defined output prefix
84
out_suffix<-"_OR_09292013"                                       #Regional suffix
83
out_prefix<-"_365d_gam_cai_lst_comb3_10042013"                #User defined output prefix
84
out_suffix<-"_OR_10042013"                                       #Regional suffix
85 85
out_suffix_modis <-"_05302013"                       #pattern to find tiles produced previously     
86 86

  
87 87
#interpolation_method<-c("gam_fusion","gam_CAI","gam_daily") #other otpions to be added later
......
278 278
#                "y_var ~ s(lat,lon) + s(elev_s) + s(LST) + ti(LST,CANHGHT)")
279 279

  
280 280
#Combination 4: for paper baseline=s(lat,lon)
281
list_models<-c("y_var ~ s(lat,lon)",
282
               "y_var ~ s(lat,lon) + s(elev_s)",
283
                "y_var ~ s(lat,lon) + s(N_w)",
284
                "y_var ~ s(lat,lon) + s(E_w)",
285
                "y_var ~ s(lat,lon) + s(LST)",
286
                "y_var ~ s(lat,lon) + s(DISTOC)",
287
                "y_var ~ s(lat,lon) + s(LC1)",
288
                "y_var ~ s(lat,lon) + s(CANHGHT)",
289
                "y_var ~ s(lat,lon) + s(LST) + ti(LST,LC1)",
290
                "y_var ~ s(lat,lon) + s(LST) + ti(LST,CANHGHT)")
291

  
292
list_models2<-c("y_var ~ s(lat,lon)",
293
               "y_var ~ s(lat,lon) + s(elev_s)",
294
               "y_var ~ s(lat,lon) + s(N_w)",
295
               "y_var ~ s(lat,lon) + s(E_w)",
296
               "y_var ~ s(lat,lon) + s(LST)",
297
               "y_var ~ s(lat,lon) + s(DISTOC)",
298
               "y_var ~ s(lat,lon) + s(LC1)",
299
               "y_var ~ s(lat,lon) + s(CANHGHT)",
300
               "y_var ~ s(lat,lon) + s(LST) + ti(LST,LC1)",
301
               "y_var ~ s(lat,lon) + s(LST) + ti(LST,CANHGHT)")
302

  
281
#list_models<-c("y_var ~ s(lat,lon)",
282
#              "y_var ~ s(lat,lon) + s(elev_s)",
283
#                "y_var ~ s(lat,lon) + s(N_w)",
284
#                "y_var ~ s(lat,lon) + s(E_w)",
285
#               "y_var ~ s(lat,lon) + s(LST)",
286
#               "y_var ~ s(lat,lon) + s(DISTOC)",
287
#              "y_var ~ s(lat,lon) + s(LC1)",
288
#             "y_var ~ s(lat,lon) + s(CANHGHT)",
289
#            "y_var ~ s(lat,lon) + s(LST) + ti(LST,LC1)",
290
#           "y_var ~ s(lat,lon) + s(LST) + ti(LST,CANHGHT)")
291

  
292
#Combination 5: for paper baseline=s(lat,lon)+s(elev)
293
list_models<-c("y_var ~ s(lat,lon) + s(elev_s)",
294
                "y_var ~ s(lat,lon) + s(elev_s) + s(N_w,E_w)",
295
                "y_var ~ s(lat,lon) + s(elev_s) + s(LST)",
296
                "y_var ~ s(lat,lon) + s(elev_s) + s(DISTOC)",
297
                "y_var ~ s(lat,lon) + s(elev_s) + s(LC1)",
298
                "y_var ~ s(lat,lon) + s(elev_s) + s(CANHGHT)",
299
                "y_var ~ s(lat,lon) + s(elev_s) + s(LST) + ti(LST,LC1)")#,
300
                #"y_var ~ s(lat,lon) + s(elev_s) + s(LST) + ti(LST,CANHGHT)")
301

  
302
list_models2<-c("y_var ~ s(lat,lon) + s(elev_s)",
303
                "y_var ~ s(lat,lon) + s(LST)")
304

  
305
interp_method2 <- "gam" #other options are "gwr" and "kriging"
303 306
#list_models<-c("y_var ~ lat*lon + elev_s")
304 307

  
305 308
#list_models<-c("y_var ~ s(lat,lon) + s(elev_s)")
......
321 324
list_param_raster_prediction<-list(list_param_data_prep,screen_data_training,
322 325
                                seed_number,nb_sample,step,constant,prop_minmax,dates_selected,
323 326
                                seed_number_month,nb_sample_month,step_month,constant_month,prop_minmax_month,
324
                                list_models,list_models2,lst_avg,out_path,script_path,use_clim_image,join_daily,
327
                                list_models,list_models2,interp_method2,lst_avg,out_path,script_path,use_clim_image,join_daily,
325 328
                                interpolation_method)
326 329
names(list_param_raster_prediction)<-c("list_param_data_prep","screen_data_training",
327 330
                                "seed_number","nb_sample","step","constant","prop_minmax","dates_selected",
328 331
                                "seed_number_month","nb_sample_month","step_month","constant_month","prop_minmax_month",
329
                                "list_models","list_models2","lst_avg","out_path","script_path","use_clim_image","join_daily",
332
                                "list_models","list_models2","interp_method2","lst_avg","out_path","script_path","use_clim_image","join_daily",
330 333
                                "interpolation_method")
331 334

  
332 335
#debug(raster_prediction_fun)
333
debug(debug_fun_test)
334
debug_fun_test(list_param_raster_prediction)
336
#debug(debug_fun_test)
337
#debug_fun_test(list_param_raster_prediction)
335 338

  
336 339
raster_prediction_obj <-raster_prediction_fun(list_param_raster_prediction)
337 340

  

Also available in: Unified diff