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