...
Code Block |
---|
|
# Metview Macro
v = |1,2,5,6,7|
f = file('result.txt') # open a handle to the output file
for i = 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 = 0 # close the file handle |
Anchor |
---|
| efficient_computations_with_vectors |
---|
| efficient_computations_with_vectors |
---|
|
Making computations more efficient by using vectorsWhen 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 simple example illustrates what happens.
Code Block |
---|
|
a = read('a.grib') # a is a fieldset
b = read('b.grib') # b is a fieldset
spd = sqrt(a*a + b*b) # some temporary GRIB files generated |
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 won't consider it to be temporary). This has the advantage that memory is released between parts of the computation (and only one field from each fieldset 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 will not be in 64-bit precision, but instead at the precision of their GRIB files.
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 fieldset variable. The following code illustrates this, with some renaming of variables in order to keep the names of the variables used in the computation the same as before.
Code Block |
---|
|
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(a, spd) # write the result back into a fieldset |
Notes on this example:
- no temporary files are generated
- computations are performed with 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 |
---|
|
a = 0
b = 0
spd = 0 |