def id11(box): """ Given a 4-tuple of (s, n, w, e), return an 11 character ID. """ lat,lon = eqarea.centre(box) return '%+05.1f%+06.1f' % (lat,lon)
def cells_logged(box, log='log/step5.log'): """ Return the set of cells that are used in a particular box (by examining the step5 log file `log`). """ try: box_i = int(box) except ValueError: pass else: from code import eqarea boxes = list(eqarea.grid()) # Note: use Hansen's 1-based convention. bounds = boxes[box_i-1] centre = eqarea.centre(bounds) box = "LND@%+03.0f%+04.0fT" % tuple(centre) with open(log) as log: for row in log: row = row.split(' ', 2) if row[1] != 'cells': continue if box in row[0]: cells = json.loads(row[2]) return set(cell[0] for cell in cells if cell[1] > 0)
def id11frombox(s, bos): """Convert a record suffix (5 words) as it appears in the BX file into an 11 character V2 Mean style station identifier. The returned strings will be of the form BOX@+NN+EEE where "+NN" and "+EEE" gives the lat/lon of the box centre. """ d = struct.unpack("%s5i" % bos, s) box = d[1:5] centre = eqarea.centre(box) return "BOX@%+03.0f%+04.0f" % centre
def blog20110106(arg): """Generates KML file for blog post. One of the ccc-gistemp boxes, and the extended box used to do station culling. """ from code import earth import math from code import eqarea p = (64.2,-90) d = 1200.0/earth.radius dd = math.degrees(d) circle = surfcircle(p, d) circle.append(circle[0]) allboxes = list(eqarea.gridsub()) # Let *box* be Boundaries of box in order: S, N, W, E. box,cells = allboxes[6] # Let *cells* be all the cells in that box. # Get the Northwest most cell. nwcell = max(cells, key=lambda x:x[1]-x[2]) nwcentre = eqarea.centre(nwcell) s,n,w,e = box # List of coordinates for box 7. box7 = [(n,w), (n,e), (s,e), (s,w)] box7.append(box7[0]) # "Extended" box. span = e-w ebox = [s-dd, n+dd, w-span/2, e+span/2] s,n,w,e = ebox # List of coordinate for extended box 7. ebox7 = [(n,w), (n,e), (s,e), (s,w)] ebox7.append(ebox7[0]) circle2 = surfcircle(nwcentre, d) circle2.append(circle2[0]) print """<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2"> <Document> <name>Station Culling</name> """ + kmlpolystyle(color='aa0000ff', id='s0') + """ """ + kmlpolystyle(color='aaffffff', id='s1') + """ """ + kmlpolystyle(color='5500ff00', id='s2') + """ """ + kmlplace("1200 km circle", circle, style="#s0") + """ """ + kmlplace("circle at %r" % (nwcentre,), circle2, style="#s1") + """ """ + kmlplace("Culling Box", ebox7, style="#s2") + """ """ + kmlplace("Box 7", box7, style="#s1") + """
def convert(inp, out): """Convert a file inp from subbox to V2 mean format.""" # Clear Climate Code from code import eqarea v2 = gio.GHCNV2Writer(file=out, scale=0.01) subbox = iter(gio.SubboxReader(inp)) # First record is metadata, which we ignore. subbox.next() for record in subbox: lat, lon = eqarea.centre(record.box) record.uid = '%+05.1f%+06.1fC' % (lat, lon) v2.write(record) v2.close()
def convert(inp, out): """Convert a file inp from subbox to V2 mean format.""" # Clear Climate Code from code import eqarea v2 = gio.GHCNV2Writer(file=out, scale=0.01) subbox = iter(gio.SubboxReader(inp)) # First record is metadata, which we ignore. subbox.next() for record in subbox: lat,lon = eqarea.centre(record.box) record.uid = '%+05.1f%+06.1fC' % (lat,lon) v2.write(record) v2.close()
def rectangle(out, latbound=(-90.0,+90.0), lonbound=(-180.0,+180.0)): """*latbound* should be a pair of (southernbound, northernbound), *lonbound* should be a pair of (westernbound, easternbound). On *out* will be written a step5mask file where every cell whose centre is within the specified rectangle will be marked as "1.000" and every other cell marked as "0.000". """ from code import eqarea from code import giss_data s,n = latbound w,e = lonbound for cell in eqarea.grid8k(): lat,lon = eqarea.centre(cell) if s <= lat < n and w <= lon < e: m = 1.0 else: m = 0.0 out.write("%sMASK%.3f\n" % (giss_data.boxuid(cell), m))
def rectangle(out, latbound=(-90.0, +90.0), lonbound=(-180.0, +180.0)): """*latbound* should be a pair of (southernbound, northernbound), *lonbound* should be a pair of (westernbound, easternbound). On *out* will be written a step5mask file where every cell whose centre is within the specified rectangle will be marked as "1.000" and every other cell marked as "0.000". """ from code import eqarea from code import giss_data s, n = latbound w, e = lonbound for cell in eqarea.grid8k(): lat, lon = eqarea.centre(cell) if s <= lat < n and w <= lon < e: m = 1.0 else: m = 0.0 out.write("%sMASK%.3f\n" % (giss_data.boxuid(cell), m))