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

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010 LSST Corporation. 

# 

# 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 <http://www.lsstcorp.org/LegalNotices/>. 

# 

""" 

@todo 

- Consider tweaking pixel scale so the average scale is as specified, rather than the scale at the center 

""" 

import hashlib 

import struct 

 

import lsst.pex.config as pexConfig 

from lsst.geom import SpherePoint, Angle, arcseconds, degrees 

from . import detail 

 

__all__ = ["BaseSkyMap"] 

 

 

class BaseSkyMapConfig(pexConfig.Config): 

patchInnerDimensions = pexConfig.ListField( 

doc="dimensions of inner region of patches (x,y pixels)", 

dtype=int, 

length=2, 

default=(4000, 4000), 

) 

patchBorder = pexConfig.Field( 

doc="border between patch inner and outer bbox (pixels)", 

dtype=int, 

default=100, 

) 

tractOverlap = pexConfig.Field( 

doc="minimum overlap between adjacent sky tracts, on the sky (deg)", 

dtype=float, 

default=1.0, 

) 

pixelScale = pexConfig.Field( 

doc="nominal pixel scale (arcsec/pixel)", 

dtype=float, 

default=0.333 

) 

projection = pexConfig.Field( 

doc="""one of the FITS WCS projection codes, such as: 

- STG: stereographic projection 

- MOL: Molleweide's projection 

- TAN: tangent-plane projection 

""", 

dtype=str, 

default="STG", 

) 

rotation = pexConfig.Field( 

doc="Rotation for WCS (deg)", 

dtype=float, 

default=0, 

) 

 

 

class BaseSkyMap: 

"""A collection of overlapping Tracts that map part or all of the sky. 

 

See TractInfo for more information. 

 

BaseSkyMap is an abstract base class. Subclasses must do the following: 

@li define __init__ and have it construct the TractInfo objects and put them in _tractInfoList 

@li define __getstate__ and __setstate__ to allow pickling (the butler saves sky maps using pickle); 

see DodecaSkyMap for an example of how to do this. (Most of that code could be moved 

into this base class, but that would make it harder to handle older versions of pickle data.) 

@li define updateSha1 to add any subclass-specific state to the hash. 

 

All SkyMap subclasses must be conceptually immutable; they must always 

refer to the same set of mathematical tracts and patches even if the in- 

memory representation of those objects changes. 

""" 

ConfigClass = BaseSkyMapConfig 

 

def __init__(self, config=None): 

"""Construct a BaseSkyMap 

 

@param[in] config: an instance of self.ConfigClass; if None the default config is used 

""" 

if config is None: 

config = self.ConfigClass() 

config.freeze() # just to be sure, e.g. for pickling 

self.config = config 

self._tractInfoList = [] 

self._wcsFactory = detail.WcsFactory( 

pixelScale=Angle(self.config.pixelScale, arcseconds), 

projection=self.config.projection, 

rotation=Angle(self.config.rotation, degrees), 

) 

self._sha1 = None 

 

def findTract(self, coord): 

"""Find the tract whose center is nearest the specified coord. 

 

@param[in] coord: ICRS sky coordinate (lsst.afw.geom.SpherePoint) 

@return TractInfo of tract whose center is nearest the specified coord 

 

@warning: 

- if tracts do not cover the whole sky then the returned tract may not include the coord 

 

@note 

- This routine will be more efficient if coord is ICRS. 

- If coord is equidistant between multiple sky tract centers then one is arbitrarily chosen. 

- The default implementation is not very efficient; subclasses may wish to override. 

""" 

distTractInfoList = [] 

for i, tractInfo in enumerate(self): 

angSep = coord.separation(tractInfo.getCtrCoord()).asDegrees() 

# include index in order to disambiguate identical angSep values 

distTractInfoList.append((angSep, i, tractInfo)) 

distTractInfoList.sort() 

return distTractInfoList[0][2] 

 

def findTractPatchList(self, coordList): 

"""Find tracts and patches that overlap a region 

 

@param[in] coordList: list of ICRS sky coordinates (lsst.afw.geom.SpherePoint) 

@return list of (TractInfo, list of PatchInfo) for tracts and patches that contain, 

or may contain, the specified region. The list will be empty if there is no overlap. 

 

@warning this uses a naive algorithm that may find some tracts and patches that do not overlap 

the region (especially if the region is not a rectangle aligned along patch x,y). 

""" 

retList = [] 

for tractInfo in self: 

patchList = tractInfo.findPatchList(coordList) 

if patchList: 

retList.append((tractInfo, patchList)) 

return retList 

 

def findClosestTractPatchList(self, coordList): 

"""Find closest tract and patches that overlap coordinates 

 

@param[in] coordList: list of ICRS sky coordinates (lsst.afw.geom.SpherePoint) 

@return list of (TractInfo, list of PatchInfo) for tracts and patches that contain, 

or may contain, the specified region. The list will be empty if there is no overlap. 

""" 

retList = [] 

for coord in coordList: 

tractInfo = self.findTract(coord) 

patchList = tractInfo.findPatchList(coordList) 

if patchList and not (tractInfo, patchList) in retList: 

retList.append((tractInfo, patchList)) 

return retList 

 

def __getitem__(self, ind): 

return self._tractInfoList[ind] 

 

def __iter__(self): 

return iter(self._tractInfoList) 

 

def __len__(self): 

return len(self._tractInfoList) 

 

def __hash__(self): 

return hash(self.getSha1()) 

 

def __eq__(self, other): 

try: 

return self.getSha1() == other.getSha1() 

except AttributeError: 

return NotImplemented 

 

def __ne__(self, other): 

return not (self == other) 

 

def getSha1(self): 

"""Return a SHA1 hash that uniquely identifies this SkyMap instance. 

 

Returns 

------- 

sha1 : bytes 

A 20-byte hash that uniquely identifies this SkyMap instance. 

 

Subclasses should almost always override `updateSha1()` instead of 

this function to add subclass-specific state to the hash. 

""" 

if self._sha1 is None: 

sha1 = hashlib.sha1() 

sha1.update(type(self).__name__.encode('utf-8')) 

configPacked = struct.pack( 

"<iiidd3sd", 

self.config.patchInnerDimensions[0], 

self.config.patchInnerDimensions[1], 

self.config.patchBorder, 

self.config.tractOverlap, 

self.config.pixelScale, 

self.config.projection.encode('ascii'), 

self.config.rotation 

) 

sha1.update(configPacked) 

self.updateSha1(sha1) 

self._sha1 = sha1.digest() 

return self._sha1 

 

def updateSha1(self, sha1): 

"""Add subclass-specific state or configuration options to the SHA1. 

 

Parameters 

---------- 

sha1 : hashlib.sha1 

A hashlib object on which `update()` can be called to add 

additional state to the hash. 

 

This method is conceptually "protected": it should be reimplemented by 

all subclasses, but called only by the base class implementation of 

`getSha1()`. 

""" 

raise NotImplementedError() 

 

def register(self, name, registry): 

"""Add SkyMap, Tract, and Patch Dimension entries to the given Gen3 

Butler Registry. 

""" 

registry.addDimensionEntry("SkyMap", {"skymap": name, "hash": self.getSha1()}) 

for tractInfo in self: 

region = tractInfo.getOuterSkyPolygon() 

centroid = SpherePoint(region.getCentroid()) 

registry.addDimensionEntry( 

"Tract", 

{"skymap": name, "tract": tractInfo.getId(), 

"region": region, 

"ra": centroid.getRa().asDegrees(), 

"dec": centroid.getDec().asDegrees()} 

) 

for patchInfo in tractInfo: 

cellX, cellY = patchInfo.getIndex() 

registry.addDimensionEntry( 

"Patch", 

{"skymap": name, "tract": tractInfo.getId(), 

"patch": tractInfo.getSequentialPatchIndex(patchInfo), 

"cell_x": cellX, "cell_y": cellY, 

"region": patchInfo.getOuterSkyPolygon(tractInfo.getWcs())} 

)