#!/usr/bin/env python
'''
Copyright (C) 2012- Swedish Meteorological and Hydrological Institute (SMHI)

This file is part of RAVE.

RAVE is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

RAVE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with RAVE.  If not, see <http://www.gnu.org/licenses/>.
'''
## Command-line dealiasing

## @file
## @author Daniel Michelson, SMHI
## @date 2012-11-20

import sys
import _raveio
import _dealias


if __name__ == "__main__":
    from optparse import OptionParser

    description = "Dealiasing of polar scan and volume data"

    usage = "usage: %prog -i <infile> [-o <outfile>] [h]"
    parser = OptionParser(usage=usage, description=description)

    parser.add_option("-i", "--input", dest="ifile", help="Input file name")
    parser.add_option("-o", "--output", dest="ofile", help="Output file name. If not specified, input file will be overwritten.")

    (options, args) = parser.parse_args()

    if not options.ifile:
        parser.print_help()
        sys.exit()

    if not options.ofile: 
        options.ofile = options.ifile

    rio = _raveio.open(options.ifile)
    obj = rio.object

    if not _dealias.dealias(obj):
        print "Input data are already dealiased. Exiting ..."
        sys.exit(1)

    rio.object = obj
    rio.save(options.ofile)
