【发布时间】:2021-03-13 20:12:45
【问题描述】:
我想将 GRIB2 文件读入 R,但无法安装 wgrib2(经过几个小时的努力),这意味着 rNOMADS 不是一个选项。没关系,因为raster 和rgdal 包都可以读取GRIB2 文件。我遇到的问题是在读取文件时层的名称被剥离。
这是一个例子。
# Load libraries
library(raster)
library(rgdal)
# Name of file
file_name <- "https://dd.weather.gc.ca/model_gem_regional/coupled/gulf_st-lawrence/grib2/00/001/CMC_coupled-rdps-stlawrence-ocean_latlon0.02x0.03_2020120100_P001.grib2"
# Load as raster brick
b <- brick(file_name)
# Get layer names
names(b)
# [1] "CMC_coupled.rdps.stlawrence.ocean_latlon0.02x0.03_2020120100_P001.1"
# [2] "CMC_coupled.rdps.stlawrence.ocean_latlon0.02x0.03_2020120100_P001.2"
# [3] "CMC_coupled.rdps.stlawrence.ocean_latlon0.02x0.03_2020120100_P001.3"
# [4] "CMC_coupled.rdps.stlawrence.ocean_latlon0.02x0.03_2020120100_P001.4"
# [5] "CMC_coupled.rdps.stlawrence.ocean_latlon0.02x0.03_2020120100_P001.5"
# [6] "CMC_coupled.rdps.stlawrence.ocean_latlon0.02x0.03_2020120100_P001.6"
# [7] "CMC_coupled.rdps.stlawrence.ocean_latlon0.02x0.03_2020120100_P001.7"
# [8] "CMC_coupled.rdps.stlawrence.ocean_latlon0.02x0.03_2020120100_P001.8"
# [9] "CMC_coupled.rdps.stlawrence.ocean_latlon0.02x0.03_2020120100_P001.9"
#[10] "CMC_coupled.rdps.stlawrence.ocean_latlon0.02x0.03_2020120100_P001.10"
如您所见,名称只是通用默认值。接下来,我尝试了rgdal。
# Load using rgdal
r <- readGDAL(file_name)
# Get names
names(r)
# [1] "band1" "band2" "band3" "band4" "band5" "band6" "band7" "band8"
# [9] "band9" "band10"
再一次,默认名称。但是,如果我使用命令行实用程序 ncl_convert2nc 将 GRIB2 文件转换为 NetCDF,然后使用 ncdf4 读入 NetCDF 文件 – 如果可以的话,我不想在我的工作流程中包含一个额外的转换步骤避免 - 肯定存在变量名称。
# [1] "UOGRD_P0_L160_GLL0" "VOGRD_P0_L160_GLL0" "ICEC_P0_L1_GLL0"
# [4] "ICETK_P0_L1_GLL0" "UICE_P0_L1_GLL0" "VICE_P0_L1_GLL0"
# [7] "ICETMP_P0_L1_GLL0" "ICEPRS_P0_L1_GLL0" "CICES_P0_L1_GLL0"
#[10] "WTMP_P0_L1_GLL0"
问题:在使用rgdal 或raster 读取GRIB2 文件时,有没有办法提取或保留变量/层名称?
PS 我需要从文件中获取变量名称的原因是,当加载(例如)@987654334 时,层 不 与指定的层顺序匹配 on the website @。从变量值可以看出这一点。虽然我可以使用从上面显示的 NetCDF 文件中收集的变量名称,但如果层的顺序发生更改,这会破坏我的包。
【问题讨论】: