41 #include "omega_photometry.h"
42 #include "omega_catalog.h"
43 #include "omega_dfs.h"
44 #include "omega_pfits.h"
45 #include "omega_utils.h"
80 const cpl_frame *refcat, cpl_parameterlist *pars, cpl_table **extra)
82 int i, nsources, nstds;
86 double omega_x1,omega_x2,omega_y1,omega_y2;
88 double photom_thre = 0.0;
90 double pixscalex = 0.0;
91 double pixscaley = 0.0;
93 double ellipticity = 0.0;
94 double aperture = 0.0;
96 const char *text = NULL;
98 const char *sex_conf = NULL;
99 const char *sex_conv = NULL;
100 const char *sex_par = NULL;
101 const char *sex_nnw = NULL;
102 const char *srcs =
"omega_cat_photom_sources.fits";
105 cpl_table *sources, *stds, *data;
106 cpl_table *matchstds = NULL;
107 cpl_matrix *from, *to;
110 cpl_propertylist *xlist = NULL;
111 cpl_propertylist *p, *filterlist;
114 npars = cpl_parameterlist_get_size(pars);
115 par = cpl_parameterlist_get_first(pars);
117 for(i=0; i<npars; i++) {
118 text = cpl_parameter_get_alias(par, CPL_PARAMETER_MODE_CLI);
119 if(strcmp(
"bin-path", text) == 0) {
120 path = cpl_parameter_get_string(par) ;
122 else if(strcmp(
"sex-config", text) == 0) {
123 sex_conf = cpl_parameter_get_string(par) ;
125 else if(strcmp(
"sex-conv", text) == 0) {
126 sex_conv = cpl_parameter_get_string(par) ;
128 else if(strcmp(
"sex-param", text) == 0) {
129 sex_par = cpl_parameter_get_string(par) ;
131 else if(strcmp(
"sex-nnw", text) == 0) {
132 sex_nnw = cpl_parameter_get_string(par) ;
134 else if(strcmp(
"photom-thre", text) == 0){
135 photom_thre = cpl_parameter_get_double(par);
137 else if(strcmp(
"radius", text) == 0){
138 radius = cpl_parameter_get_double(par);
140 else if(strcmp(
"aperture", text) == 0){
141 aperture = cpl_parameter_get_double(par);
143 par = cpl_parameterlist_get_next(pars);
149 cmd = cpl_sprintf(
"%s/sex %s -c %s -PARAMETERS_NAME %s -FILTER_NAME %s -STARNNW_NAME %s "
150 "-FILTER Y -DETECT_THRESH %g -ANALYSIS_THRESH %g -GAIN %g -PHOT_APERTURES %g -CATALOG_TYPE FITS_1.0 "
151 "-CATALOG_NAME %s -CHECKIMAGE_NAME %s -CHECKIMAGE_TYPE OBJECTS -DETECT_MINAREA 5",
152 path, star, sex_conf, sex_par, sex_conv, sex_nnw, photom_thre, 3.0, gain, aperture,
153 srcs,
"omega_ima_detected_photom_srcs.fits");
155 cpl_msg_debug(cpl_func,
"cmd: %s",cmd);
156 if (system(cmd) != 0) {
157 cpl_msg_error(cpl_func,
"Failed to detect sources in image using Sextractor");
163 data = cpl_table_load(srcs, 1, 0);
165 cpl_msg_error(cpl_func,
"Sources table is NULL");
172 cpl_msg_debug(cpl_func,
"Number of stars in reduced image: %d", nsources);
175 xlist = cpl_propertylist_load(star, 1);
176 omega_get_pixelscale(xlist, &pixscalex, &pixscaley);
180 p = cpl_propertylist_load(star, 0);
183 if (cpl_propertylist_has(xlist,
"EXTNAME") == 1) {
184 cpl_propertylist_update_string(p,
"EXTNAME",
185 cpl_propertylist_get_string(xlist,
"EXTNAME"));
197 cpl_msg_warning(cpl_func,
"Cannot find stars with CLASS_STAR near 1 to calculate seeing");
201 *extra = cpl_table_new(1);
202 cpl_table_new_column(*extra,
"FILT_ID", CPL_TYPE_STRING);
203 cpl_table_set_string(*extra,
"FILT_ID",0, cpl_propertylist_get_string(filterlist,
"FILT_ID"));
204 cpl_table_new_column(*extra,
"FILT_NAME", CPL_TYPE_STRING);
205 cpl_table_set_string(*extra,
"FILT_NAME",0, cpl_propertylist_get_string(filterlist,
"FILT_NAME"));
206 cpl_table_new_column(*extra,
"REF_MAG_ID", CPL_TYPE_STRING);
207 cpl_table_set_string(*extra,
"REF_MAG_ID",0, cpl_propertylist_get_string(filterlist,
"REF_MAG_ID"));
208 cpl_table_new_column(*extra,
"REF_MAG_ID_ERR", CPL_TYPE_STRING);
209 cpl_table_set_string(*extra,
"REF_MAG_ID_ERR",0, cpl_propertylist_get_string(filterlist,
"REF_MAG_ID_ERR"));
210 cpl_table_new_column(*extra,
"CWL", CPL_TYPE_DOUBLE);
211 cpl_table_set_double(*extra,
"CWL",0, cpl_propertylist_get_double(filterlist,
"CWL"));
212 cpl_table_new_column(*extra,
"EXPTIME", CPL_TYPE_DOUBLE);
213 if(cpl_propertylist_has(xlist,
"EXPTIME") == 0)
214 cpl_table_set_double(*extra,
"EXPTIME",0, exptime);
216 cpl_table_set_double(*extra,
"EXPTIME",0, cpl_propertylist_get_double(xlist,
"EXPTIME"));
218 cpl_table_new_column(*extra,
"AIRMASS", CPL_TYPE_DOUBLE);
219 cpl_table_set_double(*extra,
"AIRMASS",0, airmass);
220 cpl_table_new_column(*extra,
"CHIP_ID", CPL_TYPE_STRING);
222 cpl_table_new_column(*extra,
"SEEING", CPL_TYPE_DOUBLE);
223 cpl_table_set_double(*extra,
"SEEING", 0, seeing);
224 cpl_table_new_column(*extra,
"ELLIPTICITY", CPL_TYPE_DOUBLE);
225 cpl_table_set_double(*extra,
"ELLIPTICITY", 0, ellipticity);
227 freeplist(filterlist);
230 p = cpl_propertylist_new();
231 cpl_propertylist_append_bool(p,
"Y_IMAGE",0);
232 if (cpl_table_sort(sources, p) != CPL_ERROR_NONE) {
233 cpl_msg_error(cpl_func,
"Cannot sort sources table");
234 cpl_propertylist_delete(p);
235 cpl_propertylist_delete(xlist);
242 from = cpl_matrix_new(nsources, 2);
243 for (i=0; i<nsources; i++){
244 float xim = cpl_table_get_float(sources,
"X_IMAGE", i, NULL);
245 float yim = cpl_table_get_float(sources,
"Y_IMAGE", i, NULL);
246 cpl_matrix_set(from, i, 0, xim);
247 cpl_matrix_set(from, i, 1, yim);
251 if(
omega_get_coverage(xlist, 0, from, &omega_x1, &omega_x2, &omega_y1, &omega_y2) != 0){
252 cpl_msg_error(cpl_func,
"Cannot get coverage of image");
263 coords = cpl_vector_new(4);
264 cpl_vector_set(coords, 0, omega_x1);
265 cpl_vector_set(coords, 1, omega_x2);
266 cpl_vector_set(coords, 2, omega_y1);
267 cpl_vector_set(coords, 3, omega_y2);
271 cpl_msg_warning(cpl_func,
"Cannot find reference standard stars in this region: "
272 "{RA-RA:DEC-DEC}={%g-%g:%g=%g}", omega_x1,omega_x2,omega_y1,omega_y2);
280 cpl_msg_debug(cpl_func,
"%d reference standards fall in region: {RA-RA:DEC-DEC}={%g-%g:%g=%g}",
281 nstds,omega_x1,omega_x2,omega_y1,omega_y2);
284 from = cpl_matrix_new(nstds, 2);
285 for (i=0; i<nstds; i++){
286 cpl_matrix_set(from, i, 0, cpl_table_get_double(stds,
"Ra", i, NULL));
287 cpl_matrix_set(from, i, 1, cpl_table_get_double(stds,
"Dec", i, NULL));
292 wcs = cpl_wcs_new_from_propertylist(xlist);
293 if(cpl_wcs_convert(wcs, from, &to, &status, CPL_WCS_WORLD2PHYS) != CPL_ERROR_NONE){
295 cpl_array_delete(status);
301 cpl_msg_error(cpl_func,
"Error in cpl_wcs conversion. %s",cpl_error_get_message());
307 cpl_array_delete(status);
310 cpl_table_new_column(stds,
"xpredict",CPL_TYPE_FLOAT);
311 cpl_table_set_column_unit(stds,
"xpredict",
"pixels");
312 cpl_table_new_column(stds,
"ypredict",CPL_TYPE_FLOAT);
313 cpl_table_set_column_unit(stds,
"ypredict",
"pixels");
315 for(i=0; i< nstds; i++){
316 float xp = (float)cpl_matrix_get(to, i, 0);
317 float yp = (float)cpl_matrix_get(to, i, 1);
318 cpl_table_set_float(stds,
"xpredict", i, xp);
319 cpl_table_set_float(stds,
"ypredict", i, yp);
324 p = cpl_propertylist_new();
325 cpl_propertylist_append_bool(p,
"ypredict",0);
326 if (cpl_table_sort(stds, p) != CPL_ERROR_NONE) {
327 cpl_msg_error(cpl_func,
"Cannot sort stds table");
337 cpl_msg_error(cpl_func,
"Failed to match reference standards to image sources. %s",cpl_error_get_message());
343 nmatches = cpl_table_get_nrow(matchstds);
345 cpl_msg_info(cpl_func,
"Not enough matches (n=%d). Trying again...",nmatches);
348 radius = radius + 10.0;
349 freetable(matchstds);
351 cpl_msg_error(cpl_func,
"Failed to match reference standards to image sources. %s",
352 cpl_error_get_message());
355 freetable(matchstds);
358 nmatches = cpl_table_get_nrow(matchstds);
365 cpl_msg_warning(cpl_func,
"There are no standard stars in image");
368 freetable(matchstds);
372 cpl_msg_info(cpl_func,
"Found %d standard stars in image", nmatches);
381 void omega_get_extinction(cpl_table *extra, cpl_table *extinct2,
382 cpl_table *extinct1,
double *extinction, cpl_table *monit2,
383 double *shift_err,
double *extinction_airmass,
384 double *extinction_error_kwad)
392 double temp_ext = 0.;
401 cwl = cpl_table_get_double(extra,
"CWL", 0, NULL);
402 airmass = cpl_table_get_double(extra,
"AIRMASS", 0, NULL);
403 wincr = cpl_table_get_float(extinct2,
"wavel_incr", 0, NULL);
404 lwr = cwl - fmod(cwl, wincr);
405 numrows = cpl_table_get_nrow(extinct1);
406 for(i=0; i< numrows; i++) {
407 int number = cpl_table_get_float(extinct1,
"wavelength", i, NULL);
409 lwr_ext = cpl_table_get_float(extinct1,
"extinction",i, NULL);
413 upr = cwl - fmod(cwl, wincr) + wincr;
414 for(i=0; i< numrows; i++) {
415 int number = cpl_table_get_float(extinct1,
"wavelength", i, NULL);
417 upr_ext = cpl_table_get_float(extinct1,
"extinction",i, NULL);
421 slope = (upr_ext - lwr_ext) / wincr;
422 temp_ext = fmod(cwl, wincr);
423 *extinction = (temp_ext * slope) + lwr_ext;
427 shift = cpl_table_get_float(monit2,
"shift", 0, NULL);
428 *shift_err = cpl_table_get_float(monit2,
"shift_err", 0, NULL);
429 *extinction_airmass = *extinction + shift;
430 *extinction = (*extinction_airmass) * (airmass);
431 *extinction_error_kwad = ((*shift_err) * (airmass)) * ((*shift_err) * (airmass));
448 cpl_table *
omega_zeropoints(cpl_table *matches, cpl_table *extra,
const cpl_frame *monitfr,
449 const cpl_frame *extfr,
const cpl_frame *colfr)
455 double extinction = 0.0;
456 double shift_err = 0.0;
457 double extinction_airmass = 0.0;
459 double ZPerror = 0.0;
460 double extinction_error_kwad = 0.0;
461 double weight_sum = 0.0;
462 double sum_data = 0.0;
463 double clip_median=0.;
464 double clip_stdev=0.;
469 const char *refmagerr;
470 const char *filt_id = NULL;
471 const char *filt_name = NULL;
473 cpl_table *raw_tbl, *monit1, *monit2;
474 cpl_table *extinct1, *extinct2;
475 cpl_table *coltbl = NULL;
476 cpl_table *tbl, *zptbl;
477 cpl_table * raw_tbl_tmp=NULL;
481 monit1 = cpl_table_load(cpl_frame_get_filename(monitfr), 1, 0);
482 if (monit1 == NULL) {
483 cpl_msg_error(cpl_func,
"Cannot load 1st extension of monitoring report table");
487 monit2 = cpl_table_load(cpl_frame_get_filename(monitfr), 2, 0);
488 if (monit2 == NULL) {
489 cpl_msg_error(cpl_func,
"Cannot load 2nd extension of monitoring report table");
495 extinct1 = cpl_table_load(cpl_frame_get_filename(extfr), 1, 0);
496 if (extinct1 == NULL) {
497 cpl_msg_error(cpl_func,
"Cannot load 1st extension of std extinction table");
503 extinct2 = cpl_table_load(cpl_frame_get_filename(extfr), 2, 0);
504 if (extinct2 == NULL) {
505 cpl_msg_error(cpl_func,
"Cannot load 2nd extension of std extinction table");
514 coltbl = cpl_table_load(cpl_frame_get_filename(colfr), 1, 0);
516 cpl_msg_error(cpl_func,
"Cannot load colour terms table");
525 filt_id = cpl_table_get_string(extra,
"FILT_ID", 0);
526 filt_name = cpl_table_get_string(extra,
"FILT_NAME", 0);
541 nrows = cpl_table_get_nrow(matches);
542 raw_tbl = cpl_table_new(nrows);
543 refmag = cpl_table_get_string(extra,
"REF_MAG_ID", 0);
544 refmagerr = cpl_table_get_string(extra,
"REF_MAG_ID_ERR", 0);
546 cpl_table_duplicate_column(raw_tbl,
"mag", matches, refmag);
547 cpl_table_duplicate_column(raw_tbl,
"mag_err", matches, refmagerr);
548 cpl_table_duplicate_column(raw_tbl,
"instmag", matches,
"MAG_APER");
549 cpl_table_duplicate_column(raw_tbl,
"instmag_err", matches,
"MAGERR_APER");
558 raw_tbl = cpl_table_duplicate(tbl);
561 if(cpl_table_get_column_mean(raw_tbl,
"mag") == 0.0) {
562 cpl_msg_warning(cpl_func,
"Reference table magnitudes (%s) are NULL for the selected stds.",refmag);
568 if(coltbl) freetable(coltbl);
572 cpl_table_subtract_columns(raw_tbl,
"mag",
"instmag");
577 for(j = 0; j < 8; j++){
578 if(strcmp(filt_name, cpl_table_get_string(coltbl,
"FILT_NAME", j)) == 0){
579 colour = cpl_table_get_double(coltbl,
"COLOUR_TERM",j, NULL);
582 else if(strcmp(filt_id, cpl_table_get_string(coltbl,
"FILT_ID", j)) == 0){
583 colour = cpl_table_get_double(coltbl,
"COLOUR_TERM",j, NULL);
588 cpl_table_subtract_scalar(raw_tbl,
"mag", colour);
592 cpl_table_erase_column(raw_tbl,
"instmag");
593 cpl_table_name_column(raw_tbl,
"mag",
"rawZP");
599 if(cpl_table_get_nrow(raw_tbl)>=3){
601 raw_tbl_tmp=cpl_table_duplicate(raw_tbl);
604 clip_median= cpl_table_get_column_median(raw_tbl_tmp,
"rawZP");
605 clip_stdev = cpl_table_get_column_stdev(raw_tbl_tmp,
"rawZP");
606 clip_min=clip_median - 2 * clip_stdev;
607 clip_max=clip_median + 2 * clip_stdev;
609 cpl_msg_info(cpl_func,
"Using objects with magnitudes within [%g, %g] "
610 "to derive the weighted zeropoint", clip_min, clip_max);
613 cpl_table_and_selected_float(raw_tbl_tmp,
"rawZP", CPL_NOT_GREATER_THAN,
615 cpl_table_and_selected_float(raw_tbl_tmp,
"rawZP", CPL_NOT_LESS_THAN,
618 raw_tbl=cpl_table_extract_selected(raw_tbl_tmp);
619 freetable(raw_tbl_tmp);
624 cpl_table_power_column(raw_tbl,
"mag_err", 2.0);
625 cpl_table_power_column(raw_tbl,
"instmag_err", 2.0);
626 cpl_table_add_columns(raw_tbl,
"mag_err",
"instmag_err");
627 cpl_table_power_column(raw_tbl,
"mag_err",0.5);
628 cpl_table_erase_column(raw_tbl,
"instmag_err");
630 cpl_table_name_column(raw_tbl,
"mag_err",
"rawZP_err");
631 omega_get_extinction(extra, extinct2, extinct1, &extinction, monit2,
632 &shift_err, &extinction_airmass, &extinction_error_kwad);
634 cpl_table_add_scalar(raw_tbl,
"rawZP", extinction);
635 cpl_table_power_column(raw_tbl,
"rawZP_err", 2.0);
636 cpl_table_add_scalar(raw_tbl,
"rawZP_err", extinction_error_kwad);
637 cpl_table_power_column(raw_tbl,
"rawZP_err", 0.5);
638 cpl_table_name_column(raw_tbl,
"rawZP",
"zeropoint");
639 cpl_table_name_column(raw_tbl,
"rawZP_err",
"zeropoint_err");
641 cpl_table_duplicate_column(raw_tbl,
"weights", raw_tbl,
"zeropoint_err");
643 cpl_table_power_column(raw_tbl,
"weights", -2.);
647 for (i=0; i<nrows; i++) {
648 weight_sum += cpl_table_get_float(raw_tbl,
"weights", i, NULL);
651 cpl_table_multiply_columns(raw_tbl,
"zeropoint",
"weights");
654 for(i=0; i<nrows; i++) {
655 sum_data += cpl_table_get_float(raw_tbl,
"zeropoint", i, NULL);
659 meanZP = sum_data/weight_sum;
660 ZPerror = sqrt(1./weight_sum);
662 cpl_msg_debug(cpl_func,
"meanZP: %f ZPerror: %f",meanZP,ZPerror);
665 cpl_msg_info(
"",
"Photometric parameters for filter %s",filt_name);
666 cpl_msg_info(
"",
"Weighted Average Zeropoint (mag) %0.5g", meanZP);
667 cpl_msg_info(
"",
"Zeropoint error (mag) %0.5g", ZPerror);
668 cpl_msg_info(
"",
"Extinction (mag/airmass) %0.5g", extinction_airmass);
669 cpl_msg_info(
"",
"Extinction times airmass (mag) %0.5g", extinction);
670 cpl_msg_info(
"",
"Extinction error (mag/airmass) %0.5g", shift_err);
671 cpl_msg_info(
"",
"Seeing (arcsec) %0.5g", cpl_table_get_double(extra,
"SEEING",0,NULL));
672 cpl_msg_info(
"",
"Ellipticity %0.5g", cpl_table_get_double(extra,
"ELLIPTICITY",0,NULL));
677 zptbl = cpl_table_new(1);
678 cpl_table_duplicate_column(zptbl,
"CHIP_ID", extra,
"CHIP_ID");
679 cpl_table_duplicate_column(zptbl,
"FILT_ID", extra,
"FILT_ID");
680 cpl_table_duplicate_column(zptbl,
"FILT_NAME", extra,
"FILT_NAME");
682 cpl_table_new_column(zptbl,
"ZEROPOINT", CPL_TYPE_DOUBLE);
683 cpl_table_set_double(zptbl,
"ZEROPOINT", 0, meanZP);
685 cpl_table_new_column(zptbl,
"ZEROPOINT_ERR", CPL_TYPE_DOUBLE);
686 cpl_table_set_double(zptbl,
"ZEROPOINT_ERR", 0, ZPerror);
688 cpl_table_new_column(zptbl,
"EXTINCTION", CPL_TYPE_DOUBLE);
689 cpl_table_set_double(zptbl,
"EXTINCTION", 0, extinction_airmass);
691 cpl_table_new_column(zptbl,
"EXTINCTION_ERR", CPL_TYPE_DOUBLE);
692 cpl_table_set_double(zptbl,
"EXTINCTION_ERR", 0, shift_err);
694 cpl_table_duplicate_column(zptbl,
"SEEING", extra,
"SEEING");
695 cpl_table_duplicate_column(zptbl,
"ELLIPTICITY", extra,
"ELLIPTICITY");
697 cpl_table_new_column(zptbl,
"NSTARS", CPL_TYPE_INT);
698 cpl_table_set_int(zptbl,
"NSTARS", 0, nrows);
722 double * ellipticity)
726 float stellarity = 1;
732 cpl_table_select_all(sources);
733 cpl_table_and_selected_float(sources,
"FWHM_IMAGE", CPL_GREATER_THAN, 0.1);
735 n = cpl_table_and_selected_float(sources,
"CLASS_STAR", CPL_NOT_LESS_THAN, stellarity);
738 cpl_table_select_all(sources);
739 cpl_table_and_selected_float(sources,
"FWHM_IMAGE", CPL_GREATER_THAN, 0.1);
741 n = cpl_table_and_selected_float(sources,
"CLASS_STAR", CPL_NOT_LESS_THAN, stellarity);
745 cpl_table_select_all(sources);
746 cpl_table_and_selected_float(sources,
"FWHM_IMAGE", CPL_GREATER_THAN, 0.1);
748 n = cpl_table_and_selected_float(sources,
"CLASS_STAR", CPL_NOT_LESS_THAN, stellarity);
756 tbl = cpl_table_extract_selected(sources);
757 median = cpl_table_get_column_median(tbl,
"FWHM_IMAGE");
758 cpl_msg_debug(cpl_func,
"median1: %g",median);
761 cpl_table_and_selected_float(sources,
"FWHM_IMAGE", CPL_NOT_GREATER_THAN, median);
762 tbl = cpl_table_extract_selected(sources);
763 median = cpl_table_get_column_median(tbl,
"FWHM_IMAGE");
764 cpl_msg_debug(cpl_func,
"median2: %g",median);
766 *seeing = median * fabs(scale)*3600;
768 if(cpl_table_has_column(tbl,
"ELLIPTICITY")){
769 *ellipticity = cpl_table_get_column_median(tbl,
"ELLIPTICITY");