ncar command language (ncl) · files. ! ncea averages over files and keeps time record unchanged...

37
NCAR Command Language (NCL) 10 Feb 2015

Upload: others

Post on 20-Sep-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

NCAR Command Language (NCL)

10 Feb 2015

Page 2: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

NetCDF: Network Common Data Format q Network Common Data Form (netCDF) is a community standard for

sharing scientific data. Data in netCDF format is:

v  Self-Describing; A netCDF file includes information about the data it contains.

v  Portable; A netCDF file can be accessed by computers with different ways of storing integers, characters, and floating-point numbers.

v  Scalable; Small subsets of large datasets in various formats may be accessed efficiently through netCDF interfaces, even from remote servers.

v  Appendable; Data may be appended to a properly structured netCDF file without copying the dataset or redefining its structure.

v  Sharable; One writer and multiple readers may simultaneously access the same netCDF file.

v  Archivable; Access to all earlier forms of netCDF data will be supported by current and future versions of the software.

Page 3: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

netCDF File Header dimensions: lon = 360 ; lat = 180 ; nbnds = 2 ; time = UNLIMITED ; // (1046 currently)

variables: float lat(lat) ; lat:units = "degrees_north" ; lat:long_name = "Latitude" ; lat:actual_range = 89.5-89.5 ; lat:standard_name = "latitude_north" ; lat:axis = "y" ; lat:coordinate_defines = "center" ; float lon(lon) ; lon:units = "degrees_east" ; lon:long_name = "Longitude" ; lon:actual_range = 0.5359.5 ; lon:standard_name = "longitude_east" ; lon:axis = "x" ; lon:coordinate_defines = "center" ; double time(time) ; time:units = "days since 1800-1-1 00:00:00" ; time:long_name = "Time" ; time:actual_range = 6939576710 ; time:delta_t = "0000-00-07 00:00:00" ; time:avg_period = "0000-00-07 00:00:00" ; time:standard_name = "time" ; time:axis = "t" ; double time_bnds(nbnds, time) ; time_bnds:long_name = "Time Boundaries" ; short sst(lon, lat, time) ; sst:long_name = "Weekly Mean of Sea Surface

Temperature" ; sst:valid_range = -540 ; sst:actual_range = -1.835.63 ; sst:units = "degC" ; sst:add_offset = 0 ; sst:scale_factor = 0.01 ; sst:missing_value = 32767 ; sst:precision = 2 ; sst:least_significant_digit = 2 ; sst:var_desc = "Sea Surface Temperature" ; sst:dataset = "NOAA Optimum Interpolation (OI) SST V2" ; sst:level_desc = "Surface" ; sst:statistic = "Weekly Mean" ; sst:parent_stat = "Individual obs" ; sst:standard_name = "sea_surface_temperature" ;

// global attributes: :title = "NOAA Optimum Interpolation (OI) SST V2" ; :Conventions = "CF-1.0" ; :history = "Created 10/2002 by RHS" ; :comments = "Data described in Reynolds, R.W., N.A. Rayner, T.M. Smith, D.C. Stokes, and W. Wang, 2002: An Improved In Situ and Satellite SST Analysis for Climate, J. Climate" ; :platform = "Model" ; :source = "NCEP Climate Modeling Branch" ; :institution = "National Centers for Environmental Prediction" ; :references = "http://www.emc.ncep.noaa.gov/research/cmb/sst_analysis/ http://www.cdc.noaa.gov/cdc/data.ncep.oisst.v2.html"

Dimensions

Coordinate Variables

Variable Attributes

Global/File Attributes

Variables

Attributes: Descriptive info about a variable or file. T@long_name = “Temperature”

Page 4: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Displaying netCDF Files q netCDF data can be directly displayed on screen.

Page 5: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Resources to Process NetCDF Data q UNIX & Mac:

NetCDF Library (ncdump: for content of nc files)

NetCDF Operators (NCO) for file/variable processing

NCAR Command Language (NCL)

ncview (stampede), Panoply(Mac): view NetCDF files

vi/emacs/Aquaemacs File editor q  For Mac, I can install all software needed for data processing. q On stampede you may add these modules in your .login file.

if ( -s /usr/local/etc/login ) then! source /usr/local/etc/login!endif!module load ncl_ncarg/6.2.1!module load cmake/2.8.9!module load perl/5.16.2!module load hdf5/1.8.9!module load gsl!module load netcdf/3.6.3!module load nco!module load udunits!module load ncview!

Page 6: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

ncra record (time) average

ncea ensemble average

ncrcat record (time) concatenate

ncecat ensemble concatenate

ncrename rename variables, dimensions and attributes

ncatted attribute editor

ncwa weighted average

ncdiff difference

ncflint file linear interpolator

ncks kitchen sink

NCO: NetCDF Operators

Page 7: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Hyperslabs

Longitude

Leve

l

Longitude

Leve

l

A hyper-slab is a subset of data.

Page 8: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

NCL Common Options -v (variable) Operates only on listed variables separated by comma.

ncks -v T,U,V in.nc out.nc!

-x (exclude) Operates on all variables except listed after -v.

ncks -x -v CHI,PSI in.nc out.nc!

-d (dimension) Operates on a hyperslab of data

ncks -d lon,340.,50. -d lat,10.,35. in.nc out.nc!

v  Integer numbers indicate array indexes

v  Real numbers indicate actual coordinate values

-F Fortran indices starting from 1 (default is 0)!

-O Overwrite output file (if exists)!

-o output-file name!

-A Append variable to existing output file !

-H print data on screen, otherwise data go into out file !

Page 9: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

ncra: NetCDF Record (time) Averaging

q  Averages record variables across an arbitrary number of input files.

q  The record dimension (e.g. time) is retained as a degenerate (size 1) dimension.

q  Weights each record in the input files equally !

Let 85.nc to 89.nc files (5-yrs) each having 12 months.!

ncra 85.nc 86.nc 87.nc 88.nc 89.nc 8589.nc; output time=1!

ncra 8[56789].nc 8589.nc ; output time=1!

!

let V1(12,64,128), V2(24,64,128)!

ncra V1.nc out1.nc ;average all 12 times => time dimension=1!

ncra V1.nc V2.nc ;average all 36 times => time dimension=1!

ncra -F -d time,12,14 85.nc 86.nc 87.nc 8512_8602.nc 87 no use!

ncra -F -d time,3,,12 –v T 85.nc 86.nc 87.nc 858687_03.nc;march!

ncra -d time,6.,54. ??.nc 8506_8906.nc; 85,86,87,88,89 60 month!

Page 10: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

ncea: Ensemble Average (grid point average)

q  Performs grid point averages across an arbitrary number of input files.

q  ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1.

q  Each file is weighted evenly.

q  Each variable in the output file will be the same size as in any input file.

q  Coordinate variables are not averaged (time dimension will be same across all input files).

!

average three ensemble runs of 85, each having 12 months!

ncea 85_01.nc 85_03.nc 85_04.nc 85.nc !

ncea 85_0[1-5].nc 85.nc (output time dimension also 12)!

!

add hyperslab to above example; variable extracted for first three times over tropics!

ncea –d time,0,2 –d lat,-23.5,23.5 85_??.nc 85.nc !

Page 11: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Appending vs Concatenation

q Appending is the merging of file variables:

Ø  file1 = T,U,V

Ø  file2 = PSI,CHI

Ø  file3 = T,U,V,PSI,CHI

q Concatenation is the combination of variables along a record (time) dimension:

q  file 1 = T(0:12,:,:)

q  file 2 = T(13:24,:,:)

q  concatenated file = T(0:24,:,:)

Page 12: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

ncrcat: Record Concatenator q  Concatenates (join) record variables (time) across an arbitrary number

of input files. Unix wild characters (*+?. etc.) allowed.

q  Final record dimension is the sum of the lengths of the input files.

q  Input files may vary in length, but must have a record dimension. Record coordinate, if present, be monotonic.

q  concatenate 85 to 89 data into 8589!ncrcat 85.nc 86.nc 87.nc 88.nc 89.nc 8589.nc !

ncrcat 8[56789].nc 8589.nc (output time dimension 60)!

q  join data from dec 85 to feb 86 only!ncrcat -d time,11,13 85.nc 86.nc 87.nc 8512_8602.nc!

ncrcat -F -d time,12,14 85.nc 86.nc 87.nc 8512_8602.nc!

q  concatenate March Temperature data from 85 to 89!ncrcat -F -d time,3,,12 –v T 85.nc 86.nc 87.nc 858687_03.nc!

q  concatenate data from 06/85 thru 06/89 !ncrcat -d time,6.,54. ??.nc 8506_8906.nc!

Page 13: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

ncecat: Ensemble Concatenate q  Combines an arbitrary number of input files into a single output

file. Wild characters allowed.

q  Each input file is stored consecutively as a single record in the output file.

q  Input files are glued together by the creation of a record dimension “record”.

!Consider 5 realizations of year 85 simulation each time=12.!

ncecat 85_01.nc 85_02.nc 85_03.nc 85_04.nc 85_05.nc 85.nc!

ncecat 85_0[1-5].nc 85.nc; output file has dimension record=5!

!

Page 14: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Renames variables names (-v), dimensions names (-d) and attributes names (-a)

ncrename !

-a old_att,new_att!

-d old_dim,new_dim!

-v old_var,new_var !

in.nc [out.nc]!

rename variable p to pres, t to T !

ncrename –v p,pres –v t,T in.nc out.nc!

rename attributes names missing_value and Zaire!

ncrename –a missing_value,_FillValue –a Zaire,Congo in.nc out.nc!

rename dimensions names latitude and longitude!

ncrename –d longitude,lon –v longitude,lon –v rh,rhum in.nc out.nc!

ncrename: Renamer

Page 15: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

ncatted: netCDF Attribute Editor ncatted –a att-dsc in.nc (works on one file at a time)

att-dsc: att-nm, var-nm, mode, att-type, attval!

v  att-nm: attribute name to edit!

v  var-nm: variable name to edit!

v  mode: d=delete, a=append, c=create, m=mod, o=overwrite!

v  att-type: f=float, d=double, l = long, s=short, c=char!

v  att-val: new value od attribute!

!

append “Data version 2” to global attribute history.!

ncatted –a history,global,a,c,”Data version 2\n” in.nc!

modify all existing “units” to “meter second-1”!

ncatted –a –a units,,m,c,”meter second-1” in.nc!

change long_name from for variable “T” to “temperature”!

naatted –a long_name,T,o,c,temperature in.nc!

assign _FillValue attribute to 1.0e36 for all variables !

ncatted -a _FillValue,,m,f,1.0e36 in.nc !

Page 16: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

ncdiff: Difference ncdiff options File1 File2 File3!

Ø  ncdiff subtract variables in file 2 from file 1 and store results in file 3

Ø  Common dimensions must be the same size.

Ø  For anomalies, the time dimension of the mean file be removed.

Ø  File2 should be a subset of File1 if they are not identical !

85_0112.nc and 86_0112.nc contain 12months of data!

ncdiff 86_0112.nc 85_0112.nc 86m85.nc; difference of 86 & 85!

monthly anomalies of 85.nc, first get annual average 85!

ncra 85_0112.nc 85.nc!

get rid of time dimension 1!

ncwa –O -a time 85.nc 85.nc!

compute anomalies for 1985 for T!

ncdiff –v T 85_0112.nc 85.nc t_anm_85_0112.nc!

!

Page 17: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

ncwa: Weighted Average q  Averages variables in a single file over arbitrary dimensions

q  Options for weights, masks and normalizations

q  Default behavior arithmetically average every variable over all dimension to produce a scalar quantity for each.

ncwa options!

-a average over subset dimensions (separated by comma)!

-w lat_wgt provide weights in latitude dimension!

-B mask_condition, mask is 0 for ocean, 1 for land!

-N turn-off normalization or average, it integrate or sum quantities. -N option integrate quantity, instead of averaging, xavg = sum(xw)/sum(w), but it returns sum(wx) without normalizing by sum(w)!

!

Page 18: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

ncwa: Weighted Average store zonal average of all variables in in.nc to out.nc!

ncwa –a lon in.nc out.nc!

area mean average over tropical Pacific!

ncwa –a lat,lon –w gw –d lat,-20.,20. –d lon,120.,270 in.nc out.nc !

adding -B ’ORO < 0.5’ to above will only average over ocean point excluding all islands where mask >= 0.5!

ncwa –a ‘ORO < 0.5’ –a lat,lon –w gw –d lat,-20.,20. –d lon,120.,270 in.nc out.nc!

total area of the maritime tropical Pacific!

ncwa -N -v area -B ’ORO < 0.5’ -a lat,lon -d lat,-20.0,20.0 !

-d lon,120.0,270.0 in.nc out.nc!

Page 19: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

ncflint: File Linear Interpolator q  Creates output file that is a linear combination of input files

q  Linear combination can be weighted average, normalized averaged or interpolation of input files.

q  Options: -i var,val interpolation variable and value

-w wt1[,wt2] weights of files

q  output file variables are calculated using:

val3 = wgt1*val1 + wgt2*val2, if only wgt1 is given then wgt2=1-wgt1, if no weight is defined, then wgt1=wgt2=0.5 consider files 85.nc & 87.nc with time = 85 and time = 87; to linearly interpolate for time 86! ncflint -i time,86 85.nc 87.nc 86.nc !Interpolate Mar from Jan and Arp data! ncflint -w 0.667 85_01.nc 85_04.nc 85_02.nc! ncflint -w 0.667,0.333 85_01.nc 85_04.nc 85_02.nc!find sum and difference of 85 and 86! ncflint -w 1,1 85.nc 86.nc 85p86.nc! ncflint -w 1,-1 85.nc 86.nc 85m86.nc!output tst.nc be identical to input 85.nc! ncflint -w 3,-2 85.nc 85.nc tst.nc!

Page 20: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

ncks: Kitchen Sink q  ncks is a versatile utility that combines most features of ncdump

with extraction, hyper-slab and subset.

q  It extracts data from input file and write to output netCDF, to binary file or print on screen.

!

extract T,P data to output file!

ncks –v T,P in.nc out.nc!

view one variable over a dimension subset (no output file)!

ncks –H –v Pressue –d time,0,11 in.nc!

extract data from lon 260. to 45. to an output file!

ncks –d lon,260.,45. in.nc out.nc!

view contents of a netcdf file!

ncks in.nc | more!

copy in.nc to out.nc!

ncks in.nc out.nc!

!

Page 21: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

pdf

Vis5D

png

Shapefiles

NCL File IO

Page 22: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

NCL Supported Formats netCDF-3/4 [network Common Data Form] HDF4/H5 [Hierarchical Data Format (Scientific Data Set only) ] HDF-EOS [Earth Observing System; HDF4 and HDF5] GRIB-1/2 [Grid in Binary; WMO standard; NCEP, ECMWF] CCMHT [CCM History Tape; COS blocked only; ccm2nc] Shapefile [ESRI: geospatial vector data format GIS] Binary -  sequential [F: open(_,form="unformatted", access="sequential")] -  direct [F: open(_, form="unformatted", access=direct",recl=_)]

[C: write]

Page 23: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Numeric Types q  NCL includes the standard types found in nearly every programming language. q  Basic numeric types: double, float, long, integer, short, byte. q  Numeric types support all of algebraic functions available in NCL.

Non-numeric Types q  Non-numeric types have no numerical value and cannot be coerced (pushed) into

a numerical type. q  Types: character, string, logical (True/False), file, list, graphic. q  Strings use “+” operator to join one or more strings.

first = “John”; last=“Kennedy” print( first + “ “ + last) è “John Kennedy”

q  logical types supports logical operators .and., .or., .not. etc. amazon_region = lat .ge. -70 .and. lat .le. -50. .and. lon .ge. -10.0 .and. lon .le. 0.0

Page 24: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Numeric Types Type size Mini Max Literal

suffix double 64 +/- 2.22507E-308 +/- 8.98846e+307 d or D

int64 64 -9,223,372,036,854,775,808 9,223,372,036,854,775,807 q

uint64 64 0 18,446,744,073,709,551,614 Q

long 64 -9,223,372,036,854,775,808 9,223,372,036,854,775,807 l

ulong 64 0 18,446,744,073,709,551,614 L

float 32 +/-1.175494e-38 +/-1.701411e+38

long 32 -2,147,483,648 2,147,483,647 l

ulong 32 0 4,294,967,295 L

integer 32 -2,147,483,648 2,147,483,647 i

uint 32 0 4,294,967,295 I

short 16 -32768 32767 h

ushort 16 0 65535 H

byte 8 -128 127 b

ubyte 8 0 255 B

x1 = 3.3d x2 = (/23H, 33H, 145H/)!

Page 25: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Variables q  Variable name must begin with an alphabetic character or underscore “_”

Ø  may contain any combination of numeric and alphabetic characters. Ø  may have metadata (information about data) attached to the variable. Ø  metadata may be accessed created, changed and deleted via NCL

functions. Ø  variables are imported via NCL’s addfile/addfiles function and metadata

automatically attached to the variable. f = addfile(“foo.nc”, “r”)! temp = f -> T ; temp(time,lat,lon)

Named dimensions (var ! integer) temp!0 is first named dimension (“time”) temp!2 is third named dimension (“lon”)

Coordinate variables (variable & named_dimension) temp&time is time coordinate array attached to variable temp&lat is latitudes coordinate attached to variable

Variable attributes (var @ attribute) temp@units is units attribute for variable f@author is attribute author for file

Page 26: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Expressions q  Algebraic Operators:

+ addition - subtraction •  multiplication ^ exponent % modulus; print (17%3)=>2 # matrix multiplication > greater than < less than

q  Logical Operators: .lt. less than .gt. greater than .le. less than .or. equal to .ne. not equal to!.eq. equal to .and. and .or. or .xor. exclusive or .not. not

do var = start, end, stride [optional]![statement]!

end do!!

do while (scalar_logical_expression) !![statement ]!

end do!

Loops

Page 27: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

IF statements if (scalar_logical_expression) then ! [statement (s)]!end if!if (scalar_logical_expression) then!

[statement (s)!else!

[statement (s)]!end do!q  There is no explicit “else if” but nested if

statement can be used. if (conditionA) then! A_statement!else ! if (conditionB) then!

B_statement! else ! C_statement! end if ; B & C!end if ; A!

if (lat .ge. -70. .and. \! lat .le. -50. .and. \! lon .ge. -10. .and. \! lon .le. 0.0 ) then!

[statement(s)]!end if!

Page 28: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Dimensions & Subscripting q  Two types of array subscripting: standard and coordinate

Standard subscripting q  Scripting similar to C. Indices start at 0 and end at N-1.

v  Rightmost subscript varies fastest; T(time, lat, lon) v  Scripts form: start_index : end_index : optional_stride. v  Omission of start_index is 0. v  Omission of end_index is N-1, v  Default optional_stride is 1. v  : without any value means all elements.

q  Examples: Let T(ntim, nlat, nlon) T(:,:,:) is same as T with all dimension. T(0, :, ::5), first time, all latitudes and every 5th longitude

T(:1, ::-1, 4:50) first 2 times, reverse lat, lon indices 4 to 50.

Page 29: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Coordinate Subscripting q  1-D array of monotonically increasing or decreasing values where the

variable name and associated dimension name are same. For examples: time(time), lat(lat), p(p) etc. o  coordinate subscripting is invoked by enclosing natural

coordinates between curly braces “{…}” o  X = T(:, {-20:20}, {90:290:2}), all times, -20 to 20 latitude, 90 to 200

longitude with side 2.

Page 30: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Array Creation q  Arrays can be imported from a file.

f = addfile(“foo.nc”, “r”) t = f->T (T is a variable on file references by f).

function addfile ( file_path: string, status:string); status “r”:read; “w”:write; “c”:create return value is a file

q  t = f->T will read entire array of disk file f, into computer memory with metadata. t = (/ f->T /) will copy only data into memory without metadata.

q  Arrays can be created using array constructor (/ … /) x_1d = (/ 1, 2, 3, 4/) x_2d_2x3 = (/ (/1.3, 4.5, 6.7/), (/3.4, -2.4, 9.23/) /) a_string = (/”a”, “b”, c”/)

q  New arrays can be created and memory allocated using new statement. function new ( dimension_size[*]:integer, vartype:string, [ default_value] ) a = new ( 3, float) b = new ( (/18, 64, 128/), double, 1.e20); default value is 1.0e+20 s = new (100, string) ; default value is “missing”

Page 31: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

NCL Arrays q  Zero-based indexing, similar to C. q  Data type of each operand be equivalent. Let A and B be of size (12,64,128)

C = A + B ; element addition D = A - B ; element subtraction E = A * B ; element multiplication C, D and E will be created automatically.

q  Scalar values appears in a expression with array, scalar value is applied to each element of array.

F = 2*D + 2 each element of E will be multiplied by 2 and then 5 will be added to each element.

q  function dimsizes returns dimension sizes of input variable. If T(18,64,128) then

dimsizes (T) è (/18, 64,128) function typeof returns string name of type of input variable.

typeof (T) è “double” function sizeof returns total size of input variable in bytes.

sizeof(T) è 1,179,648 (=18*64*128*8)

Page 32: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Reshaping Arrays q  Function ndtooned ( nD to 1D) convert multi-dimensional arrays to 1D.

For T(18,64,128), T1 = ndtooned(T) will convert T into 1-D array of size (18*64*128 = 147,456) elements. q  Similarly, function onedtond will convert 1D array to multi-dimension.

a = (/ 1,2,3,4,5,6,7,8/) b = onedtond(a, (/2,4/) ) will make b of size 2 rows and 4 columns.

q  Another function reshape will convert a multi-dimensional array to another multi-dimensional array.

x = random_unform(-100,100, (/10,20,30/)) will generate 3d array size(10,20,30) of random numbers between -100 & 100. x1 = reshape (x, (/60,100/) ) will reshape array to 2D with same number of elements.

Page 33: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Dimension Reduction q  When a constant subscript is specified, dimension is reduced, i.e, rank of array

is reduced. Assume T(ntim, nlev, nlat, nlon) then: T1 = T(5, :, 12, : ) ; T1(nlev, nlon) is 2D instead of 4D. T2 = T (:, :, :, 0) ; T2(ntim, nlev, nlat) is 3D

Meta data of degenerated dimensions is not copied to new variable.

q  To avoid reduction of dimension and lose of metadata, constant value may be represented in array scripts.

In T1(5, :, 12,: ), 5 may be written as 5:5, and 12 as 12:12. Now T1(5:5, :, 12:12, : ) is still a 4-D array with metada for all four dimensions.

Page 34: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Basic Math Functions fabs(v): absolute value of data array=(/-3.5,-1.0,0,2.4,3.9/)!a0=fabs(array))=>(/3.5,1,0,2.4,3.9/)!

log(v): natural log of data log10(v): log base 10 of data if value is 0.0, -inf is returned if value is < 0.0, nan (not-a-number) is returned a1=log(3.4) => 1.223776!a2=log(3.4d) => 1.223775431622116!a3=log10(3.4)=> 0.5314789!

exp(v): e raised to the power of data b1=exp(3.4) => 29.9641!b2=exp(3.4d )=> 29.96410004739701!

floor(v): largest integer smaller than value ceil(v): smallest integer larger than value c1=floor(6.4) => 6!c2=ceil(6.4) => 7!

round(v, opt): nearest whole number opt=0; return same type as input opt=1; return values of type float opt=2; return values of type double opt=3; return values of type integer z=round(6.499,1)=> 6!

sin(v): sine of value (in radians) cos(v): cosine of value (in radians) tan(v): tangent of value (in radians) a1=sin(0.5) => 0.4794255!a2=cos(-1.0)=> 0.5403023!a3=tan(5.6) => -0.823934!

asin(v): inverse sine of value acos(v): inverse cosine of value atan(v): inverse tangent of value atan2(y,x): inverse tangent (y/x) of value a1=asin(-1.0) => 1.570796!a2=acos(-1.0) => 3.141593!a3=acos(-1.0d) => 3.141592653589793!a4=4.*atan(1.) => 3.141593!a5=atan2(1,0) => 1.570796 (pi/2)!

sqrt (v): square root of input (v >= 0.0) a=sqrt(3.5)=> 1.870829!

min (v): minimum value of an nD array minind (v): index of first occurrence of min value max (v): maximum value of an nD array maxind (v): index of first occurrence of maximum value

Page 35: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Basic Math Functions sum(v): sums the input x = (/1,-999,3,4,-999,6/)!print(sum(x)) => -1984!x@_FillValue=-999!print(sum(x)) => 14!

product(v): product of the input x = (/1,-99,3,4,-99,6/)!b1= product(x) => 705672!x@_FillValue=-99!b2= product(x) => 72!

ispan(istart, iend, istride): generate array of equally spaced integers fspan(fstart, fend, npts): generate array of equally spaced floats/doubles ispan(-100,100,20)=>(/-100,-80,-60,-40,-20,0,20,40,60,80,100/)!fspan(0,100,11)=>(/0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100./)!

num(v): count number of True values a = (/1,2,3,4,5/)!print(num (a.gt.3)) => 2!

any(v): True if any input value is True a = (/1, 2,-999,4,5/)!print( any (a.ge.4)) => True!print( any (a.lt. 0)) => False!

ind(v): indices where input is True a = (/1, 2,-7,4,-11,5/)!; take product of +ve values!ia=ind(a.gt.0) => (/0,1,3,5/)!print(product(a(ia))) => 40 !

all(v): True if all input values are True a = (/1, 2,-999,4,5/)!print( all (a.ge.0)) => False!a@_FillValue = -999!print(all(a.ge.0)) => True!print(all(a.gt.0.and.a.lt.6)) => True!

where(cond, true_value, false_value): array assignment based on conditional array xsqrt = where(x.ge.0, sqrt(x), x@_FillValue)!;oro=1 for land and oro=0 for water!;Convert temperature from Centigrade to Kelvin or Fahrenheit scale.!x = where(oro, T+273.14, 1.8*T+32)!

qsort(v): sprt 1D array!x=(/4.3,0.9,5.2,7.7,2.3, -4.7,-9.1/)!x1=qsort(x)=> (/-9.1,-4.7,0.9,2.3,4.3,5.2,7.7/)!

Page 36: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Basic Math Functions variance(v): unbiased estimate of variance stddev(v): sample standard deviation variance = (x-xavg)^2/(n-1)!stddev = sqrt(variance)!!f=(/7,9,-2,-8,2/)!fsum = sum(f) => 8.0!nf = dimsizes(f) => 5!favg = avg(f) => 1.6!fdev=f–favg =>(/5.4,7.4,-3.6,-9.6,0.4/)!fdev2=fdev^2=(/29.16,54.76,12.96,92.16,0.16/)!pvar=sum(fdev2)/nf => 37.84!svar=sum(fdev2)/(nf-1) => 47.3!print(variance(f)) => 47.3!print(stddev(f)) => 6.8775 [sqrt(47.3)]!

avg(v): average value of input a = (/1, 2,3,4,5,6,7,-999,9/)!a@_FillValue = -999!print(avg(a)) => 4.625!print(sum(a)) => 37!print(num(.not.ismissing(a)) =>8 !

cumsum(v, opt): cumulative sum. opt=0, no additional sum after _FillValue opt=1, set missing value to _FillValue and continue summing. opt =2, Treat missing value as 0. a = (/1,2,-999,4,5/)!x_sum(x) => 15!x_cumsum(x,0) => (/1,3,-999,-999,-999)!x_cumsum(x,1) => (/1,3,-999, 7, 12/)!x_cumsum(x,2) => (/1,3,3,7,12/)!

Page 37: NCAR Command Language (NCL) · files. ! ncea averages over files and keeps time record unchanged where ncra averages over all times, reducing time dimension to 1. ! Each file is weighted

Basic String Functions str_upper: converts inputs to uppercase str_lower: converts inputs to lowercase str_switch: switch case of inputs str_capital: capitalize all words a=“Hello World”!a1=str_lower(a) => “hello world”!a2=str_upper(a) => “HELLO WORLD”!a3=str_switch(a) => “hELLO wORLD”!a4=str_capital(s) => “Hello World”!

str_concat:concatenate strings to single + sign can also be used to join strings str_join: join strings separated by delimiter to one string onDtostring: converts 1D strings to single b=(/“Hello”,”World”/)!b1=str_concat(b) => “HelloWorld”!b2=str_join(b, “ “) => “Hello World” !b3=str_join(b,”_”) => “Hello_World”!x=(/1960,1970,1980/)!xstr=oneDtostring(x)=>”1960,1970,1980”!b=“hello”!print(b+” “+b) => “hello hello”!

str_left_strip: strips leading spaces & tabs str_right_strip: strips ending spaces & tabs str_strip: strips leading/ending spaces & tabs str_squeeze: strips leading/ending spaces & replaces multiple spaces/tabs to single s=“ abc def “!s1=str_left_string(s) =>“abc def “!s2=str_right_string(s)=>” abc def”!s3=str_strip(s) =>“abc def”!s4=str_squeeze(s) =>“abc def”!

strlen: length of string s=(/”Hello“,”World!”/)!n1=strlen(s)=> (/5,6/)!

isStrSubset (str, substr):check if string is subset of other aob=“apple_orange_banana”!o=“orange”!tf=isStrSubset(aob,o)=> True!

str_index_of_substr(str, substr, opt): staring indices of occurrences of substring in a string opt=-1; return only last match opt=0; return indices of all matches opt=n; return only n indices of matches aob=“ apple orange banana apple”!a=“apple”!n=str_index_of_substr(aob,a,0)!

! ! =>(/1,21/)!!