forked from SIMPLE-AstroDB/SIMPLE-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.py
518 lines (455 loc) · 16 KB
/
schema.py
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
"""
Schema for the SIMPLE database
"""
import enum
from datetime import datetime
import sqlalchemy as sa
from astrodbkit2.astrodb import Base
from astrodbkit2.views import view
from astropy.io.votable.ucd import check_ucd
from sqlalchemy import (
Boolean,
Column,
DateTime,
Enum,
Float,
ForeignKey,
ForeignKeyConstraint,
String,
)
from sqlalchemy.orm import validates
# -------------------------------------------------------------------------------------------------------------------
# Reference tables
REFERENCE_TABLES = [
"Publications",
"Telescopes",
"Instruments",
"Modes",
"PhotometryFilters",
"Versions",
"Parameters",
"Regimes",
]
class Publications(Base):
"""ORM for publications table.
This stores reference information (DOI, bibcodes, etc)
and has shortname as the primary key
"""
__tablename__ = 'Publications'
reference = Column(String(30), primary_key=True, nullable=False)
bibcode = Column(String(100))
doi = Column(String(100))
description = Column(String(1000))
@validates("reference")
def validate_reference(self, key, value):
if value is None or len(value) > 30:
raise ValueError(f"Provided reference is invalid; too long or None: {value}")
return value
class Telescopes(Base):
__tablename__ = 'Telescopes'
telescope = Column(String(30), primary_key=True, nullable=False)
description = Column(String(1000))
reference = Column(
String(30), ForeignKey("Publications.reference", onupdate="cascade")
)
class Instruments(Base):
__tablename__ = 'Instruments'
instrument = Column(String(30), primary_key=True, nullable=False)
mode = Column(String(30), primary_key=True)
telescope = Column(
String(30),
ForeignKey("Telescopes.telescope", onupdate="cascade"),
primary_key=True,
)
description = Column(String(1000))
reference = Column(
String(30), ForeignKey("Publications.reference", onupdate="cascade")
)
class Parameters(Base):
"""Table for storing possible parameters for the ModeledParameters table"""
__tablename__ = 'Parameters'
parameter = Column(String(30), primary_key=True, nullable=False)
description = Column(String(1000))
class PhotometryFilters(Base):
"""
ORM for filter table.
This stores relationships between filters and instruments, telescopes,
as well as wavelength and width
"""
__tablename__ = 'PhotometryFilters'
band = Column(String(30), primary_key=True, nullable=False)
# of the form instrument.filter (see SVO)
ucd = Column(String(100))
effective_wavelength = Column(Float, nullable=False)
width = Column(Float)
@validates("band")
def validate_band(self, key, value):
if "." not in value:
raise ValueError("Band name must be of the form instrument.filter")
return value
@validates("ucd")
def validate_ucd(self, key, value):
ucd_string = "phot;" + value
if check_ucd(ucd_string, check_controlled_vocabulary=True) is False:
raise ValueError(f"UCD {value} not in controlled vocabulary")
return value
@validates("effective_wavelength")
def validate_wavelength(self, key, value):
if value is None or value < 0:
raise ValueError(f"Invalid effective wavelength received: {value}")
return value
class Versions(Base):
"""
ORM for Versions table
This stores the version numbers for the database
"""
__tablename__ = 'Versions'
version = Column(String(30), primary_key=True, nullable=False)
start_date = Column(String(30))
end_date = Column(String(30))
description = Column(String(1000))
class Regimes(Base):
"""
ORM for Regimes table
Values used by Spectra and SpectralTypes tables
"""
__tablename__ = "Regimes"
regime = Column(String(30), primary_key=True, nullable=False)
description = Column(String(1000))
# -------------------------------------------------------------------------------------------------------------------
# Hard-coded enumerations
class Gravity(enum.Enum):
"""Enumeration for gravity"""
# TODO: Fix enumerations; the variable name is what's used throughout the database
a = 'alpha'
b = 'beta'
g = 'gamma'
d = 'delta'
bg = 'beta/gamma'
unknown = 'unknown'
vlg = 'vl-g'
intg = 'int-g'
fldg = 'fld-g'
# -------------------------------------------------------------------------------------------------------------------
# Main tables
class Sources(Base):
"""ORM for the sources table. This stores the main identifiers
for our objects along with ra and dec"""
__tablename__ = 'Sources'
source = Column(String(100), primary_key=True, nullable=False)
ra = Column(Float)
dec = Column(Float)
epoch = Column(Float) # decimal year
equinox = Column(String(10)) # eg, J2000
shortname = Column(String(30)) # not needed?
reference = Column(
String(30),
ForeignKey("Publications.reference", onupdate="cascade"),
nullable=False,
)
other_references = Column(String(100))
comments = Column(String(1000))
@validates("ra")
def validate_ra(self, key, value):
if value > 360 or value < 0:
raise ValueError("RA not in allowed range (0..360)")
return value
@validates("dec")
def validate_dec(self, key, value):
if value > 90 or value < -90:
raise ValueError("Dec not in allowed range (-90..90)")
return value
class Names(Base):
__tablename__ = 'Names'
source = Column(
String(100),
ForeignKey("Sources.source", ondelete="cascade", onupdate="cascade"),
nullable=False,
primary_key=True,
)
other_name = Column(String(100), primary_key=True, nullable=False)
class Photometry(Base):
# Table to store photometry information
__tablename__ = 'Photometry'
source = Column(
String(100),
ForeignKey("Sources.source", ondelete="cascade", onupdate="cascade"),
nullable=False,
primary_key=True,
)
band = Column(String(30), ForeignKey("PhotometryFilters.band"), primary_key=True)
magnitude = Column(Float, nullable=False)
magnitude_error = Column(Float)
telescope = Column(String(30), ForeignKey('Telescopes.telescope'))
epoch = Column(Float) # decimal year
comments = Column(String(1000))
reference = Column(
String(30),
ForeignKey("Publications.reference", onupdate="cascade"),
primary_key=True,
)
class Parallaxes(Base):
# Table to store parallax values in milliarcseconds
__tablename__ = 'Parallaxes'
source = Column(
String(100),
ForeignKey("Sources.source", ondelete="cascade", onupdate="cascade"),
nullable=False,
primary_key=True,
)
parallax = Column(Float, nullable=False)
parallax_error = Column(Float)
adopted = Column(Boolean) # flag for indicating if this is the adopted
comments = Column(String(1000))
reference = Column(
String(30),
ForeignKey("Publications.reference", onupdate="cascade"),
primary_key=True,
)
@validates("parallax")
def validate_value(self, key, value):
if value is None or value < 0:
raise ValueError(f"{key} not allowed to be 0 or lower")
return value
class ProperMotions(Base):
# Table to store proper motions, in milliarcseconds per year
__tablename__ = 'ProperMotions'
source = Column(
String(100),
ForeignKey("Sources.source", ondelete="cascade", onupdate="cascade"),
nullable=False,
primary_key=True,
)
mu_ra = Column(Float, nullable=False)
mu_ra_error = Column(Float)
mu_dec = Column(Float, nullable=False)
mu_dec_error = Column(Float)
adopted = Column(Boolean)
comments = Column(String(1000))
reference = Column(
String(30),
ForeignKey("Publications.reference", onupdate="cascade"),
primary_key=True,
)
class RadialVelocities(Base):
# Table to store radial velocities, in km/sec
__tablename__ = 'RadialVelocities'
source = Column(
String(100),
ForeignKey("Sources.source", ondelete="cascade", onupdate="cascade"),
nullable=False,
primary_key=True,
)
radial_velocity_km_s = Column(Float, nullable=False)
radial_velocity_error_km_s = Column(Float)
adopted = Column(Boolean)
comments = Column(String(1000))
reference = Column(
String(30),
ForeignKey("Publications.reference", onupdate="cascade"),
primary_key=True,
)
class SpectralTypes(Base):
# Table to store spectral types, as strings
__tablename__ = 'SpectralTypes'
source = Column(
String(100),
ForeignKey("Sources.source", ondelete="cascade", onupdate="cascade"),
nullable=False,
primary_key=True,
)
spectral_type_string = Column(String(10), nullable=False, primary_key=True)
spectral_type_code = Column(Float, nullable=False, primary_key=True)
spectral_type_error = Column(Float)
regime = Column(
String(30),
ForeignKey("Regimes.regime", ondelete="cascade", onupdate="cascade"),
nullable=True,
primary_key=True,
)
adopted = Column(Boolean)
comments = Column(String(1000))
reference = Column(
String(30),
ForeignKey("Publications.reference", onupdate="cascade"),
primary_key=True,
)
class Gravities(Base):
# Table to store gravity measurements
__tablename__ = 'Gravities'
source = Column(
String(100),
ForeignKey("Sources.source", ondelete="cascade", onupdate="cascade"),
nullable=False,
primary_key=True,
)
gravity = Column(Enum(Gravity, create_constraint=True, native_enum=False),
nullable=False) # restricts to enumerated values
regime = Column(
String(30),
ForeignKey("Sources.source", ondelete="cascade", onupdate="cascade"),
primary_key=True,
)
comments = Column(String(1000))
reference = Column(
String(30),
ForeignKey("Publications.reference", onupdate="cascade"),
primary_key=True,
)
class Spectra(Base):
# Table to store references to spectra
__tablename__ = 'Spectra'
source = Column(
String(100),
ForeignKey("Sources.source", ondelete="cascade", onupdate="cascade"),
nullable=False,
primary_key=True,
)
# Data
access_url = Column(String(1000), nullable=False) # URL of spectrum location
# URL of original spectrum location, if applicable
original_spectrum = Column(String(1000))
# local directory (via environment variable) of spectrum location
local_spectrum = Column(String(1000))
regime = Column(
String(30),
ForeignKey("Regimes.regime", ondelete="cascade", onupdate="cascade"),
primary_key=True,
)
telescope = Column(String(30), nullable=False)
instrument = Column(String(30), nullable=False)
mode = Column(String(30), nullable=False) # eg, Prism, Echelle, etc
observation_date = Column(DateTime, primary_key=True)
# Common metadata
comments = Column(String(1000))
reference = Column(
String(30),
ForeignKey("Publications.reference", onupdate="cascade"),
primary_key=True,
)
other_references = Column(String(100))
# Composite Foreign key constraints for instrument and mode
__table_args__ = (
ForeignKeyConstraint(
[telescope, instrument, mode],
[Instruments.telescope, Instruments.instrument, Instruments.mode],
onupdate="cascade",
),
{},
)
@validates("access_url", "regime", "source", "telescope", "instrument", "mode")
def validate_required(self, key, value):
if value is None:
raise ValueError(f"Value required for {key}")
return value
@validates("observation_date")
def validate_date(self, key, value):
if value is None:
raise ValueError(f"Invalid date received: {value}")
elif not isinstance(value, datetime):
# Convert to datetime for storing in the database
# Will throw error if unable to convert
print("WARNING: Value will be converted to ISO format.")
value = datetime.fromisoformat(value)
return value
class ModeledParameters(Base):
# Table to store derived/inferred paramaters from models
__tablename__ = 'ModeledParameters'
source = Column(
String(100),
ForeignKey("Sources.source", ondelete="cascade", onupdate="cascade"),
nullable=False,
primary_key=True,
)
parameter = Column(
String(30),
ForeignKey("Parameters.parameter", onupdate="cascade"),
primary_key=True,
)
value = Column(Float, nullable=False)
value_error = Column(Float)
unit = Column(String(20))
comments = Column(String(1000))
reference = Column(
String(30),
ForeignKey("Publications.reference", onupdate="cascade"),
primary_key=True,
)
class CompanionRelationships(Base):
# Table to store information about companions
__tablename__ = 'CompanionRelationships'
source = Column(
String(100),
ForeignKey("Sources.source", ondelete="cascade", onupdate="cascade"),
nullable=False,
primary_key=True,
)
companion_name = Column(String(100), nullable=False, primary_key=True)
projected_separation_arcsec = Column(Float)
projected_separation_error = Column(Float)
relationship = Column(String(100), nullable=False)
# Relationship of source to companion.
# Options: Child, Sibling, Parent, Unresolved Parent
comments = Column(String(1000))
reference = Column(
String(30), ForeignKey("Publications.reference", onupdate="cascade")
)
other_companion_names = Column(String(10000)) # other names of the companions
# -------------------------------------------------------------------------------------------------------------------
# Views
ParallaxView = view(
"ParallaxView",
Base.metadata,
sa.select(
Parallaxes.source.label("source"),
Parallaxes.parallax.label("parallax"),
Parallaxes.parallax_error.label("parallax_error"),
(1000.0 / Parallaxes.parallax).label("distance"), # distance in parsecs
Parallaxes.comments.label("comments"),
Parallaxes.reference.label("reference"),
)
.select_from(Parallaxes)
.where(sa.and_(Parallaxes.adopted == 1, Parallaxes.parallax > 0)),
)
PhotometryView = view(
"PhotometryView",
Base.metadata,
sa.select(
Photometry.source.label("source"),
sa.func.avg(
sa.case((Photometry.band == "2MASS.J", Photometry.magnitude))
).label("2MASS.J"),
sa.func.avg(
sa.case((Photometry.band == "2MASS.H", Photometry.magnitude))
).label("2MASS.H"),
sa.func.avg(
sa.case((Photometry.band == "2MASS.Ks", Photometry.magnitude))
).label("2MASS.Ks"),
sa.func.avg(
sa.case((Photometry.band == "WISE.W1", Photometry.magnitude))
).label("WISE.W1"),
sa.func.avg(
sa.case((Photometry.band == "WISE.W2", Photometry.magnitude))
).label("WISE.W2"),
sa.func.avg(
sa.case((Photometry.band == "WISE.W3", Photometry.magnitude))
).label("WISE.W3"),
sa.func.avg(
sa.case((Photometry.band == "WISE.W4", Photometry.magnitude))
).label("WISE.W4"),
sa.func.avg(
sa.case((Photometry.band == "IRAC.I1", Photometry.magnitude))
).label("IRAC.I1"),
sa.func.avg(
sa.case((Photometry.band == "IRAC.I2", Photometry.magnitude))
).label("IRAC.I2"),
sa.func.avg(
sa.case((Photometry.band == "IRAC.I3", Photometry.magnitude))
).label("IRAC.I3"),
sa.func.avg(
sa.case((Photometry.band == "IRAC.I4", Photometry.magnitude))
).label("IRAC.I4"),
)
.select_from(Photometry)
.group_by(Photometry.source),
)