Log In | Users | Register
Edit | Attach | New | Raw | Diff | Print | | Tools
You are here: Data » DocTools » ToolsVis » VisNCL » VisNCLBasicExamples

Basic NCL examples

Some basic examples for using NCL that have proven helpful.

Passing arguments to NCL

An elegant and secure way to pass arguments to NCL is to set environment variables and then invoke NCL which reads the variables using the getenv function.

runncl.sh is a general shell script to pass arguments to NCL.

#!/bin/bash
#
# Universal wrapper script for ncl. 
# Pass arguments from the command line to environment variables
#
# version 0.1, Thierry Corti, C2SM ETH Zurich
# 

E_BADARGS=65

if [ ! -n "$1" ]
then
  echo "Usage: `basename $0` script.ncl argument1 argument2 etc."
  exit $E_BADARGS
fi  

# save number of arguments to environment variable NCL_N_ARG
export NCL_N_ARGS=$#

# save command line arguments to environment variable NCL_ARG_#
for ((index=1; index<=$#; index++))
do
  eval export NCL_ARG_$index=\$$index
done   

# run ncl
ncl $1

Usage:

   ./runncl.sh script.ncl argument1 "string argument"

Sample application:

#!/bin/bash
#
# Use "runncl" shell script to pass different arguments to a NCL script

for YEAR in {2000..2005}
do
  echo "---------- starting $YEAR"
  ./runncl.sh runwithargs.ncl $YEAR
done  

Same script, but now runncl.sh runs in parallel !

#!/bin/bash

for YEAR in {2000..2005}
do
  echo "---------- starting $YEAR"
  # start runncl.sh and send it to the background
  ./runncl.sh runwithargs.ncl $YEAR &
done  

In NCL, the arguments can be retrieved using "getenv", optionally accompanied by a check for the right number of arguments. Here is an example:




        Error: Unsupported content type:  (Must be =text/html=, =text/plain= or =text/css=) 

Map with country borders

By default, NCL country borders are pretty outdated. This can be solved by choosing the more recent, high resolution boundary data set, as now documented in the maps example 2 on the official NCL webpage

  res@mpDataBaseVersion = "Ncarg4_1"            ; choose more recent database
  res@mpDataSetName = "Earth..4"                ; high resolution 

More than country borders - Natural Earth

Natural Earth is a public domain map dataset with cultural and physical features. The script below demonstrates how to use this data in NCL.

naturalearth.png




        Error: Unsupported content type:  (Must be =text/html=, =text/plain= or =text/css=) 

Reading Arc/Info ASCII Grid Files

NCL is rather weak when it comes to reading ASCII formats. Your best bet is to preformat your data into a very simple, regular ASCII file and take a look at some examples on the official website. Here is a function to read lat/lon Grid files using the asciiread command.




        Error: Unsupported content type:  (Must be =text/html=, =text/plain= or =text/css=) 

If you have vector data instead of raster data, from Arc/Info, your best bet is probably to use the widely known shapefile format which can be read by NCL (see documention for more information).

Attach
png
maponly_2.png (40.24K)
version 1 uploaded by ThierryCorti on 20 Jan 2011 - 09:30
maponly_new2.ncl
version 1 uploaded by ThierryCorti on 20 Jan 2011 - 09:26
png
version 1 uploaded by ThierryCorti on 01 Feb 2011 - 11:53
naturalearth_v01.ncl
version 1 uploaded by ThierryCorti on 01 Feb 2011 - 11:57
readAGR.ncl
readAGR.ncl (1.51K)
version 1 uploaded by ThierryCorti on 19 Jan 2011 - 16:54
txt
version 1 uploaded by ThierryCorti on 15 Feb 2011 - 15:30
runwithargs.ncl
version 1 uploaded by ThierryCorti on 15 Feb 2011 - 15:39
spacer
This site is managed by the Center for Climate Systems Modeling (C2SM).
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors. Ideas, requests, problems? Send feedback!