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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

# 

# LSST Data Management System 

# Copyright 2008-2016 AURA/LSST. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

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

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

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

# (at your option) any later version. 

# 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <https://www.lsstcorp.org/LegalNotices/>. 

# 

 

__all__ = ["makeMergedSchema", "copyIntoCatalog", 

"matchesToCatalog", "matchesFromCatalog"] 

 

import os.path 

 

import numpy as np 

 

import lsst.pex.exceptions as pexExcept 

from .schema import Schema 

from .schemaMapper import SchemaMapper 

from .base import BaseCatalog 

from .simple import SimpleCatalog, SimpleTable 

from .source import SourceCatalog, SourceTable 

from .match import ReferenceMatch 

 

from lsst.utils import getPackageDir 

 

 

def makeMapper(sourceSchema, targetSchema, sourcePrefix=None, targetPrefix=None): 

"""Create a SchemaMapper between the input source and target schemas 

 

@param[in] sourceSchema input source schema that fields will be mapped from 

@param[in] targetSchema target schema that fields will be mapped to 

@param[in] sourcePrefix if set, only those keys with that prefix will be mapped 

@param[in] targetPrefix if set, prepend it to the mapped (target) key name 

 

@return SchemaMapper between source and target schemas 

""" 

m = SchemaMapper(sourceSchema, targetSchema) 

for key, field in sourceSchema: 

keyName = field.getName() 

if sourcePrefix is not None: 

if not keyName.startswith(sourcePrefix): 

continue 

else: 

keyName = field.getName().replace(sourcePrefix, "", 1) 

m.addMapping(key, (targetPrefix or "") + keyName) 

return m 

 

 

def makeMergedSchema(sourceSchema, targetSchema, sourcePrefix=None, targetPrefix=None): 

"""Return a schema that is a deep copy of a mapping between source and target schemas 

@param[in] sourceSchema input source schema that fields will be mapped from 

@param[in] targetSchema target schema that fields will be mapped to 

@param[in] sourcePrefix if set, only those keys with that prefix will be mapped 

@param[in] targetPrefix if set, prepend it to the mapped (target) key name 

 

@return schema schema that is the result of the mapping between source and target schemas 

""" 

return makeMapper(sourceSchema, targetSchema, sourcePrefix, targetPrefix).getOutputSchema() 

 

 

def copyIntoCatalog(catalog, target, sourceSchema=None, sourcePrefix=None, targetPrefix=None): 

"""Copy entries from one Catalog into another 

 

@param[in] catalog source catalog to be copied from 

@param[in/out] target target catalog to be copied to (edited in place) 

@param[in] souceSchema schema of source catalog (optional) 

@param[in] sourcePrefix if set, only those keys with that prefix will be copied 

@param[in] targetPrefix if set, prepend it to the copied (target) key name 

""" 

if sourceSchema is None: 

sourceSchema = catalog.schema 

 

targetSchema = target.schema 

target.reserve(len(catalog)) 

for i in range(len(target), len(catalog)): 

target.addNew() 

 

if len(catalog) != len(target): 

raise RuntimeError("Length mismatch: %d vs %d" % 

(len(catalog), len(target))) 

 

m = makeMapper(sourceSchema, targetSchema, sourcePrefix, targetPrefix) 

for rFrom, rTo in zip(catalog, target): 

rTo.assign(rFrom, m) 

 

 

def matchesToCatalog(matches, matchMeta): 

"""Denormalise matches into a Catalog of "unpacked matches" 

 

@param[in] matches unpacked matches, i.e. a list of Match objects whose schema 

has "first" and "second" attributes which, resepectively, contain the 

reference and source catalog entries, and a "distance" field (the 

measured distance between the reference and source objects) 

@param[in] matchMeta metadata for matches (must have .add attribute) 

 

@return lsst.afw.table.BaseCatalog of matches (with ref_ and src_ prefix identifiers 

for referece and source entries, respectively) 

""" 

if len(matches) == 0: 

raise RuntimeError("No matches provided.") 

 

refSchema = matches[0].first.getSchema() 

srcSchema = matches[0].second.getSchema() 

 

mergedSchema = makeMergedSchema(refSchema, Schema(), targetPrefix="ref_") 

mergedSchema = makeMergedSchema( 

srcSchema, mergedSchema, targetPrefix="src_") 

distKey = mergedSchema.addField( 

"distance", type=np.float64, doc="Distance between ref and src") 

 

mergedCatalog = BaseCatalog(mergedSchema) 

copyIntoCatalog([m.first for m in matches], mergedCatalog, 

sourceSchema=refSchema, targetPrefix="ref_") 

copyIntoCatalog([m.second for m in matches], mergedCatalog, 

sourceSchema=srcSchema, targetPrefix="src_") 

for m, r in zip(matches, mergedCatalog): 

r.set(distKey, m.distance) 

 

# obtain reference catalog name if one is setup 

try: 

catalogName = os.path.basename(getPackageDir("astrometry_net_data")) 

except pexExcept.NotFoundError: 

catalogName = "NOT_SET" 

matchMeta.add("REFCAT", catalogName) 

mergedCatalog.getTable().setMetadata(matchMeta) 

 

return mergedCatalog 

 

 

def matchesFromCatalog(catalog, sourceSlotConfig=None): 

"""Generate a list of ReferenceMatches from a Catalog of "unpacked matches" 

 

@param[in] catalog catalog of matches. Must have schema where reference entries are 

prefixed with "ref_" and source entries are prefixed with "src_" 

@param[in] sourceSlotConfig an lsst.meas.base.baseMeasurement.SourceSlotConfig configuration 

for source slots (optional) 

 

@returns lsst.afw.table.ReferenceMatch of matches 

""" 

refSchema = makeMergedSchema( 

catalog.schema, SimpleTable.makeMinimalSchema(), sourcePrefix="ref_") 

refCatalog = SimpleCatalog(refSchema) 

copyIntoCatalog(catalog, refCatalog, sourcePrefix="ref_") 

 

srcSchema = makeMergedSchema( 

catalog.schema, SourceTable.makeMinimalSchema(), sourcePrefix="src_") 

srcCatalog = SourceCatalog(srcSchema) 

copyIntoCatalog(catalog, srcCatalog, sourcePrefix="src_") 

 

if sourceSlotConfig is not None: 

sourceSlotConfig.setupSchema(srcCatalog.schema) 

 

matches = [] 

distKey = catalog.schema.find("distance").key 

for ref, src, cat in zip(refCatalog, srcCatalog, catalog): 

matches.append(ReferenceMatch(ref, src, cat[distKey])) 

 

return matches