cif2xml.cpp 3.86 KB
Newer Older
Tobias WEBER's avatar
Tobias WEBER committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * converts a CIF file into a more parsable XML
 * @author Tobias Weber <tweber@ill.fr>
 * @date may-2019
 * @license GPLv3, see 'LICENSE' file
 */

#include "loadcif.h"
#include "libs/_cxx20/math_algos.h"

#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
23
24
25
static const std::string g_strSig = "cif2xml";
static const std::string g_strVer = "0.5";

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

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


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

Tobias WEBER's avatar
Tobias WEBER committed
138

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

Tobias WEBER's avatar
Tobias WEBER committed
145
146

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

	return true;
}



/**
 * show infos about the tool
 */
static void show_infos(const char* pcProg)
{
Tobias WEBER's avatar
Tobias WEBER committed
158
	std::cout << "This is a CIF to XML converter, version " << g_strVer << ".\n";
Tobias WEBER's avatar
Tobias WEBER committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
	std::cout << "Written by Tobias Weber (tweber@ill.fr) in May 2019.\n";

	std::cout << "\nUsage: " << pcProg << " <in.cif> <out.xml>\n";
	std::cout << std::endl;
}




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

Tobias WEBER's avatar
Tobias WEBER committed
195
196
	return 0;
}