forked from SIMPLE-AstroDB/SIMPLE-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompanions.py
158 lines (143 loc) · 5.33 KB
/
companions.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
import logging
import sqlalchemy.exc
from astrodb_utils import (
AstroDBError,
)
__all__ = [
"ingest_companion_relationships",
]
logger = logging.getLogger("SIMPLE")
def ingest_companion_relationships(
db,
source,
companion_name,
relationship,
projected_separation_arcsec=None,
projected_separation_error=None,
comment=None,
ref=None,
other_companion_names=None,
):
"""
This function ingests a single row in to the CompanionRelationship table
Parameters
----------
db: astrodbkit2.astrodb.Database
Database object created by astrodbkit2
source: str
Name of source as it appears in sources table
relationship: str
relationship is of the souce to its companion
should be one of the following: Child, Sibling, Parent, or Unresolved Parent
see note
companion_name: str
SIMBAD resovable name of companion object
projected_separation_arcsec: float (optional)
Projected separtaion should be recorded in arc sec
projected_separation_error: float (optional)
Projected separtaion should be recorded in arc sec
references: str (optional)
Discovery references of sources
comments: str (optional)
Comments
other_companion_names: comma separated names (optional)
other names used to identify the companion
ex: 'HD 89744, NLTT 24128, GJ 9326'
Returns
-------
None
Note: Relationships are constrained to one of the following:
- *Child*: The source is lower mass/fainter than the companion
- *Sibling*: The source is similar to the companion
- *Parent*: The source is higher mass/brighter than the companion
- *Unresolved Parent*: The source is the unresolved,
combined light source of an unresolved
multiple system which includes the companion
"""
# checking relationship entered
possible_relationships = ["Child", "Sibling", "Parent", "Unresolved Parent", None]
# check captialization
if relationship.title() != relationship:
logger.info(
f"Relationship captilization changed from "
f"{relationship} to {relationship.title()} "
)
relationship = relationship.title()
if relationship not in possible_relationships:
msg = (
f"Relationship given for {source}, {companion_name}: {relationship} "
"NOT one of the constrained relationships \n {possible_relationships}"
)
logger.error(msg)
raise AstroDBError(msg)
# source canot be same as companion
if source == companion_name:
msg = f"{source}: Source cannot be the same as companion name"
logger.error(msg)
raise AstroDBError(msg)
if source == companion_name:
msg = f"{source}: Source cannot be the same as companion name"
logger.error(msg)
raise AstroDBError(msg)
if projected_separation_arcsec is not None and projected_separation_arcsec < 0:
msg = f"Projected separation: {projected_separation_arcsec}, cannot be negative"
logger.error(msg)
raise AstroDBError(msg)
if projected_separation_error is not None and projected_separation_error < 0:
msg = (
f"Projected separation error: {projected_separation_error},"
" cannot be negative"
)
logger.error(msg)
raise AstroDBError(msg)
# check other names
# make sure companion name is included in the list
if other_companion_names is None:
other_companion_names = companion_name
else:
companion_name_list = other_companion_names.split(", ")
if companion_name not in companion_name_list:
companion_name_list.append(companion_name)
other_companion_names = (", ").join(companion_name_list)
try:
with db.engine.connect() as conn:
conn.execute(
db.CompanionRelationships.insert().values(
{
"source": source,
"companion_name": companion_name,
"projected_separation_arcsec": projected_separation_arcsec,
"projected_separation_error": projected_separation_error,
"relationship": relationship,
"reference": ref,
"comments": comment,
"other_companion_names": other_companion_names,
}
)
)
conn.commit()
logger.info(
"ComapnionRelationship added: ",
[
source,
companion_name,
relationship,
projected_separation_arcsec,
projected_separation_error,
comment,
ref,
],
)
except sqlalchemy.exc.IntegrityError as e:
if "UNIQUE constraint failed:" in str(e):
msg = "The companion may be a duplicate."
logger.error(msg)
raise AstroDBError(msg)
else:
msg = (
"Make sure all required parameters are provided. \\"
"Other possible errors: source may not exist in Sources table \\"
"or the reference may not exist in the Publications table. "
)
logger.error(msg)
raise AstroDBError(msg)