def mkpath(filepath, workpath, subpath=''):
    """
    Combine file fragments workpath and filepath. Optionally add a subpath of
    workpath to filepath. When path is new, make needed directories before
    returning the combined path.

    @param filepath: local file path fragment.
    @type filepath: basestring
    @param workpath: local work path fragment.
    @type workpath: basestring
    @param subpath: Optional file path fragement.
    @type subpath: basestring
    @return: Qualified full file path
    @rtype: basestring
    @requires: os
    """
    import os
    pathtofile = os.path.split(filepath)
    workpath = os.path.join(workpath, subpath, pathtofile[0])
    if not os.path.exists(workpath):
        os.makedirs(workpath)
    return os.path.join(workpath, pathtofile[1])
