Coverage for python/lsst/faro/utils/filter_map.py: 33%

43 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-08-05 01:29 -0700

1# This file is part of faro. 

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 os 

23import fnmatch 

24 

25from lsst.utils import getPackageDir 

26from lsst.pex.config import Config, DictField, ConfigDictField 

27 

28__all__ = ("FilterMapDict", "FilterMap") 

29 

30 

31class FilterMapNotFoundError(LookupError): 

32 """Exception class indicating we couldn't find a filter map 

33 """ 

34 pass 

35 

36 

37class ColortermNotFoundError(LookupError): 

38 """Exception class indicating we couldn't find the color term 

39 """ 

40 pass 

41 

42 

43class FilterMapDict(Config): 

44 """A mapping of band to physical filter label. 

45 """ 

46 data = DictField( 

47 doc="Mapping of band to physical filter label", 

48 keytype=str, 

49 itemtype=str, 

50 default={}, 

51 ) 

52 

53 

54class FilterMap(Config): 

55 """Config for mapping of band to physical filter label.""" 

56 data = ConfigDictField( 

57 doc="Mapping of band to physical filter label.", 

58 keytype=str, 

59 itemtype=FilterMapDict, 

60 default={}, 

61 ) 

62 

63 def __init__(self, filename=None): 

64 if filename is None: 

65 filename = os.path.join(getPackageDir('faro'), 'config', 'filterMap.py') 

66 self.load(filename) 

67 

68 def getFilters(self, instName, bands, doRaise=True): 

69 """Get the physical filters that correspond with input bands.""" 

70 try: 

71 trueInstName = None 

72 filterDictConfig = self.data.get(instName) 

73 if filterDictConfig is None: 

74 # try glob expression 

75 matchList = [libInstNameGlob for libInstNameGlob in self.data 

76 if fnmatch.fnmatch(instName, libInstNameGlob)] 

77 if len(matchList) == 1: 

78 trueInstName = matchList[0] 

79 filterDictConfig = self.data[trueInstName] 

80 elif len(matchList) > 1: 

81 raise FilterMapNotFoundError( 

82 "Multiple library globs match instName %r: %s" % (instName, matchList)) 

83 else: 

84 raise FilterMapNotFoundError( 

85 "No filer map dict found with instName %r" % instName) 

86 

87 filterDict = filterDictConfig.data 

88 

89 filters = [] 

90 for band in bands: 

91 if band not in filterDict: 

92 errMsg = "No filter found for band %r with instName %r" % ( 

93 band, instName) 

94 if trueInstName is not None: 

95 errMsg += " = catalog %r" % (trueInstName,) 

96 raise ColortermNotFoundError(errMsg) 

97 

98 filters.append(filterDict[band]) 

99 return filters 

100 except FilterMapNotFoundError: 

101 if doRaise: 

102 raise 

103 else: 

104 return bands