#Metview Macro

#  **************************** LICENSE START ***********************************
# 
#  Copyright 2016 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_filter
#
# Syntax        : fieldset flexpart_filter(source: string,
#                                          data:  fieldset,
#                                          param: string, 
#                                          levType: string,                                                                       
#                                          level: number,
#                                          step: number, 
#                                          release: number,                                          
#                                          ageclass: number)
#
# Category      : FLEXPART
#
# OneLineDesc   : Extract fields from FLEXPART output GRIB files
#
# Description   : 
#
# Parameters    : source - the FLEXPART output GRIB file
#                 data  -  the FLEXPART output GRIB as a fieldset. It takes
#                          precedence over source.  
#                 param -  the shortName of the parameter to extract
#                 levType - the type of level to extract. Possible 
#                           values: "hl" and "sfc". The default is "hl".
#                 level - the levels to extract
#                 step  - the forecast steps to extract
#                 release - the releases to extract. Release indexing starts
#                            at 1. 
#                 ageclass - the ageclasses to extract. Ageclass indexing starts
#                            at 1. 
#                 
# Return Value  : fieldset
#
# Dependencies  : none
#
# Example Usage : 
#                 
#
# **************************************************************************

function flexpart_filter

    ARG=arguments()

    if mod(count(ARG),2) <> 0 then
        fail("incorrect numbe of arguments are specified!")
    end if        
     
    #print(ARG) 
     
    data=nil
    inFile=""
    param=""
    levType="heightAboveGround"
    lev=-1
    step=-1E18
    release=-1
    ageclass=-1
       
    for i=1 to count(ARG) by 2 do
        
        key=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 for data!")
            end if                
        else if key = "source" then
            inFile=val
        else if key = "param" then   
            param=val
        else if key =  "levType" then
            levType = val
            if levType="hl" then
                levType="heightAboveGround"
            end if   
        else if key = "level" then
            lev=val
        else if key = "step" then
            step = val
        else if key = "release" then
            release = val        
        else if key = "ageclass" then
            ac = val   
        end if                  
    
    end for   
    
    #print("inFile=",inFile)
    #print("levType=",levType)
    #print("lev=",lev)
    
    #Read file
    if data = nil then
        if length(inFile) > 0 and inFile <> "" then     
            data=read(inFile)
        else
            return nil
        end if    
    end if    
    
    res=nil
    
    keys=["shortName","typeOfLevel"]
    refs=[param,levType]
    
    if levType <> "sfc" and lev > 0 then
        keys = keys & ["level:l"]
        refs = refs & [lev]
    end if
    
    if release > 0 then
        keys = keys & ["releaseNumber:l"]
        refs = refs & [release]
    end if 
    
    if ageclass > 0 then
        keys = keys & ["ageClass:l"]
        refs = refs & [ageclass]
    end if 
    
    if step > -1E17 then
        keys = keys & ["step:l"]
        refs = refs & [step]
    end if 
    
    #print(keys)
    #print(refs)
    
    lst=grib_get(data,keys)
    
    for i=1 to count(data) do
        v=lst[i]
        if v = refs then
            res = res & data[i]
        end if
    end for    

    return res


end flexpart_filter