NCL version 6.4.0 Karin Meier-Fleischer, DKRZ uint 32 bit; unsigned a1 = a(3) ; a1 is 4 February 27, 2017 Mary Haley, NCAR ulong 32 bit or 64 bit; unsigned a2 = a(0:2) ; a2 contains 1,2,3 ushort 16 bit; unsigned a3 = a(0:4:2) ; a3 contains 1,3,5 ubyte 8 bit; unsigned Non-numeric a4 = a(1:4:-1) ; a4 contains 5,4,3,2 Syntax characters string a5 = a(:3) ; a5 contains 1,2,3,4 = assignment syntax character a6 = a(5:3) ; a6 contains 6,5,4 graphic a7 = a(::-1) ; reverse a 6,5,4,3,2,1 := reassignment operator file ; starts a comment logical /;…;/ starts a block comment Named dimensions list @ create or reference an attribute The dimensions of an array are numbered from 0 to n-1. To attach ! create or reference a named dimension a name to an array dimension, use the ! character. & create or reference a coordinate variable Variables $...$ enclose strings when importing or exporting Assign a variable varNew!0 = "time" variables via addfile varNew!1 = "lev" {…} subscript arrays using coordinate values x = 1 ; integer varNew!2 = "lat" […] subscripts variables of type list y = 2.6 ; float varNew!3 = "lon" (/…/) array constructor d = 20.d ; double [/…/] list constructor str = "This is a string" ; string Named subscripting : array syntax delimiter res = True ; logical (True/False) | separator for named dimensions Named dimensions allow you to reorder and subscript arrays. a = (/1,2,3,4/) ; integer array \ continuation character for wrapping long lines pres(lat,lon) ; lat=21, lon=40 b = (/2,7.0,4./) ; float array :: separator when calling external codes -> used to im/export variables from/to supported file c = (/1.,2,3.,4.0/) * 1d5 ; double array pres_new1 = pres(lon|:, lat|:) ; reorder (reshape) formats d = (/"red","green","blue"/) ; string array pres_new2 = pres(lon|19:38, lat|0:9) e = (/True,False,False,True/) ; logical array ; define an new array pres_new2(20,10) Expressions f = (/(/1,2/),(/3,6/),(/4,2/)/) ; 2D array (3 x 2) ; with pres_new2(lon,lat) Algebraic operators + Addition, string concatenation - Subtraction / Negation Arrays Coordinate variables * Multiplication The leftmost dimension (dim) of a multi-dim array varies slowest A coordinate variable is a one-dimensional variable with the same / Division and the rightmost dim varies fastest (row major). name as a dimension, which provides coordinate values for that % Modulus (integers only) dimension. It must be strictly monotonic (values increasing or > Greater than a = (/4,2,1,3/) ; 4 elements; index 0-3 decreasing, not mixed). < Less than b = (/0,1,1,0/) ; 4 elements; index 0-3 ^ Exponentiation lat_pts = (/30.,40.,50.,60.,/) ; size 4 # Matrix multiplication c = a + b à c = (/4,3,2,3/) lon_pts = (/ 0.,15, 30, 45, 60/) ; size 5 Logical operators c = a – b à c = (/4,1,0,3/) lat_pts@units = "degrees_north" ; set units attribute .lt. Less than c = a * b à c = (/0,2,1,0/) lon_pts@units = "degrees_east" ; set units attribute .le. Less than or equal c = a/(b+0.1) à c = (/40,1.8182, 0.909090,30/) .eq. Equal grid = new((/4,5/),float) ; define 2D array .ne. Not equal grid!0 = "lat" ; name left dimension .ge. Greater than or equal To create a new array grid!1 = "lon" ; name right dimension .gt. Greater than n = new(4,integer) à integer array of size 4 grid&lat = lat_pts ; assign values to named .and. AND q = new((/2,3,5/),float) à float array of size 2x3x5 ; dimension "lat" .or. OR l = new(100,float,1e20) à float array with grid&lon = lon_pts ; assign values to named .xor. Exclusive OR _FillValue=1e20 ; dimension "lon" .not. NOT cities = new(20,string) à string array of size 20 Data types double 64 bit Coordinate subscripting Numeric float 32 bit Standard subscripting of arrays For coordinate subscripting, all of the rules for standard subscripting long 32 bit or 64 bit; signed +/- The indices used in standard subscripting are integers and the apply except for curly brackets { }, which are used to distinguish integer 32 bit; signed +/- general form of a standard subscript is: coordinate subscripts from standard subscripts. short 16 bit; signed +/- byte 8 bit; signed +/- m:n:i ; range m to n in strides of i complex NOT supported m = (/-5.0,10.0,15.0,20.0,25.0,30.0/) Assignment/Reassignment m!0 = "lat" ; name dimension 0 Assign a variable: Procedures: undef("procedure_name") m&lat = m ; associate the array var = "This is a string" ; type string procedure procedure_name(declaration_list) mw = m({-5. : 25. : 2}) ; contains -5.0,-15.0,25.0 Reassign the variable with a different type and shape: local local_variables ; optional, but recommended Use coordinate subscripting to select a subregion in a global grid. begin var := (/1,2,3,4/) ; type integer statements var(96,192) ; 96 lat and 192 lon elements end var_region = var({20:60},{0:70}) Metadata and attributes Metadata is the information associated with a variable or file that Functions: à Returns an array containing latitudes nearest describes the data. The metadata of a variable can be attributes like undef("function_name") to the values between 20 and 60 degrees inclusive, units, _FillValue, and for a file it can be creation_date and history. function function_name(declaration_list) and longitudes nearest to the values between 0 and local local_variables ; optional, but recommended var@units = "degK" 70 degrees inclusive. begin var@long_name = "Near Surface Temperature" var@_FillValue = -99999 statements Statements return(return_variable) If-statement title = var@long_name end if(scalar_logical_expression) then [statement(s)] Get the attributes of a variable "slp" of a file "file_name.nc": Functions can return multiple variables contained within a variable else fin = addfile("file_name.nc","r") of type list: [statement(s)] file_atts = getfilevaratts(fin,"slp") undef("ret_mulvar") end if function ret_mulvar(val1,val2) To verify whether an attribute of a variable exists, use isatt: There is no "else if" statement; use a trick to get the same effect. local ni,nj ; optional, but recommended if(isatt(slp,"units")) then begin Combine the "if" and "else" on one line, and end with an "end if" for print(slp@units) ni = val1 + val2 each “if” statement: end if nj = val1 * val2 if(scalar_logical_expression_A) then return([/ni,nj/]) ; return value list [statement(s)] Print end else if(scalar_logical_expression_B) then Print procedures echoing to stdout (standard out). [statement(s)] 1. Prints all the values of a variable or expression comp = ret_mulvar(5,2) ; call function else if(scalar_logical_expression_C) then print(variable_or_expression or file) v_add = comp[0] st ; retrieve 1 list element nd [statement(s)] v_mul = comp[1] ; retrieve 2 list element 2. Prints summary of a variable's information (commonly used) else printVarSummary(data_variable) [statement(s)] Important built-in functions and procedures end if ; expression C (includes the "else") 3. Formatted print of all elements from a list all / any Returns True if all/any of the values of end if ; expression B print_table(list) its input evaluate as True end if ; expression A cd_calendar Converts a mixed Julian/Gregorian date to 4. Prints the minimum and maximum value of a variable a UT-referenced date printMinMax(data_variable,0) Loops conform Conforms an array to the shape of another Loops are useful but may not be efficient; they should be used 5. Prints a summary of a file variable's information dimsizes Returns dimension sizes of input variable minimally. Use array arithmetic and/or built-in functions if available. printFileVarSummary(file,varname) exit Forces an NCL script to exit immediately do n=start,end[,stride] ind Returns indices where the input is True [statement(s)] Free memory ismissing Returns True for every element of the end do ; the stride is not optional if end < start Use the delete procedure to free memory. It can be used to delete input that contains a missing value a single variable or a variable list. num Counts the number of True values in input Loop while a logical expression is True: systemfunc Executes shell command and returns output delete(var) do while(scalar_logical_expression) typeof Returns type of input variable delete([/var1,var2,var3/]) [statement(s)] where Performs array assignments based on a end do conditional array User-defined functions and procedures Generally, functions return values; procedures perform tasks. They Use “continue” to skip to next loop iteration; “break” to exit a loop. must have a begin and an end statement.