Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/bin/env python 

2# encoding: utf-8 

3""" 

4*use a first-guess dispersion map to convert wavelengths to pixels* 

5 

6:Author: 

7 David Young 

8 

9:Date Created: 

10 April 15, 2021 

11""" 

12from builtins import object 

13import sys 

14import os 

15os.environ['TERM'] = 'vt100' 

16from fundamentals import tools 

17from os.path import expanduser 

18import unicodecsv as csv 

19from soxspipe.commonutils.polynomials import chebyshev_order_wavelength_polynomials 

20import numpy as np 

21 

22 

23def dispersion_map_to_pixel_arrays( 

24 log, 

25 dispersionMapPath, 

26 orderPixelTable): 

27 """*use a first-guess dispersion map to append x,y fits to line-list data frame.*  

28 

29 Return a line-list with x,y fits given a first guess dispersion map.* 

30 

31 **Key Arguments:** 

32 

33 - `log` -- logger 

34 - `dispersionMapPath` -- path to the dispersion map 

35 - `orderPixelTable` -- a data-frame including 'order', 'wavelength' and 'slit_pos' columns 

36 

37 **Usage:** 

38 

39 ```python 

40 from soxspipe.commonutils import dispersion_map_to_pixel_arrays 

41 myDict = { 

42 "order": [11, 11, 11], 

43 "wavelength": [850.3, 894.3, 983.2], 

44 "slit_position": [0, 0, 0] 

45 } 

46 orderPixelTable = pd.DataFrame(myDict) 

47 orderPixelTable = dispersion_map_to_pixel_arrays( 

48 log=log, 

49 dispersionMapPath="/path/to/map.csv", 

50 orderPixelTable=orderPixelTable 

51 ) 

52 ```  

53 """ 

54 log.debug('starting the ``dispersion_map_to_pixel_arrays`` function') 

55 

56 # READ THE FILE 

57 home = expanduser("~") 

58 dispersion_map = dispersionMapPath.replace("~", home) 

59 

60 # READ IN THE X- AND Y- GENERATING POLYNOMIALS FROM DISPERSION MAP FILE 

61 coeff = {} 

62 poly = {} 

63 with open(dispersion_map, 'rb') as csvFile: 

64 csvReader = csv.DictReader( 

65 csvFile, dialect='excel', delimiter=',', quotechar='"') 

66 for row in csvReader: 

67 axis = row["axis"] 

68 order_deg = int(row["order-deg"]) 

69 wavelength_deg = int(row["wavelength-deg"]) 

70 slit_deg = int(row["slit-deg"]) 

71 coeff[axis] = [float(v) for k, v in row.items() if k not in [ 

72 "axis", "order-deg", "wavelength-deg", "slit-deg"]] 

73 poly[axis] = chebyshev_order_wavelength_polynomials( 

74 log=log, order_deg=order_deg, wavelength_deg=wavelength_deg, slit_deg=slit_deg).poly 

75 csvFile.close() 

76 

77 # CONVERT THE ORDER-SORTED WAVELENGTH ARRAYS INTO ARRAYS OF PIXEL TUPLES 

78 orderPixelTable["fit_x"] = poly['x'](orderPixelTable, *coeff['x']) 

79 orderPixelTable["fit_y"] = poly['y'](orderPixelTable, *coeff['y']) 

80 

81 # FILTER DATA FRAME 

82 # FIRST CREATE THE MASK 

83 mask = (orderPixelTable["fit_x"] > 0) & (orderPixelTable["fit_y"] > 0) 

84 orderPixelTable = orderPixelTable.loc[mask] 

85 

86 log.debug('completed the ``dispersion_map_to_pixel_arrays`` function') 

87 return orderPixelTable