Skip to content

Commit 0d355db

Browse files
committed
read fortran name list extracted
simplified
1 parent 04e1112 commit 0d355db

File tree

1 file changed

+36
-158
lines changed

1 file changed

+36
-158
lines changed

read_k_file.py

Lines changed: 36 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,165 +1,43 @@
11
import numpy as np
2+
import Misc.Fnml as Fnml
23

3-
class K_File:
4-
def __init__(self):
5-
self.nml = {}
6-
self.nml['LPNAME'] =['PSF1A', 'PSF2A', 'PSF3A', 'PSF4A', 'PSF5A',
7-
'PSF6NA', 'PSF7NA','PSF8A', 'PSF9A', 'PSF1B',
8-
'PSF2B', 'PSF3B', 'PSF4B', 'PSF5B', 'PSF6NB',
9-
'PSF7NB', 'PSF8B', 'PSF9B', 'PSI11M','PSI12A',
10-
'PSI23A', 'PSI34A','PSI45A','PSI58A','PSI9A',
11-
'PSF7FA', 'PSI7A', 'PSF6FA','PSI6A', 'PSI12B',
12-
'PSI23B', 'PSI34B','PSI45B','PSI58B','PSI9B',
13-
'PSF7FB', 'PSI7B', 'PSF6FB','PSI6B', 'PSI89FB',
14-
'PSI89NB','PSI1L', 'PSI2L', 'PSI3L']
4+
def k_file(filename):
5+
nml = Fnml.read_Fnml(filename)
6+
nml['LPNAME'] =['PSF1A', 'PSF2A', 'PSF3A', 'PSF4A', 'PSF5A',
7+
'PSF6NA', 'PSF7NA','PSF8A', 'PSF9A', 'PSF1B',
8+
'PSF2B', 'PSF3B', 'PSF4B', 'PSF5B', 'PSF6NB',
9+
'PSF7NB', 'PSF8B', 'PSF9B', 'PSI11M','PSI12A',
10+
'PSI23A', 'PSI34A','PSI45A','PSI58A','PSI9A',
11+
'PSF7FA', 'PSI7A', 'PSF6FA','PSI6A', 'PSI12B',
12+
'PSI23B', 'PSI34B','PSI45B','PSI58B','PSI9B',
13+
'PSF7FB', 'PSI7B', 'PSF6FB','PSI6B', 'PSI89FB',
14+
'PSI89NB','PSI1L', 'PSI2L', 'PSI3L']
1515

1616

17-
self.nml['MPNAM2'] =['MPI11M067','MPI1A067', 'MPI2A067', 'MPI3A067', 'MPI4A067 ',
18-
'MPI5A067', 'MPI8A067', 'MPI9A067', 'MPI79A067', 'MPI7FA067 ',
19-
'MPI7NA067','MPI67A067','MPI6FA067','MPI6NA067', 'MPI66M067 ',
20-
'MPI1B067', 'MPI2B067', 'MPI3B067', 'MPI4B067', 'MPI5B067 ',
21-
'MPI8B067', 'MPI89B067','MPI9B067', 'MPI79B067', 'MPI7FB067 ',
22-
'MPI7NB067','MPI67B067','MPI6FB067','MPI6NB067',
23-
'MPI8A322', 'MPI89A322','MPI9A322', 'MPI79FA322','MPI79NA322',
24-
'MPI7FA322','MPI7NA322','MPI67A322','MPI6FA322', 'MPI6NA322 ',
25-
'MPI66M322','MPI6NB322','MPI6FB322','MPI67B322', 'MPI7NB322 ',
26-
'MPI7FB322','MPI79B322','MPI9B322', 'MPI89B322', 'MPI8B322 ',
27-
'MPI5B322', 'MPI4B322', 'MPI3B322', 'MPI2B322', 'MPI1B322 ',
28-
'MPI11M322','MPI1A322', 'MPI2A322', 'MPI3A322', 'MPI4A322 ',
29-
'MPI5A322', 'MPI1U157', 'MPI2U157', 'MPI3U157', 'MPI4U157',
30-
'DSL1U180', 'DSL2U180', 'DSL3U180', 'DSL4U157',
31-
'MPI5U157', 'MPI6U157', 'MPI7U157', 'DSL5U157', 'DSL6U157',
32-
'MPI1L180', 'MPI2L180', 'MPI3L180']
33-
34-
# 'DSL1U180', 'DSL2U180', 'DSL3U180', 'DSL4U157',
35-
# 'DSL5U157', 'DSL6U157'
36-
self.DSL_areas = [0.148739625718, 0.146993802189, 0.189430809952, 0.0385235076433,
37-
0.203298492472, 0.218204272396]
38-
39-
def read_Fnml(self, filename):
40-
"""Reads the namelist file and returns its contents in a dictionary"""
41-
42-
lines = open(filename,'r').readlines()
43-
44-
first_entry_found = False
45-
for line in lines:
46-
line = line.strip()
47-
48-
# remove Comments
49-
if '!' in line:
50-
idx = line.index('!')
51-
line = line[0:idx]
52-
if len(line) < 1: continue
53-
54-
# check for beginning and end of namelist
55-
if line[0] == '&': continue
56-
if line[0] == '/': first_entry_found = False
57-
58-
# remove possible comma at the end
59-
if line[-1] == ',': line = line[0,-1]
60-
61-
# find lines with keys
62-
if '=' in line:
63-
if not first_entry_found: first_entry_found = True
64-
65-
# store as many (key, value) pairs as in a single line
66-
num = line.count('=')
67-
keys = list(np.arange(num))
68-
values = list(np.arange(num))
69-
_, keys[0], line = self._split_line(line)
70-
for i in xrange(1,num):
71-
values[i-1], keys[i], line = self._split_line(line)
72-
values[-1] = line.split()
73-
74-
# convert values from string
75-
for i in xrange(num):
76-
if len(values[i]) == 1:
77-
values[i] = self._convert_value(values[i][0])
78-
else:
79-
values[i] = np.array([self._convert_value(item) for item in values[i]])
80-
81-
self.nml[keys[i]] = values[i]
82-
else: # just add values to last known key
83-
if not first_entry_found: continue
84-
values = line.strip().split()
85-
for item in values: self.nml[keys[-1]] = np.append(self.nml[keys[-1]],float(item))
86-
87-
88-
def make_v3fit_input(self, filename, type = 'all'):
89-
"""
90-
read the output from v3mags.py and make a temporary file with the formated v3fit
91-
input to use copy/paste. This routine will become part of a v3fit input generator
92-
script in the future.
93-
"""
94-
import h5py
95-
with h5py.File(filename, 'r') as f:
96-
dset = f['name']; name = dset[...]
97-
dset = f['signal']; signal = dset[...]
98-
dset = f['weight']; weight = dset[...]
99-
dset = f['sigma']; sigma = dset[...]
100-
101-
# replace with EFIT k-file data
102-
name = list(name)
103-
N = len(name)
104-
idxs = np.ones(N, dtype = bool)
105-
for i,probe in enumerate(self.nml['MPNAM2']):
106-
if probe in name:
107-
idx = name.index(probe)
108-
if 'DSL' in probe:
109-
n = int(probe[3]) - 1
110-
factor = self.DSL_areas[n]
111-
else: factor = 1
112-
signal[idx] = self.nml['EXPMP2'][i] * factor
113-
weight[idx] = self.nml['FWTMP2'][i]
114-
idxs[idx] = False
115-
for i,probe in enumerate(self.nml['LPNAME']):
116-
if probe in name:
117-
idx = name.index(probe)
118-
signal[idx] = (self.nml['COILS'][i] + self.nml['SIREF']) * 2*np.pi
119-
weight[idx] = self.nml['FWTSI'][i]
120-
idxs[idx] = False
121-
122-
if type == 'EFIT': # set weight of all other probes to zero
123-
weight[idxs] *= 0.0
124-
elif type == '2D': # set weight of all 3D probes to zero
125-
weight[295::] *= 0.0
126-
elif type == '3D': # set weight of all 2D probes to zero
127-
weight[0:295] *= 0.0
128-
129-
with open('v3fit_temp.in','w') as f:
130-
for i in np.arange(N):
131-
f.write('sdo_data_a('+format(i+1,'3d')+') = ' + format(signal[i],' 13.7e') + ' ')
132-
f.write('sdo_sigma_a('+format(i+1,'3d')+') = ' + format(sigma[i],' 13.7e') + ' ')
133-
f.write('sdo_weight_a('+format(i+1,'3d')+') = ' + str(int(weight[i])) + ' ')
134-
f.write('! ' + name[i] + '\n')
135-
17+
nml['MPNAM2'] =['MPI11M067','MPI1A067', 'MPI2A067', 'MPI3A067', 'MPI4A067 ',
18+
'MPI5A067', 'MPI8A067', 'MPI9A067', 'MPI79A067', 'MPI7FA067 ',
19+
'MPI7NA067','MPI67A067','MPI6FA067','MPI6NA067', 'MPI66M067 ',
20+
'MPI1B067', 'MPI2B067', 'MPI3B067', 'MPI4B067', 'MPI5B067 ',
21+
'MPI8B067', 'MPI89B067','MPI9B067', 'MPI79B067', 'MPI7FB067 ',
22+
'MPI7NB067','MPI67B067','MPI6FB067','MPI6NB067',
23+
'MPI8A322', 'MPI89A322','MPI9A322', 'MPI79FA322','MPI79NA322',
24+
'MPI7FA322','MPI7NA322','MPI67A322','MPI6FA322', 'MPI6NA322 ',
25+
'MPI66M322','MPI6NB322','MPI6FB322','MPI67B322', 'MPI7NB322 ',
26+
'MPI7FB322','MPI79B322','MPI9B322', 'MPI89B322', 'MPI8B322 ',
27+
'MPI5B322', 'MPI4B322', 'MPI3B322', 'MPI2B322', 'MPI1B322 ',
28+
'MPI11M322','MPI1A322', 'MPI2A322', 'MPI3A322', 'MPI4A322 ',
29+
'MPI5A322', 'MPI1U157', 'MPI2U157', 'MPI3U157', 'MPI4U157',
30+
'DSL1U180', 'DSL2U180', 'DSL3U180', 'DSL4U157',
31+
'MPI5U157', 'MPI6U157', 'MPI7U157', 'DSL5U157', 'DSL6U157',
32+
'MPI1L180', 'MPI2L180', 'MPI3L180']
33+
34+
# 'DSL1U180', 'DSL2U180', 'DSL3U180', 'DSL4U157',
35+
# 'DSL5U157', 'DSL6U157'
36+
nml['DSL_areas'] = [0.148739625718, 0.146993802189, 0.189430809952, 0.0385235076433,
37+
0.203298492472, 0.218204272396]
38+
39+
return nml
40+
13641

137-
def _split_line(self, line):
138-
"""Splits the line at the first = and finds the variable name"""
139-
A, B = line.split('=', 1) # do one split only at the first occurance of '='
140-
A = A.strip()
141-
B = B.strip()
142-
A = A.split()
143-
key = A[-1]
144-
if len(A) > 1: A = A[0:-1]
145-
else: A = []
146-
return A, key, B
147-
148-
149-
def _convert_value(self, invalue):
150-
"""Formats the values"""
151-
if 'T' in invalue: value = True
152-
elif 'F' in invalue: value = False
153-
else:
154-
try:
155-
value = float(invalue)
156-
if not '.' in invalue: value = int(value)
157-
except:
158-
value = invalue
159-
if (invalue[0]== "'") & (value[0]== "'"): value = invalue[1:-1]
160-
161-
return value
162-
163-
16442

16543

0 commit comments

Comments
 (0)