@@ -0,0 +1,2667 @@
+# Python on my mac is not configured to pick up%0A# up packages in site-package in /usr/local (I am using the%0A# Apple-provided python)%0Aimport sys%0Asys.path.append('/usr/local/lib/python2.7/site-packages')%0A%0A# To install 'dwca':%0A# pip install python-dwca-reader%0A# For documentation, see: http://python-dwca-reader.readthedocs.io/en/latest/index.html%0Afrom dwca.read import DwCAReader%0Afrom dwca.darwincore.utils import qualname as qn%0A%0A# '/Users/jar/a/ot/repo/reference-taxonomy/r/gbif-20160729/source/gbif-backbone.zip'%0A%0Aarchive = sys.argv%5B1%5D%0Aprint 'archive is', archive%0A%0Aprint '1'%0A%0Adef project_gbif(inpath, outpath):%0A%0A    with DwCAReader(inpath) as dwca:%0A%0A        print '2'%0A        sys.stdout.flush()%0A%0A        print 'Core file has type', dwca.descriptor.core.type%0A        sys.stdout.flush()%0A%0A        print 'Terms are', map(lambda (x): x.split('/')%5B-1%5D, dwca.descriptor.core.terms)%0A        sys.stdout.flush()%0A%0A        # The columns we want in outfile:%0A        #   taxonID%0A        #   parentNameUsageID%0A        #   acceptedNameUsageID%0A        #   scientificName or canonicalName%0A        #   taxonRank%0A        #   taxonomicStatus%0A        #   datasetID (formerly nameAccordingTo)%0A%0A        columns = %5B'taxonID',%0A                   'parentNameUsageID',%0A                   'acceptedNameUsageID',%0A                   'canonicalName',%0A                   'taxonRank',%0A                   'taxonomicStatus',%0A                   'datasetID'%5D%0A%0A        column_uris = map(qn, columns)%0A        canonical_name_uri = qn('canonicalName')%0A        scientific_name_uri = qn('scientificName')%0A%0A        def get_qn(shortname):%0A            q = qn(shortname)%0A            if q in column_uris:%0A                return q%0A            else:%0A                print 'Did not find term:', shortname%0A                return None%0A%0A        def get_value(row, uri):%0A            if uri in row:%0A                value = row%5Buri%5D%0A            elif uri == scientific_name_uri:%0A                # Assume the library has taken care of utf-8 issues for us%0A                value = canonical_name(row%5Bcanonical_name_uri%5D)%0A            else:%0A                print '** field is required but missing:', uri%0A            return value%0A%0A        with open(outpath, 'w') as outfile:%0A            i = 0%0A            for row in dwca:%0A                outfile.write('%5Ct'.join(map(lambda uri: get_value(row, uri).encode('utf-8'), columns)))%0A            i += 1%0A            if i %25 100000 == 0: print i, scientific.encode('utf-8'), '=%3E', canenc%0A%0A# Needed for 2016 GBIF, but not for later or earlier ones%0A%0Adef canonical_name(scientific_name):%0A    print '** NYI'%0A    return 'uluz'%0A%0Aif __name__ == '__main__':%0A    project_gbif(sys.argv%5B1%5D, sys.argv%5B2%5D)%0A

work in progress on improved import script for GBIF