2

I downloaded monthly averaged data for a given area, 2 variables and 3 years. I read the grib file on R and I see there are 72 bands (12*2*3). Great. But how do I know what each one is? I would expect some kind of legend (like this). But nothing in the Spatial Grid Data Frame seems recognizable.

Any idea?

The file can be dowloaded here.

4
  • Have you got a link to what an ERA5 grib file is? Or where we can download one? Or if you have any other metadata?
    – Spacedman
    Commented May 4, 2020 at 17:32
  • Without seeing the file, it's hard to tell, but if you have access to gdal/ogr on the command line, try gdalinfo <fname.grb>. Or download in netCDF (these are definitely well labeled)
    – Jose
    Commented May 4, 2020 at 17:43
  • @Spacedman, there's the link to the actual file on the post; here to what a grib file is; here how to download it.
    – pkry
    Commented May 4, 2020 at 17:59
  • @Jose, I'll try to download it as netCDF, I'll let you know. Thanks!
    – pkry
    Commented May 4, 2020 at 18:02

3 Answers 3

3

GRIB files usually do have a lot of metadata but GDAL doesn't really know how to cope with it. Best I can do for now is to install the gdalUtils package and run gdalinfo on it.

meta = gdalinfo("./adaptor.mars.internal-1588581859.5698528-1195-27-4b6c8643-6402-4cf9-be82-375b77b0eea7.grib")

which is a vector of strings. After the initial metadata there's a set of strings for each layer in the data:

[21] "Band 1 Block=17x1 Type=Float64, ColorInterp=Undefined"                                              
[22] "  Description = 0[-] SFC (Ground or water surface)"                                                 
[23] "  Metadata:"                                                                                        
[24] "    GRIB_COMMENT=2 metre temperature [C]"                                                           
[25] "    GRIB_ELEMENT=2T"                                                                                
[26] "    GRIB_FORECAST_SECONDS=0 sec"                                                                    
[27] "    GRIB_REF_TIME=  1262304000 sec UTC"                                                             
[28] "    GRIB_SHORT_NAME=0-SFC"                                                                          
[29] "    GRIB_UNIT=[C]"                                                                                  
[30] "    GRIB_VALID_TIME=  1262304000 sec UTC"                                                           
[31] "Band 2 Block=17x1 Type=Float64, ColorInterp=Undefined"                                              
[32] "  Description = 0[-] SFC (Ground or water surface)"                                                 
[33] "  Metadata:"                                                                                        
[34] "    GRIB_COMMENT=Total precipitation [m]"                                                           
[35] "    GRIB_ELEMENT=TP"                                                                                
[36] "    GRIB_FORECAST_SECONDS=0 sec"                                                                    
[37] "    GRIB_REF_TIME=  1262282400 sec UTC"                                                             
[38] "    GRIB_SHORT_NAME=0-SFC"                                                                          
[39] "    GRIB_UNIT=[m]"                                                                                  
[40] "    GRIB_VALID_TIME=  1262282400 sec UTC"                     

You'll have to write some code to parse those elements if you need that info, or to convert those times. I'm not sure how you can otherwise easily get that metadata into something computable.

1
  • That's the clue I needed, thanks, @Spacedman. I will work on it and be back here to update.
    – pkry
    Commented May 4, 2020 at 18:30
0

In case it might be helpful, here is what I did to retrieve the information I was looking for (Time and Variable for each Band). I had to install GDAL to use library(gdalUtils).

It's based of course on the answer by @Spacedman.

info<-gdalinfo('file.grib')

Filter and retrieve the information of interest (as seen here).

grib1<-as.data.frame(info)
grib1<-as.data.frame(grib1[grep(pattern='Band|GRIB_COMMENT|GRIB_REF_TIME', x=info),])
colnames(grib1)<-c('raw_')

Clean a little bit (with the help of this post, among others).

grib1 <- grib1 %>%
  mutate(content = case_when(
    startsWith(as.character(raw_),'Band', trim=TRUE) ~ gsub(".*Band (.+) Block.*", "\\1",raw_),
    startsWith(as.character(raw_),'GRIB_C', trim=TRUE) ~ sub(".*=", "", raw_),
    startsWith(as.character(raw_),'GRIB_R', trim=TRUE) ~ gsub(".*= (.+) sec.*", "\\1",raw_)
  )
  )
grib1 <- grib1 %>%
  mutate(column = case_when(
    startsWith(as.character(raw_),'Band', trim=TRUE) ~ 'Band',
    startsWith(as.character(raw_),'GRIB_C', trim=TRUE) ~ 'Variable',
    startsWith(as.character(raw_),'GRIB_R', trim=TRUE) ~ 'Time'
  )
  )
grib1<-grib1[,-1]

Variables to columns and time readable (as seen here).

grib1$id<-sort(rep(1:72,3))
grib1<-dcast(grib1, formula = id ~ column, value.var='content')
grib1<-grib1 %>% mutate(
  Time = as.POSIXct(as.numeric(as.character(Time)),origin="1970-01-01")
)
grib1<-grib1[,-1]
0

By using the option 'json', you can easily convert the information into an R-friendly format:

bands <- sf::gdal_utils(source = "file.grib",
                        options = c("-json"), 
                        quiet = T) %>%
  jsonlite::fromJSON()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.