Skip to content

Commit

Permalink
Fix and test formatting of values in AAVSO columns
Browse files Browse the repository at this point in the history
  • Loading branch information
mwcraig committed Sep 29, 2023
1 parent aa10768 commit f250169
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
19 changes: 18 additions & 1 deletion stellarphot/io/aavso.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,25 @@ def to_table(self):
length_limit = v.get('limit', None)
if length_limit:
if v['type'] == 'float':
table[k].info.format = f"{length_limit}f"
# For float, the width in the format specifier is the _minimum_
# width of the field, not the maximum. Since, most of the time,
# any width issues will arise because some rounding is needed,
# use the width to calculate the place to which to round the
# number, EXCEPT in the case of time. There, use all the digits and
# hope for the best.
if k != 'DATE':
max_precision = length_limit - 3
precision = min(max_precision, 6)
else:
precision = 8

# Check whether table column is all "na" -- if that is the case, don't
# set a format.
if not all(table[k] == "na"):
table[k].info.format = f"{length_limit}.{precision}f"
elif v['type'] == 'str':
# For a string, the "precision" in the format is the maximum
# size of the field.
if k == 'STARID':
print(table[k])
table[k].info.format = f".{length_limit}s"
Expand Down
4 changes: 3 additions & 1 deletion stellarphot/io/tests/test_aavso.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ def test_making_table_ensemble():
star_id=26)


def test_table_column_formats(tmp_path):
@pytest.mark.parametrize("ensemble", [True, False])
def test_table_column_formats(tmp_path, ensemble):
aef, input_data = set_up_aef()
aef.ensemble = ensemble
aef_table = aef.to_table()
aef_table.write(tmp_path / "test.csv")
with open(tmp_path / "test.csv") as f:
Expand Down

0 comments on commit f250169

Please sign in to comment.