Skip to content

Commit

Permalink
BPM improved
Browse files Browse the repository at this point in the history
  • Loading branch information
Pica committed Jan 8, 2016
1 parent b3b017c commit 7454ff4
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 44 deletions.
25 changes: 2 additions & 23 deletions megaradrp/recipes/calibration/tests/test_bpm.py
Expand Up @@ -26,28 +26,12 @@
import pytest
from numina.core import DataFrame, ObservationResult

from megaradrp.tests.simulation import simulate_flat, simulate_bias
from megaradrp.tests.simulation import simulate_flat
from megaradrp.tests.simulation import ReadParams, MegaraDetectorSat
from megaradrp.recipes.calibration.bpm import BadPixelsMaskRecipe
from megaradrp.recipes.calibration.bias import BiasRecipe

from megaradrp.recipes.calibration.tests.test_bpm_common import generate_bias

def generate_bias(detector, number, temporary_path):
fs = [simulate_bias(detector) for i in range(number)]
for aux in range(len(fs)):
fits.writeto('%s/bias_%s.fits' % (temporary_path, aux), fs[aux],
clobber=True)

fs = ["%s/bias_%s.fits" % (temporary_path, i) for i in range(number)]

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

recipe = BiasRecipe()
ri = recipe.create_input(obresult=ob)
return recipe.run(ri)

# @pytest.mark.remote
def test_bpm():
Expand All @@ -56,11 +40,6 @@ def test_bpm():
DSHAPE = (2056 * 2, 2048 * 2)
OSCAN = 50

BINR = 1
BINC = 1

SHAPE = DSHAPE[0] // BINR, DSHAPE[1] // BINC

ron = 2.0
gain = 1.0
bias = 1000.0
Expand Down
102 changes: 102 additions & 0 deletions megaradrp/recipes/calibration/tests/test_bpm_common.py
@@ -0,0 +1,102 @@
#
# Copyright 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/>.
#

"""Tests for the bpm mode recipe module."""

from numina.core import DataFrame, ObservationResult
import astropy.io.fits as fits
import numpy as np


def generate_bias(detector, number, temporary_path):
from megaradrp.tests.simulation import simulate_bias
from megaradrp.recipes.calibration.bias import BiasRecipe

fs = [simulate_bias(detector) for i in range(number)]
for aux in range(len(fs)):
fits.writeto('%s/bias_%s.fits' % (temporary_path, aux), fs[aux],
clobber=True)

fs = ["%s/bias_%s.fits" % (temporary_path, i) for i in range(number)]

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

recipe = BiasRecipe()
ri = recipe.create_input(obresult=ob)
return recipe.run(ri)

def crear_archivos(temporary_path):
from megaradrp.tests.simulation import simulate_flat
from megaradrp.tests.simulation import ReadParams, MegaraDetectorSat
from megaradrp.recipes.calibration.bpm import BadPixelsMaskRecipe

number = 5
PSCAN = 50
DSHAPE = (2056 * 2, 2048 * 2)
OSCAN = 50
ron = 2.0
gain = 1.0
bias = 1000.0

eq = 0.8 * np.ones(DSHAPE)
eq[0:15, 0:170] = 0.0

readpars1 = ReadParams(gain=gain, ron=ron, bias=bias)
readpars2 = ReadParams(gain=gain, ron=ron, bias=bias)

detector = MegaraDetectorSat(DSHAPE, OSCAN, PSCAN, eq=eq,
dark=(3.0 / 3600.0),
readpars1=readpars1, readpars2=readpars2,
bins='11')

source2 = 1.0

fs = [simulate_flat(detector, exposure=1.0, source=5000 * source2) for i in
range(number)]

for aux in range(len(fs)):
fits.writeto('%s/flat_%s.fits' % (temporary_path, aux), fs[aux],
clobber=True)

master_bias = generate_bias(detector, number, temporary_path)
master_bias_data = master_bias.biasframe.frame[0].data

fits.writeto('%s/master_bias_data0.fits' % temporary_path,
master_bias_data, clobber=True) # Master Bias

ob = ObservationResult()
ob.instrument = 'MEGARA'
ob.mode = 'bias_image'
names = []

for aux in range(number):
names.append('%s/flat_%s.fits' % (temporary_path, aux))
ob.frames = [DataFrame(filename=open(nombre).name) for nombre in names]

recipe = BadPixelsMaskRecipe()
ri = recipe.create_input(obresult=ob, master_bias=DataFrame(
filename=open(temporary_path + '/master_bias_data0.fits').name))
aux = recipe.run(ri)
fits.writeto('%s/master_bpm.fits' % temporary_path, aux.bpm_image.frame[0].data[1], clobber=True)

return names

36 changes: 15 additions & 21 deletions megaradrp/recipes/calibration/tests/test_bpm_corrector.py
Expand Up @@ -21,22 +21,22 @@

from numina.core import DataFrame, ObservationResult
import astropy.io.fits as fits
import numpy as np
import os
from numina.core.requirements import ObservationResultRequirement
from numina.flow.processing import BadPixelCorrector

from megaradrp.core.recipe import MegaraBaseRecipe
from megaradrp.requirements import MasterBiasRequirement, MasterBPMRequirement
from megaradrp.processing.trimover import OverscanCorrector, TrimImage

from megaradrp.recipes.calibration.tests.test_bpm_common import crear_archivos

class TestRecipe(MegaraBaseRecipe):
obresult = ObservationResultRequirement()
master_bias = MasterBiasRequirement()
master_bpm = MasterBPMRequirement()

def __init__(self):
def __init__(self, directorio):
self.directorio = directorio
super(TestRecipe, self).__init__(version="0.1.0")
self._MegaraBaseRecipe__flow['TestRecipe'] = [OverscanCorrector,
TrimImage,
Expand All @@ -45,8 +45,6 @@ def __init__(self):
def run(self, rinput):
import copy

directorio = os.path.dirname(os.path.abspath(__file__))

N = len(rinput.obresult.frames)
obresult1 = copy.copy(rinput.obresult)
obresult1.frames = rinput.obresult.frames[:N]
Expand All @@ -59,7 +57,8 @@ def run(self, rinput):
params['biasmap'] = mbias

reduced1 = self.bias_process_common(obresult1, params)
fits.writeto(directorio + '/tmp2/reduced_flat.fits', reduced1[0].data, clobber=True)
fits.writeto(self.directorio + '/reduced_flat.fits', reduced1[0].data,
clobber=True)

try:
with rinput.master_bpm.open() as hdul:
Expand All @@ -69,37 +68,32 @@ def run(self, rinput):
pass

reduced1 = self.bias_process_common(obresult1, params)
fits.writeto(directorio + '/tmp2/reduced_flat_bpm.fits', reduced1[0].data, clobber=True)
fits.writeto(self.directorio + '/reduced_flat_bpm.fits',
reduced1[0].data, clobber=True)

return True


def test_bpm_corrector():
number = 5
DSHAPE = (2056 * 2, 2048 * 2)
import shutil
from tempfile import mkdtemp

eq = 0.8 * np.ones(DSHAPE)
eq[0:15, 0:170] = 0.0
directorio = mkdtemp()
names = crear_archivos(directorio)

ob = ObservationResult()
ob.instrument = 'MEGARA'
ob.mode = 'bias_image'
names = []

directorio = os.path.dirname(os.path.abspath(__file__))

for aux in range(number):
names.append('%s/tmp2/flat_%s.fits' % (directorio,aux))
ob.frames = [DataFrame(filename=open(nombre).name) for nombre in names]

recipe = TestRecipe()
recipe = TestRecipe(directorio)
ri = recipe.create_input(obresult=ob, master_bias=DataFrame(
filename=open(directorio + '/tmp2/master_bias_data0.fits').name),
filename=open(directorio + '/master_bias_data0.fits').name),
master_bpm=DataFrame(filename=open(
directorio+ '/tmp2/master_bpm.fits').name))
directorio + '/master_bpm.fits').name))

recipe.run(ri)

shutil.rmtree(directorio)

if __name__ == "__main__":
test_bpm_corrector()

0 comments on commit 7454ff4

Please sign in to comment.