def nature201102(arg): """Generate SVG for image as per Nature's request. (expects to find a completed result directory). """ # Download GISTEMP global result and copy together with the global # result on local disk to a GHCN v2 format file, 'nature.v2' out = open('nature.v2', 'w') labels=['_ccc-gistemp', 'NASA_GISTEMP', 'cccNASA_diff'] print "fetching data..." dotabletov2(['result/mixedGLB.Ts.ho2.GHCN.CL.PA.txt', 'http://data.giss.nasa.gov/gistemp/tabledata/GLB.Ts+dSST.txt'], out, labels=labels[:2]) out.close() # Compute difference series: ccc-gistemp - GISTEMP: import gio from code import giss_data reader = gio.GHCNV2Reader(path=out.name) stations = list(reader) assert 2 == len(stations) assert stations[0].first_year == stations[1].first_year difference = giss_data.Series() difference.set_series(stations[0].first_month, [p-q for p,q in zip(stations[0].series, stations[1].series) if p != MISSING and q != MISSING]) difference.uid = labels[2] w = gio.GHCNV2Writer(file=open(out.name, 'a')) w.write(difference) w.close() print "generating SVG..." from tool import stationplot stationplot.main( ("stationplot -o nature.svg --axes=yyr --offset 0,-0.2,0 -c rscale=6000;yscale=300;ytick=0.2;legend=none;buginkscapepdf=1 -s 0.01 -y -d nature.v2 %s %s %s" % tuple(labels)).split()) print "generating PDF..." # Expects to find "inkscape" on PATH. # On drj's Mac, add # "/Applications/Inkscape.app/Contents/Resources/bin" to the PATH # first. # See http://inkscape.modevia.com/inkscape-man.html for inkscape # command line use. os.system('inkscape --export-pdf=nature.pdf nature.svg')
def compare(path1, path2, out): # *reader*, *station* and so are each pairs (one element for each # input file). reader = [gio.GHCNV2Reader(path=p) for p in [path1, path2]] station = map(list, reader) print >> out, "Number of stations: %d :: %d" % tuple( len(l) for l in station) uid = [set(s.uid for s in l) for l in station] d = uid[0] - uid[1] print >> out, "Stations only in %s (%d)" % (path1, len(d)) note10(out, d) del d e = uid[1] - uid[0] print >> out, "Stations only in %s (%d)" % (path2, len(e)) note10(out, e) del e common = uid[0] & uid[1] stationdict = [dict((st.uid,st) for st in l) for l in station] note_diff_years(out, common, stationdict, [path1, path2]) note_diff_months(out, common, stationdict, [path1, path2])
def counts(out, dir='.', mask=None): """Print to *out* a count for each month of the number of stations used. *dir* specifies the name of a directory for the input files (that are intermediate products of ccc-gistemp); from this directory the files used are: 'log/step3.log' (to determine what stations and months are used), and 'work/step2.v2' (to determine which specific months of those stations have data present). *mask* specifies the name of a mask file to be used; only the cells present in the mask file will be examined for station usage. """ # Dictionary that maps from station (12 char identifier) to # monthset, where *monthset* is a set of the numbers from 0 # (January) to 11 (December). stationmonths = dict() log = os.path.join(dir, 'log', 'step3.log') step2 = os.path.join(dir, 'work', 'step2.v2') for row in stations_logged(cells=cells_masked(mask), log=log): stations = json.loads(row[2]) for station,weight,months in stations: stationmonths[station] = monthset(months) | stationmonths.get( station, set()) monthcount = {} for station in gio.GHCNV2Reader(path=step2): if station.uid not in stationmonths: continue for m in stationvalidmonths(station): monthcount[m] = monthcount.get(m, 0) + 1 v2monthcount(out, monthcount)