[R] How to read a grib2 file

Michael Sumner mdsumner at gmail.com
Wed Sep 14 12:03:54 CEST 2016


On Wed, 14 Sep 2016 at 00:49 Debasish Pai Mazumder <pai1981 at gmail.com>
wrote:

> Hi Mike,
> Thanks again. I am using Mac OS
>
> Here is the required info
>
> > sessionInfo()
> R version 3.2.4 (2016-03-10)
> Platform: x86_64-apple-darwin13.4.0 (64-bit)
> Running under: OS X 10.11.6 (El Capitan)
>
> locale:
> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] rNOMADS_2.3.0 rvest_0.3.2   xml2_1.0.0    rgdal_1.1-10  raster_2.5-8
> sp_1.2-3
>
> loaded via a namespace (and not attached):
> [1] httr_1.1.0      magrittr_1.5    R6_2.1.2        rsconnect_0.4.3
> tools_3.2.4
> [6] Rcpp_0.12.4     grid_3.2.4      lattice_0.20-33
>
> I am trying to read " tmax.01.2011040100.daily.grb2" from
> http://nomads.ncdc.noaa.gov/modeldata/cfsv2_forecast_ts_9mon/2011/201104/20110401/2011040100/
>
>

I was a bit surprised, but this does work on Windows - so at the very least
you can run it there with the standard CRAN R and rgdal+raster. Here's the
R code I used, and the resulting session info. (It also works on Debian,
but I'll assume you don't want those details).

f <- "
http://nomads.ncdc.noaa.gov/modeldata/cfsv2_forecast_ts_9mon/2011/201104/20110401/2011040100/tmax.01.2011040100.daily.grb2
"
download.file(f, basename(f), mode = "wb")
library(raster)
raster(basename(f))
# class       : RasterLayer
# band        : 1  (of  1224  bands)
# dimensions  : 190, 384, 72960  (nrow, ncol, ncell)
# resolution  : 0.9374987, 0.9473684  (x, y)
# extent      : -0.4687493, 359.5307, -90.24932, 89.75068  (xmin, xmax,
ymin, ymax)
# coord. ref. : +proj=longlat +a=6371229 +b=6371229 +no_defs
# data source : tmax.01.2011040100.daily.grb2
# names       : tmax.01.2011040100.daily

This at least gives you an easy pathway if you can get a Windows machine. I
have nearly no experience with Mac, so you'll have to pursue use of rgdal
in that OS if you really need it.

HTH

R version 3.3.1 Patched (2016-09-09 r71227)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
[5] LC_TIME=English_Australia.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] raster_2.5-8 sp_1.2-4

loaded via a namespace (and not attached):
[1] rgdal_1.1-10    Rcpp_0.12.6     grid_3.3.1      lattice_0.20-33






> with regards
> -Deb
>
>
> On Tue, Sep 13, 2016 at 5:26 AM, Michael Sumner <mdsumner at gmail.com>
> wrote:
>
>> What is your computer system? What is the output of this?
>>
>> sessionInfo()
>>
>> If you point to a file I'll try it so I can tell you the minimum system
>> requirements.
>>
>> Cheers, Mike
>>
>> On Tue, 13 Sep 2016, 08:56 Debasish Pai Mazumder <pai1981 at gmail.com>
>> wrote:
>>
>>> Thanks for your suggestion. I have checked and I don't have JPEG2000 in
>>> ggdalDrivers()). I am pretty new in R. I don't understand how to do I
>>> implement JPEG2000 (JP2OpenJPEG driver) in gdalDrivers() so that I can
>>> read grib2 files
>>>
>>> with regards
>>> -Deb
>>>
>>> On Sat, Sep 10, 2016 at 2:33 AM, Michael Sumner <mdsumner at gmail.com>
>>> wrote:
>>>
>>>>
>>>>
>>>> On Sat, 10 Sep 2016 at 07:12 Debasish Pai Mazumder <pai1981 at gmail.com>
>>>> wrote:
>>>>
>>>>> Hi
>>>>> I am trying to read a grib2 file in R.
>>>>>
>>>>> Here is my script
>>>>>
>>>>> library(rgdal)
>>>>> library(sp)
>>>>> library(rNOMADS)
>>>>> gribfile<-"tmax.01.2011040100.daily.grb2"
>>>>> grib <- readGDAL(gribfile)
>>>>>
>>>>> I am getting following error :
>>>>>
>>>>> dec_jpeg2000: Unable to open JPEG2000 image within GRIB file.
>>>>> Is the JPEG2000 driver available?tmax.01.2011040100.daily.grb2 has GDAL
>>>>> driver GRIB
>>>>> and has 190 rows and 384 columns
>>>>> dec_jpeg2000: Unable to open JPEG2000 image within GRIB file.
>>>>> Is the JPEG2000 driver available?dec_jpeg2000: Unable to open JPEG2000
>>>>> image within GRIB file.
>>>>>
>>>>>
>>>> Hi there, please check if JPEG2000 is in the gdalDrivers() list, i.e.
>>>>  in
>>>>
>>>> rgdal::gdalDrivers()$name
>>>>
>>>> You are looking for one starting with "JP2" as per the list next to the
>>>> "JPEG2000" rows here:
>>>>
>>>> http://gdal.org/formats_list.html
>>>>
>>>> I have  JP2OpenJPEG on one system, but not (for example) on the
>>>> Windows CRAN binary for rgdal, which is the only readily available Windows
>>>> build for this package.
>>>>
>>>> I you don't have it, you might try on a system that has the JP2OpenJPEG driver,
>>>> or ask someone to try on your behalf. You'd want to find out if that will
>>>> enable this read for you before investing time in the Linux configuration.
>>>>
>>>> It's not too hard to set up a Linux system for this, but does assume a
>>>> bit of experience on your part. Some of the docker images in the
>>>> rockerverse have this all sorted I believe, but it's been a while since I
>>>> used them.
>>>>
>>>> https://hub.docker.com/u/rocker/
>>>>
>>>> Cheers, Mike.
>>>>
>>>>
>>>>
>>>>
>>>>> Cheers
>>>>> -Deb
>>>>>
>>>>>         [[alternative HTML version deleted]]
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> PLEASE do read the posting guide
>>>>> http://www.R-project.org/posting-guide.html
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>
>>>> --
>>>> Dr. Michael Sumner
>>>> Software and Database Engineer
>>>> Australian Antarctic Division
>>>> 203 Channel Highway
>>>> Kingston Tasmania 7050 Australia
>>>>
>>>>
>>> --
>> Dr. Michael Sumner
>> Software and Database Engineer
>> Australian Antarctic Division
>> 203 Channel Highway
>> Kingston Tasmania 7050 Australia
>>
>>
> --
Dr. Michael Sumner
Software and Database Engineer
Australian Antarctic Division
203 Channel Highway
Kingston Tasmania 7050 Australia

	[[alternative HTML version deleted]]



More information about the R-help mailing list