def subCommands(cmd, nFiles, nBatches, func, chunks=True):
    """
    Each batch handles a slice of the 2D XRF map.
    Two slicing strategies are supported:

    ..code: python

        if chunks:
            for mcaoffset in range(nBatchesPerFile):
                for filebeginoffset, fileendoffset in ...:
                    image[filebeginoffset:fileendoffset:1, mcaoffset:None:nBatchesPerFile] 
        else:
            for mcaoffset in range(nBatchesPerFile):
                for filebeginoffset in range(nChunks):
                    image[filebeginoffset:None:nFilesPerChunk, mcaoffset:None:nBatchesPerFile] 
    
    :param Command cmd:
    :param num nFiles: number of files to be process
    :param num nBatches: number sub processes
    :param callable func: signature `(Command)`
    :param chunks: 
    """
    cmd.addOption('filebeginoffset', value=0)
    cmd.addOption('fileendoffset', value=0)
    cmd.addOption('filestep', value=1)
    cmd.addOption('mcaoffset', value=0)
    cmd.addOption('mcastep', value=1)
    cmd.addOption('chunk', value=0)
    nFilesPerChunk = min((nFiles + nBatches - 1) // nBatches, nFiles)
    nChunks = (nFiles + nFilesPerChunk - 1) // nFilesPerChunk
    nBatchesPerFile = max(nBatches // nChunks, 1)
    if chunks:
        cmd.mcastep = nBatchesPerFile
        cmd.filestep = 1
        for i in range(nBatchesPerFile):
            cmd.mcaoffset = i
            for j in range(nChunks):
                filebeginoffset = j * nFilesPerChunk
                fileend = min(filebeginoffset + nFilesPerChunk, nFiles)
                if filebeginoffset >= fileend:
                    break
                cmd.filebeginoffset = filebeginoffset
                cmd.fileendoffset = nFiles - fileend
                cmd.chunk = i * nChunks + j
                func(cmd)
    else:
        cmd.mcastep = nBatchesPerFile
        cmd.filestep = nChunks
        cmd.fileendoffset = 0
        for i in range(nBatchesPerFile):
            cmd.mcaoffset = i
            for j in range(nChunks):
                cmd.filebeginoffset = j
                cmd.chunk = i * nChunks + j
                func(cmd)
