Simpler string processing

This commit is contained in:
Allen Downey
2020-12-29 16:51:48 -05:00
parent acb069f529
commit 71965d4c98
3 changed files with 96 additions and 230 deletions
+28 -133
View File
@@ -1752,154 +1752,51 @@
"\"\"\"\n",
"```\n",
"\n",
"`corners_icrs` behaves like a list, so we can use a `for` loop to iterate through the points."
"The following function does the job:"
]
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 58,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<SkyCoord (ICRS): (ra, dec) in deg\n",
" (146.27533314, 19.26190982)>\n",
"<SkyCoord (ICRS): (ra, dec) in deg\n",
" (135.42163944, 25.87738723)>\n",
"<SkyCoord (ICRS): (ra, dec) in deg\n",
" (141.60264825, 34.3048303)>\n",
"<SkyCoord (ICRS): (ra, dec) in deg\n",
" (152.81671045, 27.13611254)>\n",
"<SkyCoord (ICRS): (ra, dec) in deg\n",
" (146.27533314, 19.26190982)>\n"
]
}
],
"outputs": [],
"source": [
"for point in corners_icrs:\n",
" print(point)"
"def skycoord_to_string(skycoord):\n",
" \"\"\"Convert SkyCoord to string.\"\"\"\n",
" t = skycoord.to_string()\n",
" s = ' '.join(t)\n",
" return s.replace(' ', ', ')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From that, we can select the coordinates `ra` and `dec`:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"146d16m31.1993s 19d15m42.8754s\n",
"135d25m17.902s 25d52m38.594s\n",
"141d36m09.5337s 34d18m17.3891s\n",
"152d49m00.1576s 27d08m10.0051s\n",
"146d16m31.1993s 19d15m42.8754s\n"
]
}
],
"source": [
"for point in corners_icrs:\n",
" print(point.ra, point.dec)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The results are quantities with units, but if we select the `value` part, we get a dimensionless floating-point number."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"146.27533313607782 19.261909820533692\n",
"135.42163944306296 25.87738722767213\n",
"141.60264825107333 34.304830296257144\n",
"152.81671044675923 27.136112541397996\n",
"146.27533313607782 19.261909820533692\n"
]
}
],
"source": [
"for point in corners_icrs:\n",
" print(point.ra.value, point.dec.value)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can use string `format` to convert these numbers to strings."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['146.27533313607782, 19.261909820533692',\n",
" '135.42163944306296, 25.87738722767213',\n",
" '141.60264825107333, 34.304830296257144',\n",
" '152.81671044675923, 27.136112541397996',\n",
" '146.27533313607782, 19.261909820533692']"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"point_base = \"{point.ra.value}, {point.dec.value}\"\n",
"`SkyCoord` provides `to_string`, which returns a list of strings.\n",
"\n",
"t = [point_base.format(point=point)\n",
" for point in corners_icrs]\n",
"t"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The result is a list of strings, which we can join into a single string using `join`."
"We use `join` to make a single string with spaces between the coordinates.\n",
"\n",
"Then we use `replace` to add commas between the coordinates. "
]
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'146.27533313607782, 19.261909820533692, 135.42163944306296, 25.87738722767213, 141.60264825107333, 34.304830296257144, 152.81671044675923, 27.136112541397996, 146.27533313607782, 19.261909820533692'"
"'146.275, 19.2619, 135.422, 25.8774, 141.603, 34.3048, 152.817, 27.1361, 146.275, 19.2619'"
]
},
"execution_count": 24,
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"point_list = ', '.join(t)\n",
"point_list = skycoord_to_string(corners_icrs)\n",
"point_list"
]
},
@@ -1907,14 +1804,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that we invoke `join` on a string and pass the list as an argument.\n",
"\n",
"Before we can assemble the query, we need `columns` again (as we saw in the previous notebook)."
]
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
@@ -1930,7 +1825,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
@@ -1952,7 +1847,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 52,
"metadata": {},
"outputs": [
{
@@ -1964,7 +1859,7 @@
"WHERE parallax < 1\n",
" AND bp_rp BETWEEN -0.75 AND 2 \n",
" AND 1 = CONTAINS(POINT(ra, dec), \n",
" POLYGON(146.27533313607782, 19.261909820533692, 135.42163944306296, 25.87738722767213, 141.60264825107333, 34.304830296257144, 152.81671044675923, 27.136112541397996, 146.27533313607782, 19.261909820533692))\n",
" POLYGON(146.275, 19.2619, 135.422, 25.8774, 141.603, 34.3048, 152.817, 27.1361, 146.275, 19.2619))\n",
"\n"
]
}
@@ -1986,7 +1881,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 53,
"metadata": {
"scrolled": true
},
@@ -1996,7 +1891,7 @@
"output_type": "stream",
"text": [
"INFO: Query finished. [astroquery.utils.tap.core]\n",
"<Table length=140340>\n",
"<Table length=140339>\n",
" name dtype unit description n_bad \n",
"--------------- ------- -------- ------------------------------------------------------------------ ------\n",
" source_id int64 Unique source identifier (unique within a particular Data Release) 0\n",
@@ -2006,11 +1901,11 @@
" pmdec float64 mas / yr Proper motion in declination direction 0\n",
" parallax float64 mas Parallax 0\n",
" parallax_error float64 mas Standard error of parallax 0\n",
"radial_velocity float64 km / s Radial velocity 139374\n",
"Jobid: 1609260439320O\n",
"radial_velocity float64 km / s Radial velocity 139373\n",
"Jobid: 1609277169233O\n",
"Phase: COMPLETED\n",
"Owner: None\n",
"Output file: async_20201229114719.vot\n",
"Output file: async_20201229162609.vot\n",
"Results: None\n"
]
}
@@ -2029,16 +1924,16 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"140340"
"140339"
]
},
"execution_count": 29,
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
+66 -95
View File
File diff suppressed because one or more lines are too long
+2 -2
View File
@@ -1071,14 +1071,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The intention seems to be to define a polygon that gets wider as `g` increases.\n",
"The intention is to define a polygon that gets wider as `g` increases, to reflect increasing uncertainty.\n",
"\n",
"But we can do about as well with a simpler formula:"
]
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [