tof.cpp 4.44 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
 * convert tof files to images
 * @author Tobias Weber <tweber@ill.fr>
 * @date 15/nov/2021
 * @license GPLv2
 *
 * g++ -o tof tof.cpp -lboost_system -lboost_filesystem -lboost_iostreams -lpng
 *
 * ----------------------------------------------------------------------------
 * Takin (inelastic neutron scattering software package)
 * Copyright (C) 2017-2021  Tobias WEBER (Institut Laue-Langevin (ILL),
 *                          Grenoble, France).
 * Copyright (C) 2013-2017  Tobias WEBER (Technische Universitaet Muenchen
 *                          (TUM), Garching, Germany).
 *
 * 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 2 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 * ----------------------------------------------------------------------------
 */

#include <iostream>
#include <sstream>
33
#include <fstream>
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <string>

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
namespace fs = boost::filesystem;

#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
namespace ios = boost::iostreams;

#include <boost/gil/image.hpp>
#include <boost/gil/extension/io/png.hpp>
namespace gil = boost::gil;


#define PSD_WIDTH  128
#define PSD_HEIGHT 128
#define TOF_COUNT  128


bool convert_tof(const fs::path& tof_file, const fs::path& out_file)
{
56
	// tof file data type for counts
57
58
	using t_data = std::uint32_t;

59
	// total image, summing over all channels
Tobias WEBER's avatar
Tobias WEBER committed
60
61
62
	gil::gray16_image_t total_png(PSD_WIDTH, PSD_HEIGHT);
	auto total_view = gil::view(total_png);

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
	for(unsigned t=0; t<TOF_COUNT; ++t)
	{
		std::ostringstream ostr_file;
		ostr_file << out_file.string() << "_" << t << ".png";
		std::string png_file = ostr_file.str();

		ios::mapped_file_source file(tof_file,
			PSD_WIDTH*PSD_HEIGHT*sizeof(t_data),
			t*PSD_WIDTH*PSD_HEIGHT*sizeof(t_data));

		if(!file.is_open())
			return false;

		const t_data* data = reinterpret_cast<const t_data*>(file.data());

78
		// image of a tof channel
79
80
81
82
83
84
85
		gil::gray16_image_t png(PSD_WIDTH, PSD_HEIGHT);
		auto view = gil::view(png);

		std::uint64_t counts = 0;
		for(unsigned y=0; y<PSD_HEIGHT; ++y)
		{
			auto row = view.row_begin(y);
Tobias WEBER's avatar
Tobias WEBER committed
86
			auto total_row = total_view.row_begin(y);
87
88
89
90
91
92

			for(unsigned x=0; x<PSD_WIDTH; ++x)
			{
				t_data cnt = data[y*PSD_WIDTH + x];

				*(row + x) = cnt;
Tobias WEBER's avatar
bugfix    
Tobias WEBER committed
93
94
95
96
97

				if(t == 0)
					*(total_row + x) = cnt;
				else
					*(total_row + x) = *(total_row + x) + cnt;
98

Tobias WEBER's avatar
Tobias WEBER committed
99
				counts += cnt;
100
101
102
103
104
105
106
107
108
109
				//std::cout << std::hex << cnt << " ";
			}
			//std::cout << std::endl;
		}

		file.close();

		if(counts)
		{
			//std::cout << "Channel " << t << ": " << std::dec << counts << std::endl;
110
			// write channel image
111
112
113
114
			gil::write_view(png_file, view, gil::png_tag());
		}
	}

115
	// write total image
Tobias WEBER's avatar
Tobias WEBER committed
116
117
	std::string total_png_file = out_file.string() + ".png";
	gil::write_view(total_png_file, total_view, gil::png_tag());
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

	// meta information at the end of the file
	std::size_t size = fs::file_size(tof_file);
	std::ptrdiff_t size_rest = size - PSD_WIDTH*PSD_HEIGHT*TOF_COUNT*sizeof(t_data);
	if(size_rest > 0)
	{
		ios::mapped_file_source file(tof_file,
			size_rest,
			PSD_WIDTH*PSD_HEIGHT*TOF_COUNT*sizeof(t_data));

		if(file.is_open())
		{
			const char* data = reinterpret_cast<const char*>(file.data());

			std::string txt_file = out_file.string() + ".txt";
			std::ofstream ofstr_txt(txt_file);

			ofstr_txt.write(data, size_rest);
		}
	}

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
	return true;
}


int main(int argc, char** argv)
{
	if(argc < 2)
	{
		std::cerr << "Please give some TOF files." << std::endl;
		return -1;
	}

	for(int i=1; i<argc; ++i)
	{
		fs::path file(argv[i]);

		if(!fs::exists(file))
		{
			std::cerr << "File \"" << argv[i] << "\" does not exist!" << std::endl;
			continue;
		}


		fs::path file_out = file;
		file_out.replace_extension("");

		if(convert_tof(file, file_out))
			std::cout << "Converted file \"" << argv[i] << "\"." << std::endl;
		else
			std::cerr << "Failed to convert file \"" << argv[i] << "\"." << std::endl;
	}

	return 0;
}