#Metview Macro

#  **************************** LICENSE START ***********************************
# 
#  Copyright 2017 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 ************************************
# 

# **************************************************************************
# Function      : flexpart_title
#
# Syntax        : mtext flexpart_title (data: fieldset,
#                                      fontsize: number,
#                                      units: string)
#
# Category      : FLEXPART
#
# OneLineDesc   : Generates title for plotting FLEXPART gridded output fields
#
# Description   : 
#
# Parameters    : data  - FLEXPART fieldset
#                 fontsize  - character height in cm. Default value: 0.3
#                 units - the units string. If it is set to "auto" the units are taken form the 
#                         GRIB header. Default value: empty string.
#                 
#
# Return Value  : mtext
#
# Dependencies  : none
#
# Example Usage : 
#                 
#
# **************************************************************************

function flexpart_build_title

    ARG=arguments()

    if mod(count(ARG),2) <> 0 then
        fail("incorrect numbe of arguments are specified!")
    end if        
     
    #print(ARG) 
     
    data=nil
    fontSize=0.3
    units=""
    lev=""
    
    for i=1 to count(ARG) by 2 do
        
        key=lowercase(ARG[i])
        val=ARG[i+1]
        
        if key = "data" then
            data=val
            if type(data) <> "fieldset" then
                fail("Invalid argument specified for data!")
            end if
            if data = nil then
                fail("Invalid fieldset specified in data!") 
            end if              
        else if key = "fontsize"  then
            fontSize=val
        else if key = "level"  then
            lev=val
        else if key = "units" then
            units = val
        end if    
           
    end for     

    #the type of level
    levType=grib_get_string(data[1],"typeOfLevel")   
    
    #Ad param name from grib header
    txt="FLEXPART" & " Par: <grib_info key='shortName'/> <font color='white'> . </font>"
    
    #Add units  
    if units = "header" then
        txt=txt & "[<grib_info key='units'/>]"      
    else if units <> "" and units <> " " then
        txt=txt & "[" & units & "]"
    end if    
  
    #Add species
    txt=txt & "<font color='white'> . </font> Species: <grib_info key='speciesName'/>"
 
    #Add level
    if lev <> "" then
        txt=txt & "Level: " & lev
    else if levType = "heightAboveGround" then
        txt=txt & "Level: <grib_info key='level'/>m"
    end if
    
    #Add date,time and step
    txt=txt & "<font color='white'> . </font>Step: <grib_info key='step'/>h Valid: <grib_info key='valid-date' format='%Y-%m-%d %H'/>"   

    #Build the title
    return  mtext(
		text_line_1: txt,
		text_font_size: fontSize	
		)

end flexpart_build_title