def copyDimension(ncin, dim, fix=False, fail=True):
    """
    Arguments
    ---------
    ncin            : Instance of an object with a createDimension method
                      (i.e. NcDataset, NcGroup)
    group           : Instance of NcDimension
    fix (optional)  : convert an unlimited dimension to a fixed size dimension
    fail (Optional[bool]): raise an exception if dimension already exists

    Return
    ------
    netCDF4.Dimension

    Purpose
    -------
    Copy the given dimension to ncin
    """
    length = None if dim.isunlimited() and not fix else len(dim)
    try:
        return ncin.createDimension(dim.name, length)
    except Exception:
        if fail:
            raise
        return ncin.dimensions[dim.name]