Skip to content

Commit

Permalink
Create functions to convert fibers and bundles to a table (fixes #199)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiopasra committed Mar 11, 2018
1 parent 0cc8173 commit 715ce91
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 24 deletions.
50 changes: 47 additions & 3 deletions megaradrp/datamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import enum

import astropy.io.fits as fits
import astropy.table
from six import StringIO
from numina.datamodel import DataModel
from numina.util.convert import convert_date
Expand Down Expand Up @@ -147,6 +148,7 @@ def fiber_scale_unit(self, img, unit=False):
else:
return scale


class FibersConf(object):
"""Global configuration of the fibers"""
def __init__(self):
Expand All @@ -156,6 +158,7 @@ def __init__(self):
self.nfibers = 0
self.bundles = {}
self.fibers = {}
self.funit = "mm"

def sky_fibers(self, valid_only=False, ignored_bundles=None):
result = []
Expand Down Expand Up @@ -234,6 +237,37 @@ def spectral_coverage(self):
nx = max(upperc)
return (mn, mx), (nn, nx)

def bundles_to_table(self):
"""Convert bundles to a Table"""
attrnames = ['id', 'x', 'y', 'pa', 'enabled',
'target_type', 'target_priority', 'target_name']
cnames = ['bundle_id', 'x', 'y', 'pa', 'enabled',
'target_type', 'target_priority', 'target_name']
obj_data = {}
for a, c in zip(attrnames, cnames):
obj_data[c] = [getattr(ob, a) for ob in self.bundles.values()]
result = astropy.table.Table(obj_data, names=cnames)
result['x'].unit = self.funit
result['y'].unit = self.funit
result['pa'].unit = 'deg'
return result

def fibers_to_table(self):
"""Convert fibers to a Table"""
attrnames = ['fibid', 'name', 'x', 'y', 'inactive', 'valid',
'bundle_id']
cnames = ['fibid', 'name', 'x', 'y', 'inactive', 'valid',
'bundle_id']
obj_data = {}

for a, c in zip(attrnames, cnames):
obj_data[c] = [getattr(ob, a) for ob in self.fibers.values()]
result = astropy.table.Table(obj_data, names=cnames)
result['x'].unit = self.funit
result['y'].unit = self.funit
return result



class TargetType(enum.Enum):
"""Possible targest in a fiber bundle"""
Expand All @@ -260,14 +294,19 @@ def __init__(self):
self.x = 0
self.y = 0
self.pa = 0
self.enabled = True


class FiberConf(object):
"""Description of the fiber"""
def __init__(self):
self.fibid = 0
self.name = 'unknown'
self.bundle_id = None
self.inactive = False
self.valid = True
self.x = 0.0
self.y = 0.0


def read_fibers_extension(hdr, insmode='LCB'):
Expand All @@ -288,7 +327,7 @@ def read_fibers_extension(hdr, insmode='LCB'):
"""
conf = FibersConf()
defaults = {}
defaults['LCB'] = (89, 623)
defaults['LCB'] = (9, 623)
defaults['MOS'] = (92, 644)

if insmode not in ['LCB', 'MOS']:
Expand All @@ -298,6 +337,7 @@ def read_fibers_extension(hdr, insmode='LCB'):
conf.conf_id = hdr.get('CONFID', 1)
conf.nbundles = hdr.get('NBUNDLES', defaults[insmode][0])
conf.nfibers = hdr.get('NFIBERS', defaults[insmode][1])
conf.funit = funit = hdr.get("FUNIT", "arcsec")
# Read bundles

bun_ids = []
Expand All @@ -324,6 +364,10 @@ def read_fibers_extension(hdr, insmode='LCB'):
bb.target_priority = hdr["BUN%03d_P" % i]
bb.target_name = hdr["BUN%03d_I" % i]
bb.target_type = TargetType[hdr["BUN%03d_T" % i]]
bb.enabled = hdr.get("BUN%03d_E" % i, True)
bb.x = hdr.get("BUN%03d_X" % i, 0.0)
bb.y = hdr.get("BUN%03d_Y" % i, 0.0)
bb.pa = hdr.get("BUN%03d_O" % i, 0.0)
bb.fibers = {}
bundles[i] = bb

Expand All @@ -342,7 +386,7 @@ def read_fibers_extension(hdr, insmode='LCB'):
ff.x = hdr["FIB%03d_X" % fibid]
ff.y = hdr["FIB%03d_Y" % fibid]

ff.b = hdr["FIB%03d_B" % fibid]
ff.bundle_id = hdr["FIB%03d_B" % fibid]
ff.name = hdr.get("FIB%03d_N" % fibid, 'unknown')

ff.w1 = hdr.get("FIB%03dW1" % fibid, None)
Expand All @@ -354,7 +398,7 @@ def read_fibers_extension(hdr, insmode='LCB'):
else:
ff.valid = hdr.get("FIB%03d_V" % fibid, True)

bundles[ff.b].fibers[ff.fibid] = ff
bundles[ff.bundle_id].fibers[ff.fibid] = ff
fibers[ff.fibid] = ff

return conf
2 changes: 1 addition & 1 deletion megaradrp/instrument/configs/lcb_default_header.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion megaradrp/instrument/configs/mos_default_header.txt

Large diffs are not rendered by default.

65 changes: 46 additions & 19 deletions megaradrp/tests/test_datamodel.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#
# Copyright 2015-2017 Universidad Complutense de Madrid
# Copyright 2015-2018 Universidad Complutense de Madrid
#
# This file is part of Megara DRP
#
# SPDX-License-Identifier: GPL-3.0+
# License-Filename: LICENSE.txt
#


import astropy.io.fits as fits
import astropy.table
import pytest

from ..datamodel import MegaraDataModel, FibersConf
Expand All @@ -19,43 +21,68 @@ def create_empty_img(insmode):
return img


def test_fiberconf_LCB():
BASE_LCB = ("LCB", 'b7dcd9d1-0b60-4b43-b26e-d2c9868d5e20', 9, 623)
BASE_MOS = ("MOS", '00000000-0000-0000-0000-000000000000', 92, 644)


@pytest.mark.parametrize("name, confid, nbundles, nfibers",
[BASE_LCB, BASE_MOS])
def test_fiberconf_1(name, confid, nbundles, nfibers):

datamodel = MegaraDataModel()

img = create_empty_img('LCB')
img = create_empty_img(name)

conf = datamodel.get_fiberconf(img)

assert isinstance(conf, FibersConf)
# Default values from file
assert conf.name == 'LCB'
assert conf.conf_id == 1
assert conf.nbundles == 89
assert conf.nfibers == 623
assert conf.name == name
assert conf.conf_id == confid
assert conf.nbundles == nbundles
assert conf.nfibers == nfibers


def test_fiberconf_MOS():
def test_fiberconf_other():

datamodel = MegaraDataModel()

img = create_empty_img('MOS')
img = create_empty_img('OTHER')

with pytest.raises(ValueError):
datamodel.get_fiberconf(img)


@pytest.mark.parametrize("name, confid, nbundles, nfibers",
[BASE_LCB, BASE_MOS])
def test_bundles_to_table(name, confid, nbundles, nfibers):

datamodel = MegaraDataModel()

img = create_empty_img(name)

conf = datamodel.get_fiberconf(img)

assert isinstance(conf, FibersConf)
# Default values from file
assert conf.name == 'MOS'
assert conf.conf_id == 1
assert conf.nbundles == 92
assert conf.nfibers == 644
bundles_t = conf.bundles_to_table()
assert isinstance(bundles_t, astropy.table.Table)
assert len(bundles_t) == nbundles
assert bundles_t.colnames == ['bundle_id', 'x', 'y', 'pa', 'enabled',
'target_type', 'target_priority', 'target_name']


def test_fiberconf_other():
@pytest.mark.parametrize("name, confid, nbundles, nfibers",
[BASE_LCB, BASE_MOS])
def test_fibers_to_table(name, confid, nbundles, nfibers):

datamodel = MegaraDataModel()

img = create_empty_img('OTHER')
img = create_empty_img(name)

with pytest.raises(ValueError):
datamodel.get_fiberconf(img)
conf = datamodel.get_fiberconf(img)

fibers_t = conf.fibers_to_table()
assert isinstance(fibers_t, astropy.table.Table)
assert len(fibers_t) == nfibers
assert fibers_t.colnames == ['fibid', 'name', 'x', 'y',
'inactive', 'valid',
'bundle_id']

0 comments on commit 715ce91

Please sign in to comment.