gpt4 book ai didi

r - 从多变量 netCDF 文件创建光栅砖列表

转载 作者:行者123 更新时间:2023-12-04 10:55:25 31 4
gpt4 key购买 nike

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

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)

最佳答案

您的 (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)

time series

关于r - 从多变量 netCDF 文件创建光栅砖列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27220470/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com