NCL Website header
NCL Home> Application examples> File IO || Data files for some examples

Example pages containing: tips | resources | functions/procedures > GRIB to netCDF

GRIB to netCDF: Select Variables

Note: Support for GRIB2 files was added in version 4.3.0.

For both methods below, the user must first examine the GRIB file via the ncl_filedump program or NCL's interactive mode to determine variable names assigned by NCL.

Using ncl_convert2nc

A tool called ncl_convert2nc allows you to convert all or part of a GRIB file to netCDF.

  ncl_filedump ced1.lf00.t00z.eta.grb     <---- to view contents of file
                                                if necessary

  ncl_convert2nc ced1.lf00.t00z.eta.grb -v gridlat_6,gridlon_6
The above will create a file called "ced1.lf00.t00z.eta.nc" with the variables "gridlat_6" and "gridlon_6" written to it, along with the appropriate attributes and coordinate information.

Using an NCL script

Using an NCL script is useful if you need more control, like being able to rename the output variables or file. This method is not as simple as using ncl_convert2nc, and is possibly very slow.

begin 

;***************************************************
; Open GRIB file and get variable names
;***************************************************
   grib_in= addfile("./ced1.lf00.t00z.eta.grb","r")
   names  = getfilevarnames(grib_in)   ; extract ALL variable names

;***************************************************
; Open output netcdf file. Remove first just in case.
;***************************************************
   ncdf_out_fname = "select.nc"
   system("rm -f " + ncdf_out_fname) ; remove any pre-existing file 
   ncdf_out = addfile(ncdf_out_fname,"c")   

;***************************************************
; Specify desired GRIB variables 
;***************************************************
   grib_names = (/ "gridlat_6", "gridlon_6", "PRES_6_SFC","PRMSL_6_MSL",\
                   "HGT_6_SFC", "TMP_6_TRO","TMP_6_ISBL"/)
  
   ncl_names  = (/ "lat6", "lon6", "PS","SLP","HSFC","TTRO","T"/)

;***************************************************
; Loop over selected variables and rename on output
;***************************************************
   do i=0, dimsizes(ncl_names)-1  
   ncdf_out->$ncl_names(i)$ = grib_in->$grib_names(i)$ 
   end do   
end 
View the results from either method using ncl_filedump, or "ncdump" from the netCDF suite of tools:

    ncl_filedump select.nc

    ncdump -h select.nc
Click here for sample output.