如何在将 varnames() 和 longnames() 写入 NetCDF 文件之前将其分配给 SpatRaster?
How to assign varnames() and longnames() to SpatRaster before writting it to a NetCDF file?
我正在尝试使用 R
中的 terra
包将从 HDF5 获得的 Geo2D 数据保存到 NetCDF 文件。但是,我无法更改变量名称,包括短名称和长名称,正如您在数据信息和所附图像中可以更改的那样。
可重现的例子:
library(terra)
r <- rast(xmin=-180, xmax=180,ymin=-90, ymax=90, nrows=18, ncols=36)
values(r) <- 1:ncell(r)
# assign time
terra::time(r)<-as.POSIXct(959818500,origin = "1970-01-01",tz = "UTC")
#assign units
terra::units(r)<-"mm hr -1"
#assign short name
terra::varnames(r)<-"pr"
#assign long name
terra::longnames(r)<-"Precipitation"
writeCDF(x = r,
filename = "reproducible_example.nc",
compression = 9,
missval=1e32,
overwrite=TRUE)
SpatRaster 信息:
> r
class : SpatRaster
dimensions : 18, 36, 1 (nrow, ncol, nlyr)
resolution : 10, 10 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84
source : memory
varname : reproducible_example
name : lyr.1
min value : 1
max value : 648
time : 2000-06-01 00:15:00
图片来自Panoply:
编辑:
> sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.3 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3
locale:
[1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C LC_TIME=de_DE.UTF-8 LC_COLLATE=en_GB.UTF-8
[5] LC_MONETARY=de_DE.UTF-8 LC_MESSAGES=en_GB.UTF-8 LC_PAPER=de_DE.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] terra_1.4-15 lubridate_1.8.0 rgdal_1.5-27 hdf5r_1.3.4 stringr_1.4.0
[6] abind_1.4-5 raster_3.5-2 sp_1.4-5 gdalUtils_2.0.3.2 ncdf4.helpers_0.3-6
[11] ncdf4_1.17 DescTools_0.99.43 ffbase_0.13.3 ff_4.0.4 bit_4.0.4
loaded via a namespace (and not attached):
[1] Rcpp_1.0.7 compiler_4.1.1 R.methodsS3_1.8.1 R.utils_2.11.0 class_7.3-19 iterators_1.0.13
[7] tools_4.1.1 boot_1.3-28 rootSolve_1.8.2.3 lattice_0.20-44 Matrix_1.3-4 foreach_1.5.1
[13] fastmatch_1.1-3 rstudioapi_0.13 parallel_4.1.1 mvtnorm_1.1-3 expm_0.999-6 e1071_1.7-9
[19] generics_0.1.1 bit64_4.0.5 gld_2.6.2 grid_4.1.1 data.table_1.14.2 R6_2.5.1
[25] lmom_2.8 magrittr_2.0.1 codetools_0.2-18 MASS_7.3-54 Exact_3.0 proxy_0.4-26
[31] stringi_1.7.5 R.oo_1.24.0
你可以做到
library(terra)
r <- rast(xmin=-180, xmax=180,ymin=-90, ymax=90, nrows=18, ncols=36)
values(r) <- 1:ncell(r)
time(r) <- as.POSIXct(959818500,origin = "1970-01-01",tz = "UTC")
x <- writeCDF(r, "test.nc", "pr", longname="Precipitation", unit="mm hr -1", overwrite=TRUE)
x
#class : SpatRaster
#dimensions : 18, 36, 1 (nrow, ncol, nlyr)
#resolution : 10, 10 (x, y)
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84
#source : test.nc
#varname : pr (Precipitation)
#name : pr
#unit : mm hr -1
#time : 2000-06-01 00:15:00
或者
s <- sds(r)
units(s) <- "mm hr -1"
varnames(s) <- "pr"
longnames(s) <- "Precipitation"
y <- writeCDF(s, "test2.nc", overwrite=TRUE)
y
#class : SpatRaster
#dimensions : 18, 36, 1 (nrow, ncol, nlyr)
#resolution : 10, 10 (x, y)
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84
#source : test2.nc
#varname : pr (Precipitation)
#name : pr
#unit : mm hr -1
#time : 2000-06-01 00:15:00
sds
路由允许您将多个变量写入一个文件。
见?terra::writeCDF
我正在尝试使用 R
中的 terra
包将从 HDF5 获得的 Geo2D 数据保存到 NetCDF 文件。但是,我无法更改变量名称,包括短名称和长名称,正如您在数据信息和所附图像中可以更改的那样。
可重现的例子:
library(terra)
r <- rast(xmin=-180, xmax=180,ymin=-90, ymax=90, nrows=18, ncols=36)
values(r) <- 1:ncell(r)
# assign time
terra::time(r)<-as.POSIXct(959818500,origin = "1970-01-01",tz = "UTC")
#assign units
terra::units(r)<-"mm hr -1"
#assign short name
terra::varnames(r)<-"pr"
#assign long name
terra::longnames(r)<-"Precipitation"
writeCDF(x = r,
filename = "reproducible_example.nc",
compression = 9,
missval=1e32,
overwrite=TRUE)
SpatRaster 信息:
> r
class : SpatRaster
dimensions : 18, 36, 1 (nrow, ncol, nlyr)
resolution : 10, 10 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84
source : memory
varname : reproducible_example
name : lyr.1
min value : 1
max value : 648
time : 2000-06-01 00:15:00
图片来自Panoply:
编辑:
> sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.3 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3
locale:
[1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C LC_TIME=de_DE.UTF-8 LC_COLLATE=en_GB.UTF-8
[5] LC_MONETARY=de_DE.UTF-8 LC_MESSAGES=en_GB.UTF-8 LC_PAPER=de_DE.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] terra_1.4-15 lubridate_1.8.0 rgdal_1.5-27 hdf5r_1.3.4 stringr_1.4.0
[6] abind_1.4-5 raster_3.5-2 sp_1.4-5 gdalUtils_2.0.3.2 ncdf4.helpers_0.3-6
[11] ncdf4_1.17 DescTools_0.99.43 ffbase_0.13.3 ff_4.0.4 bit_4.0.4
loaded via a namespace (and not attached):
[1] Rcpp_1.0.7 compiler_4.1.1 R.methodsS3_1.8.1 R.utils_2.11.0 class_7.3-19 iterators_1.0.13
[7] tools_4.1.1 boot_1.3-28 rootSolve_1.8.2.3 lattice_0.20-44 Matrix_1.3-4 foreach_1.5.1
[13] fastmatch_1.1-3 rstudioapi_0.13 parallel_4.1.1 mvtnorm_1.1-3 expm_0.999-6 e1071_1.7-9
[19] generics_0.1.1 bit64_4.0.5 gld_2.6.2 grid_4.1.1 data.table_1.14.2 R6_2.5.1
[25] lmom_2.8 magrittr_2.0.1 codetools_0.2-18 MASS_7.3-54 Exact_3.0 proxy_0.4-26
[31] stringi_1.7.5 R.oo_1.24.0
你可以做到
library(terra)
r <- rast(xmin=-180, xmax=180,ymin=-90, ymax=90, nrows=18, ncols=36)
values(r) <- 1:ncell(r)
time(r) <- as.POSIXct(959818500,origin = "1970-01-01",tz = "UTC")
x <- writeCDF(r, "test.nc", "pr", longname="Precipitation", unit="mm hr -1", overwrite=TRUE)
x
#class : SpatRaster
#dimensions : 18, 36, 1 (nrow, ncol, nlyr)
#resolution : 10, 10 (x, y)
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84
#source : test.nc
#varname : pr (Precipitation)
#name : pr
#unit : mm hr -1
#time : 2000-06-01 00:15:00
或者
s <- sds(r)
units(s) <- "mm hr -1"
varnames(s) <- "pr"
longnames(s) <- "Precipitation"
y <- writeCDF(s, "test2.nc", overwrite=TRUE)
y
#class : SpatRaster
#dimensions : 18, 36, 1 (nrow, ncol, nlyr)
#resolution : 10, 10 (x, y)
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84
#source : test2.nc
#varname : pr (Precipitation)
#name : pr
#unit : mm hr -1
#time : 2000-06-01 00:15:00
sds
路由允许您将多个变量写入一个文件。
见?terra::writeCDF