Skip to content

Instantly share code, notes, and snippets.

@keflavich
Last active February 9, 2021 01:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save keflavich/e798e10e3bf9a93d1453 to your computer and use it in GitHub Desktop.
Save keflavich/e798e10e3bf9a93d1453 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:906340654bfbcdbcdc4819e4b5002fa9168b9e284a139180e19d2e6503a76547"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Querying multiple sources from the ALMA archive#\n",
"This example shows how to loop over a list of sources and query each of them in turn. It will be slow if you have a large number of objects, but astroquery will cache your results to make subsequent searches faster. Of course, if you want to re-query the archive each time, you can just pass the keyword ``cache=False`` to disable it."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, import the ALMA tool:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from astroquery.alma import Alma"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Just to make sure you're up to date, you can check the version:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import astroquery\n",
"print(astroquery.__version__)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0.3.dev2853\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We'll do a search for all 110 Messier objects"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"MessierNames = [\"M{0:d}\".format(ii) for ii in range(1,111)]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"``Alma.query_object`` will query an object by name after sending the name to the SIMBAD/SESAME name resolver. ``cache=True`` is the default, but you can change it here if you'd like."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"MessierALMAData = {name: Alma.query_object(name, cache=True) for name in MessierNames}"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Each entry in the dictionary is a an `astropy.table.Table`, which contains a lot more information than you probably want at first glance (but you may want it all later). So we'll just find out the unique set of ALMA bands that have been observed toward each source."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"MessierALMABands = {name: set(MessierALMAData[name]['Band']) for name in MessierALMAData}"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then we filter that result by Messier objects that have been observed in at least one band. (an empty set is `False`)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"observed_messier_objects = {name: MessierALMABands[name] for name in MessierALMABands if MessierALMABands[name]}\n",
"observed_messier_objects"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"{'M104': {3, 6, 7, 9},\n",
" 'M17': {6},\n",
" 'M18': {6},\n",
" 'M42': {3, 6, 7, 9},\n",
" 'M43': {3, 6, 7, 9},\n",
" 'M58': {3},\n",
" 'M65': {3},\n",
" 'M66': {3},\n",
" 'M77': {3, 7, 9},\n",
" 'M80': {6, 7},\n",
" 'M83': {3},\n",
" 'M87': {7}}"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"len(observed_messier_objects)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
"12"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So there are 12 observed Messier objects as of writing this document (April 13, 2015)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What if you have positions, not names?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from astropy import coordinates\n",
"from astropy import units as u"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 22
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make your coordinate list. I've chosen Galactic coordinates because I know they're at least moderately interesting. The last of these is Sgr B2, so it definitely has hits, but the others are unlikely to be observed."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"coord_list = coordinates.SkyCoord([(1.0, 2.0), (-50.0, 1.0), (0.6667,-0.03)]*u.deg, frame='galactic')\n",
"coord_list"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 21,
"text": [
"<SkyCoord (Galactic): (l, b) in deg\n",
" [(1.0, 2.0), (310.0, 1.0), (0.6667, -0.03)]>"
]
}
],
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You need to use the `query_region` method for coordinates. `radius` is optional:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"results = [Alma.query_region(coord, radius=1*u.arcmin) for coord in coord_list]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Each entry in the `results` list is an astropy `Table`. You can just look at its length to figure out if there were any observations performed."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"[len(x) for x in results]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 19,
"text": [
"[0, 0, 9]"
]
}
],
"prompt_number": 19
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The last entry has some hits! We can inspect the table directly:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"results[-1]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"&lt;Table masked=True length=9&gt;\n",
"<table id=\"table4521242512\">\n",
"<thead><tr><th>Project code</th><th>Source name</th><th>RA</th><th>Dec</th><th>Band</th><th>Frequency resolution</th><th>Integration</th><th>Release date</th><th>Frequency support</th><th>Velocity resolution</th><th>Pol products</th><th>Observation date</th><th>PI name</th><th>PWV</th><th>Member ous id</th><th>Asdm uid</th><th>Project title</th><th>Project type</th><th>Scan intent</th></tr></thead>\n",
"<thead><tr><th></th><th></th><th>deg</th><th>deg</th><th></th><th>kHz</th><th>s</th><th></th><th>GHz</th><th>m / s</th><th></th><th></th><th></th><th>mm</th><th></th><th></th><th></th><th></th><th></th></tr></thead>\n",
"<thead><tr><th>string128</th><th>string2048</th><th>float64</th><th>float64</th><th>int32</th><th>float64</th><th>float64</th><th>object</th><th>string21600</th><th>float64</th><th>string512</th><th>object</th><th>string2048</th><th>float32</th><th>string256</th><th>string256</th><th>string2048</th><th>string128</th><th>string2048</th></tr></thead>\n",
"<tr><td>2011.0.00017.S</td><td>SgrB2-N</td><td>266.83279166666671</td><td>-28.371111111111112</td><td>3</td><td>488.28125</td><td>304.88400000000001</td><td>2014-09-12</td><td>[95.02..96.90GHz,488.28kHz, XX] U [96.85..98.72GHz,488.28kHz, XX] U [107.02..108.89GHz,488.28kHz, XX] U [108.84..110.72GHz,488.28kHz, XX]</td><td>1422.9831324665756</td><td>XX</td><td>2012-07-04 05:37:48</td><td>Belloche, Arnaud</td><td>2.033288</td><td>uid://A002/X327408/X14</td><td>uid://A002/X44ddb2/X64a</td><td>Expanding the frontiers of chemical complexity with ALMA</td><td>S</td><td>TARGET</td></tr>\n",
"<tr><td>2011.0.00017.S</td><td>SgrB2-N</td><td>266.83279166666671</td><td>-28.371111111111112</td><td>3</td><td>488.28125</td><td>376.07100000000003</td><td>2014-09-12</td><td>[87.72..89.60GHz,488.28kHz, XX] U [89.55..91.42GHz,488.28kHz, XX] U [99.72..101.60GHz,488.28kHz, XX] U [101.55..103.42GHz,488.28kHz, XX]</td><td>1531.6503853223319</td><td>XX</td><td>2012-06-05 09:03:02</td><td>Belloche, Arnaud</td><td>0.66231287</td><td>uid://A002/X327408/X10</td><td>uid://A002/X41f8d2/X8e0</td><td>Expanding the frontiers of chemical complexity with ALMA</td><td>S</td><td>TARGET</td></tr>\n",
"<tr><td>2011.0.00017.S</td><td>SgrB2-N</td><td>266.83279166666671</td><td>-28.371111111111112</td><td>3</td><td>488.28125</td><td>547.71100000000001</td><td>2014-09-12</td><td>[95.02..96.90GHz,488.28kHz, XX] U [96.85..98.72GHz,488.28kHz, XX] U [107.02..108.89GHz,488.28kHz, XX] U [108.84..110.72GHz,488.28kHz, XX]</td><td>1422.9831324665756</td><td>XX</td><td>2012-08-01 02:31:53</td><td>Belloche, Arnaud</td><td>1.3851253</td><td>uid://A002/X327408/X14</td><td>uid://A002/X47ed8e/X3cd</td><td>Expanding the frontiers of chemical complexity with ALMA</td><td>S</td><td>TARGET</td></tr>\n",
"<tr><td>2011.0.00017.S</td><td>SgrB2-N</td><td>266.83279166666671</td><td>-28.371111111111112</td><td>3</td><td>488.28125</td><td>550.17899999999997</td><td>2014-09-12</td><td>[95.02..96.90GHz,488.28kHz, XX] U [96.85..98.72GHz,488.28kHz, XX] U [107.02..108.89GHz,488.28kHz, XX] U [108.84..110.72GHz,488.28kHz, XX]</td><td>1422.9831324665756</td><td>XX</td><td>2012-08-10 00:45:31</td><td>Belloche, Arnaud</td><td>1.7215753</td><td>uid://A002/X327408/X14</td><td>uid://A002/X494155/X23a</td><td>Expanding the frontiers of chemical complexity with ALMA</td><td>S</td><td>TARGET</td></tr>\n",
"<tr><td>2011.0.00017.S</td><td>SgrB2-N</td><td>266.83279166666671</td><td>-28.371111111111112</td><td>3</td><td>488.28125</td><td>619.88800000000003</td><td>2014-09-12</td><td>[87.72..89.60GHz,488.28kHz, XX] U [89.55..91.42GHz,488.28kHz, XX] U [99.72..101.60GHz,488.28kHz, XX] U [101.55..103.42GHz,488.28kHz, XX]</td><td>1531.6503853223319</td><td>XX</td><td>2012-07-01 07:03:55</td><td>Belloche, Arnaud</td><td>0.47709116</td><td>uid://A002/X327408/X10</td><td>uid://A002/X449249/Xde</td><td>Expanding the frontiers of chemical complexity with ALMA</td><td>S</td><td>TARGET</td></tr>\n",
"<tr><td>2011.0.00017.S</td><td>SgrB2-N</td><td>266.83279166666671</td><td>-28.371111111111112</td><td>3</td><td>488.28125</td><td>624.85599999999999</td><td>2014-09-12</td><td>[91.37..93.25GHz,488.28kHz, XX] U [93.20..95.07GHz,488.28kHz, XX] U [103.37..105.25GHz,488.28kHz, XX] U [105.19..107.07GHz,488.28kHz, XX]</td><td>1475.3184447597021</td><td>XX</td><td>2012-06-06 08:20:02</td><td>Belloche, Arnaud</td><td>0.52463043</td><td>uid://A002/X327408/X12</td><td>uid://A002/X4210d0/X815</td><td>Expanding the frontiers of chemical complexity with ALMA</td><td>S</td><td>TARGET</td></tr>\n",
"<tr><td>2011.0.00017.S</td><td>SgrB2-N</td><td>266.83279166666671</td><td>-28.371111111111112</td><td>3</td><td>488.28125</td><td>629.65200000000004</td><td>2014-09-12</td><td>[91.37..93.25GHz,488.28kHz, XX] U [93.20..95.07GHz,488.28kHz, XX] U [103.37..105.25GHz,488.28kHz, XX] U [105.19..107.07GHz,488.28kHz, XX]</td><td>1475.3184447597021</td><td>XX</td><td>2012-06-18 07:29:08</td><td>Belloche, Arnaud</td><td>1.1841874</td><td>uid://A002/X327408/X12</td><td>uid://A002/X436934/X28</td><td>Expanding the frontiers of chemical complexity with ALMA</td><td>S</td><td>TARGET</td></tr>\n",
"<tr><td>2011.0.00017.S</td><td>SgrB2-N</td><td>266.83279166666671</td><td>-28.371111111111112</td><td>3</td><td>488.28125</td><td>753.61000000000001</td><td>2014-09-12</td><td>[87.72..89.60GHz,488.28kHz, XX] U [89.55..91.42GHz,488.28kHz, XX] U [99.72..101.60GHz,488.28kHz, XX] U [101.55..103.42GHz,488.28kHz, XX]</td><td>1531.6503853223319</td><td>XX</td><td>2012-07-03 06:36:37</td><td>Belloche, Arnaud</td><td>1.5690569</td><td>uid://A002/X327408/X10</td><td>uid://A002/X44c3c1/X6cc</td><td>Expanding the frontiers of chemical complexity with ALMA</td><td>S</td><td>TARGET</td></tr>\n",
"<tr><td>2011.0.00017.S</td><td>SgrB2-N</td><td>266.83279166666671</td><td>-28.371111111111112</td><td>3</td><td>488.28125</td><td>855.72400000000005</td><td>2014-09-12</td><td>[84.09..85.96GHz,488.28kHz, XX] U [85.90..87.77GHz,488.28kHz, XX] U [96.15..98.02GHz,488.28kHz, XX] U [97.90..99.77GHz,488.28kHz, XX]</td><td>1591.7887038699191</td><td>XX</td><td>2012-08-27 01:05:09</td><td>Belloche, Arnaud</td><td>2.884989</td><td>uid://A002/X327408/Xe</td><td>uid://A002/X4b29af/X403</td><td>Expanding the frontiers of chemical complexity with ALMA</td><td>S</td><td>TARGET</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 23,
"text": [
"<Table masked=True length=9>\n",
" Project code Source name RA ... Project type Scan intent\n",
" deg ... \n",
" string128 string2048 float64 ... string128 string2048\n",
"-------------- ----------- ------------------ ... ------------ -----------\n",
"2011.0.00017.S SgrB2-N 266.83279166666671 ... S TARGET\n",
"2011.0.00017.S SgrB2-N 266.83279166666671 ... S TARGET\n",
"2011.0.00017.S SgrB2-N 266.83279166666671 ... S TARGET\n",
"2011.0.00017.S SgrB2-N 266.83279166666671 ... S TARGET\n",
"2011.0.00017.S SgrB2-N 266.83279166666671 ... S TARGET\n",
"2011.0.00017.S SgrB2-N 266.83279166666671 ... S TARGET\n",
"2011.0.00017.S SgrB2-N 266.83279166666671 ... S TARGET\n",
"2011.0.00017.S SgrB2-N 266.83279166666671 ... S TARGET\n",
"2011.0.00017.S SgrB2-N 266.83279166666671 ... S TARGET"
]
}
],
"prompt_number": 23
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or you can look at what bands were observed:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"set(results[-1]['Band'])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 26,
"text": [
"{3}"
]
}
],
"prompt_number": 26
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment