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