def extract_trend(inp, cells): import trend records = iter(gio.SubboxReader(inp)) meta = records.next() base_year = meta.yrbeg name = inp.name csv_name = name + '.csv' with open(csv_name, 'w') as csv_out: csv_file = csv.writer(csv_out) for record,box in itertools.izip(records, cells): assert record.first_year == base_year series = record.series[-360:] data = [(i//12, v) for i, v in enumerate(series) if v != MISSING] (a, b, r2) = trend.lm1(data) csv_file.writerow([id11(box), b]) if b is None: b = MISSING else: b *= 100 yield b, box
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 to_ghcnm(inp): """ Convert a file inp from subbox to GHCN-M (v3) format. """ import sys out = sys.stdout w = gio.GHCNV3Writer(file=out) subbox = iter(gio.SubboxReader(inp)) # First record is metadata, which we ignore. subbox.next() for record in subbox: record.uid = id11(record.box) w.write(record) w.close()
def extractdate(inp, cells, date): """*date* should be a string in ISO 8601 format: 'YYYY-MM'. From the binary subbox file *inp* extract the values corresponding to the date box by box. """ year, month = map(int, date.split('-')) records = iter(gio.SubboxReader(inp)) meta = records.next() base_year = meta.yrbeg # Index of required month in the record series. i = (year - base_year) * 12 + month - 1 for record, box in itertools.izip(records, cells): assert record.first_year == base_year if i >= len(record.series): yield MISSING, box else: yield record.series[i], box