Coverage for python/lsst/sims/utils/samplingFunctions.py : 18%

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
1from __future__ import division
2import numpy as np
3import warnings
5__all__ = ['spatiallySample_obsmetadata', 'samplePatchOnSphere']
8def spatiallySample_obsmetadata(obsmetadata, size=1, seed=1):
9 """
10 Sample a square patch on the sphere overlapping obsmetadata
11 field of view by picking the area enclosed in
12 obsmetadata.pointingRA \pm obsmetadata.boundLength
13 obsmetadata.pointingDec \pm obsmetadata.boundLength
15 Parameters
16 ----------
17 obsmetadata: instance of
18 `sims.catalogs.generation.db.ObservationMetaData`
19 size: integer, optional, defaults to 1
20 number of samples
22 seed: integer, optional, defaults to 1
23 Random Seed used in generating random values
24 Returns
25 -------
26 tuple of ravals, decvalues in radians
27 """
29 phi = obsmetadata.pointingRA
30 theta = obsmetadata.pointingDec
32 if obsmetadata.boundType != 'box':
33 warnings.warn('Warning: sampling obsmetata with provided boundLen and'
34 'boundType="box", despite diff boundType specified\n')
35 equalrange = obsmetadata.boundLength
36 ravals, thetavals = samplePatchOnSphere(phi=phi,
37 theta=theta,
38 delta=equalrange,
39 size=size,
40 seed=seed)
41 return ravals, thetavals
44def samplePatchOnSphere(phi, theta, delta, size, seed=1):
45 """
46 Uniformly distributes samples on a patch on a sphere between phi \pm delta,
47 and theta \pm delta on a sphere. Uniform distribution implies that the
48 number of points in a patch of sphere is proportional to the area of the
49 patch. Here, the coordinate system is the usual
50 spherical coordinate system but with the azimuthal angle theta going from
51 90 degrees at the North Pole, to -90 degrees at the South Pole, through
52 0. at the equator.
54 This function is not equipped to handle wrap-around the ranges of theta
55 phi and therefore does not work at the poles.
57 Parameters
58 ----------
59 phi: float, mandatory, degrees
60 center of the spherical patch in ra with range
61 theta: float, mandatory, degrees
62 delta: float, mandatory, degrees
63 size: int, mandatory
64 number of samples
65 seed : int, optional, defaults to 1
66 random Seed used for generating values
67 Returns
68 -------
69 tuple of (phivals, thetavals) where phivals and thetavals are arrays of
70 size size in degrees.
71 """
72 np.random.seed(seed)
73 u = np.random.uniform(size=size)
74 v = np.random.uniform(size=size)
76 phi = np.radians(phi)
77 theta = np.radians(theta)
78 delta = np.radians(delta)
80 phivals = 2. * delta * u + (phi - delta)
81 phivals = np.where(phivals >= 0., phivals, phivals + 2. * np.pi)
83 # use conventions in spherical coordinates
84 theta = np.pi / 2.0 - theta
86 thetamax = theta + delta
87 thetamin = theta - delta
89 if thetamax > np.pi or thetamin < 0.:
90 raise ValueError('Function not implemented to cover wrap around poles')
92 # Cumulative Density Function is cos(thetamin) - cos(theta) /
93 # cos(thetamin) - cos(thetamax)
94 a = np.cos(thetamin) - np.cos(thetamax)
95 thetavals = np.arccos(-v * a + np.cos(thetamin))
97 # Get back to -pi/2 to pi/2 range of decs
98 thetavals = np.pi / 2.0 - thetavals
99 return np.degrees(phivals), np.degrees(thetavals)