Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AndrewVSutherland
GitHub Repository: AndrewVSutherland/lmfdb
Path: blob/main/scripts/local_fields/import_fields.py
1127 views
1
# -*- coding: utf-8 -*-
2
r""" Import local field data.
3
4
Imports from a json file directly to the database.
5
6
Data is imported directly to the table lf_fields
7
8
"""
9
10
import sys
11
from sage.all import QQ
12
import re
13
import json
14
15
mypath = '../..'
16
sys.path.append(mypath)
17
18
from lmfdb import db
19
20
lf = db.lf_fields
21
22
def list2string(li):
23
li2 = [str(x) for x in li]
24
return ','.join(li2)
25
26
def string2list(s):
27
s = str(s)
28
if s == '':
29
return []
30
return [int(a) for a in s.split(',')]
31
32
def string2slist(sl):
33
sl = str(sl)
34
sl = sl[1:-1]
35
if sl == '':
36
return []
37
return [str(a) for a in sl.split(',')]
38
39
def make_slope_key(s):
40
qs = QQ(s)
41
sstring = str(qs*1.)
42
sstring += '0'*14
43
if qs < 10:
44
sstring = '0'+sstring
45
sstring = sstring[0:12]
46
sstring += str(qs)
47
return sstring
48
49
def top_slope(ent):
50
sl = string2slist(ent['slopes'])
51
if len(sl)>0:
52
ent['top_slope'] = make_slope_key(sl[-1])
53
elif int(ent['t'])>1:
54
ent['top_slope'] = make_slope_key(1)
55
else:
56
ent['top_slope'] = make_slope_key(0)
57
return ent
58
59
# Let us loop over input to load into a dictionary
60
fnames = ['aut','c','coeffs','e','eisen', 'f', 'gal', 'galT', 'gms', 'hw', 'inertia', 'label', 'n', 'p', 'rf', 'slopes', 't', 'u', 'unram', 'subfields', 'gsm']
61
62
def prep_ent(l):
63
l[6]=l[7]
64
l[8]= str(l[8])
65
l[19] = [[list2string(u[0]),u[1]] for u in l[19]]
66
ent = dict(zip(fnames,l))
67
ent = top_slope(ent)
68
ent['rf'] = ent['rf']
69
ent['coeffs'] = ent['coeffs']
70
return ent
71
72
count=0
73
74
# loop over files, and in each, loop over lines
75
for path in sys.argv[1:]:
76
print(path)
77
fn = open(path)
78
tot = 0
79
outrecs = []
80
for line in fn.readlines():
81
line.strip()
82
count += 1
83
if re.match(r'\S',line):
84
#print line
85
l = json.loads(line)
86
check = lf.lookup(str(l[11])) # by label
87
if check is None: # we don't have it yet
88
ent = prep_ent(l)
89
outrecs.append(ent)
90
#print str(ent['label'])
91
tot += 1
92
if len(outrecs)>0:
93
lf.insert_many(outrecs)
94
95
#outrecs=outrecs[0:125]
96
#from pprint import pprint as pp
97
#pp(outrecs[0])
98
#lf.insert_many(outrecs)
99
100
print("Added %d records" % len(outrecs))
101
102
103
104