R/SampleFITS_function.R

Defines functions SampleFITS

Documented in SampleFITS

#' ReVuePro: SampleFITS
#'
#' A function to read three-dimensional FITS images, pull first frame and export as a JPEG file for simple thumbnail viewing. Output files are black and white.
#' @param source The path to a file, or folder of FITS files you wish to sample.
#' @param destination The path to a folder where you wish to save output images. 
#' @param parallel A binary "TRUE/FALSE" variable that determines whether internal operations will be run by parallel processing. If "TRUE", function will call upon the "doParallel" package. Default is "TRUE".
#' @keywords Thermal Imaging, ReVuePro
#' @import BiocManager FITSio doParallel EBImage
#' @export
#' @examples
#' source="C:/ThermalFITSfiles"
#' destination="C:/Output"
#' SampleFITS(source = source, destination = destination, parallel = "TRUE")

SampleFITS = function(source, destination, parallel = "TRUE"){
  
      originalwd = getwd()
      require('FITSio')
	require("doParallel")
	require("EBImage")

      is.character0 <- function(x){
      is.character(x) && length(x) == 0L
      }

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

  if (parallel == "TRUE"){
   if(!is.character0(list.files(source))){
 	registerDoParallel()
  	foreach (i=1:length(list.files(source))) %dopar% { 
      setwd(source)   
      toFilter=list.files()[i]
      	fits=readFITS(toFilter)
		toWrite = fits$dat[[1]][,,1]
		toWrite = (toWrite - min(toWrite))/(max(toWrite) - min(toWrite))
		toWrite = rotate(toWrite, 180)
		writeImage(toWrite, paste(destination, "/", gsub("\\..*", "", toFilter), ".jpeg", sep = ""))
        }
     setwd(originalwd)
    }
	} else if (parallel == "FALSE") {
	if(!is.character0(list.files(source))){
	for(i in 1:length(list.files(source))){ 
      setwd(source)   
      toFilter=list.files()[i]
      	fits=readFITS(toFilter)
		toWrite = fits$dat[[1]][,,1]
		toWrite = (toWrite - min(toWrite))/(max(toWrite) - min(toWrite))
		toWrite = rotate(toWrite, 180)
		writeImage(toWrite, paste(destination, "/", gsub("\\..*", "", toFilter), ".jpeg", sep = ""))
        }
   	  setwd(originalwd)
   	 }
	}

    if(is.character0(list.files(source))){
      	fits=readFITS(source)
		toWrite = fits$dat[[1]][,,1]
		toWrite = (toWrite - min(toWrite))/(max(toWrite) - min(toWrite))
		toWrite = rotate(toWrite, 180)
		writeImage(toWrite, paste(destination, "/", gsub("\\..*", "", basename(source)), ".jpeg", sep = ""))
		}
     setwd(originalwd)
   }
joshuakrobertson/R-Package_ReVuePro documentation built on June 2, 2020, 8:23 p.m.