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(): 20 """ 21 Encoding of the pv coefficients. 22 """ 23 # read the coefficients from file 24 pv = [] 25 for line in open('../../data/60_model_levels'): 26 pv.extend([float(x) for x in line.strip().split('\t')]) 27 28 numberOfLevels = 60 29 numberOfCoefficients = 2 * (numberOfLevels + 1) 30 assert(len(pv) == numberOfCoefficients) 31 32 fout = open('out.pv.grib1', 'w') 33 gid = grib_new_from_samples('reduced_gg_sfc_grib1') 34 35 grib_set(gid, 'typeOfLevel', 'hybrid') 36 grib_set(gid, 'level', 2) 37 grib_set(gid, 'PVPresent', 1) 38 grib_set_array(gid, 'pv', pv) 39 40 grib_write(gid, fout) 41 42 grib_release(gid) 43 fout.close() 44 45 46def main(): 47 try: 48 example() 49 except GribInternalError, err: 50 if VERBOSE: 51 traceback.print_exc(file=sys.stderr) 52 else: 53 print >>sys.stderr, err.msg 54 55 return 1 56 57if __name__ == '__main__': 58 sys.exit(main()) 59