Project

General

Profile

« Previous | Next » 

Revision f34d3c5c

Added by Benoit Parmentier about 11 years ago

daily deviation function and CAI function, modifications for monthly hold out

View differences:

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: 07/30/2013                                                                                 
11
#DATE: 08/25/2013                                                                                 
12 12
#PROJECT: NCEAS INPLANT: Environment and Organisms --TASK#363--   
13 13

  
14 14
##Comments and TODO:
......
95 95
  y_var_name<-list_param$y_var_name
96 96
  out_prefix<-list_param$out_prefix
97 97
  out_path<-list_param$out_path
98

  
99
  #inserted #
100
  sampling_month_obj<-list_param$sampling_month_obj
101
  ghcn.month.subsets<-sampling_month_obj$ghcn_data
102
  sampling_month_dat <- sampling_month_obj$sampling_dat
103
  sampling_month_index <- sampling_month_obj$sampling_index
98 104
  
99 105
  #Model and response variable can be changed without affecting the script
100
  prop_month<-0 #proportion retained for validation...
101
  run_samp<-1 #sample number, can be introduced later...
102
  
106
  #prop_month<-0 #proportion retained for validation...
107
  #run_samp<-1 #sample number, can be introduced later...
108

  
109
  prop_month <- sampling_month_dat$prop[j] #proportion retained for validation...
110
  run_samp <- sampling_month_dat$run_samp[j] #sample number if multisampling...will need create mulitple prediction at daily!!! could be complicated
111
                                       #possibility is to average per proportion !!!
112
  
113
  date_month <-strptime(sampling_month_dat$date[j], "%Y%m%d")   # interpolation date being processed
114
  month_no <-strftime(date_month, "%m")          # current month of the date being processed
115
  LST_month<-paste("mm_",month_no,sep="") # name of LST month to be matched
116
  LST_name <-LST_month  
103 117
  #### STEP 2: PREPARE DATA
104 118
    
105
  data_month<-dst[dst$month==j,] #Subsetting dataset for the relevant month of the date being processed
106
  LST_name<-lst_avg[j] # name of LST month to be matched
107
  data_month$LST<-data_month[[LST_name]]
119
  #change here...use training data...
120
  ###Regression part 1: Creating a validation dataset by creating training and testing datasets
121
  
122
  #LST_name <-lst_avg[j] # name of LST month to be matched
123
  #data_month$LST<-data_month[[LST_name]]
124
  
125
  dataset_month <-ghcn.month.subsets[[j]]
126
  mod_LST <- ghcn.month.subsets[[j]][,match(LST_month, names(ghcn.month.subsets[[j]]))]  #Match interpolation date and monthly LST average
127
  dataset_month$LST <- as.data.frame(mod_LST)[,1] #Add the variable LST to the dataset
128
  #change here...
129
  dst$LST<-dst[[LST_month]] #Add the variable LST to the monthly dataset
130
  proj_str<-proj4string(dst) #get the local projection information from monthly data
108 131
  
109 132
  #TMax to model..., add precip later
110 133
  if (var=="TMAX"){   
111
    data_month$y_var<-data_month$TMax #Adding TMax as the variable modeled
134
    dataset_month$y_var<-dataset_month$TMax #Adding TMax as the variable modeled
112 135
  }
113 136
  if (var=="TMIN"){   
114
    data_month$y_var<-data_month$TMin #Adding TMin as the variable modeled
137
    dataset_month$y_var<-dataset_month$TMin #Adding TMin as the variable modeled
115 138
  }
139
  
140
  ind.training <- sampling_month_index[[j]]
141
  ind.testing  <- setdiff(1:nrow(dataset_month), ind.training)
142
  data_month_s <- dataset_month[ind.training, ]   #Training dataset currently used in the modeling
143
  data_month_v <- dataset_month[ind.testing, ]    #Testing/validation dataset using input sampling
144
  
145
  data_month <- data_month_s #training data for  monthhly predictions...
146

  
147
  #date_proc<-strptime(sampling_dat$date[i], "%Y%m%d")   # interpolation date being processed
148
  #mo<-as.integer(strftime(date_proc, "%m"))          # current month of the date being processed
149
  #day<-as.integer(strftime(date_proc, "%d"))
150
  #year<-as.integer(strftime(date_proc, "%Y"))
151
  ## end of pasted
152
    
153
  #end of insert...
154
  
116 155
  #Fit gam models using data and list of formulas
117 156
  
118 157
  list_formulas<-lapply(list_models,as.formula,env=.GlobalEnv) #mulitple arguments passed to lapply!!
......
135 174
  
136 175
  for (k in 1:length(list_out_filename)){
137 176
    #j indicate which month is predicted
138
    data_name<-paste(var,"_clim_month_",j,"_",cname[k],"_",prop_month,
177
    data_name<-paste(var,"_clim_month_",as.integer(month_no),"_",cname[k],"_",prop_month,
139 178
                     "_",run_samp,sep="")
140 179
    raster_name<-file.path(out_path,paste("CAI_",data_name,out_prefix,".tif", sep=""))
141 180
    list_out_filename[[k]]<-raster_name
......
188 227
  
189 228
  #Write out modeled layers
190 229
  
191
  data_name<-paste(var,"_clim_month_",j,"_",model_name,"_",prop_month,
230
  data_name<-paste(var,"_clim_month_",as.integer(month_no),"_",model_name,"_",prop_month,
192 231
                   "_",run_samp,sep="")
193 232
  raster_name_clim<-file.path(out_path,paste("CAI_",data_name,out_prefix,".tif", sep=""))
194 233
  writeRaster(clim_rast, filename=raster_name_clim,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
......
199 238
  rast_clim_list[[model_name]]<-raster_name_clim
200 239
  
201 240
  #Prepare object to return
202
  clim_obj<-list(rast_clim_list,data_month,mod_list,list_formulas)
203
  names(clim_obj)<-c("clim","data_month","mod","formulas")
241
  clim_obj<-list(rast_clim_list,data_month,data_month_v,sampling_month_dat[j,],mod_list,list_formulas)
242
  names(clim_obj)<-c("clim","data_month","data_month_v","sampling_month_dat","mod","formulas")
204 243
  
205
  save(clim_obj,file= file.path(out_path,paste("clim_obj_CAI_month_",j,"_",var,"_",out_prefix,".RData",sep="")))
244
  save(clim_obj,file= file.path(out_path,paste("clim_obj_CAI_month_",as.integer(month_no),"_",var,"_",prop_month,
245
                                               "_",run_samp,"_",out_prefix,".RData",sep="")))
206 246
  
207 247
  return(clim_obj) 
208 248
}
......
413 453
  #list_param_runGAMFusion<-list(i,clim_yearlist,sampling_obj,var,y_var_name, out_prefix)
414 454
  rast_clim_yearlist<-list_param$clim_yearlist
415 455
  sampling_obj<-list_param$sampling_obj
416
  ghcn.subsets<-sampling_obj$ghcn_data_day
456
  ghcn.subsets<-sampling_obj$ghcn_data
417 457
  sampling_dat <- sampling_obj$sampling_dat
418 458
  sampling <- sampling_obj$sampling_index
419 459
  var<-list_param$var
......
422 462
  dst<-list_param$dst #monthly station dataset
423 463
  out_path <-list_param$out_path
424 464
  
465
  sampling_month_obj <- list_param$sampling_month_obj
466
  daily_dev_sampling_dat <- list_param$daily_dev_sampling_dat
467
  
468
  index_d <- daily_dev_sampling_dat$index_d[i]
469
  index_m <- daily_dev_sampling_dat$index_m[i]
470
  
471
  use_clim_image <- list_param$use_clim_image # use predicted image as a base...rather than average Tmin at the station for delta
472
  join_daily <- list_param$join_daily # join monthly and daily station before calucating delta
473
  
474
  #use_clim_image
425 475
  ##########
426 476
  # STEP 1 - Read in information and get traing and testing stations
427 477
  ############# 
428 478
  
429
  date<-strptime(sampling_dat$date[i], "%Y%m%d")   # interpolation date being processed
479
  #use index_d and index_m
480
  
481
  date<-strptime(daily_dev_sampling_dat$date[i], "%Y%m%d")   # interpolation date being processed
430 482
  month<-strftime(date, "%m")          # current month of the date being processed
431 483
  LST_month<-paste("mm_",month,sep="") # name of LST month to be matched
432 484
  proj_str<-proj4string(dst) #get the local projection information from monthly data
433 485

  
434 486
  ###Regression part 1: Creating a validation dataset by creating training and testing datasets
435
  data_day<-ghcn.subsets[[i]]
436
  mod_LST <- ghcn.subsets[[i]][,match(LST_month, names(ghcn.subsets[[i]]))]  #Match interpolation date and monthly LST average
487
  
488
  data_day<-ghcn.subsets[[index_d]]
489
  mod_LST <- ghcn.subsets[[index_d]][,match(LST_month, names(ghcn.subsets[[index_d]]))]  #Match interpolation date and monthly LST average
437 490
  data_day$LST <- as.data.frame(mod_LST)[,1] #Add the variable LST to the dataset
438 491
  dst$LST<-dst[[LST_month]] #Add the variable LST to the monthly dataset
439 492
  
440
  ind.training<-sampling[[i]]
493
  ind.training<-sampling[[index_d]]
441 494
  ind.testing <- setdiff(1:nrow(data_day), ind.training)
442 495
  data_s <- data_day[ind.training, ]   #Training dataset currently used in the modeling
443 496
  data_v <- data_day[ind.testing, ]    #Testing/validation dataset using input sampling
......
445 498
  ns<-nrow(data_s)
446 499
  nv<-nrow(data_v)
447 500
  #i=1
448
  date_proc<-sampling_dat$date[i]
449
  date_proc<-strptime(sampling_dat$date[i], "%Y%m%d")   # interpolation date being processed
501
  date_proc<-sampling_dat$date[index_d]
502
  date_proc<-strptime(sampling_dat$date[index_d], "%Y%m%d")   # interpolation date being processed
450 503
  mo<-as.integer(strftime(date_proc, "%m"))          # current month of the date being processed
451 504
  day<-as.integer(strftime(date_proc, "%d"))
452 505
  year<-as.integer(strftime(date_proc, "%Y"))
453 506
  
507
  #Now get monthly data...
508
  
509
  ghcn.month.subsets<-sampling_month_obj$ghcn_data
510
  sampling_month_dat <- sampling_month_obj$sampling_dat
511
  sampling_month_index <- sampling_month_obj$sampling_index
512
  
513
  dataset_month <-ghcn.month.subsets[[index_m]]
514
  mod_LST <- ghcn.month.subsets[[index_m]][,match(LST_month, names(ghcn.month.subsets[[index_m]]))]  #Match interpolation date and monthly LST average
515
  dataset_month$LST <- as.data.frame(mod_LST)[,1] #Add the variable LST to the dataset
516
  #change here...
517
  dst$LST<-dst[[LST_month]] #Add the variable LST to the monthly dataset
518
  proj_str<-proj4string(dst) #get the local projection information from monthly data
519
  
520
  ind.training_month <- sampling_month_index[[index_m]]
521
  ind.testing_month  <- setdiff(1:nrow(dataset_month), ind.training_month)
522
  data_month_s <- dataset_month[ind.training_month, ]   #Training dataset currently used in the modeling
523
  data_month_v <- dataset_month[ind.testing_month, ]    #Testing/validation dataset using input sampling
524
  
525
  modst <- data_month_s #training data for  monthhly predictions...
526
    
454 527
  ##########
455
  # STEP 2 - JOIN DAILY AND MONTHLY STATION INFORMATION
528
  # STEP 2 - CLEAN DATA AND JOIN DAILY TO MONTHLY STATION INFORMATION
456 529
  ##########
457 530
  
458
  modst<-dst[dst$month==mo,] #Subsetting dataset for the relevant month of the date being processed
459

  
531
  #if use join
532
  #modst<-dst[dst$month==mo,] #Subsetting dataset for the relevant month of the date being processed
533
    
460 534
  if (var=="TMIN"){
461 535
    modst$LSTD_bias <- modst$LST-modst$TMin; #That is the difference between the monthly LST mean and monthly station mean
462 536
  }
......
491 565
  names(x)[pos]<-c("id")
492 566
  pos<-match("station",names(modst)) #Find column with name station ID
493 567
  names(modst)[pos]<-c("id")       #modst contains the average tmax per month for every stations...
494
  
495
  dmoday <-merge(modst,d,by="id",suffixes=c("",".y2"))  
496
  xmoday <-merge(modst,x,by="id",suffixes=c("",".y2"))  
497
  mod_pat<-glob2rx("*.y2")   #remove duplicate columns that have ".y2" in their names
498
  var_pat<-grep(mod_pat,names(dmoday),value=FALSE) # using grep with "value" extracts the matching names
499
  dmoday<-dmoday[,-var_pat] #dropping relevant columns
500
  mod_pat<-glob2rx("*.y2")   
501
  var_pat<-grep(mod_pat,names(xmoday),value=FALSE) # using grep with "value" extracts the matching names
502
  xmoday<-xmoday[,-var_pat] #Removing duplicate columns
503
  
504
  data_v<-xmoday
505
  
506
  #dmoday contains the daily tmax values for training with TMax/TMin being the monthly station tmax/tmin mean
507
  #xmoday contains the daily tmax values for validation with TMax/TMin being the monthly station tmax/tmin mean
508
  
568

  
509 569
  ##########
510 570
  # STEP 3 - interpolate daily delta across space
511 571
  ##########
512 572
  
573
  #if used images
574
  # extract from image 
513 575
  #Change to take into account TMin and TMax
514
  if (var=="TMIN"){
515
    daily_delta<-dmoday$dailyTmin-dmoday$TMin #daily detl is the difference between monthly and daily temperatures
576
  
577
  if(use_clim_image==FALSE){
578
    
579
    #must join daily and monthly data first...
580
    
581
    dmoday <-merge(modst,d,by="id",suffixes=c("",".y2"))  
582
    xmoday <-merge(modst,x,by="id",suffixes=c("",".y2"))  
583
    mod_pat<-glob2rx("*.y2")   #remove duplicate columns that have ".y2" in their names
584
    var_pat<-grep(mod_pat,names(dmoday),value=FALSE) # using grep with "value" extracts the matching names
585
    dmoday<-dmoday[,-var_pat] #dropping relevant columns
586
    mod_pat<-glob2rx("*.y2")   
587
    var_pat<-grep(mod_pat,names(xmoday),value=FALSE) # using grep with "value" extracts the matching names
588
    xmoday<-xmoday[,-var_pat] #Removing duplicate columns
589
      
590
    data_v<-xmoday
591
    
592
    #coords <-dmoday[,c("coords.x1","coords.x2")]
593
    coords <-dmoday[,c("x","y")]
594
    coordinates(dmoday)<-coords
595
    proj4string(dmoday)<-proj_str
596
    
597
    #dmoday contains the daily tmax values for training with TMax/TMin being the monthly station tmax/tmin mean
598
    #xmoday contains the daily tmax values for validation with TMax/TMin being the monthly station tmax/tmin mean
599
    
600
    if (var=="TMIN"){
601
      daily_delta <-dmoday$dailyTmin-dmoday$TMin #daily detl is the difference between monthly and daily temperatures
602
    }
603
    if (var=="TMAX"){
604
      daily_delta <- dmoday$dailyTmax-dmoday$TMax
605
    }  
606
    #daily_delta <- dmoday[[y_var_name]] - 
607
    #only one delta in this case!!!
608
    #list(mod)
609
    
610
    model_name<-paste("mod_stat_kr",sep="_")
611
    daily_delta_xy<-as.matrix(cbind(dmoday$x,dmoday$y))
612
    fitdelta<-Krig(daily_delta_xy,daily_delta,theta=1e5) #use TPS or krige
613
    mod_krtmp2 <- fitdelta
614
    #names(mod_krtmp2)[k] <- model_name
615
    #data_s$daily_delta<-daily_delta
616
    #rast_clim_list<-rast_clim_yearlist[[index_m]]  #select relevant monthly climatology image ...
617
    rast_clim_list<-rast_clim_yearlist[[index_m]]  #select relevant monthly climatology image ...
618
    rast_clim_mod <- stack(rast_clim_list)
619
    names(rast_clim_mod) <- names(rast_clim_list)
620
    rast_clim_month <- subset(rast_clim_mod,1) #example layer to interpolate to
621
    
622
    daily_delta_rast<-interpolate(rast_clim_month,fitdelta) #Interpolation of the bias surface...
623
    
624
    #Saving kriged surface in raster images
625
    data_name<-paste("daily_delta_",y_var_name,"_",model_name,"_",sampling_month_dat$prop[index_m],"_",sampling_month_dat$run_samp[index_m],"_",
626
                     sampling_dat$date[index_d],"_",sampling_dat$prop[index_d],"_",sampling_dat$run_samp[index_d],sep="")
627
    raster_name_delta<-file.path(out_path,paste(interpolation_method,"_",var,"_",data_name,out_prefix,".tif", sep=""))
628
    writeRaster(daily_delta_rast, filename=raster_name_delta,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
516 629
  }
517
  if (var=="TMAX"){
518
    daily_delta<-dmoday$dailyTmax-dmoday$TMax
630
  
631
  if(use_clim_image==TRUE){
632
    
633
    # User can choose to join daily and monthly station before interpolation:
634
    #-this ensures that the delta difference is "more" exact since its starting point is basesd on average value but there is risk to loose some stations
635
    #may need to change this option later!!
636
    #if jion_Daily is true then daily station used as training will match monthly station used as training
637
    
638
    if(join_daily==TRUE){
639
      dmoday <-merge(modst,d,by="id",suffixes=c("",".y2"))  
640
      xmoday <-merge(modst,x,by="id",suffixes=c("",".y2"))  
641
      mod_pat<-glob2rx("*.y2")   #remove duplicate columns that have ".y2" in their names
642
      var_pat<-grep(mod_pat,names(dmoday),value=FALSE) # using grep with "value" extracts the matching names
643
      dmoday<-dmoday[,-var_pat] #dropping relevant columns
644
      mod_pat<-glob2rx("*.y2")   
645
      var_pat<-grep(mod_pat,names(xmoday),value=FALSE) # using grep with "value" extracts the matching names
646
      xmoday<-xmoday[,-var_pat] #Removing duplicate columns
647
      
648
      data_v<-xmoday
649
      
650
    }else{
651
      dmoday<-d
652
      data_v<-x
653
    }
654
    
655
    
656
    #dmoday contains the daily tmax values for training with TMax/TMin being the monthly station tmax/tmin mean
657
    #xmoday contains the daily tmax values for validation with TMax/TMin being the monthly station tmax/tmin mean
658
    
659
    #coords <-dmoday[,c("coords.x1","coords.x2")]
660
    coords <-dmoday[,c("x","y")]
661
    coordinates(dmoday)<-coords
662
    proj4string(dmoday)<-proj_str
663
    
664
    #Now compute daily delta deviation from climatology layer:
665
    
666
    rast_clim_list<-rast_clim_yearlist[[index_m]]  #select relevant monthly climatology image ...
667
    rast_clim_mod <- stack(rast_clim_list)
668
    names(rast_clim_mod) <- names(rast_clim_list)
669
    extract_data_s <-extract(rast_clim_mod,dmoday,df=TRUE)
670
    #list_daily_delta
671
    daily_delta_df <- dmoday[[y_var_name]] - extract_data_s  
672
    daily_delta_df <- daily_delta_df[,-1]
673
    names(daily_delta_df) <- paste(names(daily_delta_df),"_del",sep="")
674
    
675
    names(extract_data_s) <- paste(names(extract_data_s),"_m",sep="") # "m" for monthly predictions...
676
    dmoday <-spCbind(dmoday,extract_data_s) #contains the predicted clim at locations
677
    
678
    #Now krige  forevery model !! loop
679
    list_mod_krtmp2 <- vector("list",length=nlayers(rast_clim_mod)) 
680
    list_daily_delta_rast <- vector("list",length=nlayers(rast_clim_mod)) 
681
    
682
    for(k in 1:nlayers(rast_clim_mod)){
683
      
684
      daily_delta <- daily_delta_df[[k]]
685
      #model_name<-paste("mod_kr","day",sep="_")
686
      model_name<- names(daily_delta_df)[k]
687
      
688
      daily_delta_xy<-as.matrix(cbind(dmoday$x,dmoday$y))
689
      fitdelta<-Krig(daily_delta_xy,daily_delta,theta=1e5) #use TPS or krige
690
      list_mod_krtmp2[[k]] <-fitdelta
691
      names(list_mod_krtmp2)[k] <- model_name
692
      #data_s$daily_delta<-daily_delta
693
      #rast_clim_list<-rast_clim_yearlist[[index_m]]  #select relevant monthly climatology image ...
694
      rast_clim_month <- subset(rast_clim_mod,1) #example layer to interpolate to
695
      
696
      daily_delta_rast<-interpolate(rast_clim_month,fitdelta) #Interpolation of the bias surface...
697
      
698
      #Saving kriged surface in raster images
699
      data_name<-paste("daily_delta_",y_var_name,"_",model_name,"_",sampling_month_dat$prop[index_m],"_",sampling_month_dat$run_samp[index_m],"_",
700
                       sampling_dat$date[index_d],"_",sampling_dat$prop[index_d],"_",sampling_dat$run_samp[index_d],sep="")
701
      raster_name_delta<-file.path(out_path,paste(interpolation_method,"_",var,"_",data_name,out_prefix,".tif", sep=""))
702
      writeRaster(daily_delta_rast, filename=raster_name_delta,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
703
      
704
      list_daily_delta_rast[[k]] <- raster_name_delta   
705
    }
706
    
707
    raster_name_delta <- list_daily_delta_rast
708
    mod_krtmp2 <- list_mod_krtmp2
519 709
  }
520

  
521
  daily_delta_xy<-as.matrix(cbind(dmoday$x,dmoday$y))
522
  fitdelta<-Krig(daily_delta_xy,daily_delta,theta=1e5) #use TPS or krige
523
  mod_krtmp2<-fitdelta
524
  model_name<-paste("mod_kr","day",sep="_")
525
  data_s<-dmoday #put the 
526
  data_s$daily_delta<-daily_delta
527 710
  
528 711
  #########
529 712
  # STEP 4 - Calculate daily predictions - T(day) = clim(month) + delta(day)
530 713
  #########
531 714
  
532
  rast_clim_list<-rast_clim_yearlist[[mo]]  #select relevant month
533
  rast_clim_month<-raster(rast_clim_list[[1]])
534
  
535
  daily_delta_rast<-interpolate(rast_clim_month,fitdelta) #Interpolation of the bias surface...
536
  
537
  #Saving kriged surface in raster images
538
  data_name<-paste("daily_delta_",y_var_name,"_",sampling_dat$date[i],"_",sampling_dat$prop[i],
539
                   "_",sampling_dat$run_samp[i],sep="")
540
  raster_name_delta<-file.path(out_path,paste(interpolation_method,"_",var,"_",data_name,out_prefix,".tif", sep=""))
541
  writeRaster(daily_delta_rast, filename=raster_name_delta,overwrite=TRUE)  #Writing the data in a raster file format...(IDRISI)
542
  
715
  if(use_clim_image==FALSE){
716
    list_daily_delta_rast <- rep(raster_name_delta,length=nlayers(rast_clim_mod))
717
  }
543 718
  #Now predict daily after having selected the relevant month
544
  temp_list<-vector("list",length(rast_clim_list))  
545
  for (j in 1:length(rast_clim_list)){
546
    rast_clim_month<-raster(rast_clim_list[[j]])
547
    temp_predicted<-rast_clim_month+daily_delta_rast
548
    
549
    data_name<-paste(y_var_name,"_predicted_",names(rast_clim_list)[j],"_",
550
                     sampling_dat$date[i],"_",sampling_dat$prop[i],
551
                     "_",sampling_dat$run_samp[i],sep="")
719
  temp_list<-vector("list",nlayers(rast_clim_mod))  
720
  for (k in 1:nlayers(rast_clim_mod)){
721
    rast_clim_month<-raster(rast_clim_list[[k]])
722
    daily_delta_rast <- raster(list_daily_delta_rast[[k]])
723
    temp_predicted<-rast_clim_month + daily_delta_rast
724
    
725
    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],"_",
726
                     sampling_dat$date[index_d],"_",sampling_dat$prop[index_d],"_",sampling_dat$run_samp[index_d],sep="")
552 727
    raster_name<-file.path(out_path,paste(interpolation_method,"_",data_name,out_prefix,".tif", sep=""))
553 728
    writeRaster(temp_predicted, filename=raster_name,overwrite=TRUE) 
554
    temp_list[[j]]<-raster_name
729
    temp_list[[k]]<-raster_name
555 730
  }
556 731
  
557 732
  ##########
558 733
  # STEP 5 - Prepare output object to return
559 734
  ##########
560 735
  
561
  mod_krtmp2<-fitdelta
562
  model_name<-paste("mod_kr","day",sep="_")
563
  names(temp_list)<-names(rast_clim_list)
564
  coordinates(data_s)<-cbind(data_s$x,data_s$y)
565
  proj4string(data_s)<-proj_str
736
  data_s<-dmoday #put the 
737
  #coordinates(data_s)<-cbind(data_s$x,data_s$y)
738
  #proj4string(data_s)<-proj_str
566 739
  coordinates(data_v)<-cbind(data_v$x,data_v$y)
567 740
  proj4string(data_v)<-proj_str
568 741
  
569
  delta_obj<-list(temp_list,rast_clim_list,raster_name_delta,data_s,
570
                  data_v,sampling_dat[i,],mod_krtmp2)
742
  #mod_krtmp2<-list_mod_krtmp2    
743
  #mod_krtmp2<-fitdelta
744

  
745
  model_name<-paste("mod_kr","day",sep="_")
746
  
747
  names(temp_list)<-names(rast_clim_list)
748

  
749
  #add data_month_s and data_month_v?
750
  delta_obj<-list(temp_list,rast_clim_list,raster_name_delta, data_s, data_v,
751
                 modst,data_month_v,sampling_dat[index_d,],daily_dev_sampling_dat[i,],mod_krtmp2)
571 752
  
572 753
  obj_names<-c(y_var_name,"clim","delta","data_s","data_v",
573
               "sampling_dat",model_name)
754
               "data_month_s","data_month_v","sampling_dat","daily_dev_sampling_dat",model_name)
574 755
  names(delta_obj)<-obj_names 
575
  save(delta_obj,file= file.path(out_path,paste("delta_obj_",var,"_",sampling_dat$date[i],"_",sampling_dat$prop[i],
576
                                "_",sampling_dat$run_samp[i],out_prefix,".RData",sep="")))
756
  save(delta_obj,file= file.path(out_path,paste("delta_obj_",var,"_",sampling_month_dat$prop[index_m],"_",sampling_month_dat$run_samp[index_m],"_",
757
                                                sampling_dat$date[index_d],"_",sampling_dat$prop[index_d],"_",sampling_dat$run_samp[index_d],
758
                                                out_prefix,".RData",sep="")))
577 759
  return(delta_obj)
578 760
  
579 761
}
580 762
 
763
combine_sampling_daily_monthly_for_daily_deviation_pred <- function(sampling_obj,sampling_month_obj){
764
  #This function combines sampling objects information at the daily and monthly time scale.
765
  #The data.frame created records propotion of hold out, sampling number as well as provides keys (index_d,index_m)
766
  #to access training and testing samples at the daily and monthly time scale (long term monthly!!!)
767
  #Inputs:
768
  #sampling_obj: contains daily sampling information and is output of "sampling_training_testing" function defined in sampling_script_functions.
769
  #sampling_month_obj: contains monthly sampling information and is output of "sampling_training_testing" function defined in sampling_script_functions
770
  
771
  sampling_month_dat <- sampling_month_obj$sampling_dat #extract monthly information about sampling (in data.frame format)
772
  sampling_dat <- sampling_obj$sampling_dat #extract daily information about samplint (in data.frame format)
773
              
774
  #Build new data.frame with combined information
775
  data_daily_dev <- sampling_dat 
776
  data_daily_dev$index_d <- 1:nrow(data_daily_dev) # index_d is a identifier key to the list of daily samples generated earlier in sampling_obj
777
  data_daily_dev$month_no <- as.integer(strftime(strptime(data_daily_dev$date, "%Y%m%d"),"%m")) #Extract month given specific format  "%Y%m%d"
778
  
779
  names(sampling_month_dat) <- c("dates_month","run_samp_month","prop_month") #rename before merging as table product
780
  sampling_month_dat$month_no <-as.integer(strftime(strptime(sampling_month_dat$dates_month, "%Y%m%d"),"%m")) #month of the record
781
  sampling_month_dat$index_m <-1:nrow(sampling_month_dat) # index_m is a identifier key to the list of monthly samples 
782
                                                          #generated earlier in sampling_month_obj
783
  
784
  data_daily_dev <-merge(data_daily_dev,sampling_month_dat,by = "month_no")
785
  
786
  return(data_daily_dev)
787

  
788
}

Also available in: Unified diff