-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathar_processor.py
More file actions
137 lines (107 loc) · 4.61 KB
/
ar_processor.py
File metadata and controls
137 lines (107 loc) · 4.61 KB
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
from astropy.io import fits
import sys,os
import re
import numpy as np
#import matplotlib.pylab as plt
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.switch_backend('Agg')
from scipy import signal
import argparse
import subprocess
"""
Class to process an archive file, run pulsarX DMffdot to produce .px file and extract Time-Phase, Freq-Phase, DM Curve, Intensity Profile and F-Fdot plots
"""
class ARProcessor:
def __init__(self,file,tag='test',debug=False):
self.ar_file = file
self.save_tag = tag
self.debug = debug
self.run_dmffdot()
self.fits_file = self.ar_file.replace('.ar', '.px')
self.hdul = fits.open(self.fits_file)
if self.debug:
self.np_saver()
print("Running in debug mode")
def get_time_phase(self):
time_phase = self.hdul[11].data[0]
x = np.array(time_phase[0])
y = np.array(time_phase[1])
z = np.array(time_phase[2])
self.TP= z.reshape((y.shape[0],x.shape[0])) # Time Phase Plot
self.TP_half =self.TP[:, :self.TP.shape[1]//2] #only first half to include only one peak
#self.TP=np.array(time_phase,dtype=object)
def get_freq_phase(self):
freq_phase = self.hdul[8].data[0]
x = np.array(freq_phase[0])
y = np.array(freq_phase[1])
z = np.array(freq_phase[2])
self.FP = z.reshape((y.shape[0],x.shape[0]))
self.FP_half = self.FP[:, :self.FP.shape[1]//2] # Freq Phase Plot #only first half to include only one peak
#self.FP=np.array(freq_phase,dtype=object)
def get_dm_curve(self):
self.dm_curve = self.hdul[21].data[0][1] #only chis of the DM Curve
def get_intensity_prof(self):
self.intensity_prof = self.hdul[7].data[0][1]
self.intensity_prof_half=self.intensity_prof[:len(self.intensity_prof)//2] #Intensity Profile #only first half to include only one peak
def get_ffdot(self):
ffdot = self.hdul[16].data[0]
x = np.array(ffdot[0])
y = np.array(ffdot[1])
z = np.array(ffdot[2])
self.ffdot = z.reshape((x.shape[0],y.shape[0])) # FFDOT
@staticmethod
def extract_number(text):
match = re.search(r"[-+]?\d*\.\d+|\d+", text.split('=')[1]) # Get the number after '='
return match.group(0) if match else None
def extract_and_convert(self, keyword):
"""
Extracts and converts the value associated with the given keyword
from the 15th HDU data of the FITS file (Meta).
Args:
keyword: The keyword to search for (e.g., 'P0', 'Pepoch', 'DM', 'S/N').
Returns:
The converted float value if found, otherwise None.
"""
# Extract the value for the given keyword
value = next((self.extract_number(row[4]) for row in self.hdul[15].data if keyword in row[4]), None)
# Convert the value to float if found, otherwise return None
return float(value) if value is not None else None
def get_period(self):
self.period = self.extract_and_convert("P0")
def get_pepoch(self):
self.pepoch = self.extract_and_convert("Pepoch")
def get_DM(self):
self.DM = self.extract_and_convert("DM")
def get_SNR(self):
self.SNR = self.extract_and_convert("S/N")
def run_dmffdot(self):
command = ["dmffdot", "-f", self.ar_file, "--saveimage"]
print("Running command:", " ".join(command))
# Run the command
subprocess.run(command)
# Print the final command message
print("Run complete")
def np_saver(self):
self.get_time_phase()
self.get_freq_phase()
self.get_dm_curve()
self.get_intensity_prof()
self.get_ffdot()
np.save(f'{self.save_tag}_time_phase.npy',self.TP)
np.save(f'{self.save_tag}_freq_phase.npy',self.FP)
np.save(f'{self.save_tag}_dm_curve.npy',self.dm_curve)
np.save(f'{self.save_tag}_intensity_prof.npy',self.intensity_prof)
np.save(f'{self.save_tag}_ffdot.npy',self.ffdot)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--file", help="ar file", required=True)
parser.add_argument("--debug", help="Enable debug mode", action="store_true")
parser.add_argument("--tag", help="Tag for naming the saved plots (default: 'test')", default="test")
args=parser.parse_args()
# Initialize the ARProcessor with the provided file, tag and debug option
processor = ARProcessor(args.file,args.tag,args.debug)
print(f"Succesful! Processed file: {processor.ar_file}")
if __name__ == "__main__":
main()