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

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

# See COPYRIGHT file at the top of the source tree. 

# 

# This file is part of fgcmcal. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://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 <https://www.gnu.org/licenses/>. 

"""Run fgcmcal on a single tract. 

 

""" 

 

import sys 

import traceback 

 

import numpy as np 

 

import lsst.pex.config as pexConfig 

import lsst.pipe.base as pipeBase 

from lsst.jointcal.dataIds import PerTractCcdDataIdContainer 

 

from .fgcmBuildStars import FgcmBuildStarsTask 

from .fgcmFitCycle import FgcmFitCycleConfig 

from .fgcmOutputProducts import FgcmOutputProductsTask 

from .utilities import makeConfigDict, translateFgcmLut, translateVisitCatalog 

from .utilities import computeCcdOffsets 

from .utilities import makeZptSchema, makeZptCat 

from .utilities import makeAtmSchema, makeAtmCat 

from .utilities import makeStdSchema, makeStdCat 

 

import fgcm 

 

__all__ = ['FgcmCalibrateTractConfig', 'FgcmCalibrateTractTask', 'FgcmCalibrateTractRunner'] 

 

 

class FgcmCalibrateTractConfig(pexConfig.Config): 

"""Config for FgcmCalibrateTract""" 

 

fgcmBuildStars = pexConfig.ConfigurableField( 

target=FgcmBuildStarsTask, 

doc="Task to load and match stars for fgcm", 

) 

fgcmFitCycle = pexConfig.ConfigField( 

dtype=FgcmFitCycleConfig, 

doc="Config to run a single fgcm fit cycle", 

) 

fgcmOutputProducts = pexConfig.ConfigurableField( 

target=FgcmOutputProductsTask, 

doc="Task to output fgcm products", 

) 

convergenceTolerance = pexConfig.Field( 

doc="Tolerance on repeatability convergence (per band)", 

dtype=float, 

default=0.005, 

) 

maxFitCycles = pexConfig.Field( 

doc="Maximum number of fit cycles", 

dtype=int, 

default=5, 

) 

doDebuggingPlots = pexConfig.Field( 

doc="Make plots for debugging purposes?", 

dtype=bool, 

default=False, 

) 

 

def setDefaults(self): 

pexConfig.Config.setDefaults(self) 

 

self.fgcmBuildStars.checkAllCcds = True 

self.fgcmFitCycle.useRepeatabilityForExpGrayCuts = True 

self.fgcmFitCycle.quietMode = True 

self.fgcmOutputProducts.doReferenceCalibration = False 

self.fgcmOutputProducts.doRefcatOutput = False 

self.fgcmOutputProducts.cycleNumber = 0 

self.fgcmOutputProducts.photoCal.applyColorTerms = False 

 

 

class FgcmCalibrateTractRunner(pipeBase.ButlerInitializedTaskRunner): 

"""Subclass of TaskRunner for FgcmCalibrateTractTask 

 

fgcmCalibrateTractTask.run() takes a number of arguments, one of which is 

the butler (for persistence and mapper data), and a list of dataRefs 

extracted from the command line. This task runs on a constrained set 

of dataRefs, typically a single tract. 

This class transforms the process arguments generated by the ArgumentParser 

into the arguments expected by FgcmCalibrateTractTask.run(). 

This runner does not use any parallelization. 

""" 

 

@staticmethod 

def getTargetList(parsedCmd): 

""" 

Return a list with one element: a tuple with the butler and 

list of dataRefs 

""" 

# we want to combine the butler with any (or no!) dataRefs 

return [(parsedCmd.butler, parsedCmd.id.refList)] 

 

def __call__(self, args): 

""" 

Parameters 

---------- 

args: `tuple` with (butler, dataRefList) 

 

Returns 

------- 

exitStatus: `list` with `lsst.pipe.base.Struct` 

exitStatus (0: success; 1: failure) 

""" 

butler, dataRefList = args 

 

task = self.TaskClass(config=self.config, log=self.log) 

 

exitStatus = 0 

if self.doRaise: 

results = task.runDataRef(butler, dataRefList) 

else: 

try: 

results = task.runDataRef(butler, dataRefList) 

except Exception as e: 

exitStatus = 1 

task.log.fatal("Failed: %s" % e) 

if not isinstance(e, pipeBase.TaskError): 

traceback.print_exc(file=sys.stderr) 

 

task.writeMetadata(butler) 

 

if self.doReturnResults: 

return [pipeBase.Struct(exitStatus=exitStatus, 

results=results)] 

else: 

return [pipeBase.Struct(exitStatus=exitStatus)] 

 

def run(self, parsedCmd): 

""" 

Run the task, with no multiprocessing 

 

Parameters 

---------- 

parsedCmd: `lsst.pipe.base.ArgumentParser` parsed command line 

""" 

 

resultList = [] 

 

if self.precall(parsedCmd): 

targetList = self.getTargetList(parsedCmd) 

resultList = self(targetList[0]) 

 

return resultList 

 

 

class FgcmCalibrateTractTask(pipeBase.CmdLineTask): 

""" 

Calibrate a single tract using fgcmcal 

""" 

 

ConfigClass = FgcmCalibrateTractConfig 

RunnerClass = FgcmCalibrateTractRunner 

_DefaultName = "fgcmCalibrateTract" 

 

def __init__(self, butler=None, **kwargs): 

""" 

Instantiate an `FgcmCalibrateTractTask`. 

 

Parameters 

---------- 

butler : `lsst.daf.persistence.Butler` 

""" 

 

pipeBase.CmdLineTask.__init__(self, **kwargs) 

 

@classmethod 

def _makeArgumentParser(cls): 

"""Create an argument parser""" 

 

parser = pipeBase.ArgumentParser(name=cls._DefaultName) 

parser.add_id_argument("--id", "calexp", help="Data ID, e.g. --id visit=6789", 

ContainerClass=PerTractCcdDataIdContainer) 

 

return parser 

 

# no saving of metadata for now 

def _getMetadataName(self): 

return None 

 

@pipeBase.timeMethod 

def runDataRef(self, butler, dataRefs): 

""" 

Run full FGCM calibration on a single tract, including building star list, 

fitting multiple cycles, and making outputs. 

 

Parameters 

---------- 

butler: `lsst.daf.persistence.Butler` 

dataRefs: `list` of `lsst.daf.persistence.ButlerDataRef` 

Data references for the input visits. 

If this is an empty list, all visits with src catalogs in 

the repository are used. 

Only one individual dataRef from a visit need be specified 

and the code will find the other source catalogs from 

each visit. 

 

Raises 

------ 

RuntimeError: Raised if `config.fgcmBuildStars.doReferenceMatches` is 

not True, or if fgcmLookUpTable is not available. 

""" 

 

if not butler.datasetExists('fgcmLookUpTable'): 

raise RuntimeError("Must run FgcmCalibrateTract with an fgcmLookUpTable") 

 

if not self.config.fgcmBuildStars.doReferenceMatches: 

raise RuntimeError("Must run FgcmCalibrateTract with fgcmBuildStars.doReferenceMatches") 

 

self.makeSubtask("fgcmBuildStars", butler=butler) 

self.makeSubtask("fgcmOutputProducts", butler=butler) 

 

# Run the build stars tasks 

tract = dataRefs[0].dataId['tract'] 

self.log.info("Running on tract %d" % (tract)) 

 

# Note that we will need visitCat at the end of the procedure for the outputs 

groupedDataRefs = self.fgcmBuildStars.findAndGroupDataRefs(butler, dataRefs) 

visitCat = self.fgcmBuildStars.fgcmMakeVisitCatalog(butler, groupedDataRefs) 

fgcmStarObservationCat = self.fgcmBuildStars.fgcmMakeAllStarObservations(groupedDataRefs, 

visitCat) 

 

fgcmStarIdCat, fgcmStarIndicesCat, fgcmRefCat = \ 

self.fgcmBuildStars.fgcmMatchStars(butler, 

visitCat, 

fgcmStarObservationCat) 

 

# Load the LUT 

lutCat = butler.get('fgcmLookUpTable') 

fgcmLut, lutIndexVals, lutStd = translateFgcmLut(lutCat, 

dict(self.config.fgcmFitCycle.filterMap)) 

del lutCat 

 

# Translate the visit catalog into fgcm format 

fgcmExpInfo = translateVisitCatalog(visitCat) 

 

camera = butler.get('camera') 

configDict = makeConfigDict(self.config.fgcmFitCycle, self.log, camera, 

self.config.fgcmFitCycle.maxIterBeforeFinalCycle, 

True, False, tract=tract) 

# Turn off plotting in tract mode 

configDict['doPlots'] = False 

 

# Use the first orientation. 

# TODO: DM-21215 will generalize to arbitrary camera orientations 

ccdOffsets = computeCcdOffsets(camera, fgcmExpInfo['TELROT'][0]) 

del camera 

 

# Set up the fit cycle task 

 

noFitsDict = {'lutIndex': lutIndexVals, 

'lutStd': lutStd, 

'expInfo': fgcmExpInfo, 

'ccdOffsets': ccdOffsets} 

 

fgcmFitCycle = fgcm.FgcmFitCycle(configDict, useFits=False, 

noFitsDict=noFitsDict, noOutput=True) 

 

# We determine the conversion from the native units (typically radians) to 

# degrees for the first star. This allows us to treat coord_ra/coord_dec as 

# numpy arrays rather than Angles, which would we approximately 600x slower. 

conv = fgcmStarObservationCat[0]['ra'].asDegrees() / float(fgcmStarObservationCat[0]['ra']) 

 

# To load the stars, we need an initial parameter object 

fgcmPars = fgcm.FgcmParameters.newParsWithArrays(fgcmFitCycle.fgcmConfig, 

fgcmLut, 

fgcmExpInfo) 

 

# Match star observations to visits 

# Only those star observations that match visits from fgcmExpInfo['VISIT'] will 

# actually be transferred into fgcm using the indexing below. 

 

obsIndex = fgcmStarIndicesCat['obsIndex'] 

visitIndex = np.searchsorted(fgcmExpInfo['VISIT'], 

fgcmStarObservationCat['visit'][obsIndex]) 

 

fgcmStars = fgcm.FgcmStars(fgcmFitCycle.fgcmConfig) 

fgcmStars.loadStars(fgcmPars, 

fgcmStarObservationCat['visit'][obsIndex], 

fgcmStarObservationCat['ccd'][obsIndex], 

fgcmStarObservationCat['ra'][obsIndex] * conv, 

fgcmStarObservationCat['dec'][obsIndex] * conv, 

fgcmStarObservationCat['instMag'][obsIndex], 

fgcmStarObservationCat['instMagErr'][obsIndex], 

fgcmExpInfo['FILTERNAME'][visitIndex], 

fgcmStarIdCat['fgcm_id'][:], 

fgcmStarIdCat['ra'][:], 

fgcmStarIdCat['dec'][:], 

fgcmStarIdCat['obsArrIndex'][:], 

fgcmStarIdCat['nObs'][:], 

obsX=fgcmStarObservationCat['x'][obsIndex], 

obsY=fgcmStarObservationCat['y'][obsIndex], 

refID=fgcmRefCat['fgcm_id'][:], 

refMag=fgcmRefCat['refMag'][:, :], 

refMagErr=fgcmRefCat['refMagErr'][:, :], 

flagID=None, 

flagFlag=None, 

computeNobs=True) 

 

fgcmFitCycle.setLUT(fgcmLut) 

fgcmFitCycle.setStars(fgcmStars) 

 

# Clear out some memory 

# del fgcmStarObservationCat 

del fgcmStarIdCat 

del fgcmStarIndicesCat 

del fgcmRefCat 

 

converged = False 

cycleNumber = 0 

 

previousReservedRawRepeatability = np.zeros(fgcmPars.nBands) + 1000.0 

previousParInfo = None 

previousParams = None 

previousSuperStar = None 

 

while (not converged and cycleNumber < self.config.maxFitCycles): 

 

fgcmFitCycle.fgcmConfig.updateCycleNumber(cycleNumber) 

 

if cycleNumber > 0: 

# Use parameters from previous cycle 

fgcmPars = fgcm.FgcmParameters.loadParsWithArrays(fgcmFitCycle.fgcmConfig, 

fgcmExpInfo, 

previousParInfo, 

previousParams, 

previousSuperStar) 

# We need to reset the star magnitudes and errors for the next 

# cycle 

fgcmFitCycle.fgcmStars.reloadStarMagnitudes(fgcmStarObservationCat['instMag'][obsIndex], 

fgcmStarObservationCat['instMagErr'][obsIndex]) 

fgcmFitCycle.initialCycle = False 

 

fgcmFitCycle.setPars(fgcmPars) 

fgcmFitCycle.finishSetup() 

 

fgcmFitCycle.run() 

 

# Grab the parameters for the next cycle 

previousParInfo, previousParams = fgcmFitCycle.fgcmPars.parsToArrays() 

previousSuperStar = fgcmFitCycle.fgcmPars.parSuperStarFlat.copy() 

 

self.log.info("Raw repeatability after cycle number %d is:" % (cycleNumber)) 

for i, band in enumerate(fgcmFitCycle.fgcmPars.bands): 

rep = fgcmFitCycle.fgcmPars.compReservedRawRepeatability[i] * 1000.0 

self.log.info(" Band %s, repeatability: %.2f mmag" % (band, rep)) 

 

# Check for convergence 

if np.alltrue((previousReservedRawRepeatability - 

fgcmFitCycle.fgcmPars.compReservedRawRepeatability) < 

self.config.convergenceTolerance): 

self.log.info("Raw repeatability has converged after cycle number %d." % (cycleNumber)) 

converged = True 

else: 

fgcmFitCycle.fgcmConfig.expGrayPhotometricCut[:] = fgcmFitCycle.updatedPhotometricCut 

fgcmFitCycle.fgcmConfig.expGrayHighCut[:] = fgcmFitCycle.updatedHighCut 

fgcmFitCycle.fgcmConfig.precomputeSuperStarInitialCycle = False 

fgcmFitCycle.fgcmConfig.freezeStdAtmosphere = False 

previousReservedRawRepeatability[:] = fgcmFitCycle.fgcmPars.compReservedRawRepeatability 

self.log.info("Setting exposure gray photometricity cuts to:") 

for i, band in enumerate(fgcmFitCycle.fgcmPars.bands): 

cut = fgcmFitCycle.updatedPhotometricCut[i] * 1000.0 

self.log.info(" Band %s, photometricity cut: %.2f mmag" % (band, cut)) 

 

cycleNumber += 1 

 

# Log warning if not converged 

if not converged: 

self.log.warn("Maximum number of fit cycles exceeded (%d) without convergence." % (cycleNumber)) 

 

# Do final clean-up iteration 

fgcmFitCycle.fgcmConfig.freezeStdAtmosphere = False 

fgcmFitCycle.fgcmConfig.resetParameters = False 

fgcmFitCycle.fgcmConfig.maxIter = 0 

fgcmFitCycle.fgcmConfig.outputZeropoints = True 

fgcmFitCycle.fgcmConfig.outputStandards = True 

fgcmFitCycle.fgcmConfig.doPlots = self.config.doDebuggingPlots 

fgcmFitCycle.fgcmConfig.updateCycleNumber(cycleNumber) 

fgcmFitCycle.initialCycle = False 

 

fgcmPars = fgcm.FgcmParameters.loadParsWithArrays(fgcmFitCycle.fgcmConfig, 

fgcmExpInfo, 

previousParInfo, 

previousParams, 

previousSuperStar) 

fgcmFitCycle.fgcmStars.reloadStarMagnitudes(fgcmStarObservationCat['instMag'][obsIndex], 

fgcmStarObservationCat['instMagErr'][obsIndex]) 

fgcmFitCycle.setPars(fgcmPars) 

fgcmFitCycle.finishSetup() 

 

self.log.info("Running final clean-up fit cycle...") 

fgcmFitCycle.run() 

 

self.log.info("Raw repeatability after clean-up cycle is:") 

for i, band in enumerate(fgcmFitCycle.fgcmPars.bands): 

rep = fgcmFitCycle.fgcmPars.compReservedRawRepeatability[i] * 1000.0 

self.log.info(" Band %s, repeatability: %.2f mmag" % (band, rep)) 

 

# Do the outputs. Need to keep track of tract, blah. 

 

if self.config.fgcmFitCycle.superStarSubCcd or self.config.fgcmFitCycle.ccdGraySubCcd: 

chebSize = fgcmFitCycle.fgcmZpts.zpStruct['FGCM_FZPT_CHEB'].shape[1] 

else: 

chebSize = 0 

 

zptSchema = makeZptSchema(chebSize) 

zptCat = makeZptCat(zptSchema, fgcmFitCycle.fgcmZpts.zpStruct) 

 

atmSchema = makeAtmSchema() 

atmCat = makeAtmCat(atmSchema, fgcmFitCycle.fgcmZpts.atmStruct) 

 

stdSchema = makeStdSchema(fgcmFitCycle.fgcmPars.nBands) 

stdStruct = fgcmFitCycle.fgcmStars.retrieveStdStarCatalog(fgcmFitCycle.fgcmPars) 

stdCat = makeStdCat(stdSchema, stdStruct) 

 

outStruct = self.fgcmOutputProducts.generateOutputProducts(butler, tract, 

visitCat, 

zptCat, atmCat, stdCat, 

self.config.fgcmBuildStars, 

self.config.fgcmFitCycle) 

outStruct.repeatability = fgcmFitCycle.fgcmPars.compReservedRawRepeatability 

 

return outStruct