summaryrefslogtreecommitdiffstats
path: root/conversion/dem.cc
blob: 09e801e1c99e94d7990ca845bdeaaeca2bed81cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * SOURCE: dem.hh
 *
 * MODULE:  conversion
 *
 * CLASSES: r_Conv_DEM
 *
 * PURPOSE:
 * Provides interface to convert data from/to Array format to/from DEM format.
 *
 * COMMENTS:
 * For further support send a mail to comanl@yahoo.com
 * - convertTo() writes a temp file; this should be omitted for performance reasons
*/

static const char rcsid[] = "@(#)conversion,r_Conv_DEM: $Id: dem.cc,v 1.10 2005/09/08 12:59:26 rasdev Exp $"; 
 
#include "conversion/dem.hh"

#include <float.h>
#include <string>
#include <cstring>
#include <strstream>
#include <sstream>
#include <algorithm>

using std::istringstream;
using std::istrstream;
using std::string;
using namespace std;

#include "raslib/rminit.hh"
#include "raslib/parseparams.hh"
#include "raslib/primitivetype.hh"


const r_Dimension r_Conv_DEM::srcIntervDim=1;
const r_Dimension r_Conv_DEM::destIntervDim=2;
const r_ULong     r_Conv_DEM::paramMin=6;
const char*       r_Conv_DEM::paramSep=",";
const char*       r_Conv_DEM::paramEq="=";
const char*       r_Conv_DEM::paramFlipX="flipx";
const char*       r_Conv_DEM::paramFlipY="flipy";
const char*       r_Conv_DEM::paramStartX="startx";
const char*       r_Conv_DEM::paramEndX="endx";
const char*       r_Conv_DEM::paramResX="resx";
const char*       r_Conv_DEM::paramStartY="starty";
const char*       r_Conv_DEM::paramEndY="endy";
const char*       r_Conv_DEM::paramResY="resy";

const r_Double r_Conv_DEM::NULL_DB = 0.;
const r_Double r_Conv_DEM::ZERO_DB = FLT_MIN;
const r_Double r_Conv_DEM::ZERO_DEM = 0.;

r_Conv_DEM::~r_Conv_DEM( void )
{
	//nothing to care for
}

void r_Conv_DEM::initGeoBBox( r_GeoBBox& cBBox )
{
	//flipy is selected by default
	cBBox.flipy = 1;
  
	//flipx is not selected by default
	cBBox.flipx = 0;

	//geo information are initialized by default to DBL_MAX
	// FIXME: better defaults res=1, min=-MAX?
	cBBox.startx = DBL_MAX;  
	cBBox.endx   = DBL_MAX;
	cBBox.resx   = DBL_MAX;
	cBBox.starty = DBL_MAX;  
	cBBox.endy   = DBL_MAX;
	cBBox.resy   = DBL_MAX;  
}

r_Conv_DEM::r_Conv_DEM(const char* source, const r_Minterval& lengthordomain, const r_Type* tp) throw(r_Error)
	:	r_Convertor(source, lengthordomain, tp, true)
{
	initGeoBBox(collBBox);
}

r_Conv_DEM::r_Conv_DEM(const char* source, const r_Minterval& lengthordomain, int tp) throw(r_Error)
	:	r_Convertor(source, lengthordomain, tp)
{
	initGeoBBox(collBBox);
}
	
bool 
r_Conv_DEM::decodeOptions(const char* options,
			 r_GeoBBox& cBBox) throw()
{
	RMInit::logOut << "r_Conv_DEM::decodeOptions(" << (options?options:"NULL") << ")" << endl;
  
	r_Parse_Params parseParams;
  
	initGeoBBox(cBBox);

	parseParams.add(paramFlipX,  &cBBox.flipx,  r_Parse_Params::param_type_int);
	parseParams.add(paramFlipY,  &cBBox.flipy,  r_Parse_Params::param_type_int);
	parseParams.add(paramStartX, &cBBox.startx, r_Parse_Params::param_type_double);
	parseParams.add(paramEndX,   &cBBox.endx,   r_Parse_Params::param_type_double);
	parseParams.add(paramResX,   &cBBox.resx,   r_Parse_Params::param_type_double);  
	parseParams.add(paramStartY, &cBBox.starty, r_Parse_Params::param_type_double);
	parseParams.add(paramEndY,   &cBBox.endy,   r_Parse_Params::param_type_double);  
	parseParams.add(paramResY,   &cBBox.resy,   r_Parse_Params::param_type_double);
   
	//process options
	r_Long processRet=parseParams.process(options);
	if(processRet < paramMin)
	{
		RMInit::logOut << "r_Conv_DEM::decodeOptions(...) Error: Some required options are missing!" << endl;
		return false;	   
	}

	//check if start,res,end are present
	if(cBBox.startx == DBL_MAX)
	{
		RMInit::logOut << "r_Conv_DEM::decodeOptions(...) Error: startx is not present!" << endl;
		return false;	   
	}	
	
	if(cBBox.starty == DBL_MAX)
	{
		RMInit::logOut << "r_Conv_DEM::decodeOptions(...) Error: starty is not present!" << endl;
		return false;	   
	}	  
  
	if(cBBox.endx == DBL_MAX)
	{
		RMInit::logOut << "r_Conv_DEM::decodeOptions(...) Error: endx is not present!" << endl;
		return false;	   
	}	
  
	if(cBBox.endy == DBL_MAX)
	{
		RMInit::logOut << "r_Conv_DEM::decodeOptions(...) Error: endy is not present!" << endl;
		return false;
	}	  
  
	if(cBBox.resx == DBL_MAX)
	{
		RMInit::logOut << "r_Conv_DEM::decodeOptions(...) Error: resx is not present!" << endl;
		return false;
	}	
  
	if(cBBox.resy == DBL_MAX)
	{
		RMInit::logOut << "r_Conv_DEM::decodeOptions(...) Error: resy is not present!" << endl;
		return false;
	}	  
  

	//check res 
	if(!cBBox.resx)
	{
		RMInit::logOut << "r_Conv_DEM::decodeOptions(...) Error: resx is zero!" << endl;
		return false;
	}	
  
	if(!cBBox.resy)
	{
		RMInit::logOut << "r_Conv_DEM::decodeOptions(...) Error: resy is zero!" << endl;
		return false;
	}
	
	//check start >= end
	if(cBBox.startx >= cBBox.endx )
	{
		RMInit::logOut << "r_Conv_DEM::decodeOptions(...) Error: startx >= endx!" << endl;
		return false;
	}	
  
	if(cBBox.starty >= cBBox.endy)
	{
		RMInit::logOut << "r_Conv_DEM::decodeOptions(...) Erorr: starty >= endy!" << endl;
		return false;
	}   
  
	//show parsed options
	RMInit::logOut.setf(std::ios::fixed);
	RMInit::logOut  << "r_Conv_DEM::decodeOptions(...) parsed options:" << endl
		<< " " << paramFlipX  << paramEq << cBBox.flipx 
		<< " " << paramFlipY  << paramEq << cBBox.flipy  << endl  
	  	<< " " << paramStartX << paramEq << cBBox.startx  
	  	<< " " << paramEndX   << paramEq << cBBox.endx 
	  	<< " " << paramResX   << paramEq << cBBox.resx   << endl
		<< " " << paramStartY << paramEq << cBBox.starty  
		<< " " << paramEndY   << paramEq << cBBox.endy 
		<< " " << paramResY   << paramEq << cBBox.resy   <<  endl;	
	return true;
}

string 
r_Conv_DEM::encodeOptions(const r_GeoBBox& cBBox) throw()
{
	std::ostringstream os;
   
	os.str("");
	os.setf(std::ios::fixed);
	os	<< paramFlipX  << paramEq << cBBox.flipx 
		<< paramSep << paramFlipY  << paramEq << cBBox.flipy
		<< paramSep << paramStartX << paramEq << cBBox.startx  
		<< paramSep << paramEndX   << paramEq << cBBox.endx 
		<< paramSep << paramResX   << paramEq << cBBox.resx
		<< paramSep << paramStartY << paramEq << cBBox.starty  
		<< paramSep << paramEndY   << paramEq << cBBox.endy 
		<< paramSep << paramResY   << paramEq << cBBox.resy;
   
	RMInit::logOut << "r_Conv_DEM::encodeOptions(" << os.str() << ")" << endl;   
   
	return os.str();
}	

void 
r_Conv_DEM::checkLimits() throw(r_Error)
{
	//show processed data
	RMInit::logOut  << "r_Conv_DEM::checkLimits() processed data:" << endl
		<< " minx=" << min.x  << " miny=" << min.y << " minh=" << min.h << endl
		<< " maxx=" << max.x  << " maxy=" << max.y << " maxh=" << max.h <<  endl;	
	// printf( "r_Conv_DEM::checkLimits() processed data: minx=%8G, miny=%8G, minh=%8G, maxx=%8G, maxy=%8G, maxh=%8G\n", min.x, min.y, min.h, max.x, max.y, max.h );	

	if(collBBox.startx > min.x)
	{
		RMInit::logOut << "r_Conv_DEM::checkLimits() startx( " << collBBox.startx << ") > min.x (" << min.x << " )!" << endl;
		throw r_Error();	     
	}
	if(collBBox.endx < max.x)
	{
		RMInit::logOut << "r_Conv_DEM::checkLimits() endx( " << collBBox.endx << ") < max.x (" << max.x << " )!" << endl;
		throw r_Error();	     
	}  

	if(collBBox.starty > min.y)
	{
		RMInit::logOut << "r_Conv_DEM::checkLimits() starty( " << collBBox.starty << ")  > min.y (" << min.y << " )!" << endl;
		throw r_Error();	     
	}  

	if(collBBox.endy < max.y)
	{
		RMInit::logOut << "r_Conv_DEM::checkLimits() endy( " << collBBox.endy << ") < max.y (" << max.y << " )!" << endl;
		throw r_Error();	   
	}
}

void 
r_Conv_DEM::readFromSrcStream() throw(r_Error)
{
	istrstream iFile(desc.src, desc.srcInterv[0].get_extent());
	string currStrRow;
	r_Long rowNo=0;
	r_Double noResx, noResy;
	DEMRow currRow, prevRow;
 
	min.x=min.y=min.h=DBL_MAX;
	max.x=max.y=max.h=-DBL_MAX;
	demRows.clear();
 
	//process the lines
	while(!iFile.eof())
	{
		getline(iFile, currStrRow);
		rowNo++;
		if(currStrRow.empty())
		{
			// TALK( "r_Conv_DEM::readFromSrcStream() skipping empty line " << rowNo );  
			continue;
		}
		else
		{
			// have an input stream for analysing the current line
			// (declaring this variable here allows to have a fresh one;
			// had a reentrance problem with followup lines -- PB 2005-sep-08)
			istringstream icurrRow;
			icurrRow.str(currStrRow);  

			//decode x
			icurrRow >> currRow.x;
			if(!icurrRow)
			{
				RMInit::logOut << "Error in r_Conv_DEM::readFromSrcStream():: unable to decode x in line " << rowNo << ", skipping line: " << currStrRow << endl;
				continue;
			}   
   
			//decode y   
			icurrRow >> currRow.y;
			if(!icurrRow)
			{
				RMInit::logOut << "Error in r_Conv_DEM::readFromSrcStream():: unable to decode y in line " << rowNo << ", skipping line: " << currStrRow << endl;
				continue;
			}      

			//decode h
			icurrRow >> currRow.h;
			if(!icurrRow)
			{
				RMInit::logOut << "Error in r_Conv_DEM::readFromSrcStream():: unable to decode h in line " << rowNo << ", skipping line: " << currStrRow << endl;
				continue;
			}

			//update to support NULL value: 0. (real value) goes in FLT_MIN(db value)
			//because 0.(db value) represent NULL(real value). When we do export we skip NULL values.    
			if(currRow.h == ZERO_DEM)
				currRow.h=ZERO_DB;
    
			//FIXME we ignore this check, because it may happen to have a incomplet dem
			/*
			//check if we have resx, resy
			noResx=currRow.x/collBBox.resx;
			if((currRow.x - noResx*collBBox.resx) > 0.)
			{
				RMInit::logOut << "r_Conv_DEM::readFromSrcStream() resolution for x on line " << 
					rowNo << " is not " << collBBox.resx << " !" << endl;   
				throw r_Error();
			}
			noResy=currRow.y/collBBox.resy;   
			if((currRow.y - noResy*collBBox.resy) > 0.)
			{
				RMInit::logOut << "r_Conv_DEM::readFromSrcStream() resolution for y on line " << 
					rowNo << " is not " << collBBox.resy << " !" << endl;       
				throw r_Error();
			} 
			*/   

			//compute min, max for x,y,z
			min.x=std::min<r_Double>(min.x, currRow.x);
			min.y=std::min<r_Double>(min.y, currRow.y);   
			min.h=std::min<r_Double>(min.h, currRow.h);      
			max.x=std::max<r_Double>(max.x, currRow.x);
			max.y=std::max<r_Double>(max.y, currRow.y);   
			max.h=std::max<r_Double>(max.h, currRow.h);         
	
			//store currRow
			demRows.push_back(currRow);
		}//end if(currStrRow.empty())

	}//end reading src stream

	if(demRows.empty())
	{
		// TALK( "r_Conv_DEM::readFromSrcStream() desc.src stream empty." ); 
		throw r_Error(); 
	} 
	
	// std::cout << "r_Conv_DEM::readFromSrcStream(): x=" << min.x << ":" << max.x << ", y=" << min.y << ":" << max.y << endl;

	//check limits
	checkLimits();
}


void 
r_Conv_DEM::readToSrcStream() throw(r_Error)
{
	r_Long x=0, y=0;
	r_Long xlow=0, ylow=0;
	r_Long xhigh=0, yhigh=0;
	DEMRow currRow;
	r_Bytes typeSize=0;
	r_Long offset=0;
	char* buffer=NULL;
 
	//initialize
	xlow=desc.srcInterv[0].low();
	ylow=desc.srcInterv[1].low();
 
	xhigh=desc.srcInterv[0].high();
	yhigh=desc.srcInterv[1].high();

	//compute min & max
	if (collBBox.flipx)
	{
		min.x=collBBox.endx - xhigh*collBBox.resx;
		max.x=collBBox.endx - xlow*collBBox.resx;   
	}
	else
	{
		min.x=collBBox.startx + xlow*collBBox.resx;
		max.x=collBBox.startx + xhigh*collBBox.resx; 
	}

	if(collBBox.flipy)
	{
		min.y=collBBox.endy - yhigh*collBBox.resy;
		max.y=collBBox.endy - ylow*collBBox.resy; 
	} 
	else
	{
		min.y=collBBox.starty + ylow*collBBox.resy;
		max.y=collBBox.starty + yhigh*collBBox.resy; 
	}
	
	min.h=DBL_MAX;
	max.h=-DBL_MAX;
	
	//check limits
	checkLimits(); 
	
	//prepare container
	demRows.clear();
	typeSize=((r_Primitive_Type*)desc.srcType)->size();
	buffer=new char[typeSize];
	if(!buffer)
	{
		RMInit::logOut << "r_Conv_DEM::readToSrcStream() unable to claim memory !" << endl; 
		throw  r_Ememory_allocation();  
	} 
	for(y=ylow; y<=yhigh; y++)
	{
		if(collBBox.flipy)
			currRow.y=collBBox.endy - y*collBBox.resy;
		else
			currRow.y=collBBox.starty + y*collBBox.resy; 
	
		for(x=xlow; x<=xhigh; x++)
		{
			if(collBBox.flipx)
				currRow.x=collBBox.endx - x*collBBox.resx;
			else
				currRow.x=collBBox.startx + x*collBBox.resx;
			offset=desc.srcInterv.cell_offset(r_Point(x,y))*typeSize;
			memcpy(buffer, &desc.src[offset], typeSize);

			switch(desc.srcType->type_id())
			{
			case r_Type::BOOL:
				currRow.h=((r_Primitive_Type*)desc.srcType)->get_boolean(buffer);    
				break;    
			case r_Type::CHAR:
				currRow.h=((r_Primitive_Type*)desc.srcType)->get_char(buffer);
				break;
			case r_Type::OCTET:
				currRow.h=((r_Primitive_Type*)desc.srcType)->get_octet(buffer);
				break;
			case r_Type::SHORT:
				currRow.h=((r_Primitive_Type*)desc.srcType)->get_short(buffer);
				break;    
			case r_Type::USHORT: 
				currRow.h=((r_Primitive_Type*)desc.srcType)->get_ushort(buffer);
				break;
			case r_Type::LONG:
				currRow.h=((r_Primitive_Type*)desc.srcType)->get_long(buffer);
				break;
			case r_Type::ULONG:
				currRow.h=((r_Primitive_Type*)desc.srcType)->get_ulong(buffer);
				break;            
			case r_Type::FLOAT:
				currRow.h=((r_Primitive_Type*)desc.srcType)->get_float(buffer);
				break;
			case r_Type::DOUBLE: 
				currRow.h=((r_Primitive_Type*)desc.srcType)->get_double(buffer);
				break;
			default:
				//write message to log
				RMInit::logOut << "r_Conv_DEM::readToSrcStream() srcType (" << desc.srcType->type_id() <<  ") unsupported !" << endl; 
				//clean up
				if(buffer)
				{
					delete[] buffer;
					buffer=NULL;
				}
				//report error
				throw r_Error();     
				break;
			}
			min.h=std::min<r_Double>(min.h, currRow.h);
			max.h=std::max<r_Double>(max.h, currRow.h);   
			demRows.push_back(currRow);
		}
	} 
	
	//clean up
	if(buffer)
	{
		delete[] buffer;
		buffer=NULL;
	} 
	
	if(demRows.empty())
	{
		RMInit::logOut << "r_Conv_DEM::readToSrcStream() src stream is empty !" << endl; 
		throw r_Error(); 
	} 

	RMInit::logOut << "r_Conv_DEM::readToSrcStream() processed interval [" << xlow << ":" << xhigh << "," << ylow << ":" << yhigh << "]" << endl;
}


void
r_Conv_DEM::writeFromDestStream() throw(r_Error)
{
	DEMRowVec::const_iterator iter, iterEnd;
	r_Long xdim, ydim, offset;
	r_Point currPt(destIntervDim);
	r_Bytes typeSize=0;


	//FIXME here we should modify for other type support
	if(desc.destType->type_id() != r_Type::DOUBLE)
	{
		RMInit::logOut << "r_Conv_DEM::writeFromDestStream() destType (" << desc.destType->type_id() 
			<< ") is not " << r_Type::DOUBLE << " !" << endl; 
		throw r_Error();  
	}
	
	xdim=desc.destInterv[0].get_extent();
	ydim=desc.destInterv[1].get_extent();
	iter=demRows.begin();
	iterEnd=demRows.end();
	typeSize=((r_Primitive_Type*)desc.destType)->size();

	//FIXME correction for strange effect of r_Long cast with 1e-6
	while(iter != iterEnd)
	{
		if(collBBox.flipx)
			currPt[0]=(collBBox.endx - iter->x)/collBBox.resx + 1e-6;
		else
			currPt[0]=(iter->x - collBBox.startx)/collBBox.resx + 1e-6;   
		if(collBBox.flipy)
			currPt[1]=(collBBox.endy - iter->y)/collBBox.resy + 1e-6;
		else
			currPt[1]=(iter->y - collBBox.starty)/collBBox.resy + 1e-6;
		((r_Primitive_Type*)desc.destType)->set_double(&desc.dest[desc.destInterv.cell_offset(currPt)*typeSize], iter->h);
		++iter;
	}

	RMInit::logOut << "r_Conv_DEM::writeFromDestStream() processed " << xdim << " x " << ydim << " elements." << endl;
}

void
r_Conv_DEM::writeToDestStream(ofstream& oFile) throw(r_Error)
{
	DEMRowVec::const_iterator iter, iterEnd;
	r_Double currH;

	if(!oFile.is_open())
	{
		RMInit::logOut << "r_Conv_DEM::writeToDestStream() oFile is not opened !" << endl; 
		throw r_Error();  
	}
	oFile.setf(std::ios::fixed);
	
	iter=demRows.begin();
	iterEnd=demRows.end();
	while(iter != iterEnd)
	{
		//update to support NULL value: 0. (real value) goes in FLT_MIN(db value)
		//because 0.(db value) represent NULL(real value). When we do export we skip NULL values. 
		currH = iter->h;
		if(currH != NULL_DB)
		{
			//FIXME we have to implement different here when we change server scale algorithm
			if(currH == ZERO_DB)
				currH = ZERO_DEM;
			oFile << iter->x << "\t" << iter->y << "\t" << currH << endl;
		}
		++iter;
	}
}

r_convDesc&
r_Conv_DEM::convertFrom(const char* options) throw (r_Error)
{
	bool hasSrcType=true;
	
        RMInit::logOut << "r_Conv_DEM::convertFrom(" << (options?options:"NULL") << ")" << endl;	
        
      	if(!desc.srcType)
      	{
		desc.srcType=get_external_type(desc.baseType);	
		hasSrcType=false;
	}   
	
	try
	{
		RMInit::logOut  << "r_Conv_DEM::convertFrom(...) src interval=" << desc.srcInterv << endl;
		RMInit::logOut  << "r_Conv_DEM::convertFrom(...) src type=" << desc.srcType->type_id() << endl;		

		//check options
		if(!decodeOptions(options, collBBox))
		{
			RMInit::logOut << "Error in r_Conv_DEM::convertFrom(): illegal option string: " << (options?options:"(null)") << endl;
			throw r_Error();
		}
	
		//check desc.srcInterv.dimension
		if(desc.srcInterv.dimension() != srcIntervDim)
		{
			RMInit::logOut << "r_Conv_DEM::convertFrom(" << (options?options:"NULL") 
		 		 << ") desc.srcInterv dimension (" << desc.srcInterv.dimension() 
		  		 << " != " << srcIntervDim << " !" << endl;
		 	throw r_Error();		  
		}
      	
      		//check srcType
		if(!desc.srcType->isPrimitiveType())
		{
			RMInit::logOut << "r_Conv_DEM::convertFrom(" << (options?options:"NULL") 
				 << ") desc.srcType (" << desc.srcType->type_id() 
		  		 << ") not supported, only primitive types !" << endl;
			throw r_Error();		  	
		}
	
		if(desc.srcType->isComplexType())
		{
			RMInit::logOut << "r_Conv_DEM::convertFrom(" << (options?options:"NULL") 
		  		 << ") desc.srcType (" << desc.srcType->type_id() 
		  		 << ") not supported !" << endl;
			throw r_Error();		  	
		}

		//read src stream
 		readFromSrcStream();
	
		//convert from DEM to marray 	
		//--computing the marray domain
		desc.destInterv = r_Minterval(destIntervDim);
		 
		//FIXME correction for strange efect of r_Long cast with 1e-6
		if(collBBox.flipx)
  			desc.destInterv << r_Sinterval((r_Long)((collBBox.endx - max.x)/collBBox.resx + 1e-6), 
  			                               (r_Long)((collBBox.endx - min.x)/collBBox.resx + 1e-6));
		else
  		  	desc.destInterv << r_Sinterval((r_Long)((min.x - collBBox.startx)/collBBox.resx + 1e-6), 
  			                                 (r_Long)((max.x - collBBox.startx)/collBBox.resx + 1e-6));
		if(collBBox.flipy)
  			desc.destInterv << r_Sinterval((r_Long)((collBBox.endy - max.y)/collBBox.resy + 1e-6), 
  			                               (r_Long)((collBBox.endy - min.y)/collBBox.resy + 1e-6));	
		else
  			desc.destInterv << r_Sinterval((r_Long)((min.y - collBBox.starty)/collBBox.resy + 1e-6), 
  			  		               (r_Long)((max.y - collBBox.starty)/collBBox.resy + 1e-6));
  		 
        	RMInit::logOut  << "r_Conv_DEM::convertFrom(...) dest interval=" << desc.destInterv << endl;

		//--creating the resulting type			
		desc.destType = new r_Primitive_Type("Double", r_Type::DOUBLE); 
		RMInit::logOut  << "r_Conv_DEM::convertFrom(...) dest type=" << desc.destType->type_id() << endl;	

		//--claim memory for result
		desc.dest = (char*)mystore.storage_alloc(desc.destInterv.cell_count() * ((r_Primitive_Type*)desc.destType)->size());
		if(desc.dest==NULL)
		{
			RMInit::logOut << "r_Conv_DEM::convertFrom(" << (options?options:"NULL") 
	  			 << ") unable to claim memory !" << endl;
			throw  r_Ememory_allocation();		  	
		}
		memset(desc.dest, 0, desc.destInterv.cell_count() * ((r_Primitive_Type*)desc.destType)->size());		

		//--write parsed data in desc.dest
		writeFromDestStream();
	}
	catch(r_Error& err)
	{
		//cleanup
		if(!hasSrcType)
		{
			delete desc.srcType;
			desc.srcType=NULL;
		}
 	
		//desc.destType
		if(desc.destType)
        	{
			delete desc.destType;
			desc.destType=NULL;
		}

		//desc.dest
		if(desc.dest)
		{
			mystore.storage_free(desc.dest);
			desc.dest=NULL;
		}

		//report error
		throw;
	}
	
	//cleanup
	if(!hasSrcType)
	{
		delete desc.srcType;
		desc.srcType=NULL;
	}
	
	//return result	
	return desc;
}
	

r_convDesc&
r_Conv_DEM::convertTo(const char* options) throw (r_Error)
{
	bool hasSrcType=true;

	char* pTempFileName=NULL;	// name of temp file
	string tempFileName;		// duplicate of temp file name -- heaven knows why
	ofstream oFile;			// for writing out file
	FILE* pFile=NULL;		// for reading back file
	size_t lenFile=0;		// size of file as read
	
        RMInit::logOut << "r_Conv_DEM::convertTo(" << (options?options:"NULL") << ")" << endl;

	try
	{
 		if(!desc.srcType)
		{
			desc.srcType=get_external_type(desc.baseType);	
			hasSrcType=false;
		}	   
	 
        	RMInit::logOut  << "r_Conv_DEM::convertTo(...) src interval=" << desc.srcInterv << endl;
		RMInit::logOut  << "r_Conv_DEM::convertTo(...) src type=" << desc.srcType->type_id() << endl;

	 	//check options
	 	if(!decodeOptions(options, collBBox))
			throw r_Error();

		if(!desc.srcType->isPrimitiveType())
		{
			RMInit::logOut << "r_Conv_DEM::convertTo(" << (options?options:"NULL") 
				<< ") desc.srcType (" << desc.srcType->type_id() 
				<< ") not supported, only primitive types !" << endl;
			throw r_Error();	   	
		}
		if(desc.srcType->isComplexType())
		{
			RMInit::logOut << "r_Conv_DEM::convertTo(" << (options?options:"NULL") 
		  		<< ") desc.srcType (" << desc.srcType->type_id() 
		  		<< ") not supported !" << endl;
			throw r_Error();	   	
		}
	
		//read src data
		readToSrcStream();

		//convert from marray to DEM;
		//--create the temp file
		//FIXME for multithread application
		pTempFileName=tmpnam(NULL);
		if(pTempFileName==NULL)
		{
			RMInit::logOut << "r_Conv_DEM::convertTo(" << (options?options:"NULL") 
		  		<< ") desc.srcType (" << desc.srcType->type_id() 
		  		<< ") unable to generate a tempory file !" << endl;
			throw r_Error();	   	
		}

		tempFileName=pTempFileName;
		oFile.open(tempFileName.c_str());
		if(!oFile.is_open())
		{
			RMInit::logOut << "r_Conv_DEM::convertTo(" << (options?options:"NULL") 
		  		  << ") desc.srcType (" << desc.srcType->type_id() 
		  		  << ") unable to open the tempory file !" << endl;
			throw r_Error();	   	
		}	
	
        	RMInit::logOut  << "r_Conv_DEM::convertTo(...) temp file=" << tempFileName << endl;

		//--get DEM format
		writeToDestStream(oFile);
		oFile.close();
	
		//--accessing the temp file
		if ((pFile = fopen(tempFileName.c_str(), "rb")) == NULL)
         	{
			RMInit::logOut << "r_Conv_DEM::convertTo(): unable to read back file." << endl;
			throw r_Error(r_Error::r_Error_General);
		}
		fseek(pFile, 0, SEEK_END);
		lenFile = ftell(pFile);
        	RMInit::logOut  << "r_Conv_DEM::convertTo(...) dest len=" << lenFile << endl;

		if (!lenFile)
		{
			RMInit::logOut << "r_Conv_DEM::convertTo(): source contains only NULL values." << endl;
			throw r_Error( E_DEM_EMPTY );
		}

		//--creating the resulting type			
		desc.destType = new r_Primitive_Type("Char", r_Type::CHAR); 
	
		//--computing the marray domain
		desc.destInterv = r_Minterval(srcIntervDim);
		desc.destInterv << r_Sinterval((r_Long)0, (r_Long)lenFile - 1);
	
		RMInit::logOut  << "r_Conv_DEM::convertTo(...) dest interval=" << desc.destInterv << endl;
		RMInit::logOut  << "r_Conv_DEM::convertTo(...) dest type=" << desc.destType->type_id() << endl;        

		//--claim memory for desc.dest
		desc.dest = (char*)mystore.storage_alloc(lenFile);
		if(desc.dest==NULL)
		{
			RMInit::logOut << "r_Conv_DEM::convertTo(" << (options?options:"NULL") 
		  		  << ") unable to claim memory !" << endl;
			throw r_Ememory_allocation();	   	
		}
		memset(desc.dest, 0, lenFile);
	
		//--store data in desc.dest
		fseek(pFile, 0, SEEK_SET);
		fread(desc.dest, 1, lenFile, pFile);

		//clean up
		fclose(pFile);
		pFile=NULL;
		remove(pTempFileName);		
	}
	catch(r_Error& err)
	{
		//cleanup
		if(!hasSrcType)
		{
			delete desc.srcType;
			desc.srcType=NULL; 
		}

		//desc.destType
		if(desc.destType)
	  	{
         		delete desc.destType;
			desc.destType=NULL;
		}

		//desc.dest
		if(desc.dest)
		{
			mystore.storage_free(desc.dest);
			desc.dest=NULL;
		}
	 
		// close & remove file, if not done previously
		fclose(pFile);
		pFile=NULL;
		remove(pTempFileName);

		//rethrow error
		throw;
	}
	
	//clean up
 	if(!hasSrcType)
	{
		delete desc.srcType;
		desc.srcType=NULL; 
	}	
	
	//return result
	return desc;
}

const char*
r_Conv_DEM::get_name() const throw()
{
	return get_name_from_data_format(r_DEM);
}

r_Data_Format
r_Conv_DEM::get_data_format() const throw()
{
	return r_DEM;
}

r_Convertor* 
r_Conv_DEM::clone() const throw(r_Error)
{
	return new r_Conv_DEM(desc.src, desc.srcInterv, desc.srcType);
}