astropy/astropy — pull request #2255 by taldcroft (2014-05-01 19:48:41+00:00)
Title: Add a way to control how a masked entry appears when writing an ascii table

Right now, if I do this:
```
t=table.Table()
m=table.MaskedColumn(name='col1',data=randn(2))
m.mask[0]=True
t.add_column(m)
m=table.MaskedColumn(name='col2',data=randn(2))
m.mask[1]=True
t.add_column(m)
t.write('table.csv',format='ascii', delimiter=',')
```
The resulting file looks like this:
```
col1,col2
--,0.146024400013
0.527542111907,--
```

The problem is the placeholder for masked entries: ``--``. That perhaps makes sense for space-separated files, but for any other delimiter, it would make more sense to use the empty string. This is a more common way of representing missing data, yielding:
```
col1,col2
,0.146024400013
0.527542111907,
```
This has the added advantage of working correctly if you do ``ascii.read('table.csv')`` - in the original version it parses all the columns as unmasked strings, but in this version in correctly generates masked columns, and also gets the dtypes right.

The implementation turns out to be fairly striaghtforward - ``np.ma`` provides a way to set the placeholder string: `np.ma.masked_print_option.set_display`. So it would be straightforward to add something like this to the ascii table writers:
```
...
old_print_option_str = np.ma.masked_print_option.display()
try:
#`masked_string` would be a new keyword for ascii.write
np.ma.masked_print_option.set_display(masked_string)
... the operations necessary to write the file ...
finally:
np.ma.masked_print_option.set_display(old_print_option_str)
...
```

But there are two things I'm not sure about:
* What's the best way to do this so that *all* of the `io.ascii` writers get this behavior?
* Should the default be to automatically do this if the delimiter is not the empty string? I think that actually makes more sense, because that way masked tables round-trip, but without this, they do not.