ReFRACtor
instrument.py
Go to the documentation of this file.
1 import numpy as np
2 
3 from .base import Creator
4 from .value import CreatorFlaggedValue, CreatorFlaggedValueMultiChannel
5 from .. import param
6 
7 from refractor import framework as rf
8 
10 
11  ils_half_width = param.ArrayWithUnit(dims=1)
12  dispersion = param.Iterable(rf.Dispersion)
13  ils_function = param.Iterable(rf.IlsFunction)
14  instrument_correction = param.ObjectVector("vector_instrument_correction")
15 
16  def create(self, **kwargs):
17 
18  # Store for use by other dependant creators
19  dispersion = self.common_store["dispersion"] = self.dispersion()
20 
21  ils_vec = rf.vector_ils()
22  for disp, ils_func, half_width in zip(dispersion, self.ils_function(), self.ils_half_width()):
23  ils_vec.push_back( rf.IlsConvolution(disp, ils_func, half_width) )
24  return rf.IlsInstrument(ils_vec, self.instrument_correction())
25 
27 
28  number_samples = param.Array(dims=1)
29  is_one_based = param.Scalar(bool, default=False)
30  num_parameters = param.Scalar(int, default=2)
31  desc_band_name = param.Iterable(str)
32  num_channels = param.Scalar(int)
33  spec_win = param.InstanceOf(rf.SpectralWindow)
34 
35  def retrieval_flag(self):
36  # Mask out non retrieved parameters
37  ret_flag = super().retrieval_flag()
38  ret_flag[:, slice(self.num_parameters(), ret_flag.shape[1])] = False
39  return ret_flag
40 
41  def create(self, **kwargs):
42 
43  disp_coeffs = self.value()
44  retrieval_flag = self.retrieval_flag()
45 
46  desc_band_name = self.desc_band_name()
47  number_samples = self.number_samples()
48  is_one_based = self.is_one_based()
49 
50  disp = []
51  vec_disp = rf.vector_dispersion()
52  for chan_idx in range(self.num_channels()):
53  chan_disp = rf.DispersionPolynomial(disp_coeffs.value[chan_idx, :], retrieval_flag[chan_idx, :], disp_coeffs.units,
54  desc_band_name[chan_idx], int(number_samples[chan_idx]), is_one_based)
55  disp.append(chan_disp)
56  vec_disp.push_back(chan_disp)
57 
58  # Set dispersion into the spectral window class if it is a SpectralWindowRange
59  # so it can use the dispersion to convert sample_indexes into an actual spectral range
60  # This gets converted in the SpectralWindowRange spectral_bound method. The bounds
61  # get used elsewhere to resolve which channels spectral points belong to. This
62  # should definitely be handled in a better manner somehow since the order that Dispersion
63  # gets sets into the SpectralWindowRange matters.
64  spec_win = self.spec_win()
65  if hasattr(spec_win, "dispersion"):
66  self.spec_win().dispersion = vec_disp
67 
68  return disp
69 
71 
72  # Supply these either as a combined 3D array or 3 different 2D arrays
73  delta_lambda = param.Choice(param.Array(dims=3), param.Iterable(np.ndarray))
74  response = param.Choice(param.Array(dims=3), param.Iterable(np.ndarray))
75  wavenumber = param.Choice(param.Array(dims=3), param.Iterable(np.ndarray), default=None, required=False)
76 
77  dispersion = param.Iterable(rf.Dispersion)
78  hdf_band_name = param.Iterable(str)
79  desc_band_name = param.Iterable(str)
80  interpolate = param.Scalar(bool, default=False)
81  log_space = param.Scalar(bool, default=False)
82  num_channels = param.Scalar(int)
83 
84  def create(self, **kwargs):
85 
86  delta_lambda = self.delta_lambda()
87  response = self.response()
88  wavenumber = self.wavenumber()
89 
90  if type(delta_lambda) != type(response) or (wavenumber and type(wavenumber) != type(delta_lambda)):
91  raise param.ParamError("delta_lambda, response and wavenumber (if supplied) must have the same data type")
92 
93  if isinstance(delta_lambda, np.ndarray):
94  combined = True
95  else:
96  combined = False
97 
98  dispersion = self.dispersion()
99  desc_band_name = self.desc_band_name()
100  hdf_band_name = self.hdf_band_name()
101  interpolate = self.interpolate()
102 
103  if self.log_space():
104  IlsClass = rf.IlsTableLog
105  else:
106  IlsClass = rf.IlsTableLinear
107 
108  ils_func = []
109  for chan_idx in range(self.num_channels()):
110  if wavenumber is None:
111  chan_wn = dispersion[chan_idx].pixel_grid.data
112  else:
113  if combined:
114  chan_wn = wavenumber[chan_idx, :]
115  else:
116  chan_wn = wavenumber[chan_idx]
117 
118  if combined:
119  ils_func.append( IlsClass(chan_wn, delta_lambda[chan_idx, :, :], response[chan_idx, :, :],
120  desc_band_name[chan_idx], hdf_band_name[chan_idx], interpolate) )
121  else:
122  ils_func.append( IlsClass(chan_wn, delta_lambda[chan_idx], response[chan_idx],
123  desc_band_name[chan_idx], hdf_band_name[chan_idx], interpolate) )
124 
125  return ils_func
126 
128 
129  hwhm = param.ArrayWithUnit(dims=1)
130  hdf_band_name = param.Iterable(str)
131  desc_band_name = param.Iterable(str)
132  num_channels = param.Scalar(int)
133 
134  def create(self, **kwargs):
135 
136  hwhm = self.hwhm()
137  hdf_band_name = self.hdf_band_name()
138  desc_band_name = self.desc_band_name()
139 
140  ils_func = []
141  for chan_idx in range(self.num_channels()):
142  ils_func.append( rf.IlsGaussian(hwhm.value[chan_idx], desc_band_name[chan_idx], hdf_band_name[chan_idx]) )
143 
144  return ils_func
145 
147 
148  num_channels = param.Scalar(int)
149 
150  def create(self, **kwargs):
151 
152  rel_vel = self.value()
153  ret_flag = self.retrieval_flag()
154 
155  inst_doppler = []
156  for chan_idx in range(self.num_channels()):
157  inst_doppler.append( rf.InstrumentDoppler(rel_vel[chan_idx], bool(ret_flag[chan_idx])) )
158 
159  return inst_doppler
160 
162 
163  corrections = param.Iterable(str)
164  num_channels = param.Scalar(int)
165 
166  def create(self, **kwargs):
167 
168  all_corrections = []
169  for correction_name in self.corrections():
170  self.register_parameter(correction_name, param.Iterable())
171  all_corrections.append(self.param(correction_name))
172 
173  # Map these into an outer vector for each channel, with an inner vector for each correction
174  inst_corr = rf.vector_vector_instrument_correction()
175  for chan_index in range(self.num_channels()):
176  per_channel_corr = rf.vector_instrument_correction()
177 
178  for correction in all_corrections:
179  per_channel_corr.push_back(correction[chan_index])
180 
181  inst_corr.push_back(per_channel_corr)
182 
183  return inst_corr
184 
186 
187  band_reference = param.ArrayWithUnit(dims=1)
188  num_channels = param.Scalar(int)
189  desc_band_name = param.Iterable(str)
190 
191  def create(self, **kwargs):
192 
193  params = self.value()
194  band_ref = self.band_reference()
195  retrieval_flag = self.retrieval_flag()
196  band_name = self.desc_band_name()
197 
198  rad_scaling = []
199  for chan_index in range(self.num_channels()):
200 
201  rad_scaling.append( rf.RadianceScalingSvFit(params[chan_index, :], retrieval_flag[chan_index, :], band_ref[chan_index], band_name[chan_index]) )
202 
203  return rad_scaling
204 
206 
207  units = param.InstanceOf(rf.Unit)
208  scale_factor = param.Scalar(float)
209  num_channels = param.Scalar(int)
210 
211  def create(self, **kwargs):
212 
213  apply_units = []
214  for chan_index in range(self.num_channels()):
215  apply_units.append( rf.ApplyInstrumentUnits(self.units(), self.scale_factor()) )
216 
217  return apply_units
def register_parameter(self, param_name, param_def)
Definition: base.py:95
def param(self, param_name, kwargs)
Definition: base.py:111

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:10