ERIS Pipeline Reference Manual 1.8.15
eris_nix_catalogue.c
1/* $Id$
2 *
3 * This file is part of the ERIS/NIX Pipeline
4 * Copyright (C) 2017 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /*
22 * $Author$
23 * $Date$
24 * $Rev$
25 */
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31/*-----------------------------------------------------------------------------
32 Includes
33 -----------------------------------------------------------------------------*/
34
35#include <cpl.h>
36#include <string.h>
37
38#include "casu_utils.h"
39#include "casu_wcsutils.h"
40#include "eris_nix_catalogue.h"
41
42/*----------------------------------------------------------------------------*/
46/*----------------------------------------------------------------------------*/
47
48
51/*----------------------------------------------------------------------------*/
58/*----------------------------------------------------------------------------*/
59
60cpl_error_code en_catalogue_conform_naming(cpl_frameset * frameset,
61 const char * tag) {
62
63 if (cpl_error_get_code() != CPL_ERROR_NONE) return cpl_error_get_code();
64
65 /* parameter check */
66
67 cpl_ensure_code(frameset, CPL_ERROR_NULL_INPUT);
68 cpl_ensure_code(tag, CPL_ERROR_NULL_INPUT);
69
70 /* Look for tagged file in frameset */
71
72 const cpl_frame * target_frame = cpl_frameset_find_const(frameset, tag);
73 if (target_frame != NULL) {
74 const char * filename = cpl_frame_get_filename(target_frame);
75
76 /* Read the whole input file. It's assumed this isn't so big that it's
77 going to take very long. */
78
79 cpl_table * whole = cpl_table_load(filename, 1, CPL_FALSE);
80
81 /* Naming problems. HDRL expects columns RA and DEC. If not present
82 in table look for ra, dec or Dec, if these are found then rename. */
83
84 en_catalogue_name_conformance(whole);
85
86 /* Replace the FITS table with the updated version */
87
88 cpl_propertylist * pheader = cpl_propertylist_load(filename, 0);
89 cpl_propertylist * header = cpl_propertylist_load(filename, 1);
90 cpl_table_save(whole, pheader, header, filename, CPL_IO_CREATE);
91 cpl_propertylist_delete(pheader);
92 cpl_propertylist_delete(header);
93
94 cpl_table_delete(whole);
95
96 } else {
97 cpl_msg_warning(cpl_func, "SoF has no file tagged %s", tag);
98 }
99
100 return cpl_error_get_code();
101}
102
107/*----------------------------------------------------------------------------*/
115/*----------------------------------------------------------------------------*/
116
117cpl_error_code en_catalogue_coverage(const located_imagelist * jitters,
118 double * ramin, double * ramax,
119 double * decmin, double * decmax) {
120
121 if (cpl_error_get_code() != CPL_ERROR_NONE) return cpl_error_get_code();
122
123 cpl_ensure_code(jitters, CPL_ERROR_NULL_INPUT);
124
125 for (cpl_size j=0; j < jitters->size; j++) {
126 double ra1 = 0.0;
127 double ra2 = 0.0;
128 double dec1 = 0.0;
129 double dec2 = 0.0;
130 int status = CASU_OK;
131 casu_coverage(jitters->limages[j]->plist, 1, &ra1, &ra2, &dec1,
132 &dec2, &status);
133 enu_check(status==CASU_OK, CPL_ERROR_INCOMPATIBLE_INPUT,
134 "error in casu_coverage: status=%d", status);
135
136 if (j==0) {
137 *ramin = ra1;
138 *ramax = ra2;
139 *decmin = dec1;
140 *decmax = dec2;
141 } else {
142 if (ra1 < *ramin) *ramin = ra1;
143 if (ra2 > *ramax) *ramax = ra2;
144 if (dec1 < *decmin) *decmin = dec1;
145 if (dec2 < *decmax) *decmax = dec2;
146 }
147 }
148
149 cpl_msg_info(cpl_func, "...coverage %5.3f %5.3f %5.3f %5.3f", *ramin,
150 *ramax, *decmin, *decmax);
151 cleanup:
152 return cpl_error_get_code();
153}
154
159/*----------------------------------------------------------------------------*/
168/*----------------------------------------------------------------------------*/
169
170cpl_table * en_catalogue_load_from_file(const char * filename,
171 const double ramin,
172 const double ramax,
173 const double decmin,
174 const double decmax) {
175
176
177 if (cpl_error_get_code() != CPL_ERROR_NONE) return NULL;
178
179 cpl_table * whole = NULL;
180 cpl_table * result = NULL;
181
182 /* parameter check */
183
184 cpl_ensure(filename, CPL_ERROR_NULL_INPUT, NULL);
185
186 /* Read the whole input file. It's assumed this isn't so big that it's
187 going to take very long. */
188
189 whole = cpl_table_load(filename, 1, CPL_FALSE);
190
191 /* Enforce column naming compliance */
192
193 en_catalogue_name_conformance(whole);
194
195 cpl_size nrows = cpl_table_get_nrow(whole);
196 cpl_msg_info(cpl_func, "read rows %d %s", (int)nrows, cpl_error_get_message());
197
198 /* Is there a wrap around problem? */
199
200 if (ramin < 0.0 && ramax > 0.0) {
201
202 /* yes, so we need to break problem into 2 queries */
203
204 double ramin1 = ramin + 360.0;
205 double ramax1 = 360.0;
206
207 cpl_table_unselect_all(whole);
208 cpl_table_or_selected_double(whole, "RA", CPL_NOT_LESS_THAN, ramin1);
209 cpl_table_and_selected_double(whole, "RA", CPL_NOT_GREATER_THAN,
210 ramax1);
211 cpl_table_and_selected_double(whole, "DEC", CPL_NOT_LESS_THAN, decmin);
212 cpl_table_and_selected_double(whole, "DEC", CPL_NOT_GREATER_THAN,
213 decmax);
214 result = cpl_table_extract_selected(whole);
215
216 /* second query */
217
218 ramin1 = 0.000001;
219 ramax1 = ramax;
220
221 cpl_table_unselect_all(whole);
222 cpl_table_or_selected_double(whole, "RA", CPL_NOT_LESS_THAN, ramin1);
223 cpl_table_and_selected_double(whole, "RA", CPL_NOT_GREATER_THAN,
224 ramax1);
225 cpl_table_and_selected_double(whole, "DEC", CPL_NOT_LESS_THAN, decmin);
226 cpl_table_and_selected_double(whole, "DEC", CPL_NOT_GREATER_THAN,
227 decmax);
228 cpl_table * out2 = cpl_table_extract_selected(whole);
229
230 /* merge query results */
231
232 nrows = cpl_table_get_nrow(result);
233 cpl_table_insert(result, out2, nrows+1);
234 cpl_table_delete(out2);
235
236 } else {
237
238 /* no, only one query needed */
239
240 cpl_table_unselect_all(whole);
241 cpl_table_or_selected_double(whole, "RA", CPL_NOT_LESS_THAN, ramin);
242 cpl_table_and_selected_double(whole, "RA", CPL_NOT_GREATER_THAN,
243 ramax);
244 cpl_table_and_selected_double(whole, "DEC", CPL_NOT_LESS_THAN, decmin);
245 cpl_table_and_selected_double(whole, "DEC", CPL_NOT_GREATER_THAN,
246 decmax);
247 result = cpl_table_extract_selected(whole);
248
249 for (cpl_size ir=0; ir<nrows; ir++) {
250 int null =0;
251 double ra = cpl_table_get(whole, "RA", ir, &null);
252 double dec = cpl_table_get(whole, "DEC", ir, &null);
253
254 cpl_msg_info(cpl_func, "%8.6f %8.6f %8.6f %8.6f %8.6f %8.6f", ra, ramin, ramax, dec, decmin, decmax);
255 }
256 }
257
258 /* Sort the table by DEC */
259
260 cpl_propertylist * p = cpl_propertylist_new();
261 cpl_propertylist_append_bool(p, "DEC", 0);
262 cpl_table_sort(result, p);
263 cpl_propertylist_delete(p);
264
265 /* cleanup */
266
267 cpl_table_delete(whole);
268 if (cpl_error_get_code() != CPL_ERROR_NONE) {
269 cpl_table_delete(result);
270 result = NULL;
271 }
272 return result;
273}
274
279/*----------------------------------------------------------------------------*/
288/*----------------------------------------------------------------------------*/
289
290cpl_table * en_catalogue_load_from_frameset(cpl_frameset * frameset,
291 const char * tag,
292 const double ramin,
293 const double ramax,
294 const double decmin,
295 const double decmax,
296 cpl_frameset * used,
297 const int required) {
298
299
300 if (cpl_error_get_code() != CPL_ERROR_NONE) return NULL;
301
302 cpl_table * result = NULL;
303
304 /* parameter check */
305
306 cpl_ensure(frameset, CPL_ERROR_NULL_INPUT, NULL);
307 cpl_ensure(tag, CPL_ERROR_NULL_INPUT, NULL);
308
309 /* Look for tagged file in frameset */
310
311 const cpl_frame * target_frame = cpl_frameset_find_const(frameset, tag);
312 if (target_frame != NULL) {
313 const char * filename = cpl_frame_get_filename(target_frame);
314
315 result = en_catalogue_load_from_file(filename, ramin, ramax, decmin,
316 decmax);
317 cpl_frameset_insert(used, cpl_frame_duplicate(target_frame));
318 } else {
319 enu_check(!required, CPL_ERROR_DATA_NOT_FOUND,
320 "SoF has no file tagged %s", tag);
321 }
322
323 cleanup:
324 if (cpl_error_get_code() != CPL_ERROR_NONE) {
325 cpl_table_delete(result);
326 result = NULL;
327 }
328 return result;
329}
330
335/*----------------------------------------------------------------------------*/
342/*----------------------------------------------------------------------------*/
343
344cpl_error_code en_catalogue_name_conformance(cpl_table * catalogue) {
345
346 if (cpl_error_get_code() != CPL_ERROR_NONE) return cpl_error_get_code();
347
348 cpl_ensure_code(catalogue, CPL_ERROR_NULL_INPUT);
349
350 /* Naming problems. HDRL expects columns RA and DEC. CASU expects
351 RA and Dec. If not present in table look for ra, dec or Dec, if these are found then rename. */
352
353 if (!cpl_table_has_column(catalogue, "RA")) {
354 if (cpl_table_has_column(catalogue, "ra")) {
355 cpl_table_name_column(catalogue, "ra", "RA");
356 }
357 }
358 if (!cpl_table_has_column(catalogue, "DEC")) {
359 if (cpl_table_has_column(catalogue, "dec")) {
360 cpl_table_name_column(catalogue, "dec", "DEC");
361 } else if (cpl_table_has_column(catalogue, "Dec")) {
362 cpl_table_duplicate_column(catalogue, "DEC", catalogue, "Dec");
363 }
364 }
365
366 return cpl_error_get_code();
367}
368
cpl_error_code en_catalogue_conform_naming(cpl_frameset *frameset, const char *tag)
Rename essential columns to standard names.