1#
2# Copyright 2005-2018 ECMWF.
3#
4# This software is licensed under the terms of the Apache Licence Version 2.0
5# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6#
7# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9#
10
11import traceback
12import sys
13
14from gribapi import *
15
16INPUT = '../../data/tigge_pf_ecmwf.grib2'
17VERBOSE = 1  # verbose error reporting
18
19
20def example():
21    f = open(INPUT)
22
23    mcount = grib_count_in_file(f)
24    gid_list = [grib_new_from_file(f) for i in range(mcount)]
25
26    f.close()
27
28    keys = [
29        'Ni',
30        'Nj',
31        'latitudeOfFirstGridPointInDegrees',
32        'longitudeOfFirstGridPointInDegrees',
33        'latitudeOfLastGridPointInDegrees',
34        'longitudeOfLastGridPointInDegrees',
35        'jDirectionIncrementInDegrees',
36        'iDirectionIncrementInDegrees',
37    ]
38
39    for i in range(mcount):
40        gid = gid_list[i]
41
42        print "processing message number", i + 1
43
44        for key in keys:
45            print '%s=%g' % (key, grib_get(gid, key))
46
47        print 'There are %d, average is %g, min is %g, max is %g' % (
48            grib_get_size(gid, 'values'),
49            grib_get(gid, 'average'),
50            grib_get(gid, 'min'),
51            grib_get(gid, 'max')
52        )
53
54        print '-' * 100
55
56        grib_release(gid)
57
58
59def main():
60    try:
61        example()
62    except GribInternalError, err:
63        if VERBOSE:
64            traceback.print_exc(file=sys.stderr)
65        else:
66            print >>sys.stderr, err.msg
67
68        return 1
69
70if __name__ == "__main__":
71    sys.exit(main())
72