map.cpp 2.68 KB
Newer Older
qon's avatar
qon committed
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
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
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
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>

#include "map.h"

using namespace std;

int bioem_RefMap::readRefMaps()
{
    /**************************************************************************************/
    /***********************Reading reference Particle Maps************************/
    /**************************************************************************************/
	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)
		{
			cout << "BIOEM_MAX_MAPS too small\n";
			exit(1);
		}
		fread(&Ref[0], sizeof(Ref[0]), ntotRefMap, fp);
		fclose(fp);
		
		cout << "Particle Maps read from Map Dump \nTotal Number of particles: " << ntotRefMap ;
		cout << "\n+++++++++++++++++++++++++++++++++++++++++ \n";		
	}
	else
	{
		int nummap=-1;
		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);
				}
			}
			else
			{
				int i,j;
				myfloat_t z;
				
				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)
				{
					Ref[nummap].points[i-1][j-1]=z;
				}
				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";
		
		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);
		}
	}

    return(0);
}