Re: Extracting Grib Data

From: Dennis Shea (shea AT cgd.ucar.edu)
Date: Mon Aug 22 2005 - 14:34:50 MDT

  • Next message: Mike Notaro: "MOC file"

    >Hi,
    >>I am a new user of NCL.
    >>I have a Grib file with 29 variable.
    >>I wish to extract single two variable (U and V)
    >>and to turn it to format netcdf, using NCL script:
    >
    >as I can make this task?
    >_______________________________________________

    Yes.

    The following is a simple approach that allows you
    to rename the variable. If you do not want to do this
    just eliminate the new_name code.

    [1] find the variable names that NCL is assigning
        the wind components you desire.
        
        Interactively:
        
        % ncl <return>
        % f = addfile ("foo.grb" ,"r") ; foo is name of file
        % print(f) ; analogous to ncdump -h
                                           ; what names do you want
        % quit
        
      
        
    [2] Create a script grb2nc.ncl
    [Note: substitute the appropriate names for
           the U and V winds. The should replace
           the U_NCL_Name and V_NCL_Name mentioned below
     

    begin

     diri = "./" ; input directory
     diro = "./" ; output directory
     fnam = "foo" ; input GRIB file
                                                ; Create ('c') netCDF
     system ("/bin/rm -f "+diro+fnam+".nc") ; remove any pre-exist files
     ncdf = addfile(diro+fnam+".nc","c") ; open netCDF (.nc extension)
                                                ; Read ('r') a GRIB file from the
     grbf = addfile(diri+fnam+".grb", "r") ; append .grb extension
    ;print(grbf)

     names = getfilevarnames(grbf) ; extract ALL variable names
                                                ; loop through ALL the variables

     grb_name = (/ "U_NCL_Name","V_NCL_Name"/) ; specify desired variables
     nc_name = (/ "U" , "V" /) ; new variable names [optional]

     do i=0, dimsizes(grb_name)-1 ; loop over variables and copy
        if (isfilevar(grbf,grb_name(i))) then ; is "grb_name(i)" in file
            print("writing ncdf: i,name="+ i+ " "+ grb_name(i)+" ==> "+new_name(i))
            ncdf->$new_name(i)$ = grbf->$grb_name(i)$
        end if
     end do

     print (ncdf) ; similar to ncdump -h
    end

    [3] execute script

        ncl grib2nc.ncl
        
    =======================================================================

    _______________________________________________
    ncl-talk mailing list
    ncl-talk@ucar.edu
    http://mailman.ucar.edu/mailman/listinfo/ncl-talk



    This archive was generated by hypermail 2b29 : Mon Aug 22 2005 - 20:13:36 MDT