R/FilterFITS_function.R

Defines functions FilterFITS

Documented in FilterFITS

#' ReVuePro: FilterFITS
#'
#' A function to read two-dimensional FITS images, and filter files according to the maximum value within pixel range.
#' @param source The path to a file, or folder of FITS files you wish filter.
#' @param destination The path to a folder where you wish to relocate images that do not meet image criteria. 
#' @param max.criteria The maximum pixel value a FITS image must contain to be retained.
#' @keywords Thermal Imaging, ReVuePro
#' @import FITSio
#' @export
#' @examples
#' source="C:/ThermalFITSfiles"
#' destination="C:/ToDelete"
#' FilterFITS(source=source, destination=destination, max.criteria=28.0)

FilterFITS = function(source, destination, max.criteria){
  
      originalwd = getwd()
      require('FITSio')
      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(!is.character0(list.files(source))){
      for(i in length(list.files(source)):1){ 
      setwd(source)   
      toFilter=list.files()[i]
      fits=readFITS(toFilter)
      if(max(fits$dat[[1]])<max.criteria){
          my.file.rename(list.files(source, full.names=TRUE)[i], paste(destination, "/", basename(list.files(source)[i]), sep=""))
        }
      }
     setwd(originalwd)
    }

    if(is.character0(list.files(source))){

      fits=readFITS(toFilter)
      if(max(fits$dat[[1]])<max.criteria){
          my.file.rename(list.files(source, full.names=TRUE)[i], paste(destination, "/", basename(list.files(source)[i]), sep=""))
      }
     setwd(originalwd)
   }
}
joshuakrobertson/R-Package_ReVuePro documentation built on June 2, 2020, 8:23 p.m.