# Metview Macro


# Define our range of dates

date_start = 2012-07-27
num_days  = 16


# Build our set of dates and values for the curves

dates    = nil
values_sin  = nil
values_cos  = nil

for i = 0 to num_days by hour(6) do
    dates  = dates & ([date_start + i])
    values_sin = values_sin & [sin(i)]
    values_cos = values_cos & [cos(i)]
end for


# graph plotting attributes

graph_attrib_sin    = mgraph
(
    legend            : "on",
    graph_line_colour : "blue",
    graph_line_style  : "dash"
)

graph_attrib_cos    = mgraph
(
    legend            : "on",
    graph_line_colour : "red"
)


# define the curves and associate them with the plotting attributes

curve_def_sin = input_visualiser
(
    input_x_type          : "date",
    input_date_x_values   : dates,
    input_y_values        : values_sin
)

curve_def_cos = input_visualiser
(
    input_x_type          : "date",
    input_date_x_values   : dates,
    input_y_values        : values_cos
)


legend_curves = mlegend
(
    legend_display_type         : "disjoint",
    legend_entry_plot_direction : "row", #column
    legend_text_composition     : "user_text_only",
    legend_border_colour        : "black",
    legend_text_font_size       : 0.3,
    legend_user_lines           : ['sin curve', 'cos curve']
)



# define the plot's title and legend attributes

text_plot = mtext
(
    text_line_1 : "Example curve plot"
)



# Define the output media

to_psfile = ps_output
(
    output_name : "curves.ps"
)


# Check the runmode and decide which media to putput the plot to
    
mode = runmode()
if       (mode = "execute")    then setoutput(to_psfile)
else if  (mode = "batch")      then setoutput(to_psfile)
else if  (mode = "visualise")  then print('screen')
else     fail("Only execute, batch and visualise allowed")
end if

# Call function to build layout (defined at end of macro)

dw = build_layout (dates)


# Plot the curves

plot (dw, curve_def_sin, graph_attrib_sin, curve_def_cos, graph_attrib_cos, text_plot, legend_curves)


#################################################################
#
#                End of Main Macro
#
#################################################################


# Function to build the layout

function build_layout(dates : list)

    horizontal_axis = maxis
    (
        axis_date_type : 'days'
    )

    vertical_axis = maxis
    (
        axis_title_text  : 'y-axis title',
        axis_orientation : 'vertical'
    )

    cview = cartesianview
    (  
        x_axis_type     : "date",
        x_date_min      : dates [1],
        x_date_max      : dates [count(dates)],
        y_axis_type     : "regular",
        y_automatic     : 'on',
        horizontal_axis : horizontal_axis,
        vertical_axis   : vertical_axis
    )

    page1 = plot_page
    (
        view  : cview
    )

    _display_window_ = plot_superpage
    (
        pages : [page1]
    )

    return _display_window_

end build_layout