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 bitmapPresent = grib_get(gid, 'bitmapPresent') 30 if bitmapPresent: 31 # Get the bitmap array which contains 0s and 1s 32 bitmap = grib_get_array(gid, 'bitmap', int) 33 # Do some sanity checking 34 assert len(bitmap) == grib_get_size(gid, 'values') 35 assert len(bitmap) == grib_get(gid, 'numberOfDataPoints') 36 37 i = 0 38 while 1: 39 result = grib_iterator_next(iterid) 40 if not result: 41 break 42 43 [lat, lon, value] = result 44 sys.stdout.write("- %d - lat=%.6e lon=%.6e value=" % (i, lat, lon)) 45 46 # Consult bitmap to see if the i'th value is missing 47 if bitmapPresent and bitmap[i] == 0: 48 print "missing" 49 else: 50 print "%.6f" % value 51 52 i += 1 53 54 grib_iterator_delete(iterid) 55 grib_release(gid) 56 57 f.close() 58 59 60def main(): 61 try: 62 example(sys.argv[1]) 63 except GribInternalError, err: 64 if VERBOSE: 65 traceback.print_exc(file=sys.stderr) 66 else: 67 print >>sys.stderr, err.msg 68 69 return 1 70 71if __name__ == "__main__": 72 sys.exit(main()) 73