根据时间查找两个目录(在 10 分钟内)。恶魔般的目录灾难

Finding two directories (which are in ten min bins) based on a time. A diabolical directory disaster

我四处寻找,找不到可行的解决方案。一些背景知识:

我正在使用 R 根据经过验证的图像名称查找原始图像(所有这些都有效)。问题是至少有 30 个日期目录,每个目录都有大量时间目录,这些目录被分成 10 分钟的容器。查看所有 bin 或仅查看父目录在计算上要求太多。 bin 的示例格式为

 R_Experiments\RawImageFinder\Raw16-10-0836
 R_Experiments\RawImageFinder\Raw16-10-0846

请务必注意,bins 与其起始分钟数不一致;它可能会有所不同,这就是问题所在。

我知道使用以下代码从文件名中获取图像的时间

SingleImage <- Pia1.2016-10-08.1103+N2353_hc.tif
TimeDir <- sub('.*?\.\d{4}-\d{2}-\d{2}\.(\d{2})(\d{2}).*', '\1:\2', SingleImage)
TimeDir <- sub(':','', TimeDir)
#
> print(TimeDir)
[1] "1103"

因此该图像可能属于以下任一容器:

 53,54,55,..you get the idea...,12,13

这仅取决于垃圾箱何时启动。因此,我希望 "finder" 代码查看任一侧 tin mins 内的所有可能的 bin(如上例所示),显然其中一些将不存在。 我考虑过这样做:

TimeDir1 <- as.numeric(TimeDir)+1
TimeDir2 <- as.numeric(TimeDir)+2

但是如果我们达到 59 分钟,问题就会出现,因为每小时没有 61 分钟这样的东西(哈哈)。

然后我使用以下命令来告诉要搜索哪些目录,尽管我对如何告诉它在多个目录中查找也有点困惑。

  Directorytosearch <- ParentDirectory
 #this has the \ in it, same for time, it works
  Directorytosearch <- sub('$',paste(DateDir), Directorytosearch)
  Directorytoserach <- sub('$',paste(TimeDir), Directorytoserach)


  IMAGEtocopy <- list.files(
      path = c(Directorytosearch),
      recursive = TRUE,
      include.dirs = FALSE,
      full.names = FALSE,
      pattern = SingleImagePattern)

任何帮助真的很棒! 可以使用 strptime 函数吗? 非常感谢

吉姆

更新@Nya

test <- strptime("1546", format = "%H%M")
dirs[select.image.dir(test, dirs.time)]
> dirs[select.image.dir(test, dirs.time)]
[1] "test/1546"

要列出目录,您正在寻找 list.dirs() 函数。假设以下示例是通过对所有目录的此类搜索获得的。

# directories possibly obtained with list.dirs
dirs <- c("test/1536", "test/1546", "test/1556", "test/1606")

一个好的做法是从目录和图像文件名中提取日期和时间部分。在这里,我将只使用时间,因为那是最初的请求。

# convert times
dirs.time <- sub(".*/(\d+)$", "\1", dirs)
dirs.time <- strptime(dirs.time, format="%H%M")

# test data, in your case from image file names
test <- strptime(c("1538", "1559", "1502"), format="%H%M")

通过比较图像文件的时间是否在目录时间的上下 10 分钟间隔内,将 select 所需目录的功能。然后它将提供图像所在的索引。

select.image.dir <- function(i, dt){
    res <- NULL
    # adding and substracting 10 minutes converted to seconds
    ik <- c(i - 600, i + 600)
    condition <- c(ik[1] <= dt & ik[2] >= dt)
    if(any(condition)){
        res <- which(condition)
    } else { res <- NA }
    res
}    

请注意,更新后的函数接受单个图像文件时间以在每一轮中进行测试。然后可以使用索引提取图像目录的路径。最后一次超出目录范围,因此函数 returns NA.

dirs[select.image.dir(test[1], dirs.time)]
# [1] "test/1536" "test/1546"
dirs[select.image.dir(test[2], dirs.time)]
# [1] "test/1556" "test/1606"
dirs[select.image.dir(test[3], dirs.time)]
# [1] NA NA NA NA