-
Notifications
You must be signed in to change notification settings - Fork 3
/
podi_fitpupilghost.py
executable file
·1306 lines (974 loc) · 43.7 KB
/
podi_fitpupilghost.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python3
#
# Copyright 2012-2013 Ralf Kotulla
#
# This file is part of the ODI QuickReduce pipeline package.
#
# If you find this program or parts thereof please make sure to
# cite it appropriately (please contact the author for the most
# up-to-date reference to use). Also if you find any problems
# or have suggestiosn on how to improve the code or its
# functionality please let me know. Comments and questions are
# always welcome.
#
# The code is made publicly available. Feel free to share the link
# with whoever might be interested. However, I do ask you to not
# publish additional copies on your own website or other sources.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
import sys
import os
import astropy.io.fits as pyfits
import numpy
#import ephem
import matplotlib.pyplot as plot
import scipy.interpolate
#import scipy.signal
import math
import scipy.optimize
import bottleneck
from podi_definitions import *
az_knot_limit = [50,600]
write_intermediate = True
use_buffered_files = True
def add_circle(buffer, center_x, center_y, radius, amplitude):
x, y = numpy.indices(buffer.shape)
dx = x - center_x
dy = y - center_y
d2 = dx*dx + dy*dy
print dx[0:10,0:10]
print dy[0:10,0:10]
print d2[0:10,0:10]
#print d2[995:1005, 995:1005]
tmp_buffer = numpy.zeros(shape=buffer.shape)
tmp_buffer[d2 < radius*radius] = amplitude
buffer += tmp_buffer
return
def add_annulus(buffer, center_x, center_y, radius_i, radius_o, amplitude):
x, y = numpy.indices(buffer.shape)
dx = x - center_x
dy = y - center_y
d2 = dx*dx + dy*dy
#print dx[0:10,0:10]
#print dy[0:10,0:10]
#print d2[0:10,0:10]
#print d2[995:1005, 995:1005]
tmp_buffer = numpy.zeros(shape=buffer.shape)
selected_pixels = (d2 < radius_o*radius_o) & (d2 > radius_i*radius_i)
tmp_buffer[selected_pixels] = amplitude
buffer += tmp_buffer
return
def create_from_template(command_file, buffer):
# Load command file
cmdfile = open(command_file, "r")
cmds = cmdfile.readlines()
print cmds
for i in range(len(cmds)):
line = cmds[i]
if (line[0] == "#"):
continue
items = line.strip().split()
print items
shape = items[0]
if (shape == "fillcircle"):
center_x = float(items[1])
center_y = float(items[2])
radius = float(items[3])
amplitude = float(items[4])
add_circle(buffer, center_x, center_y, radius, amplitude)
if (shape == "annulus"):
center_x = float(items[1])
center_y = float(items[2])
radius_i = float(items[3])
radius_o = float(items[4])
amplitude = float(items[5])
add_annulus(buffer, center_x, center_y, radius_i, radius_o, amplitude)
return buffer
#
# Get the median intensity level in an annulus between r_inner and r_outer
#
def get_median_level(data, radii, ri, ro):
selected = (radii > ri) & (radii < ro) #& (numpy.isfinite(data))
pixelcount = numpy.sum(selected)
if (pixelcount > 0):
#cutout = data[selected]
#median = numpy.median(cutout[0:5001])
cutout = numpy.array(data[selected], dtype=numpy.float32)
median = bottleneck.nanmean(cutout)
else:
median = numpy.NaN
return median, pixelcount
def get_radii_angles(data_fullres, center, binfac):
#
# Rebin the image 4x to speed up calculations (the pupil ghost
# doesn't vary on small scales, so this is ok to do)
#
data = rebin_image(data_fullres, binfac)
center_x, center_y = center
#
# Convert x/y coordinates to polar coordinates
#
stdout_write(" Computing radii ...\n")
x, y = numpy.indices(data.shape)
dx = x - center_x/binfac
dy = y - center_y/binfac
radius = numpy.sqrt(dx*dx + dy*dy)
angle = numpy.arctan2(dx, dy)
return data, radius, angle
def merge_OTAs(hdus, centers):
combined = numpy.zeros(shape=(9000,9000), dtype=numpy.float32)
combined[:,:] = numpy.NaN
stdout_write(" Adding OTA")
for i in range(len(hdus)):
#stdout_write("Adding OTA %s ...\n" % (hdus[i].header['EXTNAME']))
stdout_write(" %s" % (hdus[i].header['EXTNAME']))
# Use center position to add the new frame into the combined frame
# bx, by are the pixel position of the bottom left corner of the frame to be inserted
bx = combined.shape[0] / 2 - centers[i][0]
by = combined.shape[1] / 2 - centers[i][1]
#print bx, by
tx, ty = bx + hdus[i].data.shape[0], by + hdus[i].data.shape[1]
combined[bx:tx, by:ty] = hdus[i].data[:,:]
stdout_write("\n")
return combined
def load_frame(filename, pupilghost_centers, binfac, bpmdir):
hdu_ref = pyfits.open(filename)
hdus = []
centers = []
rotator_angle = hdu_ref[0].header['ROTSTART']
stdout_write("\nLoading frame %s ...\n" % (filename))
combined_file = "pg_combined_%+04d.fits" % numpy.around(rotator_angle)
print "combined-file:",combined_file
if (use_buffered_files and os.path.isfile(combined_file)):
hdu = pyfits.open(combined_file)
combined = hdu[1].data
hdu.close()
else:
for i in range(1, len(hdu_ref)):
extname = hdu_ref[i].header['EXTNAME']
if (extname in pupilghost_centers):
center_x, center_y = pupilghost_centers[extname]
stdout_write("Using center position %d, %d for OTA %s\n" % (center_y, center_x, extname))
data = hdu_ref[i].data
if (bpmdir != None):
bpmfile = "%s/bpm_xy%s.reg" % (bpmdir, extname[3:5])
mask_broken_regions(data, bpmfile, verbose=False)
hdus.append(hdu_ref[i])
centers.append((center_y, center_x))
combined = merge_OTAs(hdus, centers)
if (use_buffered_files):
pyfits.HDUList([pyfits.PrimaryHDU(), pyfits.ImageHDU(data=combined)]).writeto(combined_file, overwrite=True)
#pyfits.PrimaryHDU(data=combined).writeto(combined_file, overwrite=True)
rotated_file = "pg_rotated_%+04d.fits" % numpy.around(rotator_angle)
if (use_buffered_files and os.path.isfile(rotated_file)):
hdu = pyfits.open(rotated_file)
combined_rotated = hdu[1].data
hdu.close()
else:
#combined_rotated = rotate_around_center(combined, rotator_angle)
#combined_rotated = rotate_around_center(combined, rotator_angle, mask_nans=False, spline_order=1)
combined_rotated = rotate_around_center(combined, rotator_angle, mask_nans=True, spline_order=1)
if (use_buffered_files):
pyfits.HDUList([pyfits.PrimaryHDU(), pyfits.ImageHDU(data=combined_rotated)]).writeto(rotated_file, overwrite=True)
# pyfits.PrimaryHDU(data=combined_rotated).writeto(rotated_file, overwrite=True)
return combined_rotated, None, None, rotator_angle
data_rotated, radius, angle = get_radii_angles(combined_rotated, (combined_rotated.shape[0]/2, combined_rotated.shape[1]/2), binfac)
#angle -= math.radians(rotator_angle)
angle[angle < 0] += 2*numpy.pi
hdu_ref.close()
return data_rotated, radius, angle, rotator_angle
def subtract_background(data, radius, angle, radius_range, binfac):
# Compute the radial bin size in binned pixels
print "subtracting background - binfac=",binfac
r_inner, r_outer, dr_full = radius_range
dr = dr_full/binfac
r_inner /= binfac
r_outer /= binfac
#
# Compute the number of radial bins
#
# Here: Add some correction if the center position is outside the covered area
max_radius = 1.3 * r_outer #math.sqrt(data.shape[0] * data.shape[1])
# Splitting up image into a number of rings
n_radii = int(math.ceil(max_radius / dr))
#
# Compute the background level as a linear interpolation of the levels
# inside and outside of the pupil ghost
#
stdout_write(" Computing background-level ...")
# Define the background ring levels
radii = numpy.arange(0, max_radius, dr)
background_levels = numpy.zeros(shape=(n_radii))
background_level_errors = numpy.ones(shape=(n_radii)) * 1e9
background_levels[:] = numpy.NaN
for i in range(n_radii):
ri = i * dr
ro = ri + dr
if (ri < r_inner):
ro = numpy.min([ro, r_inner])
elif (ro > r_outer):
ri = numpy.max([ri, r_outer])
else:
# Skip the rings within the pupil ghost range for now
continue
#print i, ri, ro
median, count = get_median_level(data, radius, ri, ro)
background_levels[i] = median
background_level_errors[i] = 1. / math.sqrt(count) if count > 0 else 1e9
# Now fit a straight line to the continuum, assuming it varies
# only linearly (if at all) with radius
# define our (line) fitting function
fitfunc = lambda p, x: p[0] + p[1] * x
errfunc = lambda p, x, y, err: (y - fitfunc(p, x)) / err
bg_for_fit = background_levels
bg_for_fit[numpy.isnan(background_levels)] = 0
pinit = [0.0, 0.0] # Assume no slope and constant level of 0
out = scipy.optimize.leastsq(errfunc, pinit,
args=(radii, background_levels, background_level_errors), full_output=1)
pfinal = out[0]
covar = out[1]
stdout_write(" best-fit: %.2e + %.3e * x\n" % (pfinal[0], pfinal[1]))
#print pfinal
#print covar
#
# Now we have the fit for the background, compute the 2d background
# image and subtract it out
#
x = numpy.linspace(0, max_radius, 100)
y_fit = radii * pfinal[1] + pfinal[0]
background = pfinal[0] + pfinal[1] * radius
bg_sub = data - background
#if (write_intermediate):
# bgsub_hdu = pyfits.PrimaryHDU(data=bg_sub)
# bgsub_hdu.writeto("bgsub.fits", overwrite=True)
return bg_sub
def do_work(filenames, pupilghost_centers, binfac, radius_range, bpmdir):
all_data, all_radius, all_angle, all_bgsub = None, None, None, None
print "Running do_work"
for filename in filenames:
# Load and prepare all files
stdout_write("\n\nWorking on file %s ...\n" % (filename))
data, radius, angle, rotator_angle = load_frame(filename, pupilghost_centers, binfac, bpmdir)
if (cmdline_arg_isset("-onlyprepfiles")):
continue
bgsub_file = "pg_bgsub_%+04d.fits" % numpy.around(rotator_angle)
if (use_buffered_files and os.path.isfile(bgsub_file)):
hdu = pyfits.open(bgsub_file)
bgsub = hdu[1].data
hdu.close()
else:
bgsub = subtract_background(data, radius, angle, radius_range, binfac)
if (use_buffered_files):
pyfits.HDUList([pyfits.PrimaryHDU(), pyfits.ImageHDU(data=bgsub)]).writeto(bgsub_file, overwrite=True)
# pyfits.PrimaryHDU(data=bgsub).writeto(bgsub_file, overwrite=True)
if (cmdline_arg_isset("-onlyprepfiles")):
continue
# Create a master collection containing all files
if (all_data is None):
# If this is the first file, create the master list
all_data = data
all_radius = radius
all_angle = angle
all_bgsub = bgsub
else:
# if we already have some entries, add the new ones to the collection
all_data = numpy.append(all_data, data, axis=0)
all_radius = numpy.append(all_radius, radius, axis=0)
all_angle = numpy.append(all_angle, angle, axis=0)
all_bgsub = numpy.append(all_bgsub, bgsub, axis=0)
if (cmdline_arg_isset("-onlyprepfiles")):
sys.exit(0)
#
# Now we have a collection of a bunch of files, possibly each with separate rotator angles
#
hdu = pyfits.PrimaryHDU(data = all_data)
hdu.writeto("all_data.fits", overwrite=True)
hdu = pyfits.PrimaryHDU(data = all_bgsub)
hdu.writeto("all_bgsub.fits", overwrite=True)
pupil_sub, radial_profile, radial_2d = fit_radial_profile(all_data, all_radius, all_angle, all_bgsub, radius_range)
pyfits.PrimaryHDU(data=pupil_sub).writeto("pupilsub.fits", overwrite=True)
pyfits.PrimaryHDU(data=radial_2d).writeto("radial2d.fits", overwrite=True)
# create_mapped_coordinates(all_data, all_radius, all_angle, all_bgsub, pupil_sub, radius_range, binfac)
azimuthal_fits = fit_azimuthal_profiles(all_data, all_radius, all_angle, all_bgsub, pupil_sub, radius_range)
#
# Now all the fitting is done, let's compute the output
#
# First get a fresh buffer of coordinates
outbuffer = numpy.zeros(shape=(9000,9000))
out_data, out_radius, out_angle = get_radii_angles(outbuffer, (outbuffer.shape[0]/2, outbuffer.shape[1]/2), binfac)
out_angle[out_angle < 0] += 2*numpy.pi
azimuthal_2d = compute_pupilghost(out_data, out_radius, out_angle, radius_range, binfac,
azimuthal_fits)
pyfits.PrimaryHDU(data=azimuthal_2d).writeto("fit_nonradial.fits", overwrite=True)
# Compute the 2-d radial profile. The extreme values beyond the fitting radius
# might be garbage, so set all pixels outside the pupil ghost radial range to 0
radial_2d = radial_profile(out_radius.ravel()).reshape(out_radius.shape)
radial_2d[(radius > r_outer/binfac) | (radius < r_inner/binfac)] = 0
pyfits.PrimaryHDU(data=radial_2d).writeto("fit_radial.fits", overwrite=True)
try:
full_2d = azimuthal_2d + radial_2d
full_2d[full_2d<0] = 0
print "Writing data"
pyfits.PrimaryHDU(data=full_2d).writeto("fit_rad+nonrad.fits", overwrite=True)
except:
pass
#leftover = bg_sub - fullprofile
# pyfits.PrimaryHDU(data=leftover).writeto("fit_leftover.fits", overwrite=True)
return
#------------------------------------------------------------------------------
#
# Until now the template is still binned, blow it up to the full resolution
#
#------------------------------------------------------------------------------
print "Interpolating to full resolution"
xb, yb = numpy.indices(data.shape)
# Prepare the 2-d interpolation spline
interpol = scipy.interpolate.RectBivariateSpline(xb[:,0], yb[0,:], fullprofile)
# And use above spline to compute the full-resolution version
xo, yo = numpy.indices(data_fullres.shape)
xo = xo * 1.0 / data_fullres.shape[0] * data.shape[0]
yo = yo * 1.0 / data_fullres.shape[1] * data.shape[1]
correction = interpol(xo[:,0], yo[0,:]).reshape(data_fullres.shape)
return correction
return
def fit_radial_profile(data, radius, angle, bgsub, radius_range, binfac=1, verbose=False, show_plots=False,
force_positive=False, zero_edges=False, save_profile=None):
#------------------------------------------------------------------------------
#
# Here we assume that all files have their background removed.
# Then we continue with the radial profile.
#
#------------------------------------------------------------------------------
r_inner, r_outer, dr_full = radius_range
dr = dr_full/binfac
r_inner /= binfac
r_outer /= binfac
n_radii = int(math.ceil(r_outer / dr))
stdout_write("\nComputing radial profile (ri=%d, ro=%d, n=%d, bin=%d)...\n" % (r_inner, r_outer, n_radii, binfac))
#
# Next step: Fit the radial profile
#
pupil_radii = numpy.zeros(shape=(n_radii))
pupil_level = numpy.zeros(shape=(n_radii))
min_r, max_r = 10000, -10000
for i in range(n_radii):
ri = i * dr
ro = ri + dr
# Only use rings within the pupil gost rings
if (ri < r_outer and ro > r_inner):
ri = numpy.max([ri, r_inner])
ro = numpy.min([ro, r_outer])
min_r = numpy.min([min_r, i])
max_r = numpy.max([max_r, i])
else:
continue
if (verbose): stdout_write("radius i=%4d" % (i))
median, count = get_median_level(bgsub, radius, ri, ro)
pupil_radii[i] = 0.5 * (ri + ro)
pupil_level[i] = median
if (verbose): stdout_write(" ri: %4d ro: %4d med: %.4f\n" % (ri, ro, median))
if (force_positive):
pupil_level[pupil_level < 0] = 0.
if (zero_edges):
pupil_level[min_r] = 0.
pupil_level[max_r] = 0.
if (save_profile != None):
out = open(save_profile, "w")
print >>out, "# Data: radius, median"
dummy = numpy.empty(shape=(pupil_radii.shape[0],2))
dummy[:,0] = pupil_radii[:]
dummy[:,1] = pupil_level[:]
#dummy = numpy.append(pupil_radii, pupil_level, axis=1)
numpy.savetxt(out, dummy)
#
# Now fit the profile with a 1-D spline
#
n_knots = (r_outer-r_inner-2*dr)/dr-1
if (n_knots > 75): n_knots=75
radial_knots = numpy.linspace(r_inner+0.7*dr, r_outer-0.7*dr, n_knots)
if (verbose): print "radial knots=",radial_knots[0:5],"...",radial_knots[-5:]
radial_profile = scipy.interpolate.LSQUnivariateSpline(pupil_radii[min_r:max_r+1], pupil_level[min_r:max_r+1], radial_knots, k=2)
# In case of ValueError, this is what causes it (from scipy/intrpolate/interpolate.py):
# if not alltrue(t[k+1:n-k]-t[k:n-k-1] > 0,axis=0):
# raise ValueError('Interior knots t must satisfy '
# 'Schoenberg-Whitney conditions')
if (save_profile != None):
print >>out, "\n\n\n\n\n\n\n\n"
print >>out, "# spline fit: radius, level"
smooth_radial_1d_x = numpy.linspace(r_inner, r_outer, 1300)
smooth_radial_1d_y = radial_profile(smooth_radial_1d_x)
dummy = numpy.empty(shape=(smooth_radial_1d_x.shape[0],2))
dummy[:,0] = smooth_radial_1d_x[:]
dummy[:,1] = smooth_radial_1d_y[:]
numpy.savetxt(out, dummy)
out.close()
if (show_plots):
# create a smooth profile for plotting
smooth_radial_1d_x = numpy.linspace(r_inner, r_outer, 1300)
smooth_radial_1d_y = radial_profile(smooth_radial_1d_x)
knots_y = radial_profile(radial_knots)
plot.plot(pupil_radii, pupil_level, '.-', radial_knots, knots_y, 'o', smooth_radial_1d_x, smooth_radial_1d_y)
plot.show()
#
# Compute the 2-d radial profile
#
radius_1d = radius.ravel()
pupil_radial_2d = radial_profile(radius.ravel()).reshape(radius.shape)
# set all pixels outside the pupil ghost radial range to 0
pupil_radial_2d[(radius > r_outer) | (radius < r_inner)] = 0
# and subtract the pupil ghost
pupil_sub = bgsub - pupil_radial_2d
#if (write_intermediate):
pupil_sub_hdu = pyfits.PrimaryHDU(data = pupil_sub)
pupil_sub_hdu.writeto("all_pupilsub.fits", overwrite=True)
return pupil_sub, radial_profile, pupil_radial_2d
# template_radius_1d = template_radius.ravel()
# template_radial = radial_profile(template_radius.ravel()).reshape(template_radius.shape)
# template_radial[(template_radius > r_outer) | (template_radius < r_inner)] = 0
# pupil_sub_hdu = pyfits.PrimaryHDU(data = template_radial)
# pupil_sub_hdu.writeto("template_radial.fits", overwrite=True)
return
import scipy.ndimage
def create_mapped_coordinates(all_data, all_radius, all_angle, all_bgsub, pupil_sub, radius_range, binfac):
# Create the output array so we know for what positions we have to compute values
output = numpy.zeros(shape=(9000,9000))
output_binned, output_radius, output_angle = get_radii_angles(output, (output.shape[0]/2, output.shape[1]/2), binfac)
# Convert positions into format scipy wants
coords = numpy.zeros(shape=(output_binned.ravel().shape[0],2))
coords[:,0] = output_radius.ravel()[:]
coords[:,1] = output_angle.ravel()[:]
# Now do the hard work: Compute mean/median values for pupil ghost in polar coordinates
r_inner, r_outer, dr_full = radius_range
dr = dr_full/binfac
r_inner /= binfac
r_outer /= binfac
n_radii = int(math.ceil(r_outer / dr))
n_angles = 360
d_angle = numpy.pi / n_angles
polar = numpy.zeros(shape=(n_radii, n_angles))
for r in range(polar.shape[0]):
stdout_write("\rWorking on radius %d of %d" % (r, polar.shape[0]))
for phi in range(polar.shape[1]):
r_min = r * dr
r_max = (r+1) * dr
phi_min = phi * d_angle
phi_max = (phi+1) * d_angle
in_sector = (all_radius > r_min) & (all_radius <= r_max) & (all_angle > phi_min) & (all_angle <= phi_max)
pixels = all_bgsub[in_sector]
valid_pixels = pixels[numpy.isfinite(pixels)]
median = numpy.mean(valid_pixels)
polar[r,phi] = median
dummy = pyfits.PrimaryHDU(data=polar)
dummy.writeto("polar_fit.fits", overwrite=True)
# matched = scipy.ndimage.map_coordinates
return
def fit_azimuthal_profiles(data, radius, angle, bgsub, pupil_sub, radius_range):
show_plots = False
#------------------------------------------------------------------------------
#
# Now we have only the non-radial components of the pupil ghost left
#
#------------------------------------------------------------------------------
stdout_write("\nComputing azimuthal profiles ...\n")
r_inner, r_outer, dr_full = radius_range
dr = dr_full/binfac
r_inner /= binfac
r_outer /= binfac
n_radii = int(math.ceil(r_outer / dr))
min_r = 0
max_r = 1e6
radial_splines = [None] * n_radii
for cur_radius in range(n_radii):
ri = cur_radius * dr
ro = ri + dr
# Make sure we ignore points outside the pupil ghost area
if (ri < r_outer and ro > r_inner):
ri = numpy.max([ri, r_inner])
ro = numpy.min([ro, r_outer])
min_r = numpy.min([min_r, cur_radius])
max_r = numpy.max([max_r, cur_radius])
else:
continue
# Keep user informed and happy
sys.stdout.write("\rFitting azimuthal profile in ring %d - %d" % (int(ri), int(ro)))
sys.stdout.flush()
# Get all pixels in this ring
pixels_in_ring = (radius >= ri) & (radius < ro)
ring_angles = angle[pixels_in_ring]
ring_data = pupil_sub[pixels_in_ring]
# Eliminate all NaN pixels
valid_pixels_in_ring = numpy.isfinite(ring_data)
#
# Get min and max angles
#
min_angle = 0
max_angle = 2 * numpy.pi
# min_angle = numpy.min(ring_angles[valid_pixels_in_ring])
# max_angle = numpy.max(ring_angles[valid_pixels_in_ring])
# Select valid pixels
valid_angles = ring_angles[valid_pixels_in_ring]
valid_data = ring_data[valid_pixels_in_ring]
# compute the mean angle difference between two points
mean_da = (max_angle - min_angle) / valid_angles.shape[0]
# Select knots for the spline fitting
number_knots = numpy.sum(valid_pixels_in_ring) / 100
if (number_knots < az_knot_limit[0]): number_knots = az_knot_limit[0]
if (number_knots > az_knot_limit[1]): number_knots = az_knot_limit[1]
angle_knots = numpy.linspace(min_angle, max_angle, number_knots)
file = open("bincount.log", "a")
print >>file, ri, ro, angle_knots.shape[0], numpy.sum(valid_pixels_in_ring)
file.close()
# Now eliminate knots in regions with no data
for i in range(angle_knots.shape[0]):
diff_angle = numpy.fabs(angle_knots[i] - valid_angles)
min_diffangle = numpy.min(diff_angle)
if (min_diffangle > 3 * mean_da or angle_knots[i] <= min_angle or angle_knots[i] >= max_angle):
angle_knots[i] = numpy.NaN
good_angle_knots = angle_knots[numpy.isfinite(angle_knots)]
# sort all points in this ring, otherwise LSQUnivariateSplie chokes
si = numpy.argsort(valid_angles)
sorted_angles = numpy.zeros(valid_angles.shape)
sorted_data = numpy.zeros(valid_data.shape)
for i in range(si.shape[0]):
sorted_angles[i] = valid_angles[si[i]]
sorted_data[i] = valid_data[si[i]]
# Fit spline
try:
az_profile = scipy.interpolate.LSQUnivariateSpline(sorted_angles, sorted_data, good_angle_knots, k=3)
radial_splines[cur_radius] = az_profile
except:
stdout_write("\n#\n")
print "# Serious problem found in ring %d - %d" % (int(ri), int(ro))
print "# Number of elements in ring: ", sorted_angles.shape, sorted_data.shape
print "# Number of knots:", good_angle_knots.shape
print "#"
if (show_plots):
fine_profile_x = numpy.linspace(min_angle, max_angle, 1000)
fine_profile_y = az_profile(fine_profile_x)
plot.plot(ring_angles, ring_data, '+', color="#aaaaaa")
plot.plot(fine_profile_x, fine_profile_y)
plot.plot(angle_knots, angle_knots_y, 'x')
#plot.savefig("profile_az_%d-%d.png" % (ri,ro))
plot.close()
print " - done!"
return radial_splines
def compute_pupilghost(data, radius, angle,
radius_range, binfac,
radial_splines):
#------------------------------------------------------------------------------
#
# Now we have all components, so compute the pupil ghost template
#
#------------------------------------------------------------------------------
#
# Go through all the pixels in the data block and compute the pupil ghost.
# Radial profile is simple, for the azimuthal interpolate linearly between the two radii
#
r_inner, r_outer, dr = radius_range
r_inner /= binfac
r_outer /= binfac
dr /= binfac
nonradial_profile = numpy.zeros(shape=data.shape)
for x in range(data.shape[0]):
sys.stdout.write("\rCreating pupil-ghost template, %.1f %% done ..." % ((x+1)*100.0/data.shape[0]))
sys.stdout.flush()
for y in range(data.shape[1]):
radius_here = radius[x,y]
if (radius_here < r_inner or radius_here > r_outer):
continue
angle_here = angle[x,y]
r_here = float(radius_here) / dr
ri = int(math.floor(r_here-0.5))
ro = int(math.ceil(r_here-0.5))
if (ri < 0):
continue
elif (ro >= len(radial_splines) and ri < len(radial_splines)):
ro = ri
elif (ri >= len(radial_splines)):
continue
try:
val_i, val_o = 0,0
if (radial_splines[ri] != None):
val_i = radial_splines[ri](angle_here)
if (radial_splines[ro] != None):
val_o = radial_splines[ro](angle_here)
except:
print "Found a problem: x/y = ",x,y
print " r_here/dr/ri/ro=",r_here, dr, ri, ro
pass
# Now interpolate linearly between the two
if (ri == ro):
nonradial_profile[x,y] = val_i
else:
slope = (val_o - val_i) / float(ro - ri)
nonradial_profile[x,y] = (r_here - float(ri)) * slope + val_i
# if (nonradial_profile[x,y] > pupil_radial_2d[x,y]):
# nonradial_profile[x,y] = pupil_radial_2d[x,y]
print " complete!"
return nonradial_profile
def fit_pupilghost(hdus, centers, rotator_angles, radius_range, dr_full,
write_intermediate=True, show_plots=False):
# Choose binning of raw-data to speed up computation
binfac = 4
# Compute the radial bin size in binned pixels
dr = dr_full/binfac
r_inner, r_outer = radius_range
r_inner /= binfac
r_outer /= binfac
# Allocate some memory to hold the template
template = numpy.zeros(shape=(9000,9000))
template_binned, template_radius, template_angle = get_radii_angles(template, (4500,4500), 4)
combined = merge_OTAs(hdus, centers)
data, radius, angle = get_radii_angles(combined, (combined.shape[0]/2, combined.shape[1]/2), binfac)
angle -= rotator_angles[0]
# write some intermediate data products
if (write_intermediate):
raw_hdu = pyfits.PrimaryHDU(data=data)
raw_hdu.writeto("raw.fits", overwrite=True)
combined_hdu = pyfits.PrimaryHDU(data=combined)
combined_hdu.writeto("raw_combined.fits", overwrite=True)
sys.exit(0)
#
# Compute the number of radial bins
#
# Here: Add some correction if the center position is outside the covered area
max_radius = 1.3 * r_outer #math.sqrt(data.shape[0] * data.shape[1])
# Splitting up image into a number of rings
n_radii = int(math.ceil(max_radius / dr))
#
# Compute the background level as a linear interpolation of the levels
# inside and outside of the pupil ghost
#
stdout_write(" Computing background-level ...")
# Define the background ring levels
radii = numpy.arange(0, max_radius, dr)
background_levels = numpy.zeros(shape=(n_radii))
background_level_errors = numpy.ones(shape=(n_radii)) * 1e9
background_levels[:] = numpy.NaN
for i in range(n_radii):
ri = i * dr
ro = ri + dr
if (ri < r_inner):
ro = numpy.min([ro, r_inner])
elif (ro > r_outer):
ri = numpy.max([ri, r_outer])
else:
# Skip the rings within the pupil ghost range for now
continue
#print i, ri, ro
median, count = get_median_level(data, radius, ri, ro)
background_levels[i] = median
background_level_errors[i] = 1. / math.sqrt(count) if count > 0 else 1e9
# Now fit a straight line to the continuum, assuming it varies
# only linearly (if at all) with radius
# define our (line) fitting function
fitfunc = lambda p, x: p[0] + p[1] * x
errfunc = lambda p, x, y, err: (y - fitfunc(p, x)) / err
bg_for_fit = background_levels
bg_for_fit[numpy.isnan(background_levels)] = 0
pinit = [1.0, 0.0] # Assume no slope and constant level of 1
out = scipy.optimize.leastsq(errfunc, pinit,
args=(radii, background_levels, background_level_errors), full_output=1)
pfinal = out[0]
covar = out[1]
stdout_write(" best-fit: %.2f + %f * x\n" % (pfinal[0], pfinal[1]))
#print pfinal
#print covar
#
# Now we have the fit for the background, compute the 2d background
# image and subtract it out
#
x = numpy.linspace(0, max_radius, 100)
y_fit = radii * pfinal[1] + pfinal[0]
background = pfinal[0] + pfinal[1] * radius
bg_sub = data - background
if (write_intermediate):
bgsub_hdu = pyfits.PrimaryHDU(data=bg_sub)
bgsub_hdu.writeto("bgsub.fits", overwrite=True)
#------------------------------------------------------------------------------
#
# Now we got rid of background gradients, continue with the radial profile
#
#------------------------------------------------------------------------------
#
# Next step: Fit the radial profile
#
pupil_radii = numpy.zeros(shape=(n_radii))
pupil_level = numpy.zeros(shape=(n_radii))
min_r, max_r = 10000, -10000
for i in range(n_radii):
ri = i * dr
ro = ri + dr
# Only use rings within the pupil gost rings
if (ri < r_outer and ro > r_inner):
ri = numpy.max([ri, r_inner])
ro = numpy.min([ro, r_outer])
min_r = numpy.min([min_r, i])
max_r = numpy.max([max_r, i])
else:
continue
median, count = get_median_level(bg_sub, radius, ri, ro)
pupil_radii[i] = 0.5 * (ri + ro)
pupil_level[i] = median
#
# Now fit the profile with a 1-D spline
#
radial_knots = numpy.linspace(r_inner+0.7*dr, r_outer-0.7*dr, (r_outer-r_inner-1.4*dr)/dr/1.5)
radial_profile = scipy.interpolate.LSQUnivariateSpline(pupil_radii[min_r:max_r+1], pupil_level[min_r:max_r+1], radial_knots, k=2)
if (show_plots):
# create a smooth profile for plotting
smooth_radial_1d_x = numpy.linspace(r_inner, r_outer, 1300)
smooth_radial_1d_y = radial_profile(smooth_radial_1d_x)
knots_y = radial_profile(radial_knots)
plot.plot(pupil_radii, pupil_level, '.-', radial_knots, knots_y, 'o', smooth_radial_1d_x, smooth_radial_1d_y)
plot.show()
#
# Compute the 2-d radial profile
#
radius_1d = radius.ravel()
pupil_radial_2d = radial_profile(radius.ravel()).reshape(radius.shape)
# set all pixels outside the pupil ghost radial range to 0
pupil_radial_2d[(radius > r_outer) | (radius < r_inner)] = 0
# and subtract the pupil ghost
pupil_sub = bg_sub - pupil_radial_2d
if (write_intermediate):
pupil_sub_hdu = pyfits.PrimaryHDU(data = pupil_sub)
pupil_sub_hdu.writeto("pupilsub.fits", overwrite=True)
template_radius_1d = template_radius.ravel()
template_radial = radial_profile(template_radius.ravel()).reshape(template_radius.shape)
template_radial[(template_radius > r_outer) | (template_radius < r_inner)] = 0
pupil_sub_hdu = pyfits.PrimaryHDU(data = template_radial)
pupil_sub_hdu.writeto("template_radial.fits", overwrite=True)
sys.exit(0)
#------------------------------------------------------------------------------
#
# Now we have only the non-radial components of the pupil ghost left
#
#------------------------------------------------------------------------------
radial_splines = [None] * n_radii