39 #include "casu_mods.h"
40 #include "catalogue/casu_utils.h"
41 #include "casu_stats.h"
42 #include "catalogue/casu_fits.h"
57 #define INITALLOC 1024
59 #define DEGRAD (CPL_MATH_PI/180.0)
60 #define RADARCSEC (3600.0*180.0/CPL_MATH_PI)
62 static double pixsize (cpl_propertylist *plist);
63 static int casu_phot_open(cpl_table *phottab,
char *filt, photstrct *p);
64 static void casu_phot_close(photstrct *p);
65 static int extract_columns(cpl_table *tab, photstrct *p);
66 static int extract_coleq(cpl_table *tab, photstrct *p);
67 static void write_hdr_1(cpl_propertylist *p,
int nresim,
float med3,
float sig3,
68 float lim3,
float med5,
float sig5,
float lim5,
69 float extcoef,
float extinct,
float skybrt,
int doqc);
70 static void modifytab(photstrct *p, cpl_table *intab, casu_fits *im,
71 int minstars, cpl_image *schl_n, cpl_image *schl_s);
72 static void getextinct(cpl_image *schl_n, cpl_image *schl_s,
double ra,
73 double dec,
float *ebv);
74 static void radectolb(
double ra,
double dec,
float epoch,
double *gal_l,
76 static void photdistort(casu_fits *im, cpl_table *outtab);
77 static void getproj_pixsize(cpl_wcs *wcs,
double crv1,
double tand,
78 double secd,
double x,
double y,
double *pixsize);
80 static float propertylist_get_float_or_default(
const cpl_propertylist * list,
81 const char * name,
const float default_val);
199 casu_tfits **cats,
int nimages,
200 char *filt, cpl_table *phottab,
int minstars,
201 cpl_frame *schlf_n, cpl_frame *schlf_s,
202 const char *expkey,
const char *amkey,
203 float magerrcut,
int *status) {
204 float **stdmagptr,*resall3,*resall5,apcor3,apcor5,exptime,**magerrptr;
205 float airmass,*catcore3,*catcore5,*resim3,*resim5,cf,fluxmag3,fluxmag5;
206 float refmag,extinct,dm3,dm5,med3,mad,med5,sig3,sig5;
207 float rcore,lim5,dx,skylev,skbrt,*ebmvall,*zps3,*zps5,medebv,med;
208 float sig3det,sig5det,*ebmv,blankvalue=-99.0,sumskbr;
209 float sknoise,sumsat;
210 int nresall,nalloc_resall,i,j,k,ncat,nresim,nresimtot;
211 int ngood,oops,nlim,isrubbish,*dodgy,crud;
212 const char *fctid =
"casu_photcal_extinct";
214 cpl_propertylist *ehu_im,*ehu_cat,*phu_im,*phu_cat;
216 cpl_image *schl_n,*schl_s;
221 if (*status != CASU_OK)
227 cpl_msg_error(fctid,
"No images included in photometric calibration");
233 if (schlf_n == NULL || schlf_s == NULL) {
237 schl_n = cpl_image_load(cpl_frame_get_filename(schlf_n),CPL_TYPE_FLOAT,
239 schl_s = cpl_image_load(cpl_frame_get_filename(schlf_s),CPL_TYPE_FLOAT,
241 if (cpl_error_get_code() != CPL_ERROR_NONE) {
242 cpl_msg_error(fctid,
"Unable to read Schlegel maps");
252 if (casu_phot_open(phottab,filt,&p) != CASU_OK)
257 stdmagptr = cpl_malloc(p.ncolumns_coleq*
sizeof(
float *));
258 if (p.coleq_errcols != NULL)
259 magerrptr = cpl_malloc(p.ncolumns_coleq*
sizeof(
float *));
267 resall3 = cpl_malloc(INITALLOC*
sizeof(
float));
268 resall5 = cpl_malloc(INITALLOC*
sizeof(
float));
269 ebmvall = cpl_malloc(INITALLOC*
sizeof(
float));
271 nalloc_resall = INITALLOC;
272 zps3 = cpl_malloc(nimages*
sizeof(
float));
273 zps5 = cpl_malloc(nimages*
sizeof(
float));
276 dodgy = cpl_calloc(nimages,
sizeof(
int));
285 for (i = 0; i < nimages; i++) {
296 write_hdr_1(ehu_im,0,0.0,1.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1);
297 write_hdr_1(ehu_cat,0,0.0,1.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0);
304 ncat = (int)cpl_table_get_nrow(stds);
305 if (mstds[i] == NULL || ncat < minstars) {
306 cpl_msg_warning(fctid,
307 "Too few standards available in %" CPL_SIZE_FORMAT,
310 write_hdr_1(ehu_im,ncat,0.0,1.0,0.0,0.0,1.0,0.0,p.atm_extcoef,0.0,0.0,1);
311 write_hdr_1(ehu_cat,ncat,0.0,1.0,0.0,0.0,1.0,0.0,p.atm_extcoef,0.0,0.0,0);
319 apcor3 = cpl_propertylist_get_float(ehu_cat,
"APCOR3");
320 apcor5 = cpl_propertylist_get_float(ehu_cat,
"APCOR5");
321 rcore = cpl_propertylist_get_float(ehu_cat,
"ESO DRS RCORE");
322 skylev = cpl_propertylist_get_float(ehu_cat,
"ESO DRS SKYLEVEL");
323 sknoise = cpl_propertylist_get_float(ehu_cat,
"ESO DRS SKYNOISE");
324 dx = pixsize(ehu_im);
325 exptime = (float)cpl_propertylist_get_double(phu_im,expkey);
326 airmass = (float)cpl_propertylist_get_double(phu_im,amkey);
327 if (cpl_error_get_code() != CPL_ERROR_NONE) {
328 cpl_msg_error(fctid,
"Unable to get header %s or %s info",expkey,
336 modifytab(&p,stds,im,minstars,schl_n,schl_s);
340 ncat = (int)cpl_table_get_nrow(stds);
341 catcore3 = cpl_table_get_data_float(stds,
"Aper_flux_3");
342 catcore5 = cpl_table_get_data_float(stds,
"Aper_flux_5");
343 ebmv = cpl_table_get_data_float(stds,
"ebmv");
344 for (j = 0; j < p.ncolumns_coleq; j++) {
345 cpl_table_fill_invalid_float(stds,(p.coleq_columns)[j],blankvalue);
346 stdmagptr[j] = cpl_table_get_data_float(stds,(p.coleq_columns)[j]);
347 if (stdmagptr[j] == NULL)
349 cpl_msg_error(fctid,
"Unable to find filter column %s in photometric calibration table", p.coleq_columns[j]);
353 if (magerrptr != NULL) {
354 cpl_table_fill_invalid_float(stds,(p.coleq_errcols)[j],blankvalue);
355 magerrptr[j] = cpl_table_get_data_float(stds,(p.coleq_errcols)[j]);
361 resim3 = cpl_malloc(ncat*
sizeof(
float));
362 resim5 = cpl_malloc(ncat*
sizeof(
float));
367 extinct = p.atm_extcoef*(airmass - 1.0);
368 for (j = 0; j < ncat; j++) {
372 cf = catcore3[j]/exptime;
375 fluxmag3 = 2.5*log10((
double)cf) + apcor3;
376 cpl_table_set_float(stds,
"instmag3",j,fluxmag3);
377 cf = catcore5[j]/exptime;
380 fluxmag5 = 2.5*log10((
double)cf) + apcor5;
381 cpl_table_set_float(stds,
"instmag5",j,fluxmag5);
386 refmag = p.mag_offset;
388 for (k = 0; k < p.ncolumns_coleq; k++) {
389 if (stdmagptr[k][j] == blankvalue) {
392 }
else if (magerrptr != NULL) {
393 if (magerrptr[k][j] > magerrcut) {
398 refmag += ((p.coleq_coefs)[k]*stdmagptr[k][j]);
402 cpl_table_set_float(stds,
"refmag",j,refmag);
406 dm3 = refmag + fluxmag3 + extinct;
407 dm5 = refmag + fluxmag5 + extinct;
408 cpl_table_set_float(stds,
"dm3",j,dm3);
409 cpl_table_set_float(stds,
"dm5",j,dm5);
410 resim3[nresim] = dm3;
411 resim5[nresim++] = dm5;
412 ebmvall[nresall] = ebmv[j];
413 resall3[nresall] = dm3;
414 resall5[nresall++] = dm5;
415 if (nresall == nalloc_resall) {
416 nalloc_resall += INITALLOC;
417 resall3 = cpl_realloc(resall3,nalloc_resall*
sizeof(
float));
418 resall5 = cpl_realloc(resall5,nalloc_resall*
sizeof(
float));
419 ebmvall = cpl_realloc(ebmvall,nalloc_resall*
sizeof(
float));
426 (void)
casu_medmad(resim3,NULL,(
long)nresim,&med3,&mad);
428 (void)
casu_medmad(resim5,NULL,(
long)nresim,&med5,&mad);
431 zps5[ngood++] = med5;
443 nresimtot += cpl_table_get_nrow(stds);
453 lim3 = casu_calculate_abmag_lim(med3 + 2.5 * log10(exptime) - extinct,
454 sknoise, rcore, exptime, apcor3, extinct);
455 lim5 = med5 - 2.5*log10((5.0*sknoise*2.0*rcore*sqrt(CPL_MATH_PI))/exptime) -
460 lim3 = casu_calculate_abmag_lim(med3 + 2.5 * log10(exptime) - extinct,
461 sknoise, rcore, exptime, apcor3, extinct);
462 lim5 = med5 - 2.5*log10((5.0*sknoise*2.0*rcore*sqrt(CPL_MATH_PI))/exptime) -
468 skbrt = med3 - 2.5*log10(skylev/(exptime*dx*dx)) - extinct;
471 const float psf_fwhm = propertylist_get_float_or_default(ehu_cat,
"PSF_FWHM", 1.0f);
472 const float mean_sky = propertylist_get_float_or_default(ehu_cat,
"ESO QC MEAN_SKY", 1.0f);
474 const float pixel_scale = propertylist_get_float_or_default(ehu_im,
"ESO QC WCS_SCALE", 1.0f);
476 const float satmag = casu_calculate_abmag_sat(med3 + 2.5 * log10(exptime) - extinct,
477 65e3f, mean_sky, psf_fwhm, pixel_scale, exptime);
489 write_hdr_1(ehu_im,nresim,med3,sig3,lim3,med5,sig5,lim5,p.atm_extcoef,
491 write_hdr_1(ehu_cat,nresim,med3,sig3,lim3,med5,sig5,lim5,p.atm_extcoef,
493 cpl_propertylist_update_double(ehu_im,
"ABMAGSAT",satmag);
494 cpl_propertylist_set_comment(ehu_im,
"ABMAGSAT",
495 "Saturation limit for point sources");
496 cpl_propertylist_update_double(ehu_cat,
"ABMAGSAT",satmag);
497 cpl_propertylist_set_comment(ehu_cat,
"ABMAGSAT",
498 "Saturation limit for point sources");
504 (void)
casu_medmad(resall3,NULL,(
long)nresall,&med3,&mad);
506 (void)
casu_medmad(resall5,NULL,(
long)nresall,&med5,&mad);
508 (void)
casu_medmad(ebmvall,NULL,(
long)nresall,&medebv,&mad);
512 sig3 = p.default_zp_err;
518 char const *
const fluxcal = isrubbish ?
"UNCALIBRATED" :
"ABSOLUTE";
524 (void)
casu_medmad(zps3,NULL,(
long)ngood,&med,&mad);
526 (void)
casu_medmad(zps5,NULL,(
long)ngood,&med,&mad);
533 sumlim /= (float)nlim;
534 sumskbr /= (float)nlim;
535 sumsat /= (float)nlim;
543 freespace(stdmagptr);
544 freespace(magerrptr);
554 for (i = 0; i < nimages; i++) {
561 cpl_propertylist_update_string(phu_im,
"FLUXCAL",fluxcal);
562 cpl_propertylist_set_comment(phu_im,
"FLUXCAL",
563 "Certifies the validity of PHOTZP");
564 cpl_propertylist_update_string(phu_cat,
"FLUXCAL",fluxcal);
565 cpl_propertylist_set_comment(phu_cat,
"FLUXCAL",
566 "Certifies the validity of PHOTZP");
572 exptime = (float)cpl_propertylist_get_double(phu_im,expkey);
573 extinct = cpl_propertylist_get_float(ehu_im,
"ESO DRS EXTINCT");
576 photzp = med3 + 2.5*log10(exptime) - extinct;
578 cpl_propertylist_update_int(ehu_im,
"ESO DRS MAGNZPTALL",nresall);
579 cpl_propertylist_set_comment(ehu_im,
"ESO DRS MAGNZPTALL",
580 "number of stars in all magzpt calc");
581 cpl_propertylist_update_double(ehu_im,
"ESO DRS ZPALL1",med3);
582 cpl_propertylist_set_comment(ehu_im,
"ESO DRS ZPALL1",
583 "[mag] zeropoint 1*rcore all images");
584 cpl_propertylist_update_double(ehu_im,
"ESO DRS ZPSIGALL1",sig3);
585 cpl_propertylist_set_comment(ehu_im,
"ESO DRS ZPSIGALL1",
586 "[mag] zeropoint sigma 1*rcore all images");
587 cpl_propertylist_update_double(ehu_im,
"ESO DRS ZPALL2",med5);
588 cpl_propertylist_set_comment(ehu_im,
"ESO DRS ZPALL2",
589 "[mag] zeropoint 2*rcore all images");
590 cpl_propertylist_update_double(ehu_im,
"ESO DRS ZPSIGALL2",sig5);
591 cpl_propertylist_set_comment(ehu_im,
"ESO DRS ZPSIGALL2",
592 "[mag] zeropoint sigma 2*rcore all images");
593 cpl_propertylist_update_double(ehu_im,
"ESO DRS MEDEBV",medebv);
594 cpl_propertylist_set_comment(ehu_im,
"ESO DRS MEDEBV",
595 "[mag] median galactic colour excess");
596 cpl_propertylist_update_double(ehu_im,
"ESO DRS SIGDET1",sig3det);
597 cpl_propertylist_set_comment(ehu_im,
"ESO DRS SIGDET1",
598 "[mag] sigma det-level zpt 1*rcore");
599 cpl_propertylist_update_double(ehu_im,
"ESO DRS SIGDET2",sig5det);
600 cpl_propertylist_set_comment(ehu_im,
"ESO DRS SIGDET2",
601 "[mag] sigma det-level zpt 2*rcore");
602 cpl_propertylist_update_double(ehu_im,
"ESO QC MAGZPT",med3);
603 cpl_propertylist_set_comment(ehu_im,
"ESO QC MAGZPT",
604 "[mag] photometric zeropoint");
605 cpl_propertylist_update_double(ehu_im,
"ESO QC MAGZERR",sig3det);
606 cpl_propertylist_set_comment(ehu_im,
"ESO QC MAGZERR",
607 "[mag] photometric zeropoint error");
608 cpl_propertylist_update_double(ehu_im,
"ESO QC SKYBRIGHT",sumskbr);
609 cpl_propertylist_set_comment(ehu_im,
"ESO QC SKYBRIGHT",
610 "[mag/arcsec**2] sky brightness");
611 cpl_propertylist_update_double(ehu_im,
"PHOTZP",photzp);
612 cpl_propertylist_set_comment(ehu_im,
"PHOTZP",
613 "[mag] photometric zeropoint");
614 cpl_propertylist_update_double(ehu_im,
"PHOTZPER",sig3det);
615 cpl_propertylist_set_comment(ehu_im,
"PHOTZPER",
616 "[mag] uncertainty on PHOTZP");
617 cpl_propertylist_update_int(ehu_im,
"ESO QC MAGNZPT",nresimtot);
618 cpl_propertylist_set_comment(ehu_im,
"ESO QC MAGNZPT",
619 "number of stars in magzpt calc");
620 cpl_propertylist_update_bool(ehu_im,
"ZPFUDGED",isrubbish);
621 cpl_propertylist_set_comment(ehu_im,
"ZPFUDGED",
622 "TRUE if the ZP not derived from stds");
624 cpl_propertylist_update_double(ehu_im,
"ABMAGSAT",sumsat);
625 cpl_propertylist_set_comment(ehu_im,
"ABMAGSAT",
626 "Saturation limit for point sources");
627 cpl_propertylist_update_double(ehu_im,
"ABMAGLIM",sumlim);
628 cpl_propertylist_set_comment(ehu_im,
"ABMAGLIM",
629 "[mag] 5 sigma limiting mag");
630 cpl_propertylist_update_double(ehu_im,
"ESO QC LIMITING_MAG",sumlim);
631 cpl_propertylist_set_comment(ehu_im,
"ESO QC LIMITING_MAG",
632 "[mag] 5 sigma limiting mag.");
637 cpl_propertylist_update_int(ehu_cat,
"ESO DRS MAGNZPTALL",nresall);
638 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS MAGNZPTALL",
639 "number of stars in all magzpt calc");
640 cpl_propertylist_update_double(ehu_cat,
"ESO DRS ZPALL1",med3);
641 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS ZPALL1",
642 "[mag] zeropoint 1*rcore all group images");
643 cpl_propertylist_update_double(ehu_cat,
"ESO DRS ZPSIGALL1",sig3);
644 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS ZPSIGALL1",
645 "[mag] zeropoint sigma 1*rcore all group images");
646 cpl_propertylist_update_double(ehu_cat,
"ESO DRS ZPALL2",med5);
647 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS ZPALL2",
648 "[mag] zeropoint 2*rcore all group images");
649 cpl_propertylist_update_double(ehu_cat,
"ESO DRS ZPSIGALL2",sig5);
650 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS ZPSIGALL2",
651 "[mag] zeropoint sigma 2*rcore all group images");
652 cpl_propertylist_update_double(ehu_cat,
"ESO DRS MEDEBV",medebv);
653 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS MEDEBV",
654 "[mag] median galactic colour excess");
655 cpl_propertylist_update_double(ehu_cat,
"ESO DRS SIGDET1",sig3det);
656 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS SIGDET1",
657 "[mag] sigma det-level zpt 1*rcore");
658 cpl_propertylist_update_double(ehu_cat,
"ESO DRS SIGDET2",sig5det);
659 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS SIGDET2",
660 "[mag] sigma det-level zpt 2*rcore");
661 cpl_propertylist_update_double(ehu_cat,
"ESO QC MAGZPT",med3);
662 cpl_propertylist_set_comment(ehu_cat,
"ESO QC MAGZPT",
663 "[mag] photometric zeropoint");
664 cpl_propertylist_update_double(ehu_cat,
"ESO QC MAGZERR",sig3det);
665 cpl_propertylist_set_comment(ehu_cat,
"ESO QC MAGZERR",
666 "[mag] photometric zeropoint error");
667 cpl_propertylist_update_double(ehu_cat,
"ESO QC SKYBRIGHT",sumskbr);
668 cpl_propertylist_set_comment(ehu_cat,
"ESO QC SKYBRIGHT",
669 "[mag/arcsec**2] sky brightness");
670 cpl_propertylist_update_double(ehu_cat,
"PHOTZP",photzp);
671 cpl_propertylist_set_comment(ehu_cat,
"PHOTZP",
672 "[mag] photometric zeropoint");
673 cpl_propertylist_update_double(ehu_cat,
"PHOTZPER",sig3det);
674 cpl_propertylist_set_comment(ehu_cat,
"PHOTZPER",
675 "[mag] uncertainty on PHOTZP");
676 cpl_propertylist_update_int(ehu_cat,
"ESO QC MAGNZPT",nresimtot);
677 cpl_propertylist_set_comment(ehu_cat,
"ESO QC MAGNZPT",
678 "number of stars in magzpt calc");
679 cpl_propertylist_update_bool(ehu_cat,
"ZPFUDGED",isrubbish);
680 cpl_propertylist_set_comment(ehu_cat,
"ZPFUDGED",
681 "TRUE if the ZP not derived from stds");
683 cpl_propertylist_update_double(ehu_cat,
"ABMAGSAT",sumsat);
684 cpl_propertylist_set_comment(ehu_cat,
"ABMAGSAT",
685 "Saturation limit for point sources");
686 cpl_propertylist_update_double(ehu_cat,
"ABMAGLIM",sumlim);
687 cpl_propertylist_set_comment(ehu_cat,
"ABMAGLIM",
688 "[mag] 5 sigma limiting mag");
689 cpl_propertylist_update_double(ehu_cat,
"ESO QC LIMITING_MAG",sumlim);
690 cpl_propertylist_set_comment(ehu_cat,
"ESO QC LIMITING_MAG",
691 "[mag] 5 sigma limiting mag.");
696 if (mstds[i] != NULL) {
698 cpl_propertylist_update_string(phu_cat,
"FLUXCAL",fluxcal);
699 cpl_propertylist_set_comment(phu_cat,
"FLUXCAL",
700 "Certifies the validity of PHOTZP");
703 cpl_propertylist_update_int(ehu_cat,
"ESO DRS MAGNZPTALL",nresall);
704 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS MAGNZPTALL",
705 "number of stars in all magzpt calc");
706 cpl_propertylist_update_double(ehu_cat,
"ESO DRS ZPALL1",med3);
707 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS ZPALL1",
708 "[mag] zeropoint 1*rcore all group images");
709 cpl_propertylist_update_double(ehu_cat,
"ESO DRS ZPSIGALL1",sig3);
710 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS ZPSIGALL1",
711 "[mag] zeropoint sigma 1*rcore all group images");
712 cpl_propertylist_update_double(ehu_cat,
"ESO DRS ZPALL2",med5);
713 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS ZPALL2",
714 "[mag] zeropoint 2*rcore all group images");
715 cpl_propertylist_update_double(ehu_cat,
"ESO DRS ZPSIGALL2",sig5);
716 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS ZPSIGALL2",
717 "[mag] zeropoint sigma 2*rcore all group images");
718 cpl_propertylist_update_double(ehu_cat,
"ESO DRS MEDEBV",medebv);
719 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS MEDEBV",
720 "[mag] median galactic colour excess");
721 cpl_propertylist_update_double(ehu_cat,
"ESO DRS SIGDET1",sig3det);
722 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS SIGDET1",
723 "[mag] sigma det-level zpt 1*rcore");
724 cpl_propertylist_update_double(ehu_cat,
"ESO DRS SIGDET2",sig5det);
725 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS SIGDET2",
726 "[mag] sigma det-level zpt 2*rcore");
727 cpl_propertylist_update_double(ehu_cat,
"ESO QC MAGZPT",med3);
728 cpl_propertylist_set_comment(ehu_cat,
"ESO QC MAGZPT",
729 "[mag] photometric zeropoint");
730 cpl_propertylist_update_double(ehu_cat,
"ESO QC MAGZERR",sig3det);
731 cpl_propertylist_set_comment(ehu_cat,
"ESO QC MAGZERR",
732 "[mag] photometric zeropoint error");
733 cpl_propertylist_update_double(ehu_cat,
"ESO QC SKYBRIGHT",sumskbr);
734 cpl_propertylist_set_comment(ehu_cat,
"ESO QC SKYBRIGHT",
735 "[mag/arcsec**2] sky brightness");
736 cpl_propertylist_update_double(ehu_cat,
"PHOTZP",photzp);
737 cpl_propertylist_set_comment(ehu_cat,
"PHOTZP",
738 "[mag] photometric zeropoint");
739 cpl_propertylist_update_double(ehu_cat,
"PHOTZPER",sig3det);
740 cpl_propertylist_set_comment(ehu_cat,
"PHOTZPER",
741 "[mag] uncertainty on PHOTZP");
742 cpl_propertylist_update_int(ehu_cat,
"ESO QC MAGNZPT",nresimtot);
743 cpl_propertylist_set_comment(ehu_cat,
"ESO QC MAGNZPT",
744 "number of stars in magzpt calc");
745 cpl_propertylist_update_bool(ehu_cat,
"ZPFUDGED",isrubbish);
746 cpl_propertylist_set_comment(ehu_cat,
"ZPFUDGED",
747 "TRUE if the ZP not derived from stds");
786 extern int casu_remove_mag_outside_range(casu_tfits **mstds,
const int nimages,
787 char *filt, cpl_table *phottab,
788 const float low_mag,
const float high_mag)
793 if (casu_phot_open(phottab,filt,&p) != CASU_OK)
798 for (i = 0; i < nimages; i++) {
799 casu_tfits * ctfits = mstds[i];
801 cpl_table_unselect_all(stds);
802 for (j = 0; j < p.ncolumns_coleq; j++) {
803 const char * clnm_name = (p.coleq_columns)[j];
804 if(!cpl_table_has_column(stds, clnm_name))
809 cpl_table_or_selected_float(stds, clnm_name, CPL_LESS_THAN, low_mag);
810 cpl_table_or_selected_float(stds, clnm_name, CPL_GREATER_THAN, high_mag);
812 num_erased += cpl_table_count_selected(stds);
813 cpl_table_erase_selected(stds);
814 cpl_table_select_all(stds);
841 static double pixsize (cpl_propertylist *plist) {
845 wcs = cpl_wcs_new_from_propertylist(plist);
846 cd = cpl_matrix_get_data((cpl_matrix *)cpl_wcs_get_cd(wcs));
847 pix = 3600.0*sqrt(fabs(cd[0]*cd[3] - cd[1]*cd[2]));
879 static int casu_phot_open(cpl_table *phottab,
char *filt, photstrct *p) {
880 const char *fctid =
"casu_phot_open";
884 const char *req_cols[9] = {
"filter_name",
"atm_extcoef",
"mag_offset",
885 "coleq_columns",
"coleq_errcols",
"coleq_coefs",
886 "gal_extcoef",
"default_zp",
"default_zp_err"};
890 p->coleq_coefs = NULL;
891 p->coleq_columns = NULL;
892 p->coleq_errcols = NULL;
893 p->ncolumns_coleq = 0;
899 for (ns = 0; ns < 9; ns++) {
900 if (! cpl_table_has_column(phottab,req_cols[ns])) {
901 cpl_msg_error(fctid,
"Photometry table missing column %s",
914 filts = cpl_table_get_data_string(phottab,
"filter_name");
915 nr = cpl_table_get_nrow(phottab);
917 for (ns = 0; ns < nr; ns++) {
918 if (strncmp(filts[ns],filt,16) == 0) {
924 cpl_msg_error(fctid,
"Unable to match photometry table to filter %s",
928 cpl_table_and_selected_window(phottab,ns,1);
932 subset = cpl_table_extract_selected(phottab);
933 p->filter_name = (
char *)cpl_table_get_string(subset,
"filter_name",0);
934 p->atm_extcoef = cpl_table_get_float(subset,
"atm_extcoef",0,&
null);
935 p->mag_offset = cpl_table_get_float(subset,
"mag_offset",0,&
null);
936 p->gal_extcoef = cpl_table_get_float(subset,
"gal_extcoef",0,&
null);
937 p->default_zp = cpl_table_get_float(subset,
"default_zp",0,&
null);
938 p->default_zp_err = cpl_table_get_float(subset,
"default_zp_err",0,&
null);
939 if (extract_columns(subset,p) != CASU_OK) {
943 if (extract_coleq(subset,p) != CASU_OK) {
972 static void casu_phot_close(photstrct *p) {
975 for (j = 0; j < p->ncolumns_coleq; j++) {
976 freespace((p->coleq_columns)[j]);
977 freespace((p->coleq_errcols)[j]);
979 freespace(p->coleq_columns);
980 freespace(p->coleq_errcols);
981 freespace(p->coleq_coefs);
1005 static int extract_columns(cpl_table *tab, photstrct *p) {
1008 const char *cols[2] = {
"coleq_columns",
"coleq_errcols"};
1012 for (k = 0; k < 2; k++) {
1016 v = cpl_strdup(cpl_table_get_string(tab,cols[k],0));
1023 for (i = 0; i < j; i++)
1027 p->ncolumns_coleq = nv;
1029 if (nv == 0 || nv != p->ncolumns_coleq) {
1030 p->coleq_errcols = NULL;
1038 z = cpl_malloc(nv*
sizeof(
char *));
1039 char *saveptr = NULL;
1040 for (i = 0; i < nv; i++) {
1042 w = strtok_r(v,
",",&saveptr);
1044 w = strtok_r(NULL,
",",&saveptr);
1045 z[i] = cpl_strdup(w);
1048 p->coleq_columns = z;
1050 p->coleq_errcols = z;
1077 static int extract_coleq(cpl_table *tab, photstrct *p) {
1084 v = cpl_strdup(cpl_table_get_string(tab,
"coleq_coefs",0));
1091 for (i = 0; i < j; i++)
1097 z = cpl_malloc(nv*
sizeof(
float));
1098 char *saveptr1 = NULL;
1099 for (i = 0; i < nv; i++) {
1101 w = strtok_r(v,
",", &saveptr1);
1103 w = strtok_r(NULL,
",", &saveptr1);
1104 z[i] = (float)atof(w);
1152 static void write_hdr_1(cpl_propertylist *pl,
int nresim,
float med3,
1153 float sig3,
float lim3,
float med5,
float sig5,
1154 float lim5,
float extcoef,
float extinct,
float skybrt,
1162 cpl_propertylist_update_double(pl,
"ESO QC MAGZPT",med3);
1163 cpl_propertylist_set_comment(pl,
"ESO QC MAGZPT",
1164 "[mag] photometric zeropoint");
1165 cpl_propertylist_update_double(pl,
"ESO QC MAGZERR",sig3);
1166 cpl_propertylist_set_comment(pl,
"ESO QC MAGZERR",
1167 "[mag] photometric zeropoint error");
1168 cpl_propertylist_update_int(pl,
"ESO QC MAGNZPT",nresim);
1169 cpl_propertylist_set_comment(pl,
"ESO QC MAGNZPT",
1170 "number of stars in magzpt calc");
1171 cpl_propertylist_update_double(pl,
"ESO QC SKYBRIGHT",skybrt);
1172 cpl_propertylist_set_comment(pl,
"ESO QC SKYBRIGHT",
1173 "[mag/arcsec**2] sky brightness");
1178 cpl_propertylist_update_int(pl,
"ESO DRS MAGNZPTIM",nresim);
1179 cpl_propertylist_set_comment(pl,
"ESO DRS MAGNZPTIM",
1180 "number of stars in image magzpt calc");
1182 cpl_propertylist_update_double(pl,
"ESO DRS ZPIM1",med3);
1183 cpl_propertylist_set_comment(pl,
"ESO DRS ZPIM1",
1184 "[mag] zeropoint 1*rcore this image only");
1185 cpl_propertylist_update_double(pl,
"ESO DRS ZPSIGIM1",sig3);
1186 cpl_propertylist_set_comment(pl,
"ESO DRS ZPSIGIM1",
1187 "[mag] zeropoint sigma 1*rcore this image only");
1188 cpl_propertylist_update_double(pl,
"ESO DRS LIMIT_MAG1",lim3);
1189 cpl_propertylist_set_comment(pl,
"ESO DRS LIMIT_MAG1",
1190 "[mag] 5 sigma limiting mag 1*rcore.");
1191 cpl_propertylist_update_double(pl,
"ABMAGLIM",lim3);
1192 cpl_propertylist_set_comment(pl,
"ABMAGLIM",
1193 "[mag] 5 sigma limiting mag");
1194 cpl_propertylist_update_double(pl,
"ESO QC LIMITING_MAG",lim3);
1195 cpl_propertylist_set_comment(pl,
"ESO QC LIMITING_MAG",
1196 "[mag] 5 sigma limiting mag.");
1197 cpl_propertylist_update_double(pl,
"ESO DRS ZPIM2",med5);
1198 cpl_propertylist_set_comment(pl,
"ESO DRS ZPIM2",
1199 "[mag] zeropoint 2*rcore this image only");
1200 cpl_propertylist_update_double(pl,
"ESO DRS ZPSIGIM2",sig5);
1201 cpl_propertylist_set_comment(pl,
"ESO DRS ZPSIGIM2",
1202 "[mag] zeropoint sigma 2*rcore this image only");
1203 cpl_propertylist_update_double(pl,
"ESO DRS LIMIT_MAG2",lim5);
1204 cpl_propertylist_set_comment(pl,
"ESO DRS LIMIT_MAG2",
1205 "[mag] 5 sigma limiting mag core5.");
1206 cpl_propertylist_update_double(pl,
"ESO DRS EXTCOEF",extcoef);
1207 cpl_propertylist_set_comment(pl,
"ESO DRS EXTCOEF",
1208 "[mag] Assumed extinction coefficient");
1209 cpl_propertylist_update_double(pl,
"ESO DRS EXTINCT",extinct);
1210 cpl_propertylist_set_comment(pl,
"ESO DRS EXTINCT",
1211 "[mag] Assumed extinction");
1212 cpl_propertylist_update_double(pl,
"ESO DRS SKYBRIGHT",skybrt);
1213 cpl_propertylist_set_comment(pl,
"ESO DRS SKYBRIGHT",
1214 "[mag/arcsec**2] sky brightness");
1248 static void modifytab(photstrct *p, cpl_table *intab, casu_fits *im,
1249 int minstars, cpl_image *schl_n, cpl_image *schl_s) {
1250 float *ebmv,ebv,*galex;
1251 float *ra = NULL, *dec = NULL;
1252 double *rra = NULL, *ddec = NULL;
1254 const char *fctid =
"modifytab";
1259 nrows = (int)cpl_table_get_nrow(intab);
1260 if (nrows < minstars) {
1261 cpl_msg_error(fctid,
"Matched standards has too few stars");
1269 if (! cpl_table_has_column(intab,
"ebmv"))
1270 cpl_table_duplicate_column(intab,
"ebmv",intab,
"Peak_height");
1271 if (! cpl_table_has_column(intab,
"galextinct"))
1272 cpl_table_duplicate_column(intab,
"galextinct",intab,
"Peak_height");
1276 if (! cpl_table_has_column(intab,
"instmag3")) {
1277 cpl_table_duplicate_column(intab,
"instmag3",intab,
"Peak_height");
1278 cpl_table_set_column_invalid(intab,
"instmag3",0,nrows);
1280 if (! cpl_table_has_column(intab,
"instmag5")) {
1281 cpl_table_duplicate_column(intab,
"instmag5",intab,
"Peak_height");
1282 cpl_table_set_column_invalid(intab,
"instmag5",0,nrows);
1284 if (! cpl_table_has_column(intab,
"refmag")) {
1285 cpl_table_duplicate_column(intab,
"refmag",intab,
"Peak_height");
1286 cpl_table_set_column_invalid(intab,
"refmag",0,nrows);
1288 if (! cpl_table_has_column(intab,
"dm3")) {
1289 cpl_table_duplicate_column(intab,
"dm3",intab,
"Peak_height");
1290 cpl_table_set_column_invalid(intab,
"dm3",0,nrows);
1292 if (! cpl_table_has_column(intab,
"dm5")) {
1293 cpl_table_duplicate_column(intab,
"dm5",intab,
"Peak_height");
1294 cpl_table_set_column_invalid(intab,
"dm5",0,nrows);
1300 ebmv = cpl_table_get_data_float(intab,
"ebmv");
1301 galex = cpl_table_get_data_float(intab,
"galextinct");
1302 if (cpl_table_get_column_type(intab,
"RA") == CPL_TYPE_FLOAT) {
1303 ra = cpl_table_get_data_float(intab,
"RA");
1304 dec = cpl_table_get_data_float(intab,
"Dec");
1306 rra = cpl_table_get_data_double(intab,
"RA");
1307 ddec = cpl_table_get_data_double(intab,
"Dec");
1309 for (i = 0; i < nrows; i++) {
1310 if (schl_n == NULL || schl_s == NULL) {
1313 if (cpl_table_get_column_type(intab,
"RA") == CPL_TYPE_FLOAT && ra != NULL && dec != NULL) {
1314 getextinct(schl_n,schl_s,(
double)(ra[i]),(
double)(dec[i]),&ebv);
1315 }
else if (cpl_table_get_column_type(intab,
"RA") == CPL_TYPE_DOUBLE && rra != NULL && ddec != NULL){
1316 getextinct(schl_n,schl_s,rra[i],ddec[i],&ebv);
1322 galex[i] = p->gal_extcoef*ebv;
1327 photdistort(im,intab);
1356 static void photdistort(casu_fits *im, cpl_table *outtab) {
1358 float *flux1,*flux2,*x,*y;
1360 const cpl_array *crv;
1361 double crv1,tand,secd,distcor,ps1,ps2,psc;
1366 crv = cpl_wcs_get_crval(wcs);
1367 crv1 = DEGRAD*cpl_array_get(crv,0,&isnull);
1368 tand = tan(DEGRAD*cpl_array_get(crv,1,&isnull));
1369 secd = sqrt(1.0 + tand*tand);
1378 flux1 = cpl_table_get_data_float(outtab,
"Aper_flux_3");
1379 flux2 = cpl_table_get_data_float(outtab,
"Aper_flux_5");
1380 x = cpl_table_get_data_float(outtab,
"X_coordinate");
1381 y = cpl_table_get_data_float(outtab,
"Y_coordinate");
1382 nrows = (int)cpl_table_get_nrow(outtab);
1386 for (i = 0; i < nrows; i++) {
1387 getproj_pixsize(wcs,crv1,tand,secd,(
double)x[i],(
double)y[i],&ps1);
1389 flux1[i] *= distcor;
1390 flux2[i] *= distcor;
1417 static float propertylist_get_float_or_default(
const cpl_propertylist * list,
const char * name,
1418 const float default_val){
1420 cpl_boolean value_missing_or_mismatch = !cpl_propertylist_has(list, name);
1421 value_missing_or_mismatch = value_missing_or_mismatch || (cpl_propertylist_get_type(list, name) != CPL_TYPE_DOUBLE
1422 && cpl_propertylist_get_type(list, name) != CPL_TYPE_FLOAT);
1423 if(value_missing_or_mismatch) {
1424 cpl_msg_warning(cpl_func,
"Unable to extract %s, fallback to %f", name, default_val);
1428 return cpl_propertylist_get_float(list, name);
1460 static void getproj_pixsize(cpl_wcs *wcs,
double crv1,
double tand,
1461 double secd,
double x,
double y,
double *ps) {
1464 cpl_vector *xi,*eta;
1466 double offx,offy,ra,dec,xiv,etav,bigd,dx,dy,*xira,*etara,dx1,dx2,dy1,dy2;
1470 xy = cpl_matrix_new(4,2);
1471 for (i = 0; i < 4; i++) {
1472 offx = (i < 2 ? -0.5 : 0.5);
1473 offy = ((i == 0 || i == 3) ? -0.5 : 0.5);
1474 cpl_matrix_set(xy,i,0,x+offx);
1475 cpl_matrix_set(xy,i,1,y+offy);
1480 cpl_wcs_convert(wcs,xy,&rd,&stat,CPL_WCS_PHYS2WORLD);
1481 cpl_array_delete(stat);
1482 cpl_matrix_delete(xy);
1486 xi = cpl_vector_new(4);
1487 eta = cpl_vector_new(4);
1488 for (i = 0; i < 4; i++) {
1489 ra = cpl_matrix_get(rd,i,0);
1490 dec = cpl_matrix_get(rd,i,1);
1494 bigd = tan(dec)*tand + cos(ra);
1495 xiv = RADARCSEC*secd*sin(ra)/bigd;
1496 etav = RADARCSEC*(tan(dec) - tand*cos(ra))/bigd;
1497 cpl_vector_set(xi,i,xiv);
1498 cpl_vector_set(eta,i,etav);
1500 cpl_matrix_delete(rd);
1505 xira = cpl_vector_get_data(xi);
1506 etara = cpl_vector_get_data(eta);
1507 dx1 = xira[1] - xira[0];
1508 dx2 = xira[3] - xira[2];
1509 dy1 = etara[1] - etara[0];
1510 dy2 = etara[3] - etara[2];
1511 dy = 0.5*(sqrt(dx1*dx1 + dy1*dy1) + sqrt(dx2*dx2 + dy2*dy2));
1512 dx1 = xira[1] - xira[2];
1513 dx2 = xira[0] - xira[3];
1514 dy1 = etara[1] - etara[2];
1515 dy2 = etara[0] - etara[3];
1516 dx = 0.5*(sqrt(dx1*dx1 + dy1*dy1) + sqrt(dx2*dx2 + dy2*dy2));
1521 cpl_vector_delete(xi);
1522 cpl_vector_delete(eta);
1551 static void getextinct(cpl_image *schl_n, cpl_image *schl_s,
double ra,
1552 double dec,
float *ebv) {
1553 double gal_l,gal_b,sin_b,cos_l,sin_l;
1555 float x,y,*schl_data,xx,yy,delx,dely;
1556 int ix,iy,iarg1,iarg2,iarg3,iarg4,nx,ny;
1560 radectolb(ra,dec,2000.0,&gal_l,&gal_b);
1564 schl = (gal_b >= 0.0 ? schl_n : schl_s);
1565 schl_data = cpl_image_get_data_float(schl);
1574 x = 2048.0*sqrt(1.0 - sin_b)*cos_l + 2047.5;
1575 y = -2048.0*sqrt(1.0 - sin_b)*sin_l + 2047.5;
1577 x = 2048.0*sqrt(1.0 + sin_b)*cos_l + 2047.5;
1578 y = 2048.0*sqrt(1.0 + sin_b)*sin_l + 2047.5;
1583 nx = (int)cpl_image_get_size_x(schl);
1584 ny = (int)cpl_image_get_size_y(schl);
1585 ix = min(nx-2,max(0,(
int)x));
1586 iy = min(ny-2,max(0,(
int)y));
1596 xx = (1.0 - delx)*schl_data[iarg1] + delx*schl_data[iarg2];
1597 yy = (1.0 - delx)*schl_data[iarg3] + delx*schl_data[iarg4];
1598 *ebv = (1.0 - dely)*xx + dely*yy;
1626 static void radectolb(
double ra,
double dec,
float epoch,
double *gal_l,
1628 double ti,tf,s,z,h,arcsecrad,ra2,dec2,cosdec1,sindec1,cosra1s,sinra1s;
1629 double cos_h,sin_h,x1,x2,sin_b,cosdec2,sindec2,costheta,sintheta;
1630 double cosdra,sindra,ra1,dec1,sinra2,cosra2;
1631 double a0=282.25,theta=62.6,gal_l0=33.0;
1637 ti = (double)(epoch - 2000.0)/100.0;
1638 tf = (double)(equin - 2000.0)/100.0 - ti;
1639 s = (2306.2181 - 1.39656*ti - 0.000139*ti*ti)*tf +
1640 (0.30188 - 0.000344*ti)*tf*tf + 0.17998*tf*tf*tf;
1641 z = s + (0.79280 + 0.000410*ti)*tf*tf + 0.000205*tf*tf*tf;
1642 h = (2004.3109 - 0.85330*ti - 0.000217*ti*ti)*tf -
1643 (0.42665 + 0.000217*ti)*tf*tf - 0.041833*tf*tf*tf;
1647 arcsecrad = 1.0/(CPL_MATH_DEG_RAD*3600.0);
1651 ra1 = ra/CPL_MATH_DEG_RAD;
1652 dec1 = dec/CPL_MATH_DEG_RAD;
1656 cosdec1 = cos(dec1);
1657 sindec1 = sin(dec1);
1658 cosra1s = cos(ra1+s);
1659 sinra1s = sin(ra1+s);
1662 dec2 = asin(cosdec1*cosra1s*sin_h + sindec1*cos_h);
1663 cosdec2 = cos(dec2);
1664 sinra2 = cosdec1*sinra1s/cosdec2;
1665 cosra2 = (cosdec1*cosra1s*cos_h - sindec1*sin_h)/cosdec2;
1666 ra2 = atan2(sinra2,cosra2) + z;
1671 ra2 += CPL_MATH_2PI;
1675 a0 /= CPL_MATH_DEG_RAD;
1676 theta /= CPL_MATH_DEG_RAD;
1677 gal_l0 /= CPL_MATH_DEG_RAD;
1681 sindec2 = sin(dec2);
1682 costheta = cos(theta);
1683 sintheta = sin(theta);
1684 cosdra = cos(ra2 - a0);
1685 sindra = sin(ra2 - a0);
1686 x1 = cosdec2*cosdra;
1687 x2 = cosdec2*sindra*costheta + sindec2*sintheta;
1688 *gal_l = atan2(x2,x1) + gal_l0;
1690 *gal_l += CPL_MATH_2PI;
1691 sin_b = sindec2*costheta - cosdec2*sindra*sintheta;
1692 *gal_b = asin(sin_b);
1722 casu_calculate_abmag_lim(
const float zeropoint,
const float skynoise,
1723 const float r_core,
const float eff_expt,
const float apcor3,
const float extinction){
1724 return zeropoint - 2.5f * log10f(5.0f * skynoise * r_core * sqrt(CPL_MATH_PI) / eff_expt) - apcor3 - extinction;
1728 casu_calculate_abmag_sat(
const float zeropoint,
const float satlev,
1729 const float mean_sky,
const float psf_fwhm,
const float pixel_scale,
1730 const float exptime){
1731 const float val = (CPL_MATH_PI_4 * CPL_MATH_LN2) * (satlev - mean_sky) * powf((psf_fwhm / pixel_scale), 2.0) / exptime;
1732 return zeropoint - 2.5f*log10f(val);
int casu_fits_get_status(casu_fits *p)
char * casu_fits_get_fullname(casu_fits *p)
cpl_propertylist * casu_fits_get_phu(casu_fits *p)
cpl_propertylist * casu_fits_get_ehu(casu_fits *p)
int casu_photcal_extinct(casu_fits **images, casu_tfits **mstds, casu_tfits **cats, int nimages, char *filt, cpl_table *phottab, int minstars, cpl_frame *schlf_n, cpl_frame *schlf_s, const char *expkey, const char *amkey, float magerrcut, int *status)
Do photometric calibration.
void casu_medmad(float *data, unsigned char *bpm, long np, float *med, float *mad)
cpl_table * casu_tfits_get_table(casu_tfits *p)
cpl_propertylist * casu_tfits_get_phu(casu_tfits *p)
cpl_propertylist * casu_tfits_get_ehu(casu_tfits *p)