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

# 

# This file is part of ap_verify. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

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

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

# for details of code ownership. 

# 

# 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 GNU General Public License 

# along with this program. If not, see <http://www.gnu.org/licenses/>. 

# 

 

import os 

 

from lsst.daf.persistence import Butler 

import lsst.pex.exceptions as pexExcept 

from lsst.utils import getPackageDir 

 

from .config import Config 

 

 

class Dataset: 

"""A dataset supported by ``ap_verify``. 

 

Any object of this class is guaranteed to represent a ready-for-use 

dataset, barring concurrent changes to the file system or EUPS operations. 

Constructing a Dataset does not create a compatible output repository(ies), 

which can be done by calling `makeCompatibleRepo`. 

 

Parameters 

---------- 

datasetId : `str` 

A tag identifying the dataset. 

 

Raises 

------ 

RuntimeError 

Raised if `datasetId` exists, but is not correctly organized or incomplete 

ValueError 

Raised if `datasetId` is not a recognized dataset. No side effects if this 

exception is raised. 

""" 

 

def __init__(self, datasetId): 

# daf.persistence.Policy's behavior on missing keys is apparently undefined 

# test for __getattr__ *either* raising KeyError or returning None 

try: 

datasetPackage = self._getDatasetInfo()[datasetId] 

if datasetPackage is None: 

raise KeyError 

except KeyError: 

raise ValueError('Unsupported dataset: ' + datasetId) 

 

try: 

self._dataRootDir = getPackageDir(datasetPackage) 

except pexExcept.NotFoundError as e: 

error = 'Dataset %s requires the %s package, which has not been set up.' \ 

% (datasetId, datasetPackage) 

raise RuntimeError(error) from e 

else: 

self._validatePackage() 

 

self._initPackage(datasetPackage) 

 

def _initPackage(self, name): 

"""Prepare the package backing this dataset. 

 

Parameters 

---------- 

name : `str` 

The EUPS package identifier for the desired package. 

""" 

# No initialization required at present 

pass 

 

@staticmethod 

def getSupportedDatasets(): 

"""The dataset IDs that can be passed to this class's constructor. 

 

Returns 

------- 

datasets : `set` of `str` 

the set of IDs that will be accepted 

 

Raises 

------ 

IoError 

Raised if the config file does not exist or is not readable 

RuntimeError 

Raised if the config file exists, but does not contain the expected data 

""" 

return Dataset._getDatasetInfo().keys() 

 

@staticmethod 

def _getDatasetInfo(): 

"""Return external data on supported datasets. 

 

If an exception is raised, the program state shall be unchanged. 

 

Returns 

------- 

datasetToPackage : `dict`-like 

a map from dataset IDs to package names. 

 

Raises 

------ 

RuntimeError 

Raised if the config file exists, but does not contain the expected data 

""" 

return Config.instance['datasets'] 

 

@property 

def datasetRoot(self): 

"""The parent directory containing everything related to the dataset (`str`, read-only). 

""" 

return self._dataRootDir 

 

@property 

def rawLocation(self): 

"""The directory containing the "raw" input data (`str`, read-only). 

""" 

return os.path.join(self.datasetRoot, 'raw') 

 

@property 

def calibLocation(self): 

"""The directory containing the calibration data (`str`, read-only). 

""" 

return os.path.join(self.datasetRoot, 'calib') 

 

@property 

def refcatsLocation(self): 

"""The directory containing external astrometric and photometric 

reference catalogs (`str`, read-only). 

""" 

return os.path.join(self.datasetRoot, 'refcats') 

 

@property 

def templateLocation(self): 

"""The directory containing the image subtraction templates (`str`, read-only). 

""" 

return os.path.join(self.datasetRoot, 'templates') 

 

@property 

def configLocation(self): 

"""The directory containing configs that can be used to process the dataset (`str`, read-only). 

""" 

return os.path.join(self.datasetRoot, 'config') 

 

@property 

def obsPackage(self): 

"""The name of the obs package associated with this dataset (`str`, read-only). 

""" 

return Butler.getMapperClass(self._stubInputRepo).getPackageName() 

 

@property 

def camera(self): 

"""The name of the camera associated with this dataset (`str`, read-only). 

""" 

return Butler.getMapperClass(self._stubInputRepo).getCameraName() 

 

@property 

def _stubInputRepo(self): 

"""The directory containing the data set's input stub (`str`, read-only). 

""" 

return os.path.join(self.datasetRoot, 'repo') 

 

def _validatePackage(self): 

"""Confirm that the dataset directory satisfies all assumptions. 

 

Raises 

------ 

RuntimeError 

Raised if the package represented by this object does not conform to the 

dataset framework 

 

Notes 

----- 

Requires that `self._dataRootDir` has been initialized. 

""" 

if not os.path.exists(self.datasetRoot): 

raise RuntimeError('Could not find dataset at ' + self.datasetRoot) 

if not os.path.exists(self.rawLocation): 

raise RuntimeError('Dataset at ' + self.datasetRoot + 'is missing data directory') 

if not os.path.exists(self.calibLocation): 

raise RuntimeError('Dataset at ' + self.datasetRoot + 'is missing calibration directory') 

# Template and refcat directories might not be subdirectories of self.datasetRoot 

if not os.path.exists(self.templateLocation): 

raise RuntimeError('Dataset is missing template directory at ' + self.templateLocation) 

if not os.path.exists(self.refcatsLocation): 

raise RuntimeError('Dataset is missing reference catalog directory at ' + self.refcatsLocation) 

if not os.path.exists(self._stubInputRepo): 

raise RuntimeError('Dataset at ' + self.datasetRoot + 'is missing stub repo') 

if not _isRepo(self._stubInputRepo): 

raise RuntimeError('Stub repo at ' + self._stubInputRepo + 'is missing mapper file') 

 

def makeCompatibleRepo(self, repoDir, calibRepoDir): 

"""Set up a directory as a repository compatible with this dataset. 

 

If the directory already exists, any files required by the dataset will 

be added if absent; otherwise the directory will remain unchanged. 

 

Parameters 

---------- 

repoDir : `str` 

The directory where the output repository will be created. 

calibRepoDir : `str` 

The directory where the output calibration repository will be created. 

""" 

mapperArgs = {'mapperArgs': {'calibRoot': calibRepoDir}} 

if _isRepo(self.templateLocation): 

# Stub repo is not a parent because can't mix v1 and v2 repositories in parents list 

Butler(inputs=[{"root": self.templateLocation, "mode": "r"}], 

outputs=[{"root": repoDir, "mode": "rw", **mapperArgs}]) 

else: 

Butler(inputs=[{"root": self._stubInputRepo, "mode": "r"}], 

outputs=[{"root": repoDir, "mode": "rw", **mapperArgs}]) 

 

 

def _isRepo(repoDir): 

"""Test whether a directory has been set up as a repository. 

""" 

return os.path.exists(os.path.join(repoDir, '_mapper')) \ 

or os.path.exists(os.path.join(repoDir, 'repositoryCfg.yaml'))