A vector is an array of numbers designed for highperf= ormance computations. For a full list and details of functions and operator= s on vectors, see Vector Functio= ns.
=20Vectors serve as a way to perform computations w=
ith diverse data types. For instance, arrays of values can be extracted as =
vectors from data types such as field=
set
, geopoints<=
/span>
, netcdf =
, odb
and table
.=
Some of these types also allow their arrays to be set using vectors.
Much of the vector functionality can also be ach=
ieved using lists of numbers, but vectors are significantly more efficient =
for larger sets of numbers (say 1000 or more elements), as lists are not de=
signed to process those quantities of data.
Vectors can also be passed to inline Fortran or C/C++ = code for further processing.
Vectors are created using the vect= or() function and matrices with the matrix() function. Their elements are read or set using the [] operator :
# Allocate a vector of 5 elements
v =3D vector(5=
)
for i =3D 1 to 5 do
v[i] =3D i
end for
Vector literals can be written using the =
 character :
v =3D 3,6,7,9,10
=
=
Vector data types
By default, the elements of a vector are 64bit double precision f=
loating point values. By calling the function vector_set_d=
efault_type(type)
, where type
can be one of 'floa=
t32'
and 'float64'
, you can force all subsequently crea=
ted vectors to contain elements of that type. A vector of float32 elements =
consumes half the memory of a float64 vector, but at the expense of some ac=
curacy. The type of a vector can be queried with the dtype()
f=
unction.
How operators and functions work on vectors
Operations between vectors and vectors are carried out=
between each pair of corresponding vector values. The result is another ve=
ctor. Thus :
Z =3D X+Y
is equivalent to :
for each value i
=
Zi =3D Xi + Yi
If one operand is a scalar and the other a vector, the=
operation is carried out between each vector value and the scalar. The res=
ult is another vector. Thus :
Z =3D X+n
is equivalent to :
for each value i
Z i =
=3D X i + n
The same logic applies to functions. If the argument o=
f a function is a vector, the result is a vector where each element is the =
result of the function at the corresponding element in the input vector. Th=
us :
Z =3D f(X)
is equivalent to :
for each value i
Z =
i =3D f(X i )
Boolean operators such as =
>
or <=3D
<=
span> produce 0 when the comparison fails, or <=
/span> 1 if it succeeds. Thus :
Z =3D X > 0
gives a vector where all the values are either 1 or 0 =
depending on the corresponding values of the vector X being above 0 or not.=
Indexing vectors
Indexing a vector allows you to access particul=
ar elements inside it. Indexing uses the square bracket operator [] . At its simplest you can use it to extract or refer to=
a single value inside a vector.
X[i] =3D ith=
value of vector X :
# copies element 2 of vector X into Y
Y =3D X[2=
]
More sophisticated usage of [] allows you to extract or refer to a range of values.
x[i,j] =3D all values of vector X from the ith to th=
e jth :
# copies values 3, 4, 5, 6, 7 and 8 of X into Y
Y =3D X[3,8]
X[i,j,k] =3D every =
kth value of vector X , from the <=
/span> ith to the jth :
# copies values 1, 5, 9, 13, 17 of X into Y
Y =
=3D X[1,20,4]
An additional fourth parameter specifies how many elem=
ents to extract from the current step :
# copies values 1,2, 5,6, 9,10, 13,14, 17,18 of X into Y =
Y =3D X[1,20,4,2]
If a vector is holding data representing a rectangular=
structure, this form could be used to extract a 'subarea'.
A vector can also be used to provide a set of i=
ndexes to another vector:
# copies values 20, 10=
, 30 into r
v =3D 10, 20, 30, 40
i =3D=
2, 1, 3
r =3D v[i]
Additionally, it is possible to assign a vector=
to an indexed position in another vector, for example: v[4] =3D 99,99,99
. In this examp=
le, elements 4, 5 and 6 of v <=
/code> will be replaced.
Missing values in vectors
Vectors can contain missing values. These can b=
e assigned or tested for using the global variable vector_missing_value
. Operations betwee=
n vectors will bypass missing values. For example, if we represent a missin=
g value with an 'x', then the result of
1,2,3,x,5 + 2,2,2,2,x
will be
3,4,5,x,x
See the descriptions for particular functions a=
nd operators for specific details. The bitmap()
function can be used to translate between =
missing values and 'real' values. When a vector is printed with the =
print()
function, miss=
ing values are represented by an ' x
'.
When a vector is generated from a fieldset, e.g.
a =3D values(fieldset)
missing values from the field are automatically transl=
ated into missing values in the vector. The same is true when obtaining a v=
ector of values from a geopoints variable. Missing values in vectors are al=
so translated correctly when inserted into fieldsets and geopoints.
Exporting vector data t=
o an ASCII file
The following piece of code illustrates one way to write the contents of=
a vector variable to text file:
=20
# Metview=
Macro
v =3D 1,2,5,6,7
f =3D file('result.txt') # open a handle to the output file
for i =3D 1 to count(v) do
write(f, v[i], ',') # write each element of the vector
end for
write(f, newline) # write a newline at the end
f =3D 0 # close the file handle
=20
Making computations more ef=
ficient by using vectors
When performing computations with other data types (fieldsets, geopoints, netcdf), Metview Macro will store intermediate results on disk. This =
slight overhead can be averted by using vectors instead. The following simp=
le example illustrates what happens.
=20
a =3D r=
ead('a.grib') # a is a fieldset
b =3D read('b.grib') # b is a fieldset
spd =3D sqrt(a*a + b*b) # some temporary GRIB files generated
=20
Here, three temporary GRIB files will be generated: for the expressions =
a*a
, b*b
and their addition (the sqrt function will also generate a file, but as it is the intended result we w=
on't consider it to be temporary). This has the advantage that memory is re=
leased between parts of the computation (and only one field from each field=
set is expanded into memory at a time), but there is an overhead of file I/=
O and GRIB packing/unpacking. Also note that these intermediate results wil=
l not be in 64bit precision, but instead at the precision of their GRIB fi=
les.
An alternative is to extract the arrays of values from the fieldsets, do=
the computation with these, then write the final result back into a fields=
et variable. The following code illustrates this, with some renaming of var=
iables in order to keep the names of the variables used in the computation =
the same as before.
=20
afs =3D=
read('a.grib') # afs is a fieldset
bfs =3D read('b.grib') # bfs is a fieldset
a =3D values(afs) # a is a vector or a list of vectors
b =3D values(bfs) # b is a vector or a list of vectors
spd =3D sqrt(a*a + b*b) # spd is a vector or a list of vectors
spdfs =3D set_values(a, spd) # write the result back into a fieldset
=
=20
Notes on this example:
 no temporary files are generated
 computations are performed with the default of 64bit doubleprecision =
floating point numbers
 the vector variables are held in memory
 the
values()
function on a fieldset with many fields will =
yield a list of many vectors, which may require much memory
 if this is the end of the computation, the vector variables should be f=
reed, e.g.

=20
a =3D 0
b =3D 0
spd =3D 0
=20
=_Part_133576_584855594.1574244197900