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