This commit is contained in:
Shahram Najm 2015-03-18 10:36:56 +00:00
parent 2f59abc76d
commit 53db8aed2b
11 changed files with 161 additions and 170 deletions

View File

@ -20,24 +20,23 @@ from eccodes import *
INPUT='../../data/bufr/syno_multi.bufr'
VERBOSE=1 # verbose error reporting
def example():
# open bufr file
f = open(INPUT)
cnt=0
cnt=0
# define the attributes to be printed (see BUFR code table B)
attrs = [
'code',
'code',
'units',
'scale',
'reference',
'width'
]
# loop for the messages in the file
while 1:
# get handle for message
@ -45,52 +44,48 @@ def example():
if gid is None: break
print "message: %s" % cnt
# we need to instruct ecCodes to expand all the descriptors
# i.e. unpack the data values
codes_set(gid,'unpack',1);
codes_set(gid,'unpack',1)
#---------------------------------------------------------------
# We will read the value and all the attributes available for
# the 2m temperature.
#---------------------------------------------------------------
#get the value
key='airTemperatureAt2M'
if not codes_is_defined(gid,key):
if not codes_is_defined(gid,key):
raise Exception("Key: " + key + " was not defined")
print ' %s: %s' % (key,codes_get(gid,key))
# print the values of the attributes of the key. Attributes themselves
# are keys as well. Their name is constructed like:
# print the values of the attributes of the key. Attributes themselves
# are keys as well. Their name is constructed like:
# keyname->attributename
for attr in attrs:
key='airTemperatureAt2M' + "->" + attr
if not codes_is_defined(gid,key):
raise Exception("Key: " + key + " was not defined")
print ' %s: %s' % (key,codes_get(gid,key))
#--------------------------------------------------------------------
# The 2m temperature data element in this message has an associated
# field: percentConfidence. Its value and attributes can be accessed
# in a similar manner as was shown above for 2m temperature.
#-------------------------------------------------------------------
#get the value
key='airTemperatureAt2M->percentConfidence'
if not codes_is_defined(gid,key):
raise Exception("Key: " + key + " was not defined")
print ' %s: %s' % (key,codes_get(gid,key))
# print the values of the attributes of the key.
for attr in attrs:
key='airTemperatureAt2M->percentConfidence' + "->" + attr
if not codes_is_defined(gid,key):
if not codes_is_defined(gid,key):
raise Exception("Key: " + key + " was not defined")
print ' %s: %s' % (key,codes_get(gid,key))
#--------------------------------------------------------------------
# The 2m temperature data element in this message has an associated
# field: percentConfidence. Its value and attributes can be accessed
# in a similar manner as was shown above for 2m temperature.
#-------------------------------------------------------------------
#get the value
key='airTemperatureAt2M->percentConfidence'
if not codes_is_defined(gid,key):
raise Exception("Key: " + key + " was not defined")
print ' %s: %s' % (key,codes_get(gid,key))
# print the values of the attributes of the key.
for attr in attrs:
key='airTemperatureAt2M->percentConfidence' + "->" + attr
if not codes_is_defined(gid,key):
raise Exception("Key: " + key + " was not defined")
print ' %s: %s' % (key,codes_get(gid,key))
cnt+=1

View File

@ -23,9 +23,9 @@ from eccodes import *
INPUT='../../data/bufr/syno_1.bufr'
OUTPUT='bufr_clone_test_p.clone.bufr'
VERBOSE=1 # verbose error reporting
def example():
# open bufr file
fin = open(INPUT)
@ -39,22 +39,22 @@ def example():
# in different ways
for centre in range(0,3):
# clone the message
clone_id = codes_clone(gid)
# this is the place where you may wish to modify the clone
codes_set(clone_id,'bufrHeaderCentre',centre)
# write the cloned message to a file
codes_write(clone_id,fout)
# relase the clone's handle
codes_release(clone_id)
# release the source's handle
codes_release(gid)
fin.close()
fout.close()

View File

@ -20,14 +20,14 @@ from eccodes import *
INPUT='../../data/bufr/syno_1.bufr'
VERBOSE=1 # verbose error reporting
def example():
# open bufr file
f = open(INPUT)
cnt=0
cnt=0
# loop for the messages in the file
while 1:
# get handle for message
@ -35,20 +35,20 @@ def example():
if gid is None: break
print "message: %s" % cnt
# we need to instruct ecCodes to expand all the descriptors
# i.e. unpack the data values
codes_set(gid,'unpack',1);
codes_set(gid,'unpack',1)
#-----------------------------------
# get all the expanded data values
#-----------------------------------
key='numericValues'
# get size
num=codes_get_size(gid,key)
print ' size of %s is: %s' % (key,num)
# get values
values=codes_get_array(gid,key)
for i in xrange(len(values)):

View File

@ -20,14 +20,14 @@ from eccodes import *
INPUT='../../data/bufr/syno_multi.bufr'
VERBOSE=1 # verbose error reporting
def example():
# open bufr file
f = open(INPUT)
cnt=0
cnt=0
# loop for the messages in the file
while 1:
# get handle for message
@ -35,51 +35,48 @@ def example():
if gid is None: break
print "message: %s" % cnt
# we need to instruct ecCodes to expand all the descriptors
# i.e. unpack the data values
codes_set(gid,'unpack',1);
codes_set(gid,'unpack',1)
#----------------------------------------------
# get values for keys holding a single value
#----------------------------------------------
#Native type integer
key='blockNumber'
if not codes_is_defined(gid,key):
if not codes_is_defined(gid,key):
raise Exception("Key: " + key + " was not defined")
print ' %s: %s' % (key,codes_get(gid,key))
#Native type integer
key='stationNumber'
if not codes_is_defined(gid,key):
if not codes_is_defined(gid,key):
raise Exception("Key: " + key + " was not defined")
print ' %s: %s' % (key,codes_get(gid,key))
#Native type float
key='airTemperatureAt2M'
if not codes_is_defined(gid,key):
if not codes_is_defined(gid,key):
raise Exception("Key: " + key + " was not defined")
print ' %s: %s' % (key,codes_get(gid,key))
#Native type string
key='typicalDate'
if not codes_is_defined(gid,key):
if not codes_is_defined(gid,key):
raise Exception("Key: " + key + " was not defined")
print ' %s: %s' % (key,codes_get(gid,key))
#---------------------------------
# get values for an array
#---------------------------------
# Native type integer
key='bufrdcExpandedDescriptors'
# get size
num=codes_get_size(gid,key)
print ' size of %s is: %s' % (key,num)
# get values
values=codes_get_array(gid,key)
for i in xrange(len(values)):
@ -87,11 +84,11 @@ def example():
# Native type float
key='numericValues'
# get size
num=codes_get_size(gid,key)
print ' size of %s is: %s' % (key,num)
# get values
values=codes_get_array(gid,key)
for i in xrange(len(values)):

View File

@ -12,7 +12,7 @@
# Description: Example on how to use keys_iterator functions and the
# codes_keys_iterator structure to get all the available
# keys in a BUFR message.
import traceback
import sys
@ -21,14 +21,14 @@ from eccodes import *
INPUT='../../data/bufr/syno_1.bufr'
VERBOSE=1 # verbose error reporting
def example():
# open bufr file
f = open(INPUT)
cnt=0
cnt=0
# loop for the messages in the file
while 1:
# get handle for message
@ -36,14 +36,14 @@ def example():
if gid is None: break
print "message: %s" % cnt
# we need to instruct ecCodes to expand all the descriptors
# i.e. unpack the data values
#codes_set(gid,'unpack',1);
#codes_set(gid,'unpack',1)
# get key iterator for a given namespace
iterid = codes_keys_iterator_new(gid,'ls')
# Different types of keys can be skipped
# codes_skip_computed(iterid)
# codes_skip_coded(iterid)
@ -51,17 +51,17 @@ def example():
# codes_skip_duplicates(iterid)
# codes_skip_read_only(iterid)
# codes_skip_function(iterid)
#loop over the keys
while codes_keys_iterator_next(iterid):
# print key name
keyname = codes_keys_iterator_get_name(iterid)
#keyval = codes_get_string(iterid,keyname)
#print "%s = %s" % (keyname,keyval)
print " %s" % keyname
# delete the key iterator
print " %s" % keyname
# delete the key iterator
codes_keys_iterator_delete(iterid)
cnt+=1

View File

@ -10,7 +10,7 @@
#
# Python implementation: bufr_read_header
#
# Description: how to read the header from BUFR messages.
# Description: how to read the header from BUFR messages.
#
#
@ -21,9 +21,9 @@ from eccodes import *
INPUT='../../data/bufr/syno_multi.bufr'
VERBOSE=1 # verbose error reporting
def example():
# open bufr file
f = open(INPUT)
@ -38,9 +38,9 @@ def example():
'localTablesVersionNumber',
'numberOfSubsets',
]
cnt=0
cnt=0
# loop for the messages in the file
while 1:
# get handle for message
@ -48,17 +48,17 @@ def example():
if gid is None: break
print "message: %s" % cnt
# print the values for the selected keys from the message
for key in keys:
if not codes_is_defined(gid,key): raise Exception("Key " + key + " was not defined")
print ' %s: %s' % (key,codes_get(gid,key))
cnt+=1
# delete handle
codes_release(gid)
# close the file
f.close()

View File

@ -9,10 +9,10 @@
#
# Python implementation: bufr_read_synop
#
# Description: how to read data values from BUFR messages.
# Description: how to read data values from BUFR messages.
#
#
#
# Please note that SYNOP reports can be encoded in various ways in BUFR. Therefore the code
# below might not work directly for other types of SYNOP messages than the one used in the
# example.
@ -25,9 +25,9 @@ from eccodes import *
INPUT='../../data/bufr/syno_multi.bufr'
VERBOSE=1 # verbose error reporting
def example():
# open bufr file
f = open(INPUT)
@ -42,9 +42,9 @@ def example():
'windSpeedAt10M',
'windDirectionAt10M'
]
cnt=0
cnt=0
# loop for the messages in the file
while 1:
# get handle for message
@ -52,11 +52,11 @@ def example():
if gid is None: break
print "message: %s" % cnt
# we need to instruct ecCodes to expand all the descriptors
# i.e. unpack the data values
codes_set(gid,'unpack',1);
codes_set(gid,'unpack',1)
# print the values for the selected keys from the message
for key in keys:
if not codes_is_defined(gid,key): raise Exception("Key: " + key + " was not defined")

View File

@ -7,7 +7,7 @@
# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
#
# Python implementation: bufr_read_temp
# Python implementation: bufr_read_temp
#
# Description: how to read temperature significant levels from TEMP BUFR messages.
#
@ -20,14 +20,14 @@ from eccodes import *
INPUT='../../data/bufr/temp_101.bufr'
VERBOSE=1 # verbose error reporting
def example():
# open bufr file
f = open(INPUT)
cnt=0
cnt=0
# loop for the messages in the file
while 1:
# get handle for message
@ -35,48 +35,48 @@ def example():
if gid is None: break
print "message: %s" % cnt
# we need to instruct ecCodes to expand all the descriptors
# i.e. unpack the data values
codes_set(gid,'unpack',1);
# In what follows we rely on the fact that for
# temperature significant levels the value of key
codes_set(gid,'unpack',1)
# 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).
# We also make use of the fact that in our BUFR message
# verticalSoundingSignificance is always followed by geopotential,
# verticalSoundingSignificance is always followed by geopotential,
# airTemperature, dewpointTemperature,
# windDirection, windSpeed and pressure.
# windDirection, windSpeed and pressure.
# Get the number of the temperature significant levels.
# We find out the number of temperature significant levels by
# counting how many pressure values we have on these levels.
# We find out the number of temperature significant levels by
# counting how many pressure values we have on these levels.
numSigT=codes_get_size(gid,"/verticalSoundingSignificance=4/pressure")
print ' Number of temperature significant levels %ld' % (numSigT)
# Get pressure
# Get pressure
sigt_pres=codes_get_double_array(gid,"/verticalSoundingSignificance=4/pressure")
# Get gepotential
sigt_geo=codes_get_double_array(gid,"/verticalSoundingSignificance=4/geopotential")
if len(sigt_geo) != numSigT :
print "inconstitent number of geopotential values found!"
return 1
# Get temperature
# Get temperature
sigt_t=codes_get_double_array(gid,"/verticalSoundingSignificance=4/airTemperature")
if len(sigt_t) != numSigT :
print "inconstitent number of temprature values found!"
return 1
# Get dew point
sigt_td=codes_get_double_array(gid,"/verticalSoundingSignificance=4/dewpointTemperature")
@ -84,12 +84,12 @@ def example():
if len(sigt_td) != numSigT:
print "inconstitent number of dewpoint temperature values found!"
return 1
# Print the values
print "lev pres geo t td"
print "-------------------------------"
for i in xrange(numSigT):
print "%3d %6.0f %6.0f %.1f %.1f" % (i+1,sigt_pres[i],sigt_geo[i],sigt_t[i],sigt_td[i])

View File

@ -21,48 +21,48 @@ from eccodes import *
INPUT='../../data/bufr/syno_multi.bufr'
OUTPUT='bufr_set_keys_test_p.tmp.bufr'
VERBOSE=1 # verbose error reporting
def example():
# open bufr file
fin = open(INPUT)
# open otput bufr file
fout = open(OUTPUT,'w')
cnt=0
cnt=0
# loop for the messages in the file
while 1:
# get handle for message
gid = codes_bufr_new_from_file(fin)
if gid is None: break
print "message: %s" % cnt
# we need to instruct ecCodes to expand all the descriptors
# i.e. unpack the data values
#codes_set(gid,'unpack',1);
#codes_set(gid,'unpack',1)
# This is the place where you may wish to modify the message
# E.g. we change the centre
# set centre
val=222
print ' set bufrHeaderCentre to: %d' % val
key='bufrHeaderCentre'
if not codes_is_defined(gid,key):
if not codes_is_defined(gid,key):
raise Exception("Key: " + key + " was not defined")
codes_set(gid,key,val)
#check bufrHeaderCentre's value
print ' %s''s new value is: %d' % (key,codes_get(gid,key))
# write modified message to output
codes_write(gid,fout)
cnt+=1
# delete handle
@ -70,7 +70,7 @@ def example():
fin.close()
fout.close()
def main():
try:

View File

@ -20,14 +20,14 @@ from eccodes import *
INPUT='../../data/bufr/synop_multi_subset.bufr'
VERBOSE=1 # verbose error reporting
def example():
# open bufr file
f = open(INPUT)
cnt=0
cnt=0
# loop for the messages in the file
while 1:
# get handle for message
@ -35,37 +35,36 @@ def example():
if gid is None: break
print "message: %s" % cnt
# we need to instruct ecCodes to expand all the descriptors
# i.e. unpack the data values
codes_set(gid,'unpack',1);
codes_set(gid,'unpack',1)
# find out the number of subsets
key='numberOfSubsets'
numberOfSubsets=codes_get(gid,'numberOfSubsets')
print ' %s: %d' % (key,numberOfSubsets)
# loop over the subsets
for i in range(numberOfSubsets) :
#specify the subset number
codes_set(gid,'subsetNumber',0)
# read and print some data values
# read and print some data values
key='blockNumber'
val=codes_get(gid,key)
print ' %s: %d' % (key,val)
key='stationNumber'
val=codes_get(gid,key)
print ' %s: %d' % (key,val)
#key='airTemperatureAt2M'
#val=codes_get(gid,key)
#print ' %d: %d' % (key,val)
cnt+=1
# delete handle

View File

@ -29,7 +29,7 @@ def example():
while 1:
id = codes_new_from_file(f, CODES_PRODUCT_ANY)
if id is None: break
print 'product: ', codes_get(id, 'kindOfProduct', str)
codes_release(id)