使用archive_write_dir() 似乎会损坏栅格文件
法国的气象机构提供降水再分析数据,可以下载为.tar存档。每个存档包含三种栅格数据:降水总量、估计误差和雷达状态。后者体积特别大,由于我不需要它们,我想修改存档,只保留降水栅格数据。然而在修改后,当我尝试导入这些栅格数据时会出现错误。下面是脚本:
library(archive)
library(terra)
# download an example archive
download.file(url="https://object.files.data.gouv.fr/meteofrance/data/synchro_ftp/REANALYSES/COMEPHORE/H_COMEPHORE_202201.tar", destfile = "H_COMEPHORE_202201.tar", mode = "wb")
# list and filter the files inside the archive
dat.files<-unlist(archive("H_COMEPHORE_202201.tar"))
dat.files<-grep(pattern="_RR.gtif", x = dat.files, value = TRUE)
# extract those files into a folder
archive_extract("H_COMEPHORE_202201.tar", files = dat.files, dir="temp")
# import one of the rasters using terra
RR<-rast("temp/2022/2022010101_RR.gtif")
plot(RR)
# this works fine
# write the precipitation files into a new archive
archive_write_dir(archive = "archive.tar", dir = "temp", recursive = TRUE)
# clear the folder
unlink("temp/*", recursive = TRUE)
# extract the files again
archive_extract("archive.tar", dir="temp")
# import the same raster again using terra
RR<-rast("temp/2022/2022010101_RR.gtif")
# does not work anymore. I get the following error message:
# Error: [rast] cannot open this file as a SpatRaster: [...]/temp/2022/2022010101_RR.gtif
# In addition: Warning messages:
# 1: [...]/temp/2022/2022010101_RR.gtif:Failed to allocate memory for to read TIFF directory (0 elements of 12 bytes each) (GDAL error 1)
# 2: [...]temp/2022/2022010101_RR.gtif: TIFFReadDirectory:Failed to read directory at offset 18782 (GDAL error 1)
我到底哪里做错了?
非常感谢。
解决方案
我不知道为什么 archive_write_dir 无法写出一个有效的“archive.tar”。
{archive} 在底层运行C++,并封装了libarchive——任何错误很可能来自写入方法,正如 [Jon Spring] 所提出的,将问题定位在这里:
archive_write_files -> archive_write_files_ -> archive_write_files_。
将 archive_extract("H_COMEPHORE_202201.tar", files = dat.files, dir="temp") 替换为 utils::tar("archive.tar", files = "temp") 也能正常工作,不过。
R version 4.5.1
library(archive) # archive_1.1.13
library(terra) # terra_1.9-11
# download an example archive
download.file(url="https://object.files.data.gouv.fr/meteofrance/data/synchro_ftp/REANALYSES/COMEPHORE/H_COMEPHORE_202201.tar", destfile = "H_COMEPHORE_202201.tar", mode = "wb")
dat.files <- unlist(archive("H_COMEPHORE_202201.tar"))
dat.files <- grep(pattern="_RR.gtif", x = dat.files, value = TRUE)
archive_extract("H_COMEPHORE_202201.tar", files = dat.files, dir="temp")
tar("archive.tar", files = "temp") # write the precipitation files into a new archive
unlink("temp/*", recursive = TRUE)
archive_extract("archive.tar", dir="temp") # extract the files again
RR <- rast("temp/temp/2022/2022010101_RR.gtif") # import the same raster again using terra
plot(RR) # this works fine again
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。