簡體   English   中英

R - 將 ncdf4 文件的所有變量提取到單獨的變量名中

[英]R - Extracting all variables of ncdf4 file into separate variable names

我正在打開一個 .netcdf 文件,並希望使用非重復方法將所有變量提取到它們自己的變量名中。

目前我可以使用以下方法做到這一點

#route of file we want to open
fn <- "grid_T_19800105.nc"

#opens netCDF file
nc <- nc_open(fn)

#Extracts latitude and longitude matrices into variables
nav_lat <- ncvar_get(nc,"nav_lat")
nav_long <- ncvar_get(nc,"nav_lon")

#Extracts depth levels
depth <- ncvar_get(nc,"deptht")

#Extracts Temperature
votemper <- ncvar_get(nc,"votemper")

#Extracts Salinity
vosaline <- ncvar_get(nc,"vosaline")

#Extracts sea surface height
sossheig <- ncvar_get(nc,"sossheig")

#Extracts ice fraction
soicecov <- ncvar_get(nc,"soicecov")

#Close ncdf file to avoid memory loss
nc_close(nc)

但是似乎有一種更快的方法可以做到這一點。 目前我正在嘗試

#route of file we want to open
fn <- "grid_T_19800105.nc"

#opens netCDF file
nc <- nc_open(fn)

variables <- names(nc$var)

apply(variables,ncvar_get)

但這會返回錯誤

match.fun(FUN) 錯誤:缺少參數“FUN”,沒有默認值

一種可能的解決方案是使用 for 循環:

for(i in 1:length(variables)) {
    assign(variables[i], ncvar_get(nc, variables[i]))
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM