1

我有一个输入文件,其模式如下:-前两行包含模型和月份和年份的参数:我只需要从这两行中获取月份和年份-下一个块包含实际数据:172 行和 256 列数据进行 N 次

链接到实际数据

另一个文件是 ArcGIS ascii 网格,前 6 行是 ArcGIS 参数和一个 172 行 x 256 列的块(区域内等于 1,其他地方为 -9999)

链接到 ArcGIS ascii 网格

我想做的是读取数据并根据掩码网格计算每个时间步的平均值。目前我能想到的唯一方法是使用 3 个嵌套循环

for (i in 1:N) {
   for (j in 1:172) {
      for (k in 1:252) {
           Read the data
           Do calculation
           Write the result out
      }
   }
{

有没有更好的方法来避免使用如此复杂的 FOR 循环?任何建议将不胜感激!

编辑:根据 agstudy 的建议和其他来源,我逐行读取我的数据并使用 strsplit 来获取值。我的最终代码如下

########################## Read the grid first ########################
con <- file(gisGrid) 
open(con);

# Read the first 6 lines
gisData <- readLines(textConnection(
'ncols         256
nrows         172
xllcorner     730000
yllcorner     227000
cellsize      1320
NODATA_value  -9999'),n=6)

# Extract value
gisPara <- matrix(unlist(strsplit(gisData,' +')), ncol=2, byrow=TRUE)
summary(gisPara)

# Read the mask grid:
gridValue <- read.table(file = gisGrid, header = FALSE, skip=6)
gridValueVec <- as.vector(as.matrix(gridValue))

close(con)

######################### Read the data file #########################
con <- file(dataFile) 
open(con);

# Define number of timesteps, nRow, nCol
nTime <- 3
nRow <- 172
nCol <- 256

# Read the whole file in
data.lines <- scan(con, what=character(), sep='\n')
data <- NULL
# Create 3D object to store data for each timestep
data$timestep <- list( rep( matrix(nrow=nRow, ncol=nCol), nTime ) )
data$month <- list( rep( nTime ) )
data$year <- list( rep( nTime ) )
data$multiply <- list( rep( nTime ) )

# Loop over all timestep
for (i in 1:nTime) {
  # Read the first 2 lines
  data.lines <- data.lines[-1] # remove line from the dataset

  data.line2 <- strsplit(data.lines[1],' ')
  data$month[[i]] <- data.line2[[1]][24]
  data$year[[i]] <- as.numeric(data.line2[[1]][25])
  data.lines <- data.lines[-1]

  dataRead <- matrix(nrow=nRow, ncol=nCol)
  for(j in 1:nRow) 
  {
    dataRead[j,] <- as.numeric(strsplit(data.lines[1],' ')[[1]])
    data.lines <- data.lines[-1]
  }         

  # Multiply data with the mask grid
  dataReadVector <- as.vector(dataRead)
  gridMultiplication <- ifelse(gridValueVec==-9999, NA,
                               dataReadVector * gridValueVec )

  data$multiply[[i]] <- mean(gridMultiplication, na.rm=TRUE)
  data$timestep[[i]] <- dataRead
}

close(con)
4

1 回答 1

0

对于第一个文件,您可以使用执行以下操作:

使用仅读取 2 行的日期解析readLines

dd <- readLines(
   textConnection('internal  1.0  (free)  -1                 Sep 1990'),n=2)
regmatches(dd,gregexpr('[A-Z][a-z]{2} [0-9]{4}$',dd))[[1]]
"Sep 1990" ## strsplit(...,' ') if you want month/and year separately

然后你使用类似的东西读取矩阵:

 read.table(...,skip=2)

对于第二个 File ,您可以使用`strsplit读取文件中前 6 行的参数:

dd <- readLines(textConnection('ncols         256
nrows         172
xllcorner     730000
yllcorner     227000
cellsize      1320
NODATA_value  -9999
'),n=6)
matrix(unlist(strsplit(dd,' +')),ncol=2,byrow=TRUE)

    [,1]           [,2]    
[1,] "ncols"        "256"   
[2,] "nrows"        "172"   
[3,] "xllcorner"    "730000"
[4,] "yllcorner"    "227000"
[5,] "cellsize"     "1320"  
[6,] "NODATA_value" "-9999" 
于 2013-06-30T03:29:38.360 回答