-
Notifications
You must be signed in to change notification settings - Fork 14
/
ls.py
156 lines (131 loc) · 5.63 KB
/
ls.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ls.py
# 16 May 22:31
# 11 September 12:16 semicolon fixes
# Copyright 2014 M. Emre Aydin <[email protected]>
"""
A Simple Lomb-Scargle (LS) Code to Check the Research for the
Weighted Wavelet Z-Transform Translation of Python.
The script uses the LS method in the SciPy Package.
Available at http://github.com/eaydin
"""
# Checking the necessary modules and importing them if exists
import sys
try:
import numpy as np
except:
print "Please Install Numpy"
raise SystemExit
try:
import scipy as sp
except:
print "Please Install SciPy"
raise SystemExit
try:
import argparse
except:
"Argparse Not Available?! You sure this is Python 2.7?"
raise SystemExit
from scipy.signal import spectral
class LombScargle(object):
"""The Main class object.
This is the main class object to create a Lomb-Scargle periodogram.
Usage:
LombScargle(timeData, magnitudeData, freq_low, freq_high, \
freq_num)
Arguments:
timeData: float array of time values in JD
magnitudeData: float array of magnitude values
freq_low: the low frequency value
freq_high: the high frequency value
freq_num: number of total frequencies to evaluate
Available Attributes:
'power': returns the LS-Power only.
'freqs': returns the frequencies calculated.
'periodogram': returns the LS Periodogram as [freqs, power].
'
"""
def __init__(self, timeData, magnitudeData, freq_low, \
freq_high, freq_num):
"""Initializing the LombScargle Object"""
self.freqs = np.linspace(freq_low, freq_high, freq_num)
# Convert arrays to Numpy Arrays for lombscargle to understand
timeArray = np.asarray(timeData)
magnitudeArray = np.asarray(magnitudeData)
# Calculating the periodogram and return it
self.power = spectral.lombscargle(timeArray, magnitudeArray, \
self.freqs)
self.periodogram = np.column_stack([self.freqs, self.power])
# The Frequency and LS-Power of the Maximum LS-Power
self.maxLS = [self.freqs[self.power.argmax(axis=0)], \
np.amax(self.power)]
if __name__ == '__main__':
"""The main function run when standalone"""
description = """
The Lomb-Scargle Periodogram Program.
This script is written for research purposes to accompany the
Weighted Wavelet Z-Transform implementation.
M. Emre Aydin - [email protected]
http://about.me/emre.aydin
Available at http://github.com/eaydin
Import this script via Python to use it as a module, rather than
a standalone script. (import ls)
"""
# Parsing arguments
parser = argparse.ArgumentParser(prog="ls.py", \
formatter_class=argparse.RawDescriptionHelpFormatter, \
description=description)
parser.add_argument("-f", "--file", type=argparse.FileType("r"), \
default=sys.stdin, required=True, \
help="the Input file, Raw Lightcurve")
parser.add_argument("-o", "--output", type=argparse.FileType("w"), \
help="the Output file name")
parser.add_argument("-s", "--stdout", action="store_true", \
default=False, help="Prints the results to \
the standard output. Automatically set to 'True' \
if '-o' option is not defined, otherwise 'False'.")
parser.add_argument("-l", "--freq-low", type=float, required=True, \
help="the Low Frequency Value")
parser.add_argument("-hi", "--freq-high", type=float, required=True, \
help="the High Frequency Value")
parser.add_argument("-n", "--freq-num", type=int, required=False, \
default=1000, help="the Number of Frequencies to \
calculate. Input type is integer. Default value \
is 1000.")
parser.add_argument("--no-headers", action="store_true", \
default=False, help="Doesn't print headers to output \
if set. Default is 'False'.")
args = parser.parse_args()
# Check if not output file is specified, then set stdout to 'True'
if args.output is None:
args.stdout = True
magnitudeData = []
timeData = []
# Read the lightcurve data
# Dismiss lines beginning with '%' and '#'
for line in args.file:
if line.strip()[0] != "%" and line.strip()[0] != "#":
timeData.append(float(line.split()[0].strip()))
magnitudeData.append(float(line.split()[1].strip()))
s = LombScargle(timeData, magnitudeData, args.freq_low, \
args.freq_high, args.freq_num)
# Setting the float precision of Numpy to 5
np.set_printoptions(precision=5)
# Suppress the printing options for Numpy
np.set_printoptions(suppress=True)
# Print the full Numpy Array
np.set_printoptions(threshold='nan')
if args.stdout:
if not args.no_headers:
print "# Frequency \t LS-Power"
for i in s.periodogram:
print "%.5f %.5f "% (i[0], i[1])
if args.output is not None:
if not args.no_headers:
np.savetxt(args.output, s.periodogram, delimiter="\t", \
fmt="%10.5f")
else:
np.savetxt(args.output, s.periodogram, fmt="%10.5f", \
delimiter="\t", comments="#", \
header=" Frequency \t LS-Power")