eccodes/examples/python/bufr_read_temp.py

112 lines
3.4 KiB
Python
Raw Normal View History

# Copyright 2005-2015 ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#
# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
#
2015-03-18 10:36:56 +00:00
# Python implementation: bufr_read_temp
#
# Description: how to read temperature significant levels from TEMP BUFR messages.
#
#
# Please note that TEMP reports can be encoded in various ways in BUFR. Therefore the code
2015-03-23 11:04:29 +00:00
# below might not work directly for other types of TEMP messages than the one used in the
# example. It is advised to use bufr_dump to understand the structure of the messages.
#
import traceback
import sys
from eccodes import *
2015-11-11 18:20:31 +00:00
INPUT = '../../data/bufr/temp_101.bufr'
VERBOSE = 1 # verbose error reporting
2015-03-18 10:36:56 +00:00
def example():
2015-03-18 10:36:56 +00:00
# open bufr file
f = open(INPUT)
2015-11-11 18:20:31 +00:00
cnt = 0
2015-03-18 10:36:56 +00:00
# loop for the messages in the file
while 1:
# get handle for message
gid = codes_bufr_new_from_file(f)
2015-11-11 18:20:31 +00:00
if gid is None:
break
print "message: %s" % cnt
2015-03-18 10:36:56 +00:00
# we need to instruct ecCodes to expand all the descriptors
# i.e. unpack the data values
2015-11-11 18:20:31 +00:00
codes_set(gid, 'unpack', 1)
2015-03-18 10:36:56 +00:00
# In what follows we rely on the fact that for
# temperature significant levels the value of key
# verticalSoundingSignificance is 4 (see flag table 8001 for details).
2015-03-18 10:36:56 +00:00
# We also make use of the fact that in our BUFR message
2015-03-18 10:36:56 +00:00
# verticalSoundingSignificance is always followed by geopotential,
# airTemperature, dewpointTemperature,
2015-03-18 10:36:56 +00:00
# windDirection, windSpeed and pressure.
# Get the number of the temperature significant levels.
2015-03-18 10:36:56 +00:00
# We find out the number of temperature significant levels by
# counting how many pressure values we have on these levels.
2015-11-11 18:20:31 +00:00
numSigT = codes_get_size(gid, "/verticalSoundingSignificance=4/pressure")
print ' Number of temperature significant levels %ld' % (numSigT)
2015-03-18 10:36:56 +00:00
# Get pressure
2015-11-11 18:20:31 +00:00
sigt_pres = codes_get_array(gid, "/verticalSoundingSignificance=4/pressure")
2015-03-18 10:36:56 +00:00
# Get gepotential
2015-11-11 18:20:31 +00:00
sigt_geo = codes_get_array(gid, "/verticalSoundingSignificance=4/geopotential")
2015-03-18 10:36:56 +00:00
# Get temperature
2015-11-11 18:20:31 +00:00
sigt_t = codes_get_array(gid, "/verticalSoundingSignificance=4/airTemperature")
# Get dew point
2015-11-11 18:20:31 +00:00
sigt_td = codes_get_array(gid, "/verticalSoundingSignificance=4/dewpointTemperature")
# Check that all arrays are same size
2015-11-11 18:20:31 +00:00
if len(sigt_pres) != numSigT or len(sigt_geo) != numSigT or len(sigt_t) != numSigT or len(sigt_td) != numSigT:
print 'inconsistent array dimension'
return 1
2015-03-18 10:36:56 +00:00
# Print the values
print "lev pres geo t td"
print "-------------------------------"
2015-03-18 10:36:56 +00:00
for i in xrange(numSigT):
2015-11-11 18:20:31 +00:00
print "%3d %6.0f %6.0f %.1f %.1f" % (i + 1, sigt_pres[i], sigt_geo[i], sigt_t[i], sigt_td[i])
2015-11-11 18:20:31 +00:00
cnt += 1
# delete handle
codes_release(gid)
# close the file
f.close()
2015-11-11 18:20:31 +00:00
def main():
try:
example()
2015-11-11 18:20:31 +00:00
except CodesInternalError, err:
if VERBOSE:
traceback.print_exc(file=sys.stderr)
else:
2015-11-11 18:20:31 +00:00
print >>sys.stderr, err.msg
return 1
if __name__ == "__main__":
sys.exit(main())