cif2xml.cpp 4.69 KB
Newer Older
Tobias WEBER's avatar
Tobias WEBER committed
1
2
3
4
5
6
7
/**
 * converts a CIF file into a more parsable XML
 * @author Tobias Weber <tweber@ill.fr>
 * @date may-2019
 * @license GPLv3, see 'LICENSE' file
 */

Tobias WEBER's avatar
Tobias WEBER committed
8
#include "../structfact/loadcif.h"
Tobias WEBER's avatar
Tobias WEBER committed
9
#include "libs/math_algos.h"
Tobias WEBER's avatar
Tobias WEBER committed
10

11
#include <gemmi/version.hpp>
Tobias WEBER's avatar
Tobias WEBER committed
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <memory>


using t_real = double;
using t_vec = std::vector<t_real>;
using t_mat = m::mat<t_real, std::vector>;

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


Tobias WEBER's avatar
Tobias WEBER committed
24
25
26
static const std::string g_strSig = "cif2xml";
static const std::string g_strVer = "0.5";

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

/**
 * 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
90
	(*pOstr) << "<xtal>" << std::endl;
Tobias WEBER's avatar
Tobias WEBER committed
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


	// 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
137
138
	(*pOstr) << "\n";

Tobias WEBER's avatar
Tobias WEBER committed
139

Tobias WEBER's avatar
Tobias WEBER committed
140
141
142
143
144
	// 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
145

Tobias WEBER's avatar
Tobias WEBER committed
146
147

	(*pOstr) << "</xtal>" << std::endl;
Tobias WEBER's avatar
Tobias WEBER committed
148
149
150
151
152
153
154
155
156
157
158

	return true;
}



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

Tobias WEBER's avatar
Tobias WEBER committed
162
163
164
165
166
167
168
169
	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 "
170
171
172
173
		"along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n";

	std::cout << "This program uses the Gemmi library (version " << GEMMI_VERSION << "), " 
		<< "which is available under: https://github.com/project-gemmi/gemmi.\n";
Tobias WEBER's avatar
Tobias WEBER committed
174
	std::cout << std::endl;
Tobias WEBER's avatar
Tobias WEBER committed
175
176
177

	std::cerr << "\nUsage: " << pcProg << " <in.cif> <out.xml>\n";
	std::cerr << std::endl;
Tobias WEBER's avatar
Tobias WEBER committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
}




/**
 * 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
209

Tobias WEBER's avatar
Tobias WEBER committed
210
211
	return 0;
}