map.cpp 4.44 KB
Newer Older
1
2
3
4
5
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
6
7
#include <math.h>
#include <fftw3.h>
8
9

#include "map.h"
10
11
#include "param.h"

12
13
using namespace std;

14
int bioem_RefMap::readRefMaps(bioem_param& param)
15
{
16
17
18
	/**************************************************************************************/
	/***********************Reading reference Particle Maps************************/
	/**************************************************************************************/
19
20
21
22
23
24
25
26
27
28
29
	if (loadMap)
	{
		FILE* fp = fopen("maps.dump", "rb");
		if (fp == NULL)
		{
			cout << "Error opening dump file\n";
			exit(1);
		}
		fread(&ntotRefMap, sizeof(ntotRefMap), 1, fp);
		if (ntotRefMap > BIOEM_MAX_MAPS)
		{
30
31
			cout << "BIOEM_MAX_MAPS too small, some maps dropped\n";
			ntotRefMap = BIOEM_MAX_MAPS;
32
33
34
		}
		fread(&Ref[0], sizeof(Ref[0]), ntotRefMap, fp);
		fclose(fp);
35

36
		cout << "Particle Maps read from Map Dump \nTotal Number of particles: " << ntotRefMap ;
37
		cout << "\n+++++++++++++++++++++++++++++++++++++++++ \n";
38
39
40
41
	}
	else
	{
		int nummap=-1;
42
43
		int lasti=0;
		int lastj=0;
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
		ifstream input(filemap);
		if (!input.good())
		{
			cout << "Particle Maps Failed to open file" << endl ;
			exit(1);
		}

		char line[512] = {' '};
		char tmpLine[512] = {' '};

		while (!input.eof())
		{
			input.getline(line,512);

			strncpy(tmpLine,line,strlen(line));
			char *token = strtok(tmpLine," ");

			if (strcmp(token,"PARTICLE")==0) // to count the number of maps
			{
				nummap++;
				if (nummap % 128 == 0)
				{
					cout << "..." << nummap << "\n";
				}
				if (nummap == BIOEM_MAX_MAPS)
				{
					cout << "BIOEM_MAX_MAPS too small\n";
					exit(1);
				}
73
74
75
76
77
				if(lasti!=param.param_device.NumberPixels && lastj!=param.param_device.NumberPixels && nummap>0)
				{
					cout << "PROBLEM INCONSISTENT NUMBER OF PIXELS IN MAPS AND INPUTFILE ( " << param.param_device.NumberPixels << ", i " << lasti << ", j " << lastj << ")" << "\n";
					exit(1);
				}
78
79
80
81
			}
			else
			{
				int i,j;
82
				float z;
83

84
85
86
87
88
89
90
91
92
93
94
95
96
				char tmpVals[36]  = {' '};

				strncpy (tmpVals,line,8);
				sscanf (tmpVals,"%d",&i);

				strncpy (tmpVals,line+8,8);
				sscanf (tmpVals,"%d",&j);

				strncpy (tmpVals,line+16,12);
				sscanf (tmpVals,"%f",&z);
				//checking for Map limits
				if(i>0 && i-1 <BIOEM_MAP_SIZE_X&& j>0 && j-1<BIOEM_MAP_SIZE_Y && nummap < BIOEM_MAX_MAPS)
				{
97
					Ref[nummap].points[i-1][j-1] = (myfloat_t) z;
98
99
					lasti=i;
					lastj=j;
100
101
102
103
104
105
106
107
108
109
110
111
				}
				else
				{
					cout << "PROBLEM READING MAP (Map number " << nummap << ", i " << i << ", j " << j << ")" << "\n";
					exit(1);
				}
			}
		}
		cout << ".";
		ntotRefMap=nummap+1;
		cout << "Particle Maps read from Standard File \nTotal Number of particles: " << ntotRefMap ;
		cout << "\n+++++++++++++++++++++++++++++++++++++++++ \n";
112

113
114
115
116
117
118
119
120
121
122
123
124
125
126
		if (dumpMap)
		{
			FILE* fp = fopen("maps.dump", "w+b");
			if (fp == NULL)
			{
				cout << "Error opening dump file\n";
				exit(1);
			}
			fwrite(&ntotRefMap, sizeof(ntotRefMap), 1, fp);
			fwrite(&Ref[0], sizeof(Ref[0]), ntotRefMap, fp);
			fclose(fp);
		}
	}

127
128
129
130
131
132
133
134
135
	return(0);
}

int bioem_RefMap::PreCalculateMapsFFT(bioem_param& param)
{
	/**************************************************************************************/
	/********** Routine that pre-calculates Kernels for Convolution **********************/
	/************************************************************************************/

136
137
	myfloat_t* localMap;

138
	localMap= (myfloat_t *) myfftw_malloc(sizeof(myfloat_t) * param.param_device.NumberPixels * param.param_device.NumberPixels);
139

140
	RefMapsFFT = new mycomplex_t[ntotRefMap * param.RefMapSize];
141

142
	mycomplex_t* localout;
143
	localout= (mycomplex_t *) myfftw_malloc(sizeof(mycomplex_t) * param.param_device.NumberPixels * param.param_device.NumberFFTPixels1D);
144
145
146
147
148
149
150
151

	for (int iRefMap = 0; iRefMap < ntotRefMap ; iRefMap++)
	{
		//Assigning localMap values to padded Map
		for(int i=0; i< param.param_device.NumberPixels; i++)
		{
			for(int j=0; j< param.param_device.NumberPixels; j++)
			{
152
				localMap[i*param.param_device.NumberPixels+j]=Ref[iRefMap].points[i][j];
153
154
155
156
			}
		}

		//Calling FFT_Forward
157
		myfftw_execute_dft_r2c(param.fft_plan_r2c_forward,localMap,localout);
158
159

		// Normalizing and saving the Reference CTFs
160
161
		mycomplex_t* RefMap = &RefMapsFFT[iRefMap * param.RefMapSize];
		for(int i=0; i < param.param_device.NumberPixels * param.param_device.NumberFFTPixels1D ; i++ )
162
		{
163
164
			RefMap[i][0]=localout[i][0];
			RefMap[i][1]=localout[i][1];
165
166
167
168
169
170
171
		}
	}

	myfftw_free(localMap);
	myfftw_free(localout);

	return(0);
172
}