def ndar_parse_filename(fname):
    """
    Extract SID and protocol string from filename in the form sub-<SID>_<Protocol String>.[nii or nii.gz]
    
    :param fname:
    :return: SID, prot, fstub
    """

    # Init return values
    SID, prot, fstub = 'None', 'None', 'None'

    # Strip .nii or .nii.gz from fname
    fstub = fname.replace('.nii.gz','').replace('.nii','')

    # Split stub at first underscore
    for chunk in fstub.split('_', 1):
        if chunk.startswith('sub-'):
            # SID is everything after "sub-" in this chunk
            _, SID = chunk.split('sub-', 1)
        else:
            prot = chunk

    return SID, prot, fstub