mesh_file.cpp 159 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
#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
12
#include "io.hpp"
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]);
306
							read_arrayf = xyz.empty()? NULL : &xyz[0];
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]);
316
							read_arrayf = xyz.empty() ? NULL : &xyz[0];
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]);
327
							read_arrayf = xyz.empty() ? NULL : &xyz[0];
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));
338
							read_arrayf = xyz.empty() ? NULL : &xyz[0];
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);
349
							read_arrayf = zcorn.empty() ? NULL : &zcorn[0];
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]);
359
							read_arrayf = tops.empty() ? NULL : &tops[0];
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]);
370
							read_arrayf = perm.empty() ? NULL : &perm[0];
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]);
380
							read_arrayf = perm.empty() ? NULL : &perm[0];
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]);
391
							read_arrayf = perm.empty() ? NULL : &perm[0];
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]);
402
							read_arrayf = poro.empty() ? NULL : &poro[0];
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
							{
								std::vector<HandleType>::iterator it = std::lower_bound(old_nodes.begin(),old_nodes.end(),node_xyz,CentroidComparator(this));
Kirill Terekhov's avatar
Kirill Terekhov committed
584 585 586 587 588 589
								if( it != old_nodes.end() ) 
								{
									Storage::real_array c = RealArrayDF(*it,CoordsTag());
									if( CentroidComparator(this).Compare(node_xyz,c.data()) == 0 )
										find = static_cast<int>(it - old_nodes.begin());
								}
Kirill Terekhov's avatar
Kirill Terekhov committed
590 591 592
							}
							if( find == -1 ) newnodes[numnode++] = CreateNode(node_xyz)->GetHandle();
							else newnodes[numnode++] = old_nodes[find];
Kirill Terekhov's avatar
Kirill Terekhov committed
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 629 630 631 632 633
							//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
634
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
635 636 637 638 639 640
						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
641
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
642 643 644 645
					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
646
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
647 648 649 650
				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
651 652
				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
653 654 655 656
				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
657
							HandleType verts[8];
Kirill Terekhov's avatar
Kirill Terekhov committed
658 659 660 661 662 663 664 665 666 667
							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
668
							Cell c = CreateCell(ElementArray<Node>(this,verts,verts+8),nvf,numnodes,6).first;
Kirill Terekhov's avatar
Kirill Terekhov committed
669 670 671 672 673 674 675 676 677 678 679 680
							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
681 682
				//std::vector<HandleType> block_nodes(dims[0]*dims[1]*dims[2]*8);
				//Storage::integer block_zcorn[8];
Kirill Terekhov's avatar
Kirill Terekhov committed
683 684 685 686 687 688
			}
		}
		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
689
			std::string::size_type attrl, ql, qr, l;
Kirill Terekhov's avatar
Kirill Terekhov committed
690 691 692 693
			//~ 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);
694
			l = File.find_last_of("/\\");
Kirill Terekhov's avatar
Kirill Terekhov committed
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
			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
712 713
						if( attrl == std::string::npos ) 
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
714
							std::cout << __FILE__ << ":" << __LINE__ << " version information not found in " << File << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
715 716
							throw BadFile;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
717
						attrl = str.find("dataType=\"vtkUnstructuredGrid\"",l);
Kirill Terekhov's avatar
Kirill Terekhov committed
718 719
						if( attrl == std::string::npos ) 
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
720
							std::cout << __FILE__ << ":" << __LINE__ << " data type information not found in " << File << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
721 722
							throw BadFile;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
723
						attrl = str.find("numberOfPieces");
Kirill Terekhov's avatar
Kirill Terekhov committed
724 725
						if( attrl == std::string::npos ) 
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
726
							std::cout << __FILE__ << ":" << __LINE__ << " number of files not found in " << File << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
727 728
							throw BadFile;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
729 730 731 732 733 734 735 736 737
						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
738 739
					else 
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
740
						std::cout << __FILE__ << ":" << __LINE__ << " unexpected xml tag " << tag << " in " << File << " expected File instead " << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
741 742
						throw BadFile;
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
743 744 745 746 747 748 749 750 751 752 753 754 755
				}
				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
756 757
					else 
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
758
						std::cout << __FILE__ << ":" << __LINE__ << " unexpected xml tag " << tag << " in " << File << " expected Piece instead " << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
759 760
						throw BadFile;
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
761 762 763
				}
				else if( state == 2 )
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
764 765
					if( tag != "/File" ) 
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
766
						std::cout << __FILE__ << ":" << __LINE__ << " unexpected xml tag " << tag << " in " << File << " expected /File instead " << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
767 768
						throw BadFile;
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
769 770 771 772 773 774 775 776 777 778 779 780
					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
781
			MarkerType unused_marker = CreateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
782
			bool grid_is_2d = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
783
			for(INMOST_DATA_ENUM_TYPE k = 0; k < file_options.size(); ++k)
Kirill Terekhov's avatar
Kirill Terekhov committed
784 785 786 787 788 789 790 791 792 793
			{
				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
794
			std::vector<HandleType> old_nodes(NumberOfNodes());
Kirill Terekhov's avatar
Kirill Terekhov committed
795 796
			{
				unsigned qq = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
797
				for(Mesh::iteratorNode it = BeginNode(); it != EndNode(); ++it)
Kirill Terekhov's avatar
Kirill Terekhov committed
798 799
					old_nodes[qq++] = *it;
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
800
			if( !old_nodes.empty() ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
801
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
802
				std::sort(old_nodes.begin(),old_nodes.end(),CentroidComparator(this));
Kirill Terekhov's avatar
Kirill Terekhov committed
803 804 805 806 807 808
				//for(std::vector<HandleType>::iterator it = old_nodes.begin(); it != old_nodes.end(); ++it)
				//{
				//	Storage::real_array c = RealArrayDF(*it,CoordsTag());
				//	REPORT_VAL("coord: ",c[0] << " " << c[1] << " " << c[2]);
				//}
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
809

Kirill Terekhov's avatar
Kirill Terekhov committed
810 811
			
			FILE * f = fopen(File.c_str(),"r");
Kirill Terekhov's avatar
Kirill Terekhov committed
812 813
			if( !f ) 
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
814
				std::cout << __FILE__ << ":" << __LINE__ << " cannot open " << File << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
815 816 817
				throw BadFileName;
			}
			std::vector<Tag> datatags;
Kirill Terekhov's avatar
Kirill Terekhov committed
818 819
			std::vector<HandleType> newnodes;
			std::vector<HandleType> newcells;
Kirill Terekhov's avatar
Kirill Terekhov committed
820 821 822 823 824 825
			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
826
			int read_into = NONE, read_into_cell = NONE;
Kirill Terekhov's avatar
Kirill Terekhov committed
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
			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
844 845
						if( filled == 0 ) 
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
846
							std::cout << __FILE__ << ":" << __LINE__ << " version information not found in " << File << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
847 848
							throw BadFile;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
						//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
865 866
						else 
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
867
							std::cout << __FILE__ << ":" << __LINE__ << " unexpected data type " << readline << " in " << File << " expected BINARY or ASCII instead " << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
868 869
							throw BadFile;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
870 871 872 873 874 875 876 877 878
						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
879 880
						if( filled != 1 ) 
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
881
							std::cout << __FILE__ << ":" << __LINE__ << " cannot read attribute name in " << File << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
882 883
							throw BadFile;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
884 885 886 887
						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
888
							if( verbosity > 0 ) printf("Reading attribute %s.\n",attrname);
Kirill Terekhov's avatar
Kirill Terekhov committed
889 890 891 892 893
							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
894 895 896 897 898 899 900 901
							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
902
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
903
								std::cout << __FILE__ << ":" << __LINE__ << " unexpected data type " << attrtype << " in " << File << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
904
								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
905
								throw BadFile;
Kirill Terekhov's avatar
Kirill Terekhov committed
906 907 908
							}
							if( fgets(readline,2048,f) == NULL ) 
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
909
								std::cout << __FILE__ << ":" << __LINE__ << " expected LOOKUP_TABLE in " << File << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
910 911 912
								throw BadFile; //LOOK_UP TABLE
							}
							if( read_into == 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
913
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
914
								Tag attr = CreateTag(attrname,t,read_into_cell,read_into_cell & ESET,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
915
								unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newcells.size()/250),1);
Kirill Terekhov's avatar
Kirill Terekhov committed
916 917
								for(unsigned int it = 0; it < newcells.size(); it++)
								{
Kirill Terekhov's avatar
Kirill Terekhov committed
918

Kirill Terekhov's avatar
Kirill Terekhov committed
919 920
									if( t == DATA_INTEGER )
									{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
921
										if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
922
										{
Kirill Terekhov's avatar
Kirill Terekhov committed
923
											Storage::integer_array attrdata = IntegerArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
924 925 926 927 928 929
											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
930
										if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
931
										{
Kirill Terekhov's avatar
Kirill Terekhov committed
932
											Storage::real_array attrdata = RealArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
933 934 935 936
											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
937 938 939 940 941
									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
942 943
								}
								filled = fscanf(f,"\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
944
								datatags.push_back(attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
945
							}
Kirill Terekhov's avatar
Kirill Terekhov committed
946
							if( read_into == 1 )
Kirill Terekhov's avatar
Kirill Terekhov committed
947
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
948
								Tag attr = CreateTag(attrname,t,NODE,NONE,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
949
								unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newnodes.size())/250,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
950 951 952 953
								for(unsigned int it = 0; it < newnodes.size(); it++)
								{
									if( t == DATA_INTEGER )
									{
Kirill Terekhov's avatar
Kirill Terekhov committed
954
										Storage::integer_array attrdata = IntegerArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
955 956 957 958
										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
959
										Storage::real_array attrdata = RealArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
960 961
										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
962 963 964 965 966
									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
967 968 969 970 971 972 973 974 975
								}
								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
976
							if( verbosity > 0 ) printf("Reading attribute %s.\n",attrname);
Kirill Terekhov's avatar
Kirill Terekhov committed
977
							if( read_into == 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
978
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
979
								Tag attr = CreateTag(attrname,DATA_REAL,read_into_cell,read_into_cell & ESET,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
980
								unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newcells.size()/250),1);
Kirill Terekhov's avatar
Kirill Terekhov committed
981 982
								for(unsigned int it = 0; it < newcells.size(); it++)
								{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
983
									if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
984
									{
Kirill Terekhov's avatar
Kirill Terekhov committed
985
										Storage::real_array attrdata = RealArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
986 987 988
										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
989 990 991 992 993
									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
994 995
								}
								filled = fscanf(f,"\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
996
								datatags.push_back(attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
997
							}
Kirill Terekhov's avatar
Kirill Terekhov committed
998
							if( read_into == 1 )
Kirill Terekhov's avatar
Kirill Terekhov committed
999
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
1000
								Tag attr = CreateTag(attrname,DATA_REAL,NODE,NONE,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
1001
								unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newnodes.size()/250),1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1002 1003
								for(unsigned int it = 0; it < newnodes.size(); it++)
								{
Kirill Terekhov's avatar
Kirill Terekhov committed
1004
									Storage::real_array attrdata = RealArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1005
									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
1006 1007 1008 1009 1010
									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
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
								}
								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
1036
							if( verbosity > 0 ) printf("Reading attribute %s.\n",attrname);
Kirill Terekhov's avatar
Kirill Terekhov committed
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
							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
1048
							if( read_into == 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1049
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
1050
								Tag attr = CreateTag(attrname,t,read_into_cell,read_into_cell & ESET,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
1051
								unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newcells.size()/250),1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1052 1053 1054 1055
								for(unsigned int it = 0; it < newcells.size(); it++)
								{
									if( t == DATA_INTEGER )
									{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1056
										if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1057
										{
Kirill Terekhov's avatar
Kirill Terekhov committed
1058
											Storage::integer_array attrdata = IntegerArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1059 1060 1061 1062 1063 1064
											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
1065
										if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1066
										{
Kirill Terekhov's avatar
Kirill Terekhov committed
1067
											Storage::real_array attrdata = RealArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1068 1069 1070 1071
											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
1072 1073 1074 1075 1076
									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
1077 1078
								}
								filled = fscanf(f,"\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
1079
								datatags.push_back(attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1080
							}
Kirill Terekhov's avatar
Kirill Terekhov committed
1081
							if( read_into == 1 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1082
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
1083
								Tag attr = CreateTag(attrname,t,NODE,NONE,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
1084
								unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newnodes.size()/250),1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1085 1086 1087 1088
								for(unsigned int it = 0; it < newnodes.size(); it++)
								{
									if( t == DATA_INTEGER )
									{
Kirill Terekhov's avatar
Kirill Terekhov committed
1089
										Storage::integer_array attrdata = IntegerArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1090 1091 1092 1093
										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
1094
										Storage::real_array attrdata = RealArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1095 1096
										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
1097 1098 1099 1100 1101
									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
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
								}
								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
1125
							if( read_into == 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1126
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
1127
								Tag attr = CreateTag(attrname,t,read_into_cell,read_into_cell & ESET,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
1128
								unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newcells.size()/250),1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1129 1130 1131 1132
								for(unsigned int it = 0; it < newcells.size(); it++)
								{
									if( t == DATA_INTEGER )
									{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1133
										if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1134
										{
Kirill Terekhov's avatar
Kirill Terekhov committed
1135
											Storage::integer_array attrdata = IntegerArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1136 1137 1138 1139 1140
											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
1141
										if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1142
										{
Kirill Terekhov's avatar
Kirill Terekhov committed
1143
											Storage::real_array attrdata = RealArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1144 1145 1146
											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
1147
									if( verbosity > 1 && it%report_pace == 0)
Kirill Terekhov's avatar
Kirill Terekhov committed
1148 1149 1150 1151
									{
										printf("data %3.1f%%\r",(it*100.0)/(1.0*newcells.size()));
										fflush(stdout);
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
1152 1153
								}
								filled = fscanf(f,"\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
1154
								datatags.push_back(attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1155
							}
Kirill Terekhov's avatar
Kirill Terekhov committed
1156
							if( read_into == 1 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1157
							{
Kirill Terekhov's avatar
Kirill Terekhov committed
1158
								Tag attr = CreateTag(attrname,t,NODE,NONE,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
1159
								unsigned report_pace = std::max<unsigned>(static_cast<unsigned>(newnodes.size()/250),1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1160 1161 1162 1163
								for(unsigned int it = 0; it < newnodes.size(); it++)
								{
									if( t == DATA_INTEGER )
									{
Kirill Terekhov's avatar
Kirill Terekhov committed
1164
										Storage::integer_array attrdata = IntegerArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1165 1166 1167 1168
										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
1169
										Storage::real_array attrdata = RealArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1170 1171
										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
1172
									if( verbosity > 1 && it%report_pace == 0)
Kirill Terekhov's avatar
Kirill Terekhov committed
1173 1174 1175 1176
									{
										printf("data %3.1f%%\r",(it*100.0)/(1.0*newnodes.size()));
										fflush(stdout);
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
								}
								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
1204
								if( read_into == 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1205
								{
Kirill Terekhov's avatar
Kirill Terekhov committed
1206
									Tag attr  = CreateTag(attrname,t,read_into_cell,read_into_cell & ESET,nentries);
Kirill Terekhov's avatar
Kirill Terekhov committed
1207
									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
1208
									unsigned report_pace = std::max<unsigned>(ntuples/250,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1209 1210 1211 1212
									for(unsigned int it = 0; it < ntuples; it++)
									{
										if( t == DATA_INTEGER )
										{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1213
											if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1214
											{
Kirill Terekhov's avatar
Kirill Terekhov committed
1215
												Storage::integer_array attrdata = IntegerArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1216 1217 1218 1219 1220
												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
1221
											if( newcells[it] != InvalidHandle() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1222
											{
Kirill Terekhov's avatar
Kirill Terekhov committed
1223
												Storage::real_array attrdata = RealArray(newcells[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1224 1225 1226
												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
1227 1228 1229 1230 1231
										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
1232 1233
									}
									filled = fscanf(f,"\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
1234
									datatags.push_back(attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1235
								}
Kirill Terekhov's avatar
Kirill Terekhov committed
1236
								if( read_into == 1 )
Kirill Terekhov's avatar
Kirill Terekhov committed
1237
								{
Kirill Terekhov's avatar
Kirill Terekhov committed
1238 1239
									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
1240
									unsigned report_pace = std::max<unsigned>(ntuples/250,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1241 1242 1243 1244
									for(unsigned int it = 0; it < ntuples; it++)
									{
										if( t == DATA_INTEGER )
										{
Kirill Terekhov's avatar
Kirill Terekhov committed
1245
											Storage::integer_array attrdata = IntegerArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1246 1247 1248 1249
											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
1250
											Storage::real_array attrdata = RealArray(newnodes[it],attr);
Kirill Terekhov's avatar
Kirill Terekhov committed
1251 1252
											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
1253 1254 1255 1256 1257
										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
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
									}
									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
1279
							read_into = 2;
Kirill Terekhov's avatar
Kirill Terekhov committed
1280
							if( verbosity > 0 ) printf("Reading data for cells.\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
1281 1282 1283 1284 1285 1286
							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
1287
							read_into = 1;
Kirill Terekhov's avatar
Kirill Terekhov committed
1288
							if( verbosity > 0 ) printf("Reading data for nodes.\n");
Kirill Terekhov's avatar
Kirill Terekhov committed
1289 1290 1291 1292 1293 1294 1295
							break;
						}
						else
						{
							printf("%s\n",readline);
							printf("Unknown type of attributes\n");
							state = R_WAITDATA;
Kirill Terekhov's avatar
Kirill Terekhov committed
1296
							read_into = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
						}
					}
					case R_WAITDATA:
					{
						char check[1024];
						filled = sscanf(readline," DATASET %s",check);
						if( filled != 1 ) throw BadFile;
						if( !strcmp(check,"STRUCTURED_POINTS") )
							state = R_SPOINTS;
						else if( !strcmp(check,"STRUCTURED_GRID" ) )
							state = R_SGRID;
						else if( !strcmp(check,"UNSTRUCTURED_GRID") )
							state = R_UGRID;
						else if( !strcmp(check,"POLYDATA") )
							state = R_POLYDATA;
						else if( !strcmp(check,"RECTLINEAR_GRID") )
							state = R_RGRID;
						else if( !strcmp(check,"FIELD") )
							state = R_FIELD;
						else throw BadFile;
						break;
					}
					case R_UGRID:
					{
						INMOST_DATA_REAL_TYPE coords[3] = {0,0,0};
						int npoints = 0, i = 0, ncells = 0, ncells2 = 0, nints = 0;
						char datatype[1024];
						filled = sscanf(readline,"%*s %d %s",&npoints,datatype);
Kirill Terekhov's avatar
Kirill Terekhov committed
1325
						newnodes.resize(npoints);
Kirill Terekhov's avatar
Kirill Terekhov committed
1326
						//printf("number of nodes: %d\n",npoints);
Kirill Terekhov's avatar
Kirill Terekhov committed
1327
						if( verbosity > 0 ) printf("Reading %d nodes.\n",npoints);
Kirill Terekhov's avatar
Kirill Terekhov committed
1328
						if( filled != 2 ) throw BadFile;
Kirill Terekhov's avatar
Kirill Terekhov committed
1329
						unsigned report_pace = std::max<unsigned>(npoints/250,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
						if( binary )
						{
							i = 0;
							while(i != npoints)
							{
								if( !strcmp(datatype,"double") ) ReadCoords<double>(f,coords);
								else if( !strcmp(datatype,"float") ) ReadCoords<float>(f,coords);
								else if( !strcmp(datatype,"unsigned char") ) ReadCoords<unsigned char>(f,coords);
								else if( !strcmp(datatype,"char") ) ReadCoords<char>(f,coords);
								else if( !strcmp(datatype,"unsigned short") ) ReadCoords<unsigned short>(f,coords);
								else if( !strcmp(datatype,"short") ) ReadCoords<short>(f,coords);
								else if( !strcmp(datatype,"unsigned int") ) ReadCoords<unsigned int>(f,coords);
								else if( !strcmp(datatype,"int") ) ReadCoords<int>(f,coords);
								else if( !strcmp(datatype,"unsigned long") ) ReadCoords<unsigned long>(f,coords);
								else if( !strcmp(datatype,"long") ) ReadCoords<long>(f,coords);
								else throw BadFile;
								int find = -1;
								if( !old_nodes.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1348 1349
								{
									std::vector<HandleType>::iterator it = std::lower_bound(old_nodes.begin(),old_nodes.end(),coords,CentroidComparator(this));
Kirill Terekhov's avatar
Kirill Terekhov committed
1350 1351 1352 1353 1354 1355
									if( it != old_nodes.end() ) 
									{
										Storage::real_array c = RealArrayDF(*it,CoordsTag());
										if( CentroidComparator(this).Compare(c.data(),coords) == 0 )
											find = static_cast<int>(it - old_nodes.begin());
									}
Kirill Terekhov's avatar
Kirill Terekhov committed
1356 1357
								}
								if( find == -1 ) 
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1358
								{
Kirill Terekhov's avatar
Kirill Terekhov committed
1359
									newnodes[i] = CreateNode(coords)->GetHandle();
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1360 1361
									SetMarker(newnodes[i],unused_marker);
								}
Kirill Terekhov's avatar
Kirill Terekhov committed
1362 1363
								else 
									newnodes[i] = old_nodes[find];
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1364
								
Kirill Terekhov's avatar
Kirill Terekhov committed
1365 1366 1367 1368 1369
								if( verbosity > 1 && i%report_pace == 0)
								{
									printf("nodes %3.1f%%\r",(i*100.0)/(1.0*npoints));
									fflush(stdout);
								}
Kirill Terekhov's avatar
Kirill Terekhov committed
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
								i++;
							}
						}
						else
						{
							i = 0;
							while(i != npoints)
							{
								double c[3];
								filled = fscanf(f," %lg %lg %lg",&c[0],&c[1],&c[2]);
								coords[0] = c[0];
								coords[1] = c[1];
								coords[2] = c[2];
								if( filled != 3 ) throw BadFile;
								int find = -1;
								if( !old_nodes.empty() )
Kirill Terekhov's avatar
Kirill Terekhov committed
1386
								{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1387
									REPORT_VAL("look up",coords[0] << " " << coords[1] << " " << coords[2]);