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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

from __future__ import division 

import numpy as np 

import warnings 

 

__all__ = ['spatiallySample_obsmetadata', 'samplePatchOnSphere'] 

 

 

def spatiallySample_obsmetadata(obsmetadata, size=1, seed=1): 

""" 

Sample a square patch on the sphere overlapping obsmetadata 

field of view by picking the area enclosed in 

obsmetadata.pointingRA \pm obsmetadata.boundLength 

obsmetadata.pointingDec \pm obsmetadata.boundLength 

 

Parameters 

---------- 

obsmetadata: instance of 

`sims.catalogs.generation.db.ObservationMetaData` 

size: integer, optional, defaults to 1 

number of samples 

 

seed: integer, optional, defaults to 1 

Random Seed used in generating random values 

Returns 

------- 

tuple of ravals, decvalues in radians 

""" 

 

phi = obsmetadata.pointingRA 

theta = obsmetadata.pointingDec 

 

if obsmetadata.boundType != 'box': 

warnings.warn('Warning: sampling obsmetata with provided boundLen and' 

'boundType="box", despite diff boundType specified\n') 

equalrange = obsmetadata.boundLength 

ravals, thetavals = samplePatchOnSphere(phi=phi, 

theta=theta, 

delta=equalrange, 

size=size, 

seed=seed) 

return ravals, thetavals 

 

 

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

""" 

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

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

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

patch. Here, the coordinate system is the usual 

spherical coordinate system but with the azimuthal angle theta going from 

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

0. at the equator. 

 

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

phi and therefore does not work at the poles. 

 

Parameters 

---------- 

phi: float, mandatory, degrees 

center of the spherical patch in ra with range 

theta: float, mandatory, degrees 

delta: float, mandatory, degrees 

size: int, mandatory 

number of samples 

seed : int, optional, defaults to 1 

random Seed used for generating values 

Returns 

------- 

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

size size in degrees. 

""" 

np.random.seed(seed) 

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

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

 

phi = np.radians(phi) 

theta = np.radians(theta) 

delta = np.radians(delta) 

 

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

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

 

# use conventions in spherical coordinates 

theta = np.pi / 2.0 - theta 

 

thetamax = theta + delta 

thetamin = theta - delta 

 

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

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

 

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

# cos(thetamin) - cos(thetamax) 

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

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

 

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

thetavals = np.pi / 2.0 - thetavals 

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