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 <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include "anwcs.h"
11 #include "an-bool.h"
12 #include "starutil.h"
13 #include "bl.h"
14 #include "xylist.h"
15 #include "rdlist.h"
16 #include "boilerplate.h"
17 #include "errors.h"
18 #include "log.h"
19
wcs_xy2rd(const char * wcsfn,int ext,const char * xylsfn,const char * rdlsfn,const char * xcol,const char * ycol,int forcetan,int forcewcslib,il * fields)20 int wcs_xy2rd(const char* wcsfn, int ext,
21 const char* xylsfn, const char* rdlsfn,
22 const char* xcol, const char* ycol,
23 int forcetan,
24 int forcewcslib,
25 il* fields) {
26 rdlist_t* rdls = NULL;
27 xylist_t* xyls = NULL;
28 anwcs_t* wcs = NULL;
29 int i;
30 int rtn = -1;
31 anbool alloced_fields = FALSE;
32
33 // read WCS.
34 if (forcewcslib) {
35 wcs = anwcs_open_wcslib(wcsfn, ext);
36 } else if (forcetan) {
37 wcs = anwcs_open_tan(wcsfn, ext);
38 } else {
39 wcs = anwcs_open(wcsfn, ext);
40 }
41 if (!wcs) {
42 ERROR("Failed to read WCS file \"%s\", extension %i", wcsfn, ext);
43 return -1;
44 }
45
46 // read XYLS.
47 xyls = xylist_open(xylsfn);
48 if (!xyls) {
49 ERROR("Failed to read an xylist from file %s", xylsfn);
50 goto bailout;
51 }
52 xylist_set_include_flux(xyls, FALSE);
53 xylist_set_include_background(xyls, FALSE);
54 if (xcol)
55 xylist_set_xname(xyls, xcol);
56 if (ycol)
57 xylist_set_yname(xyls, ycol);
58
59 // write RDLS.
60 rdls = rdlist_open_for_writing(rdlsfn);
61 if (!rdls) {
62 ERROR("Failed to open file %s to write RDLS.\n", rdlsfn);
63 goto bailout;
64 }
65 if (rdlist_write_primary_header(rdls)) {
66 ERROR("Failed to write header to RDLS file %s.\n", rdlsfn);
67 goto bailout;
68 }
69
70 if (!fields) {
71 alloced_fields = TRUE;
72 fields = il_new(16);
73 }
74 if (!il_size(fields)) {
75 // add all fields.
76 int NF = xylist_n_fields(xyls);
77 for (i=1; i<=NF; i++)
78 il_append(fields, i);
79 }
80
81 logverb("Processing %zu extensions...\n", il_size(fields));
82 for (i=0; i<il_size(fields); i++) {
83 int fieldind = il_get(fields, i);
84 starxy_t xy;
85 rd_t rd;
86 int j;
87
88 if (!xylist_read_field_num(xyls, fieldind, &xy)) {
89 ERROR("Failed to read xyls file %s, field %i", xylsfn, fieldind);
90 goto bailout;
91 }
92
93 if (rdlist_write_header(rdls)) {
94 ERROR("Failed to write rdls field header to %s", rdlsfn);
95 goto bailout;
96 }
97
98 rd_alloc_data(&rd, starxy_n(&xy));
99
100 for (j=0; j<starxy_n(&xy); j++) {
101 double x, y, ra, dec;
102 x = starxy_getx(&xy, j);
103 y = starxy_gety(&xy, j);
104 anwcs_pixelxy2radec(wcs, x, y, &ra, &dec);
105 rd_setra (&rd, j, ra);
106 rd_setdec(&rd, j, dec);
107 }
108
109 if (rdlist_write_field(rdls, &rd)) {
110 ERROR("Failed to write rdls field to %s", rdlsfn);
111 goto bailout;
112 }
113 rd_free_data(&rd);
114 starxy_free_data(&xy);
115
116 if (rdlist_fix_header(rdls)) {
117 ERROR("Failed to fix rdls field header for %s", rdlsfn);
118 goto bailout;
119 }
120
121 rdlist_next_field(rdls);
122 }
123
124 if (rdlist_fix_primary_header(rdls) ||
125 rdlist_close(rdls)) {
126 ERROR("Failed to fix header of RDLS file %s", rdlsfn);
127 rdls = NULL; //# Modified by Robert Lancaster for the StellarSolver Internal Library, since it would try to close it again in bailout
128 goto bailout;
129 }
130 rdls = NULL;
131
132 if (xylist_close(xyls)) {
133 ERROR("Failed to close XYLS file %s", xylsfn);
134 xyls = NULL; //# Modified by Robert Lancaster for the StellarSolver Internal Library, since it would try to close it again in bailout
135 goto bailout;
136 }
137 xyls = NULL;
138 rtn = 0;
139
140 bailout:
141 if (alloced_fields)
142 il_free(fields);
143 if (rdls)
144 rdlist_close(rdls);
145 if (xyls)
146 xylist_close(xyls);
147 if (wcs)
148 anwcs_free(wcs);
149 return rtn;
150 }
151