Считать данные сетки ascii в матричный формат

Я пытаюсь прочитать файл в формате сетки ASCII TOMS в R. Я смог прочитать его таким образом, чтобы он открывался в R. Однако, он открывается как линейная матрица. Краткое содержание файла доступно здесь:

[Link](http://www.temis.nl/docs/README_TOMSASCII.pdf)

Образец набора данных можно скачать здесь:

[Link](http://www.temis.nl/airpollution/no2col/no2monthscia.php?Year=2005&Month=04)

Набор данных за январь 2006 года, и я просто переименовал его для более легкого доступа, так как есть немало, с которыми мне нужно поработать. Я прочитал это с помощью:

CCC<-read.csv("no2_200601.asc",header=FALSE,skip=4,sep="\t")
dim(CCC)
[1] 52560    1

Как мне прочитать это в R, чтобы данные для каждой широты находились в одной строке? Я чувствую, что это поможет построить правильную структуру данных. Примечание: позвольте мне попробовать и просто, если я понимаю:
Это означает, что структура такова, что в одной строке указан заголовок, например, lat=-89,9, а следующие 144 строки с 20 элементами в каждой принадлежат строке lat=-89.9; так что моя проблема сейчас заключается в чтении всех этих элементов перед следующим "lat=..." в одну строку.

Кроме того, я просто попытался пройти через набор файлов, используя это:

NO2files<-list.files(pattern=".asc", full.names=TRUE)
f<-lapply(NO2files, function (x) readLines (x))

for (i in 1:length (NO2files)) {  
 function(x)
 i<-readLines(x)
pattern <- "[[:digit:]]+(?=\\sbins)"
m <- regexpr(pattern, i[3], perl=TRUE)
dim <- regmatches(i[3], m)
m <- regexpr(pattern, i[4], perl=TRUE)
dim[2] <- regmatches(i[4], m)

dim <- as.integer(dim)

pattern <- "(?<=undef=).*"
m <- regexpr(pattern, i[2], perl=TRUE)
na_string <- regmatches(i[2], m)

dat1 <- i[-(1:4)]
sep <- grepl("=", dat1, fixed=TRUE)
dat2a <- dat1[sep] 
dat2b <- dat1[!sep] 
dat2b <- lapply(dat2b, substring, 
            first=seq(1,nchar(dat2b[1]),4), 
            last=  seq(4,nchar(dat2b[1]),4))
dat2b <- unlist(dat2b)
dat2b <- as.numeric(dat2b)
dat2b[dat2b==as.numeric(na_string)] <- NA
dat2b <- matrix(dat2b, nrow=dim[2], byrow=TRUE)
dat2b <- dat2b[nrow(dat2b):1, ]
}

2 ответа

Решение

Не так элегантно, как Roland, и я не уверен, почему имеют разные значения - на самом деле я делаю спасибо за комментарий ниже (другой файл).

library(stringr)
library(plyr)
library(raster)

f <- readLines("totno2_200601.asc")

# how many lat/lon values
bins.lon <- as.numeric(str_match(f[3], "Longitudes *: *([0-9]+) bins")[2])
bins.lat <- as.numeric(str_match(f[4], "Latitudes *: *([0-9]+) bins")[2])

# number of characters that represent a value
num.width <- 4

# how many lines do we need to encode the longitude bins
bins.lon.lines <- as.integer(bins.lon / (80/num.width))

# where does the data start
curr.lat.line <- 5
curr.lat.bin <- 1

m <- matrix(nrow=bins.lat, ncol=bins.lon+1)

repeat {

  # get current latitude
  lat <- as.numeric(str_match(f[curr.lat.line], "lat=\ +([0-9\\.\\-]+)")[2])

  # show progress - not necessary
  cat(curr.lat.bin, lat); cat("\n")

  # get the values for the longitudes at current latitude
  vals <- paste(f[(curr.lat.line+1):(curr.lat.line+bins.lon.lines)], sep="", collapse="")

  # split them by 4 and assign to the proper entry
  m[curr.lat.bin, ] <- c(lat, as.numeric(laply(seq(1, nchar(vals), 4), function(i) substr(vals, i, i+3))))

  curr.lat.bin <- curr.lat.bin + 1
  curr.lat.line <- curr.lat.line + bins.lon.lines + 1

  if (curr.lat.bin > bins.lat) { break }

}

m <- m[nrow(m):1, ]

plot(raster(m))

сюжет

Поскольку вы добавили требование, чтобы это можно было использовать в цикле для чтения нескольких файлов:

library(stringr)
library(plyr)
library(raster)

# this is the function-ized version 

tomsToMatrix <- function(fname, verbose=FALSE) {

  f <- readLines(fname)

  bins.lon <- as.numeric(str_match(f[3], "Longitudes *: *([0-9]+) bins")[2])
  bins.lat <- as.numeric(str_match(f[4], "Latitudes *: *([0-9]+) bins")[2])

  num.width <- 4
  bins.lon.lines <- as.integer(bins.lon / (80/num.width))
  curr.lat.line <- 5
  curr.lat.bin <- 1

  m <- matrix(nrow=bins.lat, ncol=bins.lon+1)

  repeat {
    lat <- as.numeric(str_match(f[curr.lat.line], "lat=\ +([0-9\\.\\-]+)")[2])
    if (verbose) { cat(curr.lat.bin, lat); cat("\n") }
    vals <- paste(f[(curr.lat.line+1):(curr.lat.line+bins.lon.lines)], sep="", collapse="")
    m[curr.lat.bin, ] <- c(lat, as.numeric(laply(seq(1, nchar(vals), 4), function(i) substr(vals, i, i+3))))
    curr.lat.bin <- curr.lat.bin + 1
    curr.lat.line <- curr.lat.line + bins.lon.lines + 1 
    if (curr.lat.bin > bins.lat) { break } 
  }

  m <- m[nrow(m):1, ]

  return(m)

}

setwd("/data/toms") # whatever the source directory is for **your** files

t.files <- list.files("/data/toms")
t.files
[1] "totno2_200504.asc" "totno2_200505.asc" "totno2_200506.asc"

dat <- lapply(t.files, tomsToMatrix)

str(dat)
List of 3
$ : num [1:720, 1:1441] 89.9 89.6 89.4 89.1 88.9 ...
$ : num [1:720, 1:1441] 89.9 89.6 89.4 89.1 88.9 ...
$ : num [1:720, 1:1441] 89.9 89.6 89.4 89.1 88.9 ...

Если они нужны вам как именованные записи, добавить их не составит труда.

Вот начало:

dat <- readLines("totno2_200504.asc")

#parse dimensions
pattern <- "[[:digit:]]+(?=\\sbins)"
m <- regexpr(pattern, dat[3], perl=TRUE)
dim <- regmatches(dat[3], m)

m <- regexpr(pattern, dat[4], perl=TRUE)
dim[2] <- regmatches(dat[4], m)

dim <- as.integer(dim)

#parse NA string
pattern <- "(?<=undef=).*"
m <- regexpr(pattern, dat[2], perl=TRUE)
na_string <- regmatches(dat[2], m)

#parse data
dat1 <- dat[-(1:4)]
sep <- grepl("=", dat1, fixed=TRUE)
dat2a <- dat1[sep] #might be useful 
dat2b <- dat1[!sep] #the data
dat2b <- lapply(dat2b, substring, 
                first=seq(1,nchar(dat2b[1]),4), 
                last=  seq(4,nchar(dat2b[1]),4))
dat2b <- unlist(dat2b)
dat2b <- as.numeric(dat2b)
dat2b[dat2b==as.numeric(na_string)] <- NA
dat2b <- matrix(dat2b, nrow=dim[2], byrow=TRUE)
dat2b <- dat2b[nrow(dat2b):1, ] #flip in axis

library(raster)
plot(raster(dat2b))

введите описание изображения здесь

Другие вопросы по тегам