我有一个netcdf文件,其中包含两个维度(时间和netid)和一个变量(Qout)。
dimensions:
time = UNLIMITED ; // (12784 currently)
netid = 716862 ;
variables:
float Qout(time, netid)
现在,我正在尝试为所有716862 netid计算趋势并将文件保存到csv。我现在要做的是使用nco
提取单个文件 ncks - d netid,1 infile.nc outfile.nc
并使用以下代码计算趋势;
dir="C:/Doc/Downloads/area/flow/trend/"
files <- list.files(path = dir, pattern = ".*\\.nc$",
ignore.case = TRUE, full.names = TRUE)
ncdf_timeseries <- function(filename)
{
nc <- nc_open(filename)
rivid <- ncvar_get(nc, "rivid")
qout <- ncvar_get(nc, varid = "Qout")
start.date <- as.Date("1979-01-01", format = "%Y-%d-%m")
mdates <- seq(from = start.date, to = start.date + (length(qout) - 1), by = "days")
flow <- data.frame(ID = rep(rivid, length(qout)),
Date = mdates,
Flow = qout)
x <- ts(flow$Flow, start = 1979)
nc_close(nc)
return(x)
}
listOfDataFrames <- vector("list", length = length(files))
for (i in seq_along(files))
{
rivers <- ncdf_timeseries(filename = files[i])
tren=mk.test(rivers)
slp=sens.slope(rivers)
frt=cbind(filename, tren$p.value, tren$statistic ,slp$estimates)
frt2=as.data.frame(frt)
names(frt2)=c("name", "p_Value", "MK_Z", "Slope")
listOfDataFrames[[i]] <- frt2
}
mktest <- do.call("rbind", listOfDataFrames)
write.csv(mktest,"C:/Doc/Downloads/area/flow/trend/MK_test.csv")
这是可行的,但是由于存储限制,无法提取716862文件,这很耗时。
因此,可以将其写成一个循环,读取每个netid的Qout并执行mk.test和sens.slope并通过其netid将值保存在csv中。
最诚挚的问候,
所罗门