Skip to content

Save Units of columns into files with ascii.write() #4743

Closed

Description

@Emberck

I've noticed that there's no straightforward way to write out the units of a column in a generic way using astropy.table or astropy.io.ascii.

When you print the table on the terminal the Units appear correctly under the col-name but there's no way to save them into a file.

Similar to #4112

Activity

  1. pllim commented on Mar 28, 2016

    @pllim
    Member

    What if you use my_table.write(my_filename, format='ipac')? You should get something like this:

    |wavelength|                  flux|
    |    double|                double|
    |    micron|erg / (Angstrom cm2 s)|
    |      null|                  null|
         0.0805      2.91125425657e-17 
         0.0815      3.05136210514e-17 
         0.0825      3.22549746131e-17 
         0.0835      2.78810781295e-17 
         ...              ...
    
  2. Emberck commented on Mar 28, 2016

    @Emberck
    Author

    Yes but that only work if you need an IPAC table, not with any of the other formats available and for example the 'fixed_width' (in any of it's 3 forms) won't work...
    So you are unable to get something like this:

             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 | ...
    

    and sadly the IPAC format isn't always the best format, that's why I want to make this Feature Request.

  3. taldcroft commented on Mar 28, 2016

    @taldcroft
    Member

    The ecsv format is the recommended generic way to write out to an ASCII file with units and all other metadata encapsulated. That format will round-trip and is readable by most generic CSV readers.

  4. pllim commented on Mar 28, 2016

    @pllim
    Member

    @Emberck , does ecsv as suggested by @taldcroft work for you? IMHO writing out units in fixed width format is not always desirable as some unit names can be a lot longer than your data.

  5. Emberck commented on Mar 28, 2016

    @Emberck
    Author

    I will try to play a little with ecsv and tell you how it went
    And @pllim I'm basically are with you but it's a request from the Boss...
    Thanks

  6. taldcroft commented on Mar 28, 2016

    @taldcroft
    Member

    See also #2869.

    @Emberck - note that the example you show is not including "units", but rather some other metadata which is more like "description". In astropy, "unit" has a specific meaning as a physical unit.

  7. Emberck commented on Mar 28, 2016

    @Emberck
    Author

    @taldcroft well in my example you can see that I have a mix of metadata and physical units (such as Hours for the coordinates) and in the other cols (in this case it's a very long table) I have mags , seconds and deg which are included in astropy.units and have actual physical meaning.

  8. taldcroft commented on Mar 28, 2016

    @taldcroft
    Member

    Anyway, here is a workaround that is reasonably close to your desired output:

    In [5]: t
    Out[5]: 
    <Table length=3>
      a      b     c  
    int64 float64 str1
    ----- ------- ----
        1     1.0    c
        2     2.0    d
        3     3.0    e
    
    In [6]: t['a'].unit = 'whatever'
    
    In [7]: t['b'].unit = 'seconds'
    
    In [8]: t['c'].unit = 'HH:MM:DDD'
    
    In [9]: lines = t.pformat(max_width=-1, max_lines=-1, show_dtype=False)
    
    In [11]: with open('myfile.dat', 'w') as fh:
       ....:     fh.write('\n'.join(lines))
    
    In [12]: cat myfile.dat
       a        b        c    
    whatever seconds HH:MM:DDD
    -------- ------- ---------
           1     1.0         c
           2     2.0         d
           3     3.0         e
    
  9. Emberck commented on Mar 28, 2016

    @Emberck
    Author

    Thanks @taldcroft
    Yep that should work, now I'm doing something similar and there's also an option with an ipac table after saving the table you remove the null and Data Types rows...

    In any case I think that would be nice to have the option of adding the 'Units row' within the ascii.write the same way that you can add the 'Names row' without workarounds...

    That's why I send this Feature Request, and since write already save the row if the ipac format is used I guess it should't be much trouble...

    Again thanks

    P.D.: I'm been playing around with the ecsv output but so far I don't think it would work for me... still I will keep trying for a while

  10. taldcroft commented on Mar 28, 2016

    @taldcroft
    Member

    Do you have a requirement to be able to read such files back?

  11. Emberck commented on Mar 28, 2016

    @Emberck
    Author

    Yes, I need to be able to read the files in Python, IDL and by a human that's why I'm having such troubles finding just the right format

  12. keflavich commented on Nov 24, 2019

    @keflavich
    Contributor

    This is a duplicate of #756. afaict, it's exactly the same. So I suppose we're now at the timescale of looking at this issue approximately once every three years.

  13. taldcroft commented on Nov 25, 2019

    @taldcroft
    Member

    Closing this as a duplicate of #756.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions