R/DatePreserveFITS_function.R

Defines functions DatePreserveFITS

Documented in DatePreserveFITS

#' ReVuePro: DatePreserveFITS
#'
#' An integrative function that collects the date of creation (ie. capture) from JPG images, and assigns them to a list of RAW files, as created by the function 'RmFlirTag'. Note that this function calls upon Windows Powershell in Windows, and Bash in Linux. It is therefore limited to Windows and Linux users for the moment. Furthermore, note that order of files within a given folder matter.  
#' @param PathtoJPGs The path to a folder containing JPG images from which you wish to extract creation dates. 
#' @param PathtoFITS The path to a folder containing RAW images, to which you wish to assign creation dates to.
#' @keywords VuePro, Thermal
#' @export
#' @examples
#' MyJPGS<-"C:/Users/JKR/Desktop/JPGPhotos"
#' MyFITS<-"C:/Users/JKR/Desktop/FITSPhotos"

#' DatePreserveFITS(MyJPGS, MyRAWS)

DatePreserveFITS<-function(PathtoJPGs,PathtoFITS){
    if(Sys.info()[[1]]=="Windows"){    
        if(length(list.files(PathtoJPGs))==length(list.files(PathtoFITS))){

        ReconRoot<-as.character(gsub("/", "\\\\\\\\", PathtoJPGs))
        ReconDest<-as.character(gsub("/", "\\\\\\\\", PathtoFITS))
 
        powershellstring<-{ 
            paste("powershell Import-Module Microsoft.PowerShell.Management; ",
            "Import-Module Microsoft.PowerShell.Utility; ",
            "Import-Module PSReadline; ",

            "$JPGDir = ", "\\", '"', ReconRoot, "\\", '"', "; ", 
            "$FITSDir = ", "\\", '"', ReconDest, "\\", '"', "; ", 

            "$JPGChI = get-childitem $JPGDir; ",
            "$FITSChI  = get-childitem $FITSDir; ",

            "$JPGList = $JPGChI | where {$_.extension -eq ", "\\", '"', ".jpg", "\\", '"',"}; ",
  
            "cd $JPGDir; ",

            "$JPGDates = for ($i=0; $i -le $JPGList.Length-1; $i++){",
            "(Get-Item $JPGList[$i]).LastWriteTime}; ",

            "cd $FITSDir; ",

            "$FITSList = $FITSChI | where {$_.extension -eq ", "\\", '"', ".fits", "\\", '"', "}; ",

            "for ($i=0; $i -le $JPGList.Length-1; $i++){ ",
            "(Get-Item $FITSList[$i]).LastWriteTime = $JPGDates[$i] }", sep="")
            }
 
        system(powershellstring)
      } else {
    print("Unequal files in JPG and FITS folders. Please assess your folder contents.")
    } 
  }
    if(Sys.info()[[1]]=="Linux"){
    if(length(list.files(PathtoJPGs))==length(list.files(PathtoFITS))){
    stringCount=length(list.files(PathtoJPGs))+2
    BashString=paste("bash -c '", "mapfile -t JpgArray < <(for ((i=2;i<",stringCount,";i++)); do (cd ",
                      '"', PathtoJPGs,'"',' && find ','"',"$(pwd)",'"'," -maxdepth 1 | sort",") | sed -n ${i}p; done); ",
                      "mapfile -t FITSArray < <(for ((i=2;i<",stringCount,";i++)); do (cd ",'"', PathtoFITS,'"',
                      ' && find ','"',"$(pwd)",'"'," -maxdepth 1 | sort"," ) | sed -n ${i}p; done); ",
                      "for ((i=0;i<${#JpgArray[*]};i++)) do touch -r ",'"',
                      "${JpgArray[i]}",'" ','"',"${FITSArray[i]}",'"',"; done;'", sep="")
    system(BashString)
   } else {
    print("Unequal files in JPG and FITS folders. Please assess your folder contents.")
    }
  }
}
joshuakrobertson/R-Package_ReVuePro documentation built on June 2, 2020, 8:23 p.m.