R/Temp_ScapeET_function.R

Defines functions Temp_ScapeET

Documented in Temp_ScapeET

#' ReVuePro: Temp_ScapeET

#' A function to decrypt radiometric JPGs (rJPGs) captured using the FLIR Vue Pro R, and produce CSVs of temperature according to pixel coordinates. Calls upon an external CSV for ambient temperature values.
#' @details Note that RAW files will be written such that their creation date matches that of the parent JPG. Furthermore, this function calls upon Microsoft Powershell, and is therefore currently limited to Windows users.
#' @param source A path to the folder containing radiometric JPGs (rJPGs) to be rewritten. There is no default input.
#' @param destination The path to a folder at wish you would like your output CSVs to be saved.
#' @param pwidth The width, in pixels, of the thermal image. Default is 336.
#' @param pheight The height, in pixels, of the thermal image Default is 256.
#' @param P_R1 Planck_R1 from function "Rad_Temp" in package "ReVuePro". A callibration constant, specific per FLIR camera. This can be determined using 'exiftool.exe', produced by Phil Harvey. Default is 17096.453125.
#' @param P_R2 Planck_R2 from function "Rad_Temp" in package "ReVuePro". A callibration constant, specific per FLIR camera. This can be determined using 'exiftool.exe', produced by Phil Harvey. Default is 0.04351538.
#' @param P_B Planck_B from function "Rad_Temp" in package "ReVuePro". A callibration constant, specific per FLIR camera. This can be determined using 'exiftool.exe', produced by Phil Harvey. Default is 1428.0.
#' @param P_F Planck_F from function "Rad_Temp" in package "ReVuePro". A callibration constant, specific per FLIR camera. This can be determined using 'exiftool.exe', produced by Phil Harvey. Default is 1.0.
#' @param P_O Planck_O from function "Rad_Temp" in package "ReVuePro". A callibration constant, specific per FLIR camera. This can be determined using 'exiftool.exe', produced by Phil Harvey. Default is -55.0.
#' @param emiss Emmissivity from function "Rad_Temp" in package "ReVuePro". The relative energy that is emitted (or radiated) from the surface of the object in the image, compared to that emitted from a blackbody in equivalent conditions. This value must lay between 0 and 1. Default is 0.98.
#' @param OD obj_distance from function "Rad_Temp" in package "ReVuePro". The distance between the thermal imaging camera and object to be measured within the image. This value is expressed in metres. Default is 1.0 metres.
#' @param AT_Path A path to an XLSX or CSV file containing ambient temperature and time at which ambient temperature was collected. Currently, this package supports formatting produced by WeeButton(TM) software, or outputs from the "CButtonFile", or "CButtonDate" functions (from the ReVuePro package). 
#' @param transmit wind_transmittance from function "Rad_Temp" in package "ReVuePro". The relative amount of light transmitted though a window between the thermal imaging camera and object in question. This value must lay between 0 and 1, with a default value of 1.
#' @param hum humidity from function "Rad_Temp" in package "ReVuePro". Relative, atmospheric humidity at the time if image character. This value is expressed as a percentage, and therefore should lay between 0 and 100. 
#' @keywords VuePro, Thermal
#' @import openxlsx rio
#' @examples
#' source = "C:/MyJPGs"
#' destination = "C:/MyTempScapes"
#' ambtempdata = "C:/Temperature_Data/ExperimentTemp.xlsx"
#' Temp_ScapeET(source = source, destination = destination, pwidth = 336, pheight = 256, P_R1 = 17096.453125, P_R2 = 0.04351538,
#'                  P_B = 1428.0, P_F = 1, P_O = -55.0, emiss = 0.98, OD = 1.0, AT_Path = ambtempdata, hum = 30) 
#' @export

## Rewritting FLIR JPGs with temperature values in IEEE-754 single point notation


Temp_ScapeET<-function(source, destination, pwidth = 336, pheight = 256, P_R1 = 17096.453125, 
                           P_R2 = 0.04351538, P_B = 1428.0, P_F = 1, P_O = -55.0, 
                           emiss = 0.98, OD = 1.0, AT_Path, transmit = 1, hum){

      require('openxlsx')
      require('rio')
      setwd(source)
      originalwd<-getwd()      
      dir2<-destination

      ## Preparing temperature CSV
 
            if(gsub("^.*\\.", "", AT_Path)=="xlsx"){
      ATFile_Dir = gsub(basename(AT_Path),"", AT_Path)
      ATFile_Name = gsub("\\..*", "", basename(AT_Path))
      AT_CSV = paste(ATFile_Dir, ATFile_Name, ".csv", sep="")
      convert(AT_Path, AT_CSV)
      CButtonFile(AT_CSV)
      AT_CSVmod = paste(ATFile_Dir,basename(list.files(ATFile_Dir, full.names=TRUE)[which.max(file.info(list.files(ATFile_Dir, full.names=TRUE))$mtime)]),sep="")
      AT_Clean = read.csv(AT_CSVmod)
      CSVRm <- AT_CSVmod
      if (file.exists(CSVRm)){file.remove(CSVRm)}
      }
            if(gsub("^.*\\.", "", AT_Path)=="xls"){
      ATFile_Dir = gsub(basename(AT_Path),"", AT_Path)
      ATFile_Name = gsub("\\..*", "", basename(AT_Path))
      AT_CSV = paste(ATFile_Dir, ATFile_Name, ".csv", sep="")
      convert(AT_Path, AT_CSV)
      CButtonFile(AT_CSV)
      AT_CSVmod = paste(ATFile_Dir,basename(list.files(ATFile_Dir, full.names=TRUE)[which.max(file.info(list.files(ATFile_Dir, full.names=TRUE))$mtime)]),sep="")
      AT_Clean = read.csv(AT_CSVmod)
      CSVRm <- AT_CSVmod
      if (file.exists(CSVRm)){file.remove(CSVRm)}
      }
            if(gsub("^.*\\.", "", AT_Path)=="csv"){
               temp_AT=read.csv(AT_Path)
               if(colnames(temp_AT)[1]=="Date"){
               AT_Clean = temp_AT
               } else {
               CButtonFile(AT_Path)
               ATFile_Dir = gsub(basename(AT_Path),"", AT_Path)
               AT_Data = paste(ATFile_Dir,basename(list.files(ATFile_Dir, full.names=TRUE)[which.max(file.info(list.files(ATFile_Dir, full.names=TRUE))$mtime)]),sep="")
               AT_Clean = read.csv(AT_Data)
               CSVRm <- AT_Data
               if (file.exists(CSVRm)){file.remove(CSVRm)}
               }
               } 
               FairExtensions=c("csv","xlsx")
               if(gsub("^.*\\.", "", AT_Path)%in%FairExtensions=="FALSE"){
                 return("Incorrect format of ambient temperature file")
               }
      
      ## Reconstructing FLIR image
     
      for(i in 1:length(list.files())){
      
      file=list.files()[i]      

      WriteFile=gsub("\\..*", "", file)

      readingIn <- file(file, "rb")
      BinData <- readBin(readingIn, raw(), n = file.info(file)$size)
      close(readingIn)

      toremove<-c(67703:67714,133239:133250)
      Tagless<-BinData[-toremove]
      
      Imagelength = (pwidth*pheight)*2
      Image = Tagless[2723:(2722+Imagelength)]
      
      # Converting to radiance values

      tempcon <- file("temp") 
      open(tempcon, "wb")
      writeBin(Image, tempcon, raw(), endian = .Platform$endian)
      close(tempcon)
      
      readTemp <- file("temp", "rb")
      radiance <- readBin(readTemp, integer(), n = file.info("temp")$size, size=2, endian="little")
      close(readTemp)
      
      ToRemove <- "temp"
      if (file.exists(ToRemove)){file.remove(ToRemove)}
      
      # Extracting relevant temperature from CSV

      Time=as.numeric(unlist(strsplit(sub(".*? (.+)", "\\1", file.info(list.files()[i])$mtime), ":")))
      Seconds=sum(Time[1]*3600,Time[2]*60,Time[3])
      
    { P_R1 = P_R1
      P_R2 = P_R2
      P_B = P_B
      P_F = P_F
      P_O = P_O
      emiss = emiss
      OD = OD
      AT = AT_Clean[which(abs(AT_Clean$TIS-Seconds)==min(abs(AT_Clean$TIS-Seconds))),5]
      RT = AT_Clean[which(abs(AT_Clean$TIS-Seconds)==min(abs(AT_Clean$TIS-Seconds))),5]
      WT = AT_Clean[which(abs(AT_Clean$TIS-Seconds)==min(abs(AT_Clean$TIS-Seconds))),5]
      transmit = transmit
      hum = hum
      

      Temp_vals = Rad_Temp(radiance, Planck_R1 = P_R1, 
                           Planck_R2 = P_R2, Planck_B = P_B, Planck_F = P_F, Planck_O = P_O, 
                           emissivity = emiss, obj_distance = OD, amb_temp = AT, ref_temp = AT,
                           window_temp = WT, wind_transmittance = transmit, humidity = hum)
      }
      closeAllConnections()

      ypixels=c()
      for(j in pheight:1){
      ypixels=c(ypixels,rep(j, each=336))
      } 
      xpixels=c(rep(c(1:336), times=256))
      Temp_Scape = data.frame("x" = xpixels, "y" = ypixels, "Temperature" = Temp_vals)
      Temp_Dest = paste(destination, "/", WriteFile, ".csv", sep="") 
      write.csv(Temp_Scape, file = Temp_Dest)
   }     
  setwd(originalwd)      
}
joshuakrobertson/R-Package_ReVuePro documentation built on June 2, 2020, 8:23 p.m.