NCL Home> Application examples> Data Analysis || Data files for some examples

Example pages containing: tips | resources | functions/procedures

NCL system interactions

NCL can interact with the system via the systemfunc, and getenv functions and the system procedure. These provide users with the ability to retrieve system related information and to execute shell commands or other software tools [i.e. netCDF operators (NCO) , Climate Data Operators (CDO), convert (ImageMagick), wgrib, wget].

The following illustrates using NCL in a manner commonly accomplished via shell scripts. The script (a) creates a string containing the command t be passed to the system; (b) prints (echos) the string; and, (c) passes the string to the system to be executed.

  1. Retrieve an environment variable (getenv).

  2. For each year, (a) retrieve the time via systemfunc, and (b) create a string containing a command to be executed by system. The command "msrcp" results in 12 monthly files being retrieved from the NCAR Mass Storage Systen (MSS) for each year.

  3. Create a string that results in the netCDF operator "ncrcat" being invoked to select one variable from each file and create a new file.

  4. Create a string to delete the the original 12 files.

begin
  nyrStrt = 300                         
  nyrLast = 999 

  mssi= getenv("MSS_PATH") ; retrieve MSS environment variable

  fili= "b20.007.pop.h.0"               ; first part of file name
  diri= "/ptmp/foo/"                    ; where to put msrcp  data (input)
  diro= "/ptmp/foo/output/"             ; where to put ncrcat data (outout)
  filo= "b20.TEMP."                     ; rename output files (root name)

  do nyear=nyrStrt,nyrLast ; each year has 12 files 
      wallClock = systemfunc("date") ; retrieve wall clock time
      print("year="+nyear+"   "+wallClock)

                                             ; create string for MSS comman
      msscmd= "msrcp -n 'mss:" + mssi + fili + nyear + "-[0-1][0-9].nc' "+ diri+"."
      print (" msscmd= "+ msscmd)
      system (msscmd)

                                             ; netCDF operatoer to extract and concatenate
      ncocmd= "ncrcat -v TEMP " + diri + fili + "*.nc "+diro+filo+nyear+".nc"
      print ("ncocmd = "+ncocmd)
      system (ncocmd)
                                             ; remove the temporary files
      rmcmd = "/bin/rm -f "+diri+fili+nyear+"*.nc"
      print ("rmcmd ="+ rmcmd)
      system (rmcmd)

  end do

end
Be sure to also see the Command Line Options section in the NCL Reference Manual.

The following illustrates passing a string to NCL from within a C-shell script?

Loop over netCDF files beginning with f40 and pass each file name onto NCL:


#!/bin/csh 
# 
foreach i (f40*.nc)
   echo $i
   ncl fname=\"{$i}\" foo.ncl
end

Another simlar example.

%> cat test.ncl print(inFile) print(outFile) ======================= Then, in the shell script: (a) To escape the shell's interpretation of the double-quote ("), use the backslash prior " (b) Use the curly brackets ({... }) to delineate the text associated with the environment variable name. % set outFile = "test-out" % echo $outFile test-out % ncl inFile=\"{$outFile}.tmp.nc\" outFile=\"{$outFile}.nc\" test.ncl [snip] Variable: inFile Type: string [snip] (0) test-out.tmp.nc

[snip] Variable: outFile Type: string [snip] (0) test-out.nc

The following are simple working scripts. They were not developed for pedantic purposes.

system_1.ncl: This invokes the netCDF operators (ncra and ncea) to create individual monthly, seasonal and annual files for all variables on a source year-month file. The 12 monthly files are used to create the seasonal and annual files.

system_2.ncl: This accesses a number of tar files containing GRIB. It creates the following string

wgrib -s anl_p25.1990010100 | \
     egrep "(:UGRD:200 mb|:VGRD:200 mb|:UGRD:850 mb|:VGRD:850 mb)" | \
     wgrib -i -grib anl_p25.1990010100 -o FOO.grb

This requires that a double quote character be passed to the system. Special care must be taken when doing this. (See the NCL FAQ: including the double quote (") character in an NCL string.) The final step is to remove unwanted files.
system_3.ncl: A large number of GrADS files are converted to netCDF via the Climate Data Operator: cdo -f nc import_binary ...
system_4.ncl: A large number of netCDF files are detrended via the Climate Data Operator: cdo detrend ...
system_5.ncl: Ascii (.gz) files are accessed and downloaded from the World Ocean Atlas 2009 (WOA09) ftp area using wget. They are made into netCDF files.
system_6.ncl: Use NCL to generate a netCDF Operator 'ncap2' command that will be sent to the system for execution. The purpose was to convert all type 'double' variables of rank 2 or greater to type float. There were many variables on the source file that fit these criteria.