gpt4 book ai didi

r - 从较大的栅格堆栈创建栅格堆栈的许多子集

转载 作者:行者123 更新时间:2023-12-03 06:52:39 26 4
gpt4 key购买 nike

很难将其概念化。我目前有一个 netcdf 文件,其中包含 1979-2016 年的月度气候平均值。我想从这个较大的栅格堆栈中单独制作年度栅格堆栈(即,从 1 个栅格堆栈变为 38)。我这辈子都弄不明白。任何建议都会很棒!数据可以在这里找到:http://nimbus.cos.uidaho.edu/abatz/DATA/vpd19792016.nc

到目前为止,我已经开始的工作如下:

  library(raster)
library(lubridate)
library(rgdal)
library(tidyverse)
library(tools)

file = "../data/raw/climate/aet_19792016.nc"
file_split <- file %>%
basename %>%
strsplit(split = "_") %>%
unlist
var <- file_split[1]
year <- substr(file_split[2], start = 1, stop = 4)
endyear <- substr(file_split[2], start = 5, stop = 8)

start_date <- as.Date(paste(year, "01", "01", sep = "-"))
end_date <- as.Date(paste(ifelse(year == endyear, year, endyear), "12", "31", sep = "-"))
date_seq <- seq(start_date, end_date, by = "1 month")
month_seq <- month(date_seq)

nc <- nc_open(file)
nc_att <- attributes(nc$var)$names
ncvar <- ncvar_get(nc, nc_att)
tvar <- aperm(ncvar, c(3,2,1))
proj <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0 "

raster <- brick(tvar, crs= proj)
extent(raster) <- c(-124.793, -67.043, 25.04186, 49.41686)
names(raster) <- paste(var, year(date_seq),
unique(month(date_seq, label = TRUE)),
sep = "_")
unstack(raster)

提前感谢您的任何建议!

最佳答案

首先,您必须根据 Yearsmonths 正确命名栅格堆栈:

library(ncdf4)
library(raster)

nc <- nc_open(file)
nc_att <- attributes(nc$var)$names
ncvar <- ncvar_get(nc, nc_att)
tvar <- aperm(ncvar, c(3,2,1))
proj <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0 "
r <- brick(tvar, crs= proj)
extent(r) <- c(-124.793, -67.043, 25.04186, 49.41686)
# name raster brick based on year and month
dates <- format(seq(as.Date(paste(1979,'/1/1',sep='')), as.Date(paste(2016,'/12/31', sep="")), by='month'), '%Y%m')
names(r) <- dates
r_sub <- subset(r, grep("X1982", names(r))) # subset based on year

关于r - 从较大的栅格堆栈创建栅格堆栈的许多子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46394844/

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