Skip to content

Commit

Permalink
Merge pull request #461 from AstarVienna/hb/addunitstodatacontainertable
Browse files Browse the repository at this point in the history
Add units to DataContainer table directly.
  • Loading branch information
teutoburg committed Aug 28, 2024
2 parents d3f6679 + f29502a commit 94bc07d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
32 changes: 32 additions & 0 deletions scopesim/effects/data_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,38 @@ def __init__(self, filename=None, table=None, array_dict=None, cmds=None,
if array_dict is not None:
self._from_arrays(array_dict)

# Get units as they are given in the kworgs.
unit_dict_kwargs = {
k: v
for k, v in kwargs.items()
if k.endswith("_unit")
}

# Collect the units from the table. self.meta should contain both
# kwargs and headers from the file at this point.
unit_dict = {
k: v
for k, v in self.meta.items()
if k.endswith("_unit")
}

# Verify that any units given in the kwargs
# are the same as those given in the table.
for k, v_kwargs in unit_dict_kwargs.items():
v_table = unit_dict.get(k, v_kwargs)
if v_kwargs != v_table:
raise ValueError(f"Column {k} has unit {v_table} in table, but {v_kwargs} in kwargs")

# Add units from kwargs if they are given.
if self.table:
for column in self.table.columns.values():
key_unit = f"{column.name}_unit"
if key_unit not in unit_dict:
continue
unit_kwargs = unit_dict[key_unit]
if column.unit is None:
column.unit = unit_kwargs

def _from_table(self, table):
self.table = table
self.headers += [table.meta]
Expand Down
19 changes: 12 additions & 7 deletions scopesim/effects/detector_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,15 @@ def __init__(self, pixel_size, x, y, width, height=None, angle=0, gain=1,
"image_plane_id": 0}
params.update(kwargs)

tbl = Table(data=[[0], [x], [y], [width], [height],
[angle], [gain], [pixel_size]],
names=["id", "x_cen", "y_cen", "x_size", "y_size",
"angle", "gain", "pixel_size"])
tbl.meta.update(params)

super().__init__(table=tbl, **params)
array_dict = {
"id": [0],
"x_cen": [x],
"y_cen": [y],
"x_size": [width],
"y_size": [height],
"angle": [angle],
"gain": [gain],
"pixel_size": [pixel_size],
}

super().__init__(array_dict=array_dict, **params)
2 changes: 1 addition & 1 deletion scopesim/effects/ter_curves_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def download_svo_filter(filter_name, return_style="synphot"):
if return_style == "synphot":
return SpectralElement(Empirical1D, points=wave, lookup_table=trans)
if return_style == "table":
filt = QTable(data=[wave, trans], names=["wavelength", "transmission"])
filt = Table(data=[wave, trans], names=["wavelength", "transmission"])
filt.meta["wavelength_unit"] = str(wave.unit)
filt.meta["votable_meta"] = tbl_meta # Don't pollute actual meta...
return filt
Expand Down
10 changes: 10 additions & 0 deletions scopesim/tests/tests_optics/test_DataContainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ def test_initialised_with_ascii_input(self, data_files):
dat = DataContainer(data_files[1])
assert isinstance(dat, DataContainer)
assert dat.is_fits is False
assert dat.table.colnames == ['wavelength', 'transmission']
column = dat.table['wavelength']
assert column.unit == "um"

def test_raises_wrong_units(self, data_files):
with pytest.raises(ValueError):
_ = DataContainer(
data_files[1],
wavelength_unit="m",
)

def test_initialised_with_arrays_dict_input(self):
array_dict = {"wavelength": np.linspace(1, 2, 11)*u.um,
Expand Down

0 comments on commit 94bc07d

Please sign in to comment.