File: plotradec.c

package info (click to toggle)
astrometry.net 0.98%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,428 kB
  • sloc: ansic: 165,791; python: 18,438; makefile: 1,560; sh: 157; cpp: 78; pascal: 67; awk: 56; perl: 9
file content (203 lines) | stat: -rw-r--r-- 5,503 bytes parent folder | download | duplicates (5)
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
/*
 # This file is part of the Astrometry.net suite.
 # Licensed under a 3-clause BSD style license - see LICENSE
 */
#include <assert.h>

#include "os-features.h"
#include "plotradec.h"
#include "rdlist.h"
#include "cairoutils.h"
#include "log.h"
#include "errors.h"
#include "sip_qfits.h"

DEFINE_PLOTTER(radec);

plotradec_t* plot_radec_get(plot_args_t* pargs) {
    return plotstuff_get_config(pargs, "radec");
}

void plot_radec_reset(plotradec_t* args) {
    if (args->radecvals)
        dl_free(args->radecvals);
    if (args->racol)
        free(args->racol);
    if (args->deccol)
        free(args->deccol);
    if (args->fn)
        free(args->fn);
    memset(args, 0, sizeof(plotradec_t));
    args->ext = 1;
    args->radecvals = dl_new(32);
}

void* plot_radec_init(plot_args_t* plotargs) {
    plotradec_t* args = calloc(1, sizeof(plotradec_t));
    plot_radec_reset(args);
    return args;
}

static rd_t* get_rd(plotradec_t* args, rd_t* myrd) {
    rdlist_t* rdls = NULL;
    rd_t* rd = NULL;
    if (args->fn) {
        // Open rdlist.
        rdls = rdlist_open(args->fn);
        if (!rdls) {
            ERROR("Failed to open rdlist from file \"%s\"", args->fn);
            return NULL;
        }
        if (args->racol)
            rdlist_set_raname(rdls, args->racol);
        if (args->deccol)
            rdlist_set_decname(rdls, args->deccol);

        // Find number of entries in rdlist.
        rd = rdlist_read_field_num(rdls, args->ext, NULL);
        //freerd = rd;
        rdlist_close(rdls);
        if (!rd) {
            ERROR("Failed to read FITS extension %i from file %s.\n", args->ext, args->fn);
            return NULL;
        }
    } else {
        assert(dl_size(args->radecvals));
        rd_from_dl(myrd, args->radecvals);
        rd = myrd;
    }
    return rd;
}

int plot_radec_plot(const char* command, cairo_t* cairo,
                    plot_args_t* pargs, void* baton) {
    plotradec_t* args = (plotradec_t*)baton;
    // Plot it!
    rd_t myrd;
    rd_t* rd = NULL;
    //rd_t* freerd = NULL;
    int Nrd;
    int i;

    if (!pargs->wcs) {
        ERROR("plotting radec but not plot_wcs has been set.");
        return -1;
    }

    if (args->fn && dl_size(args->radecvals)) {
        ERROR("Can only plot one of rdlist filename and radec_vals");
        return -1;
    }
    if (!args->fn && !dl_size(args->radecvals)) {
        ERROR("Neither rdlist filename nor radec_vals given!");
        return -1;
    }

    plotstuff_builtin_apply(cairo, pargs);

    rd = get_rd(args, &myrd);
    if (!rd) return -1;
    Nrd = rd_n(rd);
    // If N is specified, apply it as a max.
    if (args->nobjs)
        Nrd = MIN(Nrd, args->nobjs);

    // Plot markers.
    for (i=args->firstobj; i<Nrd; i++) {
        double x,y;
        double ra = rd_getra(rd, i);
        double dec = rd_getdec(rd, i);
        if (!plotstuff_radec2xy(pargs, ra, dec, &x, &y))
            continue;
        if (!plotstuff_marker_in_bounds(pargs, x, y))
            continue;
        plotstuff_stack_marker(pargs, x-1, y-1);
    }
    plotstuff_plot_stack(pargs, cairo);

    if (rd != &myrd)
        rd_free(rd);
    //rd_free(freerd);
    return 0;
}

int plot_radec_count_inbounds(plot_args_t* pargs, plotradec_t* args) {
    rd_t myrd;
    rd_t* rd = NULL;
    int i, Nrd, nib;

    rd = get_rd(args, &myrd);
    if (!rd) return -1;
    Nrd = rd_n(rd);
    // If N is specified, apply it as a max.
    if (args->nobjs)
        Nrd = MIN(Nrd, args->nobjs);
    nib = 0;
    for (i=args->firstobj; i<Nrd; i++) {
        double x,y;
        double ra = rd_getra(rd, i);
        double dec = rd_getdec(rd, i);
        if (!plotstuff_radec2xy(pargs, ra, dec, &x, &y))
            continue;
        if (!plotstuff_marker_in_bounds(pargs, x, y))
            continue;
        nib++;
    }
    if (rd != &myrd)
        rd_free(rd);
    return nib;
}

void plot_radec_set_racol(plotradec_t* args, const char* col) {
    free(args->racol);
    args->racol = strdup_safe(col);
}

void plot_radec_set_deccol(plotradec_t* args, const char* col) {
    free(args->deccol);
    args->deccol = strdup_safe(col);
}

void plot_radec_set_filename(plotradec_t* args, const char* fn) {
    free(args->fn);
    args->fn = strdup_safe(fn);
}

void plot_radec_vals(plotradec_t* args, double ra, double dec) {
    dl_append(args->radecvals, ra);
    dl_append(args->radecvals, dec);
}

int plot_radec_command(const char* cmd, const char* cmdargs,
                       plot_args_t* plotargs, void* baton) {
    plotradec_t* args = (plotradec_t*)baton;
    if (streq(cmd, "radec_file")) {
        plot_radec_set_filename(args, cmdargs);
    } else if (streq(cmd, "radec_ext")) {
        args->ext = atoi(cmdargs);
    } else if (streq(cmd, "radec_racol")) {
        plot_radec_set_racol(args, cmdargs);
    } else if (streq(cmd, "radec_deccol")) {
        plot_radec_set_deccol(args, cmdargs);
    } else if (streq(cmd, "radec_firstobj")) {
        args->firstobj = atoi(cmdargs);
    } else if (streq(cmd, "radec_nobjs")) {
        args->nobjs = atoi(cmdargs);
    } else if (streq(cmd, "radec_vals")) {
        plotstuff_append_doubles(cmdargs, args->radecvals);
    } else {
        ERROR("Did not understand command \"%s\"", cmd);
        return -1;
    }
    return 0;
}

void plot_radec_free(plot_args_t* plotargs, void* baton) {
    plotradec_t* args = (plotradec_t*)baton;
    free(args->radecvals);
    free(args->racol);
    free(args->deccol);
    free(args->fn);
    free(args);
}