使用R打开grib文件
我正在使用 R 来处理气象数据。我分两步进行:
- 使用 ncar 命令语言中的命令行函数 ncl_convert2nc 将 grib 转换为 netcdf,
- 使用 R 中的 ncdf 包导入 netcdf 数据。
我还有一个问题:
2- 对于某些特定的 grib 文件,使用 ncar 工具进行转换不起作用。是否有其他方法或技巧(除了转录为 netcdf 之外)在 R 中读取 grib 文件?
Dirk 回答的问题: 1-我想在 R 中自动处理许多文件。我可以在 R 中调用 ncl_convert2nc 吗? (下面由 Dirk Eddelbuettel 回答)
I am using R to work with meteorological data. I proceed in two steps:
- convert grib to netcdf using the command line function ncl_convert2nc from ncar command language
- use package ncdf in R to import the netcdf data.
I still have one problem:
2- For some particular grib files, the conversion with ncar tool does not work. Is there other ways or trick (other than transcription into netcdf) to read grib files in R ?
Problem Answered by Dirk: 1- I would like to do automatic treatment of many files within R. Can I call ncl_convert2nc within R ? (answered by Dirk Eddelbuettel below )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
关于问题 1,答案是“是”——如果您想捕获结果,请参阅
help(system)
和internal=TRUE
选项。Regarding question 1, the answer is 'Yes' -- see
help(system)
and theinternal=TRUE
option if you want to capture results.rgdal 也可以做到这一点,但灵活性较差,并且比 ncdf 或 RNetCDF 需要更多的关注和细节 - 并且取决于您构建的 GDAL/rgdal(包括 GRIB 驱动程序)。
rgdal also can do it, but is less flexible and requires more care and detail than ncdf or RNetCDF - and depends of your GDAL/rgdal built including the GRIB driver.
ncl_convert2nc 似乎是最好的解决方案。但是,如果数据结构稍微复杂一点,我使用 GrADS 将 GRIB 文件转换为 ASCII(例如 .csv),然后可以使用专用于 R 的 ncdf4 包创建 NetCDF 文件。GrADS 还提供对重新构建的支持将 GRIB 写入 NetCDF,但仅限 1 个变量。
ncl_convert2nc seems to be the best solution. However, if the structure of data is a little bit more complicated I use GrADS to convert GRIB file to ASCII (e.g. .csv) and then it is possible to create NetCDF file using ncdf4 package dedicated for R. GrADS also provides support for re-writing GRIB to NetCDF, but there is limitation to only 1 variable.
作为从 R 调用 ncl_convert2nc 的替代方案,我建议有两种替代方案:
1。 CDO 转换
另一个快速且简单的命令行解决方案是使用 cdo 转换为 netcdf 来读入:
如果要输出 netcdf4 文件,请指定“-f nc4”。
这种方法的一个潜在问题是,如果您的 grib 文件具有多个时间轴(例如,对于多个季节性预测),这可能会导致转换问题。
2. ECCODES 转换
相反,eccodes 提供了一个非常强大的 grib 转换器,可以处理多个时间轴的所有情况,这通常会导致基于 CDO 和 NCL 的转换失败。
该命令名为 grib_to_netcdf
到目前为止,grib_to_netcdf 已经能够处理我遇到的每个 grib 文件毫无问题地扔了它。
As an alternative to calling ncl_convert2nc from R, there are two alternatives I can suggest:
1. CDO conversion
Another quick and easy command line solution is to use cdo to convert to netcdf to read in:
If you want to output a netcdf4 file you specify "-f nc4".
One potential glitch with this approach is if your grib file has more than one time axis (e.g. for multiple seasonal forecasts) which can cause issues with the conversion.
2. ECCODES conversion
Instead eccodes offers a grib converter that is very robust and can handle all cases of multiple time axes which usually cause CDO and NCL based conversions to fail.
The command is called grib_to_netcdf
So far, grib_to_netcdf has been able to handle every grib file I have thrown at it without problems.
另一种解决方案是使用 wgrib/wgrib2 软件 (http://www. cpc.ncep.noaa.gov/products/wesley/wgrib2/) 并将 GRIB-1/GRIB-2 文件直接转储为 CSV 格式,例如:
然后可以直接在 R 中读取...
Another solution is to use the wgrib/wgrib2 software (http://www.cpc.ncep.noaa.gov/products/wesley/wgrib2/) and dump your GRIB-1/GRIB-2 file directly to CSV format, e.g.:
Then it may be read directly in R...