#!/usr/bin/python3.9
'''
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/>.
'''

## show Quick-Look viewer.

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

# Module/Project:
import rave_win_colors

## Dictionary containing palettes
PALS = {
    'DBZc': rave_win_colors.continuous_dBZ,
    'DBZd': rave_win_colors.discrete_dBZ_BLT,
    'MSc' : rave_win_colors.continuous_MS,
    'MSd' : rave_win_colors.discrete_MS,
    'BW'  : rave_win_colors.continuous_RAW,
}


## Main function. Uses "old" rave instead of _raveio to read data,
# but this is simply for convenience.
# @param sourcefile string of the input ODIM_H5 file
# @param SET string path to the dataset to show
# @param PAL string name of the palette with which to show the dataset
def main(sourcefile, SET, PAL):
    import rave
    import rave_ql
    
    this = rave.open(sourcefile)
    data = this.get(SET)
    
    that = rave_ql.ql(data, pal=PALS[PAL])
    that.main()


if __name__ == "__main__":
    import sys
    from optparse import OptionParser
    
    usage = "usage: %prog -i <infile> -d <dataset index> -p <palette name> [h]"
    usage += "\nPalette names:\tDBZc (continuous), DBZd (discrete)"
    usage += "\n\t\tMSc (continuous), MSd (discrete), BW (grayscale)"
    parser = OptionParser(usage=usage)
    # fmt: off
    parser.add_option("-i", "--input", dest="infile",
                      help="Name of input file to display.")

    parser.add_option("-d", "--dataset", dest="set",
                      default='/dataset1/data1/data',
                      help="Dataset name (default=/dataset1/data1/data)")

    parser.add_option("-p", "--palette", dest="pal",
                      default="DBZc",
                      help="Name of color palette to apply (DBZ, VRAD).")
    # fmt: on
    
    (options, args) = parser.parse_args()
    
    try:
        import pygtk
    except ImportError:
        parser.print_help()
        print("\nshow requires PyGTK. Exiting ...")
        sys.exit(1)
    pygtk.require('2.0')
    try:
        import gtk
    except ImportError:
        parser.print_help()
        print("\nshow requires GTK. Exiting ...")
        sys.exit(1)
    try:
        import threading
    except ImportError:
        parser.print_help()
        print("\nYou need Python compiled with threading support to run show.\nExiting ...")
        sys.exit(1)
    
    if options.infile is not None:
        main(options.infile, SET=options.set, PAL=options.pal)
    
    else:
        parser.print_help()
