使用R扩展netCDF文件中的尺寸 [英] Extend dimensions in netCDF file using R

查看:93
本文介绍了使用R扩展netCDF文件中的尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用R编写一个具有无限"尺寸的netCDF文件,以后可以对其进行扩展.

I would like to write a netCDF file using R with 'unlimited' dimensions that I can later extend.

这是我尝试过的:

library(ncdf4)

## define lat, lon time dimensions
lat <- ncdim_def("latitude", "degrees_east", vals =  44.0, unlim = TRUE) 
lon <- ncdim_def("longitude", "degrees_north", vals = -88.5, unlim = TRUE)
time <- ncdim_def("time", "days since 0000-01-01", 1:1000)

## define data with these dimensions
x <- ncvar_def("myvar", units = "m2", dim = list(lat, lon, time))

## create, write to, close nc file
nc <- nc_create(filename = "tmp.nc", vars = list(x))

ncvar_put(nc = nc, varid = x, vals = 1:1000)
nc_close(nc = nc)

我想在不同的纬度和经度处添加一个新向量

## reopen existing file
nc <- nc_open("tmp.nc", write = TRUE)

## define new lat, lon dimensions (keep time dim from above)
lat2 <- ncdim_def("latitude", "degrees_east", vals =  44.5, unlim = TRUE) 
lon2 <- ncdim_def("longitude", "degrees_north", vals = -89.0, unlim = TRUE)

## define, write new dataset at new lat lon coordinates
x2 <- ncvar_def("myvar", units = "m2", dim = list(lat2, lon2, time))
ncvar_put(nc = nc, varid = x2, vals = 11:1011)

我希望找到两个不同的纬度和经度

ncvar_get(nc, 'latitude')
ncvar_get(nc, 'longitude')

ncvar_get(nc, 'myvar')

这些表明文件是使用第一组lat/lon和'myvar'值编写的,但未附加新的值集.

These show that the file was written using the first set of lat/lon and 'myvar' values, but was not appended with the new set of values.

我知道netCDF-4的功能是具有多个无限制的尺寸并将其添加到其中.如何在R中执行此操作?

I know that the ability to have multiple unlimited dimensions, and to add to them, is a feature of netCDF-4. How can I do this in R?

我认识到我必须将维度定义"与其他概念混淆.但是我有点迷茫.

I recognize that I must be confusing the 'dimension definition' with some other concept. But I am a bit lost.

推荐答案

是的,我认为您正在混淆尺寸定义"和维度变量中的实际数据.

Yes, I think you are confusing the 'dimension definition' and the actual data within the dimension variable.

如果您运行第一个代码段,然后使用ncdump转储NetCDF文件,则会看到:

If you run your first snippet of code and then dump the NetCDF file using ncdump, you'll see:

netcdf tmp {
dimensions:
        latitude = UNLIMITED ; // (1 currently)
        longitude = UNLIMITED ; // (1 currently)
        time = 1000 ;
variables:
        double latitude(latitude) ;
                latitude:units = "degrees_east" ;
                latitude:long_name = "latitude" ;
        double longitude(longitude) ;
                longitude:units = "degrees_north" ;
                longitude:long_name = "longitude" ;
        int time(time) ;
                time:units = "days since 0000-01-01" ;
                time:long_name = "time" ;
        float myvar(time, longitude, latitude) ;
                myvar:units = "m2" ;
data:

 latitude = 44 ;

 longitude = -88.5 ;

 time = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
    20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
    ...
    990, 991, 992, 993, 994, 995, 996, 997, 998, 999, 1000 ;

 myvar =
  {{1}},
  {{2}},
  {{3}},
  ...
  {{1000}} ;
} 

尺寸表示latitudelongitude是无限的,而time尺寸自0000-01-01起固定为1000点/天.这正是您指定的,这很好.

The dimensions are saying latitude and longitude are unlimited while the time dimension is fixed at 1000 points/days since 0000-01-01. This is exactly what you specified, which is good.

因此要添加另一个纬度和经度.我将再次打开文件,读入当前数据,追加到文件中,然后再写回.

So to add another latitude and longitude. I would open the file again, read in the current data, append to it and then write it back.

library(ncdf4)
nc <- nc_open("tmp.nc", write = TRUE)
lat <- ncvar_get(nc, varid='latitude')
lat <- append(lat, 44.5)
ncvar_put(nc, varid='latitude', vals=lat, start=c(1), count=2)
nc_close(nc)

现在ncdump将为您显示两个纬度:

Now ncdump will show you two latitudes:

data:

 latitude = 44, 44.5 ;

 longitude = -88.5 ;

当然,对于不需要或不想读取所有数据和附加数据的大型数据集,您只需告诉NetCDF您要将其写入的位置即可.

Of course for large datasets you do not need or want to read in all the data and the append, you can just tell NetCDF where you want it written.

library(ncdf4)
nc <- nc_open("tmp.nc", write = TRUE)
lon = -89.0
ncvar_put(nc, varid='longitude', vals=lon, start=c(2), count=1)
nc_close(nc)

现在ncdump将显示两个纬度和两个经度:

Now ncdump will show you two latitudes and two longitudes:

data:

 latitude = 44, 44.5 ;

 longitude = -88.5, -89 ;

myvar的数据表示是一个3D数组,所以我会做不同的初始写入.我会在创建数据并将其写入文件时指定尺寸,例如:

The data represents of myvar is a 3D array, so I would have done the initial write different. I would have specified it's dimensions when creating the data and when writing it to the file, like this:

data <- array(1:1000, c(1,1,1000))
ncvar_put(nc = nc, varid='myvar', vals=data, start=c(1,1,1), count=c(1,1,1000))

然后附加到第二个纬度和经度:

Then to append to the second latitude and longitude:

data <- array(11:1011, c(1,1,1000))
ncvar_put(nc = nc, varid='myvar', vals=data, start=c(2,2,1), count=c(1,1,1000))


注意

我觉得R包对您隐藏得太多了.使用ncdim_def创建尺寸时,可以为其指定值.我认为这更像是一个三步过程.

I feel the R package hides too much from you. When you create a dimension with ncdim_def, you can give it values. This is in my mind more of a 3 step process.

  1. 创建尺寸.
  2. 创建与该维度相关联的变量.
  3. 向此变量添加数据.

希望这会有所帮助.

这篇关于使用R扩展netCDF文件中的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆