00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include <libgen.h>
00037 #include <math.h>
00038 #include <cpl.h>
00039
00040 #include "irplib_cat.h"
00041 #include "irplib_wcs.h"
00042
00043 #define FILENAME_SZBUF 1024
00044
00045
00049
00050
00053
00072
00073
00074 int irplib_2mass_get_catpars
00075 (const cpl_frame * master_index,
00076 char ** catpath,
00077 char ** catname)
00078 {
00079 cpl_propertylist * p;
00080 char * fname;
00081 int status = CPL_ERROR_NONE;
00082
00083
00084 *catpath = NULL;
00085 *catname = NULL;
00086
00087
00088 fname = cpl_strdup(cpl_frame_get_filename(master_index));
00089 if (access((const char *)fname,R_OK) != 0)
00090 {
00091 cpl_msg_error(__func__,"Can't access index file %s",fname);
00092 cpl_free(fname);
00093 return CPL_ERROR_FILE_IO;
00094 }
00095 *catpath = cpl_strdup(dirname(fname));
00096
00097
00098
00099 if ((p = cpl_propertylist_load(cpl_frame_get_filename(master_index),0)) == NULL)
00100 {
00101 cpl_msg_error(__func__,"Can't load index file header %s",fname);
00102 cpl_free(*catpath);
00103 cpl_free(fname);
00104 return CPL_ERROR_FILE_IO;
00105 }
00106
00107
00108
00109 if (cpl_propertylist_has(p,"CATNAME"))
00110 {
00111 *catname = cpl_strdup(cpl_propertylist_get_string(p,"CATNAME"));
00112 status = CPL_ERROR_NONE;
00113 } else {
00114 const char * unk = "unknown";
00115 *catname = cpl_strdup(unk);
00116 cpl_msg_warning(__func__,"Property CATNAME not in index file header %s",
00117 fname);
00118 }
00119
00120
00121 cpl_free(fname);
00122 cpl_propertylist_delete(p);
00123 return(status);
00124 }
00125
00126
00127
00128
00151
00152
00153 cpl_error_code irplib_cat_get_image_limits
00154 (const cpl_wcs * wcs,
00155 float ext_search,
00156 double * ra1,
00157 double * ra2,
00158 double * dec1,
00159 double * dec2)
00160 {
00161 double min_4q;
00162 double max_1q;
00163 int first_quad;
00164 int fourth_quad;
00165 const int * naxes;
00166 long j;
00167 const cpl_array * a;
00168
00169
00170 *ra1 = 0.0;
00171 *ra2 = 0.0;
00172 *dec1 = 0.0;
00173 *dec2 = 0.0;
00174
00175
00176 if (wcs == NULL)
00177 return CPL_ERROR_DATA_NOT_FOUND;
00178
00179
00180
00181 a = cpl_wcs_get_image_dims(wcs);
00182 if(a == NULL)
00183 return CPL_ERROR_ILLEGAL_INPUT;
00184 naxes = cpl_array_get_data_int_const(a);
00185
00186
00187
00188 *ra1 = 370.0;
00189 *ra2 = -370.0;
00190 *dec1 = 95.0;
00191 *dec2 = -95.0;
00192 first_quad = 0;
00193 fourth_quad = 0;
00194 min_4q = 370.0;
00195 max_1q = 0.0;
00196 for (j = 1; j < naxes[1]; j += 10) {
00197 long i;
00198 double y = (double)j;
00199 for (i = 1; i < naxes[0]; i += 10) {
00200 double ra, dec;
00201 double x = (double)i;
00202 irplib_wcs_xytoradec(wcs,x,y,&ra,&dec);
00203 if (ra >= 0.0 && ra <= 90.0) {
00204 first_quad = 1;
00205 if(ra > max_1q)
00206 max_1q = ra;
00207 } else if (ra >= 270.0 && ra <= 360.0) {
00208 fourth_quad = 1;
00209 if(ra - 360.0 < min_4q)
00210 min_4q = ra - 360.0;
00211 }
00212 if(ra < *ra1)
00213 *ra1 = ra;
00214 if(ra > *ra2)
00215 *ra2 = ra;
00216 if(dec < *dec1)
00217 *dec1 = dec;
00218 if(dec > *dec2)
00219 *dec2 = dec;
00220 }
00221 }
00222
00223
00224
00225
00226
00227
00228 if (first_quad && fourth_quad) {
00229 *ra1 = min_4q;
00230 *ra2 = max_1q;
00231 }
00232
00233
00234 if (ext_search)
00235 {
00236 double dra, ddec;
00237 dra = 0.5*ext_search*(*ra2 - *ra1);
00238 *ra1 -= dra;
00239 *ra2 += dra;
00240 ddec = 0.5*ext_search*(*dec2 - *dec1);
00241 *dec1 -= ddec;
00242 *dec2 += ddec;
00243 }
00244
00245
00246 return CPL_ERROR_NONE;
00247 }
00248
00249
00273
00274
00275 cpl_table * irplib_2mass_extract
00276 (char *path,
00277 float ramin,
00278 float ramax,
00279 float decmin,
00280 float decmax)
00281 {
00282 cpl_table *t,*s;
00283 cpl_table *out;
00284 int i,nrows,start,finish,first_index,last_index,irow,init,j;
00285 int wrap,iwrap;
00286 float dectest,ratest,ramin_wrap,ramax_wrap;
00287 char fullname[FILENAME_SZBUF];
00288 cpl_array *a;
00289 const char *deccol[] = {"Dec"};
00290 cpl_propertylist *p;
00291
00292
00293
00294 out = cpl_table_new(0);
00295 init = 1;
00296
00297
00298
00299
00300 a = cpl_array_wrap_string((char **)deccol,1);
00301
00302
00303
00304 wrap = (ramin < 0.0 && ramax > 0.0) ? 2 : 1;
00305
00306
00307
00308
00309 for (iwrap = 0; iwrap < wrap; iwrap++) {
00310 int first_index_ra,last_index_ra;
00311
00312 if (wrap == 2) {
00313 if (iwrap == 0) {
00314 ramin_wrap = ramin + 360.0;
00315 ramax_wrap = 360.0;
00316 } else {
00317 ramin_wrap = 0.000001;
00318 ramax_wrap = ramax;
00319 }
00320 } else {
00321 ramin_wrap = ramin;
00322 ramax_wrap = ramax;
00323 }
00324
00325
00326
00327 first_index_ra = (int)ramin_wrap;
00328 last_index_ra = (int)ramax_wrap;
00329 if(last_index_ra > 359)
00330 last_index_ra = 359;
00331
00332
00333
00334
00335 for (i = first_index_ra; i <= last_index_ra; i++)
00336 {
00337
00338
00339
00340
00341 (void)snprintf(fullname,FILENAME_SZBUF,"%s/npsc%03d.fits",path,i);
00342
00343
00344
00345
00346 p = cpl_propertylist_load(fullname,1);
00347 if (p == NULL)
00348 {
00349 cpl_error_set_message_macro(__func__,CPL_ERROR_DATA_NOT_FOUND,
00350 __FILE__, __LINE__, "2mass file %s missing",fullname);
00351 cpl_table_delete(out);
00352 cpl_array_unwrap(a);
00353 return(NULL);
00354 }
00355 nrows = cpl_propertylist_get_int(p, "NAXIS2");
00356 cpl_propertylist_delete(p);
00357
00358
00359
00360
00361 start = 0;
00362 finish = nrows;
00363 first_index = nrows/2;
00364 while (finish - start >= 2)
00365 {
00366 t = cpl_table_load_window(fullname, 1, 0, a, first_index, 1);
00367 dectest = cpl_table_get_float(t, "Dec", 0, NULL);
00368 cpl_table_delete(t);
00369 if (dectest < decmin)
00370 {
00371 start = first_index;
00372 first_index = (first_index + finish)/2;
00373 }
00374 else
00375 {
00376 finish = first_index;
00377 first_index = (first_index + start)/2;
00378 }
00379 }
00380
00381
00382
00383
00384 start = first_index;
00385 finish = nrows;
00386 last_index = start + (finish - start)/2;
00387 while (finish - start >= 2)
00388 {
00389 t = cpl_table_load_window(fullname, 1, 0, a, last_index, 1);
00390 dectest = cpl_table_get_float(t, "Dec", 0, NULL);
00391 cpl_table_delete(t);
00392 if (dectest < decmax)
00393 {
00394 start = last_index;
00395 last_index = (last_index + finish)/2;
00396 }
00397 else
00398 {
00399 finish = last_index;
00400 last_index = (last_index + start)/2;
00401 }
00402 }
00403 if (last_index < first_index)
00404 last_index = first_index;
00405
00406
00407
00408 nrows = last_index - first_index + 1;
00409 if ((t = cpl_table_load_window(fullname, 1, 0, NULL, first_index,
00410 nrows)) == NULL)
00411 {
00412 cpl_error_set_message_macro(__func__,CPL_ERROR_DATA_NOT_FOUND,
00413 __FILE__, __LINE__, "Error in subset of 2mass file %s ",
00414 fullname);
00415 cpl_table_delete(out);
00416 cpl_array_unwrap(a);
00417 return (NULL);
00418 }
00419 cpl_table_unselect_all(t);
00420
00421
00422
00423
00424
00425 for (j = 0; j < nrows; j++)
00426 {
00427 ratest = cpl_table_get_float(t, "RA", j, NULL);
00428 if (cpl_error_get_code() != CPL_ERROR_NONE)
00429 {
00430 cpl_error_set_message_macro(__func__,CPL_ERROR_DATA_NOT_FOUND,
00431 __FILE__, __LINE__, "No RA column in 2mass file %s",
00432 fullname);
00433 cpl_table_delete(t);
00434 cpl_array_unwrap(a);
00435 cpl_table_delete(out);
00436 return (NULL);
00437 }
00438 if (ratest >= ramin_wrap && ratest <= ramax_wrap)
00439 cpl_table_select_row(t, j);
00440 }
00441
00442
00443
00444
00445 s = cpl_table_extract_selected(t);
00446 if (init == 1)
00447 {
00448 cpl_table_copy_structure(out, t);
00449 init = 0;
00450 }
00451 irow = cpl_table_get_nrow(out) + 1;
00452 cpl_table_insert(out, s, irow);
00453
00454
00455
00456 cpl_table_delete(t);
00457 cpl_table_delete(s);
00458 }
00459 }
00460
00461
00462
00463 cpl_array_unwrap(a);
00464 return(out);
00465 }