4

I'm trying to create an ascii table with some information on the header, the names and units of the columns and some data, it should look like this:

 # ... Header Info ...
          Name | Morphology |         ra_u |        dec_u | ...
               | InNS+B+MOI | HH:MM:SS.SSS | ±DD:MM:SS:SSS| ...
 ==============| ========== | ============ | ============ | ...
 1_Cam_A       | I          | 04:32:01.845 | +53:54:39.03   ...
 10_Lac        | I          | 22:39:15.679 | +39:03:01.01   ... 
...

So far I've tried with numpy.savetxt and astropy.ascii.writhe, numpy won't really solve my problems and with ascii.write I've been able to get something similar but not quite right:

              Name | Morphology |         ra_u |        dec_u | ...    
================== | ========== | ============ | ============ | ...
1_Cam_A            | I          | 04:32:01.845 | +53:54:39.03   ...
...

I'm using this code:

formato= {'Name':'%-23s','Morphology':'%-10s','ra_u':'%s','dec_u':'%s',...}
names=['Name','Morphology','ra_u','dec_u','Mag6']
units=['','InNS+B+MOI','HH:MM:SS.SSS','±DD:MM:SS:SSS',...]
ascii.write(data, output='pb.txt',format='fixed_width_two_line',position_char='=',delimiter=' | ',names=names, formats=formato)

So if I make a print in my terminal the table looks as it should except for the header info, but as I save it into a file the units disappear...

Is there any way to include them in the file?, or I need to save the file and edit it later?

P.D.: I'm also tried some other formats such as IPAC for ascii.write, in that case the problem is that includes a 4th row in the header like: '| null | null |.....' and I don't know how to get rid of it...

Thanks for the help

Un saludo.

Emberck
  • 55
  • 8
  • I'm a little confused about your example. What type of object is the `data` in your example? If it's an Astropy Table how are your "units" being stored, because they aren't units in any sense recognized by astropy--just some arbitrary strings (from its POV). When you call `ascii.write()` in your example you don't do anything with the "units" either. I don't know if it's necessary, but you can also define a custom reader/writer as described here: http://docs.astropy.org/en/stable/io/ascii/read.html#advanced-customization – Iguananaut Mar 29 '16 at 08:28

2 Answers2

4

There doesn't appear to be a straightforward way to write out the units of a column in a generic way using astropy.table or astropy.io.ascii. You may want to raise an issue at https://github.com/astropy/astropy/issues with a feature request.

However, there is a pretty simple workaround using the format ascii.ipac:

tbl.write('test.txt', format='ascii.ipac')
with open('test.txt', 'r') as fh:
    output = []
    for ii, line in enumerate(fh):
        if ii not in (1,3):
            output.append(line)

with open('test.txt', 'w') as fh:
    fh.writelines(output)

which will write out in the IPAC format, then remove the 2nd and 4th lines.

keflavich
  • 18,278
  • 20
  • 86
  • 118
  • Yep, that's what I was thinking of... writhe the file and then repent and edit it... should also work somehow with the other formats like the 'fixed_width' I will raise the issue Thanks – Emberck Mar 28 '16 at 15:27
1

Unless your table absolute has to be in that format, if you want an ASCII table with more complex metadata for the columns please consider using the ECSV format.

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
  • I've tried `ECSV` format and isn't a good fit for my needs, at the end I think I will use `IPAC` with ' | ' as delimiter... thats close enough and also easy to be read by people, Python, IDL an VO... In any case thanks for the answer – Emberck Mar 29 '16 at 09:36