#!/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/>.
'''

## Composites weather radar data directly from polar scans or volumes 

## @file
## @author Daniel Michelson, SMHI
## @date 2012-01-22


## Main function. 
# @param options a set of parsed options from the command line 
def main(options):
    import _raveio
    import rave_composite

    infiles = options.infiles.split(",")

    objects = []
    for filename in infiles:
        rio = _raveio.open(filename)
        if rio.objectType is _raveio.Rave_ObjectType_SCAN or rio.objectType is _raveio.Rave_ObjectType_PVOL:
            objects.append(rio.object)
        else:
            print "Input file %s is neither polar scan or volume, ignoring." % filename

    comp = rave_composite.generate(objects, area = options.area,
                                   quantity = options.quantity,
                                   product = options.product,
                                   prodpar = options.prodpar,
                                   range = options.range, 
                                   gain = options.gain,
                                   offset = options.offset,
                                   date = options.date,
                                   time = options.time,
                                   method = options.method,
                                   qc = options.qc,
                                   gf = options.gf)
    
    rio = _raveio.new()
    rio.object = comp
    rio.filename = options.outfile
    rio.save()


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

    from rave_defines import GAIN, OFFSET

    usage = "usage: %prog -i <infile(s)> -o <outfile> -a <area> [args] [h]"
    usage += "\nGenerates weather radar composites directly from polar scans and volumes."
    parser = OptionParser(usage=usage)

    parser.add_option("-i", "--input", dest="infiles",
                      help="Name of input file(s) to composite, comma-separated in quotations.")

    parser.add_option("-o", "--output", dest="outfile",
                      help="Name of output file to write.")

    parser.add_option("-a", "--area", dest="area",
                      help="Name of Cartesian area to which to generate the composite.")

    parser.add_option("-q", "--quantity", dest="quantity",
                      default="DBZH",
                      help="The radar parameter to composite. Default=DBZH.")

    parser.add_option("-p", "--product", dest="product",
                      default="PCAPPI",
                      help="The type of Cartesian product to generate [PPI, CAPPI, PCAPPI, PMAX]. Default=PCAPPI.")

    parser.add_option("-P", "--prodpar", dest="prodpar",
                      type="float", default=1000.0,
                      help="Product parameter. For (P)CAPPIs it is the height of the desired layer. For PPIs, it is the elevation angle. Default=1000.0 (meters).")

    parser.add_option("-r", "--range", dest="range",
                      type="float", default=200000.0,
                      help="Maximum range to apply PMAX algorithm. Applies only to PMAX algorithm. Defaults to 200 km.")

    parser.add_option("-g", "--gain", dest="gain",
                      type="float", default=GAIN,
                      help="Linear gain applied to output data. Default=as defined in rave_defines.py.")

    parser.add_option("-O", "--offset", dest="offset",
                      type="float", default=OFFSET,
                      help="Linear offset applied to output data. Default=as defined in rave_defines.py.")

    parser.add_option("-d", "--date", dest="date",
                      default=None,
                      help="Nominal date of the composite to be written. Defaults to the nominal date of the last input file.")

    parser.add_option("-t", "--time", dest="time",
                      default=None,
                      help="Nominal time of the composite to be written. Defaults to the nominal time of the last input file.")

    parser.add_option("-m", "--method", dest="method",
                      default="NEAREST_RADAR",
                      help="Compositing algorithm to apply. Current choices are NEAREST_RADAR or HEIGHT_ABOVE_SEALEVEL. Default=NEAREST_RADAR.")

    parser.add_option("-Q", "--qc", dest="qc",
                      default="",
                      help="Which quality-controls to apply. Current choices are 'ropo', 'beamb', and 'rave-overshooting'. Default=None")

    parser.add_option("-G", "--gap-fill", dest="gf",
                      default="False",
                      help="Gap-fill small holes in output composite. Default=False")

    (options, args) = parser.parse_args()

    if options.infiles != None and options.outfile != None and options.area != None:

        main(options)

    else:
        parser.print_help()
