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

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

Method 1: Direct output of netCDF: no file predefinition

This method is the simple, but (possibly) inefficient way to write netCDF files from NCL. This approach is appropriate if you don't have a lot of large sized variables to write to the file. For a more efficient method, see method 2.

    ;=====================================================================
    ; Assume variables T, PS exist and that they have coordinate variables 
    ;      T  is four  dimensional:  T(time,lev,lat,lon)
    ;      PS is three dimensional: PS(time,lat,lon)
    ;
    ; Assume that ORO exists. ORO is two dimensional
    ; but it does not have any coordinate variables
    ; or attributes associated with it. 
    ; These must then be explicitly assigned. Assume the
    ; lat/lon coordinate arrays can be taken from variable T. 
    ; Note that the file name does not have to be same as variable name  
    ;=====================================================================
       system("/bin/rm -f simple.nc")   ; remove any pre-existing file
       ncdf     = addfile("simple.nc" ,"c")  ; open output netCDF file

    ; make time and UNLIMITED dimension      ; recommended  for most applications
       filedimdef(ncdf,"time",-1,True) 

    ; output variables directly
       ncdf->T  = T                          ; 4D               
       ncdf->PS = PS                         ; 3D 

       ORO!0    = "lat"                      ; assign named dimensions
       ORO!1    = "lon"
       
       ORO&lat  = T&lat                      ; copy lat from T to ORO
       ORO&lon  = T&lon 

       ORO@long_name = "orography"           ; assign attributes 
       ORO@units     = "m"

       ncdf->TOPOGRAPHY = ORO
There is no need to explicitly copy the coordinate variables or attributes. They are automatically written to the file when the variables are output.