-
Notifications
You must be signed in to change notification settings - Fork 10
/
gann.py
117 lines (105 loc) · 3.5 KB
/
gann.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys, getopt
from datetime import datetime
import math
from gann import *
def print_usage():
print(
"""
classic Gann square: gann.py -o <output file name> -s <square size>
Gann square based on date: gann.py -o <output file name> -a <base date> -b <final date> -m <path to list of dates to mark>
Gann sub square based on date: gann.py -o <output file name> -a <base date> -b <final date> -m <path to list of dates to mark> -r "<left>;<bottom>;<right>;<up>"
input date format: "dd/MM/yyyy"
"""
)
def main(argv):
cell_size = 30
date_format = "%d/%m/%Y"
# --------------------------------------
output_file_name = ""
marks_file_name = ""
square_size = -1
date_a = None
date_b = None
left, bot, right, up = 0, 0, 0, 0
# --------------------------------------
try:
opts, args = getopt.getopt(
argv,
"ho:s:a:b:m:r:",
["ofile=", "size=", "a_date=", "b_date=", "mfile=", "rect="],
)
except getopt.GetoptError:
print_usage()
sys.exit(2)
for opt, arg in opts:
if opt == "-h":
print_usage()
sys.exit()
elif opt in ("-o", "--ofile"):
output_file_name = arg
elif opt in ("-s", "--size"):
square_size = int(arg)
elif opt in ("-a", "--a_date"):
date_a = datetime.strptime(arg, date_format)
elif opt in ("-b", "--b_date"):
date_b = datetime.strptime(arg, date_format)
elif opt in ("-m", "--mfile"):
marks_file_name = arg
elif opt in ("-r", "--rect"):
rect = arg.split(";")
try:
left, bot, right, up = (
int(rect[0]),
int(rect[1]),
int(rect[2]),
int(rect[3]),
)
except ValueError as e:
print("Failed to parse range!")
if output_file_name == "":
print_usage()
sys.exit(2)
if square_size != -1:
# classic Gann square
# Info
print(f"Cells: {square_size * square_size}")
print(f"Square size: {square_size}")
print(f"Cell size: {cell_size}")
print("Building...")
stream = open(output_file_name, "w")
create_gann_square_classic(square_size, cell_size, stream)
stream.close()
elif date_a and date_b:
# date based Gann square
delta = date_b - date_a
square_size = int(math.ceil(math.sqrt(delta.days)))
if square_size % 2 == 0:
square_size += 1
# Info
print(f"Cells: {square_size * square_size}")
print(f"Square size: {square_size}")
print(f"Cell size: {cell_size}")
# Process
print("Loading data...")
marks = load_marks(marks_file_name)
print("Building...")
stream = open(output_file_name, "w")
if (
(left != 0 or bot != 0 or right != 0 or up != 0)
and left < right
and bot < up
):
create_gann_sub_square_dates(
(left, bot, right + 1, up + 1), cell_size, date_a, marks, stream
)
else:
create_gann_square_dates(square_size, cell_size, date_a, marks, stream)
stream.close()
else:
print_usage()
sys.exit(2)
print(f"Done. See {output_file_name}")
if __name__ == "__main__":
main(sys.argv[1:])