How to index observable coordinates in an astropy Table

不打扰是莪最后的温柔 提交于 2019-12-24 00:58:29

问题


I asked this question (How to sort through a list of observable coordinates?) yesterday about sorting a list of coordinates to remove certain values below a threshold, I got a great answer from @MSeifert but I have a table in which these coordinate values are matched up with other properties of the targets (e.g. apparent magnitude and Alt/Az coordinates), so what I am asking for now is a way to do this masking technique in an astropy.table rather than an astropy.coordinates.SkyCoord list from my previous question.

Let me outline the problem:

I obtain my initial RA & Dec coordinates using this line:

radecs = astropy.coordinates.SkyCoord(ra=phi*u.rad, dec=(0.5*np.pi - theta)*u.rad)

Then I convert this to a Table:

x=Table([radecs.ra,radecs.dec,altaz.alt,altaz.az,app_mag], names=('RA', 
'Dec','Altitude', 'Azimuth','Apparent Magnitude'))

Ignoring the magnitude column it then looks like this:

I would like to remove the rows that correspond to a declination less than -10 degrees, similar to how I asked to do this in my first question, but to also remove the whole row of data from the Table, not just the 'radecs' values.

I found this question online which suggsts to me that the function for removing rows from Tables doesnt exists (at least in June 2013), but I think there should be a fairly simple solution to my problem.


回答1:


I'm sorry I asked you to ask another question. I thought you may have saved the coordinates in one column rather than in different columns.

If they are in different columns it works exactly like in the other answer (but you must change the indexing a bit) because you need to index columns rather than attributes:

x=Table(...)
x1 = x[x['Dec'] > -10*u.degree] # Remove everything below -10degree declination
x2 = x[x['Apparent Magnitude'] < 20] # Remove every star with a magnitude above 20

so x['Apparent Magnitude'] gives you the column 'Apparent Magnitude' and so on.

Besides: You could also specify more complex conditions:

magnitude_below_20 = x['Apparent Magnitude'] < 20
dec_above_m10 = x['Dec'] > -10*u.degree
# Get every row that satisfies both conditions:
x_from_comb_condition = x[magnitude_below_20 & dec_above_m10]
# or using the numpy-ufunc (gives the same result):
x_from_comb_condition = x[np.logical_and(magnitude_below_20, dec_above_m10)]

or if one condition applies (| or np.logical_or) or if the condition does not apply (~ or np.logical_not)


For example:

from astropy.coordinates import SkyCoord
from astropy.table import Table
import astropy.units as u
import numpy as np
phi = np.linspace(0,2*np.pi,20)
theta = np.linspace(0, np.pi, 20)
radecs = SkyCoord(ra=phi*u.rad, dec=(0.5*np.pi - theta)*u.rad)
app_mag = np.random.uniform(15,25, 20)
x=Table([radecs.ra,radecs.dec,app_mag], names=('RA', 'Dec','Apparent Magnitude'))
x[x['Dec'] > -10*u.degree]

gives me:

RA              Dec             Apparent Magnitude
deg             deg 
float64         float64         float64
0.0             90.0            20.1080708665
18.9473684211   80.5263157895   22.7223534546
37.8947368421   71.0526315789   19.4416167208
56.8421052632   61.5789473684   20.7207435685
75.7894736842   52.1052631579   19.9318711443
94.7368421053   42.6315789474   23.8544483535
113.684210526   33.1578947368   15.8981196334
132.631578947   23.6842105263   24.2866475431
151.578947368   14.2105263158   15.9503148326
170.526315789   4.73684210526   16.5505303858
189.473684211   -4.73684210526  24.194771397

whereas the complete Table was:

RA              Dec             Apparent Magnitude
deg             deg 
float64         float64         float64
0.0             90.0            20.1080708665
18.9473684211   80.5263157895   22.7223534546
37.8947368421   71.0526315789   19.4416167208
56.8421052632   61.5789473684   20.7207435685
75.7894736842   52.1052631579   19.9318711443
94.7368421053   42.6315789474   23.8544483535
113.684210526   33.1578947368   15.8981196334
132.631578947   23.6842105263   24.2866475431
151.578947368   14.2105263158   15.9503148326
170.526315789   4.73684210526   16.5505303858
189.473684211   -4.73684210526  24.194771397
208.421052632   -14.2105263158  15.4721094564
227.368421053   -23.6842105263  20.6082525987
246.315789474   -33.1578947368  21.9730819638
265.263157895   -42.6315789474  17.3627571053
284.210526316   -52.1052631579  22.7065806097
303.157894737   -61.5789473684  23.7244993197
322.105263158   -71.0526315789  19.7676029836
341.052631579   -80.5263157895  19.2663871267
0.0             -90.0           19.5025214878

so this kept only the rows where the condition was met and "ignored" all the others.


Removing rows manually (if you know the row number) is possible:

x.remove_row(0) # Removes the first row
x.remove_row(-1) # Removes the last row

But normally indexing is what you want if you want to discard rows because of conditions. The remove_row is more for deleting one/two rows manually. Maybe because they were false positives or something else... And they work in-place. So never do x = x.remove_row(-1) because then x will be None.



来源:https://stackoverflow.com/questions/36011398/how-to-index-observable-coordinates-in-an-astropy-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!