Coverage for python/lsst/atmospec/dispersion.py: 34%

25 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-15 03:50 -0700

1# This file is part of atmospec. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22import numpy as np 

23 

24import lsst.log as lsstLog 

25 

26 

27__all__ = ['DispersionRelation'] 

28 

29 

30class DispersionRelation(object): 

31 def __init__(self, observedLines, spectralLines): 

32 """The dispersion relation, relating pixel to wavelength and vice versa 

33 

34 Parameters: 

35 ----------- 

36 observedLines : `list` of `float` 

37 The central wavelength of the observed lines in the spectrum in pix 

38 

39 spectralLines : `list` of `float` 

40 The central wavelength of the spectral lines present in the source 

41 in nm. 

42 

43 Notes: 

44 ------ 

45 The current implementation just supplies linear transformations 

46 but future extensions can support higher order polynomials, 

47 spline-fits, distortions etc 

48 """ 

49 self.observedLines = observedLines 

50 self.spectralLines = spectralLines 

51 

52 self.log = lsstLog.Log.getLogger('atmospec.dispersionRelation') 

53 self.pix2wlCoeffs = self._calcCoefficients() 

54 

55 def _calcCoefficients(self): 

56 if((self.observedLines is None) or (self.spectralLines is None)): 

57 self.log.warn('Missing input for _calcCoefficients, default transformation: 1 to 1 ') 

58 self.observedLines = [1, 2] 

59 self.spectralLines = [1, 2] 

60 pix2wlCoeffs = np.polyfit(self.observedLines, self.spectralLines, deg=1) 

61 

62 # xxx change to debug 

63 self.log.info('Pixel -> Wavelength linear transformation coefficients : ' + str(pix2wlCoeffs)) 

64 return pix2wlCoeffs 

65 

66 def Wavelength2Pixel(self, wavelength): 

67 """Currently just a linear transform""" 

68 wavelength = np.asarray(wavelength, dtype=np.float64) 

69 coef = self.pix2wlCoeffs 

70 return (wavelength-coef[1]) / coef[0] 

71 

72 def Pixel2Wavelength(self, pixel): 

73 """Currently just a linear transform""" 

74 pixel = np.asarray(pixel, dtype=np.float64) 

75 coef = self.pix2wlCoeffs 

76 return np.array(coef[1] + coef[0] * pixel)