Coverage for python/astro_metadata_translator/serialize/fits.py: 11%

Shortcuts 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

34 statements  

1# This file is part of astro_metadata_translator. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

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

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

7# for details of code ownership. 

8# 

9# Use of this source code is governed by a 3-clause BSD-style 

10# license that can be found in the LICENSE file. 

11 

12"""Transform ObservationInfo into "standard" FITS headers.""" 

13 

14__all__ = ("info_to_fits", "dates_to_fits", "group_to_fits") 

15 

16 

17def dates_to_fits(date_begin, date_end): 

18 """Convert two dates into FITS form. 

19 

20 Parameters 

21 ---------- 

22 date_begin : `astropy.time.Time` 

23 Date representing the beginning of the observation. 

24 date_end : `astropy.time.Time` 

25 Date representing the end of the observation. 

26 

27 Returns 

28 ------- 

29 cards : `dict` of `str` to `str` or `float` 

30 Header card keys and values following the FITS standard. 

31 If neither date is defined this may be empty. 

32 """ 

33 cards = {} 

34 if date_begin is None and date_end is None: 

35 # no date headers can be written 

36 return cards 

37 

38 cards["TIMESYS"] = "TAI" 

39 

40 date_avg = None 

41 if date_begin is not None and date_end is not None: 

42 date_avg = date_begin + (date_end - date_begin)/2.0 

43 

44 for fragment, date in (("OBS", date_begin), ("END", date_end), ("AVG", date_avg)): 

45 if date is not None: 

46 tai = date.tai 

47 cards[f"DATE-{fragment}"] = tai.isot 

48 cards[f"MJD-{fragment}"] = tai.mjd 

49 

50 return cards 

51 

52 

53def info_to_fits(obs_info): 

54 """Convert an `ObservationInfo` to something suitable for writing 

55 to a FITS file. 

56 

57 Parameters 

58 ---------- 

59 obs_info : `ObservationInfo` 

60 Standardized observation information to transform to FITS headers. 

61 

62 Returns 

63 ------- 

64 cards : `dict` of `str` to (`int`, `float`, `str`, `bool`) 

65 FITS header keys and values in form understood by FITS. 

66 comments : `dict` of `str` to `str` 

67 Suitable comment string. There will be at most one entry for each key 

68 in ``cards``. 

69 """ 

70 

71 cards = {} 

72 comments = {} 

73 

74 if obs_info.instrument is not None: 

75 cards["INSTRUME"] = obs_info.instrument 

76 comments["INSTRUME"] = "Name of instrument" 

77 

78 cards.update(dates_to_fits(obs_info.datetime_begin, obs_info.datetime_end)) 

79 

80 return cards, comments 

81 

82 

83def group_to_fits(obs_group): 

84 """Convert an `ObservationGroup` to something suitable for writing 

85 to a FITS file. 

86 

87 Parameters 

88 ---------- 

89 obs_group : `ObservationGroup` 

90 Collection of observation information to transform to a single 

91 FITS header. 

92 

93 Returns 

94 ------- 

95 cards : `dict` of `str` to (`int`, `float`, `str`, `bool`) 

96 FITS header keys and values in form understood by FITS. 

97 comments : `dict` of `str` to `str` 

98 Suitable comment string. There will be at most one entry for each key 

99 in ``cards``. 

100 """ 

101 

102 cards = {} 

103 comments = {} 

104 

105 oldest, newest = obs_group.extremes() 

106 

107 instruments = obs_group.property_values("instrument") 

108 if len(instruments) == 1: 

109 cards["INSTRUME"] = list(instruments)[0] 

110 comments["INSTRUME"] = "Name of instrument" 

111 

112 cards.update(dates_to_fits(oldest.datetime_begin, newest.datetime_end)) 

113 

114 return cards, comments