converting RUC model data from GRIB to netCDF

From: Kirby, Stephen (Civ, ARL/CISD) (sfkirby AT arl.army.mil)
Date: Mon Jun 20 2005 - 12:38:08 MDT


 

Hello,
 
I am trying to modify some NCL code I saw (thanks Ryan/Dennis) in the
ncl-talk archives. It was originally written to convert 3d
(time,lat,lon) ECMWF output to netCDF.
 
As a first cut, since my RUC grib files each represent a single forecast
hour, I have commented out the time references. By using the Navy's
"gribsimp" and NCL print(filename), I have found it is a 151x113 grid.
gribsimp also shows the latitude of the first grid point and the
longitude of the first grid point.
 
Right now it is complaining:
 
"fatal:Number of dimensions in parameter (2) of (filedimdef) is (2), (1)
dimensions were expected
fatal:Execute: Error occurred at or near line 110"
 
parameter(2) is dimNames which = (/"lat","lon"/).
 
I don't follow why it is expecting 1 dimension with the dimNames
parameter in the filedimdef call??
 
At the very bottom is a NCL print of the GRIB file to provide info on
the grid size and the lat and lon variables.
 
Thanks for any advice.
 
--Steve Kirby
   Research Meteorologist
 
 
Here is the NCL code:
 
;***************************************************
; RUC full variable conversion to netcdf
;***************************************************
load "/home/skirby/NCL/lib/ncarg/nclscripts/csm/contributed.ncl"
 
;;function rdVar3D (f:file, vName:string, time:integer)
function rdVar2D (f:file, vName:string)
begin
    var = f->$vName$ ; read variable
    printVarSummary(var)
 

;; var!0 = "time" ; assign 'conventional' dimension names
;; var!1 = "lat"
    var!0 = "lat"
;; var!2 = "lon"
    var!1 = "lon"
                       ; following not needed for current application
   ;var&time = time ; assign integer time coord variable
    return(var)
end
 
begin
;***************************************************
; get all file names; then loop over each file
;***************************************************
; diri = "./surface_state_4xdaily/" ; input directory
     diri = "/home/skirby/TEMP/" ; input directory
; fils = systemfunc("cd "+diri+"; ls e4oper.an.sfc.200*")
    fils = systemfunc("cd "+diri+"; ls ruc*grb")
     nfils = dimsizes(fils)
     print (fils)
 

     OS = systemfunc ("uname")
     diro = "./"
 
;***************************************************
; specify desired GRIB variables
;***************************************************
 

   ;system("banner HI")
    do nf=0,nfils-1
       print ("===========> nf="+nf+" <========================")
wcStrt = systemfunc("date") ; wall clock start
;***************************************************
; open grib file and get variable names
;***************************************************
 

       grib_in = addfile(diri+fils(nf)+".grb","r")
       names = getfilevarnames(grib_in) ; get ALL variable names
 

       wallClockElapseTime(wcStrt, "Read/open file file: "+fils(nf), 0)
       ncl_names = names
 
; lat = grib_in->g4_lat_1 ; latitude
       lat = grib_in->gridlat_236 ; latitude
       lat!0 = "lat" ; rename NCL's GRIB dimension names
; lon = grib_in->g4_lon_2 ; longitude
      lon = grib_in->gridlon_236 ; longitude
       lon!0 = "lon" ; to 'conventional' dimension
;; stime = grib_in->initial_time0
;; stime = grib_in->initial_time
;; time = grib_stime2itime (stime) ; string to integer
(contributed.ncl)
if (nf.eq.0) then
           print (names)
;; print (stime+" "+time)
       end if
 

;; ntim = dimsizes(time)
       nlat = dimsizes(lat)
       mlon = dimsizes(lon)
 

;***************************************************
; open output netcdf file
; create parameters and output file
;***********************************************
 

       system ("rm -f "+diro+fils(nf)+".nc") ; remove pre-existing file
(if any)
       netcdf_out = addfile(diro+fils(nf)+".nc","c") ; "c"reate the
netCDF file
;***********************************************
;***********************************************
; assign file attributes
;***********************************************
       fAtt = True
       fAtt@title <mailto:fAtt@title> = "GRIB-to-netCDF: processed by
sfkirby@arl.army.mil <mailto:sfkirby@arl.army.mil> "
 

       fAtt@source_file <mailto:fAtt@source_file> = fils(nf)
       fAtt@Conventions <mailto:fAtt@Conventions> = "None"
       fAtt@System <mailto:fAtt@System> = OS
       fAtt@creation_date <mailto:fAtt@creation_date> = systemfunc
("date")
       fileattdef( netcdf_out, fAtt )
 
;***********************************************
; predefine coordinate information
;***********************************************
;; dimNames = (/"time", "lat", "lon" /)
      dimNames = (/"lat","lon" /)
;; dimSizes = (/ ntim , nlat, mlon/)
       dimSizes = (/ nlat, mlon/)
;; dimUnlim = (/ True , False, False/)
       dimUnlim = (/ False,False/)
       filedimdef(netcdf_out, dimNames , dimSizes, dimUnlim )
 

;; filevardef (netcdf_out, "time" , typeof(time), "time" )
;; filevarattdef(netcdf_out, "time", time)
 

       filevardef (netcdf_out, "lat", typeof(lat), "lat")
       filevarattdef(netcdf_out, "lat", lat)
 

       filevardef (netcdf_out, "lon", typeof(lon), "lon")
       filevarattdef(netcdf_out, "lon", lon)
 
;***********************************************
; predefine variable sizes
; (unfortunately) must read variables from GRIB file
; needed for filevarattdef
;***********************************************
      do i=3, dimsizes(ncl_names)-1 ;;sk -- why 3?
;; TMP = rdVar3D (grib_in, names(i) ,time)
         TMP = rdVar2D (grib_in, names(i))
;; filevardef(netcdf_out, names(i) \
;; , typeof(TMP), (/"time","lat","lon"/) )
         filevardef(netcdf_out, names(i) \
                              , typeof(TMP), (/"lat","lon"/) )
         filevarattdef(netcdf_out, names(i) , TMP)
      end do
 

      wallClockElapseTime(wcStrt, "Defintion phase took ", 0)
;***************************************************
; write data values to predefined locations
;***************************************************
      wcStrt = systemfunc("date") ; wall clock start
;; netcdf_out->time = (/ time /)
      netcdf_out->lat = (/ lat /)
      netcdf_out->lon = (/ lon /)
do i=3, dimsizes(names)-1
;; TMP = rdVar3D (grib_in, names(i) ,time)
         TMP = rdVar2D (grib_in, names(i))
         netcdf_out->$names(i)$ = (/ TMP /)
      end do
      wallClockElapseTime(wcStrt, "Write phase took ", 0)
 

;; delete (stime) ; may change next "nf" iteration
;; delete (time)
      delete (names)
      delete (lat)
      delete (lon)
      delete (TMP)
   end do
end
 
 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++
For reference, below is a NCL dump of the RUC grib file dimensions and
some variables , PRES_236_SFC,gridlat_236 and gridlon_236:
 
 
filename: ruc
path: ruc.grb
   file global attributes:
   dimensions:
      gridx_236 = 113
      gridy_236 = 151
      lv_ISBL2 = 37
      lv_SPDY3 = 6
   variables:
      float PRES_236_SFC ( gridx_236, gridy_236 )
         center : US National Weather Service - NCEP (WMC)
         long_name : Pressure
         units : Pa
         _FillValue : -999
         coordinates : gridlat_236 gridlon_236
         level_indicator : 1
         grid_number : 236
         parameter_number : 1
         model : RUC Model from FSL (isentropic; scale: 20km at
40N)
         forecast_time : 1
         initial_time : 06/14/2005 (00:00)
 
float gridlat_236 ( gridx_236, gridy_236 )
         corners : <ARRAY>
         GridType : Lambert Conformal Secant or Tangent, Conical or
bipolar
         units : degrees_north
         Latin2 : 25
         Latin1 : 25
         Dy : 40635
         Dx : 40635
         Lov : 265
         Lo1 : 233.862
         La1 : 16.281
         
      float gridlon_236 ( gridx_236, gridy_236 )
         corners : <ARRAY>
         GridType : Lambert Conformal Secant or Tangent, Conical or
bipolar
         units : degrees_east
         Latin2 : 25
         Latin1 : 25
         Dy : 40635
         Dx : 40635
         Lov : 265
         Lo1 : 233.862
         La1 : 16.281
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++
 
 


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



This archive was generated by hypermail 2b29 : Mon Jun 20 2005 - 17:26:15 MDT