Skip to content

Commit

Permalink
Merge pull request #65 from sergiopasra/cbase_class_inheritance
Browse files Browse the repository at this point in the history
Cbase class inheritance
  • Loading branch information
sergiopasra committed Nov 20, 2015
2 parents 0e90377 + 2130091 commit 34755e3
Show file tree
Hide file tree
Showing 33 changed files with 864 additions and 856 deletions.
3 changes: 3 additions & 0 deletions megaradrp/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

from .recipe import MegaraBaseRecipe
from .processing import apextract, apextract_tracemap
91 changes: 8 additions & 83 deletions megaradrp/core.py → megaradrp/core/processing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2011-2014 Universidad Complutense de Madrid
# Copyright 2011-2015 Universidad Complutense de Madrid
#
# This file is part of Megara DRP
#
Expand All @@ -22,7 +22,6 @@
from astropy.io import fits
import numpy as np

from numina.core import BaseRecipeAutoQC as MegaraBaseRecipe
from numina.array.trace.extract import extract_simple_rss
from numina.array.utils import wc_to_pix_1d as wcs_to_pix_1d

Expand All @@ -31,90 +30,16 @@
_direc = ['normal', 'mirror']


def create(image, direction='normal', bins='11'):
'''Create a image with overscan for testing.'''

if direction not in _direc:
raise ValueError("%s must be either 'normal' or 'mirror'" % direction)

if direction == 'normal':
direcfun = lambda x: x
else:
direcfun = np.fliplr

if bins not in _binning:
raise ValueError("%s must be one if '11', '12', '21, '22'" % bins)

bng = _binning[bins]

nr = 2056 / bng[0]
nc = 2048 / bng[1]

nr2 = 2 * nr
nc2 = 2 * nc

oscan1 = 50 / bng[0]
oscan2 = oscan1 * 2

psc1 = 50 / bng[0]
psc2 = 2 * psc1

fshape = (nr2 + oscan2, nc2 + psc2)

# Row block 1
rb1 = slice(0, nr)
rb1m = slice(nr, nr + oscan1)
# Row block 2
rb2 = slice(nr + oscan2, nr2 + oscan2)
rb2m = slice(nr + oscan1, nr + oscan2)
# Col block
cb = slice(psc1, nc2 + psc1)
# Col block left
cbl = slice(0, psc1)
# Col block right
cbr = slice(nc2 + psc1, nc2 + psc2)

# Mode normal
trim1 = (rb1, cb)
pcol1 = (rb1, cbl)
ocol1 = (rb1, cbr)
orow1 = (rb1m, cb)
print(trim1, ocol1, orow1, pcol1)

trim2 = (rb2, cb)
pcol2 = (rb2, cbr)
ocol2 = (rb2, cbl)
orow2 = (rb2m, cb)
print(trim2, ocol2, orow2, pcol2)

finaldata = np.zeros(fshape, dtype='float32')

finaldata[trim1] = direcfun(np.atleast_2d(np.arange(0, nc2)))
finaldata[trim2] = direcfun(np.atleast_2d(np.arange(0, nc2)))

finaldata[orow1] = 3
finaldata[orow2] = 4

finaldata[pcol1] = 5
finaldata[pcol2] = 6

finaldata[ocol1] = 7
finaldata[ocol2] = 8

hdu = fits.PrimaryHDU(data=finaldata)
hdu.writeto(image, clobber=True)


def trim_and_o(image, out='trimmed.fits', direction='normal', bins='11'):
'''Trim a MEGARA image with overscan.'''
"""Trim a MEGARA image with overscan."""

with fits.open(image) as hdul:
hdu = trim_and_o_hdu(hdul[0])
hdu.writeto(out, clobber=True)


def trim_and_o_hdu(hdu):
'''Trim a MEGARA HDU with overscan.'''
"""Trim a MEGARA HDU with overscan."""

# FIXME: this should come from the header
direction = 'normal'
Expand All @@ -127,7 +52,7 @@ def trim_and_o_hdu(hdu):


def trim_and_o_array(array, direction='normal', bins='11'):
'''Trim a MEGARA array with overscan.'''
"""Trim a MEGARA array with overscan."""

if direction not in _direc:
raise ValueError("%s must be either 'normal' or 'mirror'" % direction)
Expand Down Expand Up @@ -160,8 +85,9 @@ def trim_and_o_array(array, direction='normal', bins='11'):
finaldata[nr:, :] = direcfun(array[nr + oscan2:, psc1:nc2 + psc1])
return finaldata


def apextract(data, trace):
'''Extract apertures.'''
"""Extract apertures."""
rss = np.empty((trace.shape[0], data.shape[1]), dtype='float32')
for idx, r in enumerate(trace):
l = r[0]
Expand Down Expand Up @@ -191,15 +117,15 @@ def extract_region(data, border1, border2, pesos, xpos):
for x, a,b in zip(xpos, border1, border2):
fill_other(pesos[:,x], a, b)

final2d = data[region,:] * pesos[region,:]
final2d = data[region,:] * pesos[region,:]

pesos[region,:] = 0.0
final = final2d.sum(axis=0)
return final


def apextract_tracemap(data, tracemap):
'''Extract apertures using a tracemap.'''
"""Extract apertures using a tracemap."""

# FIXME: a little hackish

Expand Down Expand Up @@ -227,7 +153,6 @@ def apextract_tracemap(data, tracemap):

rss = extract_simple_rss(data, borders)


return rss


103 changes: 103 additions & 0 deletions megaradrp/core/recipe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#
# Copyright 2011-2015 Universidad Complutense de Madrid
#
# This file is part of Megara DRP
#
# Megara DRP is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Megara DRP is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Megara DRP. If not, see <http://www.gnu.org/licenses/>.
#

import logging

from astropy.io import fits

import numina.array.combine as combine
from numina.flow import SerialFlow
from numina.flow.processing import BiasCorrector
from numina.core import BaseRecipe
from numina.core.dataholders import Product
from numina.core.products import QualityControlProduct

from megaradrp.processing import OverscanCorrector, TrimImage

_logger = logging.getLogger('numina.recipes.megara')

class MegaraBaseRecipe(BaseRecipe):
"""Base clase for all MEGARA Recipes"""

qc = Product(QualityControlProduct, dest='qc')

def __init__(self, version):
self.__flow = {'BiasRecipe':[OverscanCorrector, TrimImage],
'ArcCalibrationRecipe':[OverscanCorrector, TrimImage, BiasCorrector],
'FiberFlatRecipe':[OverscanCorrector, TrimImage, BiasCorrector],
'TraceMapRecipe':[OverscanCorrector, TrimImage, BiasCorrector],
}
super(MegaraBaseRecipe, self).__init__(version=version)

def __generate_flow(self, params):
flow = self.__flow[self.__class__.__name__]
try:
for cont in range(len(flow)):
if issubclass(BiasCorrector, flow[cont]):
flow[cont] = (flow[cont](params['biasmap']))
elif issubclass(TrimImage, flow[cont]) or issubclass(OverscanCorrector, flow[cont]):
flow[cont] = (flow[cont]())
basicflow = SerialFlow(flow)

except Exception as e:
_logger.error(e)
raise(e)

return basicflow

def bias_process_common(self, obresult, master_bias):

with master_bias.open() as hdul:
mbias = hdul[0].data.copy()

hdu, data = self.hdu_creation(obresult, {'biasmap':mbias})

hdr = hdu.header
# FIXME: this is incorrect in general
hdr['IMGTYP'] = ('FIBER_FLAT', 'Image type')
hdr['NUMTYP'] = ('MASTER_FIBER_FLAT', 'Data product type')
hdr = self.set_base_headers(hdr)
hdr['CCDMEAN'] = data[0].mean()

varhdu = fits.ImageHDU(data[1], name='VARIANCE')
num = fits.ImageHDU(data[2], name='MAP')
reduced = fits.HDUList([hdu, varhdu, num])
return reduced

def hdu_creation(self, obresult, params=None):

basicflow = self.__generate_flow(params)

cdata = []
try:
for frame in obresult.images:
hdulist = frame.open()
hdulist = basicflow(hdulist)
cdata.append(hdulist)

_logger.info('stacking %d images using median', len(cdata))

data = combine.median([d[0].data for d in cdata], dtype='float32')
template_header = cdata[0][0].header
hdu = fits.PrimaryHDU(data[0], header=template_header)
finally:
for hdulist in cdata:
hdulist.close()

return hdu, data
Empty file.
12 changes: 12 additions & 0 deletions megaradrp/core/tests/test_recipe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@


from numina.core import BaseRecipe

from megaradrp.core.recipe import MegaraBaseRecipe


def test_base_recipe():
version = "1.0.1"
obj = MegaraBaseRecipe(version)
assert isinstance(obj, BaseRecipe)
assert obj.__version__ == version
14 changes: 5 additions & 9 deletions megaradrp/drp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,17 @@ modes:
tagger: megaradrp.taggers.tagger_vph
pipelines:
default:
version: 1
recipes:
bias_image: megaradrp.recipes.calibration.bias.BiasRecipe
dark_image: megaradrp.recipes.DarkRecipe
fiber_flat_image: megaradrp.recipes.FiberFlatRecipe
dark_image: megaradrp.recipes.calibration.dark.DarkRecipe
fiber_flat_image: megaradrp.recipes.calibration.flat.FiberFlatRecipe
mos_image: megaradrp.recipes.scientific.FiberMOSRecipe2
flux_calibration: megaradrp.recipes.calibration.PseudoFluxCalibrationRecipe
trace_map: megaradrp.recipes.calibration.flat.TraceMapRecipe
flux_calibration: megaradrp.recipes.calibration.fluxcal.PseudoFluxCalibrationRecipe
trace_map: megaradrp.recipes.calibration.trace.TraceMapRecipe
arc_calibration: megaradrp.recipes.calibration.arc.ArcCalibrationRecipe
fail: numina.core.utils.AlwaysFailRecipe
success: numina.core.utils.AlwaysSuccessRecipe
version: 1
experimental:
recipes:
fiber_flat_image: megaradrp.recipes.calibration.flat.FiberFlatRecipe
version: 1
products:
- name: megaradrp.products.TraceMap
alias: TraceMap
Expand Down
13 changes: 4 additions & 9 deletions megaradrp/loader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2014 Universidad Complutense de Madrid
# Copyright 2014-2015 Universidad Complutense de Madrid
#
# This file is part of Megara DRP
#
Expand All @@ -17,17 +17,12 @@
# along with Megara DRP. If not, see <http://www.gnu.org/licenses/>.
#

'''Load MEGARA DRP'''
"""Load MEGARA DRP"""

from numina.core import drp_load


def megara_drp_load():
'''Entry point to load MEGARA DRP.'''
def load_drp():
"""Entry point to load MEGARA DRP."""
return drp_load('megaradrp', 'drp.yaml')


def load_cli_storage():
'''Entry point to load storage functions for the CLI.'''
import megaradrp.store
return 0
9 changes: 3 additions & 6 deletions megaradrp/processing/aperture.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@

import logging


from numina.flow.processing import TagOptionalCorrector, TagFits

from ..core import apextract, apextract_tracemap
from megaradrp.core.processing import apextract, apextract_tracemap


_logger = logging.getLogger('numina.processing')


class ApertureExtractor(TagOptionalCorrector):

'''A Node that extracts apertures.'''
"""A Node that extracts apertures."""

def __init__(self, trace, datamodel=None, mark=True,
tagger=None, dtype='float32'):
Expand All @@ -54,8 +52,7 @@ def _run(self, img):


class ApertureExtractor2(TagOptionalCorrector):

'''A Node that extracts apertures.'''
"""A Node that extracts apertures."""

def __init__(self, trace, datamodel=None, mark=True,
tagger=None, dtype='float32'):
Expand Down
2 changes: 1 addition & 1 deletion megaradrp/processing/trimover.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from numina.flow.processing import TagOptionalCorrector, TagFits

from ..core import trim_and_o_hdu
from ..core.processing import trim_and_o_hdu

_logger = logging.getLogger('megara.processing')

Expand Down

0 comments on commit 34755e3

Please sign in to comment.