从多变量netCDF文件创建栅格砖列表

时间:2014-12-01 01:18:42

标签: r raster netcdf

我一直在使用RCP(代表性浓度路径)空间数据。它是netCDF格式的一个很好的网格化数据集。如何获得砖块列表,其中每个元素代表一个多变量netCDF文件中的一个变量(通过变量我不是指lat,lon,time,depth ......等)。这就是我试图做的事情。我无法发布数据示例,但如果您想查看数据,我已将下面的脚本设置为可重现。显然欢迎提出问题......我可能没有顺利地表达与代码相关的语言。干杯。

A:包裹要求

    library(sp)
    library(maptools)
    library(raster)
    library(ncdf)
    library(rgdal)
    library(rasterVis)
    library(latticeExtra)

B:收集数据并查看netCDF文件结构

    td <- tempdir()
    tf <- tempfile(pattern = "fileZ")

    download.file("http://tntcat.iiasa.ac.at:8787/RcpDb/download/R85_NOX.zip", tf , mode = 'wb' )
    nc <- unzip( tf , exdir = td )
    list.files(td)

## Take a look at the netCDF file structure, beyond this I don't use the ncdf package directly

    ncFile <- open.ncdf(nc)
    print(ncFile) 
    vars <- names(ncFile$var)[1:12] # I'll try to use these variable names later to make a list of bricks

C:为一个变量创建栅格砖。级别对应年份

    r85NOXene <- brick(nc, lvar = 3, varname = "emiss_ene")
    NAvalue(r85NOXene) <- 0
    dim(r85NOXene) # [1] 360 720  12

D:面部名称

    data(wrld_simpl) # in maptools
    worldPolys <- SpatialPolygons(wrld_simpl@polygons)
    cTheme <- rasterTheme(region = rev(heat.colors(20)))
    levelplot(r85NOXene,layers = 4,zscaleLog = 10,main = "2020 NOx Emissions From Power Plants",
          margin = FALSE, par.settings = cTheme) + layer(sp.polygons(worldPolys))

Global NOx Emissions

E:每年为所有网格单元汇总一个变量“emis_ene”,我想对我正在使用的netCDF文件的每个变量执行此操作。

    gVals <- getValues(r85NOXene)
    dim(gVals)

    r85NOXeneA <- sapply(1:12,function(x){ mat <- matrix(gVals[,x],nrow=360)
                  matfun <- sum(mat, na.rm = TRUE) # Other conversions are needed, but not for the question 
              return(matfun)
})

F:另一个见面和问候。看看E的外观

    library(ggplot2) # loaded here because of masking issues with latticeExtra
    years <- c(2000,2005,seq(2010,2100,by=10))
    usNOxDat <- data.frame(years=years,NOx=r85NOXeneA)
    ggplot(data=usNOxDat,aes(x=years,y=(NOx))) + geom_line() # names to faces again
    detach(package:ggplot2, unload=TRUE)

G:尝试创建砖块列表。在C部分

中创建的对象列表
    brickLst <- lapply(1:12,function(x){ tmpBrk <- brick(nc, lvar = 3, varname = vars[x])
                                         NAvalue(tmpBrk) <- 0
                                         return(tmpBrk)

    # I thought a list of bricks would be a good structure to do (E) for each netCDF variable.
    # This doesn't break but, returns all variables in each element of the list. 
    # I want one variable in each element of the list.
    # with brick() you can ask for one variable from a netCDF file as I did in (C)
    # Why can't I loop through the variable names and return on variable for each list element.
})

H:摆脱你可能已下载的垃圾......抱歉

    file.remove(dir(td, pattern = "^fileZ",full.names = TRUE))
    file.remove(dir(td, pattern = "^R85",full.names = TRUE))
    close(ncFile)

1 个答案:

答案 0 :(得分:5)

您可以使用cellStats简化您的(E)步骤。

foo <- function(x){
    b <- brick(nc, lvar = 3, varname = x)
    NAvalue(b) <- 0
    cellStats(b, 'sum')
}

sumLayers <- sapply(vars, foo)

sumLayers是您正在寻找的结果,如果我正确理解您的问题。

此外,您可以使用zoo包,因为您正在处理时间序列。

library(zoo)
tt <- getZ(r85NOXene)
z <- zoo(sumLayers, tt)

xyplot(z)

time series