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 is the most common approach to writing netCDF from NCL scripts. This method is the simple, but (possibly) inefficient.

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 
    ; meta data associated with it.  These must be explicitly assigned. Assume the
    ; lat/lon coordinate arrays can be taken from variable T. 
    ; Note that the variable name on the output file does not 
    ; have to be same as variable name in the code.  
    ;=====================================================================

       system("/bin/rm -f simple.nc")   ; remove any pre-existing file
       ncdf = addfile("simple.nc" ,"c")  ; open output netCDF file

    ;===================================================================
    ; create global attributes of the file (optional)
    ;===================================================================
       fAtt               = True            ; assign file attributes
       fAtt@title         = "NCL Simple Approach to netCDF Creation"
       fAtt@source_file   =  "original-file.nc"
       fAtt@Conventions   = "None"
       fAtt@creation_date = systemfunc ("date")
       fileattdef( fout, fAtt )            ; copy file attributes

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

    ;===================================================================
    ; output variables directly; NCL will call appropriate functions
    ; to write the meta data associated with each variable
    ;===================================================================
       ncdf->T  = T                          ; 4D               
       ncdf->PS = PS                         ; 3D 

                                             ; say ORO(:,:)
       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                ; name on file different from name in code
There is no need to explicitly copy the coordinate variables or attributes. They are automatically written to the file when the variables are output.


***********************************************************************************
A more elaborate example of using the "simple method" follows.  The classic CESM 
model writes monthly 'time slice' files: for each time step (each month), all variables 
are output.  This can result in many files being created. Typically users want to work 
with all times for one or more selected  variables. Commonly, the netCDF Operator ncrcat 
would be used to perform this task. 
  
NCL can do this also. Using 'method 1', the method_1.slice2ts.ncl script
illustrates how one could optionally write various forms netCDF.  
***********************************************************************************