def mergeWCS(default_wcs, user_pars):
    """ Merges the user specified WCS values given as dictionary derived from
        the input configObj object with the output PyWCS object computed
        using distortion.output_wcs().

        The user_pars dictionary needs to have the following set of keys::

            user_pars = {'ra':None,'dec':None,'scale':None,'rot':None,
                         'outnx':None,'outny':None,'crpix1':None,'crpix2':None}
    """
    #
    # Start by making a copy of the input WCS...
    #
    outwcs = default_wcs.deepcopy()

    # If there are no user set parameters, just return a copy of
    # the original WCS:
    if all([upar is None for upar in user_pars.values()]):
        return outwcs

    if _check_custom_WCS_pars('ra', 'dec', user_pars):
        _crval = (user_pars['ra'], user_pars['dec'])
    else:
        _crval = None

    if ('scale' in user_pars and user_pars['scale'] is not None and
        not _check_close_scale(user_pars['scale'], outwcs.pscale)):
        _scale = user_pars['scale']
        _ratio = outwcs.pscale / _scale
    else:
        _ratio = None
        _scale = None

    if ('rot' not in user_pars) or user_pars['rot'] is None:
        _delta_rot = None
    else:
        _delta_rot = outwcs.orientat - user_pars['rot']
        if _delta_rot == 0.0:
            _delta_rot = None

    if _check_custom_WCS_pars('crpix1', 'crpix2', user_pars):
        _crpix = (user_pars['crpix1'], user_pars['crpix2'])
    else:
        _crpix = None

    shape = None

    if _check_custom_WCS_pars('outnx', 'outny', user_pars):
        shape = (
            int(_py2round(user_pars['outnx'])),
            int(_py2round(user_pars['outny']))
        )

        if shape[0] < 1 or shape[1] < 1:
            raise ValueError("Custom WCS output image size smaller than 1")

        if _crpix is None:
            # make sure new image is centered on the CRPIX of the old WCS:
            _crpix = ((shape[0] + 1.0) / 2.0, (shape[1] + 1.0) / 2.0)

    else:
        naxis1, naxis2 = outwcs.pixel_shape
        if _delta_rot is None:
            # no rotation is involved

            if _ratio is not None:
                # apply scale only:

                # compute output image shape:
                shape = (
                    max(1, int(_py2round(_ratio * naxis1))),
                    max(1, int(_py2round(_ratio * naxis2)))
                )

                # update CRPIX:
                if _crpix is None:
                    _crpix = 1.0 + _ratio * (outwcs.wcs.crpix - 1.0)

        else:
            _corners = np.array(
                [[0.5, 0.5],
                 [naxis1 + 0.5, 0.5],
                 [0.5, naxis2 + 0.5],
                 [naxis1 + 0.5, naxis2 + 0.5]]
            ) - outwcs.wcs.crpix

            if _ratio is not None:
                # scale corners:
                _corners *= _ratio

            # rotate corners and find new image range:
            ((_xmin, _xmax), (_ymin, _ymax)) = util.getRotatedSize(_corners,
                                                                   _delta_rot)

            # compute output image shape:
            # NOTE: _py2round may be replaced with np.ceil
            shape = (
                max(1, int(_py2round(_xmax - _xmin))),
                max(1, int(_py2round(_ymax - _ymin)))
            )

            if _crpix is None:
                # update CRPIX:
                _crpix = (-_xmin + 0.5, -_ymin + 0.5)

    # Set up the new WCS based on values from old one:

    if _ratio is not None:
        # Update plate scale
        outwcs.wcs.cd = outwcs.wcs.cd / _ratio
        outwcs.pscale = _scale

    # update orientation
    if _delta_rot is not None:
        outwcs.wcs.cd = _rotateCD(outwcs.wcs.cd, _delta_rot)
        outwcs.orientat -= _delta_rot

    if shape is not None:
        # update size:
        outwcs.pixel_shape = shape

    # update reference position
    if _crpix is not None:
        outwcs.wcs.crpix = np.array(_crpix, dtype=np.float64)

    if _crval is not None:
        outwcs.wcs.crval = np.array(_crval, dtype=np.float64)

    return outwcs