You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Save Units of columns into files with ascii.write() #4743
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:
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.
@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.
@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.
@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.
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
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
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.
Activity
pllim commentedon Mar 28, 2016
What if you use
my_table.write(my_filename, format='ipac')? You should get something like this:Emberck commentedon Mar 28, 2016
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:
and sadly the IPAC format isn't always the best format, that's why I want to make this Feature Request.
taldcroft commentedon Mar 28, 2016
The
ecsvformat 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.pllim commentedon Mar 28, 2016
@Emberck , does
ecsvas 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.Emberck commentedon Mar 28, 2016
I will try to play a little with
ecsvand tell you how it wentAnd @pllim I'm basically are with you but it's a request from the Boss...
Thanks
taldcroft commentedon Mar 28, 2016
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.
Emberck commentedon Mar 28, 2016
@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,secondsanddegwhich are included inastropy.unitsand have actual physical meaning.taldcroft commentedon Mar 28, 2016
Anyway, here is a workaround that is reasonably close to your desired output:
Emberck commentedon Mar 28, 2016
Thanks @taldcroft
Yep that should work, now I'm doing something similar and there's also an option with an
ipactable after saving the table you remove thenullandData Typesrows...In any case I think that would be nice to have the option of adding the 'Units row' within the
ascii.writethe same way that you can add the 'Names row' without workarounds...That's why I send this Feature Request, and since
writealready save the row if theipacformat is used I guess it should't be much trouble...Again thanks
P.D.: I'm been playing around with the
ecsvoutput but so far I don't think it would work for me... still I will keep trying for a whiletaldcroft commentedon Mar 28, 2016
Do you have a requirement to be able to read such files back?
Emberck commentedon Mar 28, 2016
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
keflavich commentedon Nov 24, 2019
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.
taldcroft commentedon Nov 25, 2019
Closing this as a duplicate of #756.