简体   繁体   English

使用 R 读取 grib2 数据集而不丢失元数据

[英]Reading a grib2 dataset without loss of metata with R

My question follows this previous question, that has been partially answered: Reading a grib2 dataset with 4 dimensions and 2 variables with R我的问题遵循之前的问题,该问题已得到部分回答: Reading a grib2 dataset with 4 dimensions and 2 variables with R

I am trying to read a GRIB2 file with R. This file is a probabilistic meteorological forecast: 10 variables, 1 lead time, 17 longitudes, 23 latitudes, and 51 members.我正在尝试读取带有 R 的 GRIB2 文件。此文件是概率气象预报:10 个变量、1 个提前期、17 个经度、23 个纬度和 51 个成员。

I can extract that thanks to the terra package and this script:由于 terra package 和这个脚本,我可以提取它:

require(terra)
require(dplyr)
require(data.table)
require(stats)

destfile <- "C:/Users/XXX/Documents/example_grib_file_3"

##Downloading file
grib_data <- terra::rast(destfile)
print(grib_data)

## Convert to data frame
df <- as.data.frame(grib_data, xy=TRUE)

## Colnames is a combination of members (50) X time (57) X variables (2)
colNames <- paste(names(grib_data), as.character(time(grib_data)), sep="_")
colnames(df) <- c("lon", "lat", colNames)

df2 <- data.table::melt(as.data.table(df), c("lon", "lat"))

## Split variable and time
df2$time_UTC <- sub(".*_", "", df2$variable)    
df2$variable <- sub("_.*", "", df2$variable)   

## Add members
df2 <- df2 %>% group_by(lon, lat, variable, time_UTC) %>% mutate(member=(1:length(value)))

##Convert to array
df_array <- stats::xtabs(value~lon+lat+variable+member+time_UTC, df2, drop=F)

The BIG problem is that I can't retrieve the metadata concerning the perturbation number (member).大问题是我无法检索有关扰动数(成员)的元数据。 For now, variables and members are mixed, giving 500 columns, with the variable name repeated 50 times.现在,变量和成员混合在一起,给出 500 列,变量名重复 50 次。 The member is not always at the same position along the other dimension (variable).成员并不总是在另一个维度(变量)上相同的 position。 For example, one particular member is in position 6 for temperature data, and position 50 for precipitation data.例如,某个特定成员的温度数据为 position 6,降水数据为 position 50。

So the line "Add member" in my code is totally irrelevant and needs the "perturbation number" to arrange the array.所以我的代码中的“添加成员”行是完全不相关的,需要“扰动数”来排列数组。 If I use eccodes, there is a field called "perturbationNumber", how can I retrieve it from terra and R?如果我使用 eccodes,有一个名为“perturbationNumber”的字段,我如何从 terra 和 R 中检索它?

The example file: https://drive.google.com/file/d/1kUfTdtAdNJpugMpPhdQ6tY4QZaZlUQcA/view?usp=sharing示例文件: https://drive.google.com/file/d/1kUfTdtAdNJpugMpPhdQ6tY4QZaZlUQcA/view?usp=sharing

These are the different parameters retrieved using eccodes with Python:这些是使用 Python 的 eccodes 检索到的不同参数:

grib_ls -l 48.5,3.5,1 -p paramId,name,typeOfLevel,level,dataDate,stepRange,dataType,shortName,step,perturbationNumber PATH_TO_FILE

paramId             name                typeOfLevel         level               dataDate            stepRange           dataType            shortName           step                perturbationNumber   value
167                 2 metre temperature  surface             0                   20230124            0                   pf                  2t                  0                   35                  273.517
167                 2 metre temperature  surface             0                   20230124            0                   pf                  2t                  0                   3                   273.589
167                 2 metre temperature  surface             0                   20230124            0                   pf                  2t                  0                   50                  273.229
etc...

Thanks for any help谢谢你的帮助

With your example file使用您的示例文件

f <- "example_grib_file_3"
library(terra)
#terra 1.7.5
x <- rast(f)

This is the metadata that is available这是可用的元数据

names(x)[1]
#[1] "SFC (Ground or water surface); 2 metre temperature [C]"
time(x)[1]
#[1] "2023-01-24 UTC"
units(x)[1]
#[1] "C"

And with version 1.7-5 (currently the development version) you can also get raw metadata在 1.7-5 版(目前是开发版)中,您还可以获得原始元数据

metadata(x)[[1]]
#     [,1]                    [,2]                     
#[1,] "GRIB_COMMENT"          "2 metre temperature [C]"
#[2,] "GRIB_ELEMENT"          "2T"                     
#[3,] "GRIB_FORECAST_SECONDS" "0"                      
#[4,] "GRIB_REF_TIME"         "1674518400"             
#[5,] "GRIB_SHORT_NAME"       "0-SFC"                  
#[6,] "GRIB_UNIT"             "[C]"                    
#[7,] "GRIB_VALID_TIME"       "1674518400"             

I do not know if there are ways to get other metdata from this file but in principle that is possible: see your previous question.我不知道是否有办法从此文件中获取其他元数据,但原则上这是可能的:请参阅您之前的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM