#Metview Macro

#  **************************** LICENSE START ***********************************
# 
#  Copyright 2019 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      : laplacian
#
# Syntax        : fieldset laplacian(fs:fieldset)
#                                          
# Category      : DERIVATIVES
#
# OneLineDesc   : Computes horizontal Laplacian of fields
#
# Description   : Compute the horizontal Laplacian of fields defined on a 
#	              regular lat-lon grid. The computation uses a second order accuracy
#                 finite difference scheme.
#
# Parameters    : fs - fieldset 
#                 
# Return Value  : resulting fieldset
#
# Dependencies  : none
#
# Example Usage : 
#                 
#
# **************************************************************************

function laplacian(_fs:fieldset)

    _res = nil
    
    #radius of Earth
    _R = 6371200.0
  
    # extract metadata keys
    _keys = grib_get(_fs,["gridType","paramId"])
   
    for _i=1 to count(_fs) do

        # get metadata keys
        _grid_type = _keys[_i][1]
      
        # check if grid is regular latlon 
        if _grid_type <> "regular_ll" then
            fail("divergence: [field=",_i,"] - unsupported grid (=",_grid_type,"), implemented only for regular lat-lon grid")
        end if
                  
        # compute the Laplacian
        _d1x = first_derivative_y(_fs[_i])
        _d2x = second_derivative_x(_fs[_i])
        _d2y = second_derivative_y(_fs[_i])
        _lap = _d2x + _d2y - _d1x[_i]*tanlat(_fs[_i])/_R
           	    
        _res = _res & _lap
        
    end for
    
    return _res
    
end laplacian 
