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
16VERBOSE = 1  # verbose error reporting
17
18
19def example(INPUT):
20    f = open(INPUT)
21
22    while 1:
23        gid = grib_new_from_file(f)
24        if gid is None:
25            break
26
27        iterid = grib_iterator_new(gid, 0)
28
29        missingValue = grib_get_double(gid, "missingValue")
30
31        i = 0
32        while 1:
33            result = grib_iterator_next(iterid)
34            if not result:
35                break
36
37            [lat, lon, value] = result
38
39            sys.stdout.write("- %d - lat=%.6e lon=%.6e value=" % (i, lat, lon))
40
41            if value == missingValue:
42                print "missing"
43            else:
44                print "%.6f" % value
45
46            i += 1
47
48        grib_iterator_delete(iterid)
49        grib_release(gid)
50
51    f.close()
52
53
54def main():
55    try:
56        example(sys.argv[1])
57    except GribInternalError, err:
58        if VERBOSE:
59            traceback.print_exc(file=sys.stderr)
60        else:
61            print >>sys.stderr, err.msg
62
63        return 1
64
65if __name__ == "__main__":
66    sys.exit(main())
67