forked from Caoimhinmg/PmagPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
combine_magic.py
executable file
·128 lines (107 loc) · 3.83 KB
/
combine_magic.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
#!/usr/bin/env python
import sys,os
import pmag,ipmag
import command_line_extractor as extractor
def main():
"""
NAME
combine_magic.py
DESCRIPTION
Combines magic format files of the same type together.
SYNTAX
combine_magic.py [-h] [-i] -out filename -in file1 file2 ....
OPTIONS
-h prints help message
-i allows interactive entry of input and output filenames
-F specify output file name [must come BEFORE input file names]
-f specify input file names [ must come last]
"""
if "-h" in sys.argv:
print main.__doc__
sys.exit()
if "-i" in sys.argv: # interactive
dataset,datasets=[],[]
while True:
infile=raw_input('\n\n Enter magic files for combining, <return> when done: ')
if infile=='':
break
if os.path.isfile(infile):
filenames.append(infile)
else:
print "-W- You have not provided a valid filename.\nIf the file is not in your current working directory, you will need to provide the full path to the file"
outfile=raw_input('\n\n Enter name for new combined file')
if not outfile:
return False
else: # non-interactive
dataframe = extractor.command_line_dataframe([["F", True, '']])
args = extractor.extract_and_check_args(sys.argv, dataframe)
dir_path, outfile, filenames = extractor.get_vars(["WD", "F", "f"], args)
#dir_path = pmag.get_named_arg_from_sys("-WD", ".")
#outfile = pmag.get_named_arg_from_sys("-F", reqd=True)
#if "-f" in sys.argv:
# ind=sys.argv.index("-f")
# for k in range(ind+1,len(sys.argv)):
# filenames.append(os.path.join(dir_path, sys.argv[k]))
#else:
# raise pmag.MissingCommandLineArgException("-f")
ipmag.combine_magic(filenames.split(), outfile)
main()
"""def old_main():
NAME
combine_magic.py
DESCRIPTION
Combines magic format files of the same type together.
SYNTAX
combine_magic.py [-h] [-i] -out filename -in file1 file2 ....
OPTIONS
-h prints help message
-i allows interactive entry of input and output filenames
-F specify output file name [must come BEFORE input file names]
-f specify input file names [ must come last]
#
# set up magic meta data type requirements
#
filenames=[]
datasets=[]
dir_path='.'
#
if '-WD' in sys.argv:
ind=sys.argv.index('-WD')
dir_path=sys.argv[ind+1]
if "-h" in sys.argv:
print main.__doc__
sys.exit()
if "-F" in sys.argv:
ind=sys.argv.index("-F")
output=dir_path+'/'+sys.argv[ind+1]
if "-f" in sys.argv:
ind=sys.argv.index("-f")
for k in range(ind+1,len(sys.argv)):
filenames.append(dir_path+'/'+sys.argv[k])
for infile in filenames:
dataset,file_type=pmag.magic_read(infile)
print "File ",infile," read in with ",len(dataset), " records"
for rec in dataset:
datasets.append(rec)
if '-i' in sys.argv:
quit,dataset,datasets=0,[],[]
while quit==0:
infile=raw_input('\n\n Enter magic files for combining, <return> when done: ')
if infile=='':
quit = 1
break
dataset,file_type=pmag.magic_read(infile)
print "File ",infile," read in with ",len(dataset), " records"
for rec in dataset:
datasets.append(rec)
#
# collect all the keys from all the files
#
Recs,keys=pmag.fillkeys(datasets)
#
# write out the datasets into a combined file
#
pmag.magic_write(output,Recs,file_type)
print "All records stored in ",output
#main()
"""