import glob


def get_rad4alp_dir(basepath, voltime, radar_name='A', radar_res='L',
                    scan='001', path_convention='MCH'):
    """
    gets the directory where rad4alp data is stored

    Parameters
    ----------
    basepath : str
        base path
    voltime : datetime object
        nominal time
    radar_name : str
        radar name (A, D, L, P, W)
    radar_res : str
        radar resolution (H, L)
    scan : str
        scan
    path_convention : str
        The path convention. Can be 'LTE', 'MCH' or 'RT'

    Returns
    -------
    datapath : str
        The data path
    basename : str
        The base name. ex: PHA17213

    """
    dayinfo = voltime.strftime('%y%j')
    basename = 'M'+radar_res+radar_name+dayinfo
    if path_convention == 'LTE':
        yy = dayinfo[0:2]
        dy = dayinfo[2:]
        subf = 'M'+radar_res+radar_name+yy+'hdf'+dy
        datapath = basepath+subf+'/'

        # check that M files exist. if not search P files
        dayfilelist = glob.glob(datapath+basename+'*.'+scan+'*')
        if not dayfilelist:
            subf = 'P'+radar_res+radar_name+yy+'hdf'+dy
            datapath = basepath+subf+'/'
            basename = 'P'+radar_res+radar_name+dayinfo
    elif path_convention == 'MCH':
        datapath = basepath+dayinfo+'/'+basename+'/'

        # check that M files exist. if not search P files
        dayfilelist = glob.glob(datapath+basename+'*.'+scan+'*')
        if not dayfilelist:
            basename = 'P'+radar_res+radar_name+dayinfo
            datapath = basepath+dayinfo+'/'+basename+'/'
    else:
        datapath = basepath+'M'+radar_res+radar_name+'/'

        # check that M files exist. if not search P files
        dayfilelist = glob.glob(datapath+basename+'*.'+scan+'*')
        if not dayfilelist:
            basename = 'P'+radar_res+radar_name+dayinfo
            datapath = basepath+'P'+radar_res+radar_name+'/'

    return datapath, basename