<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def datadir(*subdirs):
 """Get a path within the CASA data directory. subdirs Extra elements to append to the returned path. This function locates the directory where CASA resource data files (tables of time offsets, calibrator models, etc.) are stored. If called with no arguments, it simply returns that path. If arguments are provided, they are appended to the returned path using :func:`os.path.join`, making it easy to construct the names of specific data files. For instance:: from pwkit.environments.casa import util cal_image_path = util.datadir('nrao', 'VLA', 'CalModels', '3C286_C.im') tb = util.tools.image() tb.open(cal_image_path) """

    import os.path
    data = None

    if 'CASAPATH' in os.environ:
        data = os.path.join(os.environ['CASAPATH'].split()[0], 'data')

    if data is None:
        # The Conda CASA directory layout:
        try:
            import casadef
        except ImportError:
            pass
        else:
            data = os.path.join(os.path.dirname(casadef.task_directory), 'data')
            if not os.path.isdir(data):
                # Sigh, hack for CASA 4.7 + Conda; should be straightened out:
                dn = os.path.dirname
                data = os.path.join(dn(dn(dn(casadef.task_directory))), 'lib', 'casa', 'data')
                if not os.path.isdir(data):
                    data = None

    if data is None:
        import casac

        prevp = None
        p = os.path.dirname(casac.__file__)
        while len(p) and p != prevp:
            data = os.path.join(p, 'data')
            if os.path.isdir(data):
                break
            prevp = p
            p = os.path.dirname(p)

    if not os.path.isdir(data):
        raise RuntimeError('cannot identify CASA data directory')

    return os.path.join(data, *subdirs)