File: engine-main.c

package info (click to toggle)
astrometry.net 0.93%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,372 kB
  • sloc: ansic: 163,192; python: 18,357; makefile: 1,522; sh: 138; cpp: 78; pascal: 67; awk: 56; perl: 9
file content (349 lines) | stat: -rw-r--r-- 10,213 bytes parent folder | download | duplicates (2)
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
/*
 # This file is part of the Astrometry.net suite.
 # Licensed under a 3-clause BSD style license - see LICENSE
 */

/**
 * Accepts an augmented xylist that describes a field or set of fields to solve.
 * Reads a config file to find local indices, and merges information about the
 * indices with the job description to create an input file for 'onefield'.  Runs
 * and merges the results.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <time.h>
#include <libgen.h>
#include <getopt.h>
#include <dirent.h>
#include <assert.h>
#include <glob.h>

// Some systems (Solaris) don't have these glob symbols.  Don't really need.
#ifndef GLOB_BRACE
#define GLOB_BRACE 0
#endif
#ifndef GLOB_TILDE
#define GLOB_TILDE 0
#endif

#include "os-features.h"
#include "tic.h"
#include "fileutils.h"
#include "ioutils.h"
#include "bl.h"
#include "an-bool.h"
#include "solver.h"
#include "math.h"
#include "fitsioutils.h"
#include "solverutils.h"
#include "onefield.h"
#include "log.h"
#include "errors.h"
#include "engine.h"
#include "an-opts.h"
#include "gslutils.h"

#include "datalog.h"

static an_option_t myopts[] = {
    {'h', "help", no_argument, NULL, "print this help"},
    {'v', "verbose", no_argument, NULL, "+verbose"},
    {'c', "config",  required_argument, "file",
     "Use this config file (default: \"astrometry.cfg\" in the directory ../etc/ relative to the directory containing the \"astrometry-engine\" executable); 'none' for no config file"},
    {'d', "base-dir",  required_argument, "dir", 
     "set base directory of all output filenames."},
    {'C', "cancel",  required_argument, "file", 
     "quit solving if this file appears" },
    {'s', "solved",  required_argument, "file",
     "write to this file when a field is solved"},
    {'E', "to-stderr", no_argument, NULL,
     "send log message to stderr"},
    {'f', "inputs-from", required_argument, "file",
     "read input filenames from the given file, \"-\" for stdin"},
    {'i', "index", required_argument, "file(s)",
     "use the given index files (in addition to any specified in the config file); put in quotes to use wildcards, eg: \" -i 'index-*.fits' \""},
    {'p', "in-parallel", no_argument, NULL,
     "run the index files in parallel"},
    {'D', "data-log file", required_argument, "file",
     "log data to the given filename"},
    {'j', "job-id", required_argument, "jobid",
     "IGNORED; purely to allow process to contain the job id!"},
};

static void print_help(const char* progname, bl* opts) {
    printf("Usage:   %s [options] <augmented xylist (axy) file(s)>\n", progname);
    opts_print_help(opts, stdout, NULL, NULL);
}

FILE* datalogfid = NULL;
static void close_datalogfid() {
    if (datalogfid) {
        data_log_end();
        if (fclose(datalogfid)) {
            SYSERROR("Failed to close data log file");
        }
    }
}

int main(int argc, char** args) {
    char* default_configfn = "astrometry.cfg";
    char* default_config_path = "../etc";

    int c;
    char* configfn = NULL;
    int i;
    engine_t* engine;
    char* mydir = NULL;
    char* basedir = NULL;
    char* me;
    anbool help = FALSE;
    sl* strings = sl_new(4);
    char* cancelfn = NULL;
    char* solvedfn = NULL;
    int loglvl = LOG_MSG;
    anbool tostderr = FALSE;
    char* infn = NULL;
    FILE* fin = NULL;
    anbool fromstdin = FALSE;

    bl* opts = opts_from_array(myopts, sizeof(myopts)/sizeof(an_option_t), NULL);
    sl* inds = sl_new(4);

    char* datalog = NULL;

    engine = engine_new();

    while (1) {
        c = opts_getopt(opts, argc, args);
        if (c == -1)
            break;
        switch (c) {
	case 'j':
	    break;
        case 'D':
            datalog = optarg;
            break;
        case 'p':
            engine->inparallel = TRUE;
            break;
        case 'i':
            sl_append(inds, optarg);
            break;
        case 'd':
            basedir = optarg;
            break;
        case 'f':
            infn = optarg;
            fromstdin = streq(infn, "-");
            break;
        case 'E':
            tostderr = TRUE;
            break;
        case 'h':
            help = TRUE;
            break;
        case 'v':
            loglvl++;
            break;
        case 's':
            solvedfn = optarg;
        case 'C':
            cancelfn = optarg;
            break;
        case 'c':
            configfn = strdup(optarg);
            break;
        case '?':
            break;
        default:
            printf("Unknown flag %c\n", c);
            exit( -1);
        }
    }

    if (optind == argc && !infn) {
        // Need extra args: filename
        printf("You must specify at least one input file!\n\n");
        help = TRUE;
    }
    if (help) {
        print_help(args[0], opts);
        exit(0);
    }
    bl_free(opts);

    gslutils_use_error_system();

    log_init(loglvl);
    if (tostderr)
        log_to(stderr);

    if (datalog) {
        datalogfid = fopen(datalog, "wb");
        if (!datalogfid) {
            SYSERROR("Failed to open data log file \"%s\" for writing", datalog);
            return -1;
        }
        atexit(close_datalogfid);
        data_log_init(100);
        data_log_enable_all();
        data_log_to(datalogfid);
        data_log_start();
    }

    if (infn) {
        logverb("Reading input filenames from %s\n", (fromstdin ? "stdin" : infn));
        if (!fromstdin) {
            fin = fopen(infn, "rb");
            if (!fin) {
                ERROR("Failed to open file %s for reading input filenames", infn);
                exit(-1);
            }
        } else
            fin = stdin;
    }

    // directory containing the 'engine' executable:
    me = find_executable(args[0], NULL);
    if (!me)
        me = strdup(args[0]);
    mydir = sl_append(strings, dirname(me));
    free(me);

    // Read config file
    if (!configfn) {
        int i;
        sl* trycf = sl_new(4);
        sl_appendf(trycf, "%s/%s/%s", mydir, default_config_path, default_configfn);
        // if I'm in /usr/bin, look for config file in /etc
        if (streq(mydir, "/usr/bin")) {
            sl_appendf(trycf, "/etc/%s", default_configfn);
        }
        sl_appendf(trycf, "%s/%s", mydir, default_configfn);
        sl_appendf(trycf, "./%s", default_configfn);
        sl_appendf(trycf, "./%s/%s", default_config_path, default_configfn);
        for (i=0; i<sl_size(trycf); i++) {
            char* cf = sl_get(trycf, i);
            if (file_exists(cf)) {
                configfn = strdup(cf);
                logverb("Using config file \"%s\"\n", cf);
                break;
            } else {
                logverb("Config file \"%s\" doesn't exist.\n", cf);
            }
        }
        if (!configfn) {
            char* cflist = sl_join(trycf, "\n  ");
            logerr("Couldn't find config file: tried:\n  %s\n", cflist);
            free(cflist);
        }
        sl_free2(trycf);
    }

    if (!streq(configfn, "none")) {
        if (engine_parse_config_file(engine, configfn)) {
            logerr("Failed to parse (or encountered an error while interpreting) config file \"%s\"\n", configfn);
            exit( -1);
        }
    }

    if (sl_size(inds)) {
        // Expand globs.
        for (i=0; i<sl_size(inds); i++) {
            char* s = sl_get(inds, i);
            glob_t myglob;
            int flags = GLOB_TILDE | GLOB_BRACE;
            if (glob(s, flags, NULL, &myglob)) {
                SYSERROR("Failed to expand wildcards in index-file path \"%s\"", s);
                exit(-1);
            }
            for (c=0; c<myglob.gl_pathc; c++) {
                if (engine_add_index(engine, myglob.gl_pathv[c])) {
                    ERROR("Failed to add index \"%s\"", myglob.gl_pathv[c]);
                    exit(-1);
                }
            }
            globfree(&myglob);
        }
    }

    if (!pl_size(engine->indexes)) {
        logerr("\n\n"
               "---------------------------------------------------------------------\n"
               "You must list at least one index in the config file (%s)\n\n"
               "See http://astrometry.net/use.html about how to get some index files.\n"
               "---------------------------------------------------------------------\n"
               "\n", configfn);
        exit(-1);
    }

    if (engine->minwidth <= 0.0 || engine->maxwidth <= 0.0) {
        logerr("\"minwidth\" and \"maxwidth\" in the config file %s must be positive!\n", configfn);
        exit(-1);
    }

    free(configfn);

    if (!il_size(engine->default_depths)) {
        parse_depth_string(engine->default_depths,
                           "10 20 30 40 50 60 70 80 90 100 "
                           "110 120 130 140 150 160 170 180 190 200");
    }

    engine->cancelfn = cancelfn;
    engine->solvedfn = solvedfn;

    i = optind;
    while (1) {
        char* jobfn;
        job_t* job;
        struct timeval tv1, tv2;

        if (infn) {
            // Read name of next input file to be read.
            logverb("\nWaiting for next input filename...\n");
            jobfn = read_string_terminated(fin, "\n\r\0", 3, FALSE);
            if (strlen(jobfn) == 0)
                break;
        } else {
            if (i == argc)
                break;
            jobfn = args[i];
            i++;
        }
        gettimeofday(&tv1, NULL);
        logmsg("Reading file \"%s\"...\n", jobfn);
        job = engine_read_job_file(engine, jobfn);
        if (!job) {
            ERROR("Failed to read job file \"%s\"", jobfn);
            exit(-1);
        }

	if (basedir) {
            logverb("Setting job's output base directory to %s\n", basedir);
            job_set_output_base_dir(job, basedir);
	}

        if (engine_run_job(engine, job))
            logerr("Failed to run_job()\n");

        job_free(job);
        gettimeofday(&tv2, NULL);
        logverb("Spent %g seconds on this field.\n", millis_between(&tv1, &tv2)/1000.0);
    }

    engine_free(engine);
    sl_free2(strings);
    sl_free2(inds);

    if (fin && !fromstdin)
        fclose(fin);

    return 0;
}