From 7454ff466f2bd7f9f17f19248b4b8d8e3aa6b577 Mon Sep 17 00:00:00 2001 From: Pica Date: Fri, 8 Jan 2016 13:48:33 +0100 Subject: [PATCH] BPM improved --- .../recipes/calibration/tests/test_bpm.py | 25 +---- .../calibration/tests/test_bpm_common.py | 102 ++++++++++++++++++ .../calibration/tests/test_bpm_corrector.py | 36 +++---- 3 files changed, 119 insertions(+), 44 deletions(-) create mode 100644 megaradrp/recipes/calibration/tests/test_bpm_common.py diff --git a/megaradrp/recipes/calibration/tests/test_bpm.py b/megaradrp/recipes/calibration/tests/test_bpm.py index 19d152af..80847490 100644 --- a/megaradrp/recipes/calibration/tests/test_bpm.py +++ b/megaradrp/recipes/calibration/tests/test_bpm.py @@ -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(): @@ -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 diff --git a/megaradrp/recipes/calibration/tests/test_bpm_common.py b/megaradrp/recipes/calibration/tests/test_bpm_common.py new file mode 100644 index 00000000..0ae6a3b2 --- /dev/null +++ b/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 . +# + +"""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 + diff --git a/megaradrp/recipes/calibration/tests/test_bpm_corrector.py b/megaradrp/recipes/calibration/tests/test_bpm_corrector.py index 508aa0d4..30a75eda 100644 --- a/megaradrp/recipes/calibration/tests/test_bpm_corrector.py +++ b/megaradrp/recipes/calibration/tests/test_bpm_corrector.py @@ -21,8 +21,6 @@ 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 @@ -30,13 +28,15 @@ 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, @@ -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] @@ -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: @@ -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()