1 | #include <iostream> |
---|
2 | #include <iomanip> |
---|
3 | #include <fstream> |
---|
4 | #include <vector> |
---|
5 | #include <string> |
---|
6 | using namespace std; |
---|
7 | |
---|
8 | //################################################################################################################## |
---|
9 | |
---|
10 | //This program can be used to transform a twiss file (output of MAD-X) into an opticsfile compatible with ICOSIM++ |
---|
11 | |
---|
12 | //################################################################################################################## |
---|
13 | |
---|
14 | |
---|
15 | int main() |
---|
16 | { |
---|
17 | |
---|
18 | string inputfile, outputfile; |
---|
19 | |
---|
20 | cout << "Please enter the name of the twiss file to convert: " << endl; |
---|
21 | |
---|
22 | cin >> inputfile; |
---|
23 | |
---|
24 | cout << "Enter now the desired name for the optics file : " << endl; |
---|
25 | |
---|
26 | cin >> outputfile; |
---|
27 | |
---|
28 | ifstream entree; |
---|
29 | ofstream sortie; |
---|
30 | |
---|
31 | entree.open(inputfile.c_str()); |
---|
32 | sortie.open(outputfile.c_str()); |
---|
33 | |
---|
34 | if (entree.fail()) { |
---|
35 | |
---|
36 | cerr << "Warning: problem opening the file " << inputfile << "!" << endl; |
---|
37 | |
---|
38 | } else if (sortie.fail()) { |
---|
39 | |
---|
40 | cerr << "Warning: problem with the file " << outputfile << "!" << endl; |
---|
41 | |
---|
42 | } else { |
---|
43 | |
---|
44 | char first; |
---|
45 | string param, indic; |
---|
46 | vector <string> temp; |
---|
47 | |
---|
48 | entree >> first; |
---|
49 | |
---|
50 | while (first == '@') { |
---|
51 | |
---|
52 | entree >> param; |
---|
53 | if (param == "NAME") { |
---|
54 | sortie << "NAMEFILE,"; |
---|
55 | } else { |
---|
56 | sortie << param << ","; |
---|
57 | } |
---|
58 | entree >> indic; |
---|
59 | |
---|
60 | if (indic != "%le") { |
---|
61 | getline(entree, param); |
---|
62 | param = param.substr(2, param.size() - 3); |
---|
63 | sortie << param; |
---|
64 | } else { |
---|
65 | |
---|
66 | entree >> param; |
---|
67 | sortie << param; |
---|
68 | } |
---|
69 | |
---|
70 | sortie << endl; |
---|
71 | |
---|
72 | entree >> first; |
---|
73 | } |
---|
74 | |
---|
75 | int nbre(0); |
---|
76 | |
---|
77 | entree >> param; |
---|
78 | |
---|
79 | while (param != "$") { |
---|
80 | ++nbre; |
---|
81 | |
---|
82 | if ((param == "X") || (param == "PX") || (param == "Y") || (param == "PY") || (param == "T") || (param == "PT")) { |
---|
83 | param = param + "C"; |
---|
84 | } |
---|
85 | |
---|
86 | temp.push_back(param); |
---|
87 | |
---|
88 | entree >> param; |
---|
89 | } |
---|
90 | |
---|
91 | for (int j(0); j < temp.size() - 1; ++j) { |
---|
92 | sortie << temp[j] << ","; |
---|
93 | } |
---|
94 | |
---|
95 | sortie << temp[temp.size() - 1] << endl; |
---|
96 | |
---|
97 | entree >> param; |
---|
98 | |
---|
99 | while (param.substr(0, 1) == "%") { |
---|
100 | entree >> param; |
---|
101 | } |
---|
102 | |
---|
103 | while (!entree.eof()) { |
---|
104 | |
---|
105 | for (int i(0); i < nbre - 1; ++i) { |
---|
106 | |
---|
107 | if (param.substr(0, 1) == "\"") { |
---|
108 | param = param.substr(1, param.size() - 2); |
---|
109 | } |
---|
110 | |
---|
111 | sortie << param << ","; |
---|
112 | entree >> param; |
---|
113 | } |
---|
114 | |
---|
115 | if (param.substr(0, 1) == "\"") { |
---|
116 | param = param.substr(1, param.size() - 2); |
---|
117 | } |
---|
118 | |
---|
119 | sortie << param << endl; |
---|
120 | entree >> param; |
---|
121 | |
---|
122 | } |
---|
123 | |
---|
124 | int stop; |
---|
125 | } |
---|
126 | |
---|
127 | entree.close(); |
---|
128 | sortie.close(); |
---|
129 | |
---|
130 | return 0; |
---|
131 | } |
---|