File: create-scamp-catalog.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 (137 lines) | stat: -rw-r--r-- 3,937 bytes parent folder | download | duplicates (4)
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
/*
 # This file is part of the Astrometry.net suite.
 # Licensed under a 3-clause BSD style license - see LICENSE
 */

#include <unistd.h>
#include <stdio.h>
#include <math.h>
#include <stddef.h>

#include "scamp-catalog.h"
#include "usnob-fits.h"
#include "healpix.h"
#include "starutil.h"
#include "errors.h"
#include "log.h"

const char* OPTIONS = "hv";

void print_help(char* progname) {
    printf("Usage: %s <input-FITS-table> <output-scamp-catalog>\n"
           "  Input table must contain columns:\n"
           "      RA\n"
           "      RA_ERR\n"
           "      DEC\n"
           "      DEC_ERR\n"
           "      MAG\n"
           "      MAG_ERR\n"
           "  [-v]: verbose\n"
           "\n", progname);
}


int main(int argc, char** args) {
    int c;
    char* infn = NULL;
    char* outfn = NULL;
    scamp_cat_t* scamp;
    int loglvl = LOG_MSG;
    fitstable_t* table;
    int i, N;
    tfits_type dubl = fitscolumn_double_type();
    tfits_type any  = fitscolumn_any_type();

    while ((c = getopt(argc, args, OPTIONS)) != -1) {
        switch (c) {
        case 'h':
            print_help(args[0]);
            exit(0);
        case 'v':
            loglvl++;
            break;
        }
    }
    log_init(loglvl);

    if (optind != argc - 2) {
        print_help(args[0]);
        exit(-1);
    }

    infn = args[optind];
    outfn = args[optind+1];

    logverb("Reading input from \"%s\"\n", infn);
    logverb("Will write output to \"%s\"\n", outfn);

    table = fitstable_open(infn);
    if (!table) {
        ERROR("Failed to open input file \"%s\"", infn);
        exit(-1);
    }
    N = fitstable_nrows(table);
    logverb("Input table has %i rows\n", N);

    fitstable_add_read_column_struct(table, dubl, 1, offsetof(scamp_ref_t, ra),
                                     any, "RA", TRUE);
    fitstable_add_read_column_struct(table, dubl, 1, offsetof(scamp_ref_t, dec),
                                     any, "DEC", TRUE);
    fitstable_add_read_column_struct(table, dubl, 1, offsetof(scamp_ref_t, err_a),
                                     any, "RA_ERR", TRUE);
    fitstable_add_read_column_struct(table, dubl, 1, offsetof(scamp_ref_t, err_b),
                                     any, "DEC_ERR", TRUE);
    fitstable_add_read_column_struct(table, dubl, 1, offsetof(scamp_ref_t, mag),
                                     any, "MAG", TRUE);
    fitstable_add_read_column_struct(table, dubl, 1, offsetof(scamp_ref_t, err_mag),
                                     any, "MAG_ERR", FALSE);

    fitstable_use_buffered_reading(table, sizeof(scamp_ref_t), 1000);

    if (fitstable_read_extension(table, 1)) {
        ERROR("Failed to open table from extension 1 of \"%s\"", infn);
        fitstable_error_report_missing(table);
        logmsg("Table has columns:\n");
        fitstable_print_columns(table);
        exit(-1);
    }

    scamp = scamp_catalog_open_for_writing(outfn, TRUE);
    if (!scamp ||
        scamp_catalog_write_field_header(scamp, NULL)) {
        ERROR("Failed to open SCAMP reference catalog for writing: \"%s\"", outfn);
        exit(-1);
    }

    for (i=0; i<N; i++) {
        /*
         scamp_ref_t ref;
         memset(&ref, 0, sizeof(scamp_ref_t));
         if (fitstable_read_struct(table, i, &ref)) {
         ERROR("Failed to read entry %i from input table\n", i);
         exit(-1);
         }
         */
        scamp_ref_t* ref;
        ref = fitstable_next_struct(table);
        if (!ref) {
            ERROR("Failed to read entry %i from input table\n", i);
            exit(-1);
        }

        if (scamp_catalog_write_reference(scamp, ref)) {
            ERROR("Failed to write entry %i to SCAMP catalog.\n", i);
            exit(-1);
        }
    }

    if (scamp_catalog_close(scamp)) {
        ERROR("Failed to close SCAMP reference catalog \"%s\"", outfn);
        exit(-1);
    }

    fitstable_close(table);

    return 0;
}