问题
I have just downloaded some climate data in grib format. I want to use "R" to convert it to NetCDF format.
Furthermore, as the file consists of different variables, I would like to extract one variable at a time into individual files.
回答1:
It's hard to answer this without your specific file. You should look into producing reproducible examples, especially if you're posting to the R board.
For R, check out library(raster)
and library(ncdf4)
. I just grabbed the first grib1 file I saw, and put together a quick example.
library(raster)
library(ncdf4)
download.file(url = 'ftp://ftp.hpc.ncep.noaa.gov/grib/20130815/p06m_2013081500f030.grb', destfile = 'test.grb')
(r <- raster('test.grb'))
n <- writeRaster(r, filename = 'netcdf_in_youR_comp.nc', overwrite = TRUE)
回答2:
I know you ask for a solution using R, but it is much more straightforward to perform this task from the command line with cdo:
cdo -f nc copy test.grb test.nc
That's all you need to do. Use "-f nc4" if you want netcdf4 conventions.
To extract a variable you can first see what the variable names are using
ncdump -h test.nc
then, once you know the name of the variable you want to extract to a separate file, you can use
cdo selvar,varname test.nc var.nc
回答3:
you can use ncl installed on you computer
library(ncdf)
system(ncl_convert2nc xxxx.grb, internal = TRUE)
my.nc <- open.ncdf("result.nc")
print(my.nc)
来源:https://stackoverflow.com/questions/18501257/how-to-convert-grib1-to-netcdf-using-r