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

1import numpy as np 

2import astropy.units as u 

3 

4from lsst.faro.utils.filtermatches import filterMatches 

5 

6__all__ = ("photRepeat", "calcPhotRepeat") 

7 

8 

9def photRepeat(matchedCatalog, magName=None, nMinPhotRepeat=50, **filterargs): 

10 """Measure the photometric repeatability of a set of observations. 

11 

12 Parameters 

13 ---------- 

14 matchedCatalog : `lsst.afw.table.base.Catalog` 

15 `~lsst.afw.table.base.Catalog` object as created by 

16 `~lsst.afw.table.multiMatch` matching of sources from multiple visits. 

17 magName : `str` 

18 Name of the magnitude field. Default "slot_PsfFlux_mag". 

19 nMinPhotRepeat : `int` 

20 Minimum number of sources required to return a measurement. 

21 **filterargs 

22 Additional arguments to pass to `filterMatches` for catalog filtering. 

23 

24 Returns 

25 ------- 

26 photo_resid_meas : `dict` 

27 Photometric repeatability statistics and related quantities as measured 

28 by `calcPhotRepeat`. Returns NaN values if there are fewer than 

29 nMinPhotRepeat sources in the matched catalog. 

30 """ 

31 if magName is None: 

32 magName = 'slot_PsfFlux_mag' 

33 filteredCat = filterMatches(matchedCatalog, **filterargs) 

34 magKey = filteredCat.schema.find(magName).key 

35 

36 # Require at least nMinPhotRepeat objects to calculate the repeatability: 

37 if filteredCat.count > nMinPhotRepeat: 

38 phot_resid_meas = calcPhotRepeat(filteredCat, magKey) 

39 # Check that the number of stars with >2 visits is >nMinPhotRepeat: 

40 okcount = (phot_resid_meas['count'] > 2) 

41 if np.sum(okcount) > nMinPhotRepeat: 

42 return phot_resid_meas 

43 else: 

44 return {'nomeas': np.nan*u.mmag} 

45 else: 

46 return {'nomeas': np.nan*u.mmag} 

47 

48 

49def calcPhotRepeat(matches, magKey): 

50 """Calculate the photometric repeatability of a set of measurements. 

51 

52 Parameters 

53 ---------- 

54 matches : `lsst.afw.table.GroupView` 

55 `~lsst.afw.table.GroupView` of sources matched between visits using 

56 MultiMatch, as provided by `lsst.faro.utils.matcher.matchCatalogs`. 

57 magKey : `lsst.afw.table` schema key 

58 Magnitude column key in the ``GroupView``. 

59 E.g., ``magKey = allMatches.schema.find("slot_ModelFlux_mag").key`` 

60 where ``allMatches`` is the result of `lsst.afw.table.MultiMatch.finish()`. 

61 

62 Returns 

63 ------- 

64 statistics : `dict` 

65 Repeatability statistics and ancillary quantities calculated from the 

66 input ``GroupView`` matched catalog. Fields are: 

67 - ``count``: array of number of nonzero magnitude measurements for each 

68 input source. 

69 - ``magMean``: `~astropy.unit.Quantity` array of mean magnitudes, in mag, 

70 for each input source. 

71 - ``rms``: `~astropy.unit.Quantity` array of RMS photometric repeatability 

72 about the mean, in mmag, for each input source. 

73 - ``repeatability``: scalar `~astropy.unit.Quantity` of the median ``rms``. 

74 This is calculated using all sources with more than 2 magnitude 

75 measurements, and reported in mmag. 

76 - ``magResid``: `~astropy.unit.Quantity` array for each input source, 

77 containing the magnitude residuals, in mmag, with respect to ``magMean``. 

78 """ 

79 matches_rms = (matches.aggregate(np.nanstd, field=magKey)*u.mag).to(u.mmag) 

80 matches_count = matches.aggregate(np.count_nonzero, field=magKey) 

81 matches_mean = matches.aggregate(np.mean, field=magKey)*u.mag 

82 magResid = [] 

83 for gp in matches.groups: 

84 magResid.append(((gp[magKey]-np.mean(gp[magKey]))*(u.mag)).to(u.mmag)) 

85 magResid = np.array(magResid, dtype='object') 

86 okrms = (matches_count > 2) 

87 if np.sum(okrms) > 0: 

88 return {'count': matches_count, 'magMean': matches_mean, 'rms': matches_rms, 

89 'repeatability': np.median(matches_rms[okrms]), 'magResid': magResid} 

90 else: 

91 return {'count': 0, 'magMean': np.nan*u.mag, 'rms': np.nan*u.mmag, 

92 'repeatability': np.nan*u.mmag, 'magDiffs': 0*u.mmag}