forked from bmy21/pacs-model
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulate_sql.py
More file actions
156 lines (138 loc) · 5.23 KB
/
populate_sql.py
File metadata and controls
156 lines (138 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import numpy as np
import mysql.connector
import pickle
import glob
import sys
# create table with
'''
CREATE TABLE `resolved_fitting` (
`ObsId` int NOT NULL,
`name` varchar(50) NOT NULL DEFAULT '',
`wavelength` float NOT NULL,
`include_unres` tinyint(1) NOT NULL DEFAULT '-1',
`include_alpha` tinyint(1) NOT NULL DEFAULT '-1',
`psf_obsid` int NOT NULL,
`psffit_flux` float NOT NULL,
`psffit_rms` float NOT NULL,
`pixel_rms` float NOT NULL,
`resolved` tinyint(1) NOT NULL DEFAULT '0',
`in_au` tinyint(1) DEFAULT NULL,
`distance` float DEFAULT NULL,
`fit_ok` tinyint(1) DEFAULT NULL,
`fstar_mjy` float DEFAULT NULL,
`funres` float DEFAULT NULL,
`fres` float DEFAULT NULL,
`alpha` float DEFAULT NULL,
`x0` float DEFAULT NULL,
`y0` float DEFAULT NULL,
`r1` float DEFAULT NULL,
`r2` float DEFAULT NULL,
`ravg` float DEFAULT NULL,
`deltar` float DEFAULT NULL,
`cosinc` float DEFAULT NULL,
`theta` float DEFAULT NULL,
`e_funres` float DEFAULT NULL,
`e_fres` float DEFAULT NULL,
`e_x0` float DEFAULT NULL,
`e_y0` float DEFAULT NULL,
`e_r1` float DEFAULT NULL,
`e_r2` float DEFAULT NULL,
`e_ravg` float DEFAULT NULL,
`e_deltar` float DEFAULT NULL,
`e_cosinc` float DEFAULT NULL,
`e_theta` float DEFAULT NULL,
`e_alpha` float DEFAULT NULL,
PRIMARY KEY (`ObsId`,`name`,`wavelength`,`include_unres`,`include_alpha`,`psf_obsid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
'''
# table to write to
table = 'resolved_fitting_all'
# get location of batch output
batch_path = sys.argv[1]
# set up connection
try:
cnx = mysql.connector.connect(user='grant',
password='grant',
host='localhost',
database='herscheldb',
auth_plugin='mysql_native_password')
cursor = cnx.cursor(buffered=True)
except mysql.connector.InterfaceError:
print("Can't connect")
fs = glob.glob('{}/*/*/params.pickle'.format(batch_path))
for f in fs:
with open(f,'rb') as file:
r = pickle.load(file)
obsid = f.split('/')[-3]
name = f.split('/')[-2]
if not np.isfinite(r['distance']):
r['distance'] = 'NULL'
print(r)
if 'param_names' not in r.keys():
sql = ("INSERT INTO {} "
"(obsid, name, wavelength,distance, "
"resolved, psf_obsid, psffit_flux, "
"psffit_rms, pixel_rms)"
"VALUES ({},'{}',{},{},{},{},{},{},{})"
";".format(table, obsid, name, r['wavelength'],r['distance'],
r['resolved'].real,
r['psf_obsid'], r['psffit_flux'],
r['psffit_rms'], r['pixel_rms'])
)
else:
p = r['median']
# look in samples pickle for other derived params
fs = f.replace('params','samples')
with open(fs, 'rb') as fh:
samp = pickle.load(fh)
e_p = (r['upper_uncertainty'] + r['lower_uncertainty']) / 2
if not r['include_unres']:
p = np.insert(p, 0, 0)
e_p = np.insert(e_p, 0, 0)
samp = np.insert(samp, 0, 0, axis=1)
# derived params
e_ravg_lo, ravg, e_ravg_hi = np.percentile( (samp[:,5] + samp[:,4])/2,
[16, 50, 84] )
e_dr_lo, deltar, e_dr_hi = np.percentile( samp[:,5] - samp[:,4],
[16, 50, 84] )
e_ravg = (ravg-e_ravg_lo + e_ravg_hi-ravg) / 2
e_deltar = (deltar-e_dr_lo + e_dr_hi-deltar) / 2
if r['include_alpha']:
alpha = p[-1]
e_alpha = e_p[-1]
p = np.delete(p, -1)
e_p = np.delete(e_p, -1)
else:
alpha = r['alpha']
e_alpha = 0
sql = ("INSERT INTO {} "
"(obsid, name, wavelength, distance,"
"resolved, include_unres, include_alpha,"
"in_au, fit_ok, psf_obsid,"
"psffit_flux, psffit_rms, pixel_rms,"
"fstar_mjy, alpha, e_alpha,"
"funres, fres, x0, y0, r1, r2, cosinc, theta,"
"e_funres, e_fres, e_x0, e_y0, e_r1, e_r2, e_cosinc, e_theta,"
"ravg, e_ravg, deltar, e_deltar) "
"VALUES ({},'{}',{},{},"
"{},{},{},"
"{},{},{},"
"{},{},{},"
"{},{},{},"
"{},{},{},{},{},{},{},{},"
"{},{},{},{},{},{},{},{},"
"{},{},{},{})"
";".format(table, obsid, name, r['wavelength'], r['distance'],
r['resolved'].real,r['include_unres'].real,
r['include_alpha'].real,
r['in_au'].real, r['fit_ok'].real, r['psf_obsid'],
r['psffit_flux'], r['psffit_rms'], r['pixel_rms'],
r['stellarflux'], alpha, e_alpha,
*p, *e_p,
ravg, e_ravg, deltar, e_deltar)
)
print(sql)
cursor.execute(sql)
cnx.commit()
cursor.close()
cnx.close()