cif2xml.cpp 5.61 KB
Newer Older
Tobias WEBER's avatar
Tobias WEBER committed
1
2
3
4
5
/**
 * converts a CIF file into a more parsable XML
 * @author Tobias Weber <tweber@ill.fr>
 * @date may-2019
 * @license GPLv3, see 'LICENSE' file
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * ----------------------------------------------------------------------------
 * mag-core (part of the Takin software suite)
 * Copyright (C) 2018-2021  Tobias WEBER (Institut Laue-Langevin (ILL),
 *                          Grenoble, France).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ----------------------------------------------------------------------------
Tobias WEBER's avatar
Tobias WEBER committed
24
25
 */

Tobias WEBER's avatar
Tobias WEBER committed
26
#include "../structfact/loadcif.h"
27
#include "tlibs2/libs/math20.h"
Tobias WEBER's avatar
Tobias WEBER committed
28

29
#include <gemmi/version.hpp>
Tobias WEBER's avatar
Tobias WEBER committed
30
31
32
33
34
#include <iostream>
#include <memory>


using t_real = double;
Tobias WEBER's avatar
Tobias WEBER committed
35
using t_vec = tl2::vec<t_real, std::vector>;
Tobias WEBER's avatar
Tobias WEBER committed
36
using t_mat = tl2::mat<t_real, std::vector>;
Tobias WEBER's avatar
Tobias WEBER committed
37
38
39
40
41

constexpr t_real g_eps = 1e-6;
constexpr int g_prec = 6;


Tobias WEBER's avatar
Tobias WEBER committed
42
43
44
static const std::string g_strSig = "cif2xml";
static const std::string g_strVer = "0.5";

Tobias WEBER's avatar
Tobias WEBER committed
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

/**
 * print vector
 */
std::ostream& operator<<(std::ostream& ostr, const t_vec& vec)
{
	for(std::size_t i=0; i<vec.size(); ++i)
	{
		ostr << vec[i];
		if(i < vec.size()-1)
			ostr << ", ";
	}

	return ostr;
}


/**
 * print matrix
 */
std::ostream& operator<<(std::ostream& ostr, const t_mat& mat)
{
	for(std::size_t i=0; i<mat.size1(); ++i)
	{
		for(std::size_t j=0; j<mat.size2(); ++j)
		{
			ostr << mat(i,j);

			if(j < mat.size2()-1)
				ostr << ", ";
		}

		if(i < mat.size1()-1)
			ostr << "; ";
	}

	return ostr;
}



/**
 * convert CIF to XML
 */
bool convert_cif(const char* pcFileIn, const char* pcFileOut)
{
	auto [errstr, atoms, generatedatoms, atomnames, lattice, ops] = load_cif<t_vec, t_mat>(pcFileIn, g_eps);
	if(errstr != "")
		std::cerr << "CIF importer messages:\n" << errstr << std::endl;


	// either open output file (if given) or use standard out
	std::ostream *pOstr = &std::cout;
	std::unique_ptr<std::ostream> _ofstr;
	if(pcFileOut)
	{
		_ofstr = std::make_unique<std::ofstream>(pcFileOut);
		pOstr = _ofstr.get();
	}



	pOstr->precision(g_prec);
Tobias WEBER's avatar
Tobias WEBER committed
108
	(*pOstr) << "<xtal>" << std::endl;
Tobias WEBER's avatar
Tobias WEBER committed
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
148
149
150
151
152
153
154


	// lattice
	(*pOstr) << "\t<lattice>\n";
	(*pOstr) << "\t\t<a> " << lattice.a << " </a>\n";
	(*pOstr) << "\t\t<b> " << lattice.b << " </b>\n";
	(*pOstr) << "\t\t<c> " << lattice.c << " </c>\n";
	(*pOstr) << "\t\t<alpha> " << lattice.alpha << " </alpha>\n";
	(*pOstr) << "\t\t<beta> " << lattice.beta << " </beta>\n";
	(*pOstr) << "\t\t<gamma> " << lattice.gamma << " </gamma>\n";
	(*pOstr) << "\t</lattice>\n";
	(*pOstr) << "\n";


	// basic atoms
	(*pOstr) << "\t<atoms>\n";
	for(std::size_t i=0; i<atoms.size(); ++i)
	{
		if(atomnames.size() == atoms.size())
			(*pOstr) << "\t\t<name" << i << "> " << atomnames[i] << " </name" << i << ">\n";
		(*pOstr) << "\t\t<pos" << i << "> " << atoms[i] << " </pos" << i << ">\n";
	}
	(*pOstr) << "\t</atoms>\n";
	(*pOstr) << "\n";


	// generated atoms
	(*pOstr) << "\t<generated_atoms>\n";
	for(std::size_t i=0; i<generatedatoms.size(); ++i)
	{
		(*pOstr) << "\t\t<pos" << i << ">\n";

		for(std::size_t j=0; j<generatedatoms[i].size(); ++j)
			(*pOstr) << "\t\t\t<gen" << j << "> " << generatedatoms[i][j] << " </gen" << j << ">\n";

		(*pOstr) << "\t\t</pos" << i << ">\n";
	}
	(*pOstr) << "\t</generated_atoms>\n";
	(*pOstr) << "\n";


	// symops
	(*pOstr) << "\t<symops>\n";
	for(std::size_t i=0; i<ops.size(); ++i)
		(*pOstr) << "\t\t<op" << i << "> " << ops[i] << " </op" << i << ">\n";
	(*pOstr) << "\t</symops>\n";
Tobias WEBER's avatar
Tobias WEBER committed
155
156
	(*pOstr) << "\n";

Tobias WEBER's avatar
Tobias WEBER committed
157

Tobias WEBER's avatar
Tobias WEBER committed
158
159
160
161
162
	// meta infos
	(*pOstr) << "\t<meta>\n";
	(*pOstr) << "\t\t<origin> " << g_strSig << " </origin>\n";
	(*pOstr) << "\t\t<version> " << g_strVer << " </version>\n";
	(*pOstr) << "\t</meta>\n";
Tobias WEBER's avatar
Tobias WEBER committed
163

Tobias WEBER's avatar
Tobias WEBER committed
164
165

	(*pOstr) << "</xtal>" << std::endl;
Tobias WEBER's avatar
Tobias WEBER committed
166
167
168
169
170
171
172
173
174
175
176

	return true;
}



/**
 * show infos about the tool
 */
static void show_infos(const char* pcProg)
{
Tobias WEBER's avatar
Tobias WEBER committed
177
	std::cout << "CIF to XML converter, version " << g_strVer << ".\n";
178
	std::cout << "Written by Tobias Weber <tweber@ill.fr> in May 2019.\n";
Tobias WEBER's avatar
Tobias WEBER committed
179

Tobias WEBER's avatar
Tobias WEBER committed
180
181
182
183
184
185
186
187
	std::cout << "\nThis program is free software: you can redistribute it and/or modify "
		"it under the terms of the GNU General Public License as published by "
		"the Free Software Foundation, version 3 of the License.\n"
		"This program is distributed in the hope that it will be useful, "
		"but WITHOUT ANY WARRANTY; without even the implied warranty of "
		"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the "
		"GNU General Public License for more details.\n"
		"You should have received a copy of the GNU General Public License "
188
189
		"along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n";

190
	std::cout << "This program uses the Gemmi library (version " << GEMMI_VERSION << "), "
191
		<< "which is available under: https://github.com/project-gemmi/gemmi.\n";
Tobias WEBER's avatar
Tobias WEBER committed
192
	std::cout << std::endl;
Tobias WEBER's avatar
Tobias WEBER committed
193
194
195

	std::cerr << "\nUsage: " << pcProg << " <in.cif> <out.xml>\n";
	std::cerr << std::endl;
Tobias WEBER's avatar
Tobias WEBER committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
}




/**
 * entry point
 */
int main(int argc, char** argv)
{
	const char* pcFileIn = nullptr;
	const char* pcFileOut = nullptr;

	if(argc <= 1)
	{
		show_infos(argv[0]);
		return 0;
	}
	else if(argc == 2)
	{
		pcFileIn = argv[1];
	}
	else if(argc >= 3)
	{
		pcFileIn = argv[1];
		pcFileOut = argv[2];
	}


	if(!convert_cif(pcFileIn, pcFileOut))
		return -1;
Tobias WEBER's avatar
Tobias WEBER committed
227

Tobias WEBER's avatar
Tobias WEBER committed
228
229
	return 0;
}