Skip to content

Commit

Permalink
Merge pull request #14 from sosey/ami-imports
Browse files Browse the repository at this point in the history
import updates and pep8 changes for ami step
  • Loading branch information
dmggh authored Jun 9, 2016
2 parents 66698cd + 3e4c7a3 commit ee6698e
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 76 deletions.
17 changes: 8 additions & 9 deletions jwst/ami/ami_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import logging
import warnings
import numpy as np
from jwst import datamodels
from .. import datamodels

from .NRM_Model import NRM_Model
from . import webb_psf
from . import leastsqnrm
from .NRM_Model import NRM_Model
from . import webb_psf
from . import leastsqnrm

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
Expand Down Expand Up @@ -43,7 +43,7 @@ def apply_LG (input_model, filter_model, oversample, rotation):
Fringe analysis data
"""

# Supress harmless arithmetic warnings for now
warnings.filterwarnings("ignore", ".*invalid value.*", RuntimeWarning)
warnings.filterwarnings("ignore", ".*divide by zero.*", RuntimeWarning)
Expand All @@ -68,7 +68,7 @@ def apply_LG (input_model, filter_model, oversample, rotation):
jwnrm = NRM_Model( mask='JWST', holeshape='hex',
pixscale=leastsqnrm.mas2rad(63.52554816773347),
rotate=rotation, rotlist_deg=rots_deg,
scallist=relpixscales)
scallist=relpixscales)

# Load the filter bandpass data into the NRM model
jwnrm.bandpass = band
Expand All @@ -79,15 +79,15 @@ def apply_LG (input_model, filter_model, oversample, rotation):
# (pixguess is a guess at the pixel scale of the data)
# produces a 19x19 image of the fit
jwnrm.fit_image( input_model.data, pixguess=jwnrm.pixel )

# Construct model image from fitted PSF
jwnrm.create_modelpsf()

# Reset the warnings filter to its original state
warnings.resetwarnings()

# Store fit results in output model
output_model = models.AmiLgModel (fit_image=jwnrm.modelpsf,
output_model = datamodels.AmiLgModel (fit_image=jwnrm.modelpsf,
resid_image=jwnrm.residual,
closure_amp_table=np.asarray(jwnrm.redundant_cas),
closure_phase_table=np.asarray(jwnrm.redundant_cps),
Expand All @@ -100,4 +100,3 @@ def apply_LG (input_model, filter_model, oversample, rotation):
output_model.update (input_model)

return output_model

13 changes: 6 additions & 7 deletions jwst/ami/ami_analyze_step.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from jwst.stpipe import Step
from jwst import datamodels
from ..stpipe import Step
from .. import datamodels
from . import ami_analyze


Expand All @@ -9,14 +9,14 @@ class AmiAnalyzeStep(Step):
applying the LG algorithm.
"""

spec = """
spec = """
oversample = integer( default=3,min=1 ) # Oversampling factor
rotation = float( default=0.0 ) # Rotation initial guess (deg)
"""

reference_file_types = ['throughput']

def process( self, input ):
def process( self, input ):
"""
Short Summary
-------------
Expand All @@ -40,7 +40,7 @@ def process( self, input ):
self.log.info('Initial rotation guess = %g deg', rotate)

# Open the input data model
with models.ImageModel(input) as input_model:
with datamodels.ImageModel(input) as input_model:

# Get the name of the filter throughput reference file to use
self.filter_ref_name = self.get_reference_file(input_model,
Expand All @@ -57,7 +57,7 @@ def process( self, input ):
return result

# Open the filter throughput reference file
filter_model = models.FilterModel(self.filter_ref_name)
filter_model = datamodels.FilterModel(self.filter_ref_name)

# Do the LG analysis on the input image
result = ami_analyze.apply_LG(input_model, filter_model,
Expand All @@ -68,4 +68,3 @@ def process( self, input ):
result.meta.cal_step.ami_analyze = 'COMPLETE'

return result

19 changes: 9 additions & 10 deletions jwst/ami/ami_average.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#

import logging
from jwst import datamodels
from .. import datamodels

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


def average_LG (lg_products):
def average_LG(lg_products):
"""
Short Summary
-------------
Expand All @@ -28,19 +28,19 @@ def average_LG (lg_products):
Averaged fringe data
"""

# Create the ouput model as a copy of the first input model
log.debug (' Create output as copy of %s', lg_products[0])
output_model = models.AmiLgModel(lg_products[0]).copy()
log.debug(' Create output as copy of %s', lg_products[0])
output_model = datamodels.AmiLgModel(lg_products[0]).copy()

# Loop over the remaining list of inputs, adding their values to the output
for file in lg_products[1:]:

log.debug (' Accumulate data from %s', file)
prod = models.AmiLgModel (file)
log.debug(' Accumulate data from %s', file)
prod = datamodels.AmiLgModel(file)

output_model.fit_image += prod.fit_image
output_model.resid_image =+ prod.resid_image
output_model.resid_image += prod.resid_image
output_model.closure_amp_table['coeffs'] += prod.closure_amp_table['coeffs']
output_model.closure_phase_table['coeffs'] += prod.closure_phase_table['coeffs']
output_model.fringe_amp_table['coeffs'] += prod.fringe_amp_table['coeffs']
Expand All @@ -51,7 +51,7 @@ def average_LG (lg_products):
prod.close()

# Take the average of the accumulated results
log.debug (' Divide accumulated results by %d', len(lg_products))
log.debug(' Divide accumulated results by %d', len(lg_products))
output_model.fit_image /= len(lg_products)
output_model.resid_image /= len(lg_products)
output_model.closure_amp_table['coeffs'] /= len(lg_products)
Expand All @@ -63,4 +63,3 @@ def average_LG (lg_products):

# Return the averaged model
return output_model

9 changes: 4 additions & 5 deletions jwst/ami/ami_average_step.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from jwst.stpipe import Step
from ..stpipe import Step

from . import ami_average

Expand All @@ -8,10 +8,10 @@ class AmiAverageStep(Step):
AmiAverageStep: Averages LG results for multiple NIRISS AMI mode exposures
"""

spec = """
spec = """
"""

def process( self, input_list ):
def process(self, input_list):
"""
Short Summary
-------------
Expand All @@ -31,8 +31,7 @@ def process( self, input_list ):

# Call the LG average routine for the list of inputs
result = ami_average.average_LG(input_list)

result.meta.cal_step.ami_average = 'COMPLETE'

return result

21 changes: 10 additions & 11 deletions jwst/ami/ami_normalize.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from __future__ import division

#
# Module for normalizing the LG results for a science target by
# Module for normalizing the LG results for a science target by
# the LG results for a reference target
#

import logging
from jwst import datamodels
from .. import datamodels

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


def normalize_LG (target_model, reference_model):
def normalize_LG(target_model, reference_model):
"""
Short Summary
-------------
Expand All @@ -33,20 +33,19 @@ def normalize_LG (target_model, reference_model):
Normalized fringe data for the target
"""

# Create the ouput model as a copy of the input target model
output_model = target_model.copy()

# Apply the normalizations to the target data
#output_model.fit_image =
#output_model.resid_image =
#output_model.closure_amp_table['coeffs'] =
# output_model.fit_image =
# output_model.resid_image =
# output_model.closure_amp_table['coeffs'] =
output_model.closure_phase_table['coeffs'] -= reference_model.closure_phase_table['coeffs']
output_model.fringe_amp_table['coeffs'] /= reference_model.fringe_amp_table['coeffs']
#output_model.fringe_phase_table['coeffs'] =
#output_model.pupil_phase_table['coeffs'] =
#output_model.solns_table['coeffs'] =
# output_model.fringe_phase_table['coeffs'] =
# output_model.pupil_phase_table['coeffs'] =
# output_model.solns_table['coeffs'] =

# Return the normalized target model
return output_model

13 changes: 6 additions & 7 deletions jwst/ami/ami_normalize_step.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from jwst.stpipe import Step
from jwst import datamodels
from ..stpipe import Step
from .. import datamodels

from . import ami_normalize

Expand All @@ -9,10 +9,10 @@ class AmiNormalizeStep(Step):
AmiNormalizeStep: Normalize target LG results using reference LG results
"""

spec = """
spec = """
"""

def process( self, target, reference ):
def process(self, target, reference):
"""
Short Summary
-------------
Expand All @@ -34,8 +34,8 @@ def process( self, target, reference ):
"""

# Open the target and reference input models
target_model = models.AmiLgModel(target)
reference_model = models.AmiLgModel(reference)
target_model = datamodels.AmiLgModel(target)
reference_model = datamodels.AmiLgModel(reference)

# Call the normalization routine
result = ami_normalize.normalize_LG(target_model, reference_model)
Expand All @@ -48,4 +48,3 @@ def process( self, target, reference ):

# We're done
return result

54 changes: 27 additions & 27 deletions jwst/ami/webb_psf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import numpy as np
import logging
from . import utils
from . import NRM_consts
from . import NRM_consts

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
log.setLevel(logging.DEBUG)

def get_webbpsf_filter( filter_model, specbin=None, trim=False ):

def get_webbpsf_filter(filter_model, specbin=None, trim=False):
"""
Short Summary
-------------
Expand All @@ -27,56 +28,55 @@ def get_webbpsf_filter( filter_model, specbin=None, trim=False ):
Returns
-------
spec - 2D float array
filter bandpass data: [weight, wavelength_in_microns]
spec - 2D float array
filter bandpass data: [weight, wavelength_in_microns]
"""

W = 1 # remove confusion - wavelength index
T = 0 # remove confusion - trans index after reading in...
W = 1 # remove confusion - wavelength index
T = 0 # remove confusion - trans index after reading in...

nwave = len(filter_model.filter_table['wavelength'])
tmp_array = np.zeros((nwave, 2))

# input in angst _ANGSTROM = 1.0e-10
tmp_array[:,W] = filter_model.filter_table['wavelength'] * 1.0e-10
# input in angst _ANGSTROM = 1.0e-10
tmp_array[:, W] = filter_model.filter_table['wavelength'] * 1.0e-10

# weights (peak unity, unnormalized sum)
tmp_array[:,T] = filter_model.filter_table['throughput']
# weights (peak unity, unnormalized sum)
tmp_array[:, T] = filter_model.filter_table['throughput']

# remove leading and trailing throughput lines with 'flag' array of indices
flag = np.where(tmp_array[:,T]!=0)[0]
flag = np.where(tmp_array[:, T] != 0)[0]
spec = tmp_array[flag, :]

# rebin as desired - fewer wavelengths for debugging quickly
if specbin:
smallshape = spec.shape[0]//specbin

log.debug(' bin filter data by %d from %d to %d', specbin,
spec.shape[0], smallshape)

spec = spec[:smallshape*specbin, :] # clip trailing
spec = utils.krebin(spec, (smallshape,2))
spec[:,W] = spec[:,W] / float(specbin) # krebin added up waves
spec[:,T] = spec[:,T] / float(specbin) # krebin added up trans too
spec = utils.krebin(spec, (smallshape, 2))
spec[:, W] = spec[:, W] / float(specbin) # krebin added up waves
spec[:, T] = spec[:, T] / float(specbin) # krebin added up trans too
log.debug(' post-bin shape %s', spec.shape)

if trim:
log.debug(' TRIMing filter data ...')
wl = spec[:,W].copy()
tr = spec[:,T].copy()
idx = np.where((wl > (1.0 - 0.5*trim[1])*trim[0]) & \
wl = spec[:, W].copy()
tr = spec[:, T].copy()
idx = np.where((wl > (1.0 - 0.5*trim[1])*trim[0]) &
(wl < (1.0 + 0.5*trim[1])*trim[0]))
wl = wl[idx]
tr = tr[idx]
spec = np.zeros((len(idx[0]),2))
spec[:,1] = wl
spec[:,0] = tr
spec = np.zeros((len(idx[0]), 2))
spec[:, 1] = wl
spec[:, 0] = tr
log.debug(' post-trim shape %s', spec.shape)

log.debug(' final filter shape %s', spec.shape)
log.debug(' %d filter samples between %.3f and %.3f um', \
len(spec[:,0]), spec[0,W]/NRM_consts.um_, \
spec[-1,W]/NRM_consts.um_)

log.debug(' %d filter samples between %.3f and %.3f um',
len(spec[:, 0]), spec[0, W]/NRM_consts.um_,
spec[-1, W]/NRM_consts.um_)

return spec

0 comments on commit ee6698e

Please sign in to comment.