LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
data_factory.py
Go to the documentation of this file.
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 
22 from __future__ import annotations
23 
24 import numpy
25 import pandas
26 import random
27 from typing import Iterator
28 
29 from lsst.daf.base import DateTime
30 from lsst.sphgeom import LonLat, Region, UnitVector3d
31 from lsst.geom import SpherePoint
32 
33 
34 def _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 
64 def 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 
96 def 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 
136 def 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
Point in an unspecified spherical coordinate system.
Definition: SpherePoint.h:57
UnitVector3d is a unit vector in ℝ³ with components stored in double precision.
Definition: UnitVector3d.h:55
daf::base::PropertyList * list
Definition: fits.cc:913
pandas.DataFrame makeSourceCatalog(pandas.DataFrame objects, DateTime visit_time, int start_id=0, int ccdVisitId=1)
Definition: data_factory.py:97
pandas.DataFrame makeObjectCatalog(Region region, int count)
Definition: data_factory.py:64
pandas.DataFrame makeForcedSourceCatalog(pandas.DataFrame objects, DateTime visit_time, int ccdVisitId=1)