410 plot=dict(showFlags=False, showRejection=False)):
411
412 if not set:
413 set = psfexLib.Set(context)
414
415 cmin, cmax = None, None
416 if set.getNcontext():
417 cmin = np.empty(set.getNcontext())
418 cmax = np.empty(set.getNcontext())
419 for i in range(set.getNcontext()):
420 if set.getNsample():
421 cmin[i] = set.getContextOffset(i) - set.getContextScale(i)/2.0
422 cmax[i] = cmin[i] + set.getContextScale(i)
423 else:
424 cmin[i] = psfexLib.BIG
425 cmax[i] = -psfexLib.BIG
426
427
428
429 tab = afwTable.SourceCatalog.readFits(filename)
430
431 centroid = tab.getCentroidDefinition()
432 xm = tab.get("%s.x" % centroid)
433 ym = tab.get("%s.y" % centroid)
434
435 shape = tab.getShapeDefinition()
436 ixx = tab.get("%s.xx" % shape)
437 iyy = tab.get("%s.yy" % shape)
438
439 rmsSize = np.sqrt(0.5*(ixx + iyy))
440 elong = 0.5*(ixx - iyy)/(ixx + iyy)
441
442 flux = tab.get(prefs.getPhotfluxRkey())
443 fluxErr = tab.get(prefs.getPhotfluxerrRkey())
444 flags = getFlags(tab)
445
446
447
448
449 vigw, vigh = 35, 35
450 if set.empty():
451 set.setVigSize(vigw, vigh)
452
453 vignet = np.empty(nobj*vigw*vigh, "float32").reshape(nobj, vigw, vigh)
454
455
456 calexpFile = guessCalexp(filename)
457 mi = afwImage.MaskedImageF(calexpFile)
458 backnoise2 = np.median(mi.getVariance().getArray())
459 gain = 1.0
460
461 edgeBit = [k
for k, v
in getFlags().
items()
if v ==
"edge"][0]
462
463 for i, xc, yc in zip(range(nobj), xm, ym):
464 try:
465 x, y = int(xc), int(yc)
466 except ValueError:
467 flags[i] |= edgeBit
468
469 try:
470 pstamp = mi[x - vigw//2:x + vigw//2 + 1, y - vigh//2:y + vigh//2 + 1]
471 vignet[i] = pstamp.getImage().getArray().transpose()
472 except Exception:
473 flags[i] |= edgeBit
474
475
476 pc = 0
477 contextvalp = []
478 for i, key in enumerate(context.getName()):
479 if context.getPcflag(i):
480 contextvalp.append(pcval[pc])
481 pc += 1
482 elif key[0] == ':':
483 try:
484 contextvalp.append(tab.header[key[1:]])
485 except KeyError:
486 raise RuntimeError("*Error*: %s parameter not found in the header of %s" %
487 (key[1:], filename))
488 else:
489 try:
490 contextvalp.append(tab.get(key))
491 except KeyError:
492 raise RuntimeError("*Error*: %s parameter not found in the header of %s" %
493 (key, filename))
494 set.setContextname(i, key)
495
496
497 good = select_candidates(set, prefs, frmin, frmax,
498 flags, flux, fluxErr, rmsSize, elong, vignet,
499 plot=plot, title="%s[%d]" % (filename, ext + 1) if next > 1 else filename)
500
501
502
503 if not vignet.dtype.isnative:
504
505
506 vignet = vignet.byteswap()
507
508 for i in np.where(good)[0]:
509 sample = set.newSample()
510 sample.setCatindex(catindex)
511 sample.setExtindex(ext)
512
513 sample.setVig(vignet[i])
514 sample.setNorm(float(flux[i]))
515 sample.setBacknoise2(backnoise2)
516 sample.setGain(gain)
517 sample.setX(float(xm[i]))
518 sample.setY(float(ym[i]))
519 sample.setFluxrad(float(rmsSize[i]))
520
521 for j in range(set.getNcontext()):
522 sample.setContext(j, float(contextvalp[j][i]))
523
524 set.finiSample(sample, prefs.getProfAccuracy())
525
526
527 for j in range(set.getNcontext()):
528 cmin[j] = contextvalp[j][good].
min()
529 cmax[j] = contextvalp[j][good].
max()
530
531
532 if set.getNsample():
533 for i in range(set.getNcontext()):
534 set.setContextScale(i, cmax[i] - cmin[i])
535 set.setContextOffset(i, (cmin[i] + cmax[i])/2.0)
536
537
538 set.trimMemory()
539
540 return set
541
542
std::vector< SchemaItem< Flag > > * items