[問題] (更)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=''))
[環境敘述]:
https://imgur.com/nVXlvbb
[關鍵字]:
dim 遺失值

Links booklink

Contact Us: admin [ a t ] ucptt.com