Step-by-step guide

If we have a fieldset / GRIB file containing values and we want to not only find the min/max values at each point, but also which fields those min/max values come from, we can compute a field where the value at each grid point is the index of the field that contains the min or max value for that point. The following Python code shows a way to do this. The code itself is quite short, but it has lots of explanation!

import metview as mv

# read the GRIB file and compute the field of max values; we could
# instead use min() here, and the script will compute the indexes
# where the min values occur
t = mv.read('t2.grib')
m = t.max()
n = int(t.count())

# duplicate the field of min/max values n times
mxn = m.duplicate(n)

# create a new fieldset where each value is 1 if it equals
# the min/max value for that point; comparison is performed per field
tmask = (t == mxn)

# multiply each field by its index (1-based)
tmask_times_index = mv.Fieldset()
for i in range(0, n):
    tmask_times_index.append(tmask[i]*(i+1))

# each field now contains a mixture of zeros and j, where j
# is the index of the field; j appears at grid points where
# this field contains the min/max value for that point; take
# the max field of this fieldset to obtain a field of indexes
# where the min/max values appear. The significance of using max()
# here is that if two or more fields contain the min/max value
# for a given point, the higher index of those will be used;
# use min() if you prefer to use the lowest index in this case.
max_indexes = tmask_times_index.max()

# max_indexes is a Fieldset like any other, but the values are 1-n
# where n is the number of fields in the original data.
# This is standard fieldset, so we can, for example, plot it.
mv.plot(max_indexes)