2017-03-23 62 views
1

柵格軟件包的真正簡單的問題,也使用ncdf4加載ECMWF Era-Interim Netcdf文件。NetCDF to Raster Brick「無法找到'ncdf4'的函數'brick'的繼承方法」

簡單地這樣做:

a <- nc_open("SSTs.nc") 
B <- brick(a, varname="sst") 

返回此:

Error in (function (classes, fdef, mtable) : 
    unable to find an inherited method for function ‘brick’ for signature ‘"ncdf4"’ 

的文件只是在全球各地SST數據,1個月(Jan2016)。當我將它轉換成一個數組時(即提取維數/變量,並將時間轉換爲UTC,將其轉換爲數組),我沒有得到相同的錯誤,但是柵格包說它直接支持.nc文件因爲它們與cf-1兼容,這是Era-Interim .nc的)

任何幫助非常感謝,試圖用很多Netcdf文件(非時代臨時)。

+0

你能給樣本數據集,也可以嘗試使用'stack'或使用以前的'ncdf package' –

+0

同樣的問題[讀的NetCDF子類別,並轉換爲網格] http://stackoverflow.com/questions/33784940/read-netcdf-sub-categories-and-convert-to-grid –

+0

感謝您的答覆。使用堆棧返回此: data.frame中的錯誤(值= unlist(unname(x)),ind = factor(rep.int(names(x),: 參數意味着行數不同:1654,16 示例數據集是這樣的:https://drive.google.com/open?id=0Bz0W7Ut_SNfjcjg1ODVrc2FhN2s – Ndharwood

回答

0

謝謝Renaud Lancelot,誰給出了明確的源代碼。我已經修改了他的代碼,以配合您的數據

# load package 
library(sp) 
library(raster) 
library(ncdf4) 

# read ncdf file 
nc<-nc_open('D:/SSTs.nc') 

# extract variable name, size and dimension 
v <- nc$var[[1]] 
size <- v$varsize 
dims <- v$ndims 
nt <- size[dims]    # length of time dimension 
lat <- nc$dim$latitude$vals # latitude position 
lon <- nc$dim$longitude$vals # longitude position 

# read sst variable 
r<-list() 
for (i in 1:nt) { 
    start <- rep(1,dims)  # begin with start=(1,1,...,1) 
    start[dims] <- i    # change to start=(1,1,...,i) to read timestep i 
    count <- size    # begin with count=(nx,ny,...,nt), reads entire var 
    count[dims] <- 1    # change to count=(nx,ny,...,1) to read 1 tstep 

    dt<-ncvar_get(nc, varid = 'sst', start = start, count = count) 

    # convert to raster 
    r[i]<-raster(dt) 
} 

# create layer stack with time dimension 
r<-stack(r) 

# transpose the raster to have correct orientation 
rt<-t(r) 
extent(rt)<-extent(c(range(lon), range(lat))) 

# plot the result 
spplot(rt) 
+0

謝謝Eko - 這個應該沒有問題特別感謝修正方向 我可能會要求你澄清一下For循環的步驟嗎?這會有幫助,所以我可以開始在評論中暗示它。 – Ndharwood

+0

關於你的ncdf有3個尺寸(經度,緯度和時間),For循環將讀取時間序列數據(取決於時間維度)並將值賦給r [i],結果將被重新計算ck獲取光柵文件中的所有數據 –

+0

嗨Eko,再次感謝 - 你能告訴我For循環內的第2和第4行是做什麼的嗎?第一行復制時間步長1 BY變量中的維數,但爲什麼第二行子集變暗 - 即開始[變暗]? 同樣,第三個是告訴ncvar_get函數來計算變量的大小,但是第四行(count [dims])是做什麼的? 非常感謝 – Ndharwood