[问题] (更)the dim contain missing value

楼主: AndrewShi (没有妳的我)   2018-08-03 17:09:14
[问题类型]:
程式咨询(我想用R 做某件事情,但是我不知道要怎么用R 写出来)
[软件熟悉度]:
入门(写过其他程式,只是对语法不熟悉)
[问题叙述]:
继上篇发文一样想请问当出现the dims contain missing values这个错误讯息时该如何
解决??而继上篇发文的最后的推文后,我试了把两年的资料读进去去跑,但还是出现同样
的错误讯息。
因截取问题部分的程式码会跑出不同的错误讯息,且错误讯息追回源头还是需要放资料(
档案)进去,所以我就把完整的程式码贴上来了(当然不需要的部分还是有删掉),我想要
解决的问题就是能让这一完整的程式码跑完都不出错并输出结果(因为这一完整的程式码
是别人写好的一个package(Climdown,公开的))。
而需要放入的资料(档案)和其他程式码我放到下面这个google云端硬盘的共用资料夹里(
如无法进入资料夹或下载档案烦请推文告知),谢谢。
https://drive.google.com/drive/folders/1aI7SnSxJzIIkN9H9QjL8vMv3wF-WSpv5?usp=
sharing
其中有一段程式码会跑需要大概半小时,先行告知。
(ps:如需要Climdown这个package的GitHub网址可在推文告知,可再贴上来)。
希望有大大愿意帮忙或是提点我可能是哪里出了问题,我在程式这块目前还是新手,所以
抱歉有时候会不太懂你们说的意思,还烦请你们解释的详细(白话)一点,真的非常谢谢你
们。
[程式范例]:
# Read fine-scale grid and spatially aggregate to GCM grid
rm(list=ls())
library(ncdf4)
library(RNetCDF)
library(doParallel)
library(PCICt)
source("C:\\Users\\TOM\\Desktop\\R(code)\\降尺度\\netcdf.calendar.R")
config <- commandArgs(trailingOnly=TRUE)
if(length(config)==0) config <- "C:\\Users\\TOM\\Desktop\\R(code)\\降尺度
\\BCSD.config.R"
print(readLines(config))
source(config)
mc.cores<-4
registerDoParallel(mc.cores,cores=NULL)
nc.obs.file<-"C:\\Users\\TOM\\Desktop\\R(数据库)\\TRMM资料\\trmm_1998.nc"
nc.obs <- nc_open(nc.obs.file)
pr.nc.file<-"C:\\Users\\TOM\\Desktop\\R(数据库)\\降尺度资料
\\pr_Amon_CCSM4_historical_e1i1p1_185001-200512+rcp45_r2i1p1_200601-210012.nc"
nc.gcm <- nc_open(pr.nc.file)
# Read fine-scale and GCM grid dimensions
obs.lon <- ncvar_get(nc.obs,"longitude")
obs.lat <- ncvar_get(nc.obs,"latitude")
obs.time <- ncvar_get(nc.obs,"time")
n.lon <- length(obs.lon)
n.lat <- length(obs.lat)
obs.lats <- matrix(obs.lat, nrow=n.lon, ncol=n.lat, byrow=TRUE)
obs.lons <- matrix(obs.lon, nrow=n.lon, ncol=n.lat)
#obs.time <- netcdf.calendar(nc.obs)
obs.time <- matrix(seq(as.Date("1998-01-01"), as.Date("1998-12-31"),1))
gcm.lon <- ncvar_get(nc.gcm, 'lon')-360
gcm.lat <- ncvar_get(nc.gcm, 'lat')
gcm.lats <- matrix(gcm.lat, ncol=length(gcm.lat), nrow=length(gcm.lon),
byrow=TRUE)
gcm.lons <- matrix(gcm.lon, ncol=length(gcm.lat), nrow=length(gcm.lon))
gcm.lons.lats <- cbind(c(gcm.lons), c(gcm.lats))
gcm.time <- netcdf.calendar(nc.gcm)
nc_close(nc.gcm)
# Figure out which GCM grid boxes are associated with each fine-scale grid
point
# Confine search to 15 deg. x 15 deg. neighbourhood
dxy <- 15
mdist <- function(x, y)
apply(abs(sweep(data.matrix(y), 2, data.matrix(x), '-')), 1, sum)
nn <- foreach(i = seq_along(obs.lons)) %dopar% {
if((i %% 500)==0) cat(i, '')
gcm.lims <- ((gcm.lons.lats[,1] >= (obs.lons[i]-dxy)) &
(gcm.lons.lats[,1] <= (obs.lons[i]+dxy))) &
((gcm.lons.lats[,2] >= (obs.lats[i]-dxy)) &
(gcm.lons.lats[,2] <= (obs.lats[i]+dxy)))
gcm.lims <- which(gcm.lims)
nn.min <- which.min(mdist(c(obs.lons[i], obs.lats[i]),
gcm.lons.lats[gcm.lims,]))
gcm.lims[nn.min]
}
nn <- unlist(nn)
gridpoints <- sort(unique(nn))
cat('\n')
# Spatially aggregate the fine-scale data to the GCM grid
pr.aggregate <- matrix(NA, nrow=nrow(obs.time), ncol=length(gcm.lons))
i.starts <- sapply(split(seq_along(obs.time[,1]), obs.time[,1]), min)
i.lengths <- sapply(split(seq_along(obs.time[,1]), obs.time[,1]), length)
for(i in seq_along(i.starts)){
cat(obs.time[i.starts[i],], '\n')
pr.obs <- ncvar_get(nc.obs, varid='r', start=c(1, 1, i.starts[i]),
count=c(n.lon, n.lat, i.lengths[i]))
dim(pr.obs) <- c(prod(dim(pr.obs)[1:2]), dim(pr.obs)[3])
pr.agg <- matrix(NA, nrow=i.lengths[i],ncol=length(gcm.lons))
all.agg <- foreach(j=1:length(gridpoints)) %dopar% {
point <- gridpoints[j]
cbind(apply(pr.obs[nn==point,], 2, mean, trim=0.1, na.rm=TRUE)
)
}
all.agg <- do.call(cbind, all.agg)
pr.all.agg <- all.agg[,c(TRUE, FALSE, FALSE, FALSE)]
pr.agg[,gridpoints] <- pr.all.agg
pr.agg[is.nan(pr.agg)] <- NA
pr.aggregate[i.starts[i]:(i.starts[i]+i.lengths[i]-1),] <- pr.agg
}
nc_close(nc.obs)
save(gcm.lons, file=paste(output.dir, 'gcm.lons', output.suffix,
'.RData', sep=''))
save(gcm.lats, file=paste(output.dir, 'gcm.lats', output.suffix,
'.RData', sep=''))
save(gcm.time, file=paste(output.dir, 'gcm.time', output.suffix,
'.RData', sep=''))
save(obs.lons, file=paste(output.dir, 'obs.lons', output.suffix,
'.RData', sep=''))
save(obs.lats, file=paste(output.dir, 'obs.lats', output.suffix,
'.RData', sep=''))
save(obs.time, file=paste(output.dir, 'obs.time', output.suffix,
'.RData', sep=''))
save(pr.aggregate, file=paste(output.dir, 'pr.aggregate', output.suffix,
'.RData', sep=''))
[环境叙述]:

[关键字]:
dim 遗失值

Links booklink

Contact Us: admin [ a t ] ucptt.com