Created
June 18, 2013 20:09
-
-
Save tucotuco/5808851 to your computer and use it in GitHub Desktop.
DwC rank determiner.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_number(s): | |
try: | |
float(s) | |
return True | |
except ValueError: | |
return False | |
def valid_latlng(lat,lng): | |
# accepts lat and lng as strings. | |
if not is_number(lat): | |
return False | |
if not is_number(lng): | |
return False | |
flat = float(lat) | |
if flat < -90 or flat > 90: | |
return False | |
flng = float(lng) | |
if flng < -180 or flng > 180: | |
return False | |
return True | |
def valid_georef(rec): | |
if rec.has_key('decimallatitude'): | |
if rec.has_key('decimallongitude'): | |
return valid_latlng(rec['decimallatitude'],rec['decimallongitude']) | |
return False | |
def valid_binomial(rec): | |
if rec.has_key('genus'): | |
if rec.has_key('specificepithet'): | |
# Sufficient condition for now to have these DwC terms populated. | |
# Later may want to test them against a name authority to determine validity. | |
return True | |
return False | |
def rank(rec): | |
rank = 0 | |
if valid_georef(rec) is True and valid_binomial(rec) is True: | |
rank = 5 | |
if rec.has_key('year'): | |
rank = 6 | |
if rec.has_key('month'): | |
rank = 7 | |
if rec.has_key('day'): | |
rank = 8 | |
elif valid_binomial(rec) is True: | |
rank = 1 | |
if rec.has_key('year'): | |
rank = 2 | |
if rec.has_key('month'): | |
rank = 3 | |
if rec.has_key('day'): | |
rank = 4 | |
return rank |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment