Project

General

Profile

« Previous | Next » 

Revision aa690592

Added by Benoit Parmentier over 8 years ago

moving generate accuracy layers by tiles function to function script of mosaicing stage

View differences:

climate/research/oregon/interpolation/global_run_scalingup_mosaicing_function.R
1686 1686
  return(tb_list_tmp) #this is  a data.frame
1687 1687
}
1688 1688

  
1689
#### create a function to generate accuracy layers by tiles
1690
generate_ac_assessment_layers_by_tile <- function(lf,layers_option,df,df_tile_processed,metric_name,
1691
                                                    var_pred,list_models,use_autokrige,pred_mod_name,
1692
                                                    y_var_name,interpolation_method,
1693
                                                    days_to_process,num_cores,NA_flag_val,file_format,
1694
                                                    out_dir,out_suffix){ 
1695
  
1696
  #PARAMETERS:
1697
  #lf
1698
  #layers_option
1699
  #df: can be tb,tb_s, data_v or data_s
1700
  #df_tile_processed
1701
  #metric_name <- "rmse" #RMSE, MAE etc.
1702
  #var_pred: e.g. res_mod1, used for kriging of residuals in res_testing or res_training option
1703
  #list_models: NULL then generate the formula for kriging
1704
  #use_autokrige
1705
  #pred_mod_name <- "mod1"
1706
  #y_var_name 
1707
  #interpolation_method #c("gam_CAI") #PARAM3
1708

  
1709
  #days_to_process <- day_to_mosaic
1710
  #num_cores
1711
  #NA_flag_val <- list_param$NA_flag_val
1712
  #file_format <- list_param$file_format
1713
  #out_dir
1714
  #out_suffix
1715
  #
1716
  
1717
  #OUTPUT
1718
  #
1719
  #add options to clean up file after use!!
1720
  
1721
  ###############################
1722
  
1723
  ### START #####
1724
  
1725
  out_dir_str <- out_dir
1726
  out_suffix_str <- out_suffix
1727
  #lf <- lf_mosaic
1728
    
1729
  #Improved by adding multicores option
1730
  num_cores_tmp <- num_cores
1731
    
1732
  if(layers_option=="ac_training" | layers_option=="ac_testing"){
1733
    
1734
    out_suffix_str <- paste(layers_option,"_",out_suffix,sep="")
1735
      
1736
    list_param_accuracy_metric_raster <- list(lf,df,metric_name,pred_mod_name,y_var_name,interpolation_method,
1737
                                              days_to_process,num_cores_tmp,NA_flag_val,file_format,out_dir_str,out_suffix_str) 
1738
    names(list_param_accuracy_metric_raster) <- c("lf","tb","metric_name","pred_mod_name","y_var_name","interpolation_method",
1739
                                                  "days_to_process","num_cores","NA_flag_val","file_format","out_dir_str","out_suffix_str") 
1740
    list_raster_created_obj <- lapply(1:length(days_to_process),FUN=create_accuracy_metric_raster,
1741
                                      list_param=list_param_accuracy_metric_raster)
1742
    
1743
    #debug(create_accuracy_metric_raster)
1744
    #list_raster_created_obj <- lapply(1:1,FUN=create_accuracy_metric_raster,
1745
    #                                  list_param=list_param_accuracy_metric_raster)
1746
    #raster_created_obj <- create_accuracy_metric_raster(1, list_param_accuracy_metric_raster)
1747
    
1748
    #Extract list of files for rmse and date 1 (19920101), there should be 28 raster images
1749
    lf_accuracy_raster <- lapply(1:length(list_raster_created_obj),FUN=function(i){unlist(list_raster_created_obj[[i]]$list_raster_name)}) 
1750

  
1751
    lf_ac_assessment_layers <- lf_accuracy_raster
1752

  
1753
  }
1754
    
1755
  if(layers_option=="res_training" | layers_option=="res_testing"){
1756
    ## Create accuracy surface by kriging
1757
    #num_cores_tmp <-num_cores
1758
    #lf_day_tiles  <- lf_mosaic #list of raster files by dates
1759
    lf_day_tiles  <- lf #list of raster files by dates
1760
    #data_df <- data_day_v # data.frame table/spdf containing stations with residuals and variable
1761
      
1762
    #df_tile_processed  #tiles processed during assessment usually by region
1763
    #var_pred  #variable being modeled
1764
    #if not list of models is provided generate one
1765
    if(is.null(list_models)){
1766
      list_models <- paste(var_pred,"~","1",sep=" ") #can krige any variable here
1767
    }
1768
    
1769
    #use_autokrige #if TRUE use automap/gstat package
1770
    #y_var_name  #"dailyTmax" #PARAM2
1771
    #interpolation_method #c("gam_CAI") #PARAM3, need to select reg!!
1772
    #date_processed #can be a monthly layer
1773
    #num_cores #number of cores used
1774
    #NA_flag_val 
1775
    #file_format 
1776
    #out_dir_str <- out_dir #change to specific separate dir??
1777
    #out_suffix_str <- out_suffix 
1778
    #days_to_process <- day_to_mosaic
1779
      
1780
    #out_suffix_str <- paste("data_day_v_",out_suffix,sep="") 
1781
    out_suffix_str <- paste(layers_option,"_",var_pred,"_",out_suffix,sep="")
1782
    #browser()
1783
    df_tile_processed$path_NEX <- as.character(df_tile_processed$path_NEX) 
1784
    df_tile_processed$reg <- basename(dirname(df_tile_processed$path_NEX))
1785
    
1786
    ##By regions, selected earlier
1787
    #for(k in 1:length(region_names)){
1788
    df_tile_processed_reg <- subset(df_tile_processed,reg==region_selected)#use reg4
1789
    #i<-1 #loop by days/date to process!!
1790
    #test on the first day 
1791
    list_param_create_accuracy_residuals_raster <- list(lf,df,df_tile_processed_reg,
1792
                                                        var_pred,list_models,use_autokrige,y_var_name,interpolation_method,
1793
                                                        days_to_process,num_cores_tmp,NA_flag_val,file_format,out_dir_str,
1794
                                                        out_suffix_str) 
1795
    names(list_param_create_accuracy_residuals_raster) <- c("lf_day_tiles","data_df","df_tile_processed_reg",
1796
                                                            "var_pred","list_models","use_autokrige","y_var_name","interpolation_method",
1797
                                                            "days_to_process","num_cores_tmp","NA_flag_val","file_format","out_dir_str",
1798
                                                            "out_suffix_str") 
1799
    #browser()  
1800
    list_create_accuracy_residuals_raster_obj <- lapply(1:length(day_to_mosaic),FUN=create_accuracy_residuals_raster,
1801
                                                        list_param=list_param_create_accuracy_residuals_raster)
1802
    
1803
    #undebug(create_accuracy_residuals_raster)
1804
    #list_create_accuracy_residuals_raster_obj <- lapply(1:1,FUN=create_accuracy_residuals_raster,
1805
    #                                list_param=list_param_create_accuracy_residuals_raster)
1806
    
1807
    #create_accuracy_residuals_raster_obj <- create_accuracy_residuals_raster(1, list_param_create_accuracy_residuals_raster_obj)
1808
    
1809
    #note that three tiles did not produce a residuals surface!!! find out more later, join the output
1810
    #to df_raste_tile to keep track of which one did not work...
1811
    #lf_accuracy_residuals_raster <- as.character(unlist(lapply(1:length(list_create_accuracy_residuals_raster_obj),FUN=function(i,x){unlist(extract_from_list_obj(x[[i]]$list_pred_res_obj,"raster_name"))},x=list_create_accuracy_residuals_raster_obj))) 
1812
    lf_accuracy_residuals_raster <- lapply(1:length(list_create_accuracy_residuals_raster_obj),FUN=function(i,x){as.character(unlist(extract_from_list_obj(x[[i]]$list_pred_res_obj,"raster_name")))},x=list_create_accuracy_residuals_raster_obj)
1813
    lf_ac_assessment_layers <- lf_accuracy_residuals_raster
1814
  }
1815
    
1816
  return(lf_ac_assessment_layers)
1817

  
1818
}
1819

  
1689 1820
##################### END OF SCRIPT ######################

Also available in: Unified diff