File: ui.c

package info (click to toggle)
gnuastro 0.24-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,360 kB
  • sloc: ansic: 185,444; sh: 15,785; makefile: 1,303; cpp: 9
file content (1304 lines) | stat: -rw-r--r-- 43,996 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
/*********************************************************************
Match - A program to match catalogs and WCS warps
Match is part of GNU Astronomy Utilities (Gnuastro) package.

Original author:
     Mohammad Akhlaghi <mohammad@akhlaghi.org>
Contributing author(s):
Copyright (C) 2017-2025 Free Software Foundation, Inc.

Gnuastro 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.

Gnuastro 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 Gnuastro. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
#include <config.h>

#include <argp.h>
#include <errno.h>
#include <error.h>
#include <stdio.h>
#include <string.h>

#include <gnuastro/fits.h>
#include <gnuastro/match.h>
#include <gnuastro/threads.h>

#include <gnuastro-internal/timing.h>
#include <gnuastro-internal/options.h>
#include <gnuastro-internal/checkset.h>
#include <gnuastro-internal/fixedstringmacros.h>

#include "main.h"

#include "ui.h"
#include "authors-cite.h"





/**************************************************************/
/*********      Argp necessary global entities     ************/
/**************************************************************/
/* Definition parameters for the Argp: */
const char *
argp_program_version = PROGRAM_STRING "\n"
                       GAL_STRINGS_COPYRIGHT
                       "\n\nWritten/developed by "PROGRAM_AUTHORS;

const char *
argp_program_bug_address = PACKAGE_BUGREPORT;

static char
args_doc[] = "ASTRdata";

const char
doc[] = GAL_STRINGS_TOP_HELP_INFO PROGRAM_NAME" matches catalogs of "
  "objects and (by default) will return the re-arranged matching "
  "inputs. The optional log file will return low-level information "
  "about the match (indexs and distances).\n"
  GAL_STRINGS_MORE_HELP_INFO
  /* After the list of options: */
  "\v"
  PACKAGE_NAME" home page: "PACKAGE_URL;




















/**************************************************************/
/*********    Initialize & Parse command-line    **************/
/**************************************************************/
static void
ui_initialize_options(struct matchparams *p,
                      struct argp_option *program_options,
                      struct argp_option *gal_commonopts_options)
{
  size_t i;
  struct gal_options_common_params *cp=&p->cp;


  /* Set the necessary common parameters structure. */
  cp->program_struct     = p;
  cp->poptions           = program_options;
  cp->program_name       = PROGRAM_NAME;
  cp->program_exec       = PROGRAM_EXEC;
  cp->program_bibtex     = PROGRAM_BIBTEX;
  cp->program_authors    = PROGRAM_AUTHORS;
  cp->numthreads         = gal_threads_number();
  cp->coptions           = gal_commonopts_options;


  /* Modify common options. */
  for(i=0; !gal_options_is_last(&cp->coptions[i]); ++i)
    {
      /* Select individually. */
      switch(cp->coptions[i].key)
        {
        case GAL_OPTIONS_KEY_HDU:
          cp->coptions[i].doc="Extension name or number of first input.";
          break;
        case GAL_OPTIONS_KEY_TYPE:
          cp->coptions[i].flags=OPTION_HIDDEN;
          break;
        }

      /* Select by group. */
      switch(cp->coptions[i].group)
        {
        case GAL_OPTIONS_GROUP_TESSELLATION:
          cp->coptions[i].doc=NULL; /* Necessary to remove title. */
          cp->coptions[i].flags=OPTION_HIDDEN;
          break;
        }
    }
}





/* Parse a single option: */
error_t
parse_opt(int key, char *arg, struct argp_state *state)
{
  struct matchparams *p = state->input;

  /* Pass 'gal_options_common_params' into the child parser.  */
  state->child_inputs[0] = &p->cp;

  /* In case the user incorrectly uses the equal sign (for example
     with a short format or with space in the long format, then 'arg'
     start with (if the short version was called) or be (if the long
     version was called with a space) the equal sign. So, here we
     check if the first character of arg is the equal sign, then the
     user is warned and the program is stopped: */
  if(arg && arg[0]=='=')
    argp_error(state, "incorrect use of the equal sign ('='). For short "
               "options, '=' should not be used and for long options, "
               "there should be no space between the option, equal sign "
               "and value");

  /* Set the key to this option. */
  switch(key)
    {
    /* Read the non-option tokens (arguments). */
    case ARGP_KEY_ARG:
      /* The user may give a shell variable that is empty! In that case
         'arg' will be an empty string! We don't want to account for such
         cases (and give a clear error that no input has been given). */
      if(p->input1name)
        {
          if(p->input2name)
            argp_error(state, "only two arguments (input files) "
                       "should be given, not any more");
          else
            { if(arg[0]!='\0') p->input2name=arg; }
        }
      else
        { if(arg[0]!='\0') p->input1name=arg; }
      break;


    /* This is an option, set its value. */
    default:
      return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);
    }

  return 0;
}





void *
ui_parse_match_type(struct argp_option *option, char *arg,
                    char *filename, size_t lineno, void *junk)
{
  char *str=NULL;
  uint8_t value=GAL_MATCH_ARRANGE_INVALID;

  /* If we are printing values. */
  if(lineno==-1)
    {
      /* The output must be an allocated string (will be 'free'd later). */
      value=*(uint8_t *)(option->value);
      switch(value)
        {
        case GAL_MATCH_ARRANGE_FULL:
          gal_checkset_allocate_copy("full", &str);
          break;
        case GAL_MATCH_ARRANGE_INNER:
          gal_checkset_allocate_copy("inner", &str);
          break;
        case GAL_MATCH_ARRANGE_OUTER:
          gal_checkset_allocate_copy("outer", &str);
          break;
        case GAL_MATCH_ARRANGE_OUTERWITHINAPERTURE:
          gal_checkset_allocate_copy("outer-within-aperture", &str);
          break;
        default:
          error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at '%s' "
                "to fix the problem. %u is not a recognized match type",
                __func__, PACKAGE_BUGREPORT, value);
        }
      return str;
    }

  /* If we are reading/parsing values. */
  else
    {
      /* If the option is already set, just return. */
      if(option->set) return NULL;

      /* Read the value. */
      if(      !strcmp(arg, "full") )  value = GAL_MATCH_ARRANGE_FULL;
      else if( !strcmp(arg, "inner") ) value = GAL_MATCH_ARRANGE_INNER;
      else if( !strcmp(arg, "outer") ) value = GAL_MATCH_ARRANGE_OUTER;
      else if( !strcmp(arg, "outer-within-aperture") )
        value = GAL_MATCH_ARRANGE_OUTERWITHINAPERTURE;
      else
        error_at_line(EXIT_FAILURE, 0, filename, lineno, "'%s' (value "
                      "to '%s' option) couldn't be recognized as a known "
                      "mathing type. Acceptable values are 'full', "
                      "'inner', 'outer' or 'outer-within-aperture'", arg,
                      option->name);
      *(uint8_t *)(option->value)=value;

      /* For no un-used variable warning. This function doesn't need the
         pointer. */
      return junk=NULL;
    }
}




















/**************************************************************/
/***************       Sanity Check         *******************/
/**************************************************************/
#define UI_ONLY_FITS_ERR \
  "Only output FITS tables are supported in Gnuastro's Match " \
  "program. This is because depending on the run-time options, " \
  "the number of output tables can be very different and it is " \
  "easiest to manage them (for the user and developer!) as " \
  "separate HDUs of a single FITS file. If you need a final " \
  "output table as plain-text, you can use Gnuastro's Table " \
  "program with a command like this: 'asttable --hdu=N table.fits " \
  "--output=table.txt' (to extract the N-th HDU (counting from " \
  "0) and write it as a plain-text table. You can see the list " \
  "of HDUs in a FITS file with 'astfits table.fits'"





/* Check ONLY the options. When arguments are involved, do the check
   in 'ui_check_options_and_arguments'. */
static void
ui_check_only_options(struct matchparams *p)
{
  /* Set the k-d tree mode. */
  if(      !strcmp(p->kdtree,"build")    )
    p->kdtreemode=MATCH_KDTREE_BUILD;
  else if( !strcmp(p->kdtree,"internal") )
    p->kdtreemode=MATCH_KDTREE_INTERNAL;
  else if( !strcmp(p->kdtree,"disable")  )
    p->kdtreemode=MATCH_KDTREE_DISABLE;
  else if( gal_fits_name_is_fits(p->kdtree) )
    p->kdtreemode=MATCH_KDTREE_FILE;
  else
    error(EXIT_FAILURE, 0, "'%s' is not valid for '--kdtree'. The "
          "following values are accepted: 'build' (to build the k-d "
          "tree in the file given to '--output'), 'internal' (to "
          "force internal usage of a k-d tree for the matching), "
          "'disable' (to not use a k-d tree at all), a FITS file "
          "name (the file to read a created k-d tree from)", p->kdtree);

  /* In case '--logasoutput' is given, we also need to activate '--log',
     otherwise, the output will not have a log-table at all. */
  if(p->logasoutput) p->cp.log=1;

  /* Make sure that the k-d tree build mode is not called with
     '--outcols'. */
  if( p->kdtreemode==MATCH_KDTREE_BUILD && (p->outcols || p->coord) )
    error(EXIT_FAILURE, 0, "the '--kdtree=build' option is "
          "incompatible with the '--outcols' or '--coord' options "
          "(because in the k-d tree building mode doesn't involve "
          "actual matching. It will only build k-d tree and write "
          "it to a file so it can be used in future matches)");

  /* Make sure that a HDU is also specified for the k-d tree when its an
     external file. */
  if( p->kdtreemode==MATCH_KDTREE_FILE && p->kdtreehdu==NULL )
    error(EXIT_FAILURE, 0, "no '--kdtreehdu' specified! When a FITS "
          "file is given to '--kdtree', you should specify its "
          "extension/HDU within the file using '--kdtreehdu'. You "
          "can either use the HDU name, or its number (counting "
          "from 0). If you aren't sure what HDUs exist in the file, "
          "you can use the 'astfits %s' command to see the full list",
          p->kdtree);

  /* For an outer match, we only operate in k-d tree mode. */
  if( (   p->arrange==GAL_MATCH_ARRANGE_OUTER
       || p->arrange==GAL_MATCH_ARRANGE_OUTERWITHINAPERTURE )
      && p->kdtreemode==MATCH_KDTREE_DISABLE )
    error(EXIT_FAILURE, 0, "the 'outer' arrangement only works with the "
          "k-d tree algorithm (in other words, it conflicts with "
          "'--kdtree=disable' option). Please set '--kdtree=internal' "
          "(if you want to build the k-d tree very time), or to "
          "'--kdtree=file.fits' (where 'file.fits' is an existing k-d "
          "tree that was built once and can be used many times for "
          "a faster result on a repeated reference catalog). See the "
          "description of the '--kdtree' option of the Match program "
          "in Gnuastro manual for more (by running 'info %s')",
          PROGRAM_EXEC);

  /* The '--notmatched' option is not compatible with some options. */
  if(p->notmatched)
    {
      if(p->arrange!=GAL_MATCH_ARRANGE_INNER)
        error(EXIT_FAILURE, 0, "the '--notmatched' option is only "
              "defined with the '--arrange=inner' match type");
    }

  /* Match will only produce FITS images. */
  if(p->cp.tableformat==GAL_TABLE_FORMAT_TXT)
    error(EXIT_FAILURE, 0, "the value of '--tableformat' cannot be 'txt'"
          UI_ONLY_FITS_ERR);
}





/* Two necessary catalogs: First:  Standard input, or a file.
                           Second: '--coord',      or a file.
 */
static void
ui_check_options_and_arguments(struct matchparams *p)
{
  /* When '--coord' is given, or we are in k-d tree building mode, there
     should be no second catalog (argument). */
  if(p->coord || p->kdtreemode==MATCH_KDTREE_BUILD)
    {
      /* Make sure no second argument is given. */
      if(p->input2name)
        error(EXIT_FAILURE, 0, "only one argument can be given with the "
              "'--coord' or '--kdtree=build' options");

      /* In case '--ccol2' is given. */
      if(p->ccol2 && p->cp.quiet==0)
        {
          /* Print a warning to let the user know the option will not be
             used (the user had probably confused things if they have given
             it). */
          error(EXIT_SUCCESS, 0, "WARNING: with '--coord' or '--kdtree', "
                "you only need to set '--ccol1' (since there is only a "
                "single input table), '--ccol2' is not needed");
        }
    }

  /* We need two input catalogs. */
  else
    {
      /* Without 'coord' atleast one input is necessary. */
      if(p->input1name==NULL)
        error(EXIT_FAILURE, 0, "no inputs!\n\n"
              "Two inputs are necessary. The first can be a file, or from "
              "the standard input (e.g., a pipe). The second can be a "
              "file, or its coordinates can be directly specified on the "
              "command-line with '--coord'");

      /* If the first input should be read from the standard input, the
         contents of 'input1name' actually belong to 'input2name'. */
      if(p->input2name==NULL)
        {
          p->input2name=p->input1name;
          p->input1name=NULL;
        }
    }

  /* If the first input is a FITS file, make sure its HDU is also given. */
  if( p->input1name
      && gal_fits_file_recognized(p->input1name)
      && p->cp.hdu==NULL )
    error(EXIT_FAILURE, 0, "no HDU for first input. When the input is "
          "a FITS file, a HDU must also be specified, you can use the "
          "'--hdu' ('-h') option and give it the HDU number (starting "
          "from zero), extension name, or anything acceptable by "
          "CFITSIO");
  if( p->input2name
      && gal_fits_file_recognized(p->input2name)
      && p->hdu2==NULL )
    error(EXIT_FAILURE, 0, "no HDU for second input. Please use "
          "the '--hdu2' ('-H') option and give it the HDU number "
          "(starting from zero), extension name, or anything "
          "acceptable by CFITSIO");
}




















/**************************************************************/
/***************       Preparations         *******************/
/**************************************************************/
/* The final aperture must have the following values:

       p->aperture[0]: Major axis length.
       p->aperture[1]: Axis ratio.
       p->aperture[2]: Position angle (relative to first dim).     */
static void
ui_read_columns_aperture_2d(struct matchparams *p)
{
  size_t apersize=3;
  gal_data_t *newaper=NULL;
  double *naper=NULL, *oaper=p->aperture->array;

  /* A general sanity check: the first two elements of aperture cannot be
     zero or negative. */
  if( oaper[0]<=0 )
    error(EXIT_FAILURE, 0, "the first value of '--aperture' cannot be "
          "zero or negative");
  if( p->aperture->size>1 && oaper[1]<=0 )
    error(EXIT_FAILURE, 0, "the second value of '--aperture' cannot be "
          "zero or negative");

  /* Will be needed in more than one case. */
  if(p->aperture->size!=apersize)
    {
      newaper=gal_data_alloc(NULL, GAL_TYPE_FLOAT64, 1, &apersize, NULL,
                             0, -1, 1, NULL, NULL, NULL);
      naper=newaper->array;
    }

  /* Different based on  */
  switch(p->aperture->size)
    {
    case 1:
      naper[0]=oaper[0];
      naper[1]=1;
      naper[2]=0;
      break;

    case 2:
      naper[0] = oaper[0]>oaper[1] ? oaper[0]          : oaper[1];
      naper[1] = oaper[0]>oaper[1] ? oaper[1]/oaper[0] : oaper[0]/oaper[1];
      naper[2] = oaper[0]>oaper[1] ? 0                 : 90;
      break;

    case 3:
      if(oaper[1]>1)
        error(EXIT_FAILURE, 0, "second value to '--aperture' is larger "
              "than one. When three numbers are given to this option, "
              "the second is the axis ratio (which must always be less "
              "than 1).");
      break;

    default:
      error(EXIT_FAILURE, 0, "%zu values given to '--aperture'. In 2D, "
            "this option can only take 1, 2, or 3 values",
            p->aperture->size);
    }

  /* If a new aperture was defined, then replace it with the exitsting
     one. */
  if(newaper)
    {
      gal_data_free(p->aperture);
      p->aperture=newaper;
    }
}





/* The 3D aperture must finally have these elements.

      p->aperture[0] = Major axis length.
      p->aperture[1] = Second axis ratio.
      p->aperture[2] = Third axis ratio.
      p->aperture[3] = First ZXZ Euler angle rotation.
      p->aperture[4] = Second ZXZ Euler angle rotation.
      p->aperture[5] = Third ZXZ Euler angle rotation.   */
static void
ui_read_columns_aperture_3d(struct matchparams *p)
{
  size_t apersize=6;
  gal_data_t *newaper=NULL;
  double *naper=NULL, *oaper=p->aperture->array;

  /* A general sanity check: the first two elements of aperture cannot be
     zero or negative. */
  if( oaper[0]<=0 )
    error(EXIT_FAILURE, 0, "the first value of '--aperture' cannot be "
          "zero or negative");
  if( p->aperture->size>2 && (oaper[1]<=0 || oaper[2]<=0) )
    error(EXIT_FAILURE, 0, "the second and third values of '--aperture' "
          "cannot be zero or negative");

  /* Will be needed in more than one case. */
  if(p->aperture->size!=apersize)
    {
      newaper=gal_data_alloc(NULL, GAL_TYPE_FLOAT64, 1, &apersize, NULL,
                             0, -1, 1, NULL, NULL, NULL);
      naper=newaper->array;
    }

  /* Different based on  */
  switch(p->aperture->size)
    {
    case 1:
      naper[0] = oaper[0];
      naper[1] = naper[2] = 1;
      naper[3] = naper[4] = naper[5] = 0;
      break;

    case 3:
      /* Major axis is along the first dimension, no rotation necessary. */
      if(oaper[0]>=oaper[1] && oaper[0]>=oaper[2])
        {
          naper[0] = oaper[0];
          naper[1] = oaper[1] / oaper[0];
          naper[2] = oaper[2] / oaper[0];
          naper[3] = naper[4] = naper[5] = 0;
        }

      /* Major axis is along the second dimension. So we want 'X' to be in
         the direction of 'y'. Therefore, just the first Eurler ZXZ
         rotation is necessary by 90 degrees. Here is how the rotated
         coordinates (X,Y,Z) look like (after one rotation about Z).

                  |z (before)                   Z|  /Y
                  |                              | /
                  |                  ==>         |/
                  .-------- y                    .------X
                 /
              x /

         You see how the major axis (X) now lies in the second original
         direction (y). The length along 'x' is now along 'Y' and the third
         hasn't changed. Note that we are talking about the semi-axis
         lengths (a scalar, not a vector), so +- is irrelevant. */
      else if(oaper[1]>=oaper[0] && oaper[1]>=oaper[2])
        {
          naper[0] = oaper[1];
          naper[1] = oaper[0] / oaper[1];
          naper[2] = oaper[2] / oaper[1];
          naper[3] = 90;
          naper[4] = naper[5] = 0;
        }

      /* The major axis is along the third dimension. So we want 'X' to
         point in the direction of 'z'. To get to that point, we need 90
         degree rotations in all three Euler ZXZ rotations.

              |z (before)     z'|  /y'            |y''                  |X
              |                 | /               |                     |
              |            ==>  |/        ==>     |         ==>         |
              .------ y         .------x'         .------x'      Y------.
             /                                   /                     /
          x /                                z''/                    Z/

         We thus see that the length along the second original direction
         (y) doesn't change stays in the second direction (Y). But the
         original first axis direction (x) is now represented by (Z). Note
         that we are talking about the semi-axis lengths (a scalar, not a
         vector), so +- is irrelevant. */
      else
        {
          naper[0] = oaper[2];
          naper[1] = oaper[1] / oaper[2];
          naper[2] = oaper[0] / oaper[2];
          naper[3] = naper[4] = naper[5] = 90;
        }

      break;

    case 6:
      if(oaper[1]>1 || oaper[2]>1)
        error(EXIT_FAILURE, 0, "atleast one of the second or third values "
              "to '--aperture' is larger than one. When size numbers are "
              "given to this option (in 3D), the second and third are the "
              "axis ratios (which must always be less than 1).");
      break;

    default:
      error(EXIT_FAILURE, 0, "%zu values given to '--aperture'. In 3D, "
            "this option can only take 1, 3, or 6 values. See the "
            "description of this option in the book for more with this "
            "command:\n\n"
            "    $ info %s\n", p->aperture->size, PROGRAM_EXEC);
    }

  /* If a new aperture was defined, then replace it with the exitsting
     one. */
  if(newaper)
    {
      gal_data_free(p->aperture);
      p->aperture=newaper;
    }
}





static size_t
ui_set_columns_sanity_check_read_aperture(struct matchparams *p)
{
  size_t ccol1n=0, ccol2n=0;

  /* Make sure the columns to match are given. */
  if(p->coord || p->kdtreemode==MATCH_KDTREE_BUILD)
    {
      if(p->ccol1==NULL)
        error(EXIT_FAILURE, 0, "no value given to '--ccol1' (necessary "
              "with '--coord')");
    }
  else
    {
      if(p->ccol1==NULL || p->ccol2==NULL)
        error(EXIT_FAILURE, 0, "both '--ccol1' and '--ccol2' must be "
              "given. They specify the columns containing the "
              "coordinates to match");
    }

  /* Make sure the same number of columns is given to both. Note that a
     second catalog is only necessary when we aren't building a k-d
     tree. */
  ccol1n = p->ccol1->size;
  if( p->kdtreemode!=MATCH_KDTREE_BUILD )
    {
      ccol2n = p->coord ? p->coord->size : p->ccol2->size;
      if(ccol1n!=ccol2n)
        error(EXIT_FAILURE, 0, "number of coordinates given to '--ccol1' "
              "(%zu) and '--%s' (%zu) must be equal.\n\n"
              "If you didn't call these options, run with '--checkconfig' "
              "to see which configuration file is responsible. You can "
              "always override the configuration file values by calling "
              "the option manually on the command-line", ccol1n,
              p->coord ? "coord" : "ccol2", ccol2n);
    }

  /* Read/check the aperture values. */
  if(p->aperture)
    {
      /* If we are in an outer arrangement, an aperture should not be
         given! */
      if(p->arrange==GAL_MATCH_ARRANGE_OUTER)
        error(EXIT_FAILURE, 0, "no aperture should be given in an "
              "\"outer\" arrangement. For more information, see the "
              "\"Arranging match output\" section of the manual "
              "(e.g., by running the command 'info gnuastro "
              "arranging')");

      /* Read the aperture depending on the number of dimensions. */
      switch(ccol1n)
        {
        case 1:
          if(p->aperture->size>1)
            error(EXIT_FAILURE, 0, "%zu values given to '--aperture'. "
                  "In a 1D match, this option can only take one value",
                  p->aperture->size);
          break;

        case 2: ui_read_columns_aperture_2d(p); break;
        case 3: ui_read_columns_aperture_3d(p); break;
        default:
          error(EXIT_FAILURE, 0, "%zu dimensional matches are not "
                "currently supported (maximum is 2 dimensions). The "
                "number of dimensions is deduced from the number of "
                "values given to '--ccol1' (or '--coord') and "
                "'--ccol2'", ccol1n);
        }
    }

  /* No aperture is given; this is only possible in the following
     modes. Otherwise, we need to abort the program and inform the user to
     provide an aperture. */
  else if ( p->kdtreemode != MATCH_KDTREE_BUILD
            && p->arrange != GAL_MATCH_ARRANGE_OUTER)
    error(EXIT_FAILURE, 0, "no matching aperture specified. Please "
          "use the '--aperture' option to define the acceptable "
          "aperture for matching the coordinates (in the same units "
          "as each dimension). An aperture is not necessary when you "
          "are building a k-d tree or when you want an outer match; "
          "but this is not any of those scenarios. Please run 'info "
          "%s' for more on the format of '--aperture'", PROGRAM_EXEC);

  /* Return the number of dimensions. */
  return ccol1n;
}





/* Save the manually given coordinates (with '--coord') in column format (a
   list of datasets). */
static gal_data_t *
ui_set_columns_from_coord(struct matchparams *p)
{
  size_t i, one=1;
  gal_data_t *out=NULL;
  double *coord=p->coord->array;

  /* Write each given value as a one-row table column (single element
     datasets that are linked) */
  for(i=0;i<p->coord->size;++i)
    {
      gal_list_data_add_alloc(&out, NULL, GAL_TYPE_FLOAT64, 1, &one, NULL,
                              0, -1, 1, NULL, NULL, NULL);
      *((double *)(out->array))=coord[i];
    }
  gal_list_data_reverse(&out);

  /* Return the list. */
  return out;
}





/* We want to keep the columns as double type. So what-ever their original
   type is, convert it. */
static gal_data_t *
ui_read_columns_to_double(struct matchparams *p, char *filename,
                          char *hdu, gal_list_str_t *cols,
                          size_t numcols, char *hdu_option_name)
{
  gal_data_t *tmp, *ttmp, *tout, *out=NULL;
  struct gal_options_common_params *cp=&p->cp;
  char *diff_cols_error="%s: the number of columns matched (%zu) "
    "differs from the number of usable calls to '--ccol1' (%zu). "
    "Please give more specific values to '--ccol1' (column "
    "numberes are the only identifiers guaranteed to be unique).";

  /* Read the columns. Note that the first input's name can be NULL (if the
     user intended to use the standrad input). Also note that this function
     is called more than one time, so if the Standard input is already read
     once, we don't want to write a blank list over it (the Standard input
     will be empty after being read). */
  if(p->stdinlines==NULL)
    p->stdinlines=gal_options_check_stdin(filename, p->cp.stdintimeout,
                                          "input");
  tout=gal_table_read(filename, hdu, filename ? NULL : p->stdinlines,
                      cols, cp->searchin, cp->ignorecase, cp->numthreads,
                      cp->minmapsize, p->cp.quietmmap, NULL,
                      hdu_option_name);

  /* A small sanity check. */
  if(gal_list_data_number(tout)!=numcols)
    error(EXIT_FAILURE, 0, diff_cols_error,
          gal_checkset_dataset_name(filename, hdu),
          gal_list_data_number(tout), numcols);

  /* Go over the columns and see if they are double or not. To keep things
     simple, we'll keep a new list even if all the types are float64.*/
  tmp=tout;
  while(tmp!=NULL)
    {
      /* If the coordinate column has more than one dimension (a vector
         column), the abort with an error: */
      if(tmp->ndim!=1)
        error(EXIT_FAILURE, 0, "only single-valued columns can be given "
              "to '--ccol1' or '--ccol2'");

      /* We need ot set the 'next' pointer  */
      ttmp=tmp->next;
      tmp->next=NULL;

      /* Correct the type if necessary. */
      if(tmp->type==GAL_TYPE_FLOAT64)
        gal_list_data_add(&out, tmp);
      else
        gal_list_data_add(&out,
                          gal_data_copy_to_new_type_free(tmp,
                                                         GAL_TYPE_FLOAT64) );

      /* Set 'tmp' to the initial 'next pointer. */
      tmp=ttmp;
    }

  /* The 'out' above is in reverse, so correct it and return */
  gal_list_data_reverse(&out);
  return out;
}




#define UI_READ_KDTREE_FIXED_MSG "You can construct a k-d tree "        \
  "for your first input table using the command below (just set your "  \
  "desired output name, and give that file to '--kdtree', in a later "  \
  "call with the same first input catalog):\n\n"                        \
  "    astmatch %s -h%s --ccol1=%s,%s --kdtree=build "                  \
  "--output=KDTREE.fits\n\n"                                            \
  "To learn more about the expected format of k-d trees in Gnuastro, "  \
  "please run this command: 'info gnuastro k-d'"

static void
ui_read_kdtree(struct matchparams *p)
{
  size_t *sizetarr;
  char **strarr1 = p->ccol1->array;
  gal_data_t *keysll=gal_data_array_calloc(1);

  /* Read the external k-d tree. */
  p->kdtreedata=gal_table_read(p->kdtree, p->kdtreehdu, NULL,
                               NULL, GAL_TABLE_SEARCH_NAME, 1,
                               p->cp.numthreads, p->cp.minmapsize,
                               p->cp.quietmmap, NULL, "--kdtreehdu");

  /* It has to have only two columns, with an unsigned 32-bit integer
     type in each. */
  if( gal_list_data_number(p->kdtreedata)!=2 )
    error(EXIT_FAILURE, 0, "%s (hdu: %s, that was given to "
          "'--kdtree') has %zu column(s), but should contain 2 "
          "columns. " UI_READ_KDTREE_FIXED_MSG,
          p->kdtree, p->kdtreehdu, gal_list_data_number(p->kdtreedata),
          p->input1name, p->cp.hdu, strarr1[0], strarr1[1]);
  if( p->kdtreedata->type!=GAL_TYPE_UINT32
      || p->kdtreedata->next->type!=GAL_TYPE_UINT32 )
    error(EXIT_FAILURE, 0, "%s (hdu: %s, that was given to "
          "'--kdtree') doesn't have unsigned 32-bit integer columns. "
          UI_READ_KDTREE_FIXED_MSG, p->kdtree, p->kdtreehdu,
          p->input1name, p->cp.hdu, strarr1[0], strarr1[1]);
  if( p->kdtreedata->size!=p->cols1->size )
    error(EXIT_FAILURE, 0, "%s (hdu: %s, that was given to "
          "'--kdtree') doesn't have the same number of rows as "
          "%s, which is the first given input. "
          UI_READ_KDTREE_FIXED_MSG, p->kdtree, p->kdtreehdu,
          gal_fits_name_save_as_string(p->input1name, p->cp.hdu),
          p->input1name, p->cp.hdu, strarr1[0], strarr1[1]);

  /* Read the k-d tree root. */
  keysll[0].type=GAL_TYPE_SIZE_T;
  keysll[0].name=MATCH_KDTREE_ROOT_KEY;
  gal_fits_key_read(p->kdtree, p->kdtreehdu, keysll, 0, 0, "--kdtreehdu");
  if(keysll[0].status)
    error(EXIT_FAILURE, 0, "%s (hdu: %s, that was given to "
          "'--kdtree') doesn't have the '%s' keyword, or it "
          "couldn't be read as a number. " UI_READ_KDTREE_FIXED_MSG,
          p->kdtree, p->kdtreehdu, MATCH_KDTREE_ROOT_KEY,
          p->input1name, p->cp.hdu, strarr1[0], strarr1[1]);
  sizetarr=keysll[0].array;
  p->kdtreeroot=sizetarr[0];

  /* Clean up: since the 'name' component wasn't allocated in this example,
     we should set it to NULL before calling 'gal_data_array_free'. */
  keysll[0].name=NULL;
  gal_data_array_free(keysll, 1, 1);
}





/* Read catalog columns */
static void
ui_read_columns(struct matchparams *p)
{
  size_t i, ndim;
  char **strarr1, **strarr2;
  gal_list_str_t *cols1=NULL, *cols2=NULL;

  /* Basic sanity checks and reading of aperture values. */
  ndim=ui_set_columns_sanity_check_read_aperture(p);

  /* Convert the array of strings to a list of strings for the column
     names. */
  strarr1 = p->ccol1->array;
  strarr2 = ( (p->coord || p->kdtreemode==MATCH_KDTREE_BUILD)
              ? NULL
              : p->ccol2->array );
  for(i=0;i<ndim;++i)
    {
      gal_list_str_add(&cols1, strarr1[i], 1);
      if(strarr2) gal_list_str_add(&cols2, strarr2[i], 1);
    }
  gal_list_str_reverse(&cols1);
  if(cols2) gal_list_str_reverse(&cols2);

  /* Read-in the columns. */
  p->cols1=ui_read_columns_to_double(p, p->input1name, p->cp.hdu,
                                     cols1, ndim, "--hdu");
  if( p->kdtreemode!=MATCH_KDTREE_BUILD )
    p->cols2=( p->coord
               ? ui_set_columns_from_coord(p)
               : ui_read_columns_to_double(p, p->input2name, p->hdu2,
                                           cols2, ndim, "--hdu2") );

  /* If an external k-d tree is given, read it and make sure it has the
     same number of rows as the first input and the proper datatype. */
  if( p->kdtreemode==MATCH_KDTREE_FILE )
    ui_read_kdtree(p);

  /* If we are in k-d tree based matching and the second dataset is smaller
     than the first, and we are not in an outer-match (where the order of
     the inputs matter) print a warning to let the user know that the speed
     can be greatly improved if they swap the two. */
  if( !p->cp.quiet
      && p->kdtreemode!=MATCH_KDTREE_BUILD
      && p->kdtreemode!=MATCH_KDTREE_DISABLE
      && p->cols1->size > (2*p->cols2->size)
      && p->arrange!=GAL_MATCH_ARRANGE_OUTER)
    error(EXIT_SUCCESS, 0, "TIP: the matching speed will improve "
          "if you swap the two inputs. Recall that for an inner or "
          "full match, the order of inputs does not matter. "
          "In the k-d tree based matching, a k-d tree will be "
          "constructed for the first input and parallelized matching "
          "will occur on the second input's rows. Fewer first-input "
          "rows therefore mean a smaller k-d tree (which is easier "
          "to build), and more second-input rows will be better "
          "distributed in your system's processing threads");

  /* Free the extra spaces. */
  gal_list_str_free(cols1, 1);
  gal_list_str_free(cols2, 1);
}





static void
ui_preparations_out_cols_read(gal_list_str_t **list, char *string)
{
  /* Make sure the string is not empty. */
  if(string[0]=='\0')
    error(EXIT_FAILURE, 0, "the value(s) given to '--outcols' should "
          "a column identifier (counter, name or '_all') after the 'a' "
          "or 'b'");

  /* Add the string to the list of columns for this column. */
  gal_list_str_add(list, string, 0);
}





static void
ui_preparations_out_cols(struct matchparams *p)
{
  char *col;
  void *rptr;
  int goodvalue;
  gal_data_t *read;
  uint8_t readtype;
  gal_list_str_t *ocol;
  size_t one=1, ndim=p->coord?p->coord->size:0;

  /* Go over all the values and put the respective column identifier in the
     proper list. */
  for(ocol=p->outcols; ocol!=NULL; ocol=ocol->next)
    {
      /* For easy reading. */
      col=ocol->v;

      /* In no-match mode, the same column will be used from both catalogs
         so things are easier. */
      if(p->notmatched)
        {
          gal_list_str_add(&p->acols, col, 0);
          gal_list_str_add(&p->bcols, col, 0);
        }

      /* In match mode, we need to know which column should come from which
         catalog. */
      else
        switch(col[0])
          {
          case 'a':
            ui_preparations_out_cols_read(&p->acols, col+1);
            break;
          case 'b':
            /* With '--coord', only numbers that are smaller than the
               number of the dimensions are acceptable. */
            if(p->coord)
              {
                goodvalue=0;
                rptr=gal_type_string_to_number(col+1, &readtype);
                if(rptr)
                  {
                    read=gal_data_alloc(rptr, readtype, 1, &one, NULL, 0,
                                        -1, 1, NULL, NULL, NULL);
                    if(gal_type_is_int(readtype))
                      {
                        read=gal_data_copy_to_new_type_free(read,
                                                            GAL_TYPE_LONG);
                        if( *((long *)(read->array)) <= ndim )
                          goodvalue=1;
                      }
                    gal_data_free(read);
                  }
                if(goodvalue==0)
                  error(EXIT_FAILURE, 0, "bad value to second catalog "
                        "column (%s) of '--outcols'.\n\n"
                        "With the '--coord' option, the second catalog "
                        "is assumed to have a single row and the given "
                        "number of columns. Therefore when using "
                        "'--outcols', only integers that are less than "
                        "the number of dimensions (%zu in this case) "
                        "are acceptable", col+1, ndim);
              }
            ui_preparations_out_cols_read(&p->bcols, col+1);
            break;
          default:
            error(EXIT_FAILURE, 0, "'%s' is not a valid value for "
                  "'--outcols'.\n\n"
                  "The first character of each value to this option "
                  "must be either 'a' or 'b'. The former specifies a "
                  "column from the first input and the latter a "
                  "column from the second. The characters after them "
                  "can be any column identifier (number, name, or "
                  "regular expression). For more on column selection, "
                  "please run this command:\n\n"
                  "    $ info gnuastro \"Selecting table columns\"\n",
                  col);
          }
    }

  /* Reverse the lists so they correspond to the input order. */
  gal_list_str_reverse(&p->acols);
  gal_list_str_reverse(&p->bcols);
}





static void
ui_preparations_out_name(struct matchparams *p)
{
  /* To temporarily keep the original value. */
  char *refname = p->input1name ? p->input1name : p->input2name;

  /* If the output name is set, make sure it is a FITS file, otherwise, set
     a name from the input. */
  if(p->cp.output)
    {
      if( ! gal_fits_name_is_fits(p->cp.output) )
        error(EXIT_FAILURE, 0, "'%s' is not a recognized FITS file "
              "name. " UI_ONLY_FITS_ERR, p->cp.output);
    }
  else
    p->cp.output=gal_checkset_automatic_output(&p->cp, refname,
                                               "-matched.fits");

  /* Make sure a file with this name doesn't exist. */
  gal_checkset_writable_remove(p->cp.output, NULL, 0, p->cp.dontdelete);
}





static void
ui_preparations(struct matchparams *p)
{
  int intype;

  /* Since Match only works on tables, 'searchin' is necessary. */
  if(p->cp.searchin==0)
    error(EXIT_FAILURE, 0, "no '--searchin' option specified. Please "
          "run the following command for more information:\n\n"
          "    $ info gnuastro \"selecting table columns\"\n");

  /* We will base the mode on the first input, then check with the
     second. Note that when the first is from standard input (it is
     'NULL'), then we go into catalog mode because currently we assume
     standard input is only for plain text and WCS matching is not defined
     on plain text. */
  if( p->input1name && gal_fits_file_recognized(p->input1name) )
    {
      intype=gal_fits_hdu_format(p->input1name, p->cp.hdu, "--hdu");
      if(intype==IMAGE_HDU)
        error(EXIT_FAILURE, 0, "%s: not a catalog",
              gal_checkset_dataset_name(p->input1name, p->cp.hdu));
    }

  /* Same check for second input. */
  if(p->input2name && gal_fits_file_recognized(p->input2name))
    {
      intype=gal_fits_hdu_format(p->input2name, p->hdu2, "--hdu2");
      if(intype==IMAGE_HDU)
        error(EXIT_FAILURE, 0, "%s: not a catalog",
              gal_checkset_dataset_name(p->input2name, p->hdu2));
    }

  /* Read the input columns and prepare output columns. */
  ui_read_columns(p);
  if(p->outcols)
    ui_preparations_out_cols(p);

  /* Set the output filename. */
  ui_preparations_out_name(p);
}



















/**************************************************************/
/************         Set the parameters          *************/
/**************************************************************/
void
ui_read_check_inputs_setup(int argc, char *argv[], struct matchparams *p)
{
  size_t nthreads;
  struct gal_options_common_params *cp=&p->cp;


  /* Include the parameters necessary for argp from this program ('args.h')
     and for the common options to all Gnuastro ('commonopts.h'). We want
     to directly put the pointers to the fields in 'p' and 'cp', so we are
     simply including the header here to not have to use long macros in
     those headers which make them hard to read and modify. This also helps
     in having a clean environment: everything in those headers is only
     available within the scope of this function. */
#include <gnuastro-internal/commonopts.h>
#include "args.h"


  /* Initialize the options and necessary information.  */
  ui_initialize_options(p, program_options, gal_commonopts_options);


  /* Read the command-line options and arguments. */
  errno=0;
  if(argp_parse(&thisargp, argc, argv, 0, 0, p))
    error(EXIT_FAILURE, errno, "parsing arguments");


  /* Read the configuration files and set the common values. */
  gal_options_read_config_set(&p->cp);


  /* Sanity check only on options. */
  ui_check_only_options(p);


  /* Print the option values if asked. Note that this needs to be done
     after the option checks so un-sane values are not printed in the
     output state. */
  gal_options_print_state(&p->cp);


  /* Check that the options and arguments fit well with each other. Note
     that arguments don't go in a configuration file. So this test should
     be done after (possibly) printing the option values. */
  ui_check_options_and_arguments(p);


  /* Read/allocate all the necessary starting arrays. */
  ui_preparations(p);


  /* If the output is a FITS table, prepare all the options as FITS
     keywords to later write in output. */
  gal_options_as_fits_keywords(&p->cp);


  /* Let the user know that processing has started. */
  if(!p->cp.quiet)
    {
      printf(PROGRAM_NAME" "PACKAGE_VERSION" started on %s",
             ctime(&p->rawtime));
      nthreads=p->kdtreemode==MATCH_KDTREE_DISABLE ? 1 : p->cp.numthreads;
      printf("  - Using %zu CPU thread%s\n", nthreads,
             ( p->kdtreemode==MATCH_KDTREE_DISABLE
               ? " (sort-based match only uses a single thread)" : ""));
      printf("  - Input-1: %s; %zu rows\n",
             gal_fits_name_save_as_string(p->input1name, p->cp.hdu),
             p->cols1->size);
      if(p->kdtreemode==MATCH_KDTREE_FILE)
        printf("  - Input-1 k-d tree: %s\n",
               gal_fits_name_save_as_string(p->kdtree, p->kdtreehdu));
      if(p->kdtreemode!=MATCH_KDTREE_BUILD)
        printf("  - Input-2: %s; %zu rows\n",
               p->coord ? "from --coord"
               : gal_fits_name_save_as_string(p->input2name, p->hdu2),
               p->cols2->size);
    }
}




















/**************************************************************/
/************      Free allocated, report         *************/
/**************************************************************/
void
ui_free_report(struct matchparams *p, struct timeval *t1)
{
  /* Free the allocated arrays: */
  free(p->cp.hdu);
  free(p->aperture);
  free(p->cp.output);
  gal_data_free(p->ccol1);
  gal_data_free(p->ccol2);
  gal_list_data_free(p->cols1);
  gal_list_data_free(p->cols2);
  if(p->kdtree) free(p->kdtree);
  gal_list_str_free(p->acols, 0);
  gal_list_str_free(p->bcols, 0);
  gal_list_str_free(p->outcols, 1);
  if(p->kdtreehdu) free(p->kdtreehdu);
  gal_list_str_free(p->stdinlines, 1);

  /* Print the final message. */
  if(!p->cp.quiet)
    gal_timing_report(t1, PROGRAM_NAME" finished in: ", 0);
}