35 #include <sys/types.h>
42 #include "vircam_mods.h"
43 #include "vircam_utils.h"
44 #include "vircam_wcsutils.h"
46 #define CACHEDIR ".catcache"
47 #define CACHEIND ".catcache/index"
50 static cpl_table *vircam_2mass_extract(
char *path,
float ramin,
float ramax,
51 float decmin,
float decmax);
52 static cpl_table *vircam_ppmxl_extract(
char *path,
float ramin1,
float ramax1,
53 float decmin,
float decmax);
54 static cpl_table *check_cache(
char *catname,
float ra1_im,
float ra2_im,
55 float dec1_im,
float dec2_im);
56 static void addto_cache(cpl_table *stds,
char *catname,
float ramin,
57 float ramax,
float decmin,
float decmax);
113 char *catname, cpl_table **stds,
int *status) {
114 const char *fctid =
"vircam_getstds";
115 double xx1,xx2,yy1,yy2,r,d,xx,yy,*rad,*decd;
116 float ramin,ramax,decmin,decmax,*ra,*dec,*x,*y;
124 if (*status != VIR_OK)
129 if (strcmp(catname,
"2mass") && strcmp(catname,
"ppmxl")) {
130 cpl_msg_error(fctid,
"Catalogue %s not recognised",catname);
146 *stds = check_cache(catname,ramin,ramax,decmin,decmax);
155 if (! strcmp(catname,
"2mass")) {
156 *stds = vircam_2mass_extract(path,ramin,ramax,decmin,decmax);
157 }
else if (! strcmp(catname,
"ppmxl")) {
158 *stds = vircam_ppmxl_extract(path,ramin,ramax,decmin,decmax);
161 cpl_msg_error(fctid,
"Unable to extract data in %s\n",path);
168 addto_cache(*stds,catname,ramin,ramax,decmin,decmax);
175 n = (int)cpl_table_get_nrow(*stds);
177 cpl_table_new_column(*stds,
"xpredict",CPL_TYPE_FLOAT);
178 cpl_table_new_column(*stds,
"ypredict",CPL_TYPE_FLOAT);
184 wcs = cpl_wcs_new_from_propertylist((
const cpl_propertylist *)plist);
185 if (cpl_table_get_column_type(*stds,
"RA") == CPL_TYPE_FLOAT) {
186 ra = cpl_table_get_data_float(*stds,
"RA");
187 dec = cpl_table_get_data_float(*stds,
"Dec");
188 x = cpl_malloc(n*
sizeof(*x));
189 y = cpl_malloc(n*
sizeof(*y));
190 for (i = 0; i < n; i++) {
198 rad = cpl_table_get_data_double(*stds,
"RA");
199 decd = cpl_table_get_data_double(*stds,
"Dec");
200 x = cpl_malloc(n*
sizeof(*x));
201 y = cpl_malloc(n*
sizeof(*y));
202 for (i = 0; i < n; i++) {
214 cpl_table_wrap_float(*stds,x,
"xpredict");
215 cpl_table_set_column_unit(*stds,
"xpredict",
"pixels");
216 cpl_table_wrap_float(*stds,y,
"ypredict");
217 cpl_table_set_column_unit(*stds,
"ypredict",
"pixels");
221 p = cpl_propertylist_new();
222 cpl_propertylist_append_bool(p,
"ypredict",0);
223 cpl_table_sort(*stds,p);
224 cpl_propertylist_delete(p);
262 static cpl_table *vircam_2mass_extract(
char *path,
float ramin1,
float ramax1,
263 float decmin,
float decmax) {
265 int i,nrows,start,finish,first_index,last_index,irow,init,j;
266 int first_index_ra,last_index_ra,wrap,iwrap;
267 float dectest,ratest,ramin,ramax;
268 char fullname[SZBUF];
270 const char *deccol =
"Dec";
275 o = cpl_table_new(0);
280 a = cpl_array_wrap_string((
char **)&deccol,1);
284 wrap = (ramin1 < 0.0 && ramax1 > 0.0) ? 2 : 1;
289 for (iwrap = 0; iwrap < wrap; iwrap++) {
292 ramin = ramin1 + 360.0;
305 first_index_ra = (int)ramin;
306 last_index_ra = min((
int)ramax,359);
311 for (i = first_index_ra; i <= last_index_ra; i++) {
316 (void)snprintf(fullname,SZBUF,
"%s/npsc%03d.fits",path,i);
321 p = cpl_propertylist_load(fullname,1);
327 nrows = cpl_propertylist_get_int(p,
"NAXIS2");
328 cpl_propertylist_delete(p);
335 first_index = nrows/2;
336 while (finish - start >= 2) {
337 t = cpl_table_load_window(fullname,1,0,a,(cpl_size)first_index,
339 dectest = cpl_table_get_float(t,
"Dec",0,NULL);
341 if (dectest < decmin) {
343 first_index = (first_index + finish)/2;
345 finish = first_index;
346 first_index = (first_index + start)/2;
355 last_index = start + (finish - start)/2;
356 while (finish - start >= 2) {
357 t = cpl_table_load_window(fullname,1,0,a,(cpl_size)last_index,
359 dectest = cpl_table_get_float(t,
"Dec",0,NULL);
361 if (dectest < decmax) {
363 last_index = (last_index + finish)/2;
366 last_index = (last_index + start)/2;
369 if (last_index < first_index)
370 last_index = first_index;
374 nrows = last_index - first_index + 1;
375 if ((t = cpl_table_load_window(fullname,1,0,NULL,
376 (cpl_size)first_index,
377 (cpl_size)nrows)) == NULL) {
382 cpl_table_unselect_all(t);
388 for (j = 0; j < nrows; j++) {
389 ratest = cpl_table_get_float(t,
"RA",(cpl_size)j,NULL);
390 if (cpl_error_get_code() != CPL_ERROR_NONE) {
396 if (ratest >= ramin && ratest <= ramax)
397 cpl_table_select_row(t,(cpl_size)j);
403 s = cpl_table_extract_selected(t);
405 cpl_table_copy_structure(o,t);
408 irow = (int)cpl_table_get_nrow(o) + 1;
409 cpl_table_insert(o,s,(cpl_size)irow);
454 static cpl_table *vircam_ppmxl_extract(
char *path,
float ramin1,
float ramax1,
455 float decmin,
float decmax) {
457 int i,nrows,start,finish,first_index,last_index,irow,init,j;
458 int first_index_ra,last_index_ra,wrap,iwrap;
459 float dectest,ratest,ramin,ramax;
460 char fullname[SZBUF];
462 const char *deccol =
"Dec";
467 o = cpl_table_new(0);
472 a = cpl_array_wrap_string((
char **)&deccol,1);
476 wrap = (ramin1 < 0.0 && ramax1 > 0.0) ? 2 : 1;
481 for (iwrap = 0; iwrap < wrap; iwrap++) {
484 ramin = ramin1 + 360.0;
497 first_index_ra = (int)ramin;
498 last_index_ra = min((
int)ramax,359);
503 for (i = first_index_ra; i <= last_index_ra; i++) {
508 (void)snprintf(fullname,SZBUF,
"%s/nppmxl%03d.fits",path,i);
513 p = cpl_propertylist_load(fullname,1);
519 nrows = cpl_propertylist_get_int(p,
"NAXIS2");
520 cpl_propertylist_delete(p);
527 first_index = nrows/2;
528 while (finish - start >= 2) {
529 t = cpl_table_load_window(fullname,1,0,a,(cpl_size)first_index,
531 dectest = cpl_table_get_double(t,
"Dec",0,NULL);
533 if (dectest < decmin) {
535 first_index = (first_index + finish)/2;
537 finish = first_index;
538 first_index = (first_index + start)/2;
547 last_index = start + (finish - start)/2;
548 while (finish - start >= 2) {
549 t = cpl_table_load_window(fullname,1,0,a,(cpl_size)last_index,
551 dectest = cpl_table_get_double(t,
"Dec",0,NULL);
553 if (dectest < decmax) {
555 last_index = (last_index + finish)/2;
558 last_index = (last_index + start)/2;
561 if (last_index < first_index)
562 last_index = first_index;
566 nrows = last_index - first_index + 1;
567 if ((t = cpl_table_load_window(fullname,1,0,NULL,
568 (cpl_size)first_index,
569 (cpl_size)nrows)) == NULL) {
574 cpl_table_unselect_all(t);
580 for (j = 0; j < nrows; j++) {
581 ratest = cpl_table_get_double(t,
"RA",(cpl_size)j,NULL);
582 if (cpl_error_get_code() != CPL_ERROR_NONE) {
588 if (ratest >= ramin && ratest <= ramax)
589 cpl_table_select_row(t,(cpl_size)j);
595 s = cpl_table_extract_selected(t);
597 cpl_table_copy_structure(o,t);
600 irow = (int)cpl_table_get_nrow(o) + 1;
601 cpl_table_insert(o,s,(cpl_size)irow);
646 static cpl_table *check_cache(
char *catname,
float ra1_im,
float ra2_im,
647 float dec1_im,
float dec2_im) {
650 char fname[BUFSIZ],catname2[SZBUF],cat_cache[SZBUF];
651 float best,ra1_cat,ra2_cat,dec1_cat,dec2_cat,d1,d2,fra,fdec,ftot;
656 fd = fopen(CACHEIND,
"r");
662 wrap1 = (ra1_im < 0.0 ? 1 : 0);
667 while (fscanf(fd,
"%s %s %g %g %g %g",fname,catname2,&ra1_cat,&ra2_cat,
668 &dec1_cat,&dec2_cat) != EOF) {
669 wrap2 = (ra1_cat < 0.0 ? 1 : 0);
672 if (strcmp(catname,catname2))
677 if (!(((ra1_im >= ra1_cat && ra1_im <= ra2_cat) ||
678 (ra2_im >= ra1_cat && ra2_im <= ra2_cat)) &&
679 ((dec1_im >= dec1_cat && dec1_im <= dec2_cat) ||
680 (dec2_im >= dec1_cat && dec2_im <= dec2_cat))))
685 d1 = max(0.0,ra1_cat-ra1_im);
686 d2 = max(0.0,ra2_im-ra2_cat);
687 fra = 1.0 - (d1 + d2)/(ra2_im - ra1_im);
688 d1 = max(0.0,dec1_cat-dec1_im);
689 d2 = max(0.0,dec2_im-dec2_cat);
690 fdec = 1.0 - (d1 + d2)/(dec2_im - dec1_im);
696 snprintf(cat_cache,SZBUF,
"%s/%s",CACHEDIR,fname);
710 out_cat = cpl_table_load(cat_cache,1,0);
742 static void addto_cache(cpl_table *stds,
char *catname,
float ramin,
743 float ramax,
float decmin,
float decmax) {
751 if (access(CACHEDIR,0) != 0)
752 mkdir(CACHEDIR,0755);
756 fd = fopen(CACHEIND,
"a");
764 snprintf(newname,SZBUF,
"%s/cch_%08d",CACHEDIR,i);
765 if (access(newname,F_OK) != 0)
772 snprintf(newname,SZBUF,
"%s/cch_%08d",CACHEDIR,i);
773 cpl_table_save(stds,NULL,NULL,newname,CPL_IO_DEFAULT);
774 snprintf(newname,SZBUF,
"cch_%08d",i);
775 if (cpl_error_get_code() == CPL_ERROR_NONE)
776 fprintf(fd,
"%s %s %g %g %g %g\n",newname,catname,ramin-0.0005,
777 ramax+0.0005,decmin-0.0005,decmax+0.0005);
void vircam_radectoxy(cpl_wcs *wcs, double ra, double dec, double *x, double *y)
int vircam_getstds(cpl_propertylist *plist, int cache, char *path, char *catname, cpl_table **stds, int *status)
Get a table of standard stars that appear on an image from a catalogue.
int vircam_coverage(cpl_propertylist *plist, int fudge, double *ra1, double *ra2, double *dec1, double *dec2, int *status)