R/ReadTempScape_function.R

Defines functions ReadTempScape

Documented in ReadTempScape

#' ReVuePro: ReadTempScape
#'
#' A function to read in .CSV TempScape files, produced using the Temp_Scape, or Temp_ScapeET functions.
#' @param file The path to a .CSV "TempScape" file, or list of .CSV "TempScape" files. There is no default input.
#' @param manip The type of manipulation/calculation you wish to apply to the "Temperature" column of the .CSV "TempScape" file. Options include: "dist2Tmax" = distance in pixels to the coordinate with maximum temperature in absolute value form, "dist2Tmin" = distance in pixels to the coordinate with minimum temperature in absolute value form, "dist2xmax" = distance in pixels to the x coordinate with maximum temperature in absolute value form, "dist2xmin" = distance in pixels to the x coordinate with minumum temperature in absolute value form, "dist2ymax" = distance in pixels to the y coordinate with maximum temperature in absolute value form, "dist2ymin" = distance in pixels to the y coordinate with minimum temperature in absolute value form, "diff2Tmax" = difference in degrees Celsius of a given coordinate to the maximum temperature, "diff2Tmin" = difference in degrees Celsius of a given coordinate to the minimum temperature, "diff2xmax" = equivalent to "dist2xmax" but not in absolute value form, "diff2xmin" = equivalent to "dist2xmin" but not in absolute value form, "diff2ymax" = equivalent to "dist2ymax" but not in absolute value form, "diff2ymin" = equivalent to "dist2ymin" but not in absolute value form. All manipulation outputs will be assigned to a new column in the .CSV input file. Multiple arguments must be placed in list form. Default is NULL. 
#' @param plot A TRUE or FALSE argument, determining whether a three-dimensional plot is to be produced using your "TempScape" file; whereby the z axis is "Temperature". Default is "FALSE". 
#' @keywords VuePro, Thermal Image
#' @import dplyr plot3D stringr
#' @export
#' @examples
#' setwd("C:/Users/Tabby/Desktop/Chickadees")
#' MyTempScape<-ReadTempScape("BirdTempScape.csv", manip = list("dist2Tmax", "diff2xmin"), plot = "TRUE")

         ReadTempScape = function(file, manip=NULL, plot="FALSE"){
            
         require("plot3D")
         require("dplyr")
         require("stringr")

         if(length(file)>5){
            if(plot=="TRUE"){
              return("Plotting with more than 5 datasheets is not supported by this function")
            }
          }

         for(j in 1:length(file)){

             TempScape=read.csv(file[j])

         if(length(unlist(manip))>4){
             return("readTempScape only supports a maximum of 4 manipulations. Please remove excess manipulations.")
             }

         if(length(manip)%in%c(1:4)){
             if(!is.null(grep("Tmax", manip))){
                Tmax=max(TempScape$Temperature)
                xmax=TempScape$x[which(TempScape$Temperature==max(TempScape$Temperature))]
                ymax=TempScape$y[which(TempScape$Temperature==max(TempScape$Temperature))]
                }
             if(!is.null(grep("Tmin", manip))){
                Tmin=min(TempScape$Temperature)
                xmin=TempScape$x[which(TempScape$Temperature==min(TempScape$Temperature))]
                ymin=TempScape$y[which(TempScape$Temperature==min(TempScape$Temperature))]
                }
             if(!is.null(grep("xmax", manip))){
                xmax=TempScape$x[which(TempScape$Temperature==max(TempScape$Temperature))]
                }
             if(!is.null(grep("xmin", manip))){
                xmin=TempScape$x[which(TempScape$Temperature==min(TempScape$Temperature))]
                }
             if(!is.null(grep("ymax", manip))){
                ymax=TempScape$y[which(TempScape$Temperature==max(TempScape$Temperature))]
                }
             if(!is.null(grep("ymin", manip))){
                ymin=TempScape$y[which(TempScape$Temperature==min(TempScape$Temperature))]
                }

             if(!is.null(grep("dist2", manip))){
                for(i in 1:length(grep("dist2", manip))){
                    if(!is.null(grep("Tmax", manip[grep("dist2", manip)[i]]))){
                       dist2Tmax = sqrt(abs(TempScape$x-xmax)^2+abs(TempScape$y-ymax)^2)
                       if("dist2Tmax"%in%unlist(manip)){
                             TempScape$dist2Tmax=dist2Tmax
                          }
                       }
                    if(!is.null(grep("Tmin", manip[grep("dist2", manip)[i]]))){
                       dist2Tmin = sqrt(abs(TempScape$x-xmin)^2+abs(TempScape$y-ymin)^2)
                       if("dist2Tmin"%in%unlist(manip)){
                             TempScape$dist2Tmin=dist2Tmin
                          }
                       }
                    if(!is.null(grep("xmax", manip[grep("dist2", manip)[i]]))){
                       dist2xmax = abs(TempScape$x-xmax)
                       if("dist2xmax"%in%unlist(manip)){
                             TempScape$dist2xmax=dist2xmax
                          }
                       }
                    if(!is.null(grep("xmin", manip[grep("dist2", manip)[i]]))){
                       dist2xmin = abs(TempScape$x-xmin)
                       if("dist2xmin"%in%unlist(manip)){
                             TempScape$dist2xmin=dist2xmin
                          }
                       }
                    if(!is.null(grep("ymax", manip[grep("dist2", manip)[i]]))){
                       dist2ymax = abs(TempScape$y-ymax)
                       if("dist2ymax"%in%unlist(manip)){
                             TempScape$dist2ymax=dist2ymax
                          }
                       }
                    if(!is.null(grep("ymin", manip[grep("dist2", manip)[i]]))){
                       dist2ymin = abs(TempScape$y-ymin)
                       if("dist2ymin"%in%unlist(manip)){
                             TempScape$dist2ymin=dist2ymin
                          }
                       }
                    }
                 }
                     
             if(!is.null(grep("diff", manip))){
                for(i in 1:length(grep("diff", manip))){
                    if(!is.null(grep("Tmax", manip[grep("diff", manip)[i]]))){
                       diff2Tmax = TempScape$Temperature-Tmax
                       if("diff2Tmax"%in%unlist(manip)){
                             TempScape$diff2Tmax=diff2Tmax
                          }
                       }
                    if(!is.null(grep("Tmin", manip[grep("diff", manip)[i]]))){
                       diff2Tmin = TempScape$Temperature-Tmin
                       if("diff2Tmin"%in%unlist(manip)){
                             TempScape$diff2Tmin=diff2Tmin
                          }
                       }
                    if(!is.null(grep("xmax", manip[grep("diff", manip)[i]]))){
                       diff2xmax = abs(TempScape$x-xmax)
                       if("diff2xmax"%in%unlist(manip)){
                             TempScape$diff2xmax=diff2xmax
                          }
                       }
                    if(!is.null(grep("xmin", manip[grep("diff", manip)[i]]))){
                       diff2xmin = abs(TempScape$x-xmin)
                       if("diff2xmin"%in%unlist(manip)){
                             TempScape$diff2xmin=diff2xmin
                          }
                       }
                    if(!is.null(grep("ymax", manip[grep("diff", manip)[i]]))){
                       diff2ymax = abs(TempScape$y-ymax)
                       if("diff2ymax"%in%unlist(manip)){
                             TempScape$diff2ymax=diff2ymax
                          }
                       }
                    if(!is.null(grep("ymin", manip[grep("diff", manip)[i]]))){
                       diff2ymin = abs(TempScape$y-ymin)
                       if("diff2ymin"%in%unlist(manip)){
                             TempScape$diff2ymin=diff2ymin
                          }
                       }
                    }
                 }

             ## Further mathematics
             if(!is.null(grep("/|*|+|-|sqrt|^|sd", manip))){
                for(i in 1:length(grep("/|*|+|-|sqrt|^|sd", manip))){
                assign(paste(unlist(manip)[grep("/|*|+|-|sqrt|^|sd", manip)[i]]), eval(parse(text=unlist(manip)[grep("/|*|+|-|sqrt|^|sd", manip)[i]])))
                TempScape[[paste(unlist(manip)[grep("/|*|+|-|sqrt|^|sd", manip)[i]])]] = eval(parse(text=unlist(manip)[grep("/|*|+|-|sqrt|^|sd", manip)[i]]))
                }
             }
          }     

         if(plot=="TRUE"){
         scatter3D(x=TempScape$x, y=TempScape$y, z=TempScape$Temperature)
         if(length(file)>1){
         dev.new()
        }
      }
         return(TempScape)
      }
 }
joshuakrobertson/R-Package_ReVuePro documentation built on June 2, 2020, 8:23 p.m.