#Metview Macro

#  **************************** LICENSE START ***********************************
# 
#  Copyright 2018 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      : divergence
#
# Syntax        : fieldset divergence(fs_x:fieldset,fs_y:fieldset)
#                                          
# Category      : DERIVATIVES
#
# OneLineDesc   : Compute horizontal divergence of vector fields
#
# Description   : Compute the horizontal divergence of vector fields defined on a 
#	              regular lat-lon grid. The computation uses a second order accuracy
#                 finite difference scheme.
#
# Parameters    : fieldset_x - x component of vector field 
#                 fieldset_y - y component of vector field
#                 
# Return Value  : resulting fieldset
#
# Dependencies  : none
#
# Example Usage : 
#                 
#
# **************************************************************************
function divergence(_fs_x:fieldset,_fs_y:fieldset)

    if count(_fs_x) <> count(_fs_y) then
        fail("divergence: different number of fields in fieldsets [fieldset1=",count(_fs_x),", fieldset2=",count(_fs_y),"]")
    end if          
   
    _res = nil
    
    #radius of Earth
    _R = 6371200.0
  
    # extract metadata keys
    _keys_x = grib_get(_fs_x,["gridType","paramId"])
    _keys_y = grib_get(_fs_y,["gridType","paramId"])
    
    for _i=1 to count(_fs_x) do

        # get metadata keys
        _grid_type_x = _keys_x[_i][1]
        _grid_type_y = _keys_y[_i][1]
       
        # check if grid is regular latlon 
        if _grid_type_x <> "regular_ll" then
            fail("divergence: [fieldset_x, field=",_i,"] - unsupported grid (=",_grid_type_x,"), implemented only for regular lat-lon grid")
        end if
        
        if _grid_type_y <> "regular_ll" then
            fail("divergence: [fieldset_y, field=",_i,"] - unsupported grid (=",_grid_type_y,"), implemented only for regular lat-lon grid")
        end if
        
        # compute divergence
        _dx = first_derivative_x(_fs_x[_i])
        _dy = first_derivative_y(_fs_y[_i])       
        _d = _dx + _dy - _fs_y[_i]*tanlat(_fs_y[_i])/_R
                    
        #set paramid to divergence when x and y components are 
        #10u/10v or u/v.
        _param_x = _keys_x[_i][2]
        _param_y = _keys_y[_i][2]
        if (_param_x = "165" and _param_y = "166") or
           (_param_x = "131" and _param_y = "132") then
            _d = grib_set_long(_d,["paramId",155,"generatingProcessIdentifier",142])      
        end if
               	    
        _res = _res & _d
        
    end for
    
    return _res
    
end divergence    
