|
| 1 | +# Convert a internal TIND CaltechDATA record into a DataCite 4 standard schema json record |
| 2 | +import json |
| 3 | +import argparse |
| 4 | + |
| 5 | +def decustomize_schema(json_record): |
| 6 | + |
| 7 | + #Extract subjects to single string |
| 8 | + if "subjects" in json_record: |
| 9 | + if isinstance(json_record['subjects'],str): |
| 10 | + subjects = json_record['subjects'].split(',') |
| 11 | + array = [] |
| 12 | + for s in subjects: |
| 13 | + array.append({'subject':s}) |
| 14 | + json_record['subjects']=array |
| 15 | + else: |
| 16 | + array = [] |
| 17 | + for s in json_record['subjects']: |
| 18 | + array.append({"subject":s}) |
| 19 | + json_record['subjects']=array |
| 20 | + |
| 21 | + #Extract identifier and label as DOI |
| 22 | + if "doi" in json_record: |
| 23 | + json_record['identifier'] = {'identifier':json_record['doi'], |
| 24 | + 'identifierType':"DOI"} |
| 25 | + del json_record['doi'] |
| 26 | + |
| 27 | + #Extract title |
| 28 | + if "title" in json_record: |
| 29 | + json_record['titles'] = [{"title":json_record['title']}] |
| 30 | + del json_record['title'] |
| 31 | + |
| 32 | + #Change related identifier labels |
| 33 | + if "relatedIdentifiers" in json_record: |
| 34 | + for listing in json_record['relatedIdentifiers']: |
| 35 | + listing['relationType'] = listing.pop('relatedIdentifierRelation') |
| 36 | + listing['relatedIdentifierType'] = listing.pop('relatedIdentifierScheme') |
| 37 | + |
| 38 | + #change author formatting |
| 39 | + #Could do better with multiple affiliations |
| 40 | + if "authors" in json_record: |
| 41 | + authors = json_record['authors'] |
| 42 | + newa = [] |
| 43 | + for a in authors: |
| 44 | + new = {} |
| 45 | + if 'authorAffiliation' in a: |
| 46 | + new['affiliations'] = [a['authorAffiliation']] |
| 47 | + new['creatorName'] = a['authorName'] |
| 48 | + newa.append(new) |
| 49 | + json_record['creators']=newa |
| 50 | + del json_record['authors'] |
| 51 | + |
| 52 | + #contributors |
| 53 | + if "contributors" in json_record: |
| 54 | + for c in json_record['contributors']: |
| 55 | + if 'contributorAffiliation' in c: |
| 56 | + c['affiliations'] = [c.pop('contributorAffiliation')] |
| 57 | + if 'contributorIdentifiers' in c: |
| 58 | + c['contributorIdentifiers']['nameIdentifier'] =\ |
| 59 | + c['contributorIdentifiers'].pop('contributorIdentifier') |
| 60 | + c['contributorIdentifiers']['nameIdentifierScheme'] =\ |
| 61 | + c['contributorIdentifiers'].pop('contributorIdentifierScheme') |
| 62 | + c['nameIdentifiers'] = [c.pop('contributorIdentifiers')] |
| 63 | + if 'contributorEmail' in c: |
| 64 | + del c['contributorEmail'] |
| 65 | + #format |
| 66 | + if "format" in json_record: |
| 67 | + if isinstance(json_record['format'],list): |
| 68 | + json_record['formats']=json_record.pop('format') |
| 69 | + else: |
| 70 | + json_record['formats']=[json_record.pop('format')] |
| 71 | + |
| 72 | + #dates |
| 73 | + datetypes = set() |
| 74 | + #Save set of types for handling publicationDate |
| 75 | + if "relevantDates" in json_record: |
| 76 | + dates = json_record['relevantDates'] |
| 77 | + for d in dates: |
| 78 | + d['date']=d.pop('relevantDateValue') |
| 79 | + d['dateType']=d.pop('relevantDateType') |
| 80 | + datetypes.add(d['dateType']) |
| 81 | + json_record['dates']=json_record.pop('relevantDates') |
| 82 | + |
| 83 | + #set publicationYear |
| 84 | + year = json_record['publicationDate'].split('-')[0] |
| 85 | + json_record['publicationYear'] = year |
| 86 | + #If "Issued' date type was not manually set in metadata |
| 87 | + #We want to save the entire publicationDate |
| 88 | + if 'Issued' not in datetypes: |
| 89 | + if 'dates' in json_record: |
| 90 | + json_record['dates'].append({"date":json_record['publicationDate'],\ |
| 91 | + "dateType": "Issued"}) |
| 92 | + else: |
| 93 | + json_record['dates']=[{"date":json_record['publicationDate'],\ |
| 94 | + "dateType": "Issued"}] |
| 95 | + del json_record['publicationDate'] |
| 96 | + |
| 97 | + #license - no url available |
| 98 | + if 'license' in json_record: |
| 99 | + json_record['rightsList']=[{"rights":json_record.pop('license')}] |
| 100 | + |
| 101 | + #Funding |
| 102 | + if 'fundings' in json_record: |
| 103 | + funding = json_record['fundings'] |
| 104 | + newf = [] |
| 105 | + for f in funding: |
| 106 | + frec = {} |
| 107 | + if 'fundingName' in f: |
| 108 | + frec['funderName'] = f['fundingName'] |
| 109 | + #f['fundingName']=f.pop('funderName') |
| 110 | + if 'fundingAwardNumber' in f: |
| 111 | + frec['awardNumber']={'awardNumber':f['fundingAwardNumber']} |
| 112 | + newf.append(frec) |
| 113 | + json_record['fundingReferences']=newf |
| 114 | + del json_record['fundings'] |
| 115 | + |
| 116 | + #Geo |
| 117 | + if 'geographicCoverage' in json_record: |
| 118 | + geo = json_record['geographicCoverage'] |
| 119 | + newgeo = {} |
| 120 | + if 'geoLocationPlace' in geo: |
| 121 | + newgeo['geoLocationPlace'] = geo['geoLocationPlace'] |
| 122 | + if 'geoLocationPoint' in geo: |
| 123 | + pt = geo['geoLocationPoint'][0] |
| 124 | + newpt = {} |
| 125 | + newpt['pointLatitude'] = float(pt['pointLatitude']) |
| 126 | + newpt['pointLongitude'] = float(pt['pointLongitude']) |
| 127 | + newgeo['geoLocationPoint'] = newpt |
| 128 | + json_record['geoLocations'] = [newgeo] |
| 129 | + del json_record['geographicCoverage'] |
| 130 | + |
| 131 | + #Publisher |
| 132 | + if "publishers" in json_record: |
| 133 | + if isinstance(json_record['publisher'],list): |
| 134 | + json_record['publisher'] = json_record['publishers'][0]['publisherName'] |
| 135 | + else: |
| 136 | + json_record['publisher'] = json_record['publishers']['publisherName'] |
| 137 | + del json_record['publishers'] |
| 138 | + |
| 139 | + #description |
| 140 | + if "descriptions" in json_record: |
| 141 | + for d in json_record["descriptions"]: |
| 142 | + if 'descriptionValue' in d: |
| 143 | + d["description"] = d.pop("descriptionValue") |
| 144 | + |
| 145 | + others = ['files', 'id', 'owners', 'pid_value', 'control_number', '_oai', |
| 146 | + '_form_uuid', 'electronic_location_and_access', 'access_right'] |
| 147 | + for v in others: |
| 148 | + if v in json_record: |
| 149 | + del json_record[v] |
| 150 | + |
| 151 | + #print(json.dumps(json_record)) |
| 152 | + return json_record |
| 153 | + |
| 154 | +if __name__ == "__main__": |
| 155 | + #Read in from file for demo purposes |
| 156 | + |
| 157 | + parser = argparse.ArgumentParser(description=\ |
| 158 | + "decustomize_schema converts a internal TIND CaltechDATA record\ |
| 159 | + into a DataCite 4 standard schema json record") |
| 160 | + parser.add_argument('json_files', nargs='+', help='json file name') |
| 161 | + args = parser.parse_args() |
| 162 | + |
| 163 | + for jfile in args.json_files: |
| 164 | + infile = open(jfile,'r') |
| 165 | + data = json.load(infile) |
| 166 | + new = customize_schema(data) |
| 167 | + with open('formatted.json','w') as outfile: |
| 168 | + json.dump(new,outfile) |
| 169 | + #print(json.dumps(new)) |
0 commit comments