Miscellaneous Functions

Table of Contents

  1. Introduction
  2. acof2boxed(filename)
  3. area_on_globe(latitude, longitude)
  4. color_wheel(n,v,b)
  5. cv(main_nao[,dim_number])
  6. fix_longitude(longitude)
  7. fuzzy_floor(x[,eps])
  8. fuzzy_ceil(x[,eps])
  9. gets_matrix(filename)
  10. head(x[,n])
  11. hsv2rgb(hsv)
  12. isMissing(x)
  13. isPresent(x)
  14. magnify_interp(a, mag_factor)
  15. merid_wt(longitude)
  16. mixed_base(x,b)
  17. outer(dyad,y[,x])
  18. reverse(x[,verb_rank])
  19. scaleAxisSpan(xstart,xend[,nmax[,nice]])
  20. range(a)
  21. tail(x[,n])
  22. zone_wt(latitude)

Introduction

The following functions are defined in the file nap_function_lib.tcl.

acof2boxed(filename)

Read ascii cof (.acof) file and return boxed nao.

Example:
nap "i = acof2boxed('abc.acof')"

area_on_globe(latitude, longitude)

Given latitude and longitude vectors, this function calculates a matrix whose values are fractions of the Earth's surface area.

Example:
% [nap "area_on_globe(-90 .. 0 ... 45, 0 .. 180 ... 45)"] value -format %.4f
0.0229 0.0092 0.0092 0.0092 0.0229
0.0781 0.0312 0.0312 0.0312 0.0781
0.2115 0.0846 0.0846 0.0846 0.2115

color_wheel(n,v,b)

Square containing color wheel.
n is number rows & columns
v is desired "value" level
b is background colour outside circle

Example:
nap "color_wheel(100,255,3#150)"
This produces a u8 array with shape {3 100 100} & values from 0 to 255.

cv(main_nao[,dim_number])

This is simply an alias for coordinate_variable.

fix_longitude(longitude)

Adjust elements of longitude vector by adding or subtracting multiple of 360 to ensure:
−180 ≤ x0 < 180
0 ≤ xi+1xi < 360
Also ensure unit is degrees_east.

fuzzy_floor(x[,eps])

Like floor() except allow for rounding error.
eps is tolerance and defaults to 1e-9.

Example:
% [nap "fuzzy_floor({4.998 4.9998},1e-3)"]
4 5

fuzzy_ceil(x[,eps])

Like ceil() except allow for rounding error.
eps is tolerance and defaults to 1e-9.

Example:
% [nap "fuzzy_ceil({5.002 5.0002},1e-3)"]
6 5

gets_matrix(filename)

Read text file and return NAO matrix whose rows correspond to the lines in the file. Ignore blank lines and lines whose first non-whitespace character is '#'.

Example:
gets_matrix('my_matrix.txt')

head(x[,n])

If n ≥ 0 then result is 1st n elements of x, cycling if n > nels(x).
n defaults to 1.
If n < 0 then result is 1st nels(x)+n elements of x i.e. drop −n from end

Example:

% [nap "head({3 1 9 2 7})"]
3
% [nap "head({3 1 9 2 7}, 2)"]
3 1
% [nap "head({3 1 9 2 7}, -2)"]
3 1 9

hsv2rgb(hsv)

Convert colour in HSV form to RGB.
hsv is an array whose leading dimension has size 3.

Layer 0 along this dimension corresponds to hue as an angle in degrees. Angles of any sign or magnitude are allowed. Red = 0, yellow = 60, green = 120, cyan = 180, blue = −120, magenta = −60.

Layer 1 along this dimension corresponds to saturation in range 0.0 to 1.0.

Layer 2 along this dimension corresponds to "value". This has the same range as the RGB values, normally either 0.0 to 1.0 or 0 to 255. If you are casting the result to an integer & want a maximum of 255 then set the maximum to say 255.999. Otherwise you will get few if any 255s.

The result has the same shape as the argument (hsv).

See Foley, vanDam, Feiner and Hughes, Computer Graphics Principles and Practice, Second Edition, 1990, ISBN 0201121107 page 593.

Example:
% [nap "hsv2rgb {180.0 0.5 100.0}"]
50 100 100

isMissing(x)

1 if x misssing, 0 if present.

Example:
% [nap "isMissing {0 _ 9}"]
0 1 0

isPresent(x)

0 if x misssing, 1 if present.

Example:
% [nap "isPresent {0 _ 9}"]
1 0 1

magnify_interp(a, mag_factor)

Magnify each dimension of array a by factor defined by the corresponding element of mag_factor if this is a vector. If this is a scalar then every dimension is magnified by the same factor. The new values are estimated using multi-linear interpolation.

This function can be used to make images larger or smaller.

Example:
% [nap "magnify_interp({{1 2 3}{4 5 6}}, {1 3})"] value
1.00000 1.33333 1.66667 2.00000 2.33333 2.66667 3.00000
4.00000 4.33333 4.66667 5.00000 5.33333 5.66667 6.00000

magnify_nearest(a, mag_factor)

This function is similar to magnify_interp except that the new values are defined by the nearest neighbour rather than interpolation.

Example:
% [nap "magnify_nearest({{1 2 3}{4 5 6}}, {1 3})"] value
1 1 2 2 2 3 3
4 4 5 5 5 6 6

merid_wt(longitude)

Calculate normalised (so sum weights = 1) meridional weights from longitudes.

Example:
% [nap "merid_wt {-180 -90 -45 0 90 180}"]
0.125 0.1875 0.125 0.1875 0.25 0.125

mixed_base(x,b)

Convert scalar value x to mixed base defined by vector b.

Following example converts 87 inches to yards, feet & inches:
% [nap "mixed_base(87, {3 12})"]
2 1 3

outer(dyad,y[,x])

Tensor outer-product.

dyad is name of either

  • function with two arguments
  • binary (dyadic) operator
    x is vector
    y is vector defaulting to x
    Result is cross-product of x and y, applying dyad to each combination of x & y.
    x & y are the coordinate variables of the result.

    Following example produces a multiplication table:

    % [nap "outer('*', 1 .. 5)"]
     1  2  3  4  5
     2  4  6  8 10
     3  6  9 12 15
     4  8 12 16 20
     5 10 15 20 25
    

    reverse(x[,verb_rank])

    Reverse order of items in array x.

    Examples:

    % [nap "reverse {1 9 0 7}"]
    7 0 9 1
    % [nap "reverse {{1 2 3}{4 5 6}}"]
    4 5 6
    1 2 3
    % [nap "reverse({{1 2 3}{4 5 6}}, 1)"]
    3 2 1
    6 5 4
    

    scaleAxis(xstart,xend[,nmax[,nice]])

    Find suitable values for axis of graph.
    xstart: 1st data value
    xend: Final data value
    nmax: Max. allowable number of elements in result (Default: 10)
    nice: Allowable increments (Default: {1 2 5})
    Result is the arithmetic progression which:
  • is within interval from xstart to xend
  • has same order (ascending/descending) as xstart//xend
  • has increment equal to element of nice times a power (-30 .. 30) of 10
  • has at least two elements
  • has no more than nmax elements if possible
  • has as many elements as possible. (Ties are resolved by choosing earlier element in nice.)

    Example:
    % [nap "axis = scaleAxis(-370, 580, 10, {10 20 25 50})"] value
    -300 -200 -100 0 100 200 300 400 500

    scaleAxisSpan(xstart,xend[,nmax[,nice]])

    Find suitable values for axis of graph.
    xstart: 1st data value
    xend: Final data value
    nmax: Max. allowable number of elements in result (Default: 10)
    nice: Allowable increments (Default: {1 2 5})
    Result is the arithmetic progression which:
  • includes the interval from xstart to xend
  • has same order (ascending/descending) as xstart//xend
  • has increment equal to element of nice times a power (-30 .. 30) of 10
  • has at least two elements
  • has no more than nmax elements if possible
  • has as many elements as possible. (Ties are resolved by choosing earlier element in nice.)

    Example:
    % [nap "axis = scaleAxisSpan(-370, 580, 10, {10 20 25 50})"] value
    -400 -200 0 200 400 600

    range(a)

    Result is 2-element vector containing minimum and maximum of array a.
    Example:
    % [nap "range {{9 -1 -5}{2 9 3}}"]
    -5 9

    tail(x[,n])

    If n ≥ 0 then result is final n elements of x, cycling if n > nels(x).
    n defaults to 1.
    If n < 0 then result is final nels(x)+n elements of x i.e. drop −n from start.

    Example:

    % [nap "tail({3 1 9 2 7})"]
    7
    % [nap "tail({3 1 9 2 7}, 2)"]
    2 7
    % [nap "tail({3 1 9 2 7}, -2)"]
    9 2 7
    

    zone_wt(latitude)

    Calculate normalised (so sum weights = 1) zonal weights from latitudes.

    Example:
    % [nap "zone_wt(-90 .. 90 ... 30)"] value
    0.0334936 0.125 0.216506 0.25 0.216506 0.125 0.0334936

    Author: Harvey Davies       © 2002, CSIRO Australia.       Legal Notice and Disclaimer
    CVS Version Details: $Id: nap_function_lib.html,v 1.3 2003/03/17 05:12:27 dav480 Exp $