使用R打开grib文件

发布于 2024-09-06 05:35:59 字数 380 浏览 3 评论 0原文

我正在使用 R 来处理气象数据。我分两步进行:

  1. 使用 ncar 命令语言中的命令行函数 ncl_convert2nc 将 grib 转换为 netcdf,
  2. 使用 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:

  1. convert grib to netcdf using the command line function ncl_convert2nc from ncar command language
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

知你几分 2024-09-13 05:35:59

关于问题 1,答案是“是”——如果您想捕获结果,请参阅 help(system)internal=TRUE 选项。

Regarding question 1, the answer is 'Yes' -- see help(system) and the internal=TRUE option if you want to capture results.

自由如风 2024-09-13 05:35:59

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.

呆头 2024-09-13 05:35:59

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.

若水般的淡然安静女子 2024-09-13 05:35:59

作为从 R 调用 ncl_convert2nc 的替代方案,我建议有两种替代方案:

1。 CDO 转换

另一个快速且简单的命令行解决方案是使用 cdo 转换为 netcdf 来读入:

cdo -f nc copy file.grb file.nc

如果要输出 netcdf4 文件,请指定“-f nc4”。

这种方法的一个潜在问题是,如果您的 grib 文件具有多个时间轴(例如,对于多个季节性预测),这可能会导致转换问题。

2. ECCODES 转换

相反,eccodes 提供了一个非常强大的 grib 转换器,可以处理多个时间轴的所有情况,这通常会导致基于 CDO 和 NCL 的转换失败。

该命令名为 grib_to_netcdf

grib_to_netcdf -o output.nc input_grib.grb

到目前为止,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:

cdo -f nc copy file.grb file.nc

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

grib_to_netcdf -o output.nc input_grib.grb

So far, grib_to_netcdf has been able to handle every grib file I have thrown at it without problems.

江湖彼岸 2024-09-13 05:35:59

另一种解决方案是使用 wgrib/wgrib2 软件 (http://www. cpc.ncep.noaa.gov/products/wesley/wgrib2/) 并将 GRIB-1/GRIB-2 文件直接转储为 CSV 格式,例如:

/path/to/your/wgrib2 input_file.grb -csv output_file.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.:

/path/to/your/wgrib2 input_file.grb -csv output_file.csv

Then it may be read directly in R...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文