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
|
/*
# This file is part of the Astrometry.net suite.
# Licensed under a 3-clause BSD style license - see LICENSE
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "startree.h"
#include "fitstable.h"
#include "boilerplate.h"
#include "errors.h"
#include "log.h"
#include "fitsioutils.h"
const char* OPTIONS = "hvL:d:t:bsSci:o:R:D:PTkn:";
void printHelp(char* progname) {
BOILERPLATE_HELP_HEADER(stdout);
printf("\nUsage: %s\n"
" -i <input-fits-catalog-name>\n"
" -o <output-star-kdtree-name>\n"
" [-R <ra-column-name>]: name of RA in FITS table (default RA)\n"
" [-D <dec-column-name>]: name of DEC in FITS table (default DEC)\n"
" [-b]: build bounding boxes (default: splitting planes)\n"
" [-L Nleaf]: number of points in a kdtree leaf node (default 25)\n"
" [-t <tree type>]: {double,float,u32,u16}, default u32.\n"
" [-d <data type>]: {double,float,u32,u16}, default u32.\n"
" [-S]: include separate splitdim array\n"
" [-c]: run kdtree_check on the resulting tree\n"
" [-P]: unpermute tree + tag-along data\n"
" [-T]: write tag-along table as first extension HDU\n"
" [-k]: keep RA,Dec columns in tag-along table\n"
" [-n <name>]: kd-tree name (default \"stars\")\n"
" [-v]: +verbose\n"
"\n", progname);
}
int main(int argc, char *argv[]) {
int argidx, argchar;
startree_t* starkd;
fitstable_t* cat;
fitstable_t* tag;
int Nleaf = 0;
char* skdtfn = NULL;
char* catfn = NULL;
char* progname = argv[0];
char* racol = NULL;
char* deccol = NULL;
int loglvl = LOG_MSG;
char* treename = NULL;
int datatype = 0;
int treetype = 0;
int buildopts = 0;
anbool checktree = FALSE;
anbool unpermute = FALSE;
anbool remove_radec = TRUE;
u32* perm = NULL;
anbool tagalong_first = FALSE;
if (argc <= 2) {
printHelp(progname);
return 0;
}
while ((argchar = getopt (argc, argv, OPTIONS)) != -1)
switch (argchar) {
case 'T':
tagalong_first = TRUE;
break;
case 'n':
treename = optarg;
break;
case 'k':
remove_radec = FALSE;
break;
case 'P':
unpermute = TRUE;
break;
case 'R':
racol = optarg;
break;
case 'D':
deccol = optarg;
break;
case 'c':
checktree = TRUE;
break;
case 'L':
Nleaf = (int)strtoul(optarg, NULL, 0);
break;
case 'i':
catfn = optarg;
break;
case 'o':
skdtfn = optarg;
break;
case 't':
treetype = kdtree_kdtype_parse_tree_string(optarg);
break;
case 'd':
datatype = kdtree_kdtype_parse_data_string(optarg);
break;
case 'b':
buildopts |= KD_BUILD_BBOX;
break;
case 's':
buildopts |= KD_BUILD_SPLIT;
break;
case 'S':
buildopts |= KD_BUILD_SPLITDIM;
break;
case 'v':
loglvl++;
break;
case '?':
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
case 'h':
printHelp(progname);
return 0;
default:
return -1;
}
if (optind < argc) {
for (argidx = optind; argidx < argc; argidx++)
fprintf (stderr, "Non-option argument %s\n", argv[argidx]);
printHelp(progname);
exit(-1);
}
if (!(catfn && skdtfn)) {
printHelp(progname);
exit(-1);
}
log_init(loglvl);
fits_use_error_system();
logmsg("Building star kdtree: reading %s, writing to %s\n", catfn, skdtfn);
logverb("Reading star catalogue...");
cat = fitstable_open(catfn);
if (!cat) {
ERROR("Couldn't read catalog");
exit(-1);
}
logmsg("Got %i stars\n", fitstable_nrows(cat));
starkd = startree_build(cat, racol, deccol, datatype, treetype,
buildopts, Nleaf, argv, argc);
if (!starkd) {
ERROR("Failed to create star kdtree");
exit(-1);
}
if (checktree) {
logverb("Checking tree...\n");
if (kdtree_check(starkd->tree)) {
ERROR("kdtree_check failed!");
exit(-1);
}
}
if (treename) {
free(starkd->tree->name);
starkd->tree->name = strdup(treename);
}
if (unpermute) {
perm = starkd->tree->perm;
starkd->tree->perm = NULL;
}
if (tagalong_first) {
logmsg("Writing tag-along data...\n");
tag = fitstable_open_for_writing(skdtfn);
if (fitstable_write_primary_header(tag)) {
ERROR("Failed to write primary header");
exit(-1);
}
if (startree_write_tagalong_table(cat, tag, racol, deccol,
(int*)perm, remove_radec)) {
ERROR("Failed to write tag-along table");
exit(-1);
}
if (fitstable_close(tag)) {
ERROR("Failed to close tag-along data");
exit(-1);
}
// Append kd-tree
logverb("Appending kd-tree structure...\n");
FILE* fid = fopen(skdtfn, "r+b");
if (!fid) {
SYSERROR("Failed to open startree output file to append kd-tree: %s", skdtfn);
exit(-1);
}
if (fseeko(fid, 0, SEEK_END)) {
SYSERROR("Failed to seek to the end of the startree file to append kd-tree: %s", skdtfn);
exit(-1);
}
off_t off = ftello(fid);
printf("Offset to write starkd: %lu\n", (unsigned long)off);
if (startree_append_to(starkd, fid)) {
ERROR("Failed to append star kdtree");
exit(-1);
}
startree_close(starkd);
if (fclose(fid)) {
SYSERROR("Failed to close star kdtree file after appending tree\n");
exit(-1);
}
} else {
if (startree_write_to_file(starkd, skdtfn)) {
ERROR("Failed to write star kdtree");
exit(-1);
}
startree_close(starkd);
// Append tag-along table.
logmsg("Writing tag-along data...\n");
tag = fitstable_open_for_appending(skdtfn);
if (startree_write_tagalong_table(cat, tag, racol, deccol,
(int*)perm, remove_radec)) {
ERROR("Failed to write tag-along table");
exit(-1);
}
if (fitstable_close(tag)) {
ERROR("Failed to close tag-along data");
exit(-1);
}
}
fitstable_close(cat);
return 0;
}
|