def setfilename(imagefilename, imageindex, nbdigits=4, CCDLabel=None):
    """
    reconstruct filename string from imagefilename and update filename index with imageindex

    :param imagefilename: filename string (full path or not)
    :param imageindex: index in filename
    :type imageindex: string

    :return filename: input filename with index replaced by input imageindex
    :rtype: string
    """
    if imagefilename.endswith('mccd'):
        lenext = 5
        imagefilename = imagefilename[:-(lenext + nbdigits)
            ] + '{:04d}.mccd'.format(imageindex)
    elif CCDLabel in ('sCMOS', 'sCMOS_fliplr'):
        ext = 'tif'
        lenext = 4
        if imagefilename.endswith('tiff'):
            ext = 'tiff'
            lenext = 5
        if nbdigits is not None:
            if imagefilename.endswith(ext):
                imagefilename = imagefilename[:-(lenext + nbdigits)
                    ] + '{:04d}.{}'.format(imageindex, ext)
            elif imagefilename.endswith(ext + '.gz'):
                imagefilename = imagefilename[:-(lenext + 3 + nbdigits)
                    ] + '{:04d}.{}.gz'.format(imageindex, ext)
        elif imagefilename.endswith(ext):
            prefix, _ = imagefilename.split('.')
            prefix0 = prefix.split('_')[0]
            if imageindex > 9999:
                imagefilename = prefix0 + '_{}.{}'.format(imageindex, ext)
            else:
                imagefilename = prefix0 + '_{:04d}.{}'.format(imageindex, ext)
    elif CCDLabel in ('EIGER_4Mstack',):
        pass
    elif imagefilename.endswith('mar.tiff'):
        imagefilename = imagefilename[:-(9 + nbdigits)
            ] + '{:04d}_mar.tiff'.format(imageindex)
    elif imagefilename.endswith('mar.tif'):
        sname = imagefilename.split('_')
        if len(sname) >= 4:
            sname2 = sname[-2] + '_' + sname[-1]
            prefix = imagefilename.rstrip(sname2)
        else:
            prefix = imagefilename.split('_')[0]
        imagefilename = prefix + '_{}_mar.tif'.format(imageindex)
    elif imagefilename.endswith(('.tif', '.edf')):
        if CCDLabel in ('ImageStar_raw',):
            prefixwihtindex = imagefilename[:-4]
            prefixwithindex_list = list(prefixwihtindex)
            indexnodigit = 0
            while len(prefixwithindex_list) > 0:
                lastelem = prefixwithindex_list.pop(-1)
                print('lastelem', lastelem)
                if not lastelem.isdigit():
                    break
                indexnodigit += 1
            prefix = prefixwihtindex[:-indexnodigit]
            print('prefix', prefix)
            imagefilename = prefix + '{}.tif'.format(imageindex)
        else:
            suffix = imagefilename[-4:]
            prefix = imagefilename[:-(4 + nbdigits)]
            imagefilename = prefix + '{:04d}{}'.format(imageindex, suffix)
    elif imagefilename.endswith('mccd'):
        imagefilename = imagefilename[:-(5 + nbdigits)] + '{:04d}.mccd'.format(
            imageindex)
    elif imagefilename.endswith('edf'):
        imagefilename = imagefilename[:-(4 + nbdigits)] + '{:04d}.edf'.format(
            imageindex)
    elif imagefilename.endswith('unstacked'):
        imagefilename = imagefilename[:-(10 + nbdigits)
            ] + '{:04d}.unstacked'.format(imageindex)
    return imagefilename
