如何在 R 中创建一个采用多个栅格并将它们马赛克的循环?并用特定名称保存它们

发布于 2025-01-10 00:08:56 字数 1233 浏览 2 评论 0原文

我有大约 2000 个世界不同地区的光栅文件。其中一些来自同一天,另一些来自不同日期。我想马赛克/合并同一天的所有文件,以按天创建“图片”(光栅)。

所有文件的名称都以“MOD13A3.A2000”开头,然后是捕获图像的年份,然后是其他不重要的内容。我想做的就是获取所有以相同名称开头的文件并将它们合并。

例如,我会在这里获取前 4 个文件,因为它们以 MOD13A3.A2000032... 开头,将它们合并并只有一个名为 MOD13A3.A2000032 的文件。然后获取其他 4 个以下文件,因为它们以 MOD13A3.A2000061... 开头,将它们合并并得到另一个名为 MOD13A3.A2000061 的文件。

MOD13A3.A2000032.h12v13.006.2015138123528 // MOD13A3.A2000

032.h13v13.006.2015138123527 //

MOD13A3.A2000032.h13v14.006.2015138123528 // MOD13A3.A2000

032.h14v14.006.2015138123526 //

MOD13A3.A2000061.h12v13.006.2015136111214 //

MOD13A3.A2000061.h13v13.006.2015136111225 //

MOD13A3.A2000061.h13v14.006.2015136111218 //

MOD13A3.A2000061.h14v14.006.2015136111220 //

.. 我一直在研究其他论坛,

但因为我完全R 编程新手我不太理解发布的代码。我曾考虑过创建一个 for 循环来更改文件名的开头,例如 "MOD13A3.A2000"+i 但我不明白它的语法。我也无法找到清晰的示例来说明如何像这样更改文件名。

如果有人可以解释代码,我将非常感激!

I have around of 2000 raster files of different parts of the wolrd. Some of them are from the same day, and other are from different days. I want to mosaic/merge all the files that are from the same day to create "pictures" (rasters) by day.

All the names of the files begins with "MOD13A3.A2000" and then the day of the year the image was captured, and then other nonimportant stuff. The thing I want to do is to take all the files that start with the same name and merge them.

For example, I would take the first 4 files here, since they begin with MOD13A3.A2000032..., merge them and have just one file called MOD13A3.A2000032. And then take the other 4 following files, since they begin with MOD13A3.A2000061..., merge them and have another file called MOD13A3.A2000061.

MOD13A3.A2000032.h12v13.006.2015138123528 //

MOD13A3.A2000032.h13v13.006.2015138123527 //

MOD13A3.A2000032.h13v14.006.2015138123528 //

MOD13A3.A2000032.h14v14.006.2015138123526 //

MOD13A3.A2000061.h12v13.006.2015136111214 //

MOD13A3.A2000061.h13v13.006.2015136111225 //

MOD13A3.A2000061.h13v14.006.2015136111218 //

MOD13A3.A2000061.h14v14.006.2015136111220 //

...

I have been looking into other forums but since I am completely new to R programming I don't understand too much of the codes that are posted. I have thought about making a for cycle that changes the beginning of the file's name, like "MOD13A3.A2000"+i but I don't understand the sintaxis of it. I have not been able to find clear examples that show how to change a file's name like this either.

If someone could EXPLAIN a code, I would be SO GRATEFUL!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘寄 2025-01-17 00:08:56

我将从 terra 包开始:

library(terra)

r <- list.files(path = "rasters", pattern = "MOD13A3.A2000061.*", full.names = TRUE)
r <- lapply(r, rast)
r <- do.call(merge, r)

第一行创建具有特定模式的光栅文件列表,第二行对它们应用 rast() 函数,第三行进行 merge.

填充 free 为文件名中的模式创建一个循环:list.files 读取文件,然后获取文件名的 substr() 并查找所有 unique() 字符串。

I would start with terra package:

library(terra)

r <- list.files(path = "rasters", pattern = "MOD13A3.A2000061.*", full.names = TRUE)
r <- lapply(r, rast)
r <- do.call(merge, r)

First line creates list of raster files with specific pattern, 2nd applies rast() function to them, and 3rd do merge.

Fill free to create a loop for patterns in your filenames: list.files reads the files, then take a substr() of filenames and find all unique() strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文