gpt4 book ai didi

r - 在光栅砖中交换轴

转载 作者:行者123 更新时间:2023-12-04 15:31:45 25 4
gpt4 key购买 nike

使用 R raster包,我有一个 brick从文件中获取,具有以下 ncdump header (我展示了一个小示例文件,实际文件要大得多):

dimensions:
lon = 2 ;
lat = 3 ;
time = UNLIMITED ; // (125000 currently)
variables:
float lon(lon) ;
lon:standard_name = "longitude" ;
lon:long_name = "longitude" ;
lon:units = "degrees_east" ;
lon:axis = "X" ;
float lat(lat) ;
lat:standard_name = "latitude" ;
lat:long_name = "latitude" ;
lat:units = "degrees_north" ;
lat:axis = "Y" ;
double time(time) ;
time:standard_name = "time" ;
time:long_name = "Time" ;
time:units = "seconds since 2001-1-1 00:00:00" ;
time:calendar = "standard" ;
time:axis = "T" ;
short por(time, lat, lon) ;
por:_FillValue = 0s ;
por:missing_value = 0s ;

而在 R :
class       : RasterBrick 
dimensions : 3, 2, 6, 125000 (nrow, ncol, ncell, nlayers)
resolution : 0.008999825, 0.009000778 (x, y)
extent : 6.4955, 6.5135, 44.0955, 44.1225 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : /home/clima-archive/afantini/chym/chym_output/test.nc
names : X0, X3600, X7200, X10800, X14400, X18000, X21600, X25200, X28800, X32400, X36000, X39600, X43200, X46800, X50400, ...
z-value : 0, 449996400 (min, max)
varname : por

然而,为了更快的访问和更高的压缩率,已经交换了两个文件维度,因此分块更适合我们需要的使用类型。所以文件将是这样的( link to download the 1MB file ):
dimensions:
lon = UNLIMITED ; // (2 currently)
lat = 3 ;
time = 125000 ;
variables:
float lon(lon) ;
lon:standard_name = "longitude" ;
lon:long_name = "longitude" ;
lon:units = "degrees_east" ;
lon:axis = "X" ;
float lat(lat) ;
lat:standard_name = "latitude" ;
lat:long_name = "latitude" ;
lat:units = "degrees_north" ;
lat:axis = "Y" ;
double time(time) ;
time:standard_name = "time" ;
time:long_name = "Time" ;
time:units = "seconds since 2001-1-1 00:00:00" ;
time:calendar = "standard" ;
time:axis = "T" ;
short por(lon, lat, time) ;
por:_FillValue = 0s ;
por:missing_value = 0s ;

而在 R :
class       : RasterBrick 
dimensions : 3, 125000, 375000, 2 (nrow, ncol, ncell, nlayers)
resolution : 3600, 0.009000778 (x, y)
extent : -1800, 449998200, 44.0955, 44.1225 (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : /home/clima-archive/afantini/chym/chym_output/test_swapped.nc
names : X6.5, X6.50899982452393
degrees_east: 6.5, 6.50899982452393
varname : por

如您所见,文件打开时好像列数为 125000。我想将列数与层数交换,而不读取所有数据。我认为从光栅手册我应该使用 layerlvar , 自从:

layer: integer. The layer (variable) to use in a multi-layer file, or the layer to extract from a RasterStack/Brick or SpatialPixelsDataFrame or SpatialGridDataFrame. An empty RasterLayer (no associated values) is returned if ‘layer=0’

.......

‘lvar’: integer > 0 (default=3). To select the 'level variable' (3rd dimension variable) to use, if the file has 4 dimensions (e.g. depth instead of time)



但这似乎不起作用,例如设置 layer="time" ,因为它什么都没有改变。

我怎样才能做到这一点?

最佳答案

如果您不介意在打开/阅读后 reshape ,我认为您可以使用 ncdf4 读取变量中的数据库,然后转置它。就像是:

nc   <- nc_open(*your_nc_file*)
data <- ncvar_get(nc, por) # "por" is the name of your variable, right ?
data_new <- aperm(data, c(1,3,2)) # "transpose" the matrix

一个可能的问题可能是 data_new不再是 raster*对象,但你可以很容易地从它重新创建一个。

哈,

洛伦佐

关于r - 在光栅砖中交换轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39748615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com