Exemplo n.º 1
0
def compare_regions(dirs, labels, o):
    """Output onto *o* an HTML fragment for the comparison of the three
    regions: Global, Northern Hemisphere, Southern Hemisphere.
    """

    anomaly_file = '%s.Ts.ho2.GHCN.CL.PA.txt'
    for region, code in [
        ('global', 'GLB'),
        ('northern hemisphere', 'NH'),
        ('southern hemisphere', 'SH'),
    ]:
        # Annual series
        fs = map(lambda d: open(os.path.join(d, anomaly_file % code), 'r'),
                 dirs)
        anns = map(list, map(vischeck.annual_anomalies, fs))
        url = vischeck.asgooglechartURL(anns)
        print >> o, '<h2>%s annual temperature anomaly</h2>' % region.capitalize(
        )
        print >> o, '<p>%s in red, %s in black.</p>' % tuple(labels)
        print >> o, '<img src="%s">' % escape(url)
        print >> o, ('<h3>%s annual residues (%s - %s)</h3>' %
                     (region.capitalize(), labels[0], labels[1]))
        print >> o, '<img src="%s">' % escape(differences_url(anns))

        diffs = list(difference(anns, 0.01))
        d = map(lambda a: a[1], diffs)
        print >> o, '<h3>%s annual residue distribution</h3>' % region.capitalize(
        )
        print >> o, '<img src="%s">' % escape(distribution_url(d))
        print >> o, '<h3>%s annual residue summary</h3>' % region.capitalize()
        print >> o, '<ul>'
        print >> o, '<li>Min = %g<li>Max = %g' % (min(d), max(d))
        print >> o, '<li>Zeroes: %d/%d<li>Mean = %g<li>Standard deviation = %g' % stats(
            d)
        print >> o, '</ul>'
        top(diffs, 10, o, 'Largest %s annual residues' % region,
            lambda k, v: "%04d: %g" % (k, v))

        # Monthly series
        fs = map(lambda d: open(os.path.join(d, anomaly_file % code), 'r'),
                 dirs)
        mons = map(asmon, fs)
        diffs = list(difference(mons, 0.01))
        d = map(lambda a: a[1], diffs)
        print >> o, '<h3>%s monthly residue distribution</h3>' % region.capitalize(
        )
        print >> o, '<img src="%s">' % escape(distribution_url(d))
        print >> o, '<h3>%s monthly residue summary</h3>' % region.capitalize()
        print >> o, '<ul>'
        print >> o, '<li>Min = %g<li>Max = %g' % (min(d), max(d))
        print >> o, '<li>Zeroes: %d/%d<li>Mean = %g<li>Standard deviation = %g' % stats(
            d)
        print >> o, '</ul>'
        top(diffs, 10, o, 'Largest %s monthly residues' % region,
            lambda k, v: "%04d-%02d: %g" % (k[0], k[1] + 1, v))
def compare_regions(dirs, labels, o):
    """Output onto *o* an HTML fragment for the comparison of the three
    regions: Global, Northern Hemisphere, Southern Hemisphere.
    """

    surface = 'land'
    anomaly_file = '%s%s.Ts.GHCN.CL.PA.txt'
    for region, code in [
        ('global',              'GLB'), 
        ('northern hemisphere', 'NH'),
        ('southern hemisphere', 'SH'),
    ]:
        # Annual series
        fs = map(lambda d:
          open(os.path.join(d, anomaly_file % (surface, code)), 'r'), dirs)
        anns = map(list, map(vischeck.annual_anomalies, fs))
        url = vischeck.asgooglechartURL(anns)
        print >>o, '<h2>%s annual temperature anomaly</h2>' % region.capitalize()
        print >>o, '<p>%s in red, %s in black.</p>' % tuple(labels) 
        print >>o, '<img src="%s">' % escape(url)
        print >>o, ('<h3>%s annual residues (%s - %s)</h3>'
                    % (region.capitalize(), labels[0], labels[1]))
        print >>o, '<img src="%s">' % escape(differences_url(anns))

        diffs = list(difference(anns, 0.01))
        d = map(lambda a: a[1], diffs)
        print >>o, '<h3>%s annual residue distribution</h3>' % region.capitalize()
        print >>o, '<img src="%s">' % escape(distribution_url(d))
        print >>o, '<h3>%s annual residue summary</h3>' % region.capitalize()
        print >>o, '<ul>'
        print >>o, '<li>Min = %g<li>Max = %g' % (min(d), max(d))
        print >>o, '<li>Zeroes: %d/%d<li>Mean = %g<li>Standard deviation = %g' % stats(d)
        print >>o, '</ul>'
        top(diffs, 10, o, 'Largest %s annual residues' % region,
            lambda k, v: "%04d: %g" % (k, v))

        # Monthly series
        fs = map(lambda d:
          open(os.path.join(d, anomaly_file % (surface, code)), 'r'), dirs)
        mons = map(asmon, fs)
        diffs = list(difference(mons, 0.01))
        d = map(lambda a: a[1], diffs)
        print >>o, '<h3>%s monthly residue distribution</h3>' % region.capitalize()
        print >>o, '<img src="%s">' % escape(distribution_url(d))
        print >>o, '<h3>%s monthly residue summary</h3>' % region.capitalize()
        print >>o, '<ul>'
        print >>o, '<li>Min = %g<li>Max = %g' % (min(d), max(d))
        print >>o, '<li>Zeroes: %d/%d<li>Mean = %g<li>Standard deviation = %g' % stats(d)
        print >>o, '</ul>'
        top(diffs, 10, o, 'Largest %s monthly residues' % region,
            lambda k, v: "%04d-%02d: %g" % (k[0], k[1] + 1, v))
Exemplo n.º 3
0
def compare(dirs, labels, o):
    """Compare results from the two directories in the sequence *dirs*,
    which are labelled by the two strings in the sequence *labels*. Write a
    comparison document in HTML to the stream *o*."""
    assert len(dirs) == 2
    assert len(labels) == 2

    # XML-encode meta-characters &,<,>
    escape = xml.sax.saxutils.escape
    labels = map(escape, labels)

    # Version of ``asmon`` that avoids yielding ``None``.
    asmon_values_only = lambda f: asmon(f, values_only=True)

    title = "Comparison of %s and %s" % tuple(dirs)
    print >>o, """<!doctype HTML>
<html>
<head>
<title>%s</title>
</head>
<body>
<h1>%s</h1>
<p>Generated: %s</p>
""" % (title, title, datetime.datetime.now())

    anomaly_file = '%s.Ts.ho2.GHCN.CL.PA.txt'
    box_file = 'BX.Ts.ho2.GHCN.CL.PA.1200'
    for region, code in [
        ('global',              'GLB'), 
        ('northern hemisphere', 'NH'),
        ('southern hemisphere', 'SH'),
    ]:
        # Annual series
        fs = map(lambda d: open(os.path.join(d, anomaly_file % code), 'r'), dirs)
        anns = map(list, map(asann_values_only, fs))
        url = vischeck.asgooglechartURL(anns)
        print >>o, '<h2>%s annual temperature anomaly</h2>' % region.capitalize()
        print >>o, '<p>%s in red, %s in black.</p>' % tuple(labels) 
        print >>o, '<img src="%s">' % escape(url)
        print >>o, ('<h3>%s annual residues (%s - %s)</h3>'
                    % (region.capitalize(), labels[0], labels[1]))
        print >>o, '<img src="%s">' % escape(differences_url(anns))

        diffs = list(difference(anns, 0.01))
        d = map(lambda a: a[1], diffs)
        print >>o, '<h3>%s annual residue distribution</h3>' % region.capitalize()
        print >>o, '<img src="%s">' % escape(distribution_url(d))
        print >>o, '<h3>%s annual residue summary</h3>' % region.capitalize()
        print >>o, '<ul>'
        print >>o, '<li>Min = %g<li>Max = %g' % (min(d), max(d))
        print >>o, '<li>Zeroes: %d/%d<li>Mean = %g<li>Standard deviation = %g' % stats(d)
        print >>o, '</ul>'
        top(diffs, 10, o, 'Largest %s annual residues' % region,
            lambda k, v: "%04d: %g" % (k, v))

        # Monthly series
        fs = map(lambda d: open(os.path.join(d, anomaly_file % code), 'r'), dirs)
        mons = map(asmon_values_only, fs)
        diffs = list(difference(mons, 0.01))
        d = map(lambda a: a[1], diffs)
        print >>o, '<h3>%s monthly residue distribution</h3>' % region.capitalize()
        print >>o, '<img src="%s">' % escape(distribution_url(d))
        print >>o, '<h3>%s monthly residue summary</h3>' % region.capitalize()
        print >>o, '<ul>'
        print >>o, '<li>Min = %g<li>Max = %g' % (min(d), max(d))
        print >>o, '<li>Zeroes: %d/%d<li>Mean = %g<li>Standard deviation = %g' % stats(d)
        print >>o, '</ul>'
        top(diffs, 10, o, 'Largest %s monthly residues' % region,
            lambda k, v: "%04d-%02d: %g" % (k[0], k[1] + 1, v))

    # Box series
    fs = map(lambda d: open(os.path.join(d, box_file), 'r'), dirs)
    boxes = map(list, map(box_series, fs))
    diffs = list(difference(boxes))
    box_table = {}
    for d in diffs:
        box = d[0][0]
        box_table[box] = box_table.get(box,[])
        box_table[box].append(d[1])
    box_std_devs = []
    max_std_dev = 0.0
    for (box, box_diffs) in box_table.items():
        box_table[box] = stats(box_diffs)
        std_dev = box_table[box][3]
        max_std_dev = max(max_std_dev, std_dev)
        box_std_devs.append((box, std_dev))
    
    d = map(lambda a: a[1], diffs)
    print >>o, '<h3>Per-box monthly residue distribution</h3>'
    print >>o, '<img src="%s">' % escape(distribution_url(d))
    print >>o, '<h3>Per-box monthly residue summary</h3>'
    print >>o, '<ul>'
    print >>o, '<li>Min = %g<li>Max = %g' % (min(d), max(d))
    print >>o, '<li>Zeroes: %d/%d<li>Mean = %g<li>Standard deviation = %g' % stats(d)
    print >>o, '</ul>'
    top(diffs, 10, o,'Largest per-box monthly residues',
        lambda k, v: "Box %02d, %04d-%02d: %g" % (k[0], k[1], k[2] + 1, v))

    top(box_std_devs, 10, o,'Largest per-box standard deviations',
        lambda k, v: "Box %02d: %g" % (k, v))

    print >>o, '<h3>Geographic distribution of per-box monthly residue statistics</h3>'
    print >>o, '<table border=1 style="border-collapse:collapse; border-width:1px; border-color:#cccccc; border-style:solid; font-size:small">'
    box = 0
    for band in range(8):
        print >>o, '<tr>'
        boxes_in_band = 18 - 2 * (abs(7 - 2*band)) # silly hack to get 4,8,12,16,16,12,8,4
        box_span = 48 / boxes_in_band
        for i in range(boxes_in_band):
            std_dev = box_table[box][3]
            if std_dev == 0 or max_std_dev == 0:
                gb_level = 0xff
            else:
                gb_level = 0xff - int(0x80 * std_dev / max_std_dev)
            color = "#ff%02x%02x" % (gb_level, gb_level)
            print >>o, '<td align="center" colspan="%d" bgcolor = "%s">' % (box_span, color)
            print >>o, '%d%%<br/>%.2g</td>' % (100 * box_table[box][0]/box_table[box][1], box_table[box][3])
            box += 1
        print >>o, '</tr>'
    print >>o, '</table>'

    print >>o, "</body>"
    print >>o, "</html>"
    o.close()