-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathloaddata.cpp
2451 lines (2171 loc) · 85.8 KB
/
loaddata.cpp
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
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//***********************************************************
// L o a d D a t a
// Routines having to primarily to do with reading data
//
// Copyright (c) 1993 - 2005 by John Coulthard
//
// This file is part of QuikGrid.
//
// QuikGrid 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 2 of the License, or
// (at your option) any later version.
//
// QuikGrid 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 QuikGrid (File gpl.txt); if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// or visit their website at http://www.fsf.org/licensing/licenses/gpl.txt .
//
// Special note:
// Borland C++ stream input of floating point numbers
// will not work if it is running under Win95 which has the
// original version of MS Plus installed. This is a MS bug
// and there is a patch for it. Nonetheless, due to this bug,
// QuikGrid completely avoids the use of C++ floating point
// input (*sigh*). This explains the use of function "strtod"
// for all floating point conversions, and perhaps what may look
// like some strange constructions in the code.
//
// Sept. 19/96: Modified to use "strtod" to do floating point
// conversions (Avoids need to use MS Service Pack 1).
// Oct. 14/96: Fixed failure in "Load Metric Data" to detect
// last "z" terminated by an eof.
// Oct. 26/96: Changed DXF output to use "default" instead of
// "scientific" notation - for ER MAPPER, perhaps
// other packages as well. Also for contour layer names.
// Oct. 27/96: Deleted unused 3DDXFOUTPUT function.
// Dec. 12/96: Change Load grid to accept up to 1000x1000.
// Jan. 7/97: Remove OutputDXF code to own module DXFWRITE.CPP.
// Remove Grid output code to own module GRDWRITE.CPP
// Initial implementation of DXF input.
// Jan. 8/97: Clean up code - move common code to service routines.
// Initial implementation of MEDS input.
// LoadDXF deletes second of two identical points.
// Jan. 9/97: track separately: LinesRead, PointsRead, PointsKept.
// Initial hooks for Load USGSDEMData installed.
// Feb. ?/97: Implement read DXF input.
// Mar. 11/97: Correct bug in Read DXF input.
// Mar. 12/97: Change to handle blank lines in the DXF input correctly.
// Apr. 22/98: Implement FlipXY option.
// Jun. 27/98: Correct bug in Read DXF input (warning about more than
// 100 data points would be generated twice).
// Jan. 16/99: Comment out "Load File Meds Data"
// Jan. 17/99: Implement new style input (allow commas as a delimiter
// enforces 1 xyz triplet/line and trailing comments)
// Jan 18/99: SetSurface Title changed to show cursor position.
// Feb. 1/99: Change to distinguis lat from lon on input.
// Feb. 3/99: Change to handle lat/lon input better error checking.
// Feb. 9/99: Change Urestricted registered data points to 10000.
// Feb. 10/99: Change metric input to use newstyle blank/comma input.
// Feb. 24/99: Check for null line as last line and ignore if so.
// Feb. 26/99: Change to use function StringIsBlank
// (Lat/Lon, Metric, Report line short).
// Mar. 4/99: Add ReverseSignOfZ to StoreDataPoint routine.
// Apr. 20/99: Set MessageBox to use MB_SETFOREGROUND
// Set MessageBox to use MB_SYSTEMMODAL istead of MB_TASKMODAL.
// Changes to support cancelling of input.
// Add function to ignore comments during input.
// May 14/99: Start coding internals for reading DEM files.
// Jun 1/99: Changes to support Grid Locking.
// Jun 10/99: Fixed possible problem re use of "comp" in read DEM data.
// Oct. 12/99: SetTemplate (for default template code) installed.
// Oct. 29/99: Ignore lines with errors or blank fields installed.
// Title will show values from Speed and/or Direction grids
// if they exist. (2d mode only).
// Some leading blanks allowed in fixed format input fields.
// Dec. 15/99: Implement load ER Mapper Grid.
// Dec. 30/99: Add TabEdit - Fixed columns works with tabs.
// Jan. 31/00: Add load outline code.
// Feb. 2/00: Add LockNormalization code. Change outline code to load to
// the "outline" array.
// Feb. 3/00: Make title bar display speed and direction better.
// May 13/00: Outline input will handle Z coordinates.
// Also reset itself better if second outline data set read.
// Jun. 11/00: Fixed bug in outline input which invalidated the grid.
// (Triggered by outlines with negative z value).
// Sep. 11/00: Play around with scaling for dummy grid and data points
// when outline alone is loaded.
// Sep. 17/00: Fixed error in fixed format input Lat and Lon not
// interpreted correctly. Also maade "IgnoreLines in error"
// work for lat lon input.
// Mar. 10/01: Add support for Colour maping by initing colours when
// loading is complete. (InitializeColourMap...).
// Mar. 21-25/01: Implementation of LoadSubmetrixSXP Data.
// Mar. 24-25/01: Implement setting of "nice" colour map numbers.
// Apr. 18/01: Implement reading multiple files for sxp & xyz metric input.
// Oct. 3/01: Changed convert float to ignore lines if a blank field onlyl
// iff IgnoreErrorLines is set.
// Nov. 11/01: Change messagebox to use NotifyUser instead.
// Dec. 15/01: DXF input - skip first point after polyline identified.
// Apr. 2005: Add support for NOAA formatted lat/lon numbers.
//*******************************************************************
//
// Code candidates to move to external module(s).
// SetPrecision, ResetSurfaceTitle, SetStartupState,
// SetSurfaceTitle.
#include <windows.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <strstream>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "surfgrid.h"
#include "scatdata.h"
#include "xygrid.h"
#include "utilitys.h"
#include "rc.h"
#include "quikgrid.h"
#include "paintcon.h"
#include "loaddata.h"
#include "assert.h"
#include "grid.h"
#include "rotate.h"
#include "condraw.h"
#include "gridview.h"
using namespace std;
static ifstream inFile( " ", ios::in );
extern int GridGeneratedOK, GridLock;
extern int DisplayVolumeDifference;
extern float Turn, Tilt;
extern UINT PaintState;
extern SurfaceGrid Sgrid, Dgrid, Cgrid;
extern float CgridZadjust;
static int IgnoreZero = 1;
static int KeepEveryNthPoint = 1;
static long LinesRead,
PointsRead,
MaxInputData;
static double dxnormalize, dynormalize,
dx, dy, dz;
static char szdx[50], szdy[50], szdz[50],
szNOAAdx[3][50], szNOAAdy[3][50],
InputLine[200], Comment[200],
szPrint[400], szLoadString[255];
static int Xstart, Xlength, NewXstart, // Start posn and length of
Ystart, Ylength, NewYstart, // szdx...comment in InputLine.
Zstart, Zlength, NewZstart,
Cstart, Clength, NewCstart;
static int FixedStarts = FALSE, // Indicates that Xstart... are unchanging.
IgnoreThisLine = FALSE; // Used with IgnoreErrorLines
static char *endptr;
// Used by ConstructFilename et. all to handle multiple file names.
static int NameOffset, PathOffset;
static char szFileName[512];
// *************** G L O B A L S *******************
ScatData OutLine;
long PointsKept;
int CancelLoadData;
int FlipXY = FALSE;
int NewInputStyle = TRUE;
int ReverseSignOfZ = FALSE;
int NicePrecision = 6;
int IgnoreComments = FALSE;
int DEMZeroUndefined = TRUE;
int IgnoreErrorLines = FALSE;
int LeadingBlanks = 3;
int LockNormalization = FALSE;
int NiceRatio = 4,
NiceRatioMaxLines = 200;
int SubmetrixSelectZ = 1;
int InputFieldPosition[4]= {0, 1, 2, 3};
int NOAAInputFieldPosition[4] = {0, 3, 6, 7};
//**********************************************************
// C o n s t r u c t F i l e n a m e
//**********************************************************
// For handling multiple files... this routine is called
// for the first file in a multiple file selection. It
// copies over the path name and first file name and sets up
// the initial offsets to mark the end of the path and location
// of the next (if any) of the files in the set.
static void ConstructFilename( char FileName[] )
{
strcpy( szFileName, FileName ); // copy over path and maybe the file name.
NameOffset = strlen( FileName );
if( FileName[ NameOffset+1 ] != NULL ) // This is a multiple file selection.
{
szFileName[NameOffset] = '\\'; // append separator to the path name.
strcpy( &szFileName[NameOffset+1], &FileName[NameOffset+1] ); // and concatenate the file name.
PathOffset = NameOffset + 1; // Save size of path in filename.
strlen( &FileName[NameOffset+1])+1 ;
NameOffset += strlen( &FileName[NameOffset+1]) + 2; // Store offset of next filename in input.
}
}
//**********************************************************
// N o M o r e F i l e s
//**********************************************************
// For handling multiple files... This routine returns
// true if there are no more files available in the multiple
// file selection. If there are it sets up the file name, closes
// the old file and opens the new.
static bool NoMoreFiles( char FileName[], bool Binary=false )
{
static char LocalFileName[200];
if( FileName[NameOffset+1] == NULL ) return true;
strcpy( LocalFileName, &FileName[NameOffset]);
if( StringIsBlank(LocalFileName )) return true;
strcpy( &szFileName[PathOffset], &FileName[NameOffset]);
NameOffset += strlen( &FileName[NameOffset])+1;
inFile.close();
if( Binary ) inFile.open( szFileName, ios::binary );
else inFile.open( szFileName, ios::in );
if( !inFile )
{ NotifyUser( IDS_OPENINPUTFILEFAIL, szFileName );
return true; }
LinesRead = 1;
return false;
}
//**********************************************************
// S e t P r e c i s i o n
//**********************************************************
// Sets a nice precision to use to display numbers.
// Some data sets carry much more than 6 digits of accuracy.
// This routine will set the global NicePrecision so it is between
// 5 and 16 depending on the input data.
static void SetPrecision()
{
static double Temp, LogTemp;
Temp = fabs(dxnormalize)/fabs(ScatterData.xMax()-ScatterData.xMin() );
NicePrecision = 6; // default setting.
if( Temp == 0 ) return;
LogTemp = log10( Temp );
if( LogTemp < 1. ) return;
NicePrecision = NicePrecision + (int)LogTemp;
if( NicePrecision > 13 ) NicePrecision = 13;
return;
}
//*************************************************************
// S c o o p
//*************************************************************
// Scan InputLine starting at position Start for a numeric type string
// at posn start with length length.
static void Scoop( char input[], const int initial, int &start, int &length)
{
static char c;
length = 0;
if( input[initial]==NULL ) return;
start = initial;
// Determine start == next position not a blank, comma, tab char or colon.
while (true)
{
c = input[start];
if( (c==NULL) ) return; // ran into end of line.
if( (c == ' ') || (c == ',') || (c =='\t') || c==':' ) { start++; continue;}
break;
}
length = strcspn( &input[start], " ,\t:" ) ;
}
//*************************************************************
// M o v e F i x e d F i e l d
//*************************************************************
// Internal service routine:
// Moves text from Start in Inputline to the field.
// A small number of leading blanks will be allowed.
static void MoveFixedField( char szField[], const int Start, const int InputLength )
{
static int length, start, i;
start = Start;
strcpy ( szField, " " ); // Initialize with a blank line.
if( start < InputLength)
{
for( i = 0; i < LeadingBlanks; i++ )
if( InputLine[start] == ' '||InputLine[start]=='\t' ) start++; // check for whitespace
length = strcspn( &InputLine[start], " ,\t" ) ;
if( length > 0 )
{
strncpy( szField, &InputLine[start], length ) ;
szField[length] = NULL;
}
}
}
//*************************************************************
// G e t 3 S t r i n g s
//*************************************************************
//
// Scans InputLine for 3 strings (szdx, szdy, szdz)
// delimited by whitespace or commas.
// Used by LoadFileOutlineData) routine only.
//
static int Get3Strings()
{
static int i, InputLength;
strcpy( szdx, "0" );
strcpy( szdy, "0" );
strcpy( szdz, "0" );
if( StringIsBlank (InputLine) ) return 1; // Blank line is ok will be a move.
InputLength = strlen( InputLine );
Scoop( InputLine, 0, Xstart, Xlength );
if( Xlength < 1 || Xlength > 50 ) return 0;
strncpy( szdx, &InputLine[Xstart], Xlength ) ;
szdx[Xlength] = NULL;
Scoop( InputLine, Xstart+Xlength, Ystart, Ylength);
if( Ylength < 1 || Ylength > 50 ) return 0;
strncpy( szdy, &InputLine[Ystart], Ylength ) ;
szdy[Ylength] = NULL;
strcpy( Comment, " " ); // Initialize comment as well.
Scoop( InputLine, Ystart+Ylength, Zstart, Zlength);
if( Zlength < 1 || Zlength > 20 ) return 1; // z missing is ok for outlines.
strncpy( szdz, &InputLine[Zstart], Zlength ) ;
szdz[Zlength] = NULL;
Cstart = Zstart+Zlength;
Clength = 0;
if( InputLine[Cstart]==NULL ) return 1;
Clength = strlen( &InputLine[Cstart]);
// skip leading blanks for the free format comment field.
for ( i = 0; i < Clength; i++)
{
if( InputLine[Cstart+i] != ' ' ){ Cstart += i; break ;}
}
if( InputLine[Cstart]==NULL ) return 1;
strcpy ( Comment, &InputLine[Cstart] ) ;
Clength = strlen( Comment );
// Now trim trailing blanks
for( i = Clength-1; i > 0; i--)
if( Comment[i] != ' ' ) {Clength = i+1; break; }
Comment[Clength] = NULL;
return 1;
}
//*************************************************************
// S t r i n g P o s i t i o n M a p
//*************************************************************
// Utility scans the input line and determines the position
// of the strings it contains (start location & length)
// Everything is left in the common data structure "StringMap"
struct StringMapType
{ int Start;
int Length; };
static const int SizeOfMap = 30; // Number of strings to locate on line.
static StringMapType StringMap[SizeOfMap];
static void StringPositionMap()
{
static int i, Start, Length, ScanPosition;
for (i = 0; i < SizeOfMap; i++ ) // Initialize Position Map
StringMap[i].Start = StringMap[i].Length = 0;
ScanPosition = 0;
for( i = 0; i < SizeOfMap; i++)
{
Scoop( InputLine, ScanPosition, Start, Length );
if( Length < 1 ) return;
StringMap[i].Start = Start;
StringMap[i].Length = Length;
ScanPosition = Start + Length;
}
}
//*************************************************************
// G e t 4 S t r i n g s
//*************************************************************
// Routine scans InputLine for 4 strings (szdx, szdy, szdz, comment)
//
// Used by LatLon, NOS and Metric input.
static int Get4Strings()
{
static int InputLength, i;
szdx[0] = szdy[0] = szdz[0] = Comment[0] = NULL;
if( FixedStarts )
{
TabEdit( InputLine, sizeof(InputLine) );
InputLength = strlen( InputLine );
MoveFixedField( szdx, Xstart, InputLength);
MoveFixedField( szdy, Ystart, InputLength);
MoveFixedField( szdz, Zstart, InputLength);
// Don't want to allow leading blanks for comments??? <- NOTE
strcpy ( Comment, " " );
if( Cstart >= 0 ) MoveFixedField( Comment, Cstart, InputLength);
return 1;
} // end FixedStarts
// All the rest handles positional input.
if( LinesRead == 1 ) TabEdit( InputLine, sizeof(InputLine) );
StringPositionMap(); // Determine where the strings are in the input.
InputLength = strlen( InputLine );
Xstart = StringMap[InputFieldPosition[0]].Start;
Xlength = StringMap[InputFieldPosition[0]].Length;
if( Xlength < 1 || Xlength > 50 ) return 0;
strncpy( szdx, &InputLine[Xstart], Xlength ) ;
szdx[Xlength] = NULL;
Ystart = StringMap[InputFieldPosition[1]].Start;
Ylength = StringMap[InputFieldPosition[1]].Length;
if( Ylength < 1 || Ylength > 50 ) return 0;
strncpy( szdy, &InputLine[Ystart], Ylength ) ;
szdy[Ylength] = NULL;
Zstart = StringMap[InputFieldPosition[2]].Start;
Zlength = StringMap[InputFieldPosition[2]].Length;
if( Zlength < 1 || Zlength > 20 ) return 0;
strncpy( szdz, &InputLine[Zstart], Zlength ) ;
szdz[Zlength] = NULL;
Cstart = StringMap[InputFieldPosition[3]].Start;
Clength = StringMap[InputFieldPosition[3]].Length;
strcpy( Comment, " " );
if( Clength < 1 ) return 1;
if( Cstart < 0 ) return 1;
if( InputLine[Cstart]==NULL ) return 1;
if( Clength > sizeof(Comment) ) Clength = sizeof(Comment) - 1 ;
//NotifyUser( "Comment at %i of length at least %i ", Cstart, Clength );
// Check if comment last field take all to end of the input line.
// - If so take comment to the end of the line.
if( InputFieldPosition[3] > InputFieldPosition[0] &&
InputFieldPosition[3] > InputFieldPosition[1] &&
InputFieldPosition[3] > InputFieldPosition[2] )
{ // Take to end of the line ...
Clength = strlen( &InputLine[Cstart]);
if( Clength > sizeof(Comment) ) Clength = sizeof(Comment) - 1 ;
strncpy ( Comment, &InputLine[Cstart], Clength ) ;
Comment[Clength] = NULL;
// Now trim trailing blanks
for( i = Clength-1; i > 0; i--)
if( Comment[i] != ' ' ) {Clength = i+1; break; }
Comment[Clength] = NULL;
} // end if Take to first break character only.
else
{
strncpy( Comment, &InputLine[Cstart], Clength);
Comment[Clength] = NULL;
}
return 1;
}
//*************************************************************
// G e t N O A A S t r i n g s
//*************************************************************
// Routine scans InputLine for 8 strings (szdx, szdy, szdz, comment)
//
// Used for NOAA type lat lon input only.
static int GetNOAAStrings()
{
static int InputLength, i;
szdx[0] = szdy[0] = szdz[0] = Comment[0] = NULL;
// Don't handle fixed starts in initial implementation.
// if( FixedStarts )
// {
// TabEdit( InputLine, sizeof(InputLine) );
// InputLength = strlen( InputLine );
// MoveFixedField( szdx, Xstart, InputLength);
// MoveFixedField( szdy, Ystart, InputLength);
// MoveFixedField( szdz, Zstart, InputLength);
// Don't want to allow leading blanks for comments??? <- NOTE
// strcpy ( Comment, " " );
// if( Cstart >= 0 ) MoveFixedField( Comment, Cstart, InputLength);
// return 1;
// } // end FixedStarts
// All the rest handles positional input.
if( LinesRead == 1 ) TabEdit( InputLine, sizeof(InputLine) );
StringPositionMap(); // Determine where the strings are in the input.
InputLength = strlen( InputLine );
// For NOAA input we need three strings for lat and three for lon.
// Form is "dd mm ss.ssss"
//
// Perhaps here just pass back the input field start position.
// Maybe two string arrays of length three (one for each part?).
for( i = 0; i < 3; i++ )
{
Xstart = StringMap[NOAAInputFieldPosition[0]+i].Start;
Xlength = StringMap[NOAAInputFieldPosition[0]+i].Length;
if( Xlength < 1 || Xlength > 8 ) return 0;
strncpy( &szNOAAdx[i][0], &InputLine[Xstart], Xlength ) ;
szNOAAdx[i][Xlength] = NULL;
}
for( i = 0; i < 3; i++ )
{
Ystart = StringMap[NOAAInputFieldPosition[1]+i].Start;
Ylength = StringMap[NOAAInputFieldPosition[1]+i].Length;
if( Ylength < 1 || Ylength > 8 ) return 0;
strncpy( &szNOAAdy[i][0], &InputLine[Ystart], Ylength ) ;
szNOAAdy[i][Ylength] = NULL;
}
Zstart = StringMap[NOAAInputFieldPosition[2]].Start;
Zlength = StringMap[NOAAInputFieldPosition[2]].Length;
if( Zlength < 1 || Zlength > 10 ) return 0;
strncpy( szdz, &InputLine[Zstart], Zlength ) ;
szdz[Zlength] = NULL;
Cstart = StringMap[NOAAInputFieldPosition[3]].Start;
Clength = StringMap[NOAAInputFieldPosition[3]].Length;
strcpy( Comment, " " );
if( Clength < 1 ) return 1;
if( Cstart < 0 ) return 1;
if( InputLine[Cstart]==NULL ) return 1;
if( Clength > sizeof(Comment) ) Clength = sizeof(Comment) - 1 ;
//NotifyUser( "Comment at %i of length at least %i ", Cstart, Clength );
// Check if comment last field take all to end of the input line.
// - If so take comment to the end of the line.
if( NOAAInputFieldPosition[3] > NOAAInputFieldPosition[0] &&
NOAAInputFieldPosition[3] > NOAAInputFieldPosition[1] &&
NOAAInputFieldPosition[3] > NOAAInputFieldPosition[2] )
{ // Take to end of the line ...
Clength = strlen( &InputLine[Cstart]);
if( Clength > sizeof(Comment) ) Clength = sizeof(Comment) - 1 ;
strncpy ( Comment, &InputLine[Cstart], Clength ) ;
Comment[Clength] = NULL;
// Now trim trailing blanks
for( i = Clength-1; i > 0; i--)
if( Comment[i] != ' ' ) {Clength = i+1; break; }
Comment[Clength] = NULL;
} // end if Take to first break character only.
else
{
strncpy( Comment, &InputLine[Cstart], Clength);
Comment[Clength] = NULL;
}
return 1;
}
//****************************************************************
// A n a l y z e S t a r t
//***************************************************************
// Used to analyze first line of input only to determine if it
// is being used to specify the location of fixed input data.
// Used by "CheckForFixedStarts", following....
static void AnalyzeStart( char string[], int start )
{
if( (string[0]=='X')||(strncmp(string,"LA",2)==0) ) NewXstart = start;
if( (string[0]=='Y')||(strncmp(string,"LO",2)==0) ) NewYstart = start;
if( string[0]=='Z' ) NewZstart = start;
if( string[0]=='C' ) NewCstart = start;
}
//*************************************************************
// C h e c k F o r F i x e d S t a r t s
//*************************************************************
// Check to see if the strings contain one of LA, LO, X, Y, C, Z.
// and if so set up the starting position as fixed.
// Return true only if processed a header line and it was ok.
// (true means calling routine should read another line to replace it).
static bool CheckForFixedStarts()
{
static char c;
if( LinesRead != 1 ) return false; // Only check first line in a file.
c = szdx[0];
if( !( c=='L'||c=='C'||c=='X'||c=='Y'||c=='Z') ) return false;
NewXstart = NewYstart = NewZstart = 0;
NewCstart = -1;
AnalyzeStart( szdx, Xstart );
AnalyzeStart( szdy, Ystart );
AnalyzeStart( szdz, Zstart );
AnalyzeStart( Comment, Cstart );
if( NewXstart == NewYstart ) return false;
if( NewXstart == NewZstart ) return false;
if( NewYstart == NewZstart ) return false;
FixedStarts = TRUE;
Xstart = NewXstart;
Ystart = NewYstart;
Zstart = NewZstart;
Cstart = NewCstart;
LinesRead++;
return true;
}
//*********************************************************************
// G e t L o a d S t r i n g (internal service routine)
//*********************************************************************
static bool GetLoadString( int MessageNumber )
{
if( LoadString( (HINSTANCE)hInst, MessageNumber, szLoadString, 255 ) ) return true;
NotifyUserError( MessageNumber );
return false;
}
//*************************************************************
// R e s e t S u r f a c e T i t l e
//*************************************************************
//
// Internal service routine.
static void ResetSurfaceTitle( HWND &hwnd )
{
MaxInputData = ScatterData.MaximumPoints();
if( !GetLoadString( IDS_QUIKGRID ) ) return;
SetWindowText( hwnd, szLoadString );
}
//*************************************************************
// S e t R e a d i n g S u r f a c e T i t l e
//*************************************************************
//
// Internal service routine.
static void SetReadingSurfaceTitle( HWND &hwnd, char FileName[] )
{
if( !GetLoadString( IDS_READINGINPUTFROM ) ) return;
sprintf( szPrint, szLoadString, FileName );
SetWindowText( hwnd, szPrint );
}
//******************************************************************
// S e t S t a r t u p S t a t e
//******************************************************************
void SetStartupState( HWND &hwnd )
{
Zgrid.New(2,2);
ScatterData.Reset();
Zgrid.zset(0,0,-99); Zgrid.zset(0,1,-99);
Zgrid.zset(1,0,-99); Zgrid.zset(1,1,-99);
Zgrid.xset(0,0); Zgrid.xset(1,1);
Zgrid.yset(0,0); Zgrid.yset(1,1);
ContourLinesSet( 0, 1, NumberOfContours );
ContourBoldLabelSet(PenHighLite);
PaintContourReset();
RotateReset();
szTitle[0] = NULL;
dxnormalize = dynormalize = 0.0;
ResetSurfaceTitle( hwnd );
CancelLoadData = FALSE;
PictureChanged( hwnd );
GridGeneratedOK = FALSE;
}
//******************************************************************
// C h e c k L i m i t s
//******************************************************************
// Internal service routine.
static int CheckLimits( const long &i )
{
if( i == MaxInputData )
{ NotifyUser( IDS_TOOMUCHDATA );
return 1; }
if( CancelLoadData ) return 1;
return 0;
}
//*****************************************************************
// R e p o r t C o n v e r s i o n F a i l
//*****************************************************************
// Internal service routine.
static void ReportConversionFail( char szText[], char szType[] )
{
NotifyUser( IDS_CONVERSIONFAIL, szType, szText, LinesRead );
}
//*****************************************************************
// R e p o r t F l o a t F a i l
//*****************************************************************
// Internal service routine.
static void ReportFloatFail( char szText[] )
{
NotifyUser( IDS_FLOATINGPOINTFAIL, szText, LinesRead, szFileName );
}
//*****************************************************************
// R e p o r t L i n e S h o r t
//*****************************************************************
// Internal service routine.
static void ReportLineShort( )
{
static char TempSpace[10];
if( StringIsBlank( InputLine ) )
{
if( IgnoreErrorLines) { IgnoreThisLine = TRUE; return; }
// read another line and see if we get eof. If so no error.
inFile.getline( TempSpace, sizeof( TempSpace));
if( inFile.eof() ) return;
}
NotifyUser( IDS_NOTENOUGHNUMBERS, LinesRead, szFileName, InputLine );
}
//*****************************************************************
// R e p o r t E O F
//*****************************************************************
// Internal service routine.
static void ReportEOF( )
{
NotifyUser( IDS_UNEXPECTEDEOF, LinesRead+1, szFileName );
}
//*****************************************************************
// C o n v e r t F l o a t
//*****************************************************************
// Internal service routine.
static int ConvertFloat( double &result, char szText[] )
{
if( StringIsBlank( szText ) )
{
result = 0.0;
if( IgnoreErrorLines) IgnoreThisLine = TRUE;
return TRUE;
}
result = strtod(szText, &endptr);
if( *endptr == NULL || *endptr == ','||*endptr=='\t' ) return TRUE;
if( *endptr == 'D' ) *endptr = 'e'; // Perhaps exponent is +Dnn??
result = strtod(szText, &endptr); // so change to e and try again.
if( *endptr == NULL || *endptr == ','||*endptr=='\t' ) return TRUE;
if( !IgnoreErrorLines) { ReportFloatFail( szText ); return FALSE; }
IgnoreThisLine = TRUE;
return TRUE;
}
//*****************************************************************
// I n i t i a l i z e I n p u t
//*****************************************************************
// Internal service routine
static int InitializeInput(HWND &hwnd, char FileName[], bool binary=false )
{
CancelLoadData = FALSE;
ResetSurfaceTitle( hwnd );
szTitle[0] = NULL;
if( !binary) inFile.open( FileName, ios::in );
else inFile.open( FileName, ios::binary );
if( !inFile )
{
NotifyUser( IDS_OPENINPUTFILEFAIL , FileName );
return FALSE;
}
SetReadingSurfaceTitle( hwnd, FileName );
ScatterData.Reset();
SetWaitCursor();
PointsRead = PointsKept = LinesRead = 0;
LatLonData = FALSE;
FixedStarts = FALSE;
return TRUE;
}
//*****************************************************************
// S e t N i c e C o l o u r M a p
//*****************************************************************
void SetNiceColourMap( const float mina, const float maxa )
{
static double min, incr, max;
static double zadjust;
min = mina;
max = maxa;
incr = ((double)max-(double)min)/9.;
zadjust = ScatterData.zAdjust();
incr = AtPrecision( incr, 1 ); // take increment to 1 digit.
// Make min & max a divisor of increment.
if( incr != 0.0 )
{
min = double( int( ((double)min-zadjust) / incr)*incr) + zadjust;
max = double( int( ((double)max-zadjust) / incr)*incr) + zadjust;
}
DataColourMap( min, min+(max-min)*.5, max);
}
//*****************************************************************
// I n i t i a l i z e C o l o u r M a p
//*****************************************************************
// Internal service routine.
static void InitializeColourMap()
{
SetNiceColourMap( ScatterData.zMin(), ScatterData.zMax() );
}
//*****************************************************************
// S h u t d o w n I n p u t
//*****************************************************************
// Internal service routine. (not used by all routines).
static int ShutdownInput(HWND &hwnd, char FileName[] )
{
inFile.close();
if( ScatterData.Size() < 3 )
{
NotifyUser( IDS_TOOFEWPOINTS );
SetStartupState( hwnd);
return FALSE;
}
ScatterData.zAdjust( -ScatterData.zMin() ); // Normalize to zero.
if( ScatterData.zMin() == ScatterData.zMax() )
{
NotifyUser( "Warning: The z values for the input data span no distance (all are = %g)",
ScatterData.zMin()-ScatterData.zAdjust());
}
if( LatLonData) NicePrecision = 6;
else SetPrecision();
SetSurfaceTitle( hwnd, FileName );
RestoreCursor();
InitializeColourMap();
if( !InitializeGrid( hwnd ) ) return FALSE;
return TRUE;
}
//*************************************************************
// S t o r e D a t a P o i n t
//*************************************************************
// Internal service routine
static void StoreDataPoint( int StoreComment = FALSE, unsigned char Ignore=0 )
{
static double temp;
if( IgnoreThisLine ) return;
if( (PointsRead%KeepEveryNthPoint) != 0 ) { PointsRead++; return;}
if( FlipXY )
{
if( LatLonData ) { temp = dx; dx = -dy; dy = -temp; }
else { temp = dx; dx = dy; dy = temp; }
}
if( ReverseSignOfZ ) dz = -dz;
if( (PointsKept == 0) && (!GridLock) && (!LockNormalization) )
{ dxnormalize = dx; dynormalize = dy; }
if( IgnoreComments || (!StoreComment) )
ScatterData.SetNext( dx-dxnormalize, dy-dynormalize, dz, NULL, Ignore );
else ScatterData.SetNext( dx-dxnormalize, dy-dynormalize, dz, Comment, Ignore );
PointsKept++;
PointsRead++;
}
//*************************************************************
// K e e p E v e r y N t h P o i n t
//*************************************************************
int KeepEveryNth() { return KeepEveryNthPoint; }
int KeepEveryNth( int NewValue )
{
int OldValue = KeepEveryNthPoint;
KeepEveryNthPoint = NewValue;
if (KeepEveryNthPoint < 1 ) KeepEveryNthPoint = 1;
return OldValue;
}
//*************************************************************
// I g n o r e Z z e r o
//*************************************************************
int IgnoreZzero( ) { return IgnoreZero ; }
int IgnoreZzero( int i )
{
int oldIgnoreZero = IgnoreZero;
IgnoreZero = i;
return oldIgnoreZero;
}
//*************************************************************
// S e t S u r f a c e T i t l e
//*************************************************************
void SetSurfaceTitle( HWND &hwnd, char szFileName[], float x, float y, int ix, int iy )
{
static int i;
static char *FileNameOnly;
static GridViewType CurrentView;
ostrstream Buf;
Buf << "QuikGrid - ";
FileNameOnly = &szFileName[0];
for( i = (strlen(szFileName) - 1); i > 0; i-- )
{
if( szFileName[i] == '\\' )
{ FileNameOnly = &szFileName[i+1]; break; }
}
Buf << FileNameOnly;
// Now optionally something about the state of resolution:
CurrentView = Zgrid.view();
if( CurrentView.xIncrement > 1 )
{
Buf << " <Grid Resolution 1/" << CurrentView.xIncrement << ">";
}
if( PaintState == IDM_2DSURFACE)
{
if( (ix > -1) && (iy > -1) ) // Show cursor position?
{
if( LatLonData)
{
Buf << ": " << FormatLat( y+dynormalize) << " " ;
Buf << FormatLon( x+dxnormalize) ;
}
else Buf << ": " << setprecision( NicePrecision)
<< (x+dxnormalize) << " " << (y+dynormalize) ;
Buf << setprecision(5);
if( Zgrid.z(ix,iy) >= 0.0 ) // Display if valid.
Buf << " Z:" << (Zgrid.z(ix,iy) - ScatterData.zAdjust()) ;
Buf << setprecision(3);
if( Dgrid.xsize() > 2 && Dgrid.z(ix,iy) >= 0.0 ) // No z adjust for
Buf << " D:" << (Dgrid.z(ix,iy)) ;
if( Sgrid.xsize() > 2 && Sgrid.z(ix,iy) >= 0.0 ) // Dgrid or Sgrid!!!
Buf << " S:" << (Sgrid.z(ix,iy)) ;
Buf << setprecision(5);
if( Cgrid.xsize() > 2 && Cgrid.z(ix,iy) >= 0.0 ) // Colour grid.
Buf << " C:" << (Cgrid.z(ix,iy) - CgridZadjust) ;
} // end if( (ix...
} // end if (PaintState...
else // PaintState is 3D
Buf << " Angle about: " << setprecision(3)
<< Turn << " above: " << Tilt << ends;
Buf << ends;
char *szBuf = Buf.str();
SetWindowText( hwnd, szBuf );
delete szBuf;
}
//*****************************************************************
// G r i d S i z e R a t i o : Change Assumptions
//*****************************************************************
int GridSizeRatio() { return NiceRatio; }
int GridSizeRatio( int NewRatio )
{ int OldRatio = NiceRatio;
NiceRatio=NewRatio;
if( NiceRatio > 20 )
{ NotifyUser( IDS_GRIDRATIOINVALID ); NiceRatio = 20; }
if( NiceRatio < 1 )
{ NotifyUser( IDS_GRIDRATIOINVALID ); NiceRatio = 1; }
return OldRatio; }
//*****************************************************************
// G r i d M a x R a t i o : Change Assumptions
//*****************************************************************
int GridMaxRatio() { return NiceRatioMaxLines; }
int GridMaxRatio( int NewRatio )
{
static int OldRatio, MaxLines;
MaxLines = Zgrid.MaximumXY();
OldRatio = NiceRatioMaxLines;
NiceRatioMaxLines=NewRatio;
if( NiceRatioMaxLines > MaxLines )
{ NotifyUser( "Maximum lines is too large" );
NiceRatioMaxLines = 200; }
if( NiceRatioMaxLines < 20 )
{ NotifyUser( "Maximum lines cannot be smaller than 20" );
NiceRatioMaxLines = 20; }
return OldRatio;
}
//******************************************************************
// L o a d T e s t D a t a
//******************************************************************
void LoadTestData(HWND &hwnd )
{
ResetSurfaceTitle( hwnd );
LatLonData = 0;
struct Point3d { float x, y, z; char* comment; unsigned char ignore; } ;
static Point3d TestData[] = {
{11,11,11,"Comment for point 1",0},
{14,11.5,12, "2, - a comment",0},
{11.2,15.4,12, "Comment for point 3",0},
{13,13,17, "4, Another comment",0},
{15.1,14.7,13,"Five ",0},
{18,18,19,"Six",0},
{20,20,13,"7, The quick brown fox jumped over the lazy dog (a long commment)",0},
{14.6,19,14,"Eight",0},
{18.5,16.2,12,"Nine",0},
{12.2,11.8, 17, "Ten - initially ignored",1} } ;