mesh_file.cpp 156 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1
2
3
#ifdef _MSC_VER //kill some warnings
#define _CRT_SECURE_NO_WARNINGS
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
4
5
6
7
8
9
10
11
12
#include "inmost.h"

#if defined(USE_MESH)
#include <deque>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <stdio.h>

Kirill Terekhov's avatar
Kirill Terekhov committed
13
14


Kirill Terekhov's avatar
Kirill Terekhov committed
15
16
//vtk states

Kirill Terekhov's avatar
Kirill Terekhov committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#define R_VERSION     0
#define R_USERDATA    1
#define R_DATATYPE    2
#define R_WAITDATA    3

#define R_SGRID       4
#define R_RGRID       5
#define R_UGRID       6
#define R_POLYDATA    7
#define R_FIELD       8
#define R_SPOINTS     9

#define R_ATTRIBUTES  10
#define R_ATTRDATA    11

#define R_QUIT		  100


Kirill Terekhov's avatar
Kirill Terekhov committed
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
//gmsh states

#define GMSH_VERNONE 0
#define GMSH_VER1 1
#define GMSH_VER2 2

#define GMSH_NONE 0
#define GMSH_NODES_TOT 1
#define GMSH_NODES_NUM 2
#define GMSH_NODES_X 3
#define GMSH_NODES_Y 4
#define GMSH_NODES_Z 5
#define GMSH_ELEMENTS_TOT 6
#define GMSH_ELEMENTS_NUM 7
#define GMSH_ELEMENTS_TYPE 8
#define GMSH_ELEMENTS_REGPHYS 9
#define GMSH_ELEMENTS_REGELEM 10
#define GMSH_ELEMENTS_NUMTAGS 11
#define GMSH_ELEMENTS_TAGS 12
#define GMSH_ELEMENTS_NUMNODES 13
#define GMSH_ELEMENTS_NODELIST 14
#define GMSH_FORMAT 15
#define GMSH_SKIP_KEYWORD 16

//eclipse states
#define ECLSTRCMP(x,y) strncmp(x,y,8)

#define ECL_NEVER -1
#define ECL_NONE 0
#define ECL_SKIP_SECTION 1
#define ECL_INCLUDE 2
#define ECL_DIMENS 3
#define ECL_DX 4
#define ECL_DY 5
#define ECL_DZ 6
#define ECL_TOPS 7
#define ECL_PERMX 8
#define ECL_PERMY 9
#define ECL_PERMZ 10
#define ECL_PORO 11
#define ECL_MAPAXIS 12
#define ECL_INRAD 13
#define ECL_COORDS 14
#define ECL_ZCORN 15

#define ECL_GTYPE_NONE 0
#define ECL_GTYPE_TOPS 1
#define ECL_GTYPE_ZCORN 2
#define ECL_GTYPE_RADIAL 3
#define ECL_GTYPE_CARTESIAN 4


#define ECL_VAR_NONE 0
#define ECL_VAR_REAL 1
#define ECL_VAR_INT 2

#define ECL_IJK_DATA(i,j,k) (i + (j+k*dims[1])*dims[0])



Kirill Terekhov's avatar
Kirill Terekhov committed
95
96
97
98
99
100
101
102
103
104
template<typename T>
void ReadCoords(FILE * f,INMOST_DATA_REAL_TYPE c[3])
{
	T temp[3];
	if( fread(temp,sizeof(T),3,f) == 3 )
	{
		c[0] = temp[0];
		c[1] = temp[1];
		c[2] = temp[2];
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
105
106
107
108
109
	else 
	{
		std::cout << __FILE__ << ":" << __LINE__ << " Cannot read 3 coordinates from file " << std::endl;
		throw INMOST::BadFile;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
110
111
}

Kirill Terekhov's avatar
Kirill Terekhov committed
112
113


Kirill Terekhov's avatar
Kirill Terekhov committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#if defined(USE_PARALLEL_WRITE_TIME)
#define REPORT_MPI(x) {WriteTab(out_time) << "<MPI><![CDATA[" << #x << "]]></MPI>\n"; x;}
#define REPORT_STR(x) {WriteTab(out_time) << "<TEXT><![CDATA[" << x << "]]></TEXT>\n";}
#define REPORT_VAL(str,x) {WriteTab(out_time) << "<VALUE name=\"" << str << "\"> <CONTENT><![CDATA[" << x << "]]> </CONTENT><CODE><![CDATA[" << #x << "]]></CODE></VALUE>\n";}
#define ENTER_FUNC() long double all_time = Timer(); WriteTab(out_time) << "<FUNCTION name=\"" << __FUNCTION__ << "\" id=\"func" << func_id++ << "\">\n"; Enter();
#define EXIT_FUNC() WriteTab(out_time) << "<TIME>" << Timer() - all_time << "</TIME>\n"; Exit(); WriteTab(out_time) << "</FUNCTION>\n";
#else
#define REPORT_MPI(x) x
#define REPORT_STR(x) 
#define REPORT_VAL(str,x)
#define ENTER_FUNC()
#define EXIT_FUNC() 
#endif


#define GMV_HEADER 0
#define GMV_NODES 1
#define GMV

namespace INMOST
{

	typedef char HeaderType;
	const HeaderType EndOfData  = 0x01;
	const HeaderType NodeHeader = 0x02;
	const HeaderType EdgeHeader = 0x03;
	const HeaderType FaceHeader = 0x04;
	const HeaderType CellHeader = 0x05;
	const HeaderType ESetHeader = 0x06;
	const HeaderType TagsHeader = 0x07;
	const HeaderType MeshHeader = 0x08;
	const HeaderType EoMHeader  = 0x09;
	const HeaderType INMOSTFile   = 0x10;
	const HeaderType MeshDataHeader = 0x11;

Kirill Terekhov's avatar
Kirill Terekhov committed
149
	void Mesh::SetFileOption(std::string key, std::string val)
Kirill Terekhov's avatar
Kirill Terekhov committed
150
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
151
		for(INMOST_DATA_ENUM_TYPE k = 0; k < file_options.size(); k++)
Kirill Terekhov's avatar
Kirill Terekhov committed
152
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
153
			if(file_options[k].first == key)
Kirill Terekhov's avatar
Kirill Terekhov committed
154
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
155
				file_options[k].second = val;
Kirill Terekhov's avatar
Kirill Terekhov committed
156
157
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
158
159
160
161
162
		file_options.push_back(std::make_pair(key,val));
	}

	std::string Mesh::GetFileOption(std::string key)
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
163
		for(INMOST_DATA_ENUM_TYPE k = 0; k < file_options.size(); k++)
Kirill Terekhov's avatar
Kirill Terekhov committed
164
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
165
166
167
168
			if(file_options[k].first == key)
			{
				return file_options[k].second;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
169
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
170
		return "";
Kirill Terekhov's avatar
Kirill Terekhov committed
171
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
	
	std::ostream & operator <<(std::ostream & out, HeaderType H)
	{
		out.put(H);
		return out;
	}
	
	std::istream & operator >>(std::istream & in, HeaderType &H)
	{
		in.get(H);
		return in;
	}

	
	void Mesh::Load(std::string File)
	{
		ENTER_FUNC();
		REPORT_VAL("File",File);
		std::string LFile;
		LFile.resize(File.size());
		std::transform(File.begin(),File.end(),LFile.begin(),::tolower);
Kirill Terekhov's avatar
Kirill Terekhov committed
193
		int verbosity = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
194
		for(INMOST_DATA_ENUM_TYPE k = 0; k < file_options.size(); ++k)
Kirill Terekhov's avatar
Kirill Terekhov committed
195
196
197
198
199
200
201
202
203
204
205
		{
			if( file_options[k].first == "VERBOSITY" )
			{
				verbosity = atoi(file_options[k].second.c_str());
				if( verbosity < 0 || verbosity > 2 )
				{
					printf("%s:%d Unknown verbosity option: %s\n",__FILE__,__LINE__,file_options[k].second.c_str());
					verbosity = 1;
				}
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//		long double load_timer = Timer();
		/*
		if(File.find("gmv") != std::string::npos) //this is gmv
		{
			char readline[2048];
			std::vector<File *> fs;
			fs.push_back(fopen(File.c_str(),"r"));
			if( fs[0] == NULL ) throw BadFileName;
			while( !fs.empty() )
			{
				while( fgets(readline,2048,fs[fs.size()-1]) != NULL )
				{
					switch(state)
					{
						case 
					}
				}
				fclose(fs[fs.size()-1]);
				fs.pop();
			}
		}
		else 
		*/
		if(LFile.find(".grdecl") != std::string::npos) // this is eclipse grid
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
231
232
			FILE * f = fopen(LFile.c_str(),"r");
			if( f == NULL )
Kirill Terekhov's avatar
Kirill Terekhov committed
233
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
234
235
236
				std::cout << __FILE__ << ":" << __LINE__ << " cannot open file " << LFile << std::endl;
				throw BadFileName;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
237
			std::vector<HandleType> old_nodes(NumberOfNodes());
Kirill Terekhov's avatar
Kirill Terekhov committed
238
239
			{
				unsigned qq = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
240
				for(Mesh::iteratorNode it = BeginNode(); it != EndNode(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
241
242
					old_nodes[qq++] = *it;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
243
244
			if( !old_nodes.empty() ) 
				std::sort(old_nodes.begin(),old_nodes.end(),CentroidComparator(this));
Kirill Terekhov's avatar
Kirill Terekhov committed
245
			std::vector< std::pair< std::pair<FILE *,std::string>, int> > fs(1,std::make_pair(std::make_pair(f,File),0));
Kirill Terekhov's avatar
Kirill Terekhov committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
			char readline[2048], *p, *pend, rec[2048];
			int text_end, text_start, state = ECL_NONE, nchars;
			int waitlines = 0;
			int have_dimens = 0, totread, downread, numrecs, offset;
			int gtype = ECL_GTYPE_NONE;
			int argtype = ECL_VAR_NONE;
			int radial = ECL_GTYPE_NONE;
			Storage::real * read_arrayf = NULL;
			Storage::integer * read_arrayi = NULL;
			Storage::integer dims[3], mapaxis[6] = {0,1,0,0,1,0};
			Storage::real inrad = 0;
			std::vector<Storage::real> xyz,perm,poro, tops,zcorn;
			std::vector<Storage::integer> actnum;
			while(!fs.empty())
			{
				while(fgets(readline,2048,fs.back().first.first) != NULL)
Kirill Terekhov's avatar
Kirill Terekhov committed
262
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
263
					fs.back().second++; //line number
Kirill Terekhov's avatar
Kirill Terekhov committed
264
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
265
						if( readline[strlen(readline)-1] == '\n' ) readline[strlen(readline)-1] = '\0';
Kirill Terekhov's avatar
Kirill Terekhov committed
266
						text_end = static_cast<int>(strlen(readline));
Kirill Terekhov's avatar
Kirill Terekhov committed
267
268
269
270
271
272
273
						for(text_start = 0; isspace(readline[text_start]) && text_start < text_end; text_start++);
						if( text_start == text_end ) continue;
						for(text_end = text_end-1; isspace(readline[text_end]) && text_end > text_start; text_end--);
						readline[text_end+1] = '\0';
						p = readline + text_start;
						pend = readline + text_end + 1;
						for(char * q = p; q < pend; q++) *q = toupper(*q);
Kirill Terekhov's avatar
Kirill Terekhov committed
274
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
275
276
277
					if( p[0] == '-' && p[1] == '-' ) continue; //skip comment
					if(waitlines) {waitlines--; continue;} //skip meaningful lines
					switch(state)
Kirill Terekhov's avatar
Kirill Terekhov committed
278
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
279
280
					case ECL_NONE:
						if( !ECLSTRCMP(p,"END") ) //end of data - don't read beyond
Kirill Terekhov's avatar
Kirill Terekhov committed
281
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
282
							goto ecl_exit_loop;
Kirill Terekhov's avatar
Kirill Terekhov committed
283
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
						else if( !ECLSTRCMP(p,"INCLUDE") ) state = ECL_INCLUDE;
						else if( !ECLSTRCMP(p,"DIMENS") || !ECLSTRCMP(p,"SPECGRID") )
						{
							read_arrayi = dims;
							numrecs = 1;
							downread = totread = 3;
							argtype = ECL_VAR_INT;
							offset = state = ECL_DIMENS;
							have_dimens = 1;
						}
						else if( !ECLSTRCMP(p,"MAPAXIS") )
						{
							read_arrayi = mapaxis;
							numrecs = 1;
							downread = totread = 6;
							argtype = ECL_VAR_INT;
							offset = state = ECL_MAPAXIS;
						}
						else if( !ECLSTRCMP(p,"DX") )
						{
							assert(have_dimens);
							if( xyz.empty() ) xyz.resize(3*dims[0]*dims[1]*dims[2]);
Kirill Terekhov's avatar
Kirill Terekhov committed
306
							read_arrayf = xyz.data();
Kirill Terekhov's avatar
Kirill Terekhov committed
307
308
309
310
311
312
313
314
315
							numrecs = 3;
							downread = totread = dims[0]*dims[1]*dims[2];
							argtype = ECL_VAR_REAL;
							offset = state = ECL_DX;
						}
						else if( !ECLSTRCMP(p,"DY") )
						{
							assert(have_dimens);
							if( xyz.empty() ) xyz.resize(3*dims[0]*dims[1]*dims[2]);
Kirill Terekhov's avatar
Kirill Terekhov committed
316
							read_arrayf = xyz.data();
Kirill Terekhov's avatar
Kirill Terekhov committed
317
318
319
320
321
322
323
324
325
326
							numrecs = 3;
							downread = totread = dims[0]*dims[1]*dims[2];
							argtype = ECL_VAR_REAL;
							offset = ECL_DX;
							state = ECL_DY;
						}
						else if( !ECLSTRCMP(p,"DZ") )
						{
							assert(have_dimens);
							if( xyz.empty() ) xyz.resize(3*dims[0]*dims[1]*dims[2]);
Kirill Terekhov's avatar
Kirill Terekhov committed
327
							read_arrayf = xyz.data();
Kirill Terekhov's avatar
Kirill Terekhov committed
328
329
330
331
332
333
334
335
336
337
							numrecs = 3;
							downread = totread = dims[0]*dims[1]*dims[2];
							argtype = ECL_VAR_REAL;
							offset = ECL_DX;
							state = ECL_DZ;
						}
						else if( !ECLSTRCMP(p,"COORD") )
						{
							assert(have_dimens);
							if( xyz.empty() ) xyz.resize(2*(dims[0]+1)*(dims[1]+1));
Kirill Terekhov's avatar
Kirill Terekhov committed
338
							read_arrayf = xyz.data();
Kirill Terekhov's avatar
Kirill Terekhov committed
339
340
341
342
343
344
345
346
347
348
							numrecs = 1;
							downread = totread = 2*(dims[0]+1)*(dims[1]+1);
							argtype = ECL_VAR_REAL;
							offset = state = ECL_COORDS;
							gtype = ECL_GTYPE_ZCORN;
						}
						else if( !ECLSTRCMP(p,"ZCORN") )
						{
							assert(have_dimens);
							if( zcorn.empty() ) zcorn.resize(dims[0]*dims[1]*dims[2]*8);
Kirill Terekhov's avatar
Kirill Terekhov committed
349
							read_arrayf = zcorn.data();
Kirill Terekhov's avatar
Kirill Terekhov committed
350
351
352
353
354
355
356
357
358
							numrecs = 1;
							downread = totread = dims[0]*dims[1]*dims[2]*8;
							argtype = ECL_VAR_REAL;
							state = offset = ECL_ZCORN;
						}
						else if( !ECLSTRCMP(p,"TOPS") )
						{
							assert(have_dimens);
							tops.resize(dims[0]*dims[1]*dims[2]);
Kirill Terekhov's avatar
Kirill Terekhov committed
359
							read_arrayf = tops.data();
Kirill Terekhov's avatar
Kirill Terekhov committed
360
361
362
363
364
365
366
367
368
369
							numrecs = 1;
							downread = totread = dims[0]*dims[1]*dims[2];
							argtype = ECL_VAR_REAL;
							offset = state = ECL_TOPS;
							gtype = ECL_GTYPE_TOPS;
						}
						else if( !ECLSTRCMP(p,"PERMX") )
						{
							assert(have_dimens);
							if( perm.empty() ) perm.resize(3*dims[0]*dims[1]*dims[2]);
Kirill Terekhov's avatar
Kirill Terekhov committed
370
							read_arrayf = perm.data();
Kirill Terekhov's avatar
Kirill Terekhov committed
371
372
373
374
375
376
377
378
379
							numrecs = 3;
							downread = totread = dims[0]*dims[1]*dims[2];
							argtype = ECL_VAR_REAL;
							offset = state = ECL_PERMX;
						}
						else if( !ECLSTRCMP(p,"PERMY") )
						{
							assert(have_dimens);
							if( perm.empty() ) perm.resize(3*dims[0]*dims[1]*dims[2]);
Kirill Terekhov's avatar
Kirill Terekhov committed
380
							read_arrayf = perm.data();
Kirill Terekhov's avatar
Kirill Terekhov committed
381
382
383
384
385
386
387
388
389
390
							numrecs = 3;
							downread = totread = dims[0]*dims[1]*dims[2];
							argtype = ECL_VAR_REAL;
							offset = ECL_PERMX;
							state = ECL_PERMY;
						}
						else if( !ECLSTRCMP(p,"PERMZ") )
						{
							assert(have_dimens);
							if( perm.empty() ) perm.resize(3*dims[0]*dims[1]*dims[2]);
Kirill Terekhov's avatar
Kirill Terekhov committed
391
							read_arrayf = perm.data();
Kirill Terekhov's avatar
Kirill Terekhov committed
392
393
394
395
396
397
398
399
400
401
							numrecs = 3;
							downread = totread = dims[0]*dims[1]*dims[2];
							argtype = ECL_VAR_REAL;
							offset = ECL_PERMX;
							state = ECL_PERMZ;
						}
						else if( !ECLSTRCMP(p,"PORO") )
						{
							assert(have_dimens);
							poro.resize(3*dims[0]*dims[1]*dims[2]);
Kirill Terekhov's avatar
Kirill Terekhov committed
402
							read_arrayf = poro.data();
Kirill Terekhov's avatar
Kirill Terekhov committed
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
							numrecs = 1;
							downread = totread = dims[0]*dims[1]*dims[2];
							argtype = ECL_VAR_REAL;
							offset = state = ECL_PORO;
						}
						else if( !ECLSTRCMP(p,"RADIAL") )
						{
							radial = ECL_GTYPE_RADIAL;
						}
						else if( !ECLSTRCMP(p,"CART") )
						{
							radial = ECL_GTYPE_CARTESIAN;
						}
						else if( !ECLSTRCMP(p,"INRAD") )
						{
							if( radial != ECL_GTYPE_RADIAL ) 
							{
								std::cout << __FILE__ << ":" << __LINE__ << " inner radius specified for cartesian grid ";
								std::cout << " in " << fs.back().first.second << ":" << fs.back().second << std::endl;
							}
							if( radial == ECL_GTYPE_NONE ) radial = ECL_GTYPE_RADIAL;
							state = ECL_INRAD;
						}
						else
						{
							std::cout << __FILE__ << ":" << __LINE__ << " skipped " << p << " in " << fs.back().first.second << ":" << fs.back().second << std::endl;
						}
						break;
					case ECL_SKIP_SECTION:
						if( *(pend-1) == '/' || *p == '/' ) state = ECL_NONE;
						break;
					case ECL_INCLUDE:
						if( 1 == sscanf(p," %s",rec) )
						{
							int shift_one = 0;
							if( (rec[0] == '\'' || rec[0] == '"') && rec[0] == rec[strlen(rec)-1] ) //remove quotes
							{
								rec[strlen(rec)-1] = '\0';
								shift_one = 1;
							}
							f = fopen(rec+shift_one,"r");
							if( f == NULL )
							{
								std::cout << __FILE__ << ":" << __LINE__ << " cannot open file " << rec+shift_one << " included from "<< fs.back().first.second << " line " << fs.back().second << std::endl;
								throw BadFileName;
							}
							fs.push_back(std::make_pair(std::make_pair(f,std::string(rec+shift_one)),0));
							if( *(pend-1) == '/' ) state = ECL_NONE; else state = ECL_SKIP_SECTION;
						}
						else
						{
							std::cout << __FILE__ << ":" << __LINE__ << " cannot read file name, string " << p << " in " << fs.back().first.second << " line " << fs.back().second << std::endl;
							throw BadFile;
						}
						break;
					case ECL_ZCORN:
					case ECL_COORDS:
					case ECL_MAPAXIS:
					case ECL_DIMENS:
					case ECL_DX:
					case ECL_DY:
					case ECL_DZ:
					case ECL_PERMX:
					case ECL_PERMY:
					case ECL_PERMZ:
					case ECL_PORO:
					case ECL_TOPS:
						while( downread > 0 && p < pend )
						{
							if( 1 == sscanf(p,"%s%n",rec,&nchars) )
							{
								p += nchars;
								while(isspace(*p) && p < pend) ++p;
								int count = 1;
								int begval = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
478
								for(int q = 0; q < static_cast<int>(strlen(rec)); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
									if( rec[q] == '*' )
									{
										begval = q+1;
										rec[q] = '\0';
										break;
									}
								if( begval > 0 ) count = atoi(rec);
								if( argtype == ECL_VAR_REAL )
								{
									Storage::real val = atof(rec+begval);
									while(count)
									{
										read_arrayf[numrecs*(totread-(downread--))+(state-offset)] = val;
										count--;
									}
								}
								else if( argtype == ECL_VAR_INT )
								{
Kirill Terekhov's avatar
Kirill Terekhov committed
497
									Storage::integer val = atoi(rec+begval);
Kirill Terekhov's avatar
Kirill Terekhov committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
									while(count)
									{
										read_arrayi[numrecs*(totread-(downread--))+(state-offset)] = val;
										count--;
									}
								}
								else
								{
									std::cout << __FILE__ << ":" << __LINE__ << " probably forgot to set up argument type to read " << std::endl;
									throw Impossible;
								}
							}
							else
							{
								std::cout << __FILE__ << ":" << __LINE__ << " cannot read data " << p << " in " << fs.back().first.second << ":" << fs.back().second << std::endl;
								throw BadFile;
							}
							if( *p == '/' ) 
							{
								if( downread > 0 )
								{
									std::cout << __FILE__ << ":" << __LINE__ << " early data termination, read " << totread-downread << " of " << totread << " records in " << fs.back().first.second << ":" << fs.back().second << std::endl;
									throw BadFile;
								}
							}
						}
						if( *(pend-1) == '/' || *p == '/' ) state = ECL_NONE; else state = ECL_SKIP_SECTION;
						break;
					case ECL_INRAD:
						if( 1 == sscanf(p,"%lf%n",&inrad,&nchars) )
						{
							p += nchars;
							while(isspace(*p) && p < pend) ++p;
						}
						else
						{
							std::cout << __FILE__ << ":" << __LINE__ << " cannot read data " << p << " in " << fs.back().first.second << ":" << fs.back().second << std::endl;
							throw BadFile;
						}
						if( *(pend-1) == '/' || *p == '/' ) state = ECL_NONE; else state = ECL_SKIP_SECTION;
						break;
Kirill Terekhov's avatar
Kirill Terekhov committed
539
540
					}
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
541
542
543
ecl_exit_loop:
				fclose(fs.back().first.first);
				fs.pop_back();
Kirill Terekhov's avatar
Kirill Terekhov committed
544
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
545
			if( radial == ECL_GTYPE_RADIAL )
Kirill Terekhov's avatar
Kirill Terekhov committed
546
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
547
548
549
550
				std::cout << __FILE__ << ":" << __LINE__ << " radial grids not supported yet " << std::endl;
			}
			if( gtype == ECL_GTYPE_TOPS )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
551
				std::vector<HandleType> newnodes((dims[0]+1)*(dims[1]+1)*(dims[2]+1));
Kirill Terekhov's avatar
Kirill Terekhov committed
552
553
				Storage::real x, y, z, node_xyz[3];
				x = 0.0;
Kirill Terekhov's avatar
Kirill Terekhov committed
554
				int numnode = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
555
				for(int i = 0; i < dims[0]+1; i++)
Kirill Terekhov's avatar
Kirill Terekhov committed
556
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
557
558
559
					Storage::integer pif = std::min(dims[0]-1,i), pib = std::max(i-1,0);
					y = 0.0;
					for(int j = 0; j < dims[1]+1; j++)
Kirill Terekhov's avatar
Kirill Terekhov committed
560
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
561
562
563
564
565
566
567
568
569
570
571
572
573
574
						Storage::integer pjf = std::min(dims[1]-1,j), pjb = std::max(j-1,0);
						z = (
							 tops[ECL_IJK_DATA(pib,pjb,0)]+
							 tops[ECL_IJK_DATA(pib,pjf,0)]+
							 tops[ECL_IJK_DATA(pif,pjb,0)]+
							 tops[ECL_IJK_DATA(pif,pjf,0)]
						    )*0.25;
						z -= (
							  xyz[3*ECL_IJK_DATA(pib,pjb,0)+2]+
							  xyz[3*ECL_IJK_DATA(pib,pjf,0)+2]+
							  xyz[3*ECL_IJK_DATA(pif,pjb,0)+2]+
							  xyz[3*ECL_IJK_DATA(pif,pjf,0)+2]
							 )*0.25;
						for(int k = 0; k < dims[2]+1; k++)
Kirill Terekhov's avatar
Kirill Terekhov committed
575
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
576
577
578
579
580
581
							Storage::integer pkf = std::min(dims[2]-1,k), pkb = std::max(k-1,0);
							node_xyz[0] = x;
							node_xyz[1] = y;
							node_xyz[2] = z;
							int find = -1;
							if( !old_nodes.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
582
583
584
585
586
587
							{
								std::vector<HandleType>::iterator it = std::lower_bound(old_nodes.begin(),old_nodes.end(),node_xyz,CentroidComparator(this));
								if( it != old_nodes.end() ) find = static_cast<int>(it - old_nodes.begin());
							}
							if( find == -1 ) newnodes[numnode++] = CreateNode(node_xyz)->GetHandle();
							else newnodes[numnode++] = old_nodes[find];
Kirill Terekhov's avatar
Kirill Terekhov committed
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
							//std::cout << i << " " << j << " " << k << " ( " << x << " , " << y << " , " << z << ") " << newnodes.back()->LocalID() << std::endl; 
							x += (
								  (
								   xyz[3*ECL_IJK_DATA(pib,pjb,pkf)+0]+
								   xyz[3*ECL_IJK_DATA(pib,pjf,pkf)+0]+
								   xyz[3*ECL_IJK_DATA(pif,pjb,pkf)+0]+
								   xyz[3*ECL_IJK_DATA(pif,pjf,pkf)+0]
								  )
								-
								  (
								   xyz[3*ECL_IJK_DATA(pib,pjb,pkb)+0]+
								   xyz[3*ECL_IJK_DATA(pib,pjf,pkb)+0]+
								   xyz[3*ECL_IJK_DATA(pif,pjb,pkb)+0]+
								   xyz[3*ECL_IJK_DATA(pif,pjf,pkb)+0]
								  )
								 )*0.25;
							y += (
								  (
							       xyz[3*ECL_IJK_DATA(pib,pjb,pkf)+1]+
								   xyz[3*ECL_IJK_DATA(pib,pjf,pkf)+1]+
								   xyz[3*ECL_IJK_DATA(pif,pjb,pkf)+1]+
								   xyz[3*ECL_IJK_DATA(pif,pjf,pkf)+1]
								  )
								-
								  (
								   xyz[3*ECL_IJK_DATA(pib,pjb,pkb)+1]+
								   xyz[3*ECL_IJK_DATA(pib,pjf,pkb)+1]+
								   xyz[3*ECL_IJK_DATA(pif,pjb,pkb)+1]+
								   xyz[3*ECL_IJK_DATA(pif,pjf,pkb)+1]
								  )
								 )*0.25;
							z += (
								  xyz[3*ECL_IJK_DATA(pib,pjb,pkb)+2]+
								  xyz[3*ECL_IJK_DATA(pib,pjf,pkb)+2]+
								  xyz[3*ECL_IJK_DATA(pif,pjb,pkb)+2]+
								  xyz[3*ECL_IJK_DATA(pif,pjf,pkb)+2]+
							      xyz[3*ECL_IJK_DATA(pib,pjb,pkf)+2]+
								  xyz[3*ECL_IJK_DATA(pib,pjf,pkf)+2]+
								  xyz[3*ECL_IJK_DATA(pif,pjb,pkf)+2]+
								  xyz[3*ECL_IJK_DATA(pif,pjf,pkf)+2]
								 )*0.125;
Kirill Terekhov's avatar
Kirill Terekhov committed
629
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
630
631
632
633
634
635
						y += (
							  xyz[3*ECL_IJK_DATA(pib,pjb,0)+1]+
							  xyz[3*ECL_IJK_DATA(pif,pjb,0)+1]+
							  xyz[3*ECL_IJK_DATA(pib,pjf,0)+1]+
							  xyz[3*ECL_IJK_DATA(pif,pjf,0)+1]
							 )*0.25; 
Kirill Terekhov's avatar
Kirill Terekhov committed
636
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
637
638
639
640
					x += (
						  xyz[3*ECL_IJK_DATA(pib,0,0)+0]+
						  xyz[3*ECL_IJK_DATA(pif,0,0)+0]
						 )*0.5; 
Kirill Terekhov's avatar
Kirill Terekhov committed
641
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
642
643
644
645
				Tag tagporo,tagperm;
				if( !poro.empty() ) tagporo = CreateTag("PORO",DATA_REAL,CELL,NONE,1);
				if( !perm.empty() ) tagperm = CreateTag("PERM",DATA_REAL,CELL,NONE,3);

Kirill Terekhov's avatar
Kirill Terekhov committed
646
647
				const Storage::integer nvf[24] = { 2, 3, 1, 0, 4, 5, 7, 6, 0, 1, 5, 4, 3, 2, 6, 7, 2, 0, 4, 6, 1, 3, 7, 5 };
				const Storage::integer numnodes[6] = { 4, 4, 4, 4, 4, 4 };
Kirill Terekhov's avatar
Kirill Terekhov committed
648
649
650
651
				for(int i = 0; i < dims[0]; i++)
					for(int j = 0; j < dims[1]; j++)
						for(int k = 0; k < dims[2]; k++)
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
652
							HandleType verts[8];
Kirill Terekhov's avatar
Kirill Terekhov committed
653
654
655
656
657
658
659
660
661
662
							verts[0] = newnodes[((i+0)*(dims[1]+1)+(j+0))*(dims[2]+1)+(k+0)];
							verts[1] = newnodes[((i+1)*(dims[1]+1)+(j+0))*(dims[2]+1)+(k+0)];
							verts[2] = newnodes[((i+0)*(dims[1]+1)+(j+1))*(dims[2]+1)+(k+0)];
							verts[3] = newnodes[((i+1)*(dims[1]+1)+(j+1))*(dims[2]+1)+(k+0)];
							verts[4] = newnodes[((i+0)*(dims[1]+1)+(j+0))*(dims[2]+1)+(k+1)];
							verts[5] = newnodes[((i+1)*(dims[1]+1)+(j+0))*(dims[2]+1)+(k+1)];
							verts[6] = newnodes[((i+0)*(dims[1]+1)+(j+1))*(dims[2]+1)+(k+1)];
							verts[7] = newnodes[((i+1)*(dims[1]+1)+(j+1))*(dims[2]+1)+(k+1)];
							//for(int q = 0; q < 8; q++)
							//	std::cout << verts[q]->Coords()[0] << " " << verts[q]->Coords()[1] << " " << verts[q]->Coords()[2] << " " << verts[q]->LocalID() << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
663
							Cell c = CreateCell(ElementArray<Node>(this,verts,verts+8),nvf,numnodes,6).first;
Kirill Terekhov's avatar
Kirill Terekhov committed
664
665
666
667
668
669
670
671
672
673
674
675
							if( !poro.empty() ) c->RealDF(tagporo) = poro[(i*dims[1]+j)*dims[2]+k];
							if( !perm.empty() )
							{
								Storage::real_array arr_perm = c->RealArrayDF(tagperm);
								arr_perm[0] = perm[3*((i*dims[1]+j)*dims[2]+k)+0];
								arr_perm[1] = perm[3*((i*dims[1]+j)*dims[2]+k)+1];
								arr_perm[2] = perm[3*((i*dims[1]+j)*dims[2]+k)+2];
							}
						}
			}
			else if( gtype == ECL_GTYPE_ZCORN )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
676
677
				//std::vector<HandleType> block_nodes(dims[0]*dims[1]*dims[2]*8);
				//Storage::integer block_zcorn[8];
Kirill Terekhov's avatar
Kirill Terekhov committed
678
679
680
681
682
683
			}
		}
		else
		if(LFile.find(".pvtk") != std::string::npos) //this is legacy parallel vtk
		{
			int state = 0, np, nf = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
684
			std::string::size_type attrl, ql, qr, l;
Kirill Terekhov's avatar
Kirill Terekhov committed
685
686
687
688
			//~ if( m_state == Mesh::Serial ) SetCommunicator(INMOST_MPI_COMM_WORLD);
			std::string str, tag, attrval, path = "";
			std::vector<std::string> files;
			std::fstream stream(File.c_str(),std::ios::in);
Kirill Terekhov's avatar
Kirill Terekhov committed
689
			l = static_cast<INMOST_DATA_ENUM_TYPE>(File.find_last_of("/\\"));
Kirill Terekhov's avatar
Kirill Terekhov committed
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
			if( l != std::string::npos )
				path = File.substr(0,l+1);
			while(stream.good())
			{
				getline(stream,str,'>');
				l = str.find_first_not_of(" \t\r\n");
				if( l != std::string::npos ) str.substr(l,str.size()-l).swap(str);
				else str.clear();
				if( str.empty() ) continue;
				l = str.find_first_of(' ');
				tag = str.substr(1,l-1);
				//std::cout << " tag name " << tag << std::endl;
				if( state == 0 )
				{
					if( tag == "File" )
					{
						attrl = str.find("version=\"pvtk-1.0\"",l);
Kirill Terekhov's avatar
Kirill Terekhov committed
707
708
						if( attrl == std::string::npos ) 
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
709
							std::cout << __FILE__ << ":" << __LINE__ << " version information not found in " << File << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
710
711
							throw BadFile;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
712
						attrl = str.find("dataType=\"vtkUnstructuredGrid\"",l);
Kirill Terekhov's avatar
Kirill Terekhov committed
713
714
						if( attrl == std::string::npos ) 
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
715
							std::cout << __FILE__ << ":" << __LINE__ << " data type information not found in " << File << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
716
717
							throw BadFile;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
718
						attrl = str.find("numberOfPieces");
Kirill Terekhov's avatar
Kirill Terekhov committed
719
720
						if( attrl == std::string::npos ) 
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
721
							std::cout << __FILE__ << ":" << __LINE__ << " number of files not found in " << File << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
722
723
							throw BadFile;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
724
725
726
727
728
729
730
731
732
						ql = str.find_first_of('"',attrl);
						qr = str.find_first_of('"',ql+1);
						attrval = str.substr(ql+1,qr-ql-1);
						//std::cout << "number of peaces: " << attrval << " l: " << ql << " r: " << qr << std::endl;
						np = atoi(attrval.c_str());
						//std::cout << "np: " << np << std::endl;;
						files.resize(np);
						state = 1;
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
733
734
					else 
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
735
						std::cout << __FILE__ << ":" << __LINE__ << " unexpected xml tag " << tag << " in " << File << " expected File instead " << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
736
737
						throw BadFile;
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
738
739
740
741
742
743
744
745
746
747
748
749
750
				}
				else if( state == 1 )
				{
					if( tag == "Piece")
					{
						attrl = str.find("fileName");
						ql = str.find_first_of('"',attrl);
						qr = str.find_first_of('"',ql+1);
						attrval = str.substr(ql+1,qr-ql-1);
						//std::cout << "file" << nf << ":" << attrval << std::endl;
						files[nf++] = attrval;
						if( nf == np ) state = 2;
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
751
752
					else 
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
753
						std::cout << __FILE__ << ":" << __LINE__ << " unexpected xml tag " << tag << " in " << File << " expected Piece instead " << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
754
755
						throw BadFile;
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
756
757
758
				}
				else if( state == 2 )
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
759
760
					if( tag != "/File" ) 
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
761
						std::cout << __FILE__ << ":" << __LINE__ << " unexpected xml tag " << tag << " in " << File << " expected /File instead " << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
762
763
						throw BadFile;
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
764
765
766
767
768
769
770
771
772
773
774
775
					else break;
				}
			}
			for(int i = GetProcessorRank(); i < static_cast<int>(files.size()); i+= GetProcessorsNumber()) 
			{
				//std::cout << "load " << i << ": " << path + files[i] << " by " << GetProcessorRank() << std::endl;
				Load(path + files[i]);
			}
			ResolveShared();
		}
		else if(LFile.find(".vtk") != std::string::npos) //this is legacy vtk
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
776
			MarkerType unused_marker = CreateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
777
			bool grid_is_2d = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
778
			for(INMOST_DATA_ENUM_TYPE k = 0; k < file_options.size(); ++k)
Kirill Terekhov's avatar
Kirill Terekhov committed
779
780
781
782
783
784
785
786
787
788
			{
				if( file_options[k].first == "VTK_GRID_DIMS" )
				{
					if( atoi(file_options[k].second.c_str()) == 2 )
					{
						grid_is_2d = true;
						break;
					}
				}
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
789
			std::vector<HandleType> old_nodes(NumberOfNodes());
Kirill Terekhov's avatar
Kirill Terekhov committed
790
791
			{
				unsigned qq = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
792
				for(Mesh::iteratorNode it = BeginNode(); it != EndNode(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
793
794
					old_nodes[qq++] = *it;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
795
796
797
			if( !old_nodes.empty() ) 
				std::sort(old_nodes.begin(),old_nodes.end(),CentroidComparator(this));

Kirill Terekhov's avatar
Kirill Terekhov committed
798
799
			
			FILE * f = fopen(File.c_str(),"r");
Kirill Terekhov's avatar
Kirill Terekhov committed
800
801
			if( !f ) 
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
802
				std::cout << __FILE__ << ":" << __LINE__ << " cannot open " << File << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
803
804
805
				throw BadFileName;
			}
			std::vector<Tag> datatags;
Kirill Terekhov's avatar
Kirill Terekhov committed
806
807
			std::vector<HandleType> newnodes;
			std::vector<HandleType> newcells;
Kirill Terekhov's avatar
Kirill Terekhov committed
808
809
810
811
812
813
			std::vector<int> cp;
			std::vector<int> ct;
			unsigned int state = R_VERSION;
			char readline[2048];
			int filled;
			bool binary = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
814
			int read_into = NONE, read_into_cell = NONE;
Kirill Terekhov's avatar
Kirill Terekhov committed
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
			while( state != R_QUIT )
			{
				
				if( fgets(readline,2048,f) == NULL )
				{
					state = R_QUIT;
					continue;
				}
				if( readline[strlen(readline)-1] == '\n' )
					readline[strlen(readline)-1] = '\0';
				if( strlen(readline) == 0 ) continue;
				switch( state )
				{
					case R_VERSION:
					{
						int h,l;
						filled = sscanf(readline,"# vtk DataFile Version %1d.%1d",&h,&l);
Kirill Terekhov's avatar
Kirill Terekhov committed
832
833
						if( filled == 0 ) 
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
834
							std::cout << __FILE__ << ":" << __LINE__ << " version information not found in " << File << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
835
836
							throw BadFile;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
						//file version checker
						state = R_USERDATA;
						break;
					}
					case R_USERDATA:
					{
						//printf("Attached info: %s\n",readline);
						state = R_DATATYPE;
						break;
					}
					case R_DATATYPE:
					{
						if( !strncmp(readline,"BINARY",6) )
							binary = 1;
						else if( !strncmp(readline,"ASCII",5) )
							state = R_WAITDATA;
Kirill Terekhov's avatar
Kirill Terekhov committed
853
854
						else 
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
855
							std::cout << __FILE__ << ":" << __LINE__ << " unexpected data type " << readline << " in " << File << " expected BINARY or ASCII instead " << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
856
857
							throw BadFile;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
858
859
860
861
862
863
864
865
866
						break;
					}
					case R_ATTRDATA:
					{
						char dataname[1024];
						char attrname[1024];
						char attrtype[1024];
						int nentries = 1;
						filled = sscanf(readline," %s ",dataname);
Kirill Terekhov's avatar
Kirill Terekhov committed
867
868
						if( filled != 1 ) 
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
869
							std::cout << __FILE__ << ":" << __LINE__ << " cannot read attribute name in " << File << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
870
871
							throw BadFile;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
872
873
874
875
						if( !strcmp(dataname,"SCALARS") )
						{
							DataType t = DATA_BULK;
							filled = sscanf(readline," SCALARS %s %s %d",attrname,attrtype,&nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
876
							if( verbosity > 0 ) printf("Reading attribute %s.\n",attrname);
Kirill Terekhov's avatar
Kirill Terekhov committed
877
878
879
880
881
							if( filled < 2 ) 
							{
								printf("%s:%d found %d arguments to SCALARS field, must be >= 2\nline:\n%s\n",__FILE__,__LINE__,filled,readline); 
								throw BadFile;
							}
Kirill Terekhov's avatar
Kirill Terekhov committed
882
883
884
885
886
887
888
889
							for(unsigned int i = 0; i < strlen(attrtype); i++) attrtype[i] = tolower(attrtype[i]);
							if( !strcmp(attrtype,"bit") || !strcmp(attrtype,"unsigned_char") || !strcmp(attrtype,"char") ||
							   !strcmp(attrtype,"unsigned_short") || !strcmp(attrtype,"short") || !strcmp(attrtype,"unsigned_int") ||
							   !strcmp(attrtype,"int") || !strcmp(attrtype,"unsigned_long") || !strcmp(attrtype,"long"))
								t = DATA_INTEGER;
							else if( !strcmp(attrtype,"float") || !strcmp(attrtype,"double") )
								t = DATA_REAL;
							else
Kirill Terekhov's avatar
Kirill Terekhov committed
890
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
891
								std::cout << __FILE__ << ":" << __LINE__ << " unexpected data type " << attrtype << " in " << File << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
892
								std::cout << "expected one of: bit, unsigned_char, char, unsigned_short, short, unsigned_int, int, unsigned_long, long, float, double" << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
893
								throw BadFile;
Kirill Terekhov's avatar
Kirill Terekhov committed
894
895
896
							}
							if( fgets(readline,2048,f) == NULL ) 
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
897
								std::cout << __FILE__ << ":" << __LINE__ << " expected LOOKUP_TABLE in " << File << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
898
899
900
								throw BadFile; //LOOK_UP TABLE
							}
							if( read_into == 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
901
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
902
								Tag attr = CreateTag(attrname,t,read_into_cell,read_into_cell & ESET,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
903
								unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newcells.size()/250),1);
Kirill Terekhov's avatar
Kirill Terekhov committed
904
905
								for(unsigned int it = 0; it < newcells.size(); it++)
								{
Kirill Terekhov's avatar
Kirill Terekhov committed
906

Kirill Terekhov's avatar
Kirill Terekhov committed
907
908
									if( t == DATA_INTEGER )
									{
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
909
										if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
910
										{
Kirill Terekhov's avatar
Kirill Terekhov committed
911
											Storage::integer_array attrdata = IntegerArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
912
913
914
915
916
917
											for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										}
										else for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile;}
									}
									if( t == DATA_REAL )
									{
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
918
										if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
919
										{
Kirill Terekhov's avatar
Kirill Terekhov committed
920
											Storage::real_array attrdata = RealArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
921
922
923
924
											for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										}
										else for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; }
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
925
926
927
928
929
									if( verbosity > 1 && it%report_pace == 0)
									{
										printf("data %3.1f%%\r",(it*100.0)/(1.0*newcells.size()));
										fflush(stdout);
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
930
931
								}
								filled = fscanf(f,"\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
932
								datatags.push_back(attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
933
							}
Kirill Terekhov's avatar
Kirill Terekhov committed
934
							if( read_into == 1 )
Kirill Terekhov's avatar
Kirill Terekhov committed
935
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
936
								Tag attr = CreateTag(attrname,t,NODE,NONE,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
937
								unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newnodes.size())/250,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
938
939
940
941
								for(unsigned int it = 0; it < newnodes.size(); it++)
								{
									if( t == DATA_INTEGER )
									{
Kirill Terekhov's avatar
Kirill Terekhov committed
942
										Storage::integer_array attrdata = IntegerArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
943
944
945
946
										for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									}
									if( t == DATA_REAL )
									{
Kirill Terekhov's avatar
Kirill Terekhov committed
947
										Storage::real_array attrdata = RealArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
948
949
										for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
950
951
952
953
954
									if( verbosity > 1 && it%report_pace == 0)
									{
										printf("data %3.1f%%\r",(it*100.0)/(1.0*newnodes.size()));
										fflush(stdout);
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
955
956
957
958
959
960
961
962
963
								}
								filled = fscanf(f,"\n");
							}
							break;
						}
						else if( !strcmp(dataname,"COLOR_SCALARS") )
						{	
							char attrname[1024];
							filled = sscanf(readline," COLOR_SCALARS %s %d",attrname,&nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
964
							if( verbosity > 0 ) printf("Reading attribute %s.\n",attrname);
Kirill Terekhov's avatar
Kirill Terekhov committed
965
							if( read_into == 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
966
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
967
								Tag attr = CreateTag(attrname,DATA_REAL,read_into_cell,read_into_cell & ESET,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
968
								unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newcells.size()/250),1);
Kirill Terekhov's avatar
Kirill Terekhov committed
969
970
								for(unsigned int it = 0; it < newcells.size(); it++)
								{
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
971
									if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
972
									{
Kirill Terekhov's avatar
Kirill Terekhov committed
973
										Storage::real_array attrdata = RealArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
974
975
976
										for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									}
									else for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile;}
Kirill Terekhov's avatar
Kirill Terekhov committed
977
978
979
980
981
									if( verbosity > 1 && it%100 == 0)
									{
										printf("data %3.1f%%\r",(it*report_pace)/(1.0*newcells.size()));
										fflush(stdout);
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
982
983
								}
								filled = fscanf(f,"\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
984
								datatags.push_back(attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
985
							}
Kirill Terekhov's avatar
Kirill Terekhov committed
986
							if( read_into == 1 )
Kirill Terekhov's avatar
Kirill Terekhov committed
987
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
988
								Tag attr = CreateTag(attrname,DATA_REAL,NODE,NONE,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
989
								unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newnodes.size()/250),1);
Kirill Terekhov's avatar
Kirill Terekhov committed
990
991
								for(unsigned int it = 0; it < newnodes.size(); it++)
								{
Kirill Terekhov's avatar
Kirill Terekhov committed
992
									Storage::real_array attrdata = RealArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
993
									for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
Kirill Terekhov's avatar
Kirill Terekhov committed
994
995
996
997
998
									if( verbosity > 1 && it%report_pace == 0)
									{
										printf("data %3.1f%%\r",(it*100.0)/(1.0*newnodes.size()));
										fflush(stdout);
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
								}
								filled = fscanf(f,"\n");
							}
							break;
						}
						else if( !strcmp(dataname," LOOKUP_TABLE") )
						{
							int nentries;
							filled = sscanf(readline," LOOKUP_TABLE %*s %d",&nentries);
							if( filled != 1 ) throw BadFile;
							for(int i = 0; i < nentries; i++)
							{
								filled = fscanf(f," %*f %*f %*f %*f");
							}
							filled = fscanf(f,"\n");
							break;
						}
						else if( !strcmp(dataname,"NORMALS") || !strcmp(dataname,"VECTORS") || !strcmp(dataname,"TENSORS"))
						{
							char attrname[1024];
							char attrtype[1024];
							int nentries = 3;
							if( !strcmp(dataname,"TENSORS") ) nentries = 9;
							DataType t = DATA_BULK;
							filled = sscanf(readline,"%*s %s %s",attrname,attrtype);
Kirill Terekhov's avatar
Kirill Terekhov committed
1024
							if( verbosity > 0 ) printf("Reading attribute %s.\n",attrname);
Kirill Terekhov's avatar
Kirill Terekhov committed
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
							if( filled != 2 ) throw BadFile;
							for(unsigned int i = 0; i < strlen(attrtype); i++) attrtype[i] = tolower(attrtype[i]);
							if( !strcmp(attrtype,"bit") || !strcmp(attrtype,"unsigned_char") || !strcmp(attrtype,"char") ||
							   !strcmp(attrtype,"unsigned_short") || !strcmp(attrtype,"short") || !strcmp(attrtype,"unsigned_int") ||
							   !strcmp(attrtype,"int") || !strcmp(attrtype,"unsigned_long") || !strcmp(attrtype,"long"))
								t = DATA_INTEGER;
							else if( !strcmp(attrtype,"float") || !strcmp(attrtype,"double") )
								t = DATA_REAL;
							else
								throw BadFile;
							if( fgets(readline,2048,f) == NULL ) throw BadFile; //LOOK_UP TABLE
Kirill Terekhov's avatar
Kirill Terekhov committed
1036
							if( read_into == 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1037
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
1038
								Tag attr = CreateTag(attrname,t,read_into_cell,read_into_cell & ESET,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
1039
								unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newcells.size()/250),1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1040
1041
1042
1043
								for(unsigned int it = 0; it < newcells.size(); it++)
								{
									if( t == DATA_INTEGER )
									{
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1044
										if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1045
										{
Kirill Terekhov's avatar
Kirill Terekhov committed
1046
											Storage::integer_array attrdata = IntegerArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1047
1048
1049
1050
1051
1052
											for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										}
										else for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile;}
									}
									if( t == DATA_REAL )
									{
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1053
										if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1054
										{
Kirill Terekhov's avatar
Kirill Terekhov committed
1055
											Storage::real_array attrdata = RealArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1056
1057
1058
1059
											for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										}
										else for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile;}
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
1060
1061
1062
1063
1064
									if( verbosity > 1 && it%report_pace == 0)
									{
										printf("data %3.1f%%\r",(it*100.0)/(1.0*newcells.size()));
										fflush(stdout);
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
1065
1066
								}
								filled = fscanf(f,"\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
1067
								datatags.push_back(attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1068
							}
Kirill Terekhov's avatar
Kirill Terekhov committed
1069
							if( read_into == 1 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1070
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
1071
								Tag attr = CreateTag(attrname,t,NODE,NONE,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
1072
								unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newnodes.size()/250),1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1073
1074
1075
1076
								for(unsigned int it = 0; it < newnodes.size(); it++)
								{
									if( t == DATA_INTEGER )
									{
Kirill Terekhov's avatar
Kirill Terekhov committed
1077
										Storage::integer_array attrdata = IntegerArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1078
1079
1080
1081
										for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									}
									if( t == DATA_REAL )
									{
Kirill Terekhov's avatar
Kirill Terekhov committed
1082
										Storage::real_array attrdata = RealArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1083
1084
										for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
1085
1086
1087
1088
1089
									if( verbosity > 1 && it%report_pace == 0)
									{
										printf("data %3.1f%%\r",(it*100.0)/(1.0*newnodes.size()));
										fflush(stdout);
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
								}
								filled = fscanf(f,"\n");
							}
							break;
						}
						else if( !strcmp(dataname,"TEXTURE_COORDINATES") )
						{
							char attrname[1024];
							char attrtype[1024];
							int nentries;
							DataType t = DATA_BULK;
							filled = sscanf(readline," TEXTURE_COORDINATES %s %d %s",attrname,&nentries,attrtype);
							if( filled != 3 ) throw BadFile;
							for(unsigned int i = 0; i < strlen(attrtype); i++) attrtype[i] = tolower(attrtype[i]);
							if( !strcmp(attrtype,"bit") || !strcmp(attrtype,"unsigned_char") || !strcmp(attrtype,"char") ||
							   !strcmp(attrtype,"unsigned_short") || !strcmp(attrtype,"short") || !strcmp(attrtype,"unsigned_int") ||
							   !strcmp(attrtype,"int") || !strcmp(attrtype,"unsigned_long") || !strcmp(attrtype,"long"))
								t = DATA_INTEGER;
							else if( !strcmp(attrtype,"float") || !strcmp(attrtype,"double") )
								t = DATA_REAL;
							else
								throw BadFile;
							if( fgets(readline,2048,f) == NULL ) throw BadFile; //LOOK_UP TABLE
Kirill Terekhov's avatar
Kirill Terekhov committed
1113
							if( read_into == 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1114
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
1115
								Tag attr = CreateTag(attrname,t,read_into_cell,read_into_cell & ESET,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
1116
								unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newcells.size()/250),1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1117
1118
1119
1120
								for(unsigned int it = 0; it < newcells.size(); it++)
								{
									if( t == DATA_INTEGER )
									{
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1121
										if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1122
										{
Kirill Terekhov's avatar
Kirill Terekhov committed
1123
											Storage::integer_array attrdata = IntegerArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1124
1125
1126
1127
1128
											for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										} else for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile;}
									}
									if( t == DATA_REAL )
									{
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1129
										if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1130
										{
Kirill Terekhov's avatar
Kirill Terekhov committed
1131
											Storage::real_array attrdata = RealArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1132
1133
1134
											for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										} else for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile;}
									}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1135
									if( verbosity > 1 && it%report_pace == 0)
Kirill Terekhov's avatar
Kirill Terekhov committed
1136
1137
1138
1139
									{
										printf("data %3.1f%%\r",(it*100.0)/(1.0*newcells.size()));
										fflush(stdout);
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
1140
1141
								}
								filled = fscanf(f,"\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
1142
								datatags.push_back(attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1143
							}
Kirill Terekhov's avatar
Kirill Terekhov committed
1144
							if( read_into == 1 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1145
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
1146
								Tag attr = CreateTag(attrname,t,NODE,NONE,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
1147
								unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newnodes.size()/250),1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1148
1149
1150
1151
								for(unsigned int it = 0; it < newnodes.size(); it++)
								{
									if( t == DATA_INTEGER )
									{
Kirill Terekhov's avatar
Kirill Terekhov committed
1152
										Storage::integer_array attrdata = IntegerArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1153
1154
1155
1156
										for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									}
									if( t == DATA_REAL )
									{
Kirill Terekhov's avatar
Kirill Terekhov committed
1157
										Storage::real_array attrdata = RealArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1158
1159
										for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									}
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1160
									if( verbosity > 1 && it%report_pace == 0)
Kirill Terekhov's avatar
Kirill Terekhov committed
1161
1162
1163
1164
									{
										printf("data %3.1f%%\r",(it*100.0)/(1.0*newnodes.size()));
										fflush(stdout);
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
								}
								filled = fscanf(f,"\n");
							}
							break;
						}
						else if( !strcmp(dataname,"FIELD") )
						{
							int nfields = 0;
							filled = sscanf(readline," FIELD %*s %d",&nfields);
							if( filled != 1 ) throw BadFile;
							for(int i = 0; i < nfields; i++)
							{
								char attrname[1024];
								char attrtype[1024];
								unsigned int nentries, ntuples;
								DataType t = DATA_BULK;
								filled = fscanf(f," %s %u %u %s\n",attrname,&nentries,&ntuples,attrtype);
								if( filled != 4 ) throw BadFile;
								for(unsigned int i = 0; i < strlen(attrtype); i++) attrtype[i] = tolower(attrtype[i]);
								if( !strcmp(attrtype,"bit") || !strcmp(attrtype,"unsigned_char") || !strcmp(attrtype,"char") ||
								   !strcmp(attrtype,"unsigned_short") || !strcmp(attrtype,"short") || !strcmp(attrtype,"unsigned_int") ||
								   !strcmp(attrtype,"int") || !strcmp(attrtype,"unsigned_long") || !strcmp(attrtype,"long"))
									t = DATA_INTEGER;
								else if( !strcmp(attrtype,"float") || !strcmp(attrtype,"double") )
									t = DATA_REAL;
								else
									throw BadFile;
Kirill Terekhov's avatar
Kirill Terekhov committed
1192
								if( read_into == 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1193
								{
Kirill Terekhov's avatar
Kirill Terekhov committed
1194
									Tag attr  = CreateTag(attrname,t,read_into_cell,read_into_cell & ESET,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
1195
									if( ntuples != newcells.size() ) printf("number of tuples in field is not equal to number of cells\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
1196
									unsigned report_pace = std::max<unsigned>(ntuples/250,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1197
1198
1199
1200
									for(unsigned int it = 0; it < ntuples; it++)
									{
										if( t == DATA_INTEGER )
										{
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1201
											if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1202
											{
Kirill Terekhov's avatar
Kirill Terekhov committed
1203
												Storage::integer_array attrdata = IntegerArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1204
1205
1206
1207
1208
												for(unsigned int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
											} else for(unsigned int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile;}
										}
										if( t == DATA_REAL )
										{
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
1209
											if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1210
											{
Kirill Terekhov's avatar
Kirill Terekhov committed
1211
												Storage::real_array attrdata = RealArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1212
1213
1214
												for(unsigned int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
											} else for(unsigned int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile;}
										}
Kirill Terekhov's avatar
Kirill Terekhov committed
1215
1216
1217
1218
1219
										if( verbosity > 1 && it%report_pace == 0)
										{
											printf("data %3.1f%%\r",(it*100.0)/(1.0*ntuples));
											fflush(stdout);
										}
Kirill Terekhov's avatar
Kirill Terekhov committed
1220
1221
									}
									filled = fscanf(f,"\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
1222
									datatags.push_back(attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1223
								}
Kirill Terekhov's avatar
Kirill Terekhov committed
1224
								if( read_into == 1 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1225
								{
Kirill Terekhov's avatar
Kirill Terekhov committed
1226
1227
									Tag attr = CreateTag(attrname,t,NODE,NONE,nentries);
									if( ntuples != newnodes.size() ) printf("number of tuples in field is not equal to number of nodes\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
1228
									unsigned report_pace = std::max<unsigned>(ntuples/250,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1229
1230
1231
1232
									for(unsigned int it = 0; it < ntuples; it++)
									{
										if( t == DATA_INTEGER )
										{
Kirill Terekhov's avatar
Kirill Terekhov committed
1233
											Storage::integer_array attrdata = IntegerArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1234
1235
1236
1237
											for(unsigned int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										}
										if( t == DATA_REAL )
										{
Kirill Terekhov's avatar
Kirill Terekhov committed
1238
											Storage::real_array attrdata = RealArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1239
1240
											for(unsigned int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										}
Kirill Terekhov's avatar
Kirill Terekhov committed
1241
1242
1243
1244
1245
										if( verbosity > 1 && it%report_pace == 0)
										{
											printf("data %3.1f%%\r",(it*100.0)/(1.0*ntuples));
											fflush(stdout);
										}
Kirill Terekhov's avatar
Kirill Terekhov committed
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
									}
									filled = fscanf(f,"\n");
								}
							}
							break;
						}
						else
						{
							state = R_ATTRIBUTES;
						}
					}
					case R_ATTRIBUTES:
					{
						char datatype[1024];
						unsigned int npoints;
						filled = sscanf(readline," %s %u",datatype,&npoints);
						if( filled != 2 ) throw BadFile;
						if( !strcmp(datatype,"CELL_DATA") )
						{
							if( npoints != newcells.size() ) printf("number of attributes is not equal to number of cells\n");
							state = R_ATTRDATA;
Kirill Terekhov's avatar
Kirill Terekhov committed
1267
							read_into = 2;
Kirill Terekhov's avatar
Kirill Terekhov committed
1268
							if( verbosity > 0 ) printf("Reading data for cells.\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
1269
1270
1271
1272
1273
1274
							break;
						}
						else if( !strcmp(datatype,"POINT_DATA") )
						{
							if( npoints != newnodes.size() ) printf("number of attributes is not equal to number of nodes\n");
							state = R_ATTRDATA;
Kirill Terekhov's avatar
Kirill Terekhov committed
1275
							read_into = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
1276
							if( verbosity > 0 ) printf("Reading data for nodes.\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
1277
1278
1279
1280
1281
1282
1283
							break;
						}
						else
						{
							printf("%s\n",readline);