#!/usr/bin/env python

from optparse import OptionParser

from pupynere import netcdf_file

#import yaml
from cnv.cnv import fCNV

# ==== Parsing the options on command line
parser = OptionParser()

parser.add_option("-d", "--default", dest="defaultfilename",
    help="YAML FILE with default vaules", metavar="FILE")

(options, args) = parser.parse_args()

data = fCNV(args[0], options.defaultfilename)
print "Reading from file: %s" % args[0]

print "Saving netcdf output file: %s" % args[1]

output = netcdf_file(args[1], 'w')

output.history = "Created by cnv2cdf"

#print "Global attributes"
A = data.attributes.keys(); A.sort()
for a in A:
    output.__setattr__(a, data.attributes[a])

import pdb; pdb.set_trace()

output.createDimension('scan', int(data.attributes['nvalues']))

print "\nVariabes"
cdf_variables = []
for k in data.keys():
    cdf_variables[k] = output.createVariable(k, 'd', ('scan',))
    for a in data[k].attributes.keys():
        print "\t\033[93m%s\033[0m: %s" % (a, data[k].attributes[a])


output.close()
