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# This file is part of dax_apdb. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

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

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

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

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

21 

22from __future__ import annotations 

23 

24import numpy 

25import pandas 

26import random 

27from typing import Iterator 

28 

29from lsst.daf.base import DateTime 

30from lsst.sphgeom import LonLat, Region, UnitVector3d 

31from lsst.geom import SpherePoint 

32 

33 

34def _genPointsInRegion(region: Region, count: int) -> Iterator[SpherePoint]: 

35 """Generate bunch of SpherePoints inside given region. 

36 

37 Parameters 

38 ---------- 

39 region : `lsst.sphgeom.Region` 

40 Spherical region. 

41 count : `int` 

42 Number of points to generate. 

43 

44 Notes 

45 ----- 

46 Returned points are random but not necessarily uniformly distributed. 

47 """ 

48 bbox = region.getBoundingBox() 

49 center = bbox.getCenter() 

50 center_lon = center.getLon().asRadians() 

51 center_lat = center.getLat().asRadians() 

52 width = bbox.getWidth().asRadians() 

53 height = bbox.getHeight().asRadians() 

54 while count > 0: 

55 lon = random.uniform(center_lon - width / 2, center_lon + width / 2) 

56 lat = random.uniform(center_lat - height / 2, center_lat + height / 2) 

57 lonlat = LonLat.fromRadians(lon, lat) 

58 uv3d = UnitVector3d(lonlat) 

59 if region.contains(uv3d): 

60 yield SpherePoint(lonlat) 

61 count -= 1 

62 

63 

64def makeObjectCatalog(region: Region, count: int) -> pandas.DataFrame: 

65 """Make a catalog containing a bunch of DiaObjects inside a region. 

66 

67 Parameters 

68 ---------- 

69 region : `lsst.sphgeom.Region` 

70 Spherical region. 

71 count : `int` 

72 Number of records to generate. 

73 

74 Returns 

75 ------- 

76 catalog : `pandas.DataFrame` 

77 Catalog of DiaObjects records. 

78 

79 Notes 

80 ----- 

81 Returned catalog only contains three columns - ``diaObjectId`, ``ra``, and 

82 ``decl`` (in degrees). 

83 """ 

84 points = list(_genPointsInRegion(region, count)) 

85 # diaObjectId=0 may be used in some code for DiaSource foreign key to mean 

86 # the same as ``None``. 

87 ids = numpy.arange(1, len(points) + 1, dtype=numpy.int64) 

88 ras = numpy.array([sp.getRa().asDegrees() for sp in points], dtype=numpy.float64) 

89 decls = numpy.array([sp.getDec().asDegrees() for sp in points], dtype=numpy.float64) 

90 df = pandas.DataFrame({"diaObjectId": ids, 

91 "ra": ras, 

92 "decl": decls}) 

93 return df 

94 

95 

96def makeSourceCatalog(objects: pandas.DataFrame, visit_time: DateTime, 

97 start_id: int = 0, ccdVisitId: int = 1) -> pandas.DataFrame: 

98 """Make a catalog containing a bunch of DiaSources associated with the 

99 input DiaObjects. 

100 

101 Parameters 

102 ---------- 

103 objects : `pandas.DataFrame` 

104 Catalog of DiaObject records. 

105 visit_time : `lsst.daf.base.DateTime` 

106 Time of the visit. 

107 start_id : `int` 

108 Starting value for ``diaObjectId``. 

109 ccdVisitId : `int` 

110 Value for ``ccdVisitId`` field. 

111 

112 Returns 

113 ------- 

114 catalog : `pandas.DataFrame` 

115 Catalog of DiaSource records. 

116 

117 Notes 

118 ----- 

119 Returned catalog only contains small number of columns needed for tests. 

120 """ 

121 nrows = len(objects) 

122 midPointTai = visit_time.get(system=DateTime.MJD) 

123 df = pandas.DataFrame({ 

124 "diaSourceId": numpy.arange(start_id, start_id + nrows, dtype=numpy.int64), 

125 "diaObjectId": objects["diaObjectId"], 

126 "ccdVisitId": numpy.full(nrows, ccdVisitId, dtype=numpy.int64), 

127 "parentDiaSourceId": 0, 

128 "ra": objects["ra"], 

129 "decl": objects["decl"], 

130 "midPointTai": numpy.full(nrows, midPointTai, dtype=numpy.float64), 

131 "flags": numpy.full(nrows, 0, dtype=numpy.int64), 

132 }) 

133 return df 

134 

135 

136def makeForcedSourceCatalog(objects: pandas.DataFrame, visit_time: DateTime, 

137 ccdVisitId: int = 1) -> pandas.DataFrame: 

138 """Make a catalog containing a bunch of DiaFourceSources associated with 

139 the input DiaObjects. 

140 

141 Parameters 

142 ---------- 

143 objects : `pandas.DataFrame` 

144 Catalog of DiaObject records. 

145 visit_time : `lsst.daf.base.DateTime` 

146 Time of the visit. 

147 ccdVisitId : `int` 

148 Value for ``ccdVisitId`` field. 

149 

150 Returns 

151 ------- 

152 catalog : `pandas.DataFrame` 

153 Catalog of DiaForcedSource records. 

154 

155 Notes 

156 ----- 

157 Returned catalog only contains small number of columns needed for tests. 

158 """ 

159 nrows = len(objects) 

160 midPointTai = visit_time.get(system=DateTime.MJD) 

161 df = pandas.DataFrame({ 

162 "diaObjectId": objects["diaObjectId"], 

163 "ccdVisitId": numpy.full(nrows, ccdVisitId, dtype=numpy.int64), 

164 "midPointTai": numpy.full(nrows, midPointTai, dtype=numpy.float64), 

165 "flags": numpy.full(nrows, 0, dtype=numpy.int64), 

166 }) 

167 return df