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

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

#include "healpix.h"
#include "starutil.h"
#include "rdlist.h"

int convert_file(char* infn, char* outfn)
{
    int i, j, numfields, npoints, healpixes[12];
    FILE* hpf;
    rdlist* rdls;

    fprintf(stderr, "Reading input from RDLS file %s, writing output to HPLS file %s.\n", infn, outfn);

    // Open the two files for input and output
    rdls = rdlist_open(infn);
    if (!rdls) {
        fprintf(stderr, "Couldn't open RDLS %s.\n", infn);
        return 1;
    }

    hpf = fopen(outfn, "w");
    if (!hpf) {
        fprintf(stderr, "Couldn't open %s for writing: %s\n", outfn, strerror(errno));
        return 1;
    }

    // First line: numfields
    numfields = rdlist_n_fields(rdls);
    fprintf(hpf, "NumFields=%i\n", numfields);

    for (j=1; j<=numfields; j++) {
        int first = 1;
        // Second line and subsequent lines: npoints,ra,dec,ra,dec,...
        dl* points = rdlist_get_field(rdls, j);
        if (!points) {
            fprintf(stderr, "Failed to read RDLS field %i.\n", j);
            return 1;
        }

        for (i = 0; i < 12; i++) {
            healpixes[i] = 0;
        }

        npoints = dl_size(points) / 2;

        for (i = 0; i < npoints; i++) {
            double ra, dec;
            int hp;

            ra  = dl_get(points, i*2);
            dec = dl_get(points, i*2 + 1);

            ra=deg2rad(ra);
            dec=deg2rad(dec);

            hp = radectohealpix(ra, dec, 1);
            if ((hp < 0) || (hp >= 12)) {
                printf("ERROR: hp=%i\n", hp);
                exit(-1);
            }
            healpixes[hp] = 1;
        }
        for (i = 0; i < 12; i++) {
            if (healpixes[i]) {
                if (!first)
                    fprintf(hpf, " ");
                fprintf(hpf, "%i", i);
                first = 0;
            }
        }
        fprintf(hpf, "\n");
        fflush(hpf);

        dl_free(points);
    }

    rdlist_close(rdls);
    fclose(hpf);
    return 0;
}

int main(int argc, char** args)
{
    int i;
    if (argc == 1 || !(argc % 2)) {
        fprintf(stderr, "Usage: %s <input-rdls-file> <output-hpls-file> [...]\n", args[0]);
        return 1;
    }

    for (i=1; i+1<argc; i+=2)
        convert_file(args[i], args[i+1]);

    return 0;
}