Geographic Information Systems Asked by pkry on February 16, 2021
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.
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.
Correct answer by Spacedman on February 16, 2021
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]
Answered by pkry on February 16, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP