#Metview Macro

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

#=================================================================================
# Internal utility function to make mvl field-based computations that
# require pressure easier.
#
# OneLineDesc   : Internal utility function
#
# Input: 
#       field_o: other fieldsset
#       field_p: pressure or lnsp fieldset, or pressure vector
#       field_label_o: the labe used for field_o in error messages
#       fn_name: name of the caller function
# Return:
#       list with 2 elements: 
#           1: pressure fieldset or vector
#           2: field indicating the field_o is a pressure level fieldset
#==============================================================================

function __prepare_pressure_field_arg(field_o, field_p, field_label_o, fn_name)

    has_pl_fields = 0
    keys_o = nil
    keys_p = nil
    
    if type(field_o) = "fieldset" then
        levType = ""
        keys_o = grib_get(field_o, ["gridType", "typeOfLevel"])
        levType = keys_o[1][2]
        
        # check o_field grid type
        for i=1 to count(keys_o) do
            if keys_o[i][1] = "sh" then
                fail(fn_name & 
                    ": spherical harmonics fields are not supported! [" &
                    field_label_o & "-field=]" & i)
            end if
        end for
        
        # model levels (=ECMWF hybrid/eta)
        if levType = "hybrid" then
            if type(field_p) <> "fieldset" then
               fail(fn_name & 
                    ": for ECMWF model level data argument p must be a fieldset and not " &
                    type(field_p) & "!")
            end if  
                    
            keys_p = grib_get(field_p[1], ["gridType", "typeOfLevel", "paramId:l"]) 
            
            # pressure is defined by an lnsp field
            if keys_p[1][2] = "hybrid" and keys_p[1][3] = 152 then
                if count(field_p) <> 1 then
                    fail(fn_name & 
                        ": for model levels only one field can be specified as lnsp for second argument!")
                end if
                # computes the pressure on the levels in field_o
                field_p = unipressure(field_p[1], field_o)
            # pressure is defined by a set of pressure fields
            else if count(field_p) <> count(field_o) then
                fail(fn_name & 
                    ": for model level data if the second argument is not an lnsp fieldset it must contain the same number of pressure fields as " &
                    field_label_o & "!") 
            end if
        
        # pressure levels (the actual pressure values must be specified as a vector)
        else if levType = "isobaricInPa" or levType = "isobaricInhPa" then
            if type(field_p) <> "vector" then
                 fail(fn_name & ": for pressure level data argument p must be a vector!")
            end if 
            if count(field_o) <> count(field_p) then
                 fail(fn_name & ": number of fields in " & field_label_o & 
                    " (=" & count(field_o) & 
                    ") is different than size of vector p (=" & count(field_p) & "!")
            end if 
            has_pl_fields = 1
        
        # other level type, pressure is defined as a fieldset    
        else if count(field_p) <> count(field_o) then
            fail(fn_name & ": for typeOfLevel=" & levType & 
            " the second argument must contain the same number of pressure fields as many fields there are in " & 
            field_label_o & "!") 
        end if
        
        # check grid type for pressure fields       
        if type(field_p) = "fieldset" then
            for i=1 to count(keys_p) do
                if keys_p[i][1] = "sh" then
                    fail(fn_name & 
                        ": spherical harmonics fields are not supported! [p-field=]" & i)
                end if
            end for
        end if
    end if     
   
    # sanity check
    if has_pl_fields = 1 and type(field_p) <> "vector" then
        fail(fn_name & ": Internal error! Incositency in pressure field preparation: " &
                "has_pl_fields = 1 but type(field_p) = " & type_field_p & "!")
    end if
    
    return [field_p, has_pl_fields]
    
end __prepare_pressure_field_arg