-
Notifications
You must be signed in to change notification settings - Fork 6
/
lightcurves.py
1415 lines (1124 loc) · 52.6 KB
/
lightcurves.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
#
# Copyright (C) 2008, 2009, 2010, 2011, 2014, 2015, 2016, 2017, 2018, 2019, 2021, 2023
# Smithsonian Astrophysical Observatory
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
"""
Script:
lightcurves.py
Aim:
Provide simple light-curve cleaning routines aimed at removing
flares from Chandra light curves, in particular background light curves.
This is a combination of code based on the lc_clean routine from
Maxim Markevitch (see http://hea-www.harvard.edu/~maxim/axaf/acisbg/)
and a simple sigma-clipped algorithm
Threads:
https://cxc.harvard.edu/ciao/threads/acisbackground/
https://cxc.harvard.edu/ciao/threads/filter_ltcrv/
"""
from itertools import groupby
from operator import itemgetter
import tempfile
import numpy as np
import pycrates as pcr
from ciao_contrib.runtool import dmcopy, dmgti
import os, matplotlib
if "DISPLAY" in os.environ and os.environ["DISPLAY"] != "":
matplotlib.interactive(True)
matplotlib.rcParams["toolbar"] = "None"
else:
matplotlib.use("Agg")
import matplotlib.pyplot as plt
# NOTE: the lc_sigma_uclip algorithm is not ready for release
# __all__ = ("lc_sigma_clip", "lc_sigma_uclip", "lc_clean")
__all__ = ("lc_sigma_clip", "lc_clean")
__revision = "16 May 2023"
def _write_gti_text(outfile, tstart, tend):
"""Create a GTI file, in TEXT/DTF format, using the
tstart/tend values, and then does a dmcopy on it
to convert it to FITS format (to avoid some issues that
were found in trying to use a TEXT/DTF format GTI file
to filter an event file; these may have been fixed by
now).
"""
# Could use a crate to avoid having to create the output
# manually, but it is not clear to me how this can be done
# (since crates does not, as of CIAO 4.4, propagate
# subspace information), so do it manually. This should
# be fixed in more-recent CIAO versions, so need to
# review to see if can remove this step.
#
# (ofd, tname) = tempfile.mkstemp(text=True)
tfile = tempfile.NamedTemporaryFile(suffix=".gti", mode='w+')
tname = tfile.name
tfile.write("#TEXT/DTF\n")
def card(name, value, comment=None, unit=None):
"Add the record card to the file"
tfile.write(f"{name:<8s}= ")
# following checks not ideal but sufficient for now
if isinstance(value, str):
sfmt = '"{}"'
svalue = value
elif isinstance(value, bool):
sfmt = "{:20s}"
if value:
svalue = "T"
else:
svalue = "F"
elif isinstance(value, int):
sfmt = "{:20d}"
svalue = value
else:
# assume a float
sfmt = "{:20.13e}"
svalue = value
tfile.write(sfmt.format(svalue))
if comment is None and unit is None:
tfile.write("\n")
return
tfile.write(" /")
if unit is not None:
tfile.write(f" [{unit}]")
if comment is not None:
tfile.write(" " + comment)
tfile.write("\n")
card("XTENSION", "TABLE")
card("HDUNAME", "FILTER")
card("EXTNAME", "FILTER")
card("TFIELDS", 1)
card("HDUCLASS", "ASC")
card("HDUCLAS1", "FILTER")
card("ORIGIN", "ASC", comment="Source of FITS file")
card("CREATOR", f"lightcurves - {__revision}",
comment="tool that created this output")
card("CONTENT", "GTI", comment="Data product identification")
card("TTYPE1", "TIME", comment="Time column")
card("TFORM1", "1D", comment="data format of field.")
card("TUNIT1", "s", comment="physical unit of field.")
card("DSTYP1", "TIME", comment="DM Keyword: Descriptor name.")
card("DSVAL1", "TABLE", unit="s")
card("DSFORM1", "D", comment="DM Keyword: Descriptor datatype.")
card("DSUNIT1", "s", comment="DM Keyword: Descriptor unit.")
card("DSREF1", ":GTI")
tfile.write("END\n\n\n")
card("XTENSION", "TABLE")
card("HDUNAME", "GTI")
card("EXTNAME", "GTI")
card("TFIELDS", 2)
card("TTYPE1", "START")
card("TFORM1", "1D", comment="data format of field.")
card("TUNIT1", "s", comment="physical unit of field.")
card("TTYPE2", "STOP")
card("TFORM2", "1D", comment="data format of field.")
card("TUNIT2", "s", comment="physical unit of field.")
card("CONTENT", "GTI", comment="Data product identification")
card("HDUCLASS", "OGIP")
card("HDUCLAS1", "GTI")
card("HDUCLAS2", "STANDARD")
card("ORIGIN", "ASC", comment="Source of FITS file")
card("CREATOR", f"lightcurves - {__revision}",
comment="tool that created this output")
card("DSTYP1", "TIME", comment="DM Keyword: Descriptor name.")
card("DSVAL1", "TABLE", unit="s")
card("DSFORM1", "D", comment="DM Keyword: Descriptor datatype.")
card("DSUNIT1", "s", comment="DM Keyword: Descriptor unit.")
card("DSREF1", ":GTI")
tfile.write("END\n\n")
for (tlo, thi) in zip(tstart, tend):
tfile.write(f"{tlo:19.13e} {thi:19.13e}\n")
tfile.flush()
# Convert to FITS format and clean up.
#
dmcopy.punlearn()
dmcopy(tname, outfile, clobber=True)
# os.unlink(tname)
# We stick pretty-much everything in a class which is a
# fairly-poor design
#
class LightCurve:
"""Store data from a lightcurve and provide
methods to manipulate and display the data"""
def __init__(self, filename, verbose=1):
self.filename = filename
self.verbose = verbose
self.__read_data()
# The storage is rather redundant here (e.g. filter and clean_gti
# are the same) but was originally written to support easy comparison
# to the S-Lang version. This could be cleaned up, but is a low
# priority until it turns out to be a problem.
#
self.clean_filter = None
self.clean_gti = None
self.clean_bti = None
self.clean_min_rate = None
self.clean_max_rate = None
self.clean_mean_rate = None
self.method = None
self.userlimit = None
def report(self, msg):
"""Display the message if verbose is not 0."""
if self.verbose == 0:
return
print(msg)
def __read_data(self):
"""
Reads in the data from the filename and performs simple validation.
We should use TIMEUNIT and TIMEPIXR to handle non-standard cases,
but ignore for now.
"""
cr = pcr.read_file(self.filename)
if cr.column_exists("count_rate"):
self.ratename = "count_rate"
elif cr.column_exists("rate"):
self.ratename = "rate"
else:
raise IOError(f"No count_rate or rate column in file '{self.filename}'")
self.time = cr.get_column("time").values.copy()
if self.time.size < 1:
raise IOError(f"No data read in from the lightcurve '{self.filename}'")
elif self.time.size < 2:
raise IOError(f"Only 1 time bin found in the lightcurve '{self.filename}'")
self.report(f"Total number of bins in lightcurve = {self.time.size:d}")
self.rate = cr.get_column(self.ratename).values.copy()
if cr.column_exists("exposure"):
self.exposure = cr.get_column("exposure").values.copy()
self.bin_width = self.exposure.max()
# We do not make use of this filter, so commenting out for now
# filter = self.exposure <= 0.0
if self.verbose > 0:
nsmall = sum((self.exposure < self.bin_width) &
(self.exposure > 0.0))
n0 = sum(self.exposure <= 0.0)
print(f"Max length of one bin = {self.bin_width:g} s")
print(f"Num. bins with a smaller exp. time = {nsmall:d}")
print(f"Num. bins with exp. time = 0 = {n0:d}")
else:
self.exposure = None
self.bin_width = None
if cr.column_exists("time_min") and cr.column_exists("time_max"):
self.time_min = cr.get_column("time_min").values.copy()
self.time_max = cr.get_column("time_max").values.copy()
self.time_offset = self.time_min[0]
else:
self.time_min = None
self.time_max = None
self.time_offset = self.time[0]
self.labels = {}
self.add_label(cr, "OBJECT")
self.add_label(cr, "OBS_ID")
self.add_label(cr, "EXPOSURE", protect=False)
self.add_label(cr, "DTCOR", protect=False)
self.add_label(cr, "ONTIME", protect=False)
self.add_label(cr, "TIMEDEL", protect=False)
self.filter = self.rate > 0.0
if any(self.filter) is False:
raise IOError(f"No rows with a count rate > 0 ({self.filename})")
# Is there a better way to do this?
self.gti = self.filter
self.bti = np.logical_not(self.filter)
if self.verbose > 0 and any(self.bti):
print(f"Number of bins with a rate of 0 ct/s = {self.bti.sum()}\n")
self.mean_rate_original = self.rate.mean()
self.mean_rate_filtered = self.rate[self.gti].mean()
def add_label(self, crate, name, protect=True):
"""If there is a keyword of the given name in the crate, add
it to the store. If the keyword does not exist do
nothing.
The protect argument is now ignored.
"""
# crate.get_key_value returns None on a missing lookup,
# pycrates.get_keyval raises a LookupError when the key is
# not found.
#
val = crate.get_key_value(name)
if val is None:
return
self.labels[name] = val
def time_to_offset(self, t):
"""Convert the time(s) in t, assumed to be in seconds, to an
offset in kiloseconds
"""
return (t - self.time_offset) / 1.0e3
def calculate_valid_time_bins(self, minlength=1):
"""Returns (tstart, tend, exposure, flag) for those time bins that pass
the filter (so calculate_filter() must have already been called).
flag is True if all intervals pass the minlength filter (i.e. if using
minlength has no affect here). exposure is the exposure length of
each interval (if the input light curve had an EXPOSURE column),
otherwise None.
The minlength parameter gives the minimum number of consecutive
bins that must pass the filter to still remain valid (i.e. if
changed from 1 it acts as an additional filter).
"""
if self.clean_min_rate is None:
raise ValueError("calculate_filter() must be run before calculate_valid_time_bins()")
# Loop through the time bins and ensure that each set of
# selected times contains >= minlength bins
#
if self.time_min is None:
xlo = self.time
else:
xlo = self.time_min
if self.time_max is None:
xhi = self.time
else:
xhi = self.time_max
# A simplified version of run-length encoding to calculate
# those bins that pass both the count rate limit and the
# minlength test.
#
# We add 0's onto the start and end of the filter to simplify
# handling the start and end bins.
#
filter = np.concatenate(([0], self.clean_filter * 1, [0]))
x = np.diff(filter)
pstart, = np.where(x == 1)
pend, = np.where(x == -1)
if pstart.size != pend.size:
raise ValueError(f"Internal error: pstart/end lengths are {pstart.size} and {pend.size}!")
# Filter out those intervals that do not contain minlength
# elements.
#
pnum = pend - pstart
lcheck = pnum >= minlength
if not np.any(lcheck):
raise ValueError(f"Error: there are no periods which pass the minlength ({minlength}) check!")
i, = np.where(lcheck)
pend -= 1
xlo = xlo[pstart[i]]
xhi = xhi[pend[i]]
if self.exposure is None:
exposure = None
else:
exposure = np.asarray([self.exposure[pstart[j]:pend[j] + 1].sum()
for j in i])
return (xlo, xhi, exposure, np.all(lcheck))
# Should be able to clean up this mess somewhat
#
def report_userlimit_using_times(self, tlo=None, thi=None,
exposure=None, minlength=1):
"""Display, to the screen, the valid times as time filters along with
the corresponding exposure times. At the end indicate the amount of
valid exposure time that has been retained, although this is only
approximate as it depends on the GTI intervals in the input file
as well."""
if self.verbose < 1:
return
if (tlo is None) != (thi is None):
raise ValueError("tlo and thi must either both be None or both be arrays")
if tlo is None:
(tlo, thi, exposure, tflag) = \
self.calculate_valid_time_bins(minlength)
ctr = np.arange(tlo.size) + 1
if exposure is None:
exposure = (thi - tlo) / 1.0e3
# There is no guarantee that the exposure time of this bin is
# the same as (thi-tlo), but we include the DTCOR factor just
# in case
#
if 'DTCOR' in self.labels:
exposure *= self.labels["DTCOR"]
else:
# Assume that the exposure values include any DTCOR factor,
# and are in seconds
exposure /= 1.0e3
# ustr = [" ((time >= {:f}) && (time < {:f})) ; {:.2f} ksec, bin {}".format(s,e,d,c) for c,s,e,d in zip(ctr,tlo,thi,exposure)]
ustr = [f" ((time >= {repr(s)}) && (time < {repr(e)})) ; {d:.2f} ksec, bin {c}"
for c, s, e, d in zip(ctr, tlo, thi, exposure)]
n = len(tlo)
if n == 1:
print(ustr[0])
else:
print("\n".join(ustr))
print("")
if 'EXPOSURE' in self.labels:
print(" Exposure time of lightcurve = {:.2f} ks".format(self.labels["EXPOSURE"] / 1000.0))
print(f" Filtered exposure time = {exposure.sum():.2f} ks")
if 'DTCOR' in self.labels:
print(f" DTCOR value = {self.labels['DTCOR']:g}")
def create_userlimit_using_rates(self, minlim, maxlim):
"""Sets the userlimit field for the rate column to
lie between minlim and maxlim."""
self.userlimit = f"({self.ratename}>{repr(minlim)} && {self.ratename}<{repr(maxlim)})"
self.report(f"GTI limits calculated using a count-rate filter:\n {self.userlimit}\n")
self.report("The corresponding times are:")
self.report_userlimit_using_times(minlength=1)
# As moving towards writing out the GTI file manually for this case the
# semantics of this routine no longer matches the name, in that we end
# up storing the lo/hi values for use by _write_gti_text
#
def create_userlimit_using_times(self, lo, hi, exposure=None, minlength=1):
"Sets the userlimit field for time values between lo and hi."
# Hmm, should we just go for full accuracy? Not really worth it for
# most cases
# ulimits = ["((time>={})&&(time<{}))".format(repr(a), repr(b)) for a,b in zip(lo,hi)]
ulimits = [f"((time>={a:f})&&(time<{b:f}))"
for a, b in zip(lo, hi)]
n = len(lo)
if n == 1:
self.userlimit = ulimits[0]
else:
self.userlimit = "({})".format("||".join(ulimits))
# HACK: save the lo/hi/exposure values for use by _write_gti_text
self.userlimit_bins = (lo, hi, exposure)
self.report("GTI limits calculated using a time filter:")
self.report_userlimit_using_times(tlo=lo, thi=hi,
exposure=exposure,
minlength=minlength)
def _add_plot_labels(self):
"""Add filename/object/obsid labels to the current plot,
and make sure the borders are not displayed for this plot
Ensure these labels are at a lower depth than the main
plot items.
"""
trans = plt.gcf().transFigure
plt.text(0.015, 0.035, self.filename.split("/")[-1],
fontsize=8, horizontalalignment="left",
verticalalignment="top",
transform=trans)
plt.text(0.985, 0.035, self.method, fontsize=8,
horizontalalignment="right", verticalalignment="top",
transform=trans)
if 'OBJECT' in self.labels:
plt.text(0.015, 0.965, self.labels["OBJECT"],
fontsize=8, horizontalalignment="left",
verticalalignment="bottom",
transform=trans)
if 'OBS_ID' in self.labels:
plt.text(0.985, 0.965, "obsid={}".format(self.labels["OBS_ID"]),
fontsize=8, horizontalalignment="right",
verticalalignment="bottom",
transform=trans)
def plot(self, rateaxis="y", gcol="lime", erase=True):
"""Plot up the light curves.
The rateaxis argument determines whether the rate is drawn on
the Y axis or X axis ("y" or "x" respectively). The gcol
argument is the color to use to draw the "good" points.
The erase argument is no-longer used.
"""
if self.clean_gti is None:
raise ValueError("calculate_filter() must be run before plot()")
gti = self.clean_gti
bti = self.clean_bti
# What data are we plotting?
#
t = self.time_to_offset(self.time)
x1g = t[gti]
y1g = self.rate[gti]
if any(bti):
x1b = t[bti]
y1b = self.rate[bti]
else:
x1b, y1b = None, None
# We need to calculate the binning for the histograms, so we
# pick a bin size that is the minimum of 0.01 and (rmax-rmin)/5.0
# where rmax/min are the max/min count rates of the filtered data.
# As we have now had reports of this causing a problem - admittedly
# only for problematic data sets - then we now include a rather
# arbitrary limit on the number of bins.
#
rmin = self.clean_min_rate
rmax = self.clean_max_rate
rwidth = min(0.01, (rmax - rmin) / 5.0)
# Not bothered if number of bins is off by 1 here
dmin = self.rate.min()
dmax = self.rate.max()
nbins = (dmax - dmin) / rwidth
if nbins > 1000:
print(f"Warning: Default bin width of {rwidth:g} count/s is too small as it produces")
print(f" {nbins} bins.")
rwidth = (dmax - dmin) / 1000.0
print(f" Replacing with a width of {rwidth:g} count/s")
print(" This may indicate that the lightcurve contains strong flares that")
print(" require manual filtering.\n")
ledges = np.arange(dmin, dmax, rwidth)
ledges = np.append(ledges, ledges[-1] + rwidth)
(y2a, _) = np.histogram(self.rate, bins=ledges)
xlo2a = ledges[:-1]
xhi2a = ledges[1:]
(y2g, _) = np.histogram(y1g, bins=ledges)
xlo2g = xlo2a
xhi2g = xhi2a
# Filter out zero-rate bins as they are a visual distraction
#
i, = np.where(y2a > 0)
xlo2a = xlo2a[i]
xhi2a = xhi2a[i]
y2a = y2a[i]
i, = np.where(y2g > 0)
xlo2g = xlo2g[i]
xhi2g = xhi2g[i]
y2g = y2g[i]
# Set up the plot labels
#
title = r"mean rate={0:g} {1}".format(self.clean_mean_rate,
r"$\mathrm{s}^{-1}$")
ratelabel = r"Count Rate $\left[\mathrm{s}^{-1}\right]$"
# Create the plot
#
# setup figure
if rateaxis == "y":
# from matplotlib import tight_layout # deprecated in Matplotlib 3.5, breaking in 3.6
#
# fig, axs = plt.subplots(2, 1, tight_layout=False)
# hpad = tight_layout.get_tight_layout_figure(fig,
# axs,
# tight_layout.get_subplotspec_list(axs),
# tight_layout.get_renderer(fig))["hspace"]
# fig.subplots_adjust(hspace=2*hpad)
fig, axs = plt.subplots(2, 1, squeeze=True)
hpad = axs[0].get_position().y0 - axs[1].get_position().y1
fig.subplots_adjust(hspace=6*hpad)
else:
fig, axs = plt.subplots(2, 1, sharex=True)
fig.subplots_adjust(hspace=0)
try:
# setup subplots and figure labels
self._add_plot_labels()
fig.align_ylabels(axs[:])
axs[0].minorticks_on()
axs[1].minorticks_on()
axs[0].tick_params(axis="both", direction="in", which="both",
bottom=True, top=True, left=True, right=True)
axs[1].tick_params(axis="both", direction="in", which="both",
bottom=True, top=True, left=True, right=True)
# plot 1: lightcurve
#
# The "bad" set of points are added first, if they
# exist, so that they are drawn behind the "good" points
#
axs[0].set_title(title, fontsize=12)
plotopts = {"linestyle": "none", "fillstyle": "full"}
if any(bti):
if rateaxis == "y":
x = x1b
y = y1b
else:
x = y1b
y = x1b
plotopts["gid"] = "bad" # id = "bad"
plotopts["color"] = "blue"
plotopts["marker"] = "s" # symbol.style = "square"
plotopts["markersize"] = 3.5 # symbol.size = 2
axs[0].plot(x, y, **plotopts)
if rateaxis == "y":
x = x1g
y = y1g
else:
x = y1g
y = x1g
plotopts["gid"] = "good" # id = "good"
plotopts["marker"] = "o" # symbol.style = "circle"
plotopts["markersize"] = 6 # symbol.size = 4
plotopts["color"] = gcol
axs[0].plot(x, y, **plotopts)
if rateaxis == "y":
axs[0].axhline(y=self.clean_mean_rate, linestyle="dotted")
axs[0].set_ylabel(ratelabel, fontsize=10)
axs[0].set_xlabel(r"$\Delta$ Time [ks]", fontsize=10)
else:
axs[0].axvline(x=self.clean_mean_rate, linestyle="dotted")
axs[0].tick_params(labelbottom=False)
axs[0].set_ylabel(r"$\Delta$ Time [ks]", fontsize=10)
# plot 2: histogram
#
if any(bti):
# find consecutive indices
# - first group up consecutive value
dx2a = xhi2a - xlo2a # get binsize
x2a_test = [round(n) for n in xhi2a / dx2a]
# integer of the bin for grouping ID
groups = [list(map(itemgetter(1), g))
for k, g in groupby(enumerate(x2a_test),
lambda z: z[0] - z[1])]
grp_a_ind = []
# open list of indicies
for g in groups:
if len(g) == 1:
ind = x2a_test.index(g[0])
grp_a_ind.append(ind)
else:
ind_min = x2a_test.index(min(g))
ind_max = x2a_test.index(max(g))
grp_a_ind.append((ind_min, ind_max))
for ind in grp_a_ind:
if type(ind) == int:
xa_lo = np.array([xlo2a[ind]])
xa_hi = np.array([xhi2a[ind]])
ya = np.array([y2a[ind]])
else:
xa_lo = np.array(xlo2a[min(ind):max(ind) + 1])
xa_hi = np.array(xhi2a[min(ind):max(ind) + 1])
ya = np.array(y2a[min(ind):max(ind) + 1])
# 'concatenates' are to add line drop at histogram edges
xa = np.append(xa_lo, xa_hi[-1])
xa = np.concatenate(([xa_lo[0]], xa))
ya = np.append(ya, 0)
ya = np.concatenate(([0], ya))
# plot entire histogram as step function
axs[1].step(x=xa, y=ya, where="post",
label="everything", linewidth=1.25,
color="blue")
# try to plot up histogram of good data points
# - find consecutive indices
# - first group up consecutive value
dx2g = xhi2g - xlo2g # binsize
x2g_test = [round(n) for n in xhi2g / dx2g]
groups = [list(map(itemgetter(1), g))
for k, g in groupby(enumerate(x2g_test),
lambda z: z[0] - z[1])]
grp_g_ind = []
# open list of indicies
for g in groups:
if len(g) == 1:
ind = x2g_test.index(g[0])
grp_g_ind.append(ind)
else:
ind_min = x2g_test.index(min(g))
ind_max = x2g_test.index(max(g))
grp_g_ind.append((ind_min, ind_max))
for ind in grp_g_ind:
if type(ind) == int:
xg_lo = np.array([xlo2g[ind]])
xg_hi = np.array([xhi2g[ind]])
yg = np.array([y2g[ind]])
else:
xg_lo = np.array(xlo2g[min(ind):max(ind) + 1])
xg_hi = np.array(xhi2g[min(ind):max(ind) + 1])
yg = np.array(y2g[min(ind):max(ind) + 1])
# 'concatenates' are to add line drop at histogram edges
xg = np.append(xg_lo, xg_hi[-1])
xg = np.concatenate(([xg_lo[0]], xg))
yg = np.append(yg, 0)
yg = np.concatenate(([0], yg))
# plot entire histogram as step function
axs[1].step(x=xg, y=yg, where="post", label="good",
linewidth=1.25, color=gcol)
axs[1].axvline(x=self.clean_mean_rate, linestyle="dotted")
axs[1].set_xlim(auto=True)
axs[1].set_ylim(bottom=0, auto=True)
if rateaxis == "x":
y_minor_ticks = axs[1].yaxis.get_minorticklocs()
dytick = y_minor_ticks[1] - y_minor_ticks[0]
axs[1].set_ylim(bottom=0, top=y_minor_ticks[-1] + 2.5 * dytick)
axs[1].set_ylabel("Number", fontsize=10)
axs[1].set_xlabel(ratelabel, fontsize=10)
except Exception:
plt.close()
raise
return axs
def plot_gti_limits(self, mplaxs, gtiname, rateaxis="y",
pattern="crisscross", pcol="red"):
"Plot up the GTI limits from the GTI block of gtiname on the rate plot"
if pattern == "none":
return
cr = pcr.read_file(f"{gtiname}[gti]")
try:
xr = mplaxs[0].get_xlim()
yr = mplaxs[0].get_ylim()
startvals = self.time_to_offset(pcr.copy_colvals(cr, "start"))
stopvals = self.time_to_offset(pcr.copy_colvals(cr, "stop"))
edges = []
if rateaxis == "y":
start = np.append(startvals, xr[1])
end = np.append(xr[0], stopvals)
y_minor_ticks = mplaxs[0].yaxis.get_minorticklocs()
dytick = y_minor_ticks[1] - y_minor_ticks[0]
yrs = [yr[0] - dytick, yr[1] + dytick]
mplaxs[0].set_ylim(bottom=min(yrs),
top=max(yrs))
else:
start = np.append(startvals, yr[1])
end = np.append(yr[0], stopvals)
x_minor_ticks = mplaxs[0].xaxis.get_minorticklocs()
dxtick = x_minor_ticks[1] - x_minor_ticks[0]
xrs = [xr[0] - dxtick, xr[1] + dxtick]
mplaxs[0].set_xlim(left=min(x_minor_ticks),
right=max(x_minor_ticks))
lines = mplaxs[0].lines
zorder = max([line.get_zorder() for line in lines]) + 1
for key in mplaxs[0].spines.keys():
mplaxs[0].spines[key].zorder = zorder + 1
ropts = {}
# The choices for this parameter are: nofill, solid, updiagonal,
# downdiagonal, horizontal, vertical, crisscross, grid, polkadot
#
# No longer support: brick, zigzag, hexagon, wave
ropts["alpha"] = 0.85
ropts["facecolor"] = pcol
ropts["zorder"] = zorder
ropts["facecolor"] = "none"
ropts["edgecolor"] = pcol
if pattern.lower() == "solid":
ropts["hatch"] = "none"
ropts["facecolor"] = pcol
ropts["zorder"] = 0
elif pattern.lower() == "nofill":
ropts["hatch"] = ""
ropts["alpha"] = 0.5
elif pattern.lower() == "updiagonal":
ropts["hatch"] = "///"
elif pattern.lower() == "downdiagonal":
ropts["hatch"] = "\\\\\\"
elif pattern.lower() == "horizontal":
ropts["hatch"] = "---"
elif pattern.lower() == "vertical":
ropts["hatch"] = "|||"
elif pattern.lower() == "crisscross":
ropts["hatch"] = "xxx"
elif pattern.lower() == "grid":
ropts["hatch"] = "+++"
elif pattern.lower() == "polkadot":
ropts["hatch"] = "..."
# add regions blocking the bad intervals
for s, e in zip(start, end):
if rateaxis == "y":
if pattern.lower() == "nofill":
# edge's linestyle and linewidth need to precede all other options
# to be recognized, due to bug in matplotlib >1.5.x;
# dictionary order is randomized. From Python 3.6 onwards,
# the standard dict type maintains insertion order by default.
mplaxs[0].fill_betweenx(y=yrs, x1=s, x2=e,
linestyle="dashed",
linewidth=1, **ropts)
else:
ropts["linewidth"] = matplotlib.rcParams["hatch.linewidth"] = 0.25
mplaxs[0].fill_betweenx(y=yrs, x1=s, x2=e, **ropts)
edges.extend([s, e])
else:
if pattern.lower() == "nofill":
mplaxs[0].fill_between(x=xrs, y1=s, y2=e,
linestyle="dashed",
linewidth=1, **ropts)
else:
ropts["linewidth"] = matplotlib.rcParams["hatch.linewidth"] = 0.25
mplaxs[0].fill_between(x=xrs, y1=s, y2=e, **ropts)
edges.extend([s, e])
if rateaxis == "y":
mplaxs[0].set_xlim(left=min(edges), right=max(edges))
else:
mplaxs[0].set_ylim(bottom=min(edges), top=max(edges))
except Exception:
# Why do we need this again?
raise ValueError("Failed to highlight 'bad' time intervals in plotted lightcurve.")
def create_gti_file(self, outfile):
"""Create a GTI file called outfile based on those time periods
from infile that match the given filter (which is a string of
the form accepted by the userlimit parameter of dmgti
"""
if self.userlimit is None:
raise ValueError("calculate_gti_filter() must be run before create_gti_file()")
self.report("\nCreating GTI file")
# Do we write out the GTI manually?
if hasattr(self, "userlimit_bins"):
_write_gti_text(outfile, self.userlimit_bins[0],
self.userlimit_bins[1])
else:
dmgti.punlearn()
dmgti(self.filename, outfile, self.userlimit, clobber=True,
verbose=self.verbose)
self.report(f"Created: {outfile}")
class CleanLightCurve(LightCurve):
"Light curve filtering using the same method as the ACIS background files"
def __init__(self, filename, verbose=1):
LightCurve.__init__(self, filename, verbose=verbose)
if self.exposure is None:
raise IOError(f"The lightcurve '{filename}' does not contain an EXPOSURE column!")
self.method = "lc_clean"
def check_valid(self, minfrac):
"""Ensure that the fraction of good bins is >= minfrac (excluding
the 0-rate bins from the calculation). Throws a ValueError if
the condition is not met."""
nrows = self.filter.sum()
ngood = self.clean_filter.sum()
frac = (ngood + 0.0) / nrows
if frac < minfrac:
raise ValueError("Fraction of bins that are good ({:g}, {} of {}) is below limit of {:g}!".format(frac, ngood, nrows, minfrac))
def calculate_filter(self, mean=None, clip=3.0, sigma=None, scale=1.2):
"Filter the light curve."
# Do we need to calculate an initial mean level?
#
if mean is None:
# The sigma is calculated assuming "poisson" statistics for the
# mean value, so we have sqrt (mean-rate * bin-width) and then
# we want to convert it back to a rate, to give
# sqrt (mean-rate / bin-width)
#
omean = self.rate[self.filter].mean()
osigma = np.sqrt(omean / self.bin_width)
minval = omean - clip * osigma
maxval = omean + clip * osigma
gti, = np.where((self.rate > minval) & (self.rate < maxval))
if gti.size == 0:
raise ValueError("Unable to calculate an initial GTI mean rate level via sigma clipping; the unclipped count rate is {0:g} +/- {1:g} ct/s".format(omean,clip*osigma))
mean = self.rate[gti].mean()
self.report(f"Calculated an initial mean (sigma-clipped) rate of {mean:g} ct/s")
else:
self.report(f"Using a fixed mean rate of {mean:g} ct/s")
# Calculate the limits based on the mean value and sigma/scale clipping
#
if sigma is None:
self.clean_min_rate = mean / scale
self.clean_max_rate = mean * scale
self.report(f"Lightcurve limits use a scale factor of {scale:g} about this mean")
else:
# divide by the bin width so we can assume sigma=sqrt(signal)
#
sigval = np.sqrt(mean / self.bin_width)
self.clean_min_rate = mean - sigma * sigval
self.clean_max_rate = mean + sigma * sigval
self.report(f"Lightcurve limits clipped using {sigma:g} sigma's about this mean")
self.report(f"Filtering lightcurve between rates of {self.clean_min_rate:g} and {self.clean_max_rate:g} ct/s")
self.clean_filter = (self.rate > self.clean_min_rate) & \
(self.rate < self.clean_max_rate)
ngood = self.clean_filter.sum()
if ngood == 0:
raise ValueError("Error: no bins match rate={:g} to {:g} (data range is {:g} to {:g})".format(self.clean_min_rate, self.clean_max_rate,
self.rate[self.filter].min(), self.rate.max()))
self.report(f"Number of good time bins = {ngood}")
self.clean_gti = self.clean_filter
self.clean_bti = np.logical_not(self.clean_gti)
self.clean_mean_rate = self.rate[self.clean_gti].mean()
self.report(f"Rate filter: {repr(self.clean_min_rate)} <= {self.ratename} < {repr(self.clean_max_rate)}")
self.report(f"Mean level of filtered lightcurve = {repr(self.clean_mean_rate)} ct/s\n")