-
Notifications
You must be signed in to change notification settings - Fork 1
/
ays_export.py
executable file
·55 lines (39 loc) · 1.74 KB
/
ays_export.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
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
from __future__ import generators, print_function, division
import ays_general
from pyviability import libviability as lv
import numpy as np
import os
import argparse, argcomplete
if __name__=="__main__":
parser = argparse.ArgumentParser(
description="Export an AWS - TSM file to text.",
)
parser.add_argument("input_file", metavar="input-file",
help="file with the tsm data")
parser.add_argument("txt_file", metavar="txt-file", nargs="?", default="",
help="output text file")
parser.add_argument("-f", "--force", action="store_true",
help="overwrite text file if already existing")
# use argcomplete auto-completion
argcomplete.autocomplete(parser)
args = parser.parse_args()
if args.txt_file and (not args.force) :
if os.path.isfile(args.txt_file):
parser.error("'{}' exists already, use '--force' option to overwrite".format(args.txt_file))
if args.txt_file == args.input_file:
parser.error("'txt-file' and 'output-file' should be different from each other, not both '{}'".format(args.input_file))
header, data = ays_general.load_result_file(args.input_file)
header_txt = "#"*80 + "\n"
header_txt += ays_general.recursive_dict2string(header)
header_txt += "#"*80 + "\n"
for region in lv.REGIONS:
header_txt += "{} = {:>2d}\n".format(region, getattr(lv, region))
header_txt += "#"*80
states = data["states"]
print(header_txt)
if args.txt_file:
print("saving to {!r} ... ".format(args.txt_file), end="", flush=True)
np.savetxt(args.txt_file, states, fmt="%i", header=header_txt, comments="")
print("done")