def generate_legend(colormap, output, legend_url, orientation):
    """
    Generate an SVG legend graphic from GIBS color map.
    Arguments:
        colormap -- the color map file name
        output -- the output file name
        legend_url -- URL to access legend from GetCapabilities
        orientation -- the orientation of the legend
    """
    
    print "\nLegend location: " + output
    print "Legend URL: " + legend_url
    print "Color Map: " + colormap
    print "Orientation: " + orientation
    pt = 1.25 #pixels in point
    
    if os.path.isfile(output) == False:
        print "Generating new legend"
        cmd = 'oe_generate_legend.py -c '+colormap+' -o ' + output + ' -r ' + orientation
        try:
            run_command(cmd, sigevent_url)
        except Exception, e:
            log_sig_err("Error generating legend: " + str(e), sigevent_url)
    else:
        print "Legend already exists"
        try:
            colormap_file = urllib.urlopen(colormap)
            last_modified = colormap_file.info().getheader("Last-Modified")
            colormap_file.close()
            colormap_time = datetime.strptime(last_modified, "%a, %d %b %Y %H:%M:%S GMT")
            legend_time = datetime.fromtimestamp(os.path.getmtime(output))
            print "Color map last modified on: " + str(colormap_time)
            print "Legend last modified on: " + str(legend_time)
            if colormap_time > legend_time:
                print "Updated color map found"
                print "Generating new legend"
                cmd = 'oe_generate_legend.py -c '+colormap+' -o ' + output + ' -r ' + orientation
                run_command(cmd, sigevent_url)
        except Exception, e:
            log_sig_err("Error generating legend: " + str(e), sigevent_url)
    # check file
    try:
        # Open file.
        svg=open(output, 'r')
    except IOError:
        mssg=str().join(['Cannot read SVG legend file:  ', output])
        log_sig_err(mssg, sigevent_url)
        
    # get width and height
    dom = xml.dom.minidom.parse(svg)
    svgElement = dom.getElementsByTagName('svg')[0]
    height = float(svgElement.attributes['height'].value.replace('pt','')) * pt
    width = float(svgElement.attributes['width'].value.replace('pt','')) * pt
    svg.close()
    
    if orientation == 'horizontal':
        legend_url_template = '<LegendURL format="image/svg+xml" xlink:type="simple" xlink:role="http://earthdata.nasa.gov/gibs/legend-type/horizontal" xlink:href="%s" xlink:title="GIBS Color Map Legend: Horizontal" width="%d" height="%d"/>' % (legend_url, int(width), int(height))
    else:
        legend_url_template = '<LegendURL format="image/svg+xml" xlink:type="simple" xlink:role="http://earthdata.nasa.gov/gibs/legend-type/vertical" xlink:href="%s" xlink:title="GIBS Color Map Legend: Vertical" width="%d" height="%d"/>' % (legend_url, int(width), int(height))
    
    return legend_url_template
dataset/ETHPy150Open nasa-gibs/onearth/src/layer_config/bin/oe_configure_layer.py/generate_legend/original