1 /*
2  # This file is part of the Astrometry.net suite.
3  # Licensed under a 3-clause BSD style license - see LICENSE
4  */
5 
6 #include <unistd.h>
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <math.h>
11 #include <string.h>
12 
13 #include "healpix.h"
14 #include "starutil.h"
15 #include "rdlist.h"
16 
convert_file(char * infn,char * outfn)17 int convert_file(char* infn, char* outfn)
18 {
19     int i, j, numfields, npoints, healpixes[12];
20     FILE* hpf;
21     rdlist* rdls;
22 
23     fprintf(stderr, "Reading input from RDLS file %s, writing output to HPLS file %s.\n", infn, outfn);
24 
25     // Open the two files for input and output
26     rdls = rdlist_open(infn);
27     if (!rdls) {
28         fprintf(stderr, "Couldn't open RDLS %s.\n", infn);
29         return 1;
30     }
31 
32     hpf = fopen(outfn, "w");
33     if (!hpf) {
34         fprintf(stderr, "Couldn't open %s for writing: %s\n", outfn, strerror(errno));
35         return 1;
36     }
37 
38     // First line: numfields
39     numfields = rdlist_n_fields(rdls);
40     fprintf(hpf, "NumFields=%i\n", numfields);
41 
42     for (j=1; j<=numfields; j++) {
43         int first = 1;
44         // Second line and subsequent lines: npoints,ra,dec,ra,dec,...
45         dl* points = rdlist_get_field(rdls, j);
46         if (!points) {
47             fprintf(stderr, "Failed to read RDLS field %i.\n", j);
48             return 1;
49         }
50 
51         for (i = 0; i < 12; i++) {
52             healpixes[i] = 0;
53         }
54 
55         npoints = dl_size(points) / 2;
56 
57         for (i = 0; i < npoints; i++) {
58             double ra, dec;
59             int hp;
60 
61             ra  = dl_get(points, i*2);
62             dec = dl_get(points, i*2 + 1);
63 
64             ra=deg2rad(ra);
65             dec=deg2rad(dec);
66 
67             hp = radectohealpix(ra, dec, 1);
68             if ((hp < 0) || (hp >= 12)) {
69                 printf("ERROR: hp=%i\n", hp);
70                 exit(-1);
71             }
72             healpixes[hp] = 1;
73         }
74         for (i = 0; i < 12; i++) {
75             if (healpixes[i]) {
76                 if (!first)
77                     fprintf(hpf, " ");
78                 fprintf(hpf, "%i", i);
79                 first = 0;
80             }
81         }
82         fprintf(hpf, "\n");
83         fflush(hpf);
84 
85         dl_free(points);
86     }
87 
88     rdlist_close(rdls);
89     fclose(hpf);
90     return 0;
91 }
92 
main(int argc,char ** args)93 int main(int argc, char** args)
94 {
95     int i;
96     if (argc == 1 || !(argc % 2)) {
97         fprintf(stderr, "Usage: %s <input-rdls-file> <output-hpls-file> [...]\n", args[0]);
98         return 1;
99     }
100 
101     for (i=1; i+1<argc; i+=2)
102         convert_file(args[i], args[i+1]);
103 
104     return 0;
105 }
106