CR2RE Pipeline Reference Manual 1.7.0
irplib_cat.c
1/* $Id: irplib_cat.c,v 1.10 2009-12-01 12:34:25 cgarcia Exp $
2 *
3 * This file is part of the irplib package
4 * Copyright (C) 2002,2003 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 02111-1307 USA
19 */
20
21/*
22 * $Author: cgarcia $
23 * $Date: 2009-12-01 12:34:25 $
24 * $Revision: 1.10 $
25 * $Name: not supported by cvs2svn $
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32/*-----------------------------------------------------------------------------
33 Includes
34 -----------------------------------------------------------------------------*/
35
36#include <libgen.h>
37#include <math.h>
38#include <cpl.h>
39
40#include "irplib_cat.h"
41#include "irplib_wcs.h"
42
43#define FILENAME_SZBUF 1024
44
45/*----------------------------------------------------------------------------*/
49/*----------------------------------------------------------------------------*/
50
52
53/*---------------------------------------------------------------------------*/
72/*---------------------------------------------------------------------------*/
73
74int
75irplib_2mass_get_catpars(const cpl_frame *master_index,
76 char **catpath,
77 char **catname)
78{
79 cpl_propertylist *p;
80 char *fname;
81 int status = CPL_ERROR_NONE;
82
83 /* Initialise a few things */
84 *catpath = NULL;
85 *catname = NULL;
86
87 /* First get the full path to the index file and make sure it exists */
88 fname = cpl_strdup(cpl_frame_get_filename(master_index));
89 if (access((const char *)fname, R_OK) != 0) {
90 cpl_msg_error(__func__, "Can't access index file %s", fname);
91 cpl_free(fname);
92 return CPL_ERROR_FILE_IO;
93 }
94 *catpath = cpl_strdup(dirname(fname));
95
96 /* Try to load the propertylist. If it is not possible signal a fatal
97 error since this probably means the whole file is messed up */
98 if ((p = cpl_propertylist_load(cpl_frame_get_filename(master_index), 0)) ==
99 NULL) {
100 cpl_msg_error(__func__, "Can't load index file header %s", fname);
101 cpl_free(*catpath);
102 cpl_free(fname);
103 return CPL_ERROR_FILE_IO;
104 }
105
106 /* If there is a catalogue name in the header then send it back. If there
107 isn't then give a default name and send a warning */
108 if (cpl_propertylist_has(p, "CATNAME")) {
109 *catname = cpl_strdup(cpl_propertylist_get_string(p, "CATNAME"));
110 status = CPL_ERROR_NONE;
111 }
112 else {
113 const char *unk = "unknown";
114 *catname = cpl_strdup(unk);
115 cpl_msg_warning(__func__,
116 "Property CATNAME not in index file header %s", fname);
117 }
118
119 /* Free and return */
120 cpl_free(fname);
121 cpl_propertylist_delete(p);
122 return (status);
123}
124
125
126/*---------------------------------------------------------------------------*/
149/*---------------------------------------------------------------------------*/
150
151cpl_error_code
153 float ext_search,
154 double *ra1,
155 double *ra2,
156 double *dec1,
157 double *dec2)
158{
159 double min_4q;
160 double max_1q;
161 int first_quad;
162 int fourth_quad;
163 const int *naxes;
164 long j;
165 const cpl_array *a;
166
167 /* Initialise these in case of failure later*/
168 *ra1 = 0.0;
169 *ra2 = 0.0;
170 *dec1 = 0.0;
171 *dec2 = 0.0;
172
173 /* Grab the WCS info from the property list */
174 if (wcs == NULL)
175 return CPL_ERROR_DATA_NOT_FOUND;
176
177 /* Get the size of the data array */
178
179 a = cpl_wcs_get_image_dims(wcs);
180 if (a == NULL)
181 return CPL_ERROR_ILLEGAL_INPUT;
182 naxes = cpl_array_get_data_int_const(a);
183
184 /* Find the RA and Dec limits of the image */
185
186 *ra1 = 370.0;
187 *ra2 = -370.0;
188 *dec1 = 95.0;
189 *dec2 = -95.0;
190 first_quad = 0;
191 fourth_quad = 0;
192 min_4q = 370.0;
193 max_1q = 0.0;
194 for (j = 1; j < naxes[1]; j += 10) {
195 long i;
196 double y = (double)j;
197 for (i = 1; i < naxes[0]; i += 10) {
198 double ra, dec;
199 double x = (double)i;
200 irplib_wcs_xytoradec(wcs, x, y, &ra, &dec);
201 if (ra >= 0.0 && ra <= 90.0) {
202 first_quad = 1;
203 if (ra > max_1q)
204 max_1q = ra;
205 }
206 else if (ra >= 270.0 && ra <= 360.0) {
207 fourth_quad = 1;
208 if (ra - 360.0 < min_4q)
209 min_4q = ra - 360.0;
210 }
211 if (ra < *ra1)
212 *ra1 = ra;
213 if (ra > *ra2)
214 *ra2 = ra;
215 if (dec < *dec1)
216 *dec1 = dec;
217 if (dec > *dec2)
218 *dec2 = dec;
219 }
220 }
221
222 /* Now have a look to see if you had RA values in both the first and
223 fourth quadrants. If you have, then make the minimum RA a negative
224 value. This will be the signal to the caller that you have the
225 wraparound... */
226
227 if (first_quad && fourth_quad) {
228 *ra1 = min_4q;
229 *ra2 = max_1q;
230 }
231
232 /* Pad out search a bit */
233 if (ext_search) {
234 double dra, ddec;
235 dra = 0.5 * ext_search * (*ra2 - *ra1);
236 *ra1 -= dra;
237 *ra2 += dra;
238 ddec = 0.5 * ext_search * (*dec2 - *dec1);
239 *dec1 -= ddec;
240 *dec2 += ddec;
241 }
242
243 /* Exit */
244 return CPL_ERROR_NONE;
245}
246
247/*---------------------------------------------------------------------------*/
271/*---------------------------------------------------------------------------*/
272
273cpl_table *
275 float ramin,
276 float ramax,
277 float decmin,
278 float decmax)
279{
280 cpl_table *t, *s;
281 cpl_table *out;
282 int i, nrows, start, finish, first_index, last_index, irow, init, j;
283 int wrap, iwrap;
284 float dectest, ratest, ramin_wrap, ramax_wrap;
285 char fullname[FILENAME_SZBUF];
286 cpl_array *a;
287 const char *deccol[] = {"Dec"};
288 cpl_propertylist *p;
289
290 /* Create an output table */
291
292 out = cpl_table_new(0);
293 init = 1;
294
295 /* Create a cpl array */
296
297 /* deccol will NOT be modified */
298 a = cpl_array_wrap_string((char **)deccol, 1);
299
300 /* Is there a wrap around problem? */
301
302 wrap = (ramin < 0.0 && ramax > 0.0) ? 2 : 1;
303
304 /* Loop for each query. If there is a wrap around problem then we need 2
305 queries. If not, then we only need 1 */
306
307 for (iwrap = 0; iwrap < wrap; iwrap++) {
308 int first_index_ra, last_index_ra;
309
310 if (wrap == 2) {
311 if (iwrap == 0) {
312 ramin_wrap = ramin + 360.0;
313 ramax_wrap = 360.0;
314 }
315 else {
316 ramin_wrap = 0.000001;
317 ramax_wrap = ramax;
318 }
319 }
320 else {
321 ramin_wrap = ramin;
322 ramax_wrap = ramax;
323 }
324
325 /* Find out where in the index to look */
326
327 first_index_ra = (int)ramin_wrap;
328 last_index_ra = (int)ramax_wrap;
329 if (last_index_ra > 359)
330 last_index_ra = 359;
331
332 /* Look at the min and max RA and decide which files need to be
333 opened. */
334
335 for (i = first_index_ra; i <= last_index_ra; i++) {
336 /* Ok, we've found one that needs opening. Read the file with
337 the relevant CPL call */
338
339 (void)snprintf(fullname, FILENAME_SZBUF, "%s/npsc%03d.fits", path,
340 i);
341
342 /* Read the propertylist so that you know how many rows there
343 are in the table */
344
345 p = cpl_propertylist_load(fullname, 1);
346 if (p == NULL) {
347 cpl_error_set_message_macro(__func__, CPL_ERROR_DATA_NOT_FOUND,
348 __FILE__, __LINE__,
349 "2mass file %s missing", fullname);
350 cpl_table_delete(out);
351 cpl_array_unwrap(a);
352 return (NULL);
353 }
354 nrows = cpl_propertylist_get_int(p, "NAXIS2");
355 cpl_propertylist_delete(p);
356
357 /* Load various rows until you find the Dec range that you
358 have specified. First the minimum Dec */
359
360 start = 0;
361 finish = nrows;
362 first_index = nrows / 2;
363 while (finish - start >= 2) {
364 t = cpl_table_load_window(fullname, 1, 0, a, first_index, 1);
365 dectest = cpl_table_get_float(t, "Dec", 0, NULL);
366 cpl_table_delete(t);
367 if (dectest < decmin) {
368 start = first_index;
369 first_index = (first_index + finish) / 2;
370 }
371 else {
372 finish = first_index;
373 first_index = (first_index + start) / 2;
374 }
375 }
376
377 /* Load various rows until you find the Dec range that you
378 have specified. Now the maximum Dec */
379
380 start = first_index;
381 finish = nrows;
382 last_index = start + (finish - start) / 2;
383 while (finish - start >= 2) {
384 t = cpl_table_load_window(fullname, 1, 0, a, last_index, 1);
385 dectest = cpl_table_get_float(t, "Dec", 0, NULL);
386 cpl_table_delete(t);
387 if (dectest < decmax) {
388 start = last_index;
389 last_index = (last_index + finish) / 2;
390 }
391 else {
392 finish = last_index;
393 last_index = (last_index + start) / 2;
394 }
395 }
396 if (last_index < first_index)
397 last_index = first_index;
398
399 /* Ok now now load all the rows in the relevant dec limits */
400
401 nrows = last_index - first_index + 1;
402 if ((t = cpl_table_load_window(fullname, 1, 0, NULL, first_index,
403 nrows)) == NULL) {
404 cpl_error_set_message_macro(__func__, CPL_ERROR_DATA_NOT_FOUND,
405 __FILE__, __LINE__,
406 "Error in subset of 2mass file %s ",
407 fullname);
408 cpl_table_delete(out);
409 cpl_array_unwrap(a);
410 return (NULL);
411 }
412 cpl_table_unselect_all(t);
413
414 /* Right, we now know what range of rows to search. Go through
415 these and pick the ones that are in the correct range of RA.
416 If a row qualifies, then 'select' it. */
417
418 for (j = 0; j < nrows; j++) {
419 ratest = cpl_table_get_float(t, "RA", j, NULL);
420 if (cpl_error_get_code() != CPL_ERROR_NONE) {
421 cpl_error_set_message_macro(__func__,
422 CPL_ERROR_DATA_NOT_FOUND,
423 __FILE__, __LINE__,
424 "No RA column in 2mass file %s",
425 fullname);
426 cpl_table_delete(t);
427 cpl_array_unwrap(a);
428 cpl_table_delete(out);
429 return (NULL);
430 }
431 if (ratest >= ramin_wrap && ratest <= ramax_wrap)
432 cpl_table_select_row(t, j);
433 }
434
435 /* Extract the rows that have been selected now and append them
436 onto the output table */
437
438 s = cpl_table_extract_selected(t);
439 if (init == 1) {
440 cpl_table_copy_structure(out, t);
441 init = 0;
442 }
443 irow = cpl_table_get_nrow(out) + 1;
444 cpl_table_insert(out, s, irow);
445
446 /* Tidy up */
447
448 cpl_table_delete(t);
449 cpl_table_delete(s);
450 }
451 }
452
453 /* Ok, now just return the table and get out of here */
454
455 cpl_array_unwrap(a);
456 return (out);
457}
458
cpl_table * irplib_2mass_extract(char *path, float ramin, float ramax, float decmin, float decmax)
Extract standards from the 2mass catalogue.
Definition irplib_cat.c:274
cpl_error_code irplib_cat_get_image_limits(const cpl_wcs *wcs, float ext_search, double *ra1, double *ra2, double *dec1, double *dec2)
Get coverage in ra, dec of a frame.
Definition irplib_cat.c:152
int irplib_2mass_get_catpars(const cpl_frame *master_index, char **catpath, char **catname)
Find the name of the standard catalogue being used and its location.
Definition irplib_cat.c:75