堆叠从R中下载的PRISM下载的气候数据

时间:2014-12-11 21:22:09

标签: r

我下载棱镜气候数据已有几年了,我希望能得到一些关于如何为每一年堆叠这些数据的建议。所以在下载并解压后,每个月的数据都在不同的文件夹中。我需要堆叠每年的月度数据。我是R的新人,我真的很感激,如果我能得到一些关于如何实现这一目标的建议。文件夹名称如下: PRISM_ppt_stable_4kmM2_201312_bil PRISM_ppt_stable_4kmM2_201311_bil PRISM_ppt_stable_4kmM2_201310_bil等等

4 个答案:

答案 0 :(得分:2)

以下是@Amstell脚本的变体。它不使用单独的RasterLayer对象,因此更容易维护,更通用,因为s可以有任意数量的层。我想我会在循环之外运行一次list.files,然后在循环内部使用grep来获取子集。

    library(raster)
    setwd("./Prism Weather Data All/1895-2014_BIL/ppt")   
    years <- 1900:1900
    for (i in years) {
      filenames <- list.files(pattern=paste(".*_",i,".*\\.bil$", sep = ""))
      s <- stack(filenames)
      y <- data.frame(rasterToPoints(s))
      colnames(y) <- c("lon", "lat", month.abb)
      y$year <- i
      y$gridNumber <- cellFromXY(s, y[, 1:2])
      # write.csv( ) ?
    }

另请注意,.../毫无意义。对当前目录使用./(或不执行任何操作),对父目录使用../(依此类推../../

答案 1 :(得分:0)

数据所在的文件格式是什么? 是csv还是一些文字? 如果是csv你可以在R中使用read.csv函数,它将为你创建一个整洁的data.frame对象。

您可以为每年创建一些迭代器来读取每个文件夹中的数据,例如:

year = 2010:2014
months = 1:12
folder_names = sapply(year, function(y) { sapply(month, function(m){ 
                    paste(
                        paste(
                              paste("PRISM_ppt_stable_4kmM2_", y, sep=""), 
                               m, sep=""),
                          "_bil", sep="") }) })

这样,您就可以生成所有目录名称。

现在您可以从它们执行读取,但为此我们需要知道您保存数据的文件。

答案 2 :(得分:0)

我已经从这个网站获得了很多帮助来处理PRISM数据,并且正在向您传递一些代码以使其更容易。以下代码将从光栅文件中提取信息,并将其放在一个漂亮的data.frame中,以供您每年使用。

设置

在此之前设置一些内容非常重要。

  1. 将您需要的所有年份的所有* .bil和* .hdr文件解压缩到特定目录
  2. 使用* .bil和* .hdr文件将
  3. setwd()分配给该目录 如果您想将年份导出到* .csv文件,
  4. path <- ".../"包含导出路径 - 如果您愿意
  5. startingyear和endyear分配给您想要提取的年份
  6. 使用* .bil和* .hdr文件将
  7. filenames <- list.files(path = ".../"分配给目录。这应与setwd()
  8. 相同

    代码

    library(plyr)
    library(dplyr)
    library(raster)
    library(rgdal)
    library(tidyr)
    library(reshape2)
    library(data.table)
    
    ### Precipitation (ppt) ###
    
    # !!!!!  Set to directory with *.hdr and *.bil only
    setwd(".../Prism Weather Data All/1895-2014_BIL/ppt/")   
    
    # Path to write out (if necessary)
    path <- ".../Prism Weather Data All/csv/ppt/") 
    
    # Set years to process
    startingyear <- 1900       
    endingyear <- 1900
    total <- startingyear:endingyear
    
    for (i in total) {
      year <- as.character(i)
    
      #  Make sure to set the correct path where *.hdr and *.bil are
      filenames <- list.files(path = ".../Prism Weather Data All/1895-2014_BIL/ppt/", 
                              pattern = paste(".*_", year, ".*\\.bil", sep = ""))
    
      # Assign raster file to object
      r1 = raster(filenames[1])
      r2 = raster(filenames[2])
      r3 = raster(filenames[3])
      r4 = raster(filenames[4])
      r5 = raster(filenames[5])
      r6 = raster(filenames[6])
      r7 = raster(filenames[7])
      r8 = raster(filenames[8])
      r9 = raster(filenames[9])
      r10 = raster(filenames[10])
      r11 = raster(filenames[11])
      r12 = raster(filenames[12])
    
      # Assign values from raster file to month object
      jan <- as.data.frame(r1, na.rm = TRUE)
      feb <- as.data.frame(r2, na.rm = TRUE)
      mar <- as.data.frame(r3, na.rm = TRUE)
      apr <- as.data.frame(r4, na.rm = TRUE)
      may <- as.data.frame(r5, na.rm = TRUE)
      jun <- as.data.frame(r6, na.rm = TRUE)
      jul <- as.data.frame(r7, na.rm = TRUE)
      aug <- as.data.frame(r8, na.rm = TRUE)
      sep <- as.data.frame(r9, na.rm = TRUE)
      oct <- as.data.frame(r10, na.rm = TRUE)
      nov <- as.data.frame(r11, na.rm = TRUE)
      dec <- as.data.frame(r12, na.rm = TRUE)
    
      # Bind all gridnumbers with monthly data to object
      jan <- cbind(gridNumber = rownames(jan), jan, stringsAsFactors = FALSE)
      feb <- cbind(gridNumber = rownames(feb), feb, stringsAsFactors = FALSE)
      mar <- cbind(gridNumber = rownames(mar), mar, stringsAsFactors = FALSE)
      apr <- cbind(gridNumber = rownames(apr), apr, stringsAsFactors = FALSE)
      may <- cbind(gridNumber = rownames(may), may, stringsAsFactors = FALSE)
      jun <- cbind(gridNumber = rownames(jun), jun, stringsAsFactors = FALSE)
      jul <- cbind(gridNumber = rownames(jul), jul, stringsAsFactors = FALSE)
      aug <- cbind(gridNumber = rownames(aug), aug, stringsAsFactors = FALSE)
      sep <- cbind(gridNumber = rownames(sep), sep, stringsAsFactors = FALSE)
      oct <- cbind(gridNumber = rownames(oct), oct, stringsAsFactors = FALSE)
      nov <- cbind(gridNumber = rownames(nov), nov, stringsAsFactors = FALSE)
      dec <- cbind(gridNumber = rownames(dec), dec, stringsAsFactors = FALSE)
    
      # Merge Monthly data
      month_list <- lapply(list(jan,feb,mar, apr, may, jun, jul, aug, sep, oct, nov, dec),setDT)
      for(j in seq_along(month_list)){
      set(month_list[[j]],j="ID",value = names(month_list[[j]])[2])
      setnames(month_list[[j]],  names(month_list[[j]])[2], "value")
      }
    
      # Faster processing of aggregated data
      long_data <- rbindlist(month_list)
      wide <- dcast.data.table(long_data, gridNumber~ID, value = 'value')
      wide$year <- year
      colnames(wide) <- c("gridNumber", "jan","feb","mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec", "year")
    }
    

    结果

    enter image description here

答案 3 :(得分:-1)

获取类似

的文件名
.querySelectorAll()

也许您需要订购它们。然后做

function removeAttr() {
    var i, buttons = document.querySelectorAll('.choice');
    for (var i in Object.keys(buttons)) {
        buttons[i].removeAttribute('class');
    }
}

然后对堆栈做任何你想做的事情(你没有指定)。要获得值和单元格中心矩阵,您可以

 f <- list.files(pattern='.bil$', recursive=TRUE, full=TRUE)