<filename>tool/grid.py
#!/usr/local/bin/python3.4
#
# <NAME>, Revision 2016-01-06
# grid.py


"""
grid YYYY-MM [v2-file]

Display gridded anomalies as SVG file.
"""

# Regular expression used to match/validate the "when" argument.
RE_WHEN = r'(\d{4})-(\d{2})'


def map(when, inp, out):
    """Take a cccgistemp subbox file in V2 mean format as *inp* and
    produce an SVG file on *out*."""

    import math
    import re

    out.write("""<svg 
      xmlns="http://www.w3.org/2000/svg"
      xmlns:xlink="http://www.w3.org/1999/xlink"
      version="1.1">\n""")

    m = re.match(RE_WHEN, when)
    year, month = m.groups()
    # Convert to 0-based month.
    month = int(month) - 1
    assert 0 <= month < 12

    out.write("""<g transform='translate(180,90)'>\n""")
    for (lat, lon), v in filter_month(inp, year, month):
        x, y = topixel(lat, lon)
        y = -y
        if v > 0:
            fill = 'red'
        else:
            fill = 'blue'
        radius_scale = 0.25
        r = math.sqrt(abs(v)) * radius_scale
        out.write("""<circle cx='%.1f' cy='%.1f' r='%.1f' fill='%s' />\n""" %
                  (x, y, r, fill))
    out.write('</g>\n')
    out.write("""</svg>\n""")


def topixel(lat, lon):
    u"""Return x,y coordinate.  Plate Carr\xe9e projection."""

    return lon, lat


def filter_month(inp, year, month):
    """Yield each value from the v2.mean file for the year and month in
    question.  Invalid values (marked with -9999 in the file) are
    ignored.  Each value is yielded as ((lat,lon),v) where lat,lon are
    the latitude and longitude in degrees (positive is North and East).
    """

    for line in inp:
        if line[12:16] == year:
            v = int(line[16 + 5 * month:21 + 5 * month])
            if v != -9999:
                lat = float(line[:5])
                lon = float(line[5:11])
                yield ((lat, lon), v)


def usage(out):
    out.write("Usage:\n")
    out.write(__doc__)
    return 2


def main(argv=None):
    import re
    import sys
    if argv is None:
        argv = sys.argv

    if len(argv) <= 1:
        return usage(sys.stderr)

    when = argv[1]
    if not re.match(RE_WHEN, when):
        return usage(sys.stderr)
    if len(argv) > 2:
        f = open(argv[2])
    else:
        f = sys.stdin
    map(when, f, sys.stdout)


if __name__ == '__main__':
    main()

<filename>tool/grid.py
#!/usr/local/bin/python3.4
#
# <NAME>, Revision 2016-01-06
# grid.py


"""
grid YYYY-MM [v2-file]

Display gridded anomalies as SVG file.
"""

# Regular expression used to match/validate the "when" argument.
RE_WHEN = r'(\d{4})-(\d{2})'


def map(when, inp, out):
    """Take a cccgistemp subbox file in V2 mean format as *inp* and
    produce an SVG file on *out*."""

    import math
    import re

    out.write("""<svg 
      xmlns="http://www.w3.org/2000/svg"
      xmlns:xlink="http://www.w3.org/1999/xlink"
      version="1.1">\n""")

    m = re.match(RE_WHEN, when)
    year, month = m.groups()
    # Convert to 0-based month.
    month = int(month) - 1
    assert 0 <= month < 12

    out.write("""<g transform='translate(180,90)'>\n""")
    for (lat, lon), v in filter_month(inp, year, month):
        x, y = topixel(lat, lon)
        y = -y
        if v > 0:
            fill = 'red'
        else:
            fill = 'blue'
        radius_scale = 0.25
        r = math.sqrt(abs(v)) * radius_scale
        out.write("""<circle cx='%.1f' cy='%.1f' r='%.1f' fill='%s' />\n""" %
                  (x, y, r, fill))
    out.write('</g>\n')
    out.write("""</svg>\n""")


def topixel(lat, lon):
    u"""Return x,y coordinate.  Plate Carr\xe9e projection."""

    return lon, lat


def filter_month(inp, year, month):
    """Yield each value from the v2.mean file for the year and month in
    question.  Invalid values (marked with -9999 in the file) are
    ignored.  Each value is yielded as ((lat,lon),v) where lat,lon are
    the latitude and longitude in degrees (positive is North and East).
    """

    for line in inp:
        if line[12:16] == year:
            v = int(line[16 + 5 * month:21 + 5 * month])
            if v != -9999:
                lat = float(line[:5])
                lon = float(line[5:11])
                yield ((lat, lon), v)


def usage(out):
    out.write("Usage:\n")
    out.write(__doc__)
    return 2


def main(argv=None):
    import re
    import sys
    if argv is None:
        argv = sys.argv

    if len(argv) <= 1:
        return usage(sys.stderr)

    when = argv[1]
    if not re.match(RE_WHEN, when):
        return usage(sys.stderr)
    if len(argv) > 2:
        f = open(argv[2])
    else:
        f = sys.stdin
    map(when, f, sys.stdout)


if __name__ == '__main__':
    main()

#!/usr/local/bin/python3.4 # # <NAME>, Revision 2016-01-06 # grid.py grid YYYY-MM [v2-file]  Display gridded anomalies as SVG file. # Regular expression used to match/validate the "when" argument. Take a cccgistemp subbox file in V2 mean format as *inp* and     produce an SVG file on *out*. <svg        xmlns="http://www.w3.org/2000/svg"       xmlns:xlink="http://www.w3.org/1999/xlink"       version="1.1">\n # Convert to 0-based month. <g transform='translate(180,90)'>\n <circle cx='%.1f' cy='%.1f' r='%.1f' fill='%s' />\n </svg>\n Return x,y coordinate.  Plate Carr\xe9e projection. Yield each value from the v2.mean file for the year and month in     question.  Invalid values (marked with -9999 in the file) are     ignored.  Each value is yielded as ((lat,lon),v) where lat,lon are     the latitude and longitude in degrees (positive is North and East).