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

1from __future__ import division 

2import numpy as np 

3import warnings 

4 

5__all__ = ['spatiallySample_obsmetadata', 'samplePatchOnSphere', 'uniformSphere'] 

6 

7 

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 

14 

15 Parameters 

16 ---------- 

17 obsmetadata: instance of 

18 `sims.catalogs.generation.db.ObservationMetaData` 

19 size: integer, optional, defaults to 1 

20 number of samples 

21 

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 """ 

28 

29 phi = obsmetadata.pointingRA 

30 theta = obsmetadata.pointingDec 

31 

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 

42 

43 

44def uniformSphere(npoints, seed=42): 

45 """ 

46 Just make RA, dec points on a sphere 

47 """ 

48 np.random.seed(seed) 

49 u = np.random.uniform(size=npoints) 

50 v = np.random.uniform(size=npoints) 

51 

52 ra = 2.*np.pi * u 

53 dec = np.arccos(2.*v - 1.) 

54 # astro convention of -90 to 90 

55 dec -= np.pi/2. 

56 return np.degrees(ra), np.degrees(dec) 

57 

58 

59def samplePatchOnSphere(phi, theta, delta, size, seed=1): 

60 """ 

61 Uniformly distributes samples on a patch on a sphere between phi \pm delta, 

62 and theta \pm delta on a sphere. Uniform distribution implies that the 

63 number of points in a patch of sphere is proportional to the area of the 

64 patch. Here, the coordinate system is the usual 

65 spherical coordinate system but with the azimuthal angle theta going from 

66 90 degrees at the North Pole, to -90 degrees at the South Pole, through 

67 0. at the equator. 

68 

69 This function is not equipped to handle wrap-around the ranges of theta 

70 phi and therefore does not work at the poles. 

71 

72 Parameters 

73 ---------- 

74 phi: float, mandatory, degrees 

75 center of the spherical patch in ra with range 

76 theta: float, mandatory, degrees 

77 delta: float, mandatory, degrees 

78 size: int, mandatory 

79 number of samples 

80 seed : int, optional, defaults to 1 

81 random Seed used for generating values 

82 Returns 

83 ------- 

84 tuple of (phivals, thetavals) where phivals and thetavals are arrays of 

85 size size in degrees. 

86 """ 

87 np.random.seed(seed) 

88 u = np.random.uniform(size=size) 

89 v = np.random.uniform(size=size) 

90 

91 phi = np.radians(phi) 

92 theta = np.radians(theta) 

93 delta = np.radians(delta) 

94 

95 phivals = 2. * delta * u + (phi - delta) 

96 phivals = np.where(phivals >= 0., phivals, phivals + 2. * np.pi) 

97 

98 # use conventions in spherical coordinates 

99 theta = np.pi / 2.0 - theta 

100 

101 thetamax = theta + delta 

102 thetamin = theta - delta 

103 

104 if thetamax > np.pi or thetamin < 0.: 

105 raise ValueError('Function not implemented to cover wrap around poles') 

106 

107 # Cumulative Density Function is cos(thetamin) - cos(theta) / 

108 # cos(thetamin) - cos(thetamax) 

109 a = np.cos(thetamin) - np.cos(thetamax) 

110 thetavals = np.arccos(-v * a + np.cos(thetamin)) 

111 

112 # Get back to -pi/2 to pi/2 range of decs 

113 thetavals = np.pi / 2.0 - thetavals 

114 return np.degrees(phivals), np.degrees(thetavals)