40 #include "irplib_cat.h"
41 #include "irplib_wcs.h"
43 #define FILENAME_SZBUF 1024
75 (
const cpl_frame * master_index,
80 const char * unk =
"unknown";
82 int status = CPL_ERROR_NONE;
89 fname = cpl_strdup(cpl_frame_get_filename(master_index));
90 if (access((
const char *)fname,R_OK) != 0)
92 cpl_msg_error(__func__,
"Can't access index file %s",fname);
94 return CPL_ERROR_FILE_IO;
96 *catpath = cpl_strdup(dirname(fname));
100 if ((p = cpl_propertylist_load(cpl_frame_get_filename(master_index),0)) == NULL)
102 cpl_msg_error(__func__,
"Can't load index file header %s",fname);
105 return CPL_ERROR_FILE_IO;
110 if (cpl_propertylist_has(p,
"CATNAME"))
112 *catname = cpl_strdup(cpl_propertylist_get_string(p,
"CATNAME"));
113 status = CPL_ERROR_NONE;
115 *catname = cpl_strdup(unk);
116 cpl_msg_warning(__func__,
"Property CATNAME not in index file header %s",
122 cpl_propertylist_delete(p);
154 (
const cpl_wcs * wcs,
184 return CPL_ERROR_DATA_NOT_FOUND;
188 a = cpl_wcs_get_image_dims(wcs);
190 return CPL_ERROR_ILLEGAL_INPUT;
191 naxes = cpl_array_get_data_int_const(a);
203 for (j = 1; j < naxes[1]; j += 10) {
205 for (i = 1; i < naxes[0]; i += 10) {
207 irplib_wcs_xytoradec(wcs,x,y,&ra,&dec);
208 if (ra >= 0.0 && ra <= 90.0) {
212 }
else if (ra >= 270.0 && ra <= 360.0) {
214 if(ra - 360.0 < min_4q)
233 if (first_quad && fourth_quad) {
241 dra = 0.5*ext_search*(*ra2 - *ra1);
244 ddec = 0.5*ext_search*(*dec2 - *dec1);
250 return CPL_ERROR_NONE;
288 int i,nrows,start,finish,first_index,last_index,irow,init,j;
289 int first_index_ra,last_index_ra,wrap,iwrap;
290 float dectest,ratest,ramin_wrap,ramax_wrap;
291 char fullname[FILENAME_SZBUF];
293 const char *deccol[] = {
"Dec"};
298 out = cpl_table_new(0);
304 a = cpl_array_wrap_string((
char **)deccol,1);
308 wrap = (ramin < 0.0 && ramax > 0.0) ? 2 : 1;
313 for (iwrap = 0; iwrap < wrap; iwrap++) {
316 ramin_wrap = ramin + 360.0;
319 ramin_wrap = 0.000001;
329 first_index_ra = (int)ramin_wrap;
330 last_index_ra = (int)ramax_wrap;
331 if(last_index_ra > 359)
337 for (i = first_index_ra; i <= last_index_ra; i++)
343 (void)snprintf(fullname,FILENAME_SZBUF,
"%s/npsc%03d.fits",path,i);
348 p = cpl_propertylist_load(fullname,1);
351 cpl_error_set_message_macro(__func__,CPL_ERROR_DATA_NOT_FOUND,
352 __FILE__, __LINE__,
"2mass file %s missing",fullname);
353 cpl_table_delete(out);
357 nrows = cpl_propertylist_get_int(p,
"NAXIS2");
358 cpl_propertylist_delete(p);
365 first_index = nrows/2;
366 while (finish - start >= 2)
368 t = cpl_table_load_window(fullname, 1, 0, a, first_index, 1);
369 dectest = cpl_table_get_float(t,
"Dec", 0, NULL);
371 if (dectest < decmin)
374 first_index = (first_index + finish)/2;
378 finish = first_index;
379 first_index = (first_index + start)/2;
388 last_index = start + (finish - start)/2;
389 while (finish - start >= 2)
391 t = cpl_table_load_window(fullname, 1, 0, a, last_index, 1);
392 dectest = cpl_table_get_float(t,
"Dec", 0, NULL);
394 if (dectest < decmax)
397 last_index = (last_index + finish)/2;
402 last_index = (last_index + start)/2;
405 if (last_index < first_index)
406 last_index = first_index;
410 nrows = last_index - first_index + 1;
411 if ((t = cpl_table_load_window(fullname, 1, 0, NULL, first_index,
414 cpl_error_set_message_macro(__func__,CPL_ERROR_DATA_NOT_FOUND,
415 __FILE__, __LINE__,
"Error in subset of 2mass file %s ",
417 cpl_table_delete(out);
421 cpl_table_unselect_all(t);
427 for (j = 0; j < nrows; j++)
429 ratest = cpl_table_get_float(t,
"RA", j, NULL);
430 if (cpl_error_get_code() != CPL_ERROR_NONE)
432 cpl_error_set_message_macro(__func__,CPL_ERROR_DATA_NOT_FOUND,
433 __FILE__, __LINE__,
"No RA column in 2mass file %s",
437 cpl_table_delete(out);
440 if (ratest >= ramin_wrap && ratest <= ramax_wrap)
441 cpl_table_select_row(t, j);
447 s = cpl_table_extract_selected(t);
450 cpl_table_copy_structure(out, t);
453 irow = cpl_table_get_nrow(out) + 1;
454 cpl_table_insert(out, s, irow);