Skip to content

Commit

Permalink
Fix BBS50 species table
Browse files Browse the repository at this point in the history
Ports b80f9ca and
80f07a0 and
14475dd

into BBS50.
  • Loading branch information
ethanwhite committed Jan 10, 2014
1 parent 8e3e2c8 commit 3012035
Showing 1 changed file with 22 additions and 52 deletions.
74 changes: 22 additions & 52 deletions scripts/bbs50stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,29 @@ def __init__(self, **kwargs):
def download(self, engine=None, debug=False):
try:
Script.download(self, engine, debug)

engine = self.engine


# Species table
table = Table("species", cleanup=Cleanup(), contains_pk=True,
header_rows=6)

table.columns=[("species_id", ("pk-int",) ),
("AOU", ("int",) ),
("english_common_name", ("char",50) ),
("french_common_name", ("char",50) ),
("spanish_common_name", ("char",50) ),
("sporder", ("char",30) ),
("family", ("char",30) ),
("genus", ("char",30) ),
("species", ("char",50) ),
]
table.fixed_width = [7,6,51,51,51,51,51,51,50]

engine.table = table
engine.create_table()
engine.insert_data_from_url(self.urls["species"])

# Routes table
if not os.path.isfile(engine.format_filename("routes_new.csv")):
engine.download_files_from_archive(self.urls["routes"],
Expand Down Expand Up @@ -83,56 +103,6 @@ def download(self, engine=None, debug=False):
engine.insert_data_from_file(engine.format_filename("weather_new.csv"))


# Species table
table = Table("species", pk=False, delimiter=',')

table.columns=[("species_id" , ("pk-auto",) ),
("AOU" , ("int",) ),
("genus" , ("char",30) ),
("species" , ("char",50) ),
("subspecies" , ("char",30) ),
("id_to_species" , ("bool",) )]

engine.table = table
engine.create_table()

engine.download_file(self.urls["species"], "SpeciesList.txt")
species_list = open(engine.format_filename("SpeciesList.txt"), "rb")
for n in range(8):
species_list.readline()

rows = []
for line in species_list:
if line and len(line) > 273:
latin_name = line[273:].split()
if len(latin_name) < 2:
# If there's no species given, add "None" value
latin_name.append("None")
subspecies = ' '.join(latin_name[2:]) if len(latin_name) > 2 else "None"
id_to_species = "1" if latin_name[1] != "None" else "0"
if latin_name[1] == "sp.":
latin_name[1] = "None"
id_to_species = "0"
if ("x" in latin_name or "/" in latin_name
or "/" in subspecies or "or" in latin_name):
# Hybrid species or only identified to a group of species
latin_name[1] = ' '.join(latin_name[1:])
subspecies = "None"
id_to_species = "0"

rows.append(','.join([
line.split()[1],
latin_name[0],
latin_name[1],
subspecies,
id_to_species
]))

engine.add_to_table(rows)

species_list.close()


# Region_codes table
table = Table("region_codes", pk=False, header_rows=11,
fixed_width=[11, 11, 30])
Expand Down

0 comments on commit 3012035

Please sign in to comment.