-
Notifications
You must be signed in to change notification settings - Fork 8
/
acb.py
1384 lines (1038 loc) · 49.6 KB
/
acb.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
import cluster
import pypeline_io as io
import numpy as np
import time
import argparse
import data_operations as do
from astropy.io import fits
import multiprocessing as mp
import ciao_contrib.runtool as rt
import ciao
from tqdm import tqdm
def get_arguments():
help_str = """
This part of the pypeline creates all of the adaptive circular binned (acb) files
needed to do the spectral fitting. This fitting should likely be offloaded onto a
high performance computer.
Sample call (with the ciao environment running):
python acb.py --cluster_config_file /data_dir/A115/A115_pypeline_config.ini --resolution 2
python acb.py --cluster_config_file /data/dir/A115/A115_pypeline_config.ini --temperature_map
"""
prog = 'python acb.py'
# logger.debug("Getting commandline arguments.")
parser = argparse.ArgumentParser(description=help_str, prog=prog)
parser.add_argument("--cluster_config_file", "-c", dest="cluster_config",
action="store", default=None,
help="Path to the cluster configuration file")
# parser.add_argument("--parallel", "-p", dest="parallel",
# action="store_true", default=False,
# help='Run in parallel (default False)')
parser.add_argument("--temperature_map", "-t", dest='temperature_map',
action="store_true", default=False,
help="Create a temperature map after the spectral fitting process.")
parser.add_argument("--resolution", "-r", dest='resolution',
action='store', default=2, type=int,
help='Generate a low, medium, or high resolution temperature map. Low = 1, Med = 2, High = 3. '
'High resolution is a fit for every pixel, medium pixels are 3x3, low pixels are 5x5.')
parser.add_argument('--make_fitting_commands', dest='commands', action='store_true', default=False)
parser.add_argument('--eff_times_to_fits', dest='eff_times_fits', action='store_true', default=False)
parser.add_argument('--make_pressure_map', dest='pressure', action='store_true', default=False)
parser.add_argument('--make_entropy_map', dest='entropy', action='store_true', default=False)
parser.add_argument('--shock_finder', dest='shock', action='store_true', default=False)
args = parser.parse_args()
return args, parser
n = 6700
full_x_max = n
full_y_max = n
YY, XX = np.meshgrid(np.arange(full_y_max * 2), np.arange(full_x_max * 2))
big_mask = np.sqrt((full_x_max - XX) ** 2 + (full_y_max - YY) ** 2)
def generate_radius_map(x, y, x_max, y_max):
x_start = full_x_max - x
y_start = full_y_max - y
x_stop = x_start + x_max
y_stop = y_start + y_max
return big_mask[x_start:x_stop, y_start:y_stop]
def create_circle_regions_in_parallel(cluster: cluster.ClusterObj, num_cpus=1):
start_time = time.time()
observation_lists = cluster.parallel_observation_lists(num_cpus)
for observation_list in observation_lists:
processes = [mp.Process(target=create_circle_region_for,
args=(observation,)) for observation in observation_list]
for process in processes:
process.start()
for process in processes:
process.join()
end_time = time.time()
print("Time elapsed making regions for fit: {:0.2f} (s)".format(end_time-start_time))
def create_circle_region_for(observation: cluster.Observation):
mask_fits = fits.open(observation.cluster.combined_mask)
region_map = observation.cluster.scale_map_region_index
scale_map = observation.cluster.scale_map
mask = mask_fits[0].data
bounds = scale_map.shape
xvals = np.arange(bounds[1])
yvals = np.arange(bounds[0])
print("Making circular fitting regions for observation {}".format(observation.id))
image_fits = fits.open(observation.acisI_comb_img)
image_header = image_fits[0].header
cdelt1p = image_header['CDELT1P']
cdelt2p = image_header['CDELT2P']
crval1p = image_header['CRVAL1P']
crval2p = image_header['CRVAL2P']
crpix1p = image_header['CRPIX1P']
crpix2p = image_header['CRPIX2P']
radii = mask * scale_map * cdelt1p
newx = ((xvals + 1 - crpix1p) * cdelt1p) + crval1p
newy = ((yvals + 1 - crpix2p) * cdelt2p) + crval2p
xx, yy = np.meshgrid(newx, newy)
non_zero_indices = np.nonzero(radii)
nz_rad = radii[non_zero_indices]
nz_x = xx[non_zero_indices]
nz_y = yy[non_zero_indices]
obs_regions = region_map[non_zero_indices]
region_array = np.array([(i, j, k, l) for i, j, k, l in zip(nz_x, nz_y, nz_rad, obs_regions)])
regions = [["circle({x},{y},{rad})".format(x=x[0], y=x[1], rad=x[2]), int(x[3])] for x in region_array]
observation.scale_map_region_list = regions
def create_circle_regions(cluster):
start_time = time.time()
scale_map_fits = fits.open(cluster.scale_map_file)
mask_fits = fits.open(cluster.combined_mask)
region_map = cluster.scale_map_region_index
scale_map = scale_map_fits[0].data
mask = mask_fits[0].data
bounds = scale_map.shape
xvals = np.arange(bounds[1])
yvals = np.arange(bounds[0])
for observation in cluster.observations:
print("Making circular fitting regions for observation {}".format(observation.id))
image_fits = fits.open(observation.acisI_comb_img)
image_header = image_fits[0].header
cdelt1p = image_header['CDELT1P']
cdelt2p = image_header['CDELT2P']
crval1p = image_header['CRVAL1P']
crval2p = image_header['CRVAL2P']
crpix1p = image_header['CRPIX1P']
crpix2p = image_header['CRPIX2P']
radii = mask * scale_map * cdelt1p
newx = ((xvals + 1 - crpix1p) * cdelt1p) + crval1p
newy = ((yvals + 1 - crpix2p) * cdelt2p) + crval2p
xx, yy = np.meshgrid(newx, newy)
non_zero_indices = np.nonzero(radii)
nz_rad = radii[non_zero_indices]
nz_x = xx[non_zero_indices]
nz_y = yy[non_zero_indices]
obs_regions = region_map[non_zero_indices]
region_array = np.array([(i, j, k, l) for i, j, k, l in zip(nz_x, nz_y, nz_rad, obs_regions)])
regions = [["circle({x},{y},{rad})".format(x=x[0], y=x[1], rad=x[2]), int(x[3])] for x in region_array]
observation.scale_map_region_list = regions
end_time = time.time()
print("Time elapsed making regions for fit: {:0.2f} (s)".format(end_time-start_time))
def create_region_index_map(cluster):
mask_fits = fits.open(cluster.combined_mask)
mask = mask_fits[0].data
sz = mask.shape
nx = sz[0]
ny = sz[1]
indexmap = np.zeros(sz)
position = 0
region_string = []
for ci in range(nx):
for cj in range(ny):
if mask[ci,cj] == 1:
if ci % 3 == 0 and cj % 3 == 0: # makes it a lower resolution image than it needs to be.
region_string.append(str(position))
indexmap[ci,cj] = position
position += 1
region_string = '\n'.join(region_string)
with open(cluster.region_list, 'w') as f:
f.write(region_string)
region_file = mask_fits
region_file[0].data = indexmap
region_file[0].writeto(cluster.region_to_index, overwrite=True)
def create_scale_map_region_index(cluster: cluster.ClusterObj):
scale_map = cluster.scale_map
scale_map_regions = np.zeros(scale_map.shape)
sx = scale_map.shape[0]
sy = scale_map.shape[1]
region_num = 1
for x in range(sx):
for y in range(sy):
if scale_map[x,y] != 0:
scale_map_regions[x,y] = region_num
region_num += 1
else:
scale_map_regions[x,y] = np.nan
fits.writeto(cluster.scale_map_region_file, # filename
scale_map_regions, # data to write
cluster.scale_map_header, # header so coordinate information is written
overwrite=True) # self explanatory
def _update_completed_things(current, max_num, thing):
io.clear_line()
io.write("{current} out of {max} {thing} complete. ".format(
current=current,
max=max_num,
thing=thing
))
io.flush()
def _source_free_region(counter, current, max_num):
io.clear_line()
io.write("Encountered a source-free region -- recalculating...{counter} - {current}/{max} complete".format(
counter=counter,
current=current,
max=max_num
))
io.flush()
def _update_effective_exposure_time(obsid, current_region, number_regions, time_elapsed):
#io.clear_line()
#io.write("{current_region} of {num_regions} complete. Time elapsed: {time}".format(
print("ObsID {obsid} -\t{current_region} of {num_regions} complete. Time elapsed: {time}".format(
obsid=obsid,
current_region=current_region,
num_regions=number_regions,
time=time_elapsed
))
io.flush()
def create_scale_map_in_parallel(cluster: cluster.ClusterObj):
mask = cluster.combined_mask_data
cts_image = np.zeros(mask.shape)
back_rescale = np.zeros(mask.shape)
for obs in cluster.observations:
cts_image += obs.acisI_combined_image
t_obs = obs.acisI_combined_image_header['EXPOSURE']
t_back = obs.backI_combined_image_header['EXPOSURE']
back_rescale += (t_obs / t_back) * obs.backI_combined_image
signal = cts_image - back_rescale
signal[np.where(signal < 0)] = 0
sz = signal.shape
max_x = sz[0]
max_y = sz[1]
io.make_directory(cluster.acb_dir)
cluster.initialize_scale_map_csv()
pix_x = np.zeros(sz)
pix_y = np.zeros(sz)
for j in range(max_y):
for i in range(max_x):
pix_x[i, j] = float(i)
pix_y[i, j] = float(j)
num_pix = max_x * max_y
start_time = time.time()
indices = np.vstack(np.where(mask==1)).T
num_index_lists = (indices.shape[0] // mp.cpu_count())
index_lists = np.array_split(indices, num_index_lists)
num_iterations = len(index_lists)
for i, index_list in enumerate(index_lists):
if i % 100 == 0:
print("{} of {} iterations complete.".format(i, num_iterations))
processes = [mp.Process(target=calculate_radius_at_index,
args=(index, cluster, pix_x, pix_y, cts_image, num_pix, back_rescale))
for index in index_list]
for process in processes:
process.start()
for process in processes:
process.join()
cluster.write_scale_map_csv_to_fits()
end_time = time.time()
print("Time elapsed {:0.2f} seconds.".format(end_time - start_time))
def calculate_radius_at_index(index, cluster: cluster.ClusterObj,
pix_x: np.ndarray, pix_y: np.ndarray,
counts_image: np.ndarray, num_pix: int,
back_rescale: np.ndarray):
x_index = index[0]
y_index = index[1]
#print("Working on region at x:{} y:{}".format(x_index, y_index))
delta_x = x_index-pix_x
delta_y = y_index-pix_y
radius = np.sqrt(delta_x**2 + delta_y**2)
dr = 24.0
min_dr = 0.125
hilo = 0
niter = 0
max_radius = 100
r = max_radius + 1 # potentially a IDL vestige
counter = 0
signal_to_noise = 0
scale_map_radius = 0
while (dr > min_dr) and (niter < 100):
indices = np.where(radius <= r)
counts_map_total = np.sum(counts_image[indices])
if counts_map_total == 0:
counter += 1
_source_free_region(counter, x_index*y_index, num_pix)
sn_val = 0
hilo = -1
else:
backmap_tot = np.sum(back_rescale[indices])
signal_total = counts_map_total - backmap_tot
noise_total = np.sqrt(counts_map_total + backmap_tot)
sn_val = signal_total / noise_total
if float(sn_val) < float(cluster.target_sn):
if r > max_radius:
r = max_radius + 1
niter = 110
# exit by setting niter=110.
# (niter=100 means niter hit max niter.
# niter=110 means radius hit max radius)
signal_to_noise = 0
scale_map_radius = 0
else:
if hilo == 1:
dr *= 0.5
r += dr
hilo = -1
else:
snmapval = signal_to_noise
if (sn_val < snmapval) or (snmapval == 0.0):
signal_to_noise = sn_val
scale_map_radius = r
if hilo == -1:
dr *= 0.5
r -= dr
hilo = 1
niter += 1
#print("x:{} y:{} -> radius: {}.".format(x_index, y_index, scale_map_radius))
cluster.write_scale_map_radius(x_index, y_index, scale_map_radius, signal_to_noise)
# def binary_search_radii(arguments):
# cluster, index = arguments
# radii = np.arange(start=1, stop=101, step=0.125)
# left = 0
# right = radii.shape[0]
# nx, ny = cluster.combined_mask_data.shape
# x, y = index
# radius = generate_radius_map(x, y, nx, ny)
# if np.sum(cluster.counts_image[radius<=radii[-1]]) == 0: # radii[-1] == max bin radius
# update_stuff()
# print("None @ {index}".format(index=index))
# cluster.write_scale_map_radius(x, y, 0, 0) # no radius, no S/N ratio
# return
# while left < right:
# middle = int((left+right)/2)
# r = radii[middle]
# #indices_within_r = np.where(radius<=r)
# indices_within_r = radius<=r
# total_counts = np.sum(cluster.counts_image[indices_within_r])
# back_map_total = np.sum(cluster.back_rescale[indices_within_r])
# signal_total = total_counts - back_map_total
# noise_total = np.sqrt(total_counts + back_map_total)
# signal_to_noise = signal_total / noise_total
# if signal_to_noise < cluster.target_sn:
# left = middle + 1
# else:
# right = middle
# update_stuff()
# if r <= 100:
# cluster.write_scale_map_radius(x, y, r, signal_to_noise)
# else:
# cluster.write_scale_map_radius(x, y, 0, 0)
update_counter = 0
def update_stuff():
global update_counter
update_counter += 1
if update_counter % 1000 == 0:
io.clear_line()
count = update_counter * mp.cpu_count()
io.write("{count} regions finished.".format(count=count))
io.flush()
def binary_search_radii_wrapper(args):
image, index, search_radii, s_to_n = args
return binary_search_radii(image, index, search_radii, float(s_to_n))
def binary_search_radii(image=np.zeros(0), index=(0,0), search_radii=np.arange(1,100.125,0.125), s_to_n=40):
"""Use a binary search algorithm to find the smallest radius circular bin, centered at the given index,
that affords the desired signal to noise ratio.
Keyword arguments:
image -- The image you are binning (2d numpy array)
index -- The pixel within the image the circular bin is centered on (e.g. [0,0])
search_radii -- The various radii to search through in an effort to find the smallest (1D numpy array)
s_to_n -- The desired signal to noise ratio each bin must achieve.
returns -- x,y (the seperated index argument), bin radius, signal to noise
"""
radii = search_radii
left = 0
right = radii.shape[0]
nx, ny = image.shape
x, y = index
max_radii = search_radii[-1]
buff_radius = int(max_radii+2)
x1 = x - buff_radius
x1 = 0 if x1 < 0 else x1
x2 = x + buff_radius
x2 = nx if x2 > nx else x2
y1 = y - buff_radius
y1 = 0 if y1 < 0 else y1
y2 = y + buff_radius
y2 = ny if y2 > ny else y2
small_image = image[x1:x2, y1:y2]
radius = generate_radius_map(x, y, nx, ny)[x1:x2, y1:y2]
if np.sum(small_image[radius<=radii[-1]]) == 0:
return x,y,0,0
last_good_radii = None
last_good_s_to_n = 0
while left < right:
middle = int((left+right)/2)
r = radii[middle]
indices_within_r = radius<=r
total_counts = np.sum(small_image[indices_within_r])
noise_total = np.sqrt(total_counts)
signal_to_noise = total_counts / noise_total
if signal_to_noise < s_to_n:
left = middle + 1
else:
last_good_radii=r
last_good_s_to_n = signal_to_noise
right = middle
if r <= 100:
return x,y,last_good_radii,last_good_s_to_n
else:
counter += 1
return x,y,0,0
def generate_acb_scale_map_for(indices=None, image=np.zeros(0), max_bin_radius=100, step_size=0.125, s_to_n=40, num_processes=20):
"""Generate an adaptive circular bin map for the given image.
Keyword arguments:
image -- The image you want an adaptive circular bin map for (2D numpy array)
max_bin_radius -- The maximum bin radius for each circular bin (int)
step_size -- The step size between different radii. (float)
s_to_n -- The desired signal to noise ratio for each bin (int although can be float)
num_processes -- The number of processes you want to use for your multiprocessing pool (int)
returns -- The adaptive circular bin map and the signal to noise map (both 2D numpy arrays)
"""
# indices = np.vstack(np.where(~np.isnan(image))).T
radii_to_search = np.arange(start=1, stop=max_bin_radius+step_size, step=step_size)
acb_scale_map = np.zeros(image.shape)
s_to_n_map = np.zeros(image.shape)
arguments = [[image, index, radii_to_search, s_to_n] for index in indices]
with mp.Pool(num_processes) as pool:
results = list(tqdm(pool.imap(binary_search_radii_wrapper, arguments), total=len(arguments), desc="Calculating ACB Map"))
np_res = np.array(results)
print(np_res.shape)
x = np_res[:,0].astype(int)
y = np_res[:,1].astype(int)
acb_scale_map[x,y] = np_res[:,2]
s_to_n_map[x,y] = np_res[:,3]
return acb_scale_map, s_to_n_map
def fast_acb_creation_parallel(cluster: cluster.ClusterObj, num_cpus=mp.cpu_count()):
start_time = time.time()
indices = cluster.scale_map_indices
print("Calculating {num_regions} regions".format(num_regions=indices.shape[0]))
cluster.initialize_scale_map_csv()
cluster.back_rescale
cluster.counts_image
cluster.combined_mask_data
scale_map, s_to_n_map = generate_acb_scale_map_for(indices, cluster.counts_image, num_processes=num_cpus, s_to_n=cluster.signal_to_noise)
io.write_numpy_array_to_fits(scale_map, cluster.scale_map_file, cluster.xray_surface_brightness_nosrc_cropped_header)
io.write_numpy_array_to_fits(s_to_n_map, f'{cluster.acb_dir}/{cluster.name}_signal_to_noise_map.fits', cluster.xray_surface_brightness_nosrc_cropped_header)
end_time = time.time()
print("Time elapsed {:0.2f} seconds.".format(end_time - start_time))
def fast_acb_creation_serial(cluster: cluster.ClusterObj):
start_time = time.time()
indices = cluster.scale_map_indices
print("Calculating {num_regions} regions".format(num_regions=indices.shape[0]))
cluster.initialize_scale_map_csv()
for index in indices:
binary_search_radii((cluster, index))
cluster.write_scale_map_csv_to_fits()
end_time = time.time()
print("Time elapsed {elapsed:0.2f} seconds.".format(elapsed=end_time - start_time))
def prepare_efftime_circle_parallel(cluster: cluster.ClusterObj, num_cpus=1):
try:
from ciao_contrib import runtool as rt
except ImportError:
print("Failed to import CIAO python scripts. ")
raise
observation_lists = cluster.parallel_observation_lists(num_cpus)
for observation_list in observation_lists:
print("Preparing for effective time calculations in parallel.")
processes = [mp.Process(target=prepare_effective_time_circles_for,
args=(observation,)) for observation in observation_list]
for process in processes:
process.start()
for process in processes:
process.join()
def prepare_effective_time_circles_for(observation: cluster.Observation):
io.delete_if_exists(observation.effbtime)
io.delete_if_exists(observation.effdtime)
if not io.file_exists(observation.acisI_nosrc_combined_mask_file):
print("Removing point sources from the observations combined mask file.")
print("dmcopy infile='{}[exclude sky=region({})]' outfile={} clobber=True".format(
observation.acisI_combined_mask_file,
observation.cluster.sources_file,
observation.acisI_nosrc_combined_mask_file
))
rt.dmcopy.punlearn()
rt.dmcopy(
infile="{fits_file}[exclude sky=region({source_file})]".format(
fits_file=observation.acisI_combined_mask_file,
source_file=observation.cluster.sources_file
),
outfile=observation.acisI_nosrc_combined_mask_file,
clobber=True
)
else:
print("{acis} already exists.".format(
acis=observation.acisI_nosrc_combined_mask_file
))
# if not io.file_exists(observation.acisI_high_energy_combined_image_file):
print("Creating high band (9.5-12 keV) source image cropped to combined region.")
rt.dmcopy.punlearn()
rt.dmcopy(
infile="{fits_file}[sky=region({crop_file})]".format(
fits_file=observation.clean,
crop_file=observation.cluster.master_crop_file
),
outfile=observation.acisI_high_energy_temp_image,
clobber=True
)
##########need to change to obs specific
rt.dmcopy.punlearn()
rt.dmcopy(
infile="{fits_file}[EVENTS][bin sky=4][energy=9500:12000]".format(
fits_file=observation.acisI_high_energy_temp_image
),
outfile=observation.acisI_high_energy_combined_image_file,
option="image",
clobber=True
)
io.delete_if_exists(observation.acisI_high_energy_temp_image)
print("Creating high band (9.5-12 keV) background image cropped to combined region.")
rt.dmcopy.punlearn()
rt.dmcopy(
infile="{fits_file}[sky=region({crop_file})]".format(
fits_file=observation.back,
crop_file=observation.cluster.master_crop_file
),
outfile=observation.backI_high_energy_temp_image,
clobber=True
)
rt.dmcopy.punlearn()
rt.dmcopy(
infile="{fits_file}[EVENTS][bin sky=4][energy=9500:12000]".format(
fits_file=observation.backI_high_energy_temp_image
),
outfile=observation.backI_high_energy_combined_image_file,
option="image",
clobber=True
)
io.delete_if_exists(observation.backI_high_energy_temp_image)
def prepare_efftime_circle(cluster):
try:
from ciao_contrib import runtool as rt
except ImportError:
print("Failed to import CIAO python scripts. ")
raise
for observation in cluster.observations:
io.delete_if_exists(observation.effbtime)
io.delete_if_exists(observation.effdtime)
if not io.file_exists(observation.acisI_nosrc_combined_mask_file):
print("Removing point sources from the observations combined mask file.")
print("dmcopy infile='{}[exclude sky=region({})]' outfile={} clobber=True".format(
observation.acisI_combined_mask_file,
cluster.sources_file,
observation.acisI_nosrc_combined_mask_file
))
rt.dmcopy.punlearn()
rt.dmcopy(
infile="{fits_file}[exclude sky=region({source_file})]".format(
fits_file=observation.acisI_combined_mask_file,
source_file=cluster.sources_file
),
outfile=observation.acisI_nosrc_combined_mask_file,
clobber=True
)
else:
print("{acis} already exists.".format(
acis=observation.acisI_nosrc_combined_mask_file
))
if not io.file_exists(observation.acisI_high_energy_combined_image_file):
print("Creating high band (9.5-12 keV) source image cropped to combined region.")
rt.dmcopy.punlearn()
rt.dmcopy(
infile="{fits_file}[sky=region({crop_file})]".format(
fits_file=observation.clean,
crop_file=cluster.master_crop_file
),
outfile=observation.acisI_high_energy_temp_image,
clobber=True
)
rt.dmcopy.punlearn()
rt.dmcopy(
infile="{fits_file}[EVENTS][bin sky=4][energy=9500:12000]".format(
fits_file=observation.acisI_high_energy_temp_image
),
outfile=observation.acisI_high_energy_combined_image_file,
option="image",
clobber=True
)
else:
print("{fits_file} already exists.".format(
fits_file=observation.acisI_high_energy_combined_image_file
))
io.delete_if_exists(observation.acisI_high_energy_temp_image)
if not io.file_exists(observation.backI_high_energy_combined_image_file):
print("Creating high band (9.5-12 keV) background image cropped to combined region.")
rt.dmcopy.punlearn()
rt.dmcopy(
infile="{fits_file}[sky=region({crop_file})]".format(
fits_file=observation.back,
crop_file=cluster.master_crop_file
),
outfile=observation.backI_high_energy_temp_image,
clobber=True
)
rt.dmcopy.punlearn()
rt.dmcopy(
infile="{fits_file}[EVENTS][bin sky=4][energy=9500:12000]".format(
fits_file=observation.backI_high_energy_temp_image
),
outfile=observation.backI_high_energy_combined_image_file,
option="image",
clobber=True
)
else:
print("{fits_file} already exists.".format(
fits_file=observation.backI_high_energy_combined_image_file
))
io.delete_if_exists(observation.backI_high_energy_temp_image)
def calculate_effective_times(cluster: cluster.ClusterObj):
start_time = time.time()
scale_map = cluster.scale_map
number_of_regions = cluster.number_of_regions
nx = scale_map.shape[0]
ny = scale_map.shape[1]
effective_data_times = np.zeros(scale_map.shape)
effective_background_times = np.zeros(scale_map.shape)
for observation in cluster.observations:
print("Starting observation {obs}".format(obs=observation.id))
high_energy_data = observation.acisI_high_energy_combined_image
background = observation.backI_high_energy_combined_image
sum_acis_high_energy = np.sum(high_energy_data) # get the total counts in the high energy image
sum_back_high_energy = np.sum(background)
bg_to_data_ratio = sum_back_high_energy / sum_acis_high_energy
source_subtracted_data = observation.acisI_nosrc_combined_mask
exposure_time = observation.acisI_high_energy_combined_image_header['EXPOSURE']
YY, XX = np.meshgrid(np.arange(ny), np.arange(nx))
counter = 0
print("Starting effective exposure time calculations...")
for x in range(nx):
for y in range(ny):
if scale_map[x,y] >= 1:
radius = np.sqrt((x - XX)**2 + (y - YY)**2)
region = np.where(radius <= scale_map[x, y])
source_subtracted_area = np.sum(source_subtracted_data[region])
total_area = source_subtracted_data[region].size
fractional_area = source_subtracted_area / total_area
fractional_exposure_time = fractional_area * exposure_time
effective_data_times[x, y] = fractional_exposure_time
effective_background_times[x, y] = fractional_exposure_time * bg_to_data_ratio
counter += 1
if counter % 1000 == 0 or counter == number_of_regions or counter == 1:
time_elapsed = time.strftime("%H hours %M minutes %S seconds.",
time.gmtime(time.time()-start_time))
_update_effective_exposure_time(obsid=observation.id,
current_region=counter,
number_regions=number_of_regions,
time_elapsed=time_elapsed
)
observation.effective_data_time = effective_data_times
observation.effective_background_time = effective_background_times
def calculate_effective_times_in_parallel(cluster: cluster.ClusterObj, num_cpus=1):
observation_lists = cluster.parallel_observation_lists(num_cpus)
print('Calculating effective times in parallel using {} processes.'.format(num_cpus))
for observation_list in observation_lists:
processes = [mp.Process(target=calculate_effective_time_for,
args=(observation,)) for observation in observation_list]
for process in processes:
process.start()
for process in processes:
process.join()
def calculate_effective_times_in_parallel_map(cluster: cluster.ClusterObj, num_cpus=1):
with mp.Pool(num_cpus) as pool:
result = pool.map(calculate_effective_time_for, cluster.observations)
return result
def calculate_effective_times_in_serial(cluster: cluster.ClusterObj):
for observation in cluster.observations:
calculate_effective_time_for(observation)
def calculate_effective_time_for(observation: cluster.Observation):
"""This function returns 2 image maps, data and background, of the cluster representing the same area
of the cluster the scale map represents. Each pixel of the map represents the effective time observed
for each of the ACB regions. The effective observed time is essentially the integrated observing time
for each acb region. This is value differs from a simple, area of region * exposure time as some parts
of the region may be masked (i.e. a removed point source within the ACB region)."""
print("Starting observation {obs}".format(obs=observation.id))
start_time = time.time()
scale_map = observation.cluster.scale_map
nx = scale_map.shape[0]
ny = scale_map.shape[1]
effective_data_times = np.zeros(scale_map.shape)
effective_background_times = np.zeros(scale_map.shape)
if observation.acisI_nosrc_combined_mask.shape != scale_map.shape:
observation.reproject_nosrc_combined_mask(observation.cluster.scale_map_file)
if observation.acisI_combined_mask.shape != scale_map.shape:
observation.reproject_combined_mask(observation.cluster.scale_map_file)
high_energy_data = observation.acisI_high_energy_combined_image
background = observation.backI_high_energy_combined_image
sum_acis_high_energy = np.sum(high_energy_data) # get the total counts in the high energy image
sum_back_high_energy = np.sum(background)
bg_to_data_ratio = sum_back_high_energy / sum_acis_high_energy
source_subtracted_mask = observation.acisI_nosrc_combined_mask
exposure_time = observation.acisI_high_energy_combined_image_header['EXPOSURE']
indices = np.vstack(np.where(observation.acisI_combined_mask * scale_map > 0)).T
total = len(indices)
counter = 1
for index in indices:
x,y = index
if counter % 5000 == 0:
time_elapsed = time.strftime("%H h %M m %S s",
time.gmtime(time.time() - start_time))
print("ObsID {}\t {} of {} regions calculated. Time elapsed: {}. Avg {:2f} ms/region".format(
observation.id,
counter, total, time_elapsed, ((time.time() - start_time)/counter)*1000))
io.flush()
radius_map = generate_radius_map(x, y, nx, ny)
circle_mask = radius_map <= scale_map[x, y]
source_subtracted_area = np.sum(source_subtracted_mask[circle_mask])
total_area = source_subtracted_mask[circle_mask].size
fractional_area = source_subtracted_area / total_area
fractional_exposure_time = fractional_area * exposure_time
effective_data_times[x, y] = fractional_exposure_time
effective_background_times[x, y] = fractional_exposure_time * bg_to_data_ratio
counter += 1
observation.effective_data_time = effective_data_times
observation.effective_background_time = effective_background_times
time_elapsed = time.strftime("%H hours %M minutes %S seconds.",
time.gmtime(time.time() - start_time))
print("ObsID {} complete. Time elapsed: {}".format(observation.id, time_elapsed))
def prepare_for_spec(cluster_obj: cluster.ClusterObj):
try:
import ciao
except ImportError:
print("Must be running CIAO before running prepare_for_spec.")
raise
io.make_directory(cluster_obj.super_comp_dir)
cluster_obj.initialize_best_fits_file()
print("Preparing files for spectral analysis and copying to {super_comp_dir} for offloading computation.".format(
super_comp_dir=cluster_obj.super_comp_dir
))
io.copy(cluster_obj.configuration_filename, cluster_obj.super_comp_cluster_config)
for observation in cluster_obj.observations:
print("Copying files for {obsid}".format(obsid=observation.id))
io.copy(observation.clean, cluster_obj.acisI_clean_obs(observation.id))
io.copy(observation.back, cluster_obj.backI_clean_obs(observation.id))
io.copy(observation.aux_response_file, observation.arf_sc)
io.copy(observation.redistribution_matrix_file, observation.rmf_sc)
io.copy(observation.acis_mask, observation.acis_mask_sc)
exposure = ciao.get_exposure(observation.clean)
io.write_contents_to_file(exposure, observation.exposure_time_file, binary=False)
def make_commands_lis(cluster: cluster.ClusterObj, resolution):
print("Creating {}".format(cluster.command_lis))
offset = [None, 5, 3, 1][resolution]
start_time = time.time()
region_list = cluster.scale_map_regions_to_fit(resolution)
command_string = []
pypeline_dir = io.get_user_input("Enter the directory containing the pix2pix.py portion of the pypeline on the remote machine: ")
pix2pix_path = "{pypeline_dir}/pix2pix.py".format(pypeline_dir=pypeline_dir)
data_dir = io.get_user_input("Enter the directory containing the cluster data on the remote machine:\n"
"For example: /home/user/data/clustername/\n")
for region in region_list:
new_command = "python {pix2pix} {cluster_config} {region}".format(
pix2pix=pix2pix_path,
cluster_config="{data_dir}/{name}_pypeline_config.ini".format(
data_dir=data_dir,
name=cluster.name
),
region=region
)
command_string.append(new_command)
command_lis = "\n".join(command_string)
region_string = '\n'.join([str(x) for x in region_list])
io.write_contents_to_file(command_lis, cluster.command_lis, binary=False)
io.write_contents_to_file(region_string, cluster.filtered_region_list, binary=False)
end_time = time.time()
print("Time elapsed: {time:0.2f} sec".format(time=(end_time-start_time)))
def make_temperature_map(cluster: cluster.ClusterObj, resolution, average=False):
#coordinates = get_pixel_coordinates(cluster)
# indices of this array are the region number minus 1
# that is, region number 1 is coordinate array index 0
# region 100 = coordinates[99]
# high_res_offset = 0
# med_res_offset = 1
# low_res_offset = 2
io.make_directory(cluster.output_dir)
offset = [None, 2, 1, 0][resolution]