-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcluster.py
2103 lines (1744 loc) · 75.6 KB
/
cluster.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 os
import sys
import configparser
import csv
import config
import pypeline_io as io
import numpy as np
import astropy.io.fits as fits # migrate pycrates commands to fits
import logging
try:
from ciao_contrib import runtool as rt
from sherpa.astro import ui as sherpa
except ImportError:
print("Unable to load CIAO. Fitting calls will fail")
rt = None
sherpa = None
acisI = [0, 1, 2, 3]
acisS = [4, 5, 6, 7, 8, 9]
back_illuminated_ids = [5, 7]
class CCD:
def __init__(self, observation=None, id=None):
self.observation = observation
self.id = id
if self.id in acisI:
self.type = "I"
elif self.id in acisS:
self.type = "S"
print("")
else:
print("Error with observation {} chip id {}. Cannot determine type (ACIS-I or ACIS-S).".format(
observation.id, self.id))
exit(-1)
if self.id in back_illuminated_ids:
self.back_illuminated = True
else:
self.back_illuminated = False
def __repr__(self):
return "OBSID {}:ACIS-{}:CCD {}: Back Illuminated={}".format(self.observation.id, self.type, self.id,
self.back_illuminated)
class Observation:
def __init__(self,
obsid=0,
cluster=None,
):
self.id = obsid
self.cluster = cluster
self.set_ccds()
def set_ccds(self):
try:
self.ccds = self.get_ccds()
self.acis_I_chips, self.acis_S_chips = self.get_acis_I_and_S_chips()
# if len(self.acis_S_chips) > 0:
# print("Observation {} has ACIS-S data that is not yet supported. "
# "Feel free to implement and submit a pull request!".format(self.id))
# # if len(self.acis_I_chips) > 0:
# print("ACIS-I chips to be used:")
# for ccd in self.acis_I_chips:
# print(ccd)
except FileNotFoundError:
#print("Observation {} not yet downloaded".format(self.id))
pass
def get_ccds(self):
chip_ids = self.oif_detnam
ccds = []
if chip_ids[:5].upper() == "ACIS-":
ids = chip_ids[5:]
for chip in ids:
ccds.append(CCD(observation=self, id=int(chip)))
else:
print("Error determining ACIS CCDs used. Check observation {} and try again".format(self.id))
exit(-1)
return ccds
def get_acis_I_and_S_chips(self):
acis_I_ccds = []
acis_S_ccds = []
for ccd in self.ccds:
if ccd.type == 'I':
acis_I_ccds.append(ccd)
else:
acis_S_ccds.append(ccd)
if len(acis_I_ccds) > len(acis_S_ccds):
self.acis_type = 0 # ACIS-I
self.ccd_filter = "0:3"
else:
self.acis_type = 1 # ACIS-S
self.ccd_filter = "4:9"
return acis_I_ccds, acis_S_ccds
def effective_data_time_for_region(self, region_number):
coordinates_for_region = self.coordinates_for_scale_map_region(region_number,
self.cluster.scale_map_region_index)
return self.effective_data_time[coordinates_for_region][0]
def effective_background_time_for_region(self, region_number):
coordinates_for_region = self.coordinates_for_scale_map_region(region_number,
self.cluster.scale_map_region_index)
return self.effective_background_time[coordinates_for_region][0]
def extract_spec_for_region(self, region_number):
data_time = self.effective_data_time_for_region(region_number)
back_time = self.effective_background_time_for_region(region_number)
self.write_temp_region(region_number)
region_file = self.temp_region_filename(region_number)
infile = "{clean}[sky=region({region_file})][bin pi]".format(
clean=self.sc_clean,
region_file=region_file
)
outfile = io.get_path(
"{super_comp_dir}/{obsid}_{region_number}.pi".format(
super_comp_dir=self.cluster.super_comp_dir,
obsid=self.id,
region_number=region_number
))
rt.dmextract(infile=infile, outfile=outfile, clobber=True)
infile = "{back}[sky=region({region_file})][bin pi]".format(
back=self.sc_back,
region_file=region_file
)
outfile = io.get_path(
"{super_comp_dir}/{obsid}_back_{region_number}.pi".format(
super_comp_dir=self.cluster.super_comp_dir,
obsid=self.id,
region_number=region_number
))
rt.dmextract(infile=infile, outfile=outfile, clobber=True)
data_pi = "{super_comp_dir}/{obsid}_{region_number}.pi".format(
super_comp_dir=self.cluster.super_comp_dir,
obsid=self.id,
region_number=region_number
)
back_pi = "{super_comp_dir}/{obsid}_back_{region_number}.pi".format(
super_comp_dir=self.cluster.super_comp_dir,
obsid=self.id,
region_number=region_number
)
warf = "'{super_comp_dir}/{name}_{obsid}.arf'".format(
super_comp_dir=self.cluster.super_comp_dir,
name=self.cluster.name,
obsid=self.id
)
wrmf = "'{super_comp_dir}/{name}_{obsid}.rmf'".format(
super_comp_dir=self.cluster.super_comp_dir,
name=self.cluster.name,
obsid=self.id
)
# Put this background file into the 'grouped' data file for the region
# rt.dmhedit(infile=data_pi, filelist="", operation="add", key="BACKFILE", value=back_pi)
rt.dmhedit(infile=data_pi, filelist="", operation="add", key="EXPOSURE", value=data_time)
rt.dmhedit(infile=data_pi, filelist="", operation="add", key="RESPFILE", value=wrmf)
rt.dmhedit(infile=data_pi, filelist="", operation="add", key="ANCRFILE", value=warf)
rt.dmhedit(infile=data_pi, filelist="", operation="add", key="BACKFILE", value=back_pi)
rt.dmhedit(infile=back_pi, filelist="", operation="add", key="EXPOSURE", value=back_time)
io.append_to_file(self.cluster.spec_lis(region_number), "{}\n".format(data_pi))
io.delete(self.temp_region_filename(region_number))
return data_pi, back_pi
@property
def directory(self):
return io.get_path("{cluster_dir}/{obsid}/".format(cluster_dir=self.cluster.directory,
obsid=self.id))
@property
def ccd_filtered_reprocessed_evt2_filename(self):
return "{evt2}[ccd_id={ccd_filter}]".format(
evt2=self.reprocessed_evt2_filename,
ccd_filter=self.ccd_filter
)
@property # refactor so exclude points to the content of the file, not the file
def exclude(self):
return self.cluster.exclude_file
@property
def exclude_file(self):
return self.cluster.exclude_file
@property
def cropped_clean_infile_string(self):
return "{}[sky=region({})]".format(self.clean,
self.cluster.master_crop_file)
@property
def cropped_background_infile_string(self):
return "{}[sky=region({})]".format(self.back,
self.cluster.master_crop_file)
@property
def oif_filename(self):
return io.get_path("{obs_dir}/oif.fits".format(obs_dir=self.directory))
@property
def oif_fits(self):
return fits.open(self.oif_filename)
@property
def oif_detnam(self):
return self.oif_fits[1].header['detnam']
@property
def response_file_region_covering_ccds(self):
return io.get_path("{analysis_dir}/acisI_region_0.reg".format(
analysis_dir=self.analysis_directory
))
@property
def mask_file(self):
return io.get_filename_matching("{}/*msk1.fits".format(self.reprocessing_directory))[0]
@property
def analysis_directory(self):
return io.get_path("{obs_dir}/analysis/".format(obs_dir=self.directory))
@property
def reprocessing_directory(self):
# return io.get_path("{analysis_dir}/repro/".format(analysis_dir=self.analysis_directory))
return io.get_path("{obs_dir}/repro/".format(obs_dir=self.directory))
@property
def primary_directory(self):
return io.get_path("{observation_dir}/primary/".format(observation_dir=self.directory))
@property
def secondary_directory(self):
return io.get_path("{observation_dir}/secondary/".format(observation_dir=self.directory))
@property
def combined_directory(self):
return self.cluster.combined_directory
@property
def global_response_directory(self):
return io.get_path('{analysis_directory}/globalresponse/'.format(analysis_directory=self.analysis_directory))
@property
def data_filename(self):
return io.get_path('{analysis_dir}/acisI.fits'.format(analysis_dir=self.analysis_directory))
@property
def back_filename(self):
return io.get_path('{analysis_dir}/merged_back.fits'.format(analysis_dir=self.analysis_directory))
@property
def clean(self):
return io.get_path("{analysis_dir}/acisI_clean.fits".format(analysis_dir=self.analysis_directory))
@property
def broad_flux_filename(self):
return io.get_path("{cluster_dir}/{cluster_name}_{obsid}_broad_flux.img".format(
cluster_dir=self.cluster.directory,
cluster_name=self.cluster.name,
obsid=self.id
))
@property
def broad_flux(self):
return io.get_pixel_values(self.broad_flux_filename)
@property
def sc_clean(self):
return io.get_path("{super_comp_dir}/acisI_clean_{obsid}.fits".format(
super_comp_dir=self.cluster.super_comp_dir,
obsid=self.id
))
@property
def back(self):
return self.background_nosrc_filename
#return io.get_path("{analysis_dir}/backI_clean.fits".format(analysis_dir=self.analysis_directory))
@property
def sc_back(self):
return io.get_path("{super_comp_dir}/backI_clean_{obsid}.fits".format(
super_comp_dir=self.cluster.super_comp_dir,
obsid=self.id
))
@property
def fov_file(self):
return io.get_filename_matching("{}/*{}*fov1.fits".format(
self.reprocessing_directory,
self.id
))[0]
@property
def acisI_comb_img(self):
return io.get_path("{combined_dir}/acisI_comb_img-{obsid}.fits".format(
combined_dir=self.combined_directory,
obsid=self.id))
@property
def acisI_combined_image_filename(self):
return self.acisI_comb_img
@property
def acisI_combined_image(self):
if not hasattr(self, "_acisI_comb_image"):
self._acisI_combined_image = io.get_pixel_values(self.acisI_comb_img)
return self._acisI_combined_image
@property
def acisI_combined_image_header(self):
if not hasattr(self, "_acisI_combined_image_header"):
self._acisI_combined_image_header = fits.open(self.acisI_comb_img)[0].header
return self._acisI_combined_image_header
@property
def backI_comb_img(self):
return io.get_path("{combined_dir}/backI_comb_img-{obsid}.fits".format(
combined_dir=self.combined_directory,
obsid=self.id)
)
@property
def backI_comb_temp_img(self):
return io.get_path("{combined_dir}/backI_comb_temp_img-{obsid}.fits".format(
combined_dir=self.combined_directory,
obsid=self.id)
)
@property
def backI_combined_image(self):
if not hasattr(self, "_backI_combined_image"):
self._backI_combined_image = io.get_pixel_values(self.backI_comb_img)
return self._backI_combined_image
@property
def backI_combined_image_header(self):
if not hasattr(self, "_backI_combined_image_header"):
self._backI_combined_image_header = fits.open(self.backI_comb_img)[0].header
return self._backI_combined_image_header
@property
def effbtime(self):
return io.get_path("{acb_dir}/effbtime-{obsid}_circle.dat".format(
acb_dir=self.cluster.acb_dir,
obsid=self.id
))
@property
def effdtime(self):
return io.get_path("{acb_dir}/effdtime-{obsid}_circle.dat".format(
acb_dir=self.cluster.acb_dir,
obsid=self.id
))
@property
def merged_back_lis(self):
return io.get_path("{analysis_dir}/merged_back.lis".format(analysis_dir=self.analysis_directory))
def reproject_combined_mask(self, file_to_match):
from ciao import reproject
print("Reprojecting combined mask for {}".format(self.id))
temp_file = io.temp_filename(self.acisI_combined_mask_file)
reproject(infile=self.acisI_combined_mask_file,
matchfile=file_to_match,
outfile=temp_file,
overwrite=True)
io.move(temp_file, self.acisI_combined_mask_file)
self._acisI_combined_mask = io.get_pixel_values(self.acisI_combined_mask_file)
@property
def acisI_combined_mask_file(self):
return io.get_path("{combined_dir}/acisI_comb_mask-{obsid}.fits".format(
combined_dir=self.combined_directory,
obsid=self.id
))
@property
def acisI_combined_mask(self):
if not hasattr(self, "_acisI_combined_mask"):
self._acisI_combined_mask = io.get_pixel_values(self.acisI_combined_mask_file)
return self._acisI_combined_mask
def reproject_nosrc_combined_mask(self, file_to_match):
from ciao import reproject
print("Reprojecting source removed combined mask for {}".format(self.id))
temp_file = io.temp_filename(self.acisI_nosrc_combined_mask_file)
reproject(infile=self.acisI_nosrc_combined_mask_file,
matchfile=file_to_match,
outfile=temp_file,
overwrite=True)
io.move(temp_file, self.acisI_nosrc_combined_mask_file)
self._acisI_nosrc_combined_mask = io.get_pixel_values(self.acisI_nosrc_combined_mask_file)
@property
def acisI_nosrc_combined_mask_file(self):
return io.get_path("{combined_dir}/acisI_comb_mask_nosrc-{obsid}.fits".format(
combined_dir=self.combined_directory,
obsid=self.id
))
@property
def acisI_nosrc_combined_mask(self):
if not hasattr(self, '_acisI_nosrc_combined_mask'):
self._acisI_nosrc_combined_mask = io.get_pixel_values(self.acisI_nosrc_combined_mask_file)
return self._acisI_nosrc_combined_mask
@property
def acis_nosrc_filename(self):
return io.get_path("{analysis_dir}/acis_nosrc_{obsid}.fits".format(
analysis_dir=self.analysis_directory,
obsid=self.id
))
@property
def background_nosrc_filename(self):
return io.get_path("{analysis_dir}/back_nosrc_{obsid}.fits".format(
analysis_dir=self.analysis_directory,
obsid=self.id
))
@property
def temp_acis_comb_mask_filename(self):
return io.get_path("{combined_dir}/acis_comb_mask_int-{obsid}.fits".format(
combined_dir=self.combined_directory,
obsid=self.id
))
@property
def temp_acis_comb_filename(self):
return io.get_path("{combined_dir}/acis_comb_temp-{obsid}.fits".format(
combined_dir=self.combined_directory,
obsid=self.id
))
@property
def temp_back_comb_filename(self):
return io.get_path("{combined_dir}/back_comb_temp-{obsid}.fits".format(
combined_dir=self.combined_directory,
obsid=self.id
))
@property
def temporary_acis_combined_energy_filtered_infile_string(self):
return "{}[bin sky=4][energy=700:8000]".format(self.temp_acis_comb_filename)
@property
def temporary_back_combined_energy_filtered_infile_string(self):
return "{}[bin sky=4][energy=700:8000]".format(self.temp_back_comb_filename)
@property
def acisI_high_energy_combined_image_file(self):
return io.get_path("{combined_dir}/acisI_hien_comb_img-{obsid}.fits".format(
combined_dir=self.combined_directory,
obsid=self.id
))
@property
def acisI_high_energy_combined_image(self):
if not hasattr(self, "_acisI_high_energy_combined_image"):
self._acisI_high_energy_combined_image = \
io.get_pixel_values(self.acisI_high_energy_combined_image_file)
return self._acisI_high_energy_combined_image
@property
def acisI_high_energy_combined_image_header(self):
if not hasattr(self, "_acisI_high_energy_combined_image_header"):
self._acisI_high_energy_combined_image_header = \
fits.open(self.acisI_high_energy_combined_image_file)[0].header
return self._acisI_high_energy_combined_image_header
@property
def acisI_high_energy_temp_image(self):
return io.get_path("{combined_dir}/acisI_high_energy_int_img_{obsid}.fits".format(
combined_dir=self.combined_directory,
obsid=self.id
))
@property
def backI_high_energy_combined_image_file(self):
return io.get_path("{combined_dir}/backI_hien_comb_img-{obsid}.fits".format(
combined_dir=self.combined_directory,
obsid=self.id
))
@property
def backI_high_energy_combined_image(self):
if not hasattr(self, "_backI_high_energy_combined_image"):
self._backI_high_energy_combined_image = \
io.get_pixel_values(self.backI_high_energy_combined_image_file)
return self._backI_high_energy_combined_image
@property
def backI_high_energy_temp_image(self):
return io.get_path("{combined_dir}/backI_hien_int_img_{obsid}.fits".format(
combined_dir=self.combined_directory,
obsid=self.id
))
@property
def scale_map_region_list_filename(self):
return "{acb_dir}/{cluster_name}_{obsid}_scale_map_region_list.reg".format(
acb_dir=self.cluster.acb_dir,
cluster_name=self.cluster.name,
obsid=self.id
)
@property
def scale_map_region_list(self):
if not hasattr(self, "_scale_map_region_list"):
region_list = []
with open(self.scale_map_region_list_filename, 'r') as f:
reader = csv.reader(f, delimiter='#')
region_list = list(reader)
self._scale_map_region_list = region_list
return self._scale_map_region_list
@scale_map_region_list.setter
def scale_map_region_list(self, circle_list):
with open(self.scale_map_region_list_filename, 'w') as f:
writer = csv.writer(f, delimiter='#')
writer.writerows(circle_list)
@property
def point_spread_function_map_filename(self):
return io.get_path("{cluster_dir}/{name}_{obsid}_psfmap.fits".format(
cluster_dir=self.cluster.directory,
name=self.cluster.name,
obsid=self.id
))
@property
def broad_threshold_image_filename(self):
return io.get_path("{cluster_dir}/{name}_{obsid}_broad_thresh.img".format(
cluster_dir=self.cluster.directory,
name=self.cluster.name,
obsid=self.id
))
@property
def source_map_filename(self):
return io.get_path("{obs_dir}/{obsid}_source_events.fits".format(
obs_dir=self.directory,
obsid=self.id
))
@property
def source_image_filename(self):
return io.get_path("{obs_dir}/{obsid}_image.fits".format(
obs_dir=self.directory,
obsid=self.id
))
@property
def normalized_background_without_sources_filename(self):
return io.get_path("{obs_dir}/{obsid}_nbgd.fits".format(
obs_dir=self.directory,
obsid=self.id
))
@property
def source_cell_map_filename(self):
return io.get_path("{obs_dir}/{obsid}_source_cell.fits".format(
obs_dir=self.directory,
obsid=self.id
))
@property
def source_region_filename(self):
# return io.get_path("{obs_dir}/{obsid}_sources.reg".format(
# obs_dir=self.directory,
# obsid=self.id
# ))
return self.cluster.sources_file
@property
def bad_pixel_file(self):
return io.get_path("{obs_analysis_dir}/bpix1_new.fits".format(
obs_analysis_dir=self.analysis_directory
))
@property
def aux_response_file(self):
return io.get_path("{analysis_directory}/globalresponse/acisI.arf".format(
analysis_directory=self.analysis_directory
))
@property
def original_reprocessed_evt2_filename(self):
evt2_filename = io.get_filename_matching("{}/acis*repro_evt2.fits".format(self.reprocessing_directory))
if isinstance(evt2_filename, list):
if len(evt2_filename) >= 1:
evt2_filename = evt2_filename[-1]
return io.get_path(evt2_filename)
return None
@property
def original_reprocessed_evt2_file_exists(self):
if self.original_reprocessed_evt2_filename:
return True
else:
return False
@property
def original_reprocessed_bad_pixel_filename(self):
bpix1_filename = io.get_filename_matching("{}/*repro_bpix1.fits".format(self.reprocessing_directory))
if isinstance(bpix1_filename, list):
if len(bpix1_filename) >= 1:
bpix1_filename = bpix1_filename[-1]
return io.get_path(bpix1_filename)
return None
@property
def original_reprocessed_bad_pixel_file_exists(self):
if self.original_reprocessed_bad_pixel_filename:
return True
else:
return False
@property
def level_1_event_filename(self):
return io.get_filename_matching("{secondary_dir}/acis*evt1.fits".format(
secondary_dir=self.secondary_directory
))
# return io.get_filename_matching("{analysis_dir}/acis*evt1.fits".format(
# analysis_dir=self.analysis_directory
# ))[0]
@property
def reprocessed_evt2_filename(self):
return self.original_reprocessed_evt2_filename
#return io.get_path("{analysis_dir}/evt2.fits".format(analysis_dir=self.analysis_directory))
@property
def reprocessed_bad_pixel_filename(self):
return self.original_reprocessed_bad_pixel_filename
#return io.get_path("{analysis_dir}/bpix1_new.fits".format(analysis_dir=self.analysis_directory))
@property
def redistribution_matrix_file(self):
return io.get_path("{analysis_directory}/globalresponse/acisI.rmf".format(
analysis_directory=self.analysis_directory
))
@property
def rmf_sc(self):
return io.get_path("{super_comp_dir}/{name}_{obsid}.rmf".format(
super_comp_dir=self.cluster.super_comp_dir,
name=self.cluster.name,
obsid=self.id
))
@property
def arf_sc(self):
return io.get_path("{super_comp_dir}/{name}_{obsid}.arf".format(
super_comp_dir=self.cluster.super_comp_dir,
name=self.cluster.name,
obsid=self.id
))
@property
def exposure_time_file(self):
return io.get_path("{super_comp_dir}/{name}_{obsid}_exptime.dat".format(
super_comp_dir=self.cluster.super_comp_dir,
name=self.cluster.name,
obsid=self.id
))
@property
def exposure_time(self):
return float(io.read_line_number(self.exposure_time_file, 1))
@property
def pcad_asol(self):
return io.get_path("{analysis_dir}/pcad_asol1.lis".format(
analysis_dir=self.analysis_directory
))
@property
def acis_mask(self):
filename = io.get_filename_matching("{repro_dir}/*_msk1.fits".format(
repro_dir=self.reprocessing_directory
))[0]
return filename
@property
def acis_mask_sc(self):
return io.get_path("{super_comp_dir}/{obsid}_msk1.fits".format(
super_comp_dir=self.cluster.super_comp_dir,
obsid=self.id
))
@property
def effective_data_time_file(self):
return io.get_path('{acb_dir}/{name}_effective_data_time_{obs}.npy'.format(
acb_dir=self.cluster.acb_dir,
name=self.cluster.name,
obs=self.id
))
@property
def effective_data_time(self):
if not hasattr(self, "_effective_data_time"):
self._effective_data_time = np.load(self.effective_data_time_file)
return self._effective_data_time
@effective_data_time.setter
def effective_data_time(self, effective_time):
np.save(self.effective_data_time_file, effective_time)
@property
def effective_background_time_file(self):
return io.get_path('{acb_dir}/{name}_effective_background_time_{obs}.npy'.format(
acb_dir=self.cluster.acb_dir,
name=self.cluster.name,
obs=self.id
))
@property
def effective_background_time(self):
if not hasattr(self, "_effective_background_time"):
self._effective_background_time = np.load(self.effective_background_time_file)
return self._effective_background_time
@effective_background_time.setter
def effective_background_time(self, effective_time):
np.save(self.effective_background_time_file, effective_time)
@property
def ccd_merge_list(self):
return io.get_path('{obs_analysis_dir}/acisI.lis'.format(
obs_analysis_dir=self.analysis_directory)
)
@property
def acisI_region_0_filename(self):
return io.get_path('{obs_analysis_dir}/acisI_region_0.reg'.format(obs_analysis_dir=self.analysis_directory))
@property
def acisI_region_0_size(self):
if not hasattr(self, "_acisI_region_0_size"):
try:
acisI_region_0 = io.read_contents_of_file(self.acisI_region_0_filename)
radius = acisI_region_0.split(',')[-1][:-2]
self._acisI_region_0_size = radius
except FileNotFoundError:
pass
return self._acisI_region_0_size
def coordinates_for_scale_map_region(self, region, scale_map_regions):
return np.where(scale_map_regions == region)
def coordinates_for_big_region_index(self, region, region_index_map):
return np.where(region_index_map == region)
def get_region_from_region_number(self, region_number):
with open(self.scale_map_region_list_filename, 'r') as f:
reader = csv.reader(f, delimiter='#')
for row in reader:
if int(row[1]) == int(region_number):
return row[0]
print('Error: region {reg} not found. exiting'.format(reg=region_number))
return -1
def temp_region_filename(self, region_number):
if region_number == -1:
return
temp_region_file_name = io.get_path("{acb_dir}/temp_{region_number}_{obsid}.reg".format(
acb_dir=self.cluster.acb_dir,
region_number=region_number,
obsid=self.id
))
return temp_region_file_name
def write_temp_region(self, region_number):
region = self.get_region_from_region_number(region_number)
io.write_contents_to_file(region, self.temp_region_filename(region_number), False)
def reprocessed_evt2_for_ccd(self, ccd_id):
return io.get_path("{evt2_file}[ccd_id={ccd_id}]".format(evt2_file=self.reprocessed_evt2_filename,
ccd_id=ccd_id))
def acis_ccd(self, ccd_id):
return io.get_path("{analysis_dir}/acis_ccd{id}.fits".format(analysis_dir=self.analysis_directory,
id=ccd_id))
# ********************************************************************
#
# ClusterObj
#
# ********************************************************************
class ClusterObj:
"""Cluster objects are intended to be pythonic representations of
a galaxy cluster for processing through the Xray pypeline.
"""
def __init__(self,
name="",
observation_ids=[],
data_directory="",
hydrogen_column_density=0,
redshift=0,
abundance=0,
last_step_completed=0,
signal_to_noise=50
):
"""
Initialization method.
Parameters
----------
name : str
The name of the cluster. This variable is used to name directories and is the prefix for many filenames.
There should be no spaces in the name.
E.g. Abell 115 -> A115 or Abell_115
observation_ids : str[]
A list of chandra observation ids for download. These should all be for the same galaxy cluster.
E.g. 3233, 15175, 15144
data_directory : str
The main data repository directory. This is the directory where the script will create a cluster.name
subdirectory.
E.g. /home/user/username/cluster_data/
hydrogen_column_density : float
The hydrogen column density for the cluster.
E.g. 0.2
redshift : float
The redshift of the cluster
E.g. 0.197
abundance : float
The solar abundance of the galaxy cluster.
E.g. 0.2
last_step_completed : int
This is a state variable indicating the last step of the pypeline that was completed. It indicates where the
pypeline will begin if run without any arguments.
"""
self.name = name
self.data_directory = data_directory
self.hydrogen_column_density = hydrogen_column_density
self.redshift = redshift
self.abundance = abundance
self._last_step_completed = last_step_completed
self.observation_ids = observation_ids
self.observations = [Observation(obsid=x, cluster=self) for x in self.observation_ids]
self.signal_to_noise = float(signal_to_noise)
def write_cluster_data(self):
"""
:return:
"""
if ("" == self.name) or ("" == self.data_directory):
print("Trying to write cluster data file before any work complete")
sys.exit(1)
cluster_config = configparser.ConfigParser()
cluster_dict = dict(self)
cluster_dict['observation_ids'] = ','.join(self.observation_ids)
cluster_config['cluster'] = cluster_dict
try:
with open(self.configuration_filename, 'w') as configfile:
cluster_config.write(configfile)
print("Cluster data written to {}".format(self.configuration_filename))
except FileExistsError:
print("File exists and I can't overwrite! File: {}".format(self.configuration_filename))
sys.exit(1)
except FileNotFoundError:
if self.data_directory != config.sys_config.data_directory:
self.data_directory = config.sys_config.data_directory
self.write_cluster_data()
else:
print("Cannot write cluster config to {}!".format(self.configuration_filename))
print("Try updating your configuration file to reflect its current path.")
sys.exit(1)
return
def initialize_cluster(self):
print("Initializing cluster object")
self.get_cluster_info_from_user()
io.make_directory(self.directory)
self.write_cluster_data()
io.make_initial_directories(self)
print("Initialization complete.") # Next step is to run the following command: ")
print("Please continue running ClusterPyXT on {name}".format(name=self.name))
def get_cluster_info_from_user(self):
self.name = io.get_user_input("Enter the cluster name: ", "cluster name")
self.data_directory = os.path.normpath(config.sys_config.data_directory)
self.observation_ids = get_observation_ids()
self.observations = [Observation(obsid=x, cluster=self) for x in self.observation_ids]
print()
get_fitting_values = \
io.check_yes_no("Enter values for fitting (nH, z, abundance, S/N) now? [y/n]")
if get_fitting_values:
self.hydrogen_column_density = io.get_user_input(
"Enter the hydrogen column density for {} (on order of 10^22, e.g. 0.052 for 5.2e20): ".format(self.name),
"hydrogen column density")
self.redshift = io.get_user_input("Enter the redshift of {}: ".format(self.name), "redshift")
self.abundance = io.get_user_input("Enter the abundance: ", "abundance")
self.signal_to_noise = io.get_user_input("Enter the desired signal to noise ratio: ",
"the signal to noise ratio")
else:
print("Before completing the ACB portion of the pypeline, you need "
"to edit the configuration file ({config}) "
"and update the values for hydrogen column density, redshift, "
"and abundance.".format(config=self.configuration_filename))
self.hydrogen_column_density = "Update me! (on order of 10^22 e.g. 0.052 for 5.2e20)"
self.redshift = "Update me! (e.g. 0.192)"
self.abundance = "Update me! (e.g. 0.2)"
self.signal_to_noise = "Update me! (e.g. 50)"
self._last_step_completed = 0
return
def obs_directory(self, obsid):
return io.get_path("{}/{}".format(self.directory, obsid))
def obs_analysis_directory(self, obsid):
return io.get_path("{}/analysis/".format(self.obs_directory(obsid)))
def __repr__(self):
return "<ClusterObj name:{}>".format(self.name)
def __str__(self):
ret_str = """Cluster: {}
Observations IDs: {}
Data Directory: {}
Hydrogen Column Density: {}
Redshift (z): {}
Abundance: {}
Signal to Noise Ratio: {}
Last Step Completed: {}""".format(self.name,
self.observation_ids,
self.data_directory,
self.hydrogen_column_density,
self.redshift,
self.abundance,
self.signal_to_noise,
self._last_step_completed
)
return ret_str
def __iter__(self):
yield 'name', self.name
yield 'observation_ids', str(self.observation_ids)
yield 'data_directory', str(self.data_directory)
yield 'hydrogen_column_density', str(self.hydrogen_column_density)
yield 'redshift', str(self.redshift)
yield 'abundance', str(self.abundance)
yield 'signal_to_noise', str(self.signal_to_noise)
yield 'last_step_completed', str(self._last_step_completed)
return
@property
def directory(self):
"""The directory containing the cluster data"""
return io.get_path("{data_dir}/{cluster_name}/".format(
data_dir=self.data_directory,
cluster_name=self.name
))
@property
def configuration_filename(self):