Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Vectors serve as a way to perform computations with diverse data types. For instance, arrays of values can be extracted as vectors from data types such as fieldset , geopoints , 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 achieved using lists of numbers, but vectors are significantly more efficient for larger sets of numbers (say 1000 or more elements), as lists are not designed to process those quantities of data.

Vectors can also be passed to inline Fortran or C/C++ code for further processing.

...

Vector literals can be written using the | character :

v = |3,6,7,9,10|

Vector data types

By default, the elements of a vector are 64-bit double precision floating point values. By calling the function vector_set_default_type(type), where type can be one of 'float32' and 'float64', you can force all subsequently created 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 accuracy. The type of a vector can be queried with the dtype() function.

How operators and functions work on vectors

...

If a vector is holding data representing a rectangular structure, this form could be used to extract a 'sub-area'.

A vector can also be used to provide a set of indexes to another vector:

# copies values 20, 10, 30 into r
v = |10, 20, 30, 40|

i = |2, 1, 3|
r = v[i] 

Additionally, it is possible to assign a vector to an indexed position in another vector, for example: v[4] = |99,99,99| . In this example, elements 4, 5 and 6 of v will be replaced.

...

Code Block
languagepy
afs   = read('a.grib')        # afs is a fieldset
bfs   = read('b.grib')        # bfs is a fieldset
a     = values(afs)           # a is a vector or a list of vectors
b     = values(bfs)           # b is a vector or a list of vectors
spd   = sqrt(a*a + b*b)       # spd is a vector or a list of vectors
spdfs = set_values(aafs, spd)  # write the result back into a fieldset

...

  • no temporary files are generated
  • computations are performed with the default of 64-bit double-precision 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 freed, e.g.

    • Code Block
      languagepy
      a   = 0
      b   = 0
      spd = 0

...