def get_environment(environmentConfig):
    try:
        # Open file.
        environment_config=open(environmentConfig, 'r')
        print ('\nUsing environment config: ' + environmentConfig + '\n')
    except IOError:
        mssg=str().join(['Cannot read environment configuration file:  ', environmentConfig])
        raise Exception(mssg)
        
    dom = xml.dom.minidom.parse(environment_config)
    
    # Caches
    try:
        cacheLocationElements = dom.getElementsByTagName('CacheLocation')
        cacheLocation_wmts = None
        cacheLocation_twms = None
        cacheBasename_wmts = None
        cacheBasename_twms = None
        for cacheLocation in cacheLocationElements:
            try:
                if str(cacheLocation.attributes['service'].value).lower() == "wmts":
                    cacheLocation_wmts = cacheLocation.firstChild.nodeValue.strip()
                    cacheBasename_wmts = cacheLocation.attributes['basename'].value
                elif str(cacheLocation.attributes['service'].value).lower() == "twms":
                    cacheLocation_twms = cacheLocation.firstChild.nodeValue.strip()
                    cacheBasename_twms = cacheLocation.attributes['basename'].value
            except KeyError:
                # Set to defaults
                cacheLocation_wmts = cacheLocation.firstChild.nodeValue.strip()
                cacheBasename_wmts = "cache_all_wmts" 
                cacheLocation_twms = cacheLocation.firstChild.nodeValue.strip()
                cacheBasename_twms = "cache_all_twms"                
                log_sig_warn("'service' or 'basename' not defined in <CacheLocation>"+cacheLocation_wmts+"</CacheLocation> Using defaults TWMS:'cache_all_twms', WMTS:'cache_all_wmts'", sigevent_url)    
    except IndexError:
        raise Exception('Required <CacheLocation> element is missing in ' + environmentConfig)
        
    # Services
    try:
        getTileService = get_dom_tag_value(dom, 'GetTileServiceLocation')
    except IndexError:
        getTileService = None
    
    getCapabilitiesElements = dom.getElementsByTagName('GetCapabilitiesLocation')
    wmts_getCapabilities = None
    twms_getCapabilities = None
    for getCapabilities in getCapabilitiesElements:
        try:
            if str(getCapabilities.attributes['service'].value).lower() == "wmts":
                wmts_getCapabilities = getCapabilities.firstChild.nodeValue.strip()
            elif str(getCapabilities.attributes['service'].value).lower() == "twms":
                twms_getCapabilities = getCapabilities.firstChild.nodeValue.strip()
        except KeyError:
            raise Exception('service is not defined in <GetCapabilitiesLocation>')
            
    serviceUrlElements = dom.getElementsByTagName('ServiceURL')
    wmtsServiceUrl = None
    twmsServiceUrl = None
    for serviceUrl in serviceUrlElements:
        try:
            if str(serviceUrl.attributes['service'].value).lower() == "wmts":
                wmtsServiceUrl = serviceUrl.firstChild.nodeValue.strip()
            elif str(serviceUrl.attributes['service'].value).lower() == "twms":
                twmsServiceUrl = serviceUrl.firstChild.nodeValue.strip()
        except KeyError:
            raise Exception('service is not defined in <ServiceURL>')      
 
    stagingLocationElements = dom.getElementsByTagName('StagingLocation')
    wmtsStagingLocation = None
    twmsStagingLocation = None
    for stagingLocation in stagingLocationElements:
        try:
            if str(stagingLocation.attributes['service'].value).lower() == "wmts":
                wmtsStagingLocation = stagingLocation.firstChild.nodeValue.strip()
            elif str(stagingLocation.attributes['service'].value).lower() == "twms":
                twmsStagingLocation = stagingLocation.firstChild.nodeValue.strip()
        except KeyError:
            raise Exception('service is not defined in <StagingLocation>') 
    
    if twmsStagingLocation != None:
        add_trailing_slash(twmsStagingLocation)
        if not os.path.exists(twmsStagingLocation):
            os.makedirs(twmsStagingLocation)
    if wmtsStagingLocation != None:
        add_trailing_slash(wmtsStagingLocation)
        if not os.path.exists(wmtsStagingLocation):
            os.makedirs(wmtsStagingLocation) 
    try:
        legendLocation = add_trailing_slash(get_dom_tag_value(dom, 'LegendLocation'))
    except IndexError:
        legendLocation = None
    try:
        legendUrl = add_trailing_slash(get_dom_tag_value(dom, 'LegendURL'))
    except IndexError:
        legendUrl = None

    # Modified in 0.9 to allow for multiple versioned colormap locations and URLs
    try:
        colormapLocations = dom.getElementsByTagName('ColorMapLocation')
        for location in colormapLocations:
            if 'version' not in location.attributes.keys():
                if len(colormapLocations) > 1:
                    log_sig_err('Multiple <ColorMapLocation> elements but not all have a "version" attribute', sigevent_url)
                else:
                    location.attributes['version'] = ''
    except KeyError:
        colormapLocations = None

    try:
        colormapUrls = dom.getElementsByTagName('ColorMapURL')
        for url in colormapUrls:
            if 'version' not in url.attributes.keys():
                if len(colormapUrls) > 1:
                    log_sig_err('Multiple <ColorMapURL> elements but not all have a "version" attribute', sigevent_url)
                else:
                    url.attributes['version'] = ''
    except KeyError:
        colormapUrls = None

    # Get mapfile parameters from environment config file (if mapfile generation chosen)
    if create_mapfile is True:
        # Get/create mapfile staging location
        try:
            mapfileStagingLocation = dom.getElementsByTagName('MapfileStagingLocation')[0].firstChild.nodeValue
        except IndexError:
            log_sig_exit('ERROR', 'Mapfile creation chosen but no <MapfileStagingLocation> present in environment config file.', sigevent_url)
        try:
            os.makedirs(mapfileStagingLocation)
        except OSError:
            if not os.path.exists(mapfileStagingLocation):
                log_sig_exit('ERROR', 'Mapfile staging location: ' + mapfileStagingLocation + ' cannot be created.', sigevent_url)
            pass

        # Get output mapfile location
        try:
            mapfileLocationElement = dom.getElementsByTagName('MapfileLocation')[0]
            mapfileLocation = mapfileLocationElement.firstChild.nodeValue
        except IndexError:
            log_sig_exit('ERROR', 'Mapfile creation chosen but no <MapfileLocation> present in environment config file.', sigevent_url)

        try:
            mapfileLocationBasename = mapfileLocationElement.attributes['basename'].value
        except KeyError:
            log_sig_exit('ERROR', 'No "basename" attribute present for <MapfileLocation>.', sigevent_url)
            mapfileLocationBasename = None

        # Get output mapfile config location
        try:
            mapfileConfigLocation = get_dom_tag_value(dom, 'MapfileConfigLocation')
        except IndexError:
            mapfileConfigLocation = '/etc/onearth/config/mapserver/'
            log_sig_err('Mapfile creation chosen but no <MapfileConfigLocation> present in environment config file.', sigevent_url)
        try:
            mapfileConfigBasename = dom.getElementsByTagName('MapfileConfigLocation')[0].attributes['basename'].value
        except KeyError:
            log_sig_exit('ERROR', 'Mapfile creation chosen but no "basename" attribute specified for <MapfileConfigLocation>.', sigevent_url)
    else:
        mapfileStagingLocation = None
        mapfileLocation = None
        mapfileLocationBasename = None
        mapfileConfigLocation = None
        mapfileConfigBasename = None
        
    return Environment(add_trailing_slash(cacheLocation_wmts),
                       add_trailing_slash(cacheLocation_twms),
                       cacheBasename_wmts, cacheBasename_twms,
                       add_trailing_slash(wmts_getCapabilities), 
                       add_trailing_slash(twms_getCapabilities), 
                       add_trailing_slash(getTileService),
                       add_trailing_slash(wmtsServiceUrl), 
                       add_trailing_slash(twmsServiceUrl),
                       wmtsStagingLocation, twmsStagingLocation,
                       legendLocation, legendUrl,
                       colormapLocations, colormapUrls,
                       mapfileStagingLocation, mapfileLocation,
                       mapfileLocationBasename, mapfileConfigLocation, 
                       mapfileConfigBasename)
Gets environment metadata from a environment configuration file.
dataset/ETHPy150Open nasa-gibs/onearth/src/layer_config/bin/oe_configure_layer.py/get_environment