NCL Website header
NCL Home > Training > Workshops

Interactive text for NCL Workshop

This page contains text for an interactive NCL session that we demonstrate during the first day of the NCL workshop.

You can try this interactive session on your own. To enter an NCL interactive session, type at the UNIX command line:

   ncl
Each NCL interactive line will look like:

  ncl n>

where n is the line number. You can quit out of an NCL interactive session by typing "quit".

At the teacher's instruction or on your own, highlight text with your mouse until the next blank line, copy it, and then paste it into your NCL interactive session to see what happens.


Interactive session 1 - regular variables

;----------------------------------------------------
; Interactive session 1:   variables  
;----------------------------------------------------
s = "Hello World" 
print(s)             ; 'q' to exit the printed info


i = 5                
print(i)             ; display all variable information         


f = 9.3              ; float
print(f)


d = 3.12d            ; double
print(d)


;========  simple array creation


I = ispan(-5, 3, 1)      ; example of a built-in function
print(I)



F = ispan(-10, 4, 2)*1.  ; create an array of floats
print(F)



D = ispan(-10, 4, 2)*1d5 ; create an array of doubles
print(D)


q = fspan(-32.4, 12.789, 5)
print (q)

    

;=========  array + descriptive info [attributes]

t = (/ 18  , 30.2, -999./)      ; manually create an array with 3 elements
print(t)                        ; what type is t 

print(avg(t))                   ; print the average of t

t@_FillValue= -999.             ; indicate that -999 is missing

print(avg(t))                   ; print the average of t again.
                                ; note the value is different

t@long_name = "Temperature"    ; add meta data ["attributes"]
t@units     = "degC"
print(t)

t = (9./5)*t + 32              ; array calculation
print(t)                       ; What is not correct? 

t@units = "Fahrenheit"         ; *USER* responsibility to update attributes
print(t)                       ; (if desired)

;=========

T1 = t                         ; creates variable T1 with attributes
                               ; direct variable-to-variable copy
print(T1)                      ; copies values and meta data

T2 = 0.556*(t -32)
print(T2)                      ; What do you notice about T2 ??
quit                           ; exit interactive mode

Interactive session 2a - coordinate variables

;----------------------------------------------------
; Interactive session 2a:   variables  
;----------------------------------------------------

time       = 1995*100 + ispan(1,12,1)
time@units = "yyyymm"
print(time)

ntim       = dimsizes(time)    ; # of elements in time
print(ntim)

pi         = 4.*atan(1.0)
rad        = pi/180.
t          = 10*sin(rad*ispan(0,165,15)) + 273.15
t@long_name= "temperature"
t@units    = "degK"
print(t)

; ====

t!0        = "time"            ; name the dimension
print(t) 


t&time     =  time             ; assign values to the named dimension     
print(t)                       ; "complete" variable
                                   ; data-object or structure

; ====

printVarSummary(t)             ; overview of variable


; ====
print( t(7:9) )                ; use standard subscripting

print( t({199506:199508}) )    ; use coordinate subscripting  {...}
   

T3 = t({199501:199512:3} )    ; use coordinate subscripting  {...} stride 3
print( T3 )

; ====
print( t&time(::2) )           ; every other point

print( t&time(::-1) )          ; reverse the order    
; ====
                                     ; latitude
nlat       = 4             
lat        = fspan( -10, 20, nlat)
lat@units  = "degrees_north"
lat!0      = "lat"
lat&lat    =  lat 
print(lat)
                                     ; longitude
lon        = (/ -180, 0, 175 /)      ; manually specify
lon@units  = "degrees_east"
lon!0      = "lon"
lon&lon    =  lon 
mlon       = dimsizes(lon)
print(lon)
                                    ; create a complete NCL "data object"
                                    ; some people might call this a "structure"
x  = random_normal( -5, 12, (/ntim,nlat,mlon/) )
printVarSummary(x)

x!0        = "time"             ; assign dimension names
x!1        = "lat"
x!2        = "lon"
printVarSummary(x)

x&time     =  time              ; assign values to the named dimensions
x&lat      =  lat
x&lon      =  lon
printVarSummary(x)

x@long_name= "WhatEver"         ; attributes
x@units    = "whiskers/furlong"
x@info     = "Created for NCL class"
x@creation_date = systemfunc("date")

printVarSummary(x)              ; complete data object

; ==== Do not quit out of this interactive session!
; ==== It is needed for next session.


Interactive session 2b - subscripting, reordering
You must have run the previous interactive session in order to start this one.

;----------------------------------------------------
; Interactive session 2b:   subscripting, reordering
;----------------------------------------------------
; ====
; ====        All the following are "variable-to-variable" transfer
; ====

x2d = x(0,:,:)                       ; dimension reduction
print(x2d)



x3d = x(0:0,:,:)                     ; no dimension reduction
print(x3d)


z  = x(lat|:, lon|:, time|:)         ; dimension reorder
printVarSummary (z)



a  = x(lat|2:3 , lon|:, time|::4)    ; standard subscripting
printVarSummary(a)



b  = x(lon|:, {lat|-5:15}, {time|199502:199510:2})  ; coordinate sub
print(b)


; ====
; ====  reshape
; ====

x1d  = ndtooned( x )                ; 3 dimensions to one dimension    
printVarSummary(x1d)
    
X2D  = onedtond( x1d, (/ntim, nlat*mlon/) ) 
printVarSummary(X2D)

; ====
; ====
; ====
XX  = (/ x /)                        ; the (/ ...  /)  strip meta data
print(XX)                            ; except _FillValue



b5 = 5*b
print(b5)                            ; what do you notice?
    

quit                                 ; exit interactive mode


Interactive session 3 - file I/O

These are commands that you type at the UNIX command line, and not from within interactive NCL. "ncdump" is a tool that comes with the NetCDF software. "ncl_filedump" and "ncl_convert2nc" come with the NCL software.

ncdump -h   NCEP_06_climo.nc

ncl_filedump  MOD05_L2.A2002161.1830.004.2003221153410.hdf | less


ncl_filedump  ruc.grb | less
 
ncl_convert2nc   any_grib_file.grb

ncl_filedump     any_grib_file.nc