#!/usr/bin/env python

from optparse import OptionParser

import bufr


# ==== Parsing the options on command line
parser = OptionParser()
parser.add_option("--header", dest="header",
                  action="store_true", default=False,
                  help="Show only the file's header")

#parser.add_option("-o", "--output", dest="output",
#                  help="Output filename", default="./input.nml",
#                  metavar="ref.yaml")

#parser.add_option("-r", "--referencefile", dest="reference",
#                  help="Reference filename", default="./input.ref",
#                  metavar="ref.yaml")

(options, args) = parser.parse_args()


filename = args[0]
f=open(filename,'r')
header = f.read(20)

sectiondata={}
# Read section 0
sectiondata[0]=bufr.section0(f)
# Read section 1
if sectiondata[0]['version']==3:
    sectiondata[1]=bufr.section1v3(f)
elif sectiondata[0]['version']==4:
    sectiondata[1]=bufr.section1v4(f)
# Read section 2

if (sectiondata[1]['optionalsec'] != 0):
    print "Atention!!!! Not ready to read the section 2"

# Read section 3
    sectiondata[3]=bufr.section3(f)

# ============================================================================
if options.header:
    for k in sectiondata:
        #print k,sectiondata[k]
        print "Section: %s" % k
        for kk in sectiondata[k]:
            print "  %s: %s" % (kk,sectiondata[k][kk])

