-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.py
106 lines (88 loc) · 3.05 KB
/
sim.py
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
100
101
102
103
104
105
106
import sys
import os
import math
import numpy as np
import time
import galsim
import pyfits as pf
# simulations 2, all spots at center
def main(argv):
# NL function
def f(x, beta): return x - beta*x*x
stamp_size = 64 # power of 2, please
n_tiles = 2048/stamp_size
pixel_scale = 1.0
n_res = 18 # Chaz's file is 1 micron
random_seed = 3339201
if not os.path.isdir('output'):
os.mkdir('output')
file_name = os.path.join('output', 'h2rg_sim_offset_norm_0_point_1.fits')
print("hola")
nobj = n_tiles*n_tiles
rng = galsim.BaseDeviate(random_seed+nobj)
# Setup the images:
psf_image = galsim.ImageF(stamp_size * n_tiles, stamp_size * n_tiles)
# Read PSF model
psf_file = os.path.join(
'data', 'chazPSF_lamda1_cd3_f11_pix1_noboxcar.fits')
psf_im = galsim.fits.read(psf_file)
psf = galsim.InterpolatedImage(psf_im, scale=pixel_scale, flux=1.)
print("hola 2")
ix_list = []
iy_list = []
for ix in range(n_tiles):
for iy in range(n_tiles):
ix_list.append(ix)
iy_list.append(iy)
# Build each postage stamp:
f = open("sim_spots_position_random_norm_0_point_1.txt", 'w')
for k in range(nobj):
rng = galsim.BaseDeviate(random_seed+k)
# Determine the bounds for this stamp and its center position.
ix = ix_list[k]
iy = iy_list[k]
b = galsim.BoundsI(ix*stamp_size+1, (ix+1)*stamp_size,
iy*stamp_size+1, (iy+1)*stamp_size)
sub_psf_image = psf_image[b]
print(" ")
print("Center in big image: ", b.center().x, b.center().y)
#line="%g %g \n" %(b.center().x, b.center().y)
#f.write (line)
#ud = galsim.UniformDeviate(rng)
# Following lines to test effect of miscentering on B histogram
random = np.random.uniform(low=-0.1, high=0.1)
x = random
y = np.sqrt(0.1**2 - x**2)
offset = (x, y)
#offset=(ud(), ud())
#offset=(0.0, 0.0)
# if k%2==0:
# offset=(0.0,0.0)
# else:
# offset=(0.5,0.5)
# ud = galsim.UniformDeviate(rng)
# offset=(ud(), ud())
print("ix, iy, dx, dy, norm: ", ix, iy, offset, np.sqrt(x**2 + y**2))
final_x, final_y = b.center().x + offset[0], b.center().y + offset[1]
print("Center in image + offset: ", final_x, final_y)
final_x_int, final_y_int = np.int(
np.rint(final_x)), np.int(np.rint(final_y))
print("Center in integer form: ", final_x_int, final_y_int)
line = "%g %g \n" % (final_x_int, final_y_int)
f.write(line)
# continue
psf.drawImage(sub_psf_image, offset=offset,
scale=pixel_scale*n_res, use_true_center=False)
# f.close()
f.close()
galsim.fits.write(psf_image, file_name)
if __name__ == "__main__":
import pdb
import traceback
import sys
try:
main(sys.argv)
except:
thingtype, value, tb = sys.exc_info()
traceback.print_exc()
pdb.post_mortem(tb)