File: txt2las.c

package info (click to toggle)
liblas 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,888 kB
  • ctags: 4,614
  • sloc: cpp: 31,630; xml: 4,195; python: 2,928; ansic: 2,439; cs: 2,411; sh: 143; makefile: 37
file content (920 lines) | stat: -rwxr-xr-x 33,249 bytes parent folder | download | duplicates (3)
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
/***************************************************************************
 * $Id$
 * $Date$
 *
 * Project: libLAS -- C/C++ read/write library for LAS LIDAR data
 * Purpose: ASCII text to LAS translation
 * Author:  Martin Isenburg isenburg@cs.unc.edu 
 ***************************************************************************
 * Copyright (c) 2007, Martin Isenburg isenburg@cs.unc.edu 
 *
 * See LICENSE.txt in this source distribution for more information.
 **************************************************************************/

#include <liblas.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LAS_FORMAT_10 0
#define LAS_FORMAT_11 1
#define LAS_FORMAT_12 2



void usage()
{

    fprintf(stderr,"----------------------------------------------------------\n");
    fprintf(stderr,"    txt2las (version %s) usage:\n", LAS_GetVersion());
    fprintf(stderr,"----------------------------------------------------------\n");
    fprintf(stderr,"\n");

    fprintf(stderr,"Parse a text file with a given format:\n");
    fprintf(stderr,"  txt2las -parse tsxyz lidar.txt\n");
    fprintf(stderr,"\n");

    fprintf(stderr,"Set the scale:\n");
    fprintf(stderr,"  txt2las --parse xyz --scale 0.02 -i lidar.txt -o lidar.laz\n");
    fprintf(stderr,"\n");
    
    fprintf(stderr,"Set the xyz scale:\n");
    fprintf(stderr,"  txt2las --parse xsysz --verbose --xyz_scale 0.02 0.02 0.01 lidar.txt\n");
    fprintf(stderr,"\n");
    
    fprintf(stderr,"----------------------------------------------------------\n");
    fprintf(stderr," The '--parse txyz' flag specifies how to format each\n");
    fprintf(stderr," each line of the ASCII file. For example, 'txyzia'\n");
    fprintf(stderr," means that the first number of each line should be the\n");
    fprintf(stderr," gpstime, the next three numbers should be the x, y, and\n");
    fprintf(stderr," z coordinate, the next number should be the intensity\n");
    fprintf(stderr," and the next number should be the scan angle.\n");
    fprintf(stderr," The supported entries are:\n");
    fprintf(stderr,"   a - scan angle\n");
    fprintf(stderr,"   i - intensity\n");
    fprintf(stderr,"   n - number of returns for given pulse\n");
    fprintf(stderr,"   r - number of this return\n");
    fprintf(stderr,"   c - classification\n");
    fprintf(stderr,"   u - user data (does not currently work)\n");
    fprintf(stderr,"   p - point source ID\n");
    fprintf(stderr,"   e - edge of flight line\n");
    fprintf(stderr,"   d - direction of scan flag\n");
    fprintf(stderr,"   R - red channel of RGB color\n");
    fprintf(stderr,"   G - green channel of RGB color\n");
    fprintf(stderr,"   B - blue channel of RGB color\n\n");
    
    fprintf(stderr,"\n----------------------------------------------------------\n");
    fprintf(stderr," The '-scale 0.02' flag specifies the quantization. The\n");
    fprintf(stderr," default value of 0.01 means that the smallest increment\n");
    fprintf(stderr," two between coordinates is 0.01. If measurements are in\n");
    fprintf(stderr," meters this corresponds to centimeter accuracy, which is\n");
    fprintf(stderr," commonly considered sufficient for LIDAR data.\n");

    fprintf(stderr,"\n----------------------------------------------------------\n");
    fprintf(stderr," Other parameters such as '--xyz_offset 500000 2000000 0'\n");
    fprintf(stderr," or '-xyz_scale 0.02 0.02 0.01' or '-file_creation 67 2003'\n");
    fprintf(stderr," or '-system_identifier \"Airborne One Leica 50,000 Hz\"'\n");
    fprintf(stderr," or '-generating_software \"TerraScan\"' are available too.\n");

    fprintf(stderr, "For more information, see the full documentation for txt2las at:\n"
                    " http://liblas.org/utilities/txt2las.html\n");
    fprintf(stderr,"----------------------------------------------------------\n");

}

static void VecUpdateMinMax3dv(double min[3], double max[3], const double v[3])
{
  if (v[0]<min[0]) min[0]=v[0]; else if (v[0]>max[0]) max[0]=v[0];
  if (v[1]<min[1]) min[1]=v[1]; else if (v[1]>max[1]) max[1]=v[1];
  if (v[2]<min[2]) min[2]=v[2]; else if (v[2]>max[2]) max[2]=v[2];
}

static void VecCopy3dv(double v[3], const double a[3])
{
  v[0] = a[0];
  v[1] = a[1];
  v[2] = a[2];
}

static int parse(const char* parse_string, const char* line, double* xyz, LASPointH point, double* gps_time)
{
    int temp_i;
    float temp_f;
    const char* p = parse_string;
    const char* l = line;

    LASColorH color = LASColor_Create();
    
    while (p[0])
    {
        /* // we expect the x coordinate */
        if (p[0] == 'x') 
        {
            /* // first skip white spaces */
            while (l[0] && (l[0] == ' ' || l[0] == ',' || l[0] == '\t')) l++; 
            if (l[0] == 0) return FALSE;
            if (sscanf(l, "%lf", &(xyz[0])) != 1) return FALSE;
            /* // then advance to next white space */
            while (l[0] && l[0] != ' ' && l[0] != ',' && l[0] != '\t') l++; 
        }
        /* // we expect the y coordinate */
        else if (p[0] == 'y') 
        {
            /* // first skip white spaces */
            while (l[0] && (l[0] == ' ' || l[0] == ',' || l[0] == '\t')) l++; 
            if (l[0] == 0) return FALSE;
            if (sscanf(l, "%lf", &(xyz[1])) != 1) return FALSE;
            /* // then advance to next white space */
            while (l[0] && l[0] != ' ' && l[0] != ',' && l[0] != '\t') l++; 
        }
        /* // we expect the x coordinate */
        else if (p[0] == 'z') 
        {
            /* // first skip white spaces */
            while (l[0] && (l[0] == ' ' || l[0] == ',' || l[0] == '\t')) l++; 
            if (l[0] == 0) return FALSE;
            if (sscanf(l, "%lf", &(xyz[2])) != 1) return FALSE;
            /* // then advance to next white space */
            while (l[0] && l[0] != ' ' && l[0] != ',' && l[0] != '\t') l++; 
        }
        /* // we expect a string or a number that we don't care about */
        else if (p[0] == 's') 
        {
            /* // first skip white spaces */
            while (l[0] && (l[0] == ' ' || l[0] == ',' || l[0] == '\t')) l++; 
            if (l[0] == 0) return FALSE;
            /* // then advance to next white space */
            while (l[0] && l[0] != ' ' && l[0] != ',' && l[0] != '\t') l++; 
        }
        /* // we expect the intensity */
        else if (p[0] == 'i') 
        {
            /* // first skip white spaces */
            while (l[0] && (l[0] == ' ' || l[0] == ',' || l[0] == '\t')) l++; 
            if (l[0] == 0) return FALSE;
            if (sscanf(l, "%f", &temp_f) != 1) return FALSE;
            if (temp_f < 0.0f || temp_f > 65535.0f) 
                fprintf(stderr, 
                        "WARNING: intensity %g is out of range of unsigned short\n", 
                        temp_f);
            LASPoint_SetIntensity(point, (int)temp_f);
/*          point->intensity = (unsigned short)temp_f; */
            /* // then advance to next white space*/
            while (l[0] && l[0] != ' ' && l[0] != ',' && l[0] != '\t') l++; 
        }
        /* // we expect the scan angle */
        else if (p[0] == 'a') 
        {
            /* // first skip white spaces */
            while (l[0] && (l[0] == ' ' || l[0] == ',' || l[0] == '\t')) l++; 
            if (l[0] == 0) return FALSE;
            if (sscanf(l, "%f", &temp_f) != 1) return FALSE;
            if (temp_f < -128.0f || temp_f > 127.0f) 
                fprintf(stderr, 
                        "WARNING: scan angle %g is out of range of char\n", 
                        temp_f);
            LASPoint_SetScanAngleRank(point, (int)temp_f);
/*          point->scan_angle_rank = (char)temp_f; */
            /* // then advance to next white space*/
            while (l[0] && l[0] != ' ' && l[0] != ',' && l[0] != '\t') l++; 
        }
        /* // we expect the number of returns of given pulse */
        else if (p[0] == 'n') 
        {
            /* // first skip white spaces */
            while (l[0] && (l[0] == ' ' || l[0] == ',' || l[0] == '\t')) l++; 
            if (l[0] == 0) return FALSE;
            if (sscanf(l, "%d", &temp_i) != 1) return FALSE;
            if (temp_i < 0 || temp_i > 7) 
                fprintf(stderr, 
                        "WARNING: return number %d is out of range of three bits\n", 
                        temp_i);
            LASPoint_SetNumberOfReturns(point, temp_i);
/*          point->number_of_returns_of_given_pulse = temp_i & 7; */
            /* // then advance to next white space */
            while (l[0] && l[0] != ' ' && l[0] != ',' && l[0] != '\t') l++; 
        }
        /*  // we expect the number of the return */
        else if (p[0] == 'r')
        {
            /* // first skip white spaces */
            while (l[0] && (l[0] == ' ' || l[0] == ',' || l[0] == '\t')) l++; 
            if (l[0] == 0) return FALSE;
            if (sscanf(l, "%d", &temp_i) != 1) return FALSE;
            if (temp_i < 0 || temp_i > 7) 
                fprintf(stderr, 
                        "WARNING: return number %d is out of range of three bits\n", 
                        temp_i);
/*          point->return_number = temp_i & 7; */
            LASPoint_SetReturnNumber(point, temp_i);
            /* // then advance to next white space */
            while (l[0] && l[0] != ' ' && l[0] != ',' && l[0] != '\t') l++; 
        }
        /* // we expect the classification */
        else if (p[0] == 'c') 
        {
            /* // first skip white spaces */
            while (l[0] && (l[0] == ' ' || l[0] == ',' || l[0] == '\t')) l++; 
            if (l[0] == 0) return FALSE;
            if (sscanf(l, "%d", &temp_i) != 1) return FALSE;
            if (temp_i < 0 || temp_i > 255) 
                fprintf(stderr, 
                        "WARNING: classification %d is out of range of unsigned char\n", 
                        temp_i);
            LASPoint_SetClassification(point, temp_i);
/*          point->classification = (unsigned char)temp_i; */
            /* // then advance to next white space */
            while (l[0] && l[0] != ' ' && l[0] != ',' && l[0] != '\t') l++; 
        }
        /* // we expect the user data */
        else if (p[0] == 'u') 
        {
            /* // first skip white spaces */
            while (l[0] && (l[0] == ' ' || l[0] == ',' || l[0] == '\t')) l++; 
            if (l[0] == 0) return FALSE;
            if (sscanf(l, "%d", &temp_i) != 1) return FALSE;
            if (temp_i < 0 || temp_i > 255) 
                fprintf(stderr, 
                        "WARNING: user data %d is out of range of unsigned char\n", 
                        temp_i);
            
            LASPoint_SetUserData(point, temp_i);
/*              point->user_data = temp_i & 255; */
            /* // then advance to next white space */
            while (l[0] && l[0] != ' ' && l[0] != ',' && l[0] != '\t') l++; 
        }
        /* // we expect the point source ID */
        else if (p[0] == 'p') 
        {
            /* // first skip white spaces */
            while (l[0] && (l[0] == ' ' || l[0] == ',' || l[0] == '\t')) l++; 
            if (l[0] == 0) return FALSE;
            if (sscanf(l, "%d", &temp_i) != 1) return FALSE;
            if (temp_i < 0 || temp_i > 65535) 
                fprintf(stderr, 
                        "WARNING: point source ID %d is out of range of unsigned short\n", 
                        temp_i);
/*          point->point_source_ID = temp_i & 65535; */
            LASPoint_SetPointSourceId(point, temp_i);
            /*// then advance to next white space */
            while (l[0] && l[0] != ' ' && l[0] != ',' && l[0] != '\t') l++; 
        }
        /* // we expect the edge of flight line flag */
        else if (p[0] == 'e') 
        {
            /* // first skip white spaces */
            while (l[0] && (l[0] == ' ' || l[0] == ',' || l[0] == '\t')) l++; 
            if (l[0] == 0) return FALSE;
            if (sscanf(l, "%d", &temp_i) != 1) return FALSE;
            if (temp_i < 0 || temp_i > 1) 
                fprintf(stderr, 
                        "WARNING: edge of flight line flag %d is out of range of boolean flag\n", 
                        temp_i);
            LASPoint_SetFlightLineEdge(point, temp_i ? 1: 0);
/*          point->edge_of_flight_line = (temp_i ? 1 : 0); */
            /* // then advance to next white space */
            while (l[0] && l[0] != ' ' && l[0] != ',' && l[0] != '\t') l++; 
        }
        /* // we expect the direction of scan flag */
        else if (p[0] == 'd') 
        {
            /* // first skip white spaces */
            while (l[0] && (l[0] == ' ' || l[0] == ',' || l[0] == '\t')) l++; 
            if (l[0] == 0) return FALSE;
            if (sscanf(l, "%d", &temp_i) != 1) return FALSE;
            if (temp_i < 0 || temp_i > 1) 
                fprintf(stderr, 
                        "WARNING: direction of scan flag %d is out of range of boolean flag\n", 
                        temp_i);
            LASPoint_SetScanDirection(point, temp_i ? 1: 0);
/*          point->scan_direction_flag = (temp_i ? 1 : 0); */
            /* // then advance to next white space */
            while (l[0] && l[0] != ' ' && l[0] != ',' && l[0] != '\t') l++; 
        }
        /* // we expect the gps time */
        else if (p[0] == 't') 
        {
            /* // first skip white spaces */
            while (l[0] && (l[0] == ' ' || l[0] == ',' || l[0] == '\t')) l++; 
            if (l[0] == 0) return FALSE;
            if (sscanf(l, "%lf", gps_time) != 1) return FALSE;
            /* // then advance to next white space */
            while (l[0] && l[0] != ' ' && l[0] != ',' && l[0] != '\t') l++; 
        }
        /* // we expect the red channel of the RGB field */
        else if (p[0] == 'R') 
        {
            /* // first skip white spaces */
            while (l[0] && (l[0] == ' ' || l[0] == ',' || l[0] == '\t')) l++; 
            if (l[0] == 0) return FALSE;
            if (sscanf(l, "%d", &temp_i) != 1) return FALSE;
            LASColor_SetRed(color, temp_i);
            /* // then advance to next white space */
            while (l[0] && l[0] != ' ' && l[0] != ',' && l[0] != '\t') l++; 
        }
        /* // we expect the green channel of the RGB field */
        else if (p[0] == 'G') 
        {
            /* // first skip white spaces */
            while (l[0] && (l[0] == ' ' || l[0] == ',' || l[0] == '\t')) l++; 
            if (l[0] == 0) return FALSE;
            if (sscanf(l, "%d", &temp_i) != 1) return FALSE;
            LASColor_SetGreen(color, temp_i);
            /* // then advance to next white space */
            while (l[0] && l[0] != ' ' && l[0] != ',' && l[0] != '\t') l++; 
        }
        /* // we expect the blue channel of the RGB field */
        else if (p[0] == 'B') 
        {
            /* // first skip white spaces */
            while (l[0] && (l[0] == ' ' || l[0] == ',' || l[0] == '\t')) l++; 
            if (l[0] == 0) return FALSE;
            if (sscanf(l, "%d", &temp_i) != 1) return FALSE;
            LASColor_SetBlue(color, temp_i);
            /* // then advance to next white space */
            while (l[0] && l[0] != ' ' && l[0] != ',' && l[0] != '\t') l++; 
        }

        else
        {
        fprintf(stderr, "ERROR: next symbol '%s' unknown in parse control string\n", p);
        }
        p++;
    }
    LASPoint_SetColor(point, color);
    LASColor_Destroy(color);
    return TRUE;
}


int main(int argc, char *argv[])
{
    int i;
    int dry = FALSE;
    int verbose = FALSE;
    char* file_name_in = 0;
    char* file_name_out = 0;
    double xyz_min[3] = {0.0, 0.0, 0.0};
    double xyz_max[3] = {0.0, 0.0, 0.0};
    double xyz_scale[3] = {0.01,0.01,0.01};
    double xyz_offset[3] = {0.0,0.0,0.0};
    unsigned int number_of_point_records = 0;
    unsigned int number_of_points_by_return[8] = {0,0,0,0,0,0,0,0};
    char* parse_string = "xyz";
    int file_creation_day = 0;
    int file_creation_year = 0;
    char* system_identifier = 0;
    char* generating_software = 0;
#define MAX_CHARACTERS_PER_LINE 512
    char line[MAX_CHARACTERS_PER_LINE];
    double xyz[3];
    LASPointH point = NULL;
    double gps_time;
    FILE* file_in = NULL;
    char* parse_less = NULL;
    LASHeaderH header = NULL;
    LASWriterH writer = NULL;
    LASError err;
    int format = LAS_FORMAT_12;

    int xyz_min_quant[3] = {0, 0, 0};
    int xyz_max_quant[3] = {0, 0, 0};

    double xyz_min_dequant[3] = {0.0, 0.0, 0.0};
    double xyz_max_dequant[3] = {0.0, 0.0, 0.0};

    for (i = 1; i < argc; i++)
    {
        if (    strcmp(argv[i],"-h") == 0       ||
                strcmp(argv[i],"--help") == 0
            )
        {
            usage();
            exit(0);
        }
        else if (   strcmp(argv[i],"-v") == 0   ||
                    strcmp(argv[i],"--verbose") == 0
            )
        {
            verbose = TRUE;
        }    
        else if (   strcmp(argv[i],"-d") == 0       ||
                    strcmp(argv[i],"-dry") == 0     ||
                    strcmp(argv[i],"--dry") == 0
            )
        {
            dry = TRUE;
        }          
        else if (   strcmp(argv[i],"--parse") == 0  ||
                    strcmp(argv[i],"-parse") == 0   ||
                    strcmp(argv[i],"-p") == 0       
                )
        {
            i++;
            parse_string = argv[i];
        }
        else if (   strcmp(argv[i],"--scale") == 0  ||
                    strcmp(argv[i],"-scale") == 0   ||
                    strcmp(argv[i],"-s") == 0       
                )
        {

            i++;
            sscanf(argv[i], "%lf", &(xyz_scale[2]));
            xyz_scale[0] = xyz_scale[1] = xyz_scale[2];
        }
        else if (   strcmp(argv[i],"--xyz_scale") == 0  ||
                    strcmp(argv[i],"-xyz_scale") == 0    
                )

        {
            i++;
            sscanf(argv[i], "%lf", &(xyz_scale[0]));
            i++;
            sscanf(argv[i], "%lf", &(xyz_scale[1]));
            i++;
            sscanf(argv[i], "%lf", &(xyz_scale[2]));
        }
        else if (   strcmp(argv[i],"--xyz_offset") == 0  ||
                    strcmp(argv[i],"-xyz_offset") == 0    
                )
        {
            i++;
            sscanf(argv[i], "%lf", &(xyz_offset[0]));
            i++;
            sscanf(argv[i], "%lf", &(xyz_offset[1]));
            i++;
            sscanf(argv[i], "%lf", &(xyz_offset[2]));
        }
        else if (   strcmp(argv[i],"--input") == 0  ||
                    strcmp(argv[i],"-input") == 0   ||
                    strcmp(argv[i],"-i") == 0       ||
                    strcmp(argv[i],"-in") == 0
                )
        {
            i++;
            file_name_in = argv[i];
        }
        
        else if (   strcmp(argv[i],"--output") == 0  ||
                    strcmp(argv[i],"--out") == 0     ||
                    strcmp(argv[i],"-out") == 0     ||
                    strcmp(argv[i],"-o") == 0       
                )
        {
            i++;
            file_name_out = argv[i];
        }

        else if (   strcmp(argv[i],"--format") == 0   ||
                    strcmp(argv[i],"-f") == 0    ||
                    strcmp(argv[i],"-format") == 0 
                )
        {
            i++;
            if (strcmp(argv[i], "1.0") == 0) {
                format = LAS_FORMAT_10;
            }
            else if (strcmp(argv[i], "1.1") == 0) {
                format = LAS_FORMAT_11;
            } 
            else if (strcmp(argv[i], "1.2") == 0) {
                format = LAS_FORMAT_12;
            }
            else {
                LASError_Print("Format must be specified as 1.0, 1.1, or 1.2");
            }

        }
        else if (   strcmp(argv[i],"--system_identifier") == 0   ||
                    strcmp(argv[i],"-system_identifier") == 0   ||
                    strcmp(argv[i],"-s") == 0   ||
                    strcmp(argv[i],"-sys_id") == 0)
        {
            i++;
            system_identifier = (char*) malloc(31 * sizeof(char));
            strcpy(system_identifier, argv[i]);
        }

        else if (   strcmp(argv[i],"--generating_software") == 0   ||
                    strcmp(argv[i],"-generating_software") == 0   ||
                    strcmp(argv[i],"-g") == 0   ||
                    strcmp(argv[i],"-gen_soft") == 0)
        {
            i++;
            generating_software = (char*) malloc(31*sizeof(char));
            strcpy(generating_software, argv[i]);
        }

        else if (   strcmp(argv[i],"--file_creation") == 0   ||
                    strcmp(argv[i],"-file_creation") == 0)
        {
            i++;
            file_creation_day = (unsigned short)atoi(argv[i]);
            i++;
            file_creation_year = (unsigned short)atoi(argv[i]);
        }
        else if (   file_name_in == NULL && 
                    file_name_out == NULL
                )
        {
            file_name_in = argv[i];
        }
        else if (   file_name_in && 
                    file_name_out == NULL
                )
        {
            file_name_out = argv[i];
        }
        else 
        {
            fprintf(stderr, "ERROR: unknown argument '%s'\n",argv[i]);
            usage();
            exit(1);
        }
    }

    /* create output file name if none specified and no piped output requested */
    if (file_name_out == NULL && file_name_in != NULL)
    {
        int len = (int)strlen(file_name_in);
        file_name_out = LASCopyString(file_name_in);

        while (len > 0 && file_name_out[len] != '.')
        {
            len--;
        }
        file_name_out[len] = '.';
        file_name_out[len+1] = 'l';
        file_name_out[len+2] = 'a';
        file_name_out[len+3] = 's';
        file_name_out[len+4] = '\0';
    }


    /* make sure that input and output are not *both* piped */
    if (file_name_in == NULL && file_name_out == NULL)
    {
        LASError_Print("both input and output filenames are null!");
        usage();
        exit(1);
    }

    file_in = fopen(file_name_in, "r");

    if (file_in == NULL)
    {
        LASError_Print("could not open file to read for first pass");
        exit(1);
    }

    /* create a cheaper parse string that only looks for 'x' 'y' 'z' and 'r' */
    parse_less = LASCopyString(parse_string);
    for (i = 0; i < (int)strlen(parse_string); i++)
    {
        if (parse_less[i] != 'x' && 
            parse_less[i] != 'y' && 
            parse_less[i] != 'z' && 
            parse_less[i] != 'r') 
        {
            parse_less[i] = 's';
        }
    }

    do
    {
        parse_less[i] = '\0';
        printf("nuking %d for %c\n", i, parse_less[i]);
        i--;
    } while (parse_less[i] == 's');
    
    
    /* first pass to figure out the bounding box and number of returns */
    if (verbose) {
        fprintf(stderr, 
                "first pass over file '%s' with parse '%s'\n", 
                file_name_in, 
                parse_less);
    }

    /* read the first line */
    while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in))
    {
        point = LASPoint_Create();
        if (parse(parse_less, line, xyz, point, &gps_time))
        {
            /* init the bounding box */
            VecCopy3dv(xyz_min, xyz);
            VecCopy3dv(xyz_max, xyz);

            /* mark that we found the first point */
            number_of_point_records = 1;

            /* create return histogram */
            number_of_points_by_return[LASPoint_GetReturnNumber(point)]++;
            
            /* we can stop this loop */
            break;
        }
        else
        {
            fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", 
                    line, 
                    parse_less);

        }
        LASPoint_Destroy(point);
        point = NULL;
    }

    /* did we manage to parse a line? */
    if (number_of_point_records != 1)
    {
        fprintf(stderr, "ERROR: could not parse any lines with '%s'\n", 
                parse_less);
        exit(1);
    }

    /* loop over the remaining lines */
    while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in))
    {
        point = LASPoint_Create();
        if (parse(parse_less, line, xyz, point, &gps_time))
        {
            /* update bounding box */
            VecUpdateMinMax3dv(xyz_min, xyz_max, xyz);

            /* count points */
            number_of_point_records++;

            /* create return histogram */
            number_of_points_by_return[LASPoint_GetReturnNumber(point)]++;
        }
        else
        {
            fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", 
            line, 
            parse_less);

        }
        LASPoint_Destroy(point);
        point = NULL;
    }

    /* output some stats */
    if (verbose)
    {
        fprintf(stderr, 
                "npoints %d min %g %g %g max %g %g %g\n", 
                number_of_point_records, 
                xyz_min[0], 
                xyz_min[1], 
                xyz_min[2], 
                xyz_max[0], 
                xyz_max[1], 
                xyz_max[2]
                );
        fprintf(stderr, 
                "return histogram %d %d %d %d %d %d %d %d\n", 
                number_of_points_by_return[0], 
                number_of_points_by_return[1], 
                number_of_points_by_return[2], 
                number_of_points_by_return[3], 
                number_of_points_by_return[4], 
                number_of_points_by_return[5], 
                number_of_points_by_return[6], 
                number_of_points_by_return[7]
                );
    }

    /* close the input file */    
      fclose(file_in);

    /*  compute bounding box after quantization */
    for (i = 0; i < 3; i++)
    {
        xyz_min_quant[i] = (int)(0.5 + (xyz_min[i] - xyz_offset[i]) / xyz_scale[i]);
        xyz_max_quant[i] = (int)(0.5 + (xyz_max[i] - xyz_offset[i]) / xyz_scale[i]);
    }

    for (i = 0; i < 3; i++)
    {
        xyz_min_dequant[i] = xyz_offset[i] + (xyz_min_quant[i] * xyz_scale[i]);
        xyz_max_dequant[i] = xyz_offset[i] + (xyz_max_quant[i] * xyz_scale[i]);
    }

    /* make sure there is not sign flip */
    for (i = 0; i < 3; i++)
    {
        if ((xyz_min[i] > 0) != (xyz_min_dequant[i] > 0))
        {
            fprintf(stderr, 
                    "WARNING: quantization sign flip for %s min coord %g -> %g. use offset or scale up\n", 
                    (i ? (i == 1 ? "y" : "z") : "x"), 
                    xyz_min[i], 
                    xyz_min_dequant[i]
                   );
        }
        if ((xyz_max[i] > 0) != (xyz_max_dequant[i] > 0))
        {
            fprintf(stderr, 
                    "WARNING: quantization sign flip for %s max coord %g -> %g. use offset or scale up\n", 
                    (i ? (i == 1 ? "y" : "z") : "x"), 
                    xyz_max[i], 
                    xyz_max_dequant[i]
                   );
        }
    }

    /* populate the header */
      
    header = LASHeader_Create();

    if (system_identifier) LASHeader_SetSystemId(header, system_identifier);
    if (generating_software) LASHeader_SetSoftwareId(header, generating_software);
    LASHeader_SetCreationDOY(header, file_creation_day);
    LASHeader_SetCreationYear(header, file_creation_year);

    
    if (format == LAS_FORMAT_10) {
        LASHeader_SetVersionMinor(header, 0);
    } else if (format == LAS_FORMAT_11){
        LASHeader_SetVersionMinor(header, 1);
    } else if (format == LAS_FORMAT_12) {
        LASHeader_SetVersionMinor(header, 2);
    }      

    if (strstr(parse_string,"t") && (strstr(parse_string, "R") || strstr(parse_string, "G") ||strstr(parse_string, "B") ) )
    {
        fprintf(stderr, "Setting point format to 3, overriding version to 1.2 -- RGB + time\n");
        LASHeader_SetDataFormatId(header, 3);
        LASHeader_SetVersionMinor(header, 2);
    }
    else if ((strstr(parse_string, "R") || strstr(parse_string, "G") ||strstr(parse_string, "B") ) )
    {
        fprintf(stderr, "Setting point format to 2, overriding version to 1.2 -- RGB\n");
        LASHeader_SetDataFormatId(header, 2);
        LASHeader_SetVersionMinor(header, 2);
    }
    else if (strstr(parse_string,"t")) 
    {
        fprintf(stderr, "Setting point format to 1\n");
        LASHeader_SetDataFormatId(header, 1);
    }
    
    else
    {
        LASHeader_SetDataFormatId(header, 0);
    }
    LASHeader_SetPointRecordsCount(header, number_of_point_records);
    LASHeader_SetScale(header, xyz_scale[0], xyz_scale[1], xyz_scale[2]);
    LASHeader_SetOffset(header, xyz_offset[0], xyz_offset[1], xyz_offset[2]);
    LASHeader_SetMin(header, xyz_min_dequant[0], xyz_min_dequant[1], xyz_min_dequant[2]);
    LASHeader_SetMax(header, xyz_max_dequant[0], xyz_max_dequant[1], xyz_max_dequant[2]);
    LASHeader_SetPointRecordsByReturnCount(header, 0, number_of_points_by_return[1]);
    LASHeader_SetPointRecordsByReturnCount(header, 1, number_of_points_by_return[2]);
    LASHeader_SetPointRecordsByReturnCount(header, 2, number_of_points_by_return[3]);
    LASHeader_SetPointRecordsByReturnCount(header, 3, number_of_points_by_return[4]);
    LASHeader_SetPointRecordsByReturnCount(header, 4, number_of_points_by_return[5]);




    /* reopen input file for the second pass */
    file_in = fopen(file_name_in, "r");

    if (file_in == 0)
    {
        fprintf(stderr, "ERROR: could not open '%s' for second pass\n",file_name_in);
        exit(1);
    }

    /*
        because the output goes to a file we can do everything in a 
        single pass and compute the header information along the way 
    */
    
    /* open output file */
    printf("Creating file...\n");
    writer = LASWriter_Create(file_name_out, header, LAS_MODE_WRITE);
    if (!writer) { 
        LASError_Print("Could not open file for write mode ");
        exit(1);
    }     

    if (verbose) {
        fprintf(stderr, 
                "scanning %s with parse '%s' writing to %s\n", 
                file_name_in , 
                parse_string, 
                file_name_out
               );
    }
    

    /* read the first line */
    while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in))
    {
        point = LASPoint_Create();
        LASPoint_SetHeader(point, header);
        if (parse(parse_string, line, xyz, point, &gps_time))
        {
            /* init the bounding box */
            VecCopy3dv(xyz_min, xyz);
            VecCopy3dv(xyz_max, xyz);

            /* mark that we found the first point */
            number_of_point_records = 1;

            /* create return histogram */
            number_of_points_by_return[LASPoint_GetReturnNumber(point)]++;
            
            /* compute the quantized x, y, and z values */
            LASPoint_SetX(point, xyz[0]);
            LASPoint_SetY(point, xyz[1]);
            LASPoint_SetZ(point, xyz[2]);

            LASPoint_SetTime(point, gps_time); 

            /* write the first point */
            err = LASWriter_WritePoint(writer, point);
            if (err) {
                LASError_Print("could not write point");
                exit(1);
            }
              
            /* we can stop this loop */
            break;
        }
        else
        {
            fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", 
                    line, 
                    parse_string);
        }
        LASPoint_Destroy(point);
        point = NULL;
    }

    /* did we manage to parse a line? */
    if (number_of_point_records != 1)
    {
        fprintf(stderr, "ERROR: could not parse any lines with '%s'\n", 
                parse_less);
        exit(1);
    }

    /* loop over the remaining lines */
    while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in))
    {
        point = LASPoint_Create();
        LASPoint_SetHeader(point, header);
        
        if (parse(parse_string, line, xyz, point, &gps_time))
        {
            /* update bounding box */
            VecUpdateMinMax3dv(xyz_min, xyz_max, xyz);

            /* count points */
            number_of_point_records++;

            /* create return histogram */
            number_of_points_by_return[LASPoint_GetReturnNumber(point)]++;

            /* compute the quantized x, y, and z values */
            LASPoint_SetX(point, xyz[0]);
            LASPoint_SetY(point, xyz[1]);
            LASPoint_SetZ(point, xyz[2]);

            LASPoint_SetTime(point, gps_time); 
            
            /* write the first point */
            err = LASWriter_WritePoint(writer, point);
            if (err) {
                LASError_Print("could not write point");
                exit(1);
            }

        }
        else
        {
            fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", 
            line, 
            parse_string);
        }
        LASPoint_Destroy(point);
        point = NULL;
    }


    /* close up stuff */
    fclose(file_in);
    LASWriter_Destroy(writer);
    if (verbose)
    {
        fprintf(stderr, "done.\n");
    }


    return 0;
}