Skip to content

Commit

Permalink
camatch.py: more robust against underscores in key names.
Browse files Browse the repository at this point in the history
Fixes issue 93.


git-svn-id: https://ccc-gistemp.googlecode.com/svn/trunk@623 a3c903a6-8ee9-11dd-8a4e-8714b3728bc5
  • Loading branch information
drj@ravenbrook.com committed Nov 24, 2010
1 parent 6a38039 commit c7a4a69
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions tool/camatch.py
Expand Up @@ -52,7 +52,7 @@ def match(cameta, cadata, ghcnmeta, ghcndata, table):
overlap,q,id12 = match_quality(cadata[castid][castid+'0'],
ghcndata[ghcnst.uid])
match.q = q
wmo = match.cast['WMO Identifier'] or 'nowmo'
wmo = blah_get(match.cast, 'WMO Identifier') or 'nowmo'
print match.type, wmo, castid, id12, \
"%.2f" % sep, "%4d" % overlap, q
if match.type == 'wmo' or match.q + match.sep < 1:
Expand Down Expand Up @@ -128,7 +128,7 @@ def itermatches(cadict, ghcndict):
for cast in cadict.values():
result = Struct()
result.cast = cast
wmo = cast['WMO Identifier']
wmo = blah_get(cast, 'WMO Identifier')
lat = cast['Latitude']
lon = cast['Longitude']
caloc = iso6709(lat, lon)
Expand Down Expand Up @@ -183,18 +183,32 @@ def cametaasdict(meta):
def itermeta():
for row in meta:
item = json.loads(row)
# Convert some key keys into floating point.
for key in ['Latitude', 'Longitude', 'Elevation']:
item[key] = float(item[key])
yield item['Climate Identifier'], item
id = blah_get(item, 'Climate Identifier')
yield id, item
return dict(itermeta())


def ghcnmetaasdict(ghcnmeta):
"""Take an open file descriptor on the 'v2.inv' file and return a
dictionary."""

stations = gio.station_meta(file=ghcnmeta, format='v2')
stations = gio.station_metadata(file=ghcnmeta, format='v2')
return stations

def blah_get(target, key):
"""Look up *key* in the dictionary *target*, trying variants of the
key with spaces replaced with '_'. Raises exception if a value is
not found at any of the variants.
"""

l = [key, key.replace(' ', '_')]
# Sort any valid keys to the beginning of the list.
l.sort(key=lambda k: k in target, reverse=True)
return target[l[0]]

def iso6709(lat, lon):
"""Return the lat,lon pair as a string in ISO 6709 format."""

Expand Down

0 comments on commit c7a4a69

Please sign in to comment.