|
39 | 39 | FAST_CLASSES = {} |
40 | 40 |
|
41 | 41 |
|
| 42 | +def _check_multidim_table(table, max_ndim=1): |
| 43 | + """Check that ``table`` has only columns with ndim <= ``max_ndim`` |
| 44 | +
|
| 45 | + Currently ECSV is the only built-in format that supports output of arbitrary |
| 46 | + N-d columns, but HTML supports 2-d. |
| 47 | + """ |
| 48 | + # Check for N-d columns |
| 49 | + nd_names = [col.info.name for col in table.itercols() if len(col.shape) > max_ndim] |
| 50 | + if nd_names: |
| 51 | + raise ValueError(f'column(s) {",".join(nd_names)} with dimension > {max_ndim} ' |
| 52 | + "cannot be be written with this format, try using 'ecsv' " |
| 53 | + "(Enhanced CSV) format") |
| 54 | + |
| 55 | + |
42 | 56 | class CsvWriter: |
43 | 57 | """ |
44 | 58 | Internal class to replace the csv writer ``writerow`` and ``writerows`` |
@@ -1221,6 +1235,20 @@ def __init__(self): |
1221 | 1235 | self.meta = OrderedDict(table=OrderedDict(), |
1222 | 1236 | cols=OrderedDict()) |
1223 | 1237 |
|
| 1238 | + def _check_multidim_table(self, table, max_ndim=1): |
| 1239 | + """Check that ``table`` has only 1-d columns. |
| 1240 | +
|
| 1241 | + If a reader class supports N-d columns then override this method. |
| 1242 | +
|
| 1243 | + Parameters |
| 1244 | + ---------- |
| 1245 | + table : `~astropy.table.Table` |
| 1246 | + Input table. |
| 1247 | + max_ndim : int |
| 1248 | + Max allowed number of dimensions (default=1) |
| 1249 | + """ |
| 1250 | + _check_multidim_table(table, max_ndim) |
| 1251 | + |
1224 | 1252 | def read(self, table): |
1225 | 1253 | """Read the ``table`` and return the results in a format determined by |
1226 | 1254 | the ``outputter`` attribute. |
@@ -1409,6 +1437,10 @@ def write(self, table): |
1409 | 1437 | # only used by ECSV and is otherwise just a pass-through. |
1410 | 1438 | table = self.update_table_data(table) |
1411 | 1439 |
|
| 1440 | + # Check that table has only 1-d columns. Classes that support output of |
| 1441 | + # N-d columns can override this method. |
| 1442 | + self._check_multidim_table(table) |
| 1443 | + |
1412 | 1444 | # Now use altered columns |
1413 | 1445 | new_cols = list(table.columns.values()) |
1414 | 1446 | # link information about the columns to the writer object (i.e. self) |
|
0 commit comments