In [14]:
from astropy.table import Table, Column
from astropy import log
log.setLevel('WARNING')

from astropy.io import ascii

Emmanuelle : deplacer des colonnes dans un fichiers dont les colonnes sont separees par des |

In [15]:
table = ascii.read("file.dat")  
In [16]:
print(table)
 col1  col2       col3             col4       ... col14 col15 col16 col17
----- ------ -------------- ----------------- ... ----- ----- ----- -----
N 147 103322 00^h33^m09.s79 18.849 +or- 0.005 ...    --    --    --    --
N 147 112780 00^h33^m05.s61 21.006 +or- 0.021 ...    --    --    --    --
IC 10 102032 00^h20^m14.s98 18.883 +or- 0.006 ...    --    --    --    --
IC 10 105880 00^h20^m12.s38 22.437 +or- 0.068 ...    --    --    --    --
IC 10 105975 00^h20^m12.s30 15.666 +or- 0.001 ...    --    --    --    --
IC 10 107349 00^h20^m11.s39 19.530 +or- 0.015 ...    --    --    --    --
In [17]:
columns = [Column(table["col1"], name="name"), 
           Column(table["col2"], name="id"), 
           Column(table["col3"], name="RAJ2000"), 
           Column(table["col11"], name="DEJ2000")]
for i in range(4, len(table.columns)+1):
    if i == 11: continue
    columns.append(table["col{}".format(i)])

new_table = Table(columns)
print(new_table)
 name   id      RAJ2000         DEJ2000     ... col14 col15 col16 col17
----- ------ -------------- --------------- ... ----- ----- ----- -----
N 147 103322 00^h33^m09.s79 +48^d29^m09.s28 ...    --    --    --    --
N 147 112780 00^h33^m05.s61 +48^d28^m47.s43 ...    --    --    --    --
IC 10 102032 00^h20^m14.s98 +59^d21^m01.s16 ...    --    --    --    --
IC 10 105880 00^h20^m12.s38 +59^d19^m41.s95 ...    --    --    --    --
IC 10 105975 00^h20^m12.s30 +59^d20^m42.s09 ...    --    --    --    --
IC 10 107349 00^h20^m11.s39 +59^d19^m03.s87 ...    --    --    --    --

Emmanuelle : jointure de table ascii

In [22]:
table1 = ascii.read("file1.dat")  
table2 = ascii.read("file2.dat") 

new_table1 = Table([Column(table1["col1"], name="id"),
                    Column(table1["col2"], name="text")])
new_table2 = Table([Column(table2["col1"], name="id"),
                    Column(table2["col2"], name="mag")])
In [23]:
from astropy.table import join
In [24]:
jointable = join(new_table1, new_table2, keys='id', join_type='outer')
In [25]:
print (jointable)
print (jointable.columns)
print (jointable["text"])
jointable.write("matable.ascii", format="ascii.fixed_width")
   id   ...  mag 
------- ... -----
5857.45 ...    --
 6161.3 ...  18.7
6162.17 ... 161.8
6166.44 ...  19.3
6169.04 ...  45.7
6169.56 ...  63.4
<TableColumns names=('id','text','mag')>
                                 text                                 
----------------------------------------------------------------------
Ca I  2.93   0.230  72.4       90.0  73.1  69.2       62.3  76.6  76.6
Ca I  2.52  -1.030  19.9       32.4        19.0             24.7  24.7
            Ca I  1.90  -0.090 150.9      160.9 160.5 147.0      145.0
                                                                    --
Ca I  2.52  -0.540  50.6       63.0  56.6  39.2       45.3  58.0  58.0
                                                                    --
WARNING: AstropyDeprecationWarning: matable.ascii already exists. Automatically overwriting ASCII files is deprecated. Use the argument 'overwrite=True' in the future. [astropy.io.ascii.ui]

Comment traiter le cas des tables a colonnes alignees ..?

In [26]:
with open("newfile1.dat", "w") as fout:
    with open("file1.dat", "r") as fd:
        fout.write(fd.read().replace("|"," "))
        
table1 = ascii.read("newfile1.dat", 
                    format="fixed_width_no_header", 
                    col_starts=(0,8,14,20,27,38,44,50,61,68,74)) 

print (table1)
  col1  col2 col3  col4  col5  col6  col7  col8  col9 col10 col11
------- ---- ---- ----- ----- ----- ----- ----- ----- ----- -----
5857.45 Ca I 2.93  0.23  72.4  90.0  73.1  69.2  62.3  76.6  76.6
 6161.3 Ca I 2.52 -1.03  19.9  32.4    --  19.0    --  24.7  24.7
6162.17 Ca I  1.9 -0.09 150.9 160.9 160.5 147.0 145.0    --    --
6169.04 Ca I 2.52 -0.54  50.6  63.0  56.6  39.2  45.3  58.0  58.0

avec le ReadMe

In [27]:
table1 = ascii.read("hip_main.dat", readme="ReadMe")
table2 = ascii.read("hip_va_1.dat", readme="ReadMe")
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-27-9db646a5a2f7> in <module>()
----> 1 table1 = ascii.read("hip_main.dat", readme="ReadMe")
      2 table2 = ascii.read("hip_va_1.dat", readme="ReadMe")

/usr/lib64/python3.4/site-packages/astropy/io/ascii/ui.py in read(table, guess, **kwargs)
    323         # through below to the non-guess way so that any problems result in a
    324         # more useful traceback.
--> 325         dat = _guess(table, new_kwargs, format, fast_reader_param)
    326         if dat is None:
    327             guess = False

/usr/lib64/python3.4/site-packages/astropy/io/ascii/ui.py in _guess(table, read_kwargs, format, fast_reader)
    471             reader = get_reader(**guess_kwargs)
    472             reader.guessing = True
--> 473             dat = reader.read(table)
    474             _read_trace.append({'kwargs': guess_kwargs, 'status': 'Success (guessing)',
    475                                 'dt': '{0:.3f} ms'.format((time.time() - t0) * 1000)})

/usr/lib64/python3.4/site-packages/astropy/io/ascii/cds.py in read(self, table)
    320                     return table
    321         else:
--> 322             return super(Cds, self).read(table)

/usr/lib64/python3.4/site-packages/astropy/io/ascii/core.py in read(self, table)
   1197                 col.str_vals.append(str_vals[j])
   1198 
-> 1199         self.data.masks(cols)
   1200         if hasattr(self.header, 'table_meta'):
   1201             self.meta['table'].update(self.header.table_meta)

/usr/lib64/python3.4/site-packages/astropy/io/ascii/core.py in masks(self, cols)
    743         if self.fill_values:
    744             self._set_fill_values(cols)
--> 745             self._set_masks(cols)
    746 
    747     def _set_fill_values(self, cols):

/usr/lib64/python3.4/site-packages/astropy/io/ascii/core.py in _set_masks(self, cols)
    794             for col in (col for col in cols if col.fill_values):
    795                 col.mask = numpy.zeros(len(col.str_vals), dtype=numpy.bool)
--> 796                 for i, str_val in ((i, x) for i, x in enumerate(col.str_vals)
    797                                    if x in col.fill_values):
    798                     col.str_vals[i] = col.fill_values[str_val]

/usr/lib64/python3.4/site-packages/astropy/io/ascii/core.py in <genexpr>(.0)
    795                 col.mask = numpy.zeros(len(col.str_vals), dtype=numpy.bool)
    796                 for i, str_val in ((i, x) for i, x in enumerate(col.str_vals)
--> 797                                    if x in col.fill_values):
    798                     col.str_vals[i] = col.fill_values[str_val]
    799                     col.mask[i] = True

KeyboardInterrupt: 
In [ ]:
jointable = join(table1, table2, keys="HIP", join_type="left")
In [ ]:
print (jointable)

utilisation d'une table MRT

necessite la librairie pyreadme : http://cds.u-strasbg.fr/resources/doku.php?id=anafile

  • download package python
  • tar -xzvf pyreadme_1.2.xxx.tar.gz
  • copier le repertoire pyreadme_1.2.xxx/cds dans le repertoire courrant
In [28]:
from cds import *
In [29]:
tablemaker = CDSTablesMaker()
table = tablemaker.addTable("ajaa6d51t2_mrt.txt")
print (table.table)
    ID    Num RAh RAm  RAs  DEd  DEm    DEs      HJD     RVel  e_RVel  r_RVel
               h  min   s   deg arcmin arcsec     d     km / s km / s        
--------- --- --- --- ----- --- ------ ------ --------- ------ ------ -------
LeoII-001   4  11  13 34.31  22      9   11.7 2455591.9  71.64   0.78 Paper I
LeoII-001   4  11  13 34.32  22      9  11.59 2452704.2  72.84   2.55    KK07
LeoII-001   4  11  13 34.32  22      9  11.59 2453061.2  75.24   2.21    KK07
LeoII-001   4  11  13  34.3  22      9   11.7 2453770.0  68.81   2.36    KG10
LeoII-002   3  11  13 18.22  22      8   29.4 2455597.9   67.9   1.28 Paper I
LeoII-002   3  11  13 18.22  22      8   29.4 2456340.9  81.64   1.19 Paper I
LeoII-002   3  11  13 18.23  22      8  29.29 2452704.2  74.99  28.67    KK07
LeoII-003   2  11  13 28.53  22      8   17.4 2455599.8  75.43   1.85 Paper I
LeoII-003   2  11  13 28.53  22      8   17.3 2453061.2  80.03   1.43    KK07
LeoII-004   7  11  13 22.25  22      3  36.63 2454212.8  79.73   1.05 Paper I
      ... ... ... ...   ... ...    ...    ...       ...    ...    ...     ...
LeoII-192   2  11  13 29.14  22      6   4.72 2452704.2  66.63   6.69    KK07
LeoII-192   2  11  13 29.14  22      6   4.72 2453061.2  68.22   2.34    KK07
LeoII-193   2  11  13 41.63  22      8   52.4 2452704.2  69.45   8.94    KK07
LeoII-193   2  11  13 41.63  22      8   52.4 2453061.2  68.49   2.28    KK07
LeoII-194   2  11  13 24.36  22     10  58.51 2452693.2  82.52  23.34    KK07
LeoII-194   2  11  13 24.36  22     10  58.51 2453061.2  89.95   2.41    KK07
LeoII-195   2  11  13 30.89  22      9   32.8 2452704.2  71.71   2.77    KK07
LeoII-195   2  11  13 30.89  22      9   32.8 2453061.2  72.58    1.5    KK07
LeoII-196   2  11  13 31.58  22      8   34.2 2453770.0  70.95   2.19    KG10
LeoII-196   2  11  13 31.54  22      8  33.91 2449432.0  67.34    3.3     V95
Length = 596 rows
In [30]:
tablemaker.writeCDSTables()
 
tablemaker.title = "my title"
tablemaker.author = 'theauthorname'

tablemaker.makeReadMe()
                                                      (theauthorname, Date ?)
================================================================================
my title
    Authors ?
    =
================================================================================
Keywords: 

Abstract:
  Abstract ?

File Summary:
--------------------------------------------------------------------------------
 FileName    Lrecl   Records    Explanations
--------------------------------------------------------------------------------
ReadMe                 80        .  this file
ajaa6d51t2_mrt.txt.cds 66       18  

--------------------------------------------------------------------------------
Byte-by-byte Description of file: ajaa6d51t2_mrt.txt.cds
--------------------------------------------------------------------------------
 Bytes Format Units  Label     Explanations
--------------------------------------------------------------------------------
 1- 9   A9     ---    ID      ? Stellar identifier
11-11   I1     ---    Num     [2/7] ? Number of observations
13-14   I2     h      RAh     [11/11] ? Hour of Right Ascension (J2000)
16-17   I2     min    RAm     [12/14] ? Minute of Right Ascension (J2000)
19-23   F5.2   s      RAs     [3.35/59.75] ? Second of Right Ascension (J2000)
25-26   I2     deg    DEd     [22/22] ? Degree of Declination (J2000)
28-29   I2     arcmin DEm     [3/16] ? Arcminute of Declination (J2000)
31-35   F5.2   arcsec DEs     [0.78/59.11] ? Arcsecond of Declination (J2000)
37-45   F9.1   d      HJD     [2449430.8/2456341.9] ? Heliocentric Julian Date
47-52   F6.2   km / s RVel    [52.4/103.01] ? Radial velocity (1)
54-58   F5.2   km / s e_RVel  [0.54/31.61] ? Uncertainty in RVel
60-66   A7     ---    r_RVel  ? Reference for RVel (2)

--------------------------------------------------------------------------------



See also:
None
================================================================================

In [ ]: