#Metview Macro

# **************************** LICENSE START ***********************************
#
# Copyright 2012 ECMWF. This software is distributed under the terms
# of the Apache License version 2.0. In applying this license, ECMWF does not
# waive the privileges and immunities granted to it by virtue of its status as
# an Intergovernmental Organization or submit itself to any jurisdiction.
#
# ***************************** LICENSE END ************************************

# prevents macro from working except if with Parameters

mode = runmode()

if not(mode = "edit") then
	fail("Error : Must use Macro with Parameters")
end if



on edit

    # define the user interface components
    a = icon(
            name  : "input data",
            class : "GRIB"
            )

    b = any(
            name    : "verif date (yyyy-mm-dd)",
            default : "2012-03-01",
            help    : "help_script",
            help_script_command : "echo Dates must be in YYYY-MM-DD format"
            )

    c = slider(
            name    : "days",
            min     : 1,
            max     : 10,
            default : 5
            )

    d = option_menu(
            name        : "parameter",
            values      : ["UV", "t"],
            default     : "t"
            )
    
    e = any(
            name        : "area",
            help        : "help_input",
            input_type  : "area",
            default     : [30,-25,50,65]  # S, W, N, E
            )

    f = option_menu(
            name        : "contour type",
            values      : ["isolines", "shaded"],
            default     : "isolines"
            )

    g = toggle(
             name       : 'land shade', default: 'off'
            )

    return [a, b, c, d, e, f, g]
end edit


on visualise(input)
    indata      = input["input data"]
    vf_date     = input["verif date (yyyy-mm-dd)"]
    n_of_days   = input["days"]
    par         = input["parameter"]
    the_area    = input["area"]
    contouring 	= input["contour type"]
    land_shade_on = input["land shade"]

    # translate "UV" into a list, since that is what 'read' understands
    if (par = "UV") then
        par = ["u", "v"]
    end if

    print (vf_date, "   ", n_of_days, "   ", par)
    
    # Define some visual definitions
    
    pos = mcont(
            contour_line_thickness       : 2,
            contour_line_colour          : "red",
            contour_highlight            : "off",
            contour_level_selection_type : "level_list",
            contour_max_level            : 10,
            contour_min_level            : 0.5,
            contour_level_list           : [0.5,1,2,4,10]
            )
    
    neg = mcont(
            contour_line_thickness       : 2,
            contour_highlight            : "off",
            contour_level_selection_type : "level_list",
            contour_max_level            : -0.5,
            contour_min_level            : -10,
            contour_level_list           : [-10,-4,-2,-1,-0.5]
            )

    diff_shade = mcont(
            legend                       : "on",
            contour_line_colour          : "black",
            contour_highlight            : "off",
            contour_level_selection_type : "level_list",
            contour_max_level            : 20,
            contour_min_level            : -20,
            contour_shade_max_level      : 20,
            contour_shade_min_level      : -20,
            contour_level_list           : [-20,-9,-6,-3,-1.5,-1,-0.5,0.5,1,1.5,3,6,9,20],
            contour_label_height         : 0.15,
            contour_label_frequency      : 1,
            contour_shade                : "on",
            contour_shade_method         : "area_fill",
            contour_shade_colour_method  : "list",
            contour_shade_colour_list    : ["violet","bluish_purple","lavender","purplish_blue","sky","blue_green","white","cream","orange_yellow","yellowish_orange","orange","tangerine","rust"]
            )


    coloured_wind = mwind(
            legend                                : "on",
            wind_arrow_legend_text                : "m/s",
            wind_advanced_method                  : "on",
            wind_advanced_colour_max_level_colour : "red",
            wind_advanced_colour_min_level_colour : "blue",
            wind_advanced_colour_direction        : "clockwise",
            wind_thinning_factor                  : 1
            )
    
    
    mycoast = (
            map_coastline_resolution        : "low",
            map_coastline_thickness         : 3,
            map_coastline_land_shade        : "off",
            map_coastline_land_shade_colour : "grey",
            map_coastline_sea_shade         : "on",
            map_coastline_sea_shade_colour  : "RGB(0.9,0.95,1)",
            map_grid_longitude_increment    : 10
            )

    if (land_shade_on = 'on') then
         mycoast = (mycoast, map_coastline_land_shade : "on")
    end if

    acoast = mcoast(mycoast)

    ps_atlantic = geoview(
            map_projection      : "polar_stereographic",
            map_area_definition : "corners",
            area                : the_area,
            coastlines          : acoast
            )
    
    
    # Define the display window
    
    page = plot_page(
            view  : ps_atlantic
            )
    
    dw = plot_superpage(
            custom_width    : 29.7,
            custom_height   : 21.0,
            pages           : page
            )
    
    
    # Check which visual definition to use: contour or wind arrows?
    
    if (par = ['u', 'v']) then
        visdef = coloured_wind
    else
        a = 1  # for syntax correctness!
        if contouring = 'isolines' then
            visdef = [pos, neg]
        else
            visdef = diff_shade
        end if
    end if
    
    
    
    # Derive the difference field
    
    fa_diff = fc_an_diff(indata, par, vf_date, n_of_days)
    
    
    # Plot the result
    
    plot (dw, fa_diff, visdef)
end visualise


# exit program if not visualised
on execute(input)
    fail("Error : Visualise only")
end execute

on save(input)
    fail("Error : Visualise only")
end save

on examine(input)
    fail("Error : Visualise only")
end examine

on prepare(input)
    fail("Error : Visualise only")
end prepare



# ===================================================================

# A function to compute a difference field
# Author, date, restrictions, argument types, etc,...

function fc_an_diff (infile, par, vf_date, n_of_days)

    fc = read(
        type    : "fc",
        param   : par,
        date    : vf_date - n_of_days,
        step    : n_of_days*24,
        data    : infile
        )

    an = read(
        type  : "an",
        param : par,
        date  : vf_date,
        step  : 0,
        data  : infile
        )

    return fc-an

end fc_an_diff

