Skip to content

external_query.py

ned(coord, radius)

Perform a cone search for sources with NED.

Parameters:

Name Type Description Default
coord SkyCoord

The coordinate of the centre of the cone.

required
radius Angle

The radius of the cone in angular units.

required

Returns:

Type Description
List[Dict[str, Any]]

A list of dicts, where each dict is a query result row with the following keys:

  • object_name: the name of the astronomical object.
  • database: the source of the result, i.e. NED.
  • separation_arcsec: separation to the query coordinate in arcsec.
  • otype: object type.
  • otype_long: long form of the object type.
  • ra_hms: RA coordinate string in hms format.
  • dec_dms: Dec coordinate string in ±dms format.
Source code in vast_pipeline/utils/external_query.py
 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
def ned(coord: SkyCoord, radius: Angle) -> List[Dict[str, Any]]:
    """Perform a cone search for sources with NED.

    Args:
        coord: The coordinate of the centre of the cone.
        radius: The radius of the cone in angular units.

    Returns:
        A list of dicts, where each dict is a query result row with the following keys:

            - object_name: the name of the astronomical object.
            - database: the source of the result, i.e. NED.
            - separation_arcsec: separation to the query coordinate in arcsec.
            - otype: object type.
            - otype_long: long form of the object type.
            - ra_hms: RA coordinate string in hms format.
            - dec_dms: Dec coordinate string in ±dms format.
    """
    # NED API doesn't supply the long-form object types.
    # Copied from https://ned.ipac.caltech.edu/Documents/Guides/Database
    NED_OTYPES = {
        "*": "Star or Point Source",
        "**": "Double star",
        "*Ass": "Stellar association",
        "*Cl": "Star cluster",
        "AbLS": "Absorption line system",
        "Blue*": "Blue star",
        "C*": "Carbon star",
        "EmLS": "Emission line source",
        "EmObj": "Emission object",
        "exG*": "Extragalactic star (not a member of an identified galaxy)",
        "Flare*": "Flare star",
        "G": "Galaxy",
        "GammaS": "Gamma ray source",
        "GClstr": "Cluster of galaxies",
        "GGroup": "Group of galaxies",
        "GPair": "Galaxy pair",
        "GTrpl": "Galaxy triple",
        "G_Lens": "Lensed image of a galaxy",
        "HII": "HII region",
        "IrS": "Infrared source",
        "MCld": "Molecular cloud",
        "Neb": "Nebula",
        "Nova": "Nova",
        "Other": "Other classification (e.g. comet; plate defect)",
        "PN": "Planetary nebula",
        "PofG": "Part of galaxy",
        "Psr": "Pulsar",
        "QGroup": "Group of QSOs",
        "QSO": "Quasi-stellar object",
        "Q_Lens": "Lensed image of a QSO",
        "RadioS": "Radio source",
        "Red*": "Red star",
        "RfN": "Reflection nebula",
        "SN": "Supernova",
        "SNR": "Supernova remnant",
        "UvES": "Ultraviolet excess source",
        "UvS": "Ultraviolet source",
        "V*": "Variable star",
        "VisS": "Visual source",
        "WD*": "White dwarf",
        "WR*": "Wolf-Rayet star",
        "XrayS": "X-ray source",
        "!*": "Galactic star",
        "!**": "Galactic double star",
        "!*Ass": "Galactic star association",
        "!*Cl": "Galactic Star cluster",
        "!Blue*": "Galactic blue star",
        "!C*": "Galactic carbon star",
        "!EmObj": "Galactic emission line object",
        "!Flar*": "Galactic flare star",
        "!HII": "Galactic HII region",
        "!MCld": "Galactic molecular cloud",
        "!Neb": "Galactic nebula",
        "!Nova": "Galactic nova",
        "!PN": "Galactic planetary nebula",
        "!Psr": "Galactic pulsar",
        "!RfN": "Galactic reflection nebula",
        "!Red*": "Galactic red star",
        "!SN": "Galactic supernova",
        "!SNR": "Galactic supernova remnant",
        "!V*": "Galactic variable star",
        "!WD*": "Galactic white dwarf",
        "!WR*": "Galactic Wolf-Rayet star",
    }
    ned_result_table = Ned.query_region(coord, radius=radius)
    if ned_result_table is None or len(ned_result_table) == 0:
        ned_results_dict_list = []
    else:
        ned_results_df = ned_result_table[
            ["Object Name", "Separation", "Type", "RA", "DEC"]
        ].to_pandas()
        ned_results_df = ned_results_df.rename(
            columns={
                "Object Name": "object_name",
                "Separation": "separation_arcsec",
                "Type": "otype",
                "RA": "ra_hms",
                "DEC": "dec_dms",
            }
        )
        ned_results_df["otype_long"] = ned_results_df.otype.replace(NED_OTYPES)
        # convert NED result separation (arcmin) to arcsec
        ned_results_df["separation_arcsec"] = ned_results_df["separation_arcsec"] * 60
        # convert coordinates to RA (hms) Dec (dms) strings
        ned_results_df["ra_hms"] = Longitude(
            ned_results_df["ra_hms"], unit="deg"
        ).to_string(unit="hourangle")
        ned_results_df["dec_dms"] = Latitude(
            ned_results_df["dec_dms"], unit="deg"
        ).to_string(unit="deg")
        ned_results_df["database"] = "NED"
        # convert dataframe to dict and replace float NaNs with None for JSON encoding
        ned_results_dict_list = ned_results_df.sort_values("separation_arcsec").to_dict(
            orient="records"
        )
    return ned_results_dict_list

simbad(coord, radius)

Perform a cone search for sources with SIMBAD.

Parameters:

Name Type Description Default
coord SkyCoord

The coordinate of the centre of the cone.

required
radius Angle

The radius of the cone in angular units.

required

Returns:

Type Description
List[Dict[str, Any]]

A list of dicts, where each dict is a query result row with the following keys:

  • object_name: the name of the astronomical object.
  • database: the source of the result, i.e. SIMBAD.
  • separation_arcsec: separation to the query coordinate in arcsec.
  • otype: object type.
  • otype_long: long form of the object type.
  • ra_hms: RA coordinate string in hms format.
  • dec_dms: Dec coordinate string in ±dms format.
Source code in vast_pipeline/utils/external_query.py
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
def simbad(coord: SkyCoord, radius: Angle) -> List[Dict[str, Any]]:
    """Perform a cone search for sources with SIMBAD.

    Args:
        coord: The coordinate of the centre of the cone.
        radius: The radius of the cone in angular units.

    Returns:
        A list of dicts, where each dict is a query result row with the following keys:

            - object_name: the name of the astronomical object.
            - database: the source of the result, i.e. SIMBAD.
            - separation_arcsec: separation to the query coordinate in arcsec.
            - otype: object type.
            - otype_long: long form of the object type.
            - ra_hms: RA coordinate string in hms format.
            - dec_dms: Dec coordinate string in ±dms format.
    """
    CustomSimbad = Simbad()
    CustomSimbad.add_votable_fields(
        "distance_result",
        "otype(S)",
        "otype(V)",
        "otypes",
    )
    try:
        simbad_result_table = CustomSimbad.query_region(coord, radius=radius)
    except requests.HTTPError:
        # try the Harvard mirror
        CustomSimbad.SIMBAD_URL = "https://simbad.harvard.edu/simbad/sim-script"
        simbad_result_table = CustomSimbad.query_region(coord, radius=radius)
    if simbad_result_table is None:
        simbad_results_dict_list = []
    else:
        simbad_results_df = simbad_result_table[
            ["MAIN_ID", "DISTANCE_RESULT", "OTYPE_S", "OTYPE_V", "RA", "DEC"]
        ].to_pandas()
        simbad_results_df = simbad_results_df.rename(
            columns={
                "MAIN_ID": "object_name",
                "DISTANCE_RESULT": "separation_arcsec",
                "OTYPE_S": "otype",
                "OTYPE_V": "otype_long",
                "RA": "ra_hms",
                "DEC": "dec_dms",
            }
        )
        simbad_results_df["database"] = "SIMBAD"
        # convert coordinates to RA (hms) Dec (dms) strings
        simbad_results_df["ra_hms"] = Longitude(
            simbad_results_df["ra_hms"], unit="hourangle"
        ).to_string(unit="hourangle")
        simbad_results_df["dec_dms"] = Latitude(
            simbad_results_df["dec_dms"], unit="deg"
        ).to_string(unit="deg")
        simbad_results_dict_list = simbad_results_df.to_dict(orient="records")
    return simbad_results_dict_list

tns(coord, radius)

Perform a cone search for sources with the Transient Name Server (TNS).

Parameters:

Name Type Description Default
coord SkyCoord

The coordinate of the centre of the cone.

required
radius Angle

The radius of the cone in angular units.

required

Returns:

Type Description
List[Dict[str, Any]]

A list of dicts, where each dict is a query result row with the following keys:

  • object_name: the name of the transient.
  • database: the source of the result, i.e. TNS.
  • separation_arcsec: separation to the query coordinate in arcsec.
  • otype: object type.
  • otype_long: long form of the object type. Not given by TNS, will always be an empty string.
  • ra_hms: RA coordinate string in hms format.
  • dec_dms: Dec coordinate string in ±dms format.
Source code in vast_pipeline/utils/external_query.py
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
def tns(coord: SkyCoord, radius: Angle) -> List[Dict[str, Any]]:
    """Perform a cone search for sources with the Transient Name Server (TNS).

    Args:
        coord: The coordinate of the centre of the cone.
        radius: The radius of the cone in angular units.

    Returns:
        A list of dicts, where each dict is a query result row with the following keys:

            - object_name: the name of the transient.
            - database: the source of the result, i.e. TNS.
            - separation_arcsec: separation to the query coordinate in arcsec.
            - otype: object type.
            - otype_long: long form of the object type. Not given by TNS, will always be
                an empty string.
            - ra_hms: RA coordinate string in hms format.
            - dec_dms: Dec coordinate string in ±dms format.
    """
    TNS_API_URL = "https://www.wis-tns.org/api/"
    headers = {
        "user-agent": settings.TNS_USER_AGENT,
    }

    search_dict = {
        "ra": coord.ra.to_string(unit="hourangle", sep=":", pad=True),
        "dec": coord.dec.to_string(unit="deg", sep=":", alwayssign=True, pad=True),
        "radius": str(radius.value),
        "units": radius.unit.name,
    }
    r = requests.post(
        urljoin(TNS_API_URL, "get/search"),
        data={"api_key": settings.TNS_API_KEY, "data": json.dumps(search_dict)},
        headers=headers,
    )
    tns_results_dict_list: List[Dict[str, Any]]
    if r.ok:
        tns_results_dict_list = r.json()["data"]["reply"]
        # Get details for each object result. TNS API doesn't support doing this in one
        # request, so we iterate.
        for result in tns_results_dict_list:
            search_dict = {
                "objname": result["objname"],
            }
            r = requests.post(
                urljoin(TNS_API_URL, "get/object"),
                data={"api_key": settings.TNS_API_KEY, "data": json.dumps(search_dict)},
                headers=headers,
            )
            if r.ok:
                object_dict = r.json()["data"]["reply"]
                object_coord = SkyCoord(
                    ra=object_dict["radeg"], dec=object_dict["decdeg"], unit="deg"
                )
                result["otype"] = object_dict["object_type"]["name"]
                if result["otype"] is None:
                    result["otype"] = ""
                result["otype_long"] = ""
                result["separation_arcsec"] = coord.separation(object_coord).arcsec
                result["ra_hms"] = object_coord.ra.to_string(unit="hourangle")
                result["dec_dms"] = object_coord.dec.to_string(unit="deg")
                result["database"] = "TNS"
                result["object_name"] = object_dict["objname"]
    return tns_results_dict_list