38 #include "vircam_mods.h"
39 #include "vircam_utils.h"
40 #include "vircam_pfits.h"
41 #include "vircam_stats.h"
42 #include "vircam_fits.h"
71 #define NOMPIXSIZE 0.34
72 #define INITALLOC 1024
75 static double pixsize (cpl_propertylist *plist);
76 static int vircam_phot_open(cpl_table *phottab,
char *filt);
77 static void vircam_phot_close(
void);
78 static int extract_columns(cpl_table *tab);
79 static int extract_coleq(cpl_table *tab);
80 static void write_hdr_1(cpl_propertylist *p,
int nresim,
float med3,
float sig3,
81 float lim3,
float med5,
float sig5,
float lim5,
82 float extinct,
float skybrt,
int ncut,
int doqc);
84 static void modifytab(cpl_table *intab, vir_fits *im,
float saturate,
85 int minstars, cpl_image *schl_n, cpl_image *schl_s,
86 int *method, cpl_table **outtab);
87 static void getextinct(cpl_image *schl_n, cpl_image *schl_s,
double ra,
88 double dec,
float *jext,
float *hext,
float *kext,
90 static mywcs *getwcs(vir_fits *in);
91 static void radectolb(
double ra,
double dec,
float epoch,
double *gal_l,
93 static float distort_corr(
double x,
double y, mywcs *w);
185 cpl_propertylist **pl,
int nimages,
char *filt,
186 cpl_table *phottab,
int *status) {
187 float **stdmagptr,*resall3,*resall5,cdfudge,saturate,apcor3,apcor5,exptime;
188 float airmass,*catcore3,*catcore5,*resim3,*resim5,cf,fluxmag3,fluxmag5;
189 float refmag,extinct,dm3,dm5,med3,mad,cut,lcut,hcut,med5,sig3,sig5;
190 float rcore,lim3,lim5,dx,skylev,skbrt;
191 int nresall,nalloc_resall,i,j,k,ncat,nresim,ncut;
193 const char *fctid =
"vircam_photcal";
195 cpl_propertylist *ehu_im,*ehu_cat;
200 if (*status != VIR_OK)
206 cpl_msg_error(fctid,
"No images included in photometric calibration");
213 if (vircam_phot_open(phottab,filt) != VIR_OK)
218 stdmagptr = cpl_malloc(p.ncolumns*
sizeof(
float *));
224 resall3 = cpl_malloc(INITALLOC*
sizeof(
float));
225 resall5 = cpl_malloc(INITALLOC*
sizeof(
float));
227 nalloc_resall = INITALLOC;
234 for (i = 0; i < nimages; i++) {
239 cdfudge = 2.5*log10((
double)pixsize(ehu_im)/NOMPIXSIZE);
243 ncat = (int)cpl_table_get_nrow(stds);
245 cpl_msg_warning(fctid,
"Too few standards available");
247 write_hdr_1(ehu_im,ncat,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0,1);
248 write_hdr_1(ehu_cat,ncat,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0,0);
255 saturate = cpl_propertylist_get_float(ehu_cat,
"ESO QC SATURATION");
256 apcor3 = cpl_propertylist_get_float(ehu_cat,
"APCOR3");
257 apcor5 = cpl_propertylist_get_float(ehu_cat,
"APCOR5");
258 rcore = cpl_propertylist_get_float(ehu_cat,
"ESO DRS RCORE");
259 skylev = cpl_propertylist_get_float(ehu_cat,
"ESO DRS SKYLEVEL");
260 dx = pixsize(ehu_im);
263 if (cpl_error_get_code() != CPL_ERROR_NONE) {
264 cpl_msg_error(fctid,
"Unable to get header info");
271 cpl_table_select_all(stds);
272 (void)cpl_table_and_selected_float(stds,
"Ellipticity",CPL_LESS_THAN,
274 (void)cpl_table_and_selected_float(stds,
"Peak_height",CPL_LESS_THAN,
276 if (cpl_error_get_code() != CPL_ERROR_NONE) {
277 cpl_msg_error(fctid,
"Unable to select data from matched stds tab");
284 for (j = 0; j < p.ncolumns; j++) {
285 (void)snprintf(junk,SZBUF,
"%ssig",(p.columns)[j]);
286 (void)cpl_table_and_selected_float(stds,junk,CPL_LESS_THAN,0.1);
292 cl = cpl_table_extract_selected(stds);
293 ncat = (int)cpl_table_get_nrow(cl);
295 cpl_msg_warning(fctid,
"Too few good standards available");
296 cpl_table_delete(cl);
298 write_hdr_1(ehu_im,ncat,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0,1);
299 write_hdr_1(ehu_cat,ncat,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0,0);
305 catcore3 = cpl_table_get_data_float(cl,
"Aper_flux_3");
306 catcore5 = cpl_table_get_data_float(cl,
"Aper_flux_5");
307 for (j = 0; j < p.ncolumns; j++)
308 stdmagptr[j] = cpl_table_get_data_float(cl,(p.columns)[j]);
312 resim3 = cpl_malloc(ncat*
sizeof(
float));
313 resim5 = cpl_malloc(ncat*
sizeof(
float));
318 extinct = p.extinct*(airmass - 1.0);
319 for (j = 0; j < ncat; j++) {
323 cf = catcore3[j]/exptime;
326 fluxmag3 = 2.5*log10((
double)cf) + apcor3;
327 cf = catcore5[j]/exptime;
330 fluxmag5 = 2.5*log10((
double)cf) + apcor5;
335 for (k = 0; k < p.nmags; k++)
336 refmag += ((p.coloureq)[k]*stdmagptr[k][j]);
340 dm3 = refmag + fluxmag3 + extinct;
341 dm5 = refmag + fluxmag5 + extinct;
342 resim3[nresim] = dm3 + cdfudge;
343 resim5[nresim++] = dm5 + cdfudge;
344 resall3[nresall] = dm3 + cdfudge;
345 resall5[nresall++] = dm5 + cdfudge;
346 if (nresall == nalloc_resall) {
347 nalloc_resall += INITALLOC;
348 resall3 = cpl_realloc(resall3,nalloc_resall*
sizeof(
float));
349 resall5 = cpl_realloc(resall5,nalloc_resall*
sizeof(
float));
356 cut = max(3.0*1.48*mad,0.1);
361 for (j = 0; j < nresim; j++)
362 if (resim3[j] < lcut || resim3[j] > hcut)
365 cut = max(3.0*1.48*mad,0.1);
378 lim3 = med3 - 2.5*log10((5.0*sig3*rcore*sqrt(CPL_MATH_PI))/exptime) -
380 lim5 = med5 - 2.5*log10((5.0*sig5*rcore*sqrt(CPL_MATH_PI))/exptime) -
385 skbrt = lim3 - 2.5*log10(skylev/(exptime*dx*dx)) - extinct;
390 write_hdr_1(ehu_im,nresim,med3,sig3,lim3,med5,sig5,lim5,p.extinct,
392 write_hdr_1(ehu_cat,nresim,med3,sig3,lim3,med5,sig5,lim5,p.extinct,
400 cut = max(3.0*1.48*mad,0.1);
405 cut = max(3.0*1.48*mad,0.1);
420 freespace(stdmagptr);
426 for (i = 0; i < nimages; i++) {
431 cpl_propertylist_update_int(ehu_im,
"ESO DRS MAGNZPTALL",nresall);
432 cpl_propertylist_set_comment(ehu_im,
"ESO DRS MAGNZPTALL",
433 "number of stars in all magzpt calc");
434 cpl_propertylist_update_double(ehu_im,
"ESO DRS ZPALL1",med3);
435 cpl_propertylist_set_comment(ehu_im,
"ESO DRS ZPALL1",
436 "[mag] zeropoint 1*rcore all group images");
437 cpl_propertylist_update_double(ehu_im,
"ESO DRS ZPSIGALL1",sig3);
438 cpl_propertylist_set_comment(ehu_im,
"ESO DRS ZPSIGALL1",
439 "[mag] zeropoint sigma 1*rcore all group images");
440 cpl_propertylist_update_double(ehu_im,
"ESO DRS ZPALL2",med5);
441 cpl_propertylist_set_comment(ehu_im,
"ESO DRS ZPALL2",
442 "[mag] zeropoint 2*rcore all group images");
443 cpl_propertylist_update_double(ehu_im,
"ESO DRS ZPSIGALL2",sig5);
444 cpl_propertylist_set_comment(ehu_im,
"ESO DRS ZPSIGALL2",
445 "[mag] zeropoint sigma 2*rcore all group images");
449 cpl_propertylist_update_int(ehu_cat,
"ESO DRS MAGNZPTALL",nresall);
450 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS MAGNZPTALL",
451 "number of stars in all magzpt calc");
452 cpl_propertylist_update_double(ehu_cat,
"ESO DRS ZPALL1",med3);
453 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS ZPALL1",
454 "[mag] zeropoint 1*rcore all group images");
455 cpl_propertylist_update_double(ehu_cat,
"ESO DRS ZPSIGALL1",sig3);
456 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS ZPSIGALL1",
457 "[mag] zeropoint sigma 1*rcore all group images");
458 cpl_propertylist_update_double(ehu_cat,
"ESO DRS ZPALL2",med5);
459 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS ZPALL2",
460 "[mag] zeropoint 2*rcore all group images");
461 cpl_propertylist_update_double(ehu_cat,
"ESO DRS ZPSIGALL2",sig5);
462 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS ZPSIGALL2",
463 "[mag] zeropoint sigma 2*rcore all group images");
475 extern int vircam_photcal_extinct(vir_fits **images, cpl_table **mstds,
476 vir_tfits **cats,
int nimages,
477 char *filt, cpl_table *phottab,
int minstars,
478 cpl_frame *schlf_n, cpl_frame *schlf_s,
480 float **stdmagptr,*resall3,*resall5,cdfudge,saturate,apcor3,apcor5,exptime;
481 float airmass,*catcore3,*catcore5,*resim3,*resim5,cf,fluxmag3,fluxmag5;
482 float refmag,extinct,dm3,dm5,med3,mad,med5,sig3,sig5;
483 float rcore,lim3,lim5,dx,skylev,skbrt,*ebmvall,*zps3,*zps5,medebv,med;
484 float sig3det,sig5det,*ebmv;
485 int nresall,nalloc_resall,i,j,k,ncat,nresim,ncut,nresimtot,method;
487 const char *fctid =
"vircam_photcal_extinct";
489 cpl_propertylist *ehu_im,*ehu_cat;
491 cpl_image *schl_n,*schl_s;
495 if (*status != VIR_OK)
501 cpl_msg_error(fctid,
"No images included in photometric calibration");
507 schl_n = cpl_image_load(cpl_frame_get_filename(schlf_n),CPL_TYPE_FLOAT,
509 schl_s = cpl_image_load(cpl_frame_get_filename(schlf_s),CPL_TYPE_FLOAT,
511 if (cpl_error_get_code() != CPL_ERROR_NONE) {
512 cpl_msg_error(fctid,
"Unable to read Schlegel maps");
521 if (vircam_phot_open(phottab,filt) != VIR_OK)
526 stdmagptr = cpl_malloc(p.ncolumns*
sizeof(
float *));
532 resall3 = cpl_malloc(INITALLOC*
sizeof(
float));
533 resall5 = cpl_malloc(INITALLOC*
sizeof(
float));
534 ebmvall = cpl_malloc(INITALLOC*
sizeof(
float));
536 nalloc_resall = INITALLOC;
537 zps3 = cpl_malloc(nimages*
sizeof(
float));
538 zps5 = cpl_malloc(nimages*
sizeof(
float));
548 for (i = 0; i < nimages; i++) {
553 cdfudge = 2.5*log10((
double)pixsize(ehu_im)/NOMPIXSIZE);
557 ncat = (int)cpl_table_get_nrow(stds);
559 cpl_msg_warning(fctid,
"Too few standards available");
561 write_hdr_1(ehu_im,ncat,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0,1);
562 write_hdr_1(ehu_cat,ncat,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0,0);
569 saturate = cpl_propertylist_get_float(ehu_cat,
"ESO QC SATURATION");
570 apcor3 = cpl_propertylist_get_float(ehu_cat,
"APCOR3");
571 apcor5 = cpl_propertylist_get_float(ehu_cat,
"APCOR5");
572 rcore = cpl_propertylist_get_float(ehu_cat,
"ESO DRS RCORE");
573 skylev = cpl_propertylist_get_float(ehu_cat,
"ESO DRS SKYLEVEL");
574 dx = pixsize(ehu_im);
577 if (cpl_error_get_code() != CPL_ERROR_NONE) {
578 cpl_msg_error(fctid,
"Unable to get header info");
585 modifytab(stds,im,saturate,minstars,schl_n,schl_s,&method,&cl);
587 cpl_msg_error(fctid,
"Unable to select data from matched stds tab");
589 write_hdr_1(ehu_im,ncat,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0,1);
590 write_hdr_1(ehu_cat,ncat,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0,0);
596 ncat = (int)cpl_table_get_nrow(cl);
597 catcore3 = cpl_table_get_data_float(cl,
"Aper_flux_3");
598 catcore5 = cpl_table_get_data_float(cl,
"Aper_flux_5");
599 ebmv = cpl_table_get_data_float(cl,
"ebmv");
600 for (j = 0; j < p.ncolumns; j++)
601 stdmagptr[j] = cpl_table_get_data_float(cl,(p.columns)[j]);
605 resim3 = cpl_malloc(ncat*
sizeof(
float));
606 resim5 = cpl_malloc(ncat*
sizeof(
float));
611 extinct = p.extinct*(airmass - 1.0);
612 for (j = 0; j < ncat; j++) {
616 cf = catcore3[j]/exptime;
619 fluxmag3 = 2.5*log10((
double)cf) + apcor3;
620 cf = catcore5[j]/exptime;
623 fluxmag5 = 2.5*log10((
double)cf) + apcor5;
628 for (k = 0; k < p.nmags; k++)
629 refmag += ((p.coloureq)[k]*stdmagptr[k][j]);
630 refmag += min(10.0,ebmv[j])*p.extcoef;
634 dm3 = refmag + fluxmag3 + extinct;
635 dm5 = refmag + fluxmag5 + extinct;
636 resim3[nresim] = dm3 + cdfudge;
637 resim5[nresim++] = dm5 + cdfudge;
638 ebmvall[nresall] = ebmv[j];
639 resall3[nresall] = dm3 + cdfudge;
640 resall5[nresall++] = dm5 + cdfudge;
641 if (nresall == nalloc_resall) {
642 nalloc_resall += INITALLOC;
643 resall3 = cpl_realloc(resall3,nalloc_resall*
sizeof(
float));
644 resall5 = cpl_realloc(resall5,nalloc_resall*
sizeof(
float));
645 ebmvall = cpl_realloc(ebmvall,nalloc_resall*
sizeof(
float));
656 zps5[ngood++] = med5;
660 nresimtot += cpl_table_get_nrow(cl);
661 ncut += cpl_table_get_nrow(stds) - nresim;
671 lim3 = med3 - 2.5*log10((5.0*sig3*rcore*sqrt(CPL_MATH_PI))/exptime) -
673 lim5 = med5 - 2.5*log10((5.0*sig5*rcore*sqrt(CPL_MATH_PI))/exptime) -
678 skbrt = lim3 - 2.5*log10(skylev/(exptime*dx*dx)) - extinct;
683 write_hdr_1(ehu_im,nresim,med3,sig3,lim3,med5,sig5,lim5,p.extinct,
685 write_hdr_1(ehu_cat,nresim,med3,sig3,lim3,med5,sig5,lim5,p.extinct,
687 cpl_propertylist_update_double(ehu_im,
"ESO QC LIMITING_MAG",lim3);
688 cpl_propertylist_set_comment(ehu_im,
"ESO QC LIMITING_MAG",
689 "[mag] 5 sigma limiting mag.");
690 cpl_propertylist_update_double(ehu_im,
"ESO QC SKYBRIGHT",skbrt);
691 cpl_propertylist_set_comment(ehu_im,
"ESO QC SKYBRIGHT",
692 "[mag/arcsec**2] sky brightness");
702 (void)
vircam_medmad(ebmvall,NULL,(
long)nresall,&medebv,&mad);
728 freespace(stdmagptr);
738 for (i = 0; i < nimages; i++) {
743 cpl_propertylist_update_int(ehu_im,
"ESO DRS MAGNZPTALL",nresall);
744 cpl_propertylist_set_comment(ehu_im,
"ESO DRS MAGNZPTALL",
745 "number of stars in all magzpt calc");
746 cpl_propertylist_update_double(ehu_im,
"ESO DRS ZPALL1",med3);
747 cpl_propertylist_set_comment(ehu_im,
"ESO DRS ZPALL1",
748 "[mag] zeropoint 1*rcore all images");
749 cpl_propertylist_update_double(ehu_im,
"ESO DRS ZPSIGALL1",sig3);
750 cpl_propertylist_set_comment(ehu_im,
"ESO DRS ZPSIGALL1",
751 "[mag] zeropoint sigma 1*rcore all images");
752 cpl_propertylist_update_double(ehu_im,
"ESO DRS ZPALL2",med5);
753 cpl_propertylist_set_comment(ehu_im,
"ESO DRS ZPALL2",
754 "[mag] zeropoint 2*rcore all images");
755 cpl_propertylist_update_double(ehu_im,
"ESO DRS ZPSIGALL2",sig5);
756 cpl_propertylist_set_comment(ehu_im,
"ESO DRS ZPSIGALL2",
757 "[mag] zeropoint sigma 2*rcore all images");
758 cpl_propertylist_update_double(ehu_im,
"ESO DRS MEDEBV",medebv);
759 cpl_propertylist_set_comment(ehu_im,
"ESO DRS MEDEBV",
760 "[mag] median galactic colour excess");
761 cpl_propertylist_update_double(ehu_im,
"ESO DRS SIGDET1",sig3det);
762 cpl_propertylist_set_comment(ehu_im,
"ESO DRS SIGDET1",
763 "[mag] sigma det-level zpt 1*rcore");
764 cpl_propertylist_update_double(ehu_im,
"ESO DRS SIGDET2",sig5det);
765 cpl_propertylist_set_comment(ehu_im,
"ESO DRS SIGDET2",
766 "[mag] sigma det-level zpt 2*rcore");
767 cpl_propertylist_update_double(ehu_im,
"ESO QC MAGZPT",med3);
768 cpl_propertylist_set_comment(ehu_im,
"ESO QC MAGZPT",
769 "[mag] photometric zeropoint");
770 cpl_propertylist_update_double(ehu_im,
"ESO QC MAGZERR",sig3det);
771 cpl_propertylist_set_comment(ehu_im,
"ESO QC MAGZERR",
772 "[mag] photometric zeropoint error");
773 cpl_propertylist_update_int(ehu_im,
"ESO QC MAGNZPT",nresimtot);
774 cpl_propertylist_set_comment(ehu_im,
"ESO QC MAGNZPT",
775 "number of stars in magzpt calc");
776 cpl_propertylist_update_int(ehu_im,
"ESO QC MAGNCUT",ncut);
777 cpl_propertylist_set_comment(ehu_im,
"ESO QC MAGNCUT",
778 "number of stars cut from magzpt calc");
783 cpl_propertylist_update_int(ehu_cat,
"ESO DRS MAGNZPTALL",nresall);
784 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS MAGNZPTALL",
785 "number of stars in all magzpt calc");
786 cpl_propertylist_update_double(ehu_cat,
"ESO DRS ZPALL1",med3);
787 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS ZPALL1",
788 "[mag] zeropoint 1*rcore all group images");
789 cpl_propertylist_update_double(ehu_cat,
"ESO DRS ZPSIGALL1",sig3);
790 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS ZPSIGALL1",
791 "[mag] zeropoint sigma 1*rcore all group images");
792 cpl_propertylist_update_double(ehu_cat,
"ESO DRS ZPALL2",med5);
793 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS ZPALL2",
794 "[mag] zeropoint 2*rcore all group images");
795 cpl_propertylist_update_double(ehu_cat,
"ESO DRS ZPSIGALL2",sig5);
796 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS ZPSIGALL2",
797 "[mag] zeropoint sigma 2*rcore all group images");
798 cpl_propertylist_update_double(ehu_cat,
"ESO DRS MEDEBV",medebv);
799 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS MEDEBV",
800 "[mag] median galactic colour excess");
801 cpl_propertylist_update_double(ehu_cat,
"ESO DRS SIGDET1",sig3det);
802 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS SIGDET1",
803 "[mag] sigma det-level zpt 1*rcore");
804 cpl_propertylist_update_double(ehu_cat,
"ESO DRS SIGDET2",sig5det);
805 cpl_propertylist_set_comment(ehu_cat,
"ESO DRS SIGDET2",
806 "[mag] sigma det-level zpt 2*rcore");
807 cpl_propertylist_update_double(ehu_cat,
"ESO QC MAGZPT",med3);
808 cpl_propertylist_set_comment(ehu_cat,
"ESO QC MAGZPT",
809 "[mag] photometric zeropoint");
810 cpl_propertylist_update_double(ehu_cat,
"ESO QC MAGZERR",sig3det);
811 cpl_propertylist_set_comment(ehu_cat,
"ESO QC MAGZERR",
812 "[mag] photometric zeropoint error");
813 cpl_propertylist_update_int(ehu_cat,
"ESO QC MAGNZPT",nresimtot);
814 cpl_propertylist_set_comment(ehu_cat,
"ESO QC MAGNZPT",
815 "number of stars in magzpt calc");
816 cpl_propertylist_update_int(ehu_cat,
"ESO QC MAGNCUT",ncut);
817 cpl_propertylist_set_comment(ehu_cat,
"ESO QC MAGNCUT",
818 "number of stars cut from magzpt calc");
881 cpl_propertylist **pl,
int nimages,
char *filt,
882 cpl_table *phottab,
int nbsize, cpl_table **illcor,
883 float *illcor_rms,
int *status) {
884 const char *fctid =
"vircam_illum";
886 float **stdmagptr,fracx,fracy,**results,cdfudge,saturate,apcor3,exptime;
887 float airmass,*catcore3,*xx,*yy,extinct,cf,fluxmag3,refmag,dm3,med,mad;
888 float xmin,xmax,ymin,ymax,*resall,medall,lcut,hcut,sig,cut,*good;
889 int nx,ny,ifracx,ifracy,nbsizx,nbsizy,nbx,nby,*nres,*nall,i,j,ncat,k;
890 int ix,iy,ind,nresall,nallocall,ngood;
892 cpl_propertylist *ehu_im,*ehu_cat;
899 if (*status != VIR_OK)
905 cpl_msg_error(fctid,
"No images included in photometric calibration");
912 if (vircam_phot_open(phottab,filt) != VIR_OK)
917 stdmagptr = cpl_malloc(p.ncolumns*
sizeof(
float *));
923 fracx = ((float)nx)/((float)nbsize);
924 fracy = ((float)ny)/((float)nbsize);
925 ifracx = (int)(fracx + 0.1);
926 ifracy = (int)(fracy + 0.1);
929 nbsize = max(vircam_nint(0.9*nbsize),min(nbsize,min(nbsizx,nbsizy)));
930 nbsize = min(nx,min(ny,nbsize));
941 results = cpl_malloc(nbx*nby*
sizeof(
float *));
942 good = cpl_malloc(nbx*nby*
sizeof(
float));
943 nres = cpl_calloc(nbx*nby,
sizeof(
int));
944 nall = cpl_malloc(nbx*nby*
sizeof(
int));
945 for (i = 0; i < nbx*nby; i++) {
946 results[i] = cpl_malloc(INITALLOC*
sizeof(
float));
949 resall = cpl_malloc(INITALLOC*
sizeof(
float));
951 nallocall = INITALLOC;
962 for (i = 0; i < nimages; i++) {
969 cdfudge = 2.5*log10((
double)pixsize(ehu_im)/NOMPIXSIZE);
974 saturate = cpl_propertylist_get_float(ehu_cat,
"ESO QC SATURATION");
975 apcor3 = cpl_propertylist_get_float(ehu_cat,
"APCOR3");
978 if (cpl_error_get_code() != CPL_ERROR_NONE) {
979 cpl_msg_error(fctid,
"Unable to get header info for %s",
987 cpl_table_select_all(stds);
988 (void)cpl_table_and_selected_float(stds,
"Ellipticity",CPL_LESS_THAN,
990 (void)cpl_table_and_selected_float(stds,
"Peak_height",CPL_LESS_THAN,
992 if (cpl_error_get_code() != CPL_ERROR_NONE) {
993 cpl_msg_error(fctid,
"Unable select data from matched stds tab %s",
1001 for (j = 0; j < p.ncolumns; j++) {
1002 (void)snprintf(junk,SZBUF,
"%ssig",(p.columns)[j]);
1003 (void)cpl_table_and_selected_float(stds,junk,CPL_LESS_THAN,0.1);
1009 cl = cpl_table_extract_selected(stds);
1010 ncat = (int)cpl_table_get_nrow(cl);
1012 cpl_msg_error(fctid,
"No good standards available for %s",
1014 cpl_table_delete(cl);
1021 catcore3 = cpl_table_get_data_float(cl,
"Aper_flux_3");
1022 xx = cpl_table_get_data_float(cl,
"X_coordinate");
1023 yy = cpl_table_get_data_float(cl,
"Y_coordinate");
1024 for (j = 0; j < p.ncolumns; j++)
1025 stdmagptr[j] = cpl_table_get_data_float(cl,(p.columns)[j]);
1029 extinct = p.extinct*(airmass - 1.0);
1030 for (j = 0; j < ncat; j++) {
1034 cf = catcore3[j]/exptime;
1037 fluxmag3 = 2.5*log10((
double)cf) + apcor3;
1042 for (k = 0; k < p.nmags; k++)
1043 refmag += ((p.coloureq)[k]*stdmagptr[k][j]);
1047 dm3 = refmag + fluxmag3 + extinct + cdfudge;
1051 ix = (int)(xx[j]/(
float)nbsize);
1052 iy = (int)(yy[j]/(
float)nbsize);
1054 if (nres[ind] == nall[ind] - 1) {
1055 results[ind] = cpl_realloc(results[ind],
1056 (nall[ind]+INITALLOC)*
sizeof(
float));
1057 nall[ind] += INITALLOC;
1059 results[ind][nres[ind]] = dm3;
1064 if (nresall == nallocall) {
1065 resall = cpl_realloc(resall,(nallocall+INITALLOC)*
sizeof(
float));
1066 nallocall += INITALLOC;
1068 resall[nresall++] = dm3;
1079 (void)
vircam_medmad(resall,NULL,(
long)nresall,&medall,&mad);
1080 cut = max(3.0*1.48*mad,0.1);
1081 lcut = medall - cut;
1082 hcut = medall + cut;
1092 for (i = 0; i < nbx*nby; i++) {
1094 (void)
vircam_medmad(results[i],NULL,(
long)nres[i],&med,&mad);
1095 cut = max(3.0*1.48*mad,0.3);
1101 good[ngood++] = med;
1111 xmax = xmin + nbsize - 1;
1115 ymax = ymin + nbsize - 1;
1121 cpl_table_set_float(*illcor,
"xmin",(cpl_size)i,xmin);
1122 cpl_table_set_float(*illcor,
"xmax",(cpl_size)i,xmax);
1123 cpl_table_set_float(*illcor,
"ymin",(cpl_size)i,ymin);
1124 cpl_table_set_float(*illcor,
"ymax",(cpl_size)i,ymax);
1125 cpl_table_set_float(*illcor,
"illcor",(cpl_size)i,med);
1137 for (i = 0; i < nbx*nby; i++)
1138 freespace(results[i]);
1142 freespace(stdmagptr);
1145 vircam_phot_close();
1175 static double pixsize (cpl_propertylist *plist) {
1176 double cd1_1,cd1_2,pix;
1182 pix = 3600.0*sqrt(cd1_1*cd1_1 + cd1_2*cd1_2);
1212 static int vircam_phot_open(cpl_table *phottab,
char *filt) {
1213 const char *fctid =
"vircam_phot_open";
1216 const char *req_cols[5] = {
"filter",
"extinction",
"offset",
"columns",
1230 for (ns = 0; ns < 5; ns++) {
1231 if (! cpl_table_has_column(phottab,req_cols[ns])) {
1232 cpl_msg_error(fctid,
"Photometry table missing column %s",
1242 ns = (int)cpl_table_and_selected_string(phottab,
"filter",CPL_EQUAL_TO,filt);
1244 cpl_msg_error(fctid,
"Unable to match photometry table to filter %s",
1247 }
else if (ns > 1) {
1248 cpl_msg_error(fctid,
"More than one row matches filter %s",filt);
1253 subset = cpl_table_extract_selected(phottab);
1254 p.filt = (
char *)cpl_table_get_string(subset,
"filter",0);
1255 p.extinct = cpl_table_get_float(subset,
"extinction",0,&null);
1256 p.offset = cpl_table_get_float(subset,
"offset",0,&null);
1259 if(cpl_table_has_column(subset,
"extcoef")){
1260 p.extcoef = cpl_table_get_float(subset,
"extcoef",0,&null);
1262 cpl_msg_warning(cpl_func,
"Table column extcoef not found in the table, "
1263 "assuming an extinction coefficient of Zero");
1267 if (extract_columns(subset) != VIR_OK) {
1271 if (extract_coleq(subset) != VIR_OK) {
1299 static void vircam_phot_close(
void) {
1302 for (j = 0; j < p.ncolumns; j++)
1303 freespace((p.columns)[j]);
1304 freespace(p.columns);
1305 freespace(p.coloureq);
1327 static int extract_columns(cpl_table *tab) {
1333 v = cpl_strdup(cpl_table_get_string(tab,
"columns",0));
1340 for (i = 0; i < j; i++)
1347 p.columns = cpl_malloc(nv*
sizeof(
char *));
1348 for (i = 0; i < nv; i++) {
1352 w = strtok(NULL,
",");
1353 (p.columns)[i] = cpl_strdup(w);
1378 static int extract_coleq(cpl_table *tab) {
1384 v = cpl_strdup(cpl_table_get_string(tab,
"coleq",0));
1391 for (i = 0; i < j; i++)
1398 p.coloureq = cpl_malloc(nv*
sizeof(
float));
1399 for (i = 0; i < nv; i++) {
1403 w = strtok(NULL,
",");
1404 (p.coloureq)[i] = (
float)atof(w);
1410 static void write_hdr_1(cpl_propertylist *pl,
int nresim,
float med3,
1411 float sig3,
float lim3,
float med5,
float sig5,
1412 float lim5,
float extinct,
float skybrt,
int ncut,
1420 cpl_propertylist_update_double(pl,
"ESO QC MAGZPT",med3);
1421 cpl_propertylist_set_comment(pl,
"ESO QC MAGZPT",
1422 "[mag] photometric zeropoint");
1423 cpl_propertylist_update_double(pl,
"ESO QC MAGZERR",sig3);
1424 cpl_propertylist_set_comment(pl,
"ESO QC MAGZERR",
1425 "[mag] photometric zeropoint error");
1426 cpl_propertylist_update_int(pl,
"ESO QC MAGNZPT",nresim);
1427 cpl_propertylist_set_comment(pl,
"ESO QC MAGNZPT",
1428 "number of stars in magzpt calc");
1429 cpl_propertylist_update_int(pl,
"ESO QC MAGNCUT",ncut);
1430 cpl_propertylist_set_comment(pl,
"ESO QC MAGNCUT",
1431 "number of stars cut from magzpt calc");
1432 cpl_propertylist_update_double(pl,
"ESO QC LIMITING_MAG",lim3);
1433 cpl_propertylist_set_comment(pl,
"ESO QC LIMITING_MAG",
1434 "[mag] 5 sigma limiting mag.");
1435 cpl_propertylist_update_double(pl,
"ESO QC SKYBRIGHT",skybrt);
1436 cpl_propertylist_set_comment(pl,
"ESO QC SKYBRIGHT",
1437 "[mag/arcsec**2] sky brightness");
1442 cpl_propertylist_update_int(pl,
"ESO DRS MAGNZPTIM",nresim);
1443 cpl_propertylist_set_comment(pl,
"ESO DRS MAGNZPTIM",
1444 "number of stars in image magzpt calc");
1446 cpl_propertylist_update_double(pl,
"ESO DRS ZPIM1",med3);
1447 cpl_propertylist_set_comment(pl,
"ESO DRS ZPIM1",
1448 "[mag] zeropoint 1*rcore this image only");
1449 cpl_propertylist_update_double(pl,
"ESO DRS ZPSIGIM1",sig3);
1450 cpl_propertylist_set_comment(pl,
"ESO DRS ZPSIGIM1",
1451 "[mag] zeropoint sigma 1*rcore this image only");
1452 cpl_propertylist_update_double(pl,
"ESO DRS LIMIT_MAG1",lim3);
1453 cpl_propertylist_set_comment(pl,
"ESO DRS LIMIT_MAG1",
1454 "[mag] 5 sigma limiting mag 1*rcore.");
1455 cpl_propertylist_update_double(pl,
"ESO DRS ZPIM2",med5);
1456 cpl_propertylist_set_comment(pl,
"ESO DRS ZPIM2",
1457 "[mag] zeropoint 2*rcore this image only");
1458 cpl_propertylist_update_double(pl,
"ESO DRS ZPSIGIM2",sig5);
1459 cpl_propertylist_set_comment(pl,
"ESO DRS ZPSIGIM2",
1460 "[mag] zeropoint sigma 2*rcore this image only");
1461 cpl_propertylist_update_double(pl,
"ESO DRS LIMIT_MAG2",lim5);
1462 cpl_propertylist_set_comment(pl,
"ESO DRS LIMIT_MAG2",
1463 "[mag] 5 sigma limiting mag core5.");
1464 cpl_propertylist_update_double(pl,
"ESO DRS EXTINCT",extinct);
1465 cpl_propertylist_set_comment(pl,
"ESO DRS EXTINCT",
1466 "[mag] Assumed extinction.");
1467 cpl_propertylist_update_double(pl,
"ESO DRS SKYBRIGHT",skybrt);
1468 cpl_propertylist_set_comment(pl,
"ESO DRS SKYBRIGHT",
1469 "[mag/arcsec**2] sky brightness");
1472 static void modifytab(cpl_table *intab, vir_fits *im,
float saturate,
1473 int minstars, cpl_image *schl_n, cpl_image *schl_s,
1474 int *method, cpl_table **outtab) {
1476 float satcut,*ebmv,*jmk,*jmag,*kmag,jext,hext,kext,ebv;
1477 float *jmk_corr,*flux1,*flux2,*x,*y,distcor,*ra,*dec;
1480 const char *fctid =
"modifytab";
1491 tmptab = cpl_table_duplicate(intab);
1492 nrows = (int)cpl_table_get_nrow(tmptab);
1493 cpl_table_duplicate_column(tmptab,
"Pkandsky",tmptab,
"Peak_height");
1494 cpl_table_add_columns(tmptab,
"Pkandsky",
"Sky_level");
1499 satcut = 0.9*saturate;
1500 cpl_table_select_all(tmptab);
1501 cpl_table_and_selected_float(tmptab,
"Ellipticity",CPL_LESS_THAN,0.5);
1502 cpl_table_and_selected_float(tmptab,
"Pkandsky",CPL_LESS_THAN,satcut);
1503 cpl_table_and_selected_float(tmptab,
"Classification",CPL_EQUAL_TO,-1.0);
1504 cpl_table_and_selected_float(tmptab,
"Statistic",CPL_LESS_THAN,3.0);
1509 if (cpl_table_count_selected(tmptab) < minstars) {
1510 cpl_table_delete(tmptab);
1511 cpl_msg_error(fctid,
"Matched standards has too few stars that are"
1512 " not saturated or have low enough ellipticity");
1518 cpl_table_not_selected(tmptab);
1519 cpl_table_erase_selected(tmptab);
1520 cpl_table_select_all(tmptab);
1521 nrows = cpl_table_get_nrow(tmptab);
1526 cpl_table_duplicate_column(tmptab,
"ebmv",tmptab,
"Peak_height");
1527 cpl_table_duplicate_column(tmptab,
"jmk",tmptab,
"Peak_height");
1528 cpl_table_duplicate_column(tmptab,
"jmk_corr",tmptab,
"Peak_height");
1533 ebmv = cpl_table_get_data_float(tmptab,
"ebmv");
1534 jmk = cpl_table_get_data_float(tmptab,
"jmk");
1535 jmk_corr = cpl_table_get_data_float(tmptab,
"jmk_corr");
1536 jmag = cpl_table_get_data_float(tmptab,
"j_m");
1537 kmag = cpl_table_get_data_float(tmptab,
"k_m");
1538 ra = cpl_table_get_data_float(tmptab,
"RA");
1539 dec = cpl_table_get_data_float(tmptab,
"Dec");
1540 for (i = 0; i < nrows; i++) {
1541 getextinct(schl_n,schl_s,(
double)(ra[i]),(
double)(dec[i]),&jext,
1543 jmk[i] = jmag[i] - kmag[i];
1544 jmk_corr[i] = jmk[i] - jext + kext;
1546 ebmv[i] = max(0.0,ebv - (0.5-jmk_corr[i])/0.53);
1555 cpl_table_and_selected_float(tmptab,
"jmk_corr",CPL_NOT_LESS_THAN,0.0);
1556 cpl_table_and_selected_float(tmptab,
"jmk_corr",CPL_NOT_GREATER_THAN,1.0);
1557 cpl_table_and_selected_float(tmptab,
"jmk",CPL_LESS_THAN,2.0);
1558 if (cpl_table_count_selected(tmptab) > minstars) {
1559 *outtab = cpl_table_extract_selected(tmptab);
1560 cpl_table_delete(tmptab);
1567 cpl_table_select_all(tmptab);
1568 cpl_table_and_selected_float(tmptab,
"jmk_corr",CPL_NOT_GREATER_THAN,1.0);
1569 cpl_table_and_selected_float(tmptab,
"jmk",CPL_NOT_LESS_THAN,0.0);
1570 if (cpl_table_count_selected(tmptab) > minstars) {
1571 *outtab = cpl_table_extract_selected(tmptab);
1572 cpl_table_delete(tmptab);
1580 cpl_table_unselect_all(tmptab);
1581 *outtab = cpl_table_duplicate(tmptab);
1582 cpl_table_delete(tmptab);
1588 flux1 = cpl_table_get_data_float(*outtab,
"Aper_flux_3");
1589 flux2 = cpl_table_get_data_float(*outtab,
"Aper_flux_5");
1590 x = cpl_table_get_data_float(*outtab,
"X_coordinate");
1591 y = cpl_table_get_data_float(*outtab,
"Y_coordinate");
1592 nrows = cpl_table_get_nrow(*outtab);
1593 for (i = 0; i < nrows; i++) {
1594 distcor = distort_corr((
double)x[i],(
double)y[i],w);
1595 flux1[i] *= distcor;
1596 flux2[i] *= distcor;
1602 static void getextinct(cpl_image *schl_n, cpl_image *schl_s,
double ra,
1603 double dec,
float *jext,
float *hext,
float *kext,
1605 double gal_l,gal_b,sin_b,cos_l,sin_l;
1607 float x,y,*schl_data,xx,yy,delx,dely;
1608 int ix,iy,iarg1,iarg2,iarg3,iarg4,nx,ny;
1612 radectolb(ra,dec,2000.0,&gal_l,&gal_b);
1613 gal_l *= CPL_MATH_DEG_RAD;
1614 gal_b *= CPL_MATH_DEG_RAD;
1618 schl = (gal_b >= 0.0 ? schl_n : schl_s);
1619 schl_data = cpl_image_get_data_float(schl);
1628 x = 2048.0*sqrt(1.0 - sin_b)*cos_l + 2047.5;
1629 y = -2048.0*sqrt(1.0 - sin_b)*sin_l + 2047.5;
1631 x = 2048.0*sqrt(1.0 + sin_b)*cos_l + 2047.5;
1632 y = 2048.0*sqrt(1.0 + sin_b)*sin_l + 2047.5;
1637 nx = (int)cpl_image_get_size_x(schl_n);
1638 ny = (int)cpl_image_get_size_y(schl_n);
1639 ix = min(nx-2,max(0,(
int)x));
1640 iy = min(ny-2,max(0,(
int)y));
1651 xx = (1.0 - delx)*schl_data[iarg1] + delx*schl_data[iarg2];
1652 yy = (1.0 - delx)*schl_data[iarg3] + delx*schl_data[iarg4];
1653 *ebv = (1.0 - dely)*xx + dely*yy;
1655 *ebv = 0.1 + 0.65*(*ebv - 0.1);
1661 *jext = 0.90*(*ebv);
1662 *hext = 0.58*(*ebv);
1663 *kext = 0.37*(*ebv);
1666 static void radectolb(
double ra,
double dec,
float epoch,
double *gal_l,
1668 double ti,tf,s,z,h,arcsecrad,ra2,dec2,cosdec1,sindec1,cosra1s,sinra1s;
1669 double cos_h,sin_h,x1,x2,sinb,cosdec2,sindec2,costheta,sintheta;
1670 double cosdra,sindra,ra1,dec1,sinra2,cosra2;
1671 double a0=282.25,theta=62.6,gal_l0=33.0;
1677 ti = (double)(epoch - 2000.0)/100.0;
1678 tf = (double)(equin - 2000.0)/100.0 - ti;
1679 s = (2306.2181 - 1.39656*ti - 0.000139*ti*ti)*tf +
1680 (0.30188 - 0.000344*ti)*tf*tf + 0.17998*tf*tf*tf;
1681 z = s + (0.79280 + 0.000410*ti)*tf*tf + 0.000205*tf*tf*tf;
1682 h = (2004.3109 - 0.85330*ti - 0.000217*ti*ti)*tf -
1683 (0.42665 + 0.000217*ti)*tf*tf - 0.041833*tf*tf*tf;
1687 arcsecrad = 1.0/(CPL_MATH_DEG_RAD*3600.0);
1691 ra1 = ra/CPL_MATH_DEG_RAD;
1692 dec1 = dec/CPL_MATH_DEG_RAD;
1696 cosdec1 = cos(dec1);
1697 sindec1 = sin(dec1);
1698 cosra1s = cos(ra1+s);
1699 sinra1s = sin(ra1+s);
1702 dec2 = asin(cosdec1*cosra1s*sin_h + sindec1*cos_h);
1703 cosdec2 = cos(dec2);
1704 sinra2 = cosdec1*sinra1s/cosdec2;
1705 cosra2 = (cosdec1*cosra1s*cos_h - sindec1*sin_h)/cosdec2;
1706 ra2 = atan2(sinra2,cosra2) + z;
1711 ra2 += CPL_MATH_2PI;
1715 a0 /= CPL_MATH_DEG_RAD;
1716 theta /= CPL_MATH_DEG_RAD;
1717 gal_l0 /= CPL_MATH_DEG_RAD;
1721 sindec2 = sin(dec2);
1722 costheta = cos(theta);
1723 sintheta = sin(theta);
1724 cosdra = cos(ra2 - a0);
1725 sindra = sin(ra2 - a0);
1726 x1 = cosdec2*cosdra;
1727 x2 = cosdec2*sindra*costheta + sindec2*sintheta;
1728 *gal_l = atan2(x2,x1) + gal_l0;
1730 *gal_l += CPL_MATH_2PI;
1731 sinb = sindec2*costheta - cosdec2*sindra*sintheta;
1732 *gal_b = asin(sinb);
1735 static mywcs *getwcs(vir_fits *in) {
1737 cpl_propertylist *plist;
1743 w = cpl_malloc(
sizeof(mywcs));
1744 if (strstr(cpl_propertylist_get_string(plist,
"CTYPE1"),
"TAN") == NULL)
1748 w->crval[0] = cpl_propertylist_get_double(plist,
"CRVAL1");
1749 w->crval[1] = cpl_propertylist_get_double(plist,
"CRVAL2");
1750 w->crpix[0] = cpl_propertylist_get_double(plist,
"CRPIX1");
1751 w->crpix[1] = cpl_propertylist_get_double(plist,
"CRPIX2");
1752 w->cd[0] = cpl_propertylist_get_double(plist,
"CD1_1");
1753 w->cd[1] = cpl_propertylist_get_double(plist,
"CD1_2");
1754 w->cd[2] = cpl_propertylist_get_double(plist,
"CD2_1");
1755 w->cd[3] = cpl_propertylist_get_double(plist,
"CD2_2");
1757 if (cpl_propertylist_has(plist,
"PV2_1"))
1758 w->pv21 = cpl_propertylist_get_double(plist,
"PV2_1");
1761 if (cpl_propertylist_has(plist,
"PV2_3"))
1762 w->pv23 = cpl_propertylist_get_double(plist,
"PV2_3");
1764 w->pv23 = -0.3333333333;
1765 if (cpl_propertylist_has(plist,
"PV2_5"))
1766 w->pv25 = cpl_propertylist_get_double(plist,
"PV2_5");
1771 w->pv23 = -0.33333333;
1779 for (i = 0; i < 2; i++)
1780 w->crval[i] /= CPL_MATH_DEG_RAD;
1781 for (i = 0; i < 4; i++)
1782 w->cd[i] /= CPL_MATH_DEG_RAD;
1786 w->tand = tan(w->crval[1]);
1787 w->secd = 1.0/cos(w->crval[1]);
1794 static float distort_corr(
double x,
double y, mywcs *w) {
1795 double xi,xn,r,rprime,drprime_bydr,dc,tanr;
1797 xi = (x - w->crpix[0])*(w->cd[0]) + (y - w->crpix[1])*(w->cd[1]);
1798 xn = (x - w->crpix[0])*(w->cd[2]) + (y - w->crpix[1])*(w->cd[3]);
1799 r = sqrt(xi*xi + xn*xn);
1804 rprime = r*w->pv21 + pow(r,(
double)3.0)*w->pv23 +
1805 pow(r,(
double)5.0)*w->pv25;
1806 drprime_bydr = w->pv21 + 3.0*pow(r,(
double)2.0)*w->pv23 +
1807 5.0*pow(r,(
double)4.0)*w->pv25;
1809 dc = tanr*drprime_bydr*(1.0 + tanr*tanr)/r;
1812 dc = (1.0 + tanr*tanr)*tanr/r;
cpl_table * vircam_illcor_newtab(int nrows)
void vircam_medsig(float *data, unsigned char *bpm, long np, float *med, float *sig)
int vircam_illum(vir_fits **images, cpl_table **mstds, cpl_propertylist **pl, int nimages, char *filt, cpl_table *phottab, int nbsize, cpl_table **illcor, float *illcor_rms, int *status)
Work out the illumination correction.
cpl_propertylist * vircam_tfits_get_ehu(vir_tfits *p)
int vircam_pfits_get_exptime(const cpl_propertylist *plist, float *exptime)
Get the value of exposure time.
int vircam_meansigcut(float *data, unsigned char *bpm, long npts, float lcut, float hcut, float *mean, float *sig)
char * vircam_fits_get_fullname(vir_fits *p)
cpl_image * vircam_fits_get_image(vir_fits *p)
int vircam_pfits_get_cd12(const cpl_propertylist *plist, double *cd12)
Get the value of cd1_2.
void vircam_medmad(float *data, unsigned char *bpm, long np, float *med, float *mad)
cpl_propertylist * vircam_fits_get_phu(vir_fits *p)
cpl_propertylist * vircam_fits_get_ehu(vir_fits *p)
int vircam_pfits_get_airmass(const cpl_propertylist *plist, float *airmass)
Get the value of the airmass.
int vircam_pfits_get_cd11(const cpl_propertylist *plist, double *cd11)
Get the value of cd1_1.
int vircam_photcal(vir_fits **images, cpl_table **mstds, cpl_propertylist **pl, int nimages, char *filt, cpl_table *phottab, int *status)
Work out the photometric zeropoint.