File: get-wcs.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 (134 lines) | stat: -rw-r--r-- 3,197 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
/*
 # This file is part of the Astrometry.net suite.
 # Licensed under a 3-clause BSD style license - see LICENSE
 */

/**
 Reads a FITS file, tries to interpret a WCS header and writes out a TAN
 interpretation of it.
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <math.h>

#include "sip.h"
#include "sip_qfits.h"
#include "fitsioutils.h"
#include "starutil.h"
#include "errors.h"
#include "log.h"
#include "ioutils.h"

static char* OPTIONS = "ho:e:t";

static void printHelp(char* progname) {
    printf("%s <input-file>\n"
           "   [-e <extension>]\n"
           "   [-o <output-file>]\n"
           "   [-t]: force TAN (not SIP)\n"
           "\n", progname);
}


int main(int argc, char *argv[]) {
    char* progname = argv[0];
    int argchar;
    char* infn = NULL;
    char* outfn = NULL;
    sip_t* wcs;
    int ext = 0;
    anbool forcetan = FALSE;
    tan_t* tan;

    while ((argchar = getopt (argc, argv, OPTIONS)) != -1)
        switch (argchar) {
        case '?':
        case 'h':
            printHelp(progname);
            return 0;
        case 'e':
            ext = atoi(optarg);
            break;
        case 'o':
            outfn = optarg;
            break;
        case 't':
            forcetan = TRUE;
            break;
        default:
            return -1;
        }

    if (optind != (argc - 1)) {
        printHelp(progname);
        exit(-1);
    }
    infn = argv[optind];

    log_init(LOG_MSG);
    fits_use_error_system();
    errors_log_to(stderr);

    wcs = sip_read_tan_or_sip_header_file_ext(infn, ext, NULL, forcetan);
    if (!wcs) {
        ERROR("Failed to parse WCS header");
        exit(-1);
    }

    tan = &(wcs->wcstan);

    printf("crval1 %.12g\n", tan->crval[0]);
    printf("crval2 %.12g\n", tan->crval[1]);
    printf("crpix1 %g\n", tan->crpix[0]);
    printf("crpix2 %g\n", tan->crpix[1]);
    printf("cd11 %.12g\n", tan->cd[0][0]);
    printf("cd12 %.12g\n", tan->cd[0][1]);
    printf("cd21 %.12g\n", tan->cd[1][0]);
    printf("cd22 %.12g\n", tan->cd[1][1]);

    if (wcs->a_order) {
        printf("a_order %i\n", wcs->a_order);
        printf("b_order %i\n", wcs->b_order);
        // FIXME -- print the coefficients!
    }

    if (outfn) {
        FILE* fout;
        anbool tostdout;
        tostdout = streq(outfn, "-");
        if (tostdout)
            fout = stdout;
        else {
            fout = fopen(outfn, "wb");
            if (!fout) {
                SYSERROR("Failed to open output file %s", outfn);
                exit(-1);
            }
        }

        if (wcs->a_order) {
            if (sip_write_to(wcs, fout)) {
                ERROR("Failed to write SIP header to file \"%s\"", outfn);
                exit(-1);
            }
        } else {
            if (tan_write_to(&(wcs->wcstan), fout)) {
                ERROR("Failed to write TAN header to file \"%s\"", outfn);
                exit(-1);
            }
        }

        if (!tostdout) {
            if (fclose(fout)) {
                SYSERROR("Failed to close output file \"%s\"", outfn);
                exit(-1);
            }
        }
    }

    return 0;
}