使用ncdf4 :: ncvar_get时如何指定尺寸顺序? [英] How can I specify dimension order when using ncdf4::ncvar_get?
问题描述
在上一个问题之后(是否从netCDF中快速读取时间序列?)我已经将netCDF文件重新排列为提供快速的时间序列读取(github上的脚本脚本最终将被清理. .. ).
Following a previous question (Faster reading of time series from netCDF?) I have re-permuted my netCDF files to provide fast time-series reads (scripts on github to be cleaned up eventually ...).
简而言之,为了使读取更快,我将尺寸从lat, lon, time
重新排列为time, lat, lon
.现在,我现有的脚本中断了,因为它们假定对于'start'参数,尺寸遵循ncvar_get
的ncdf4文档,始终为lat, lon, time
:
In short, to make reads faster, I have rearranged the dimensions from lat, lon, time
to time, lat, lon
. Now, my existing scripts break because they assume that the dimensions will always be lat, lon, time
, following the ncdf4 documentation of ncvar_get
, for the 'start' argument:
订单为X-Y-Z-T(即时间维度为最后一个)
Order is X-Y-Z-T (i.e., the time dimension is last)
但是,事实并非如此.
此外,通过命令行netCDF实用程序ncdump -h
和R函数ncdf4::nc_open
列出的变量顺序存在相关的不一致.第一个表示维度按预期(纬度,经度,时间)顺序排列,而后者则按时间先于时间(时间,纬度,经度)看到维度.
Furthermore, there is a related inconsistency in the order of variables listed via the commandline netCDF utility ncdump -h
and the R function ncdf4::nc_open
. The first says that the dimensions are in the expected (lat, lon, time) order while the latter sees dimensions with time first (time, lat, lon).
作为一个最小的示例,下载文件 test.nc 并运行
For a minimal example, download the file test.nc and run
bash-$ ncdump -h .nc
bash-$ R
R> library(ncdf4)
R> print(nc_open("test.nc")
我想做的是从变量"lwdown"中获取记录5-15.
What I want to do is get records 5-15 from the variable "lwdown"
my.nc <- nc_open("test.nc")
但是这不起作用,因为R首先看到时间维度,所以我必须将脚本更改为
But this doesn't work, since R sees the time dimension first, so I must change my scripts to
ncvar_get(my.nc, "lwdown", start = c(5, 1, 1), count = c(10, 1, 1))
更新我的脚本和函数并不难,除了我希望能够从文件中读取数据而不受尺寸顺序的限制.
It wouldn't be so bad to update my scripts and functions, except that I want to be able to read data from files regardless of the dimension order.
除了可以通用化此功能以使其独立于尺寸顺序工作之外,还有其他方法吗?
Other than Is there a way to generalize this function so that it works independent of dimension order?
推荐答案
在问这个问题时,我想出了这个解决方案,尽管仍有改进的余地:
While asking the question, I figured out this solution, though there is still room for improvement:
我能得到的最接近的方法是打开文件并以这种方式查找订单:
The closest I can get is to open the file and find the order in this way:
my.nc$var$lwdown$dim[[1]]$name
[1] "time"
my.nc$var$lwdown$dim[[2]]$name
[1] "lon"
my.nc$var$lwdown$dim[[3]]$name
[1] "lat"
有点不满意,尽管它使我想到了以下解决方案:
which is a bit unsatisfying, although it led me to this solution:
如果我想从c(lat = 1, lon = 1, time = 5)
开始,但ncvar_get
需要任意顺序,我可以说"
If I want to start at c(lat = 1, lon = 1, time = 5)
, but the ncvar_get
expects an arbitrary order, I can say"
start <- c(lat = 1, lon = 1, time = 5)
count <- c(lat = 1, lon = 1, time = 10)
dim.order <- sapply(my.nc$var$lwdown$dim, function(x) x$name)
ncvar_get(my.nc, "lwdown", start = start[dim.order], count = count[dim.order])
这篇关于使用ncdf4 :: ncvar_get时如何指定尺寸顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!