eccodes/examples/python/bufr_read_tropical_cyclone.py

205 lines
7.3 KiB
Python
Raw Normal View History

# Copyright 2005-2016 ECMWF.
2015-12-16 11:03:37 +00:00
#
# 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.
#
# Python implementation: bufr_read_tropical_cyclone
#
# Description: how to read data of the ECMWF EPS tropical cyclone tracks encoded in BUFR format.
#
import traceback
import sys
import collections
from eccodes import *
INPUT = '../../data/bufr/tropical_cyclone.bufr'
VERBOSE = 1 # verbose error reporting
data=collections.defaultdict(dict)
2015-12-29 13:53:51 +00:00
2015-12-16 11:03:37 +00:00
def example():
# open bufr file
f = open(INPUT)
cnt = 0
2015-12-16 11:03:37 +00:00
# loop for the messages in the file
while 1:
# get handle for message
gid = codes_bufr_new_from_file(f)
if gid is None:
break
print '**************** MESSAGE: ',cnt+1,' *****************'
2015-12-16 11:03:37 +00:00
# we need to instruct ecCodes to expand all the descriptors
# i.e. unpack the data values
codes_set(gid, 'unpack', 1)
numObs= codes_get(gid,"numberOfSubsets")
2015-12-16 11:03:37 +00:00
year = codes_get(gid, "year")
month = codes_get(gid, "month")
day = codes_get(gid, "day")
hour = codes_get(gid, "hour")
minute= codes_get(gid, "minute")
2015-12-29 13:53:51 +00:00
print 'Date and time: ', day,'.',month,'.',year,' ',hour,':',minute
2015-12-16 11:03:37 +00:00
stormIdentifier = codes_get(gid,"stormIdentifier")
print 'Storm identifier: ', stormIdentifier
#How many different timePeriod in the data structure?
numberOfPeriods=0
2015-12-16 11:03:37 +00:00
while True:
numberOfPeriods=numberOfPeriods+1
2015-12-16 11:03:37 +00:00
try:
codes_get_array(gid,"#%d#timePeriod" %numberOfPeriods)
except CodesInternalError as err:
2015-12-16 11:03:37 +00:00
break
#the numberOfPeriods includes the analysis (period=0)
# Get ensembleMemberNumber
memberNumber = codes_get_array(gid, "ensembleMemberNumber")
memberNumberLen=len(memberNumber)
2015-12-29 13:53:51 +00:00
2015-12-16 11:03:37 +00:00
# Observed Storm Centre
significance = codes_get(gid,'#1#meteorologicalAttributeSignificance')
latitudeCentre = codes_get(gid,'#1#latitude')
longitudeCentre = codes_get(gid,'#1#longitude')
2015-12-29 13:53:51 +00:00
if significance!=1:
2015-12-16 11:03:37 +00:00
print 'ERROR: unexpected #1#meteorologicalAttributeSignificance'
return 1
if (latitudeCentre==CODES_MISSING_DOUBLE) and (longitudeCentre==CODES_MISSING_DOUBLE):
2015-12-29 13:53:51 +00:00
print 'Observed storm centre position missing'
2015-12-16 11:03:37 +00:00
else:
2015-12-29 13:53:51 +00:00
print 'Observed storm centre: latitude=',latitudeCentre,' longitude=',longitudeCentre
2015-12-16 11:03:37 +00:00
# Location of storm in perturbed analysis
significance = codes_get(gid,'#2#meteorologicalAttributeSignificance')
2015-12-29 13:53:51 +00:00
if significance!=4:
2015-12-16 11:03:37 +00:00
print 'ERROR: unexpected #2#meteorologicalAttributeSignificance'
return 1
latitudeAnalysis = codes_get_array(gid,'#2#latitude')
longitudeAnalysis = codes_get_array(gid,'#2#longitude')
pressureAnalysis = codes_get_array(gid,'#1#pressureReducedToMeanSeaLevel')
2015-12-16 11:03:37 +00:00
# Location of Maximum Wind
significance=codes_get(gid,'#3#meteorologicalAttributeSignificance')
2015-12-29 13:53:51 +00:00
if significance!=3:
2015-12-16 11:03:37 +00:00
print 'ERROR: unexpected #3#meteorologicalAttributeSignificance=', significance
return 1
latitudeMaxWind0=codes_get_array(gid,'#3#latitude')
longitudeMaxWind0= codes_get_array(gid,'#3#longitude')
2015-12-16 11:03:37 +00:00
windMaxWind0= codes_get_array(gid,'#1#windSpeedAt10M')
2015-12-29 13:53:51 +00:00
if len(latitudeAnalysis)==len(memberNumber) and len(latitudeMaxWind0)==len(memberNumber):
for k in range(len(memberNumber)):
data[k][0]=[latitudeAnalysis[k],longitudeAnalysis[k],pressureAnalysis[k],latitudeMaxWind0[k],longitudeMaxWind0[k],windMaxWind0[k]]
2015-12-16 11:03:37 +00:00
else:
for k in range(len(memberNumber)):
data[k][0]=[latitudeAnalysis[0],longitudeAnalysis[0],pressureAnalysis[k],latitudeMaxWind0[0],longitudeMaxWind0[0],windMaxWind0[k]]
2015-12-29 13:53:51 +00:00
timePeriod=[0 for x in range(numberOfPeriods)]
for i in range(1,numberOfPeriods):
2015-12-16 11:03:37 +00:00
rank1 = i * 2 + 2
rank3 = i * 2 + 3
ivalues= codes_get_array(gid,"#%d#timePeriod" %(i))
2015-12-29 13:53:51 +00:00
if len(ivalues)==1:
timePeriod[i]=ivalues[0]
2015-12-16 11:03:37 +00:00
else:
2015-12-29 13:53:51 +00:00
for j in range(len (ivalues)):
if ivalues[j]!=CODES_MISSING_LONG:
timePeriod[i]=ivalues[j]
break
2015-12-16 11:03:37 +00:00
2015-12-29 13:53:51 +00:00
#Location of the storm
2015-12-16 11:03:37 +00:00
values = codes_get_array(gid, "#%d#meteorologicalAttributeSignificance" % rank1)
2015-12-29 13:53:51 +00:00
if len(values)==1:
significance=values[0]
2015-12-16 11:03:37 +00:00
else:
2015-12-29 13:53:51 +00:00
for j in range(len (values)):
if values[j]!=CODES_MISSING_LONG:
significance=values[j]
break
if significance==1:
2015-12-16 11:03:37 +00:00
lat = codes_get_array(gid, "#%d#latitude" % rank1)
lon = codes_get_array(gid, "#%d#longitude" % rank1)
press = codes_get_array(gid, "#%d#pressureReducedToMeanSeaLevel" % (i + 1))
else:
print 'ERROR: unexpected meteorologicalAttributeSignificance=',significance
2015-12-29 13:53:51 +00:00
#Location of maximum wind
2015-12-16 11:03:37 +00:00
values = codes_get_array(gid, "#%d#meteorologicalAttributeSignificance" % rank3)
2015-12-29 13:53:51 +00:00
if len(values)==1:
significanceWind=values[0]
2015-12-16 11:03:37 +00:00
else:
2015-12-29 13:53:51 +00:00
for j in range(len (values)):
if values[j]!=CODES_MISSING_LONG:
significanceWind=values[j]
break
2015-12-16 11:03:37 +00:00
2015-12-29 13:53:51 +00:00
if significanceWind==3:
latWind = codes_get_array(gid, "#%d#latitude" % rank3)
lonWind = codes_get_array(gid, "#%d#longitude" % rank3)
wind10m = codes_get_array(gid, "#%d#windSpeedAt10M" % (i + 1))
2015-12-16 11:03:37 +00:00
else:
print 'ERROR: unexpected meteorologicalAttributeSignificance=',significanceWind
2015-12-16 11:03:37 +00:00
for k in range(len(memberNumber)):
data[k][i]=[lat[k],lon[k],press[k],latWind[k],lonWind[k],wind10m[k]]
2015-12-16 11:03:37 +00:00
2015-12-29 13:53:51 +00:00
# ---------------------------------------- Print the values -------------
2015-12-16 11:03:37 +00:00
for m in range(len(memberNumber)):
print "== Member %d" %memberNumber[m]
2015-12-16 11:03:37 +00:00
print "step latitude longitude pressure latitude longitude wind"
for s in range(len(timePeriod)):
if data[m][s][0]!=CODES_MISSING_DOUBLE and data[m][s][1]!=CODES_MISSING_DOUBLE:
print " {:>3d}{}{:>6.1f}{}{:>6.1f}{}{:>8.1f}{}{:>6.1f}{}{:>6.1f}{}{:>6.1f}".format(\
timePeriod[s],' ',data[m][s][0],' ',data[m][s][1],' ',data[m][s][2],' ',
data[m][s][3],' ',data[m][s][4],' ',data[m][s][5])
2015-12-16 11:03:37 +00:00
2015-12-29 13:53:51 +00:00
# -----------------------------------------------------------------------
cnt += 1
2015-12-16 11:03:37 +00:00
# release the BUFR message
2015-12-16 11:03:37 +00:00
codes_release(gid)
# close the file
2015-12-16 11:03:37 +00:00
f.close()
def main():
try:
example()
except CodesInternalError as err:
2015-12-16 11:03:37 +00:00
if VERBOSE:
traceback.print_exc(file=sys.stderr)
else:
sys.stderr.write(err.msg + '\n')
2015-12-16 11:03:37 +00:00
return 1
if __name__ == "__main__":
sys.exit(main())