-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataReadIn.py
133 lines (106 loc) · 3.65 KB
/
DataReadIn.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
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 26 09:11:24 2016
@author: marisa
"""
import numpy as np
import os
def read_header(filepath):
f = open(filepath)
lines = f.readlines()
header0 = lines[0]
h0_lines = header0.split()
if h0_lines[0] == '#':
h0_lines = h0_lines[1:len(h0_lines)]
else:
h0_lines = h0_lines
file_name = h0_lines[1]
pulsar_name = h0_lines[3]
nsub = int(h0_lines[5])
nch = int(h0_lines[7])
npol = int(h0_lines[9])
nbins = int(h0_lines[11])
rms = float(h0_lines[13])
# return file_name, pulsar_name, nsub, nch, npol, nbins, rms
return pulsar_name, nch, nbins, rms
def read_data(filepath, profilenumber, nbins):
d = open(filepath)
lines = d.readlines()
profile_start = 2+profilenumber*(nbins+1)
profile_end = profile_start + nbins
lines_block = lines[profile_start:profile_end]
if lines[profile_start-1].split()[0] == '#':
freqc = float(lines[profile_start-1].split()[6])
bw = float(lines[profile_start-1].split()[8])
freqm = 10**((np.log10(freqc+ bw/2.)+ np.log10(freqc - bw/2.))/2)
else:
freqc = float(lines[profile_start-1].split()[5])
bw = float(lines[profile_start-1].split()[7])
freqm = 10**((np.log10(freqc+ bw/2.)+ np.log10(freqc - bw/2.))/2)
datalist = []
for i in range(nbins):
data= float(lines_block[i].split()[3])
datalist.append(data)
return np.array(datalist), freqm
def read_raw(filename):
pulsar_name = filename[0:10]
loaded = np.loadtxt(filename)
nch = np.shape(loaded)[0]
nbins = np.shape(loaded)[1] -1
datas = loaded[:,0:nbins]
freq = loaded[:,nbins]
return pulsar_name, nch, nbins, datas, freq
def read_Krishnak(filename):
#function for reading data downloaded from: http://rac.ncra.tifr.res.in/da/pulsar/pulsar.html
pulsar_name = filename[-17:-5]
loaded = np.loadtxt(filename)
nch = 1
nbins = np.shape(loaded)[0]
datas = loaded[:,1]
freq = 0.327 #(327MHz)
return pulsar_name, nch, nbins, datas, freq
def log_writer(logpath,logfilename, my_string):
logplace = os.path.join(logpath,logfilename)
with open(logplace,'a') as log_file:
log_file.write(my_string + '\n')
def read_Stefan_data(filepath, profilenumber, nbins):
d = open(filepath)
lines = d.readlines()
profile_start = profilenumber*(nbins+1)
profile_end = profile_start + nbins
lines_block = lines[profile_start:profile_end]
datalist = []
for i in range(nbins):
data= float(lines_block[i].split()[3])
datalist.append(data)
return np.array(datalist)
def read_Paul(filename):
#function for reading /Paul/1937+21_mjd_57397.txt at the moment
#Single column of ascii data
pulsar_name = "B1937+21"
datas = np.loadtxt(filename)
nch = 1
nbins = np.shape(datas)[0]
mainpulse = datas[nbins/2:nbins]
nbinsmain = nbins/2
freq = 1.4 #(1.4GHz)
return pulsar_name, nch, nbinsmain, mainpulse, freq
def skip_first_col(fname, delimiter=None):
with open(fname, 'r') as fin:
for line in fin:
try:
yield line.split(delimiter, 1)[1]
except IndexError:
continue
#def read_Stefan(filepath):
# datamatrix = np.loadtxt(filepath)
# ##determine number of blocks
# number_blocks = datamatrix[-1][0]
# blocks = []
# for i in range(number_blocks):
# block = datamatrix[datamatrix[:, 0] == i, :]
# blocks.append(block)
# return blocks[0:10]
#
##divide observation up into sub_int blocks
##divide each block into channels