-
Notifications
You must be signed in to change notification settings - Fork 4
/
ctp2nml.py
147 lines (130 loc) · 3.66 KB
/
ctp2nml.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
#!/usr/bin/env python
#
# create namelist datafile from original CTP parameter file
#
# note 'begin' and 'end' statements must start at the beginning
# of the line
#
import sys
import getopt
import pdb
logicals = [
'MC_SMEAR',
'DOING_PHSP',
'DOING_KAON',
'DOING_PION',
'DOING_DELTA',
'DOING_SEMI',
'DOING_HPLUS',
'DOING_RHO',
'DOING_DECAY',
'MC_SMEAR',
'HARD_CUTS',
'USING_RAD',
'DOING_PHSP',
'USING_ELOSS',
'CORRECT_ELOSS',
'CORRECT_RASTER',
'USING_CIT_GENERATION',
'USING_COULOMB',
'USE_OFFSHELL_RAD',
'DO_FERMI',
'USING_TGT_FIELD']
output = sys.stdout
def is_logical(x):
item = x.strip()
# check if the item is in the list
try:
logicals.index( item.upper() ) # use upper case
except:
is_logical = False
return is_logical
is_logical = True
return is_logical
#------------------------------------------------------------
def usage(): # describe the usage of this program
print "\nusage: ctp2nml.py [-options] ctp_file \n"
print "options: -h,? this message "
print " -o output file (def: stdout) "
#------------------------------------------------------------
#------------------------------------------------------------
# commandline arguments
args = sys.argv[1:] # argument list w/o program name
#------------------------------------------------------------
# handle options
options = "o:h?" # the ones following the colon require an argument
try:
options, arguments = getopt.getopt(args, options)
except getopt.GetoptError, (msg, opt) :
# print help information and exit:
print "ERROR in option : ", opt, " reason: ", msg
usage()
sys.exit(1)
# handle the option values
for o, v in options:
if o == "-h" or o == "-?":
# help
usage()
sys.exit(0)
if o == "-o":
output = open(v,'w')
#------------------------------------------------------------
if (len(arguments) != 1):
print 'you need to enter the name of the CTP file ! \n'
usage()
sys.exit(-1)
filename = arguments[0]
d = open(filename).readlines()
found_set = False
first = True
output.write('! This is a namelist file created from :' + filename+' \n')
for l in d:
blank = (len(l) == 0)
if blank: continue
# replace all ; with the comment character !
ll = l.replace(';','!')
# handle the begin param: extract the namelist name and write the data
if ll.find('begin') == 0:
line = ' &'
found_set = True
f = ll.split()
name = f[2].upper()
line += name + '\n'
output.write(line)
continue
# handle the end param part
if ll.find('end') == 0:
found_set = False
output.write('/ \n')
continue
# extract the variable assignments including comments
if found_set and (not blank) :
line = ' '
comment = ''
f = ll.split('!')
if len(f) > 1:
comment = '!' + f[1][:-1]
fv = f[0].split('=')
var_name = fv[0].replace('.','%')
if len(fv) > 1:
var_val = fv[1]
# check if it is a logical variable
if is_logical(var_name):
val = int( float(var_val))
if val == 0 :
var_val = 'F'
elif val == 1 :
var_val = 'T'
else:
print 'problem with logical : ', var_name, var_val, val
var_val = 'F'
line += var_name + ' = ' + var_val + comment + '\n'
else:
# this is for lines that only contain comments
line += var_name + comment + '\n'
output.write(line)
continue
output.write(ll)
# done
if output != sys.stdout:
output.close()