-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathselect_even_odd_entries.py
executable file
·93 lines (69 loc) · 2.3 KB
/
select_even_odd_entries.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
#!/usr/bin/env python
import optparse
from sys import *
import os,sys,re
from optparse import OptionParser
import glob
import subprocess
from os import system
import linecache
import time
import shutil
#=========================
def setupParserOptions():
parser = optparse.OptionParser()
parser.set_usage("%prog -i <particles.star file> -o <output name>")
parser.add_option("-i",dest="star",type="string",metavar="FILE",
help="_data.star file containing per-particle entries from auto-refine")
parser.add_option("-d", action="store_true",dest="debug",default=False,
help="debug")
options,args = parser.parse_args()
if len(args) > 0:
parser.error("Unknown commandline options: " +str(args))
if len(sys.argv) < 2:
parser.print_help()
sys.exit()
params={}
for i in parser.option_list:
if isinstance(i.dest,str):
params[i.dest] = getattr(options,i.dest)
return params
#=============================
def checkConflicts(params):
if not params['star']:
print "\nWarning: no .star file specified\n"
elif not os.path.exists(params['star']):
print "\nError: star file '%s' does not exist\n" % params['star']
sys.exit()
#==============================
def selectEveOdd(params):
f1 = open(params['star'],'r')
outfileeve = '%s_eve.star' %(params['star'][:-5])
outfileodd = '%s_odd.star' %(params['star'][:-5])
if os.path.exists(outfileeve):
print "\nOutput file %s already exists, exiting.\n" %(outfileeve)
sys.exit()
if os.path.exists(outfileodd):
print "\nOutput file %s already exists, exiting.\n" %(outfileodd)
sys.exit()
eve = open(outfileeve,'w')
odd = open(outfileodd,'w')
linecounter=1
for line in f1:
if len(line) < 40:
eve.write(line)
odd.write(line)
if len(line) > 40:
if linecounter % 2 == 0:
eve.write(line)
if linecounter % 2 != 0:
odd.write(line)
linecounter = linecounter + 1
f1.close()
odd.close()
eve.close()
#==============================
if __name__ == "__main__":
params=setupParserOptions()
checkConflicts(params)
selectEveOdd(params)