Skip to content

Commit

Permalink
Merge pull request #62 from Pica4x6/traces_deletion
Browse files Browse the repository at this point in the history
Traces + Bias improvements
  • Loading branch information
sergiopasra committed Oct 13, 2015
2 parents 0e29690 + 0188ace commit 109e0d0
Show file tree
Hide file tree
Showing 18 changed files with 142 additions and 27,797 deletions.
2 changes: 1 addition & 1 deletion megaradrp/drp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ modes:
pipelines:
default:
recipes:
bias_image: megaradrp.recipes.BiasRecipe
bias_image: megaradrp.recipes.calibration.bias.BiasRecipe
dark_image: megaradrp.recipes.DarkRecipe
fiber_flat_image: megaradrp.recipes.FiberFlatRecipe
mos_image: megaradrp.recipes.scientific.FiberMOSRecipe2
Expand Down
4 changes: 2 additions & 2 deletions megaradrp/recipes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
# along with Megara DRP. If not, see <http://www.gnu.org/licenses/>.
#

from .calibration import BiasRecipe, DarkRecipe, FiberFlatRecipe
from .scientific import FiberMOSRecipe
from .calibration import DarkRecipe, FiberFlatRecipe
from .scientific import FiberMOSRecipe
5 changes: 2 additions & 3 deletions megaradrp/recipes/calibration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@

'''Calibration Recipes for Megara'''

from .base import BiasRecipe, DarkRecipe
from .base import DarkRecipe
from .base import PseudoFluxCalibrationRecipe
from .flat import TraceMapRecipe, TwilightFiberFlatRecipe
from .flat import FiberFlatRecipe
from .base import ArcRecipe
from .base import LCB_IFU_StdStarRecipe, FiberMOS_StdStarRecipe
from .base import SensitivityFromStdStarRecipe, S_And_E_FromStdStarsRecipe
from .base import BadPixelsMaskRecipe, LinearityTestRecipe
from .base import BadPixelsMaskRecipe, LinearityTestRecipe
1 change: 0 additions & 1 deletion megaradrp/recipes/calibration/arc.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
# FIXME: remove this later
from numina.core.products import LinesCatalog
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
from numina.array.wavecal.arccalibration import arccalibration_direct
from numina.array.wavecal.arccalibration import fit_solution
from numina.array.wavecal.arccalibration import gen_triplets_master
Expand Down
76 changes: 0 additions & 76 deletions megaradrp/recipes/calibration/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from numina.core import Product, DataProductRequirement, Requirement
from numina.core.products import ArrayType
from numina.core.requirements import ObservationResultRequirement
from numina.core import RecipeError
from numina.array.combine import median as c_median
from numina.flow import SerialFlow
from numina.flow.processing import BiasCorrector
Expand All @@ -50,63 +49,6 @@
_logger = logging.getLogger('numina.recipes.megara')


class BiasRecipe(MegaraBaseRecipe):
'''Process BIAS images and create MASTER_BIAS.'''

obresult = ObservationResultRequirement()

biasframe = Product(MasterBias)

def __init__(self):
super(BiasRecipe, self).__init__(
version="0.1.0"
)

def run(self, rinput):
return self.process(rinput.obresult)

def process(self, obresult):
_logger.info('starting bias reduction')

if not obresult.images:
raise RecipeError('Frame list is empty')

cdata = []

o_c = OverscanCorrector()
t_i = TrimImage()

basicflow = SerialFlow([o_c, t_i])

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 = c_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()

hdr = hdu.header
hdr = self.set_base_headers(hdr)
hdr['IMGTYP'] = ('BIAS', 'Image type')
hdr['NUMTYP'] = ('MASTER_BIAS', 'Data product type')
hdr['CCDMEAN'] = data[0].mean()

varhdu = fits.ImageHDU(data[1], name='VARIANCE')
num = fits.ImageHDU(data[2], name='MAP')
hdulist = fits.HDUList([hdu, varhdu, num])
_logger.info('bias reduction ended')

result = self.create_result(biasframe=hdu)
return result


class DarkRecipe(MegaraBaseRecipe):

Expand Down Expand Up @@ -240,24 +182,6 @@ def add_wcs(hdr):
return result


class ArcRecipe(MegaraBaseRecipe):

master_bias = MasterBiasRequirement()
obresult = ObservationResultRequirement()

fiberflat_frame = Product(MasterFiberFlat)
fiberflat_rss = Product(MasterFiberFlat)
traces = Product(ArrayType)

def __init__(self):
super(ArcRecipe, self).__init__(
version="0.1.0"
)

def run(self, rinput):
pass


class LCB_IFU_StdStarRecipe(MegaraBaseRecipe):

master_bias = MasterBiasRequirement()
Expand Down
67 changes: 67 additions & 0 deletions megaradrp/recipes/calibration/bias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import logging

from astropy.io import fits

from megaradrp.core import MegaraBaseRecipe
from megaradrp.processing import OverscanCorrector, TrimImage
from megaradrp.products import MasterBias

from numina.array.combine import median as c_median
from numina.core import Product, RecipeError
from numina.core.requirements import ObservationResultRequirement
from numina.flow import SerialFlow

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


class BiasRecipe(MegaraBaseRecipe):
'''Process BIAS images and create MASTER_BIAS.'''

obresult = ObservationResultRequirement()
biasframe = Product(MasterBias)

def __init__(self):
super(BiasRecipe, self).__init__(version="0.1.0")

def run(self, rinput):
return self.process(rinput.obresult)

def process(self, obresult):
_logger.info('starting bias reduction')

if not obresult.images:
raise RecipeError('Frame list is empty')

cdata = []
o_c = OverscanCorrector()
t_i = TrimImage()
basicflow = SerialFlow([o_c, t_i])

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 = c_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()

hdr = hdu.header
hdr = self.set_base_headers(hdr)
hdr['IMGTYP'] = ('BIAS', 'Image type')
hdr['NUMTYP'] = ('MASTER_BIAS', 'Data product type')
hdr['CCDMEAN'] = data[0].mean()

varhdu = fits.ImageHDU(data[1], name='VARIANCE')
num = fits.ImageHDU(data[2], name='MAP')
hdulist = fits.HDUList([hdu, varhdu, num])
_logger.info('bias reduction ended')

result = self.create_result(biasframe=hdu)
return result
3 changes: 1 addition & 2 deletions megaradrp/recipes/calibration/flat.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,12 @@ def run(self, rinput):
hs = 3
step1 = 2
background1 = 150.0
npred = 1
maxdis1 = 2.0

_logger.info('find peaks in column %i', cstart)

central_peaks = init_traces(data, center=cstart, hs=hs,
background=background1, npred=npred)
background=background1)

_logger.info(' %i peaks found', len(central_peaks))

Expand Down
65 changes: 65 additions & 0 deletions megaradrp/recipes/calibration/tests/test_bias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os

import pytest

from numina.tests.testcache import download_cache

from numina.core import import_object
from numina.core.pipeline import DrpSystem
from numina.core import ObservationResult
from numina.core import DataFrame
from megaradrp.recipes.calibration.bias import BiasRecipe
from megaradrp.loader import megara_drp_load

@pytest.mark.remote
def test_bias(drpmocker):

BASE_URL = 'http://guaix.fis.ucm.es/~spr/megara_test/BIAS/%s'
images = ['e99d2937d2c29a27c0ba4eebfcf7918e',
'e99d2937d2c29a27c0ba4eebfcf7918e',
'e99d2937d2c29a27c0ba4eebfcf7918e']

fs = [download_cache(BASE_URL % i) for i in images]

ob = ObservationResult()
ob.instrument = 'MEGARA'
ob.mode = 'bias_image'
ob.frames = [DataFrame(filename=f.name) for f in fs]

drpmocker.add_drp('MEGARA', megara_drp_load)

# Here we could directly import the required pipeline,
# but the idea is to test all the process
insdrp = DrpSystem().query_by_name(ob.instrument)
pipeline = insdrp.pipelines.get('default')
recipe_fqn = pipeline.recipes.get(ob.mode)
RecipeClass = import_object(recipe_fqn)

assert RecipeClass is BiasRecipe

# TODO: these should be created by a build_recipe_input method
recipe = BiasRecipe()
ri = recipe.create_input(obresult=ob)

result = recipe.run(ri)
# assert result.qc >= QC.UNKNOWN

# Checks on the image
hdulist = result.biasframe.open()
assert len(hdulist) == 1

hdu = hdulist[0]
assert hdu.shape == (4112, 4096)

data = hdu.data
mlevel = 0.0

block = data[1980:2020, 1980:2020]
mblock = block.mean()
sblock = block.std()

assert abs(mblock - mlevel) < 5 * sblock

# In the end, remove the files
for f in fs:
os.remove(f.name)
84 changes: 0 additions & 84 deletions megaradrp/recipes/calibration/traces.py

This file was deleted.

Loading

0 comments on commit 109e0d0

Please sign in to comment.