R/FLIR2Fits_function.R

Defines functions FLIR2Fits

Documented in FLIR2Fits

#' ReVuePro: FLIR2Fits
#'
#' A culmination function to decrypt radiometric JPGs (rJPGs) captured using a FLIR Vue Pro R camera, convert units of infrared radiation (kW/m2) to units of temperature (degrees Celsius), and construct a FITS file using IEEE754, single point format to encode temperature information. 
#' @details Note that FITS 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 to save your rewritten, Vue Pro radiometric JPG (rJPG).
#' @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 amb_temp from function "Rad_Temp" in package "ReVuePro". Ambient/atmospheric temperature at the time of image capture, in degrees Celcius.
#' @param RT ref_temp from function "Rad_Temp" in package "ReVuePro". The temperature reflected from the object in question - again, in degrees Celcius. Default is to assume that the reflected temperature is equal to ambient temperature.
#' @param WT window_temp from function "Rad_Temp" in package "ReVuePro". The temperature of window though which the thermal image was take, in degrees Calcius. Default is to assume that window temperature is equal to ambient temperature.
#' @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. 
#' @param battery_check If "TRUE", assess battery level of computer every ten loops. If batter power is below 5 percent, unprocessed images will be shuffled to a new folder entitled "JPGs2Do", within the parent directory. After files are shuffled, the R session will quit. Default is "TRUE".
#' @keywords VuePro, Thermal
#' @import FITSio
#' @export
#' @examples
#' FLIR2Fits("C:/MyJPGS", "C:/MyFITS", 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 = 23.0, hum = 30, battery_check=="FALSE") 

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


FLIR2Fits<-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, RT = AT,
                           WT = AT, transmit = 1, hum, battery_check="TRUE"){
      require('FITSio')
      battery_life = 100

	substrRight <- function(x, n){
      substr(x, nchar(x)-n+1, nchar(x))
	}

      my.file.rename <- function(from, to) {
      todir <- dirname(to)
	if (!isTRUE(file.info(todir)$isdir)) dir.create(todir, recursive=TRUE)
	file.rename(from = from,  to = to)
      }

      originalwd<-getwd()
      
      setwd(source)
      dir2<-destination
      
      for(i in 1:length(list.files())){
      
      file=list.files()[i]      

      WriteFile=gsub("\\..*", "", file)
      WriteFITS=paste(WriteFile, ".fits", sep="")

      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)}

    { 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
      RT = RT
      WT = WT
      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)
      }
      setwd(dir2)      

      FITSdf=as.data.frame(matrix(Temp_vals, ncol=256))
      FITSdf[,1:ncol(FITSdf)]=rev(FITSdf[,1:ncol(FITSdf)])
      writeFITSim(as.matrix(FITSdf), file = WriteFITS, type = "single")
      setwd(source)

      closeAllConnections()

      if(Sys.info()[[1]]=="Windows"){
      if(nchar(i)>1){
        if(substrRight(i, 1)==0){
          battery_life=as.numeric(substr(system("WMIC PATH Win32_Battery Get EstimatedChargeRemaining", intern=TRUE)[2],start=1,stop=2))
            if(battery_check=="TRUE"){
             if(battery_life<=5){
              for(k in 1:(length(list.files())-i)){
                # Shuffle JPGs yet to be done
                dir.create(paste(dirname(source), "/", "JPGs2Do", sep=""))
                     newdirJPG=paste(dirname(source), "/", "JPGs2Do", sep="")
                Remaining=list.files()[i+1]
                Remaining_name=paste(source, "/", Remaining, sep="")
                NewDest_name=paste(newdirJPG, "/",list.files()[i+k])
                my.file.rename(Remaining_name,NewDest_name)
                
                }
              quit()
        	} 
              }
     	   }
        }
      }
    }     
  DatePreserveFITS(source, destination) 
  setwd(originalwd)      
}
joshuakrobertson/R-Package_ReVuePro documentation built on June 2, 2020, 8:23 p.m.