使用R从.asc文件(ESRI ascii网格)中删除标头(6行)并导出 [英] use R to remove header (6 lines) from .asc file (ESRI ascii grid) and export

查看:188
本文介绍了使用R从.asc文件(ESRI ascii网格)中删除标头(6行)并导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有800多个.asc文件(ESRI ascii网格),每个文件的头由6行组成,然后用空格分隔栅格数据.这里以一个小文件为例.我是使用read.asciigrid(sp包)阅读的.

I have over 800 .asc files (ESRI ascii grids) that each have a header consisting of 6 lines, then the raster data separated by spaces. Here is a small file as an example. I read it in using read.asciigrid (sp package).

new("SpatialGridDataFrame" ,data = structure(list(mydata.asc = c(4,4,4,4,3,4,4,4,1,1,1,1,1,4,4,4,4,4,3,4 ,4,4,1,1,1,1,1,4,4,4,4,3,4, 4,4,1,1,1,1,1,4,4,4,4,4,4,4,4,1,1,1,1,1,4,4,4, 4,3,4,4,4,6,6,1,1,1,1,4,4,4,4,3,4,4,4,6,6,1,1,1,1, 4,4,4,4,4,4,4,4,6,6,1,1,1,4,4,4,4,4,4,4,4,6,6, 1,1,1,4,4,4,4,4,4,4,4,4,1,1,1,4,4,4,4,4,4,4,4, 4,4,4,6,6,6,4,4,4,4,4,4,4,4,4,4,4,6)).名称= "mydata.asc",row.names = c(NA, -143L),类="data.frame") ,grid = new("GridTopology" ,cellcentre.offset = c(394984.42630274,2671265.4912109) ,单元格大小= c(25,25) ,cells.dim = c(13L,11L)) ,bbox = structure(c(394971.92630274,2671252.9912109,395296.92630274, 2671527.9912109),.Dim = c(2L,2L),.Dimnames = list(NULL,c("min","max")))) ,proj4string = new("CRS" ,projargs = NA_character_))

new("SpatialGridDataFrame" , data = structure(list(mydata.asc = c(4, 4, 4, 4, 3, 4, 4, 4, 1, 1, 1, 1, 1, 4, 4, 4, 4, 3, 4, 4, 4, 1, 1, 1, 1, 1, 4, 4, 4, 4, 3, 4, 4, 4, 1, 1, 1, 1, 1, 4, 4, 4, 4, 3, 4, 4, 4, 6, 1, 1, 1, 1, 4, 4, 4, 4, 3, 4, 4, 4, 6, 1, 1, 1, 1, 4, 4, 4, 4, 3, 4, 4, 4, 6, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 1, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 1, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6)), .Names = "mydata.asc", row.names = c(NA, -143L), class = "data.frame") , grid = new("GridTopology" , cellcentre.offset = c(394984.42630274, 2671265.4912109) , cellsize = c(25, 25) , cells.dim = c(13L, 11L) ) , bbox = structure(c(394971.92630274, 2671252.9912109, 395296.92630274, 2671527.9912109), .Dim = c(2L, 2L), .Dimnames = list(NULL, c("min", "max"))) , proj4string = new("CRS" , projargs = NA_character_ ) )

如果使用文本编辑器查看文件,则文件的外观如下.

Here is what the file looks like if you view it with a text editor.

这是我想做的步骤

1)读入文件 2)删除前6行(标题) 3)将文件另存为.asc文件,文件名相同,但位置不同

1) read in file 2) remove first 6 lines (header) 3) save file back out as .asc file with the same filename but in a different location

当然,我想对800个文件执行此操作,但是如果我能弄清楚如何对一个文件执行此操作,则我应该能够编写一个函数来遍历所有文件.

Of course, I'd like to do this to 800 files, but if I can figure out how to do this for one file, I should be able to write a function to loop through all files.

感谢您的帮助.

-al

更新: 感谢@Luca Braglia,这是对我有用的最终代码.

UPDATE: This is the final code that worked for me, thanks to @Luca Braglia.

设置工作目录

setwd("c:/temp/hdr/ascii")
newdir <- "c:/temp/hdr/ascii_no_hdr/"

files <- dir(pattern="*.asc")

for (my.file in files){
  i <- read.table(my.file,skip=6,sep="")
  write.table(i,file=paste(newdir,my.file,sep=""),sep="",row.names=FALSE,col.names=FALSE)
}

我不想要列名和行名.一段非常简单有效的代码.

I didn't want the col and row names. A very simple and effective piece of code.

推荐答案

您可以列出所有文件,在for循环中将其全部读取(使用read.tableskip选项)

You can list all files, within a for loop read them all (using skip option of read.table)

## you are in the directory with your asc files
files <- dir(pattern="*.asc")

# loop
for (my.file in  files) {
     i <- read.table(my.file, skip = 6, sep = " ")
     # change names here if you don't want V1, V2 ...
     write.table(i, file = paste("new_dir", my.file, sep = "/"), 
                 sep = " ", row.names = FALSE)

}   

这篇关于使用R从.asc文件(ESRI ascii网格)中删除标头(6行)并导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆