00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifdef HAVE_CONFIG_H
00031 #include <config.h>
00032 #endif
00033
00034 #include <cpl.h>
00035 #include <math.h>
00036 #include <string.h>
00037
00038 #include "vircam_mods.h"
00039 #include "vircam_utils.h"
00040 #include "vircam_pfits.h"
00041 #include "vircam_stats.h"
00042 #include "vircam_fits.h"
00043
00044 typedef struct {
00045 char *filt;
00046 char **columns;
00047 int ncolumns;
00048 float extinct;
00049 float *coloureq;
00050 float offset;
00051 int nmags;
00052 } photstrct;
00053
00054 static photstrct p;
00055
00056 #define NOMPIXSIZE 0.34
00057 #define INITALLOC 1024
00058 #define SZBUF 1024
00059
00060 static double pixsize (cpl_propertylist *plist);
00061 static int vircam_phot_open(cpl_table *phottab, char *filt);
00062 static void vircam_phot_close(void);
00063 static int extract_columns(cpl_table *tab);
00064 static int extract_coleq(cpl_table *tab);
00065 static void write_hdr_1(cpl_propertylist *p, int nresim, float med3, float sig3,
00066 float lim3, float med5, float sig5, float lim5,
00067 float extinct, float skybrt, int ncut, int doqc);
00068
00071
00155
00156
00157 extern int vircam_photcal(vir_fits **images, cpl_table **mstds,
00158 cpl_propertylist **pl, int nimages, char *filt,
00159 cpl_table *phottab, int *status) {
00160 float **stdmagptr,*resall3,*resall5,cdfudge,saturate,apcor3,apcor5,exptime;
00161 float airmass,*catcore3,*catcore5,*resim3,*resim5,cf,fluxmag3,fluxmag5;
00162 float refmag,extinct,dm3,dm5,med3,mad,cut,lcut,hcut,med5,sig3,sig5;
00163 float rcore,lim3,lim5,dx,skylev,skbrt;
00164 int nresall,nalloc_resall,i,j,k,ncat,nresim,ncut;
00165 char junk[SZBUF];
00166 const char *fctid = "vircam_photcal";
00167 vir_fits *im;
00168 cpl_propertylist *ehu_im,*ehu_cat;
00169 cpl_table *stds,*cl;
00170
00171
00172
00173 if (*status != VIR_OK)
00174 return(*status);
00175
00176
00177
00178 if (nimages <= 0) {
00179 cpl_msg_error(fctid,"No images included in photometric calibration");
00180 FATAL_ERROR
00181 }
00182
00183
00184
00185
00186 if (vircam_phot_open(phottab,filt) != VIR_OK)
00187 FATAL_ERROR
00188
00189
00190
00191 stdmagptr = cpl_malloc(p.ncolumns*sizeof(float *));
00192
00193
00194
00195
00196
00197 resall3 = cpl_malloc(INITALLOC*sizeof(float));
00198 resall5 = cpl_malloc(INITALLOC*sizeof(float));
00199 nresall = 0;
00200 nalloc_resall = INITALLOC;
00201
00202
00203
00204
00205
00206
00207 for (i = 0; i < nimages; i++) {
00208 im = images[i];
00209 ehu_im = vircam_fits_get_ehu(im);
00210 stds = mstds[i];
00211 ehu_cat = pl[i];
00212 cdfudge = 2.5*log10((double)pixsize(ehu_im)/NOMPIXSIZE);
00213
00214
00215
00216 ncat = cpl_table_get_nrow(stds);
00217 if (ncat <= 3) {
00218 cpl_msg_warning(fctid,"Too few standards available");
00219 cpl_error_reset();
00220 write_hdr_1(ehu_im,ncat,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0,1);
00221 write_hdr_1(ehu_cat,ncat,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0,0);
00222 continue;
00223 }
00224
00225
00226
00227
00228 saturate = cpl_propertylist_get_float(ehu_cat,"ESO QC SATURATION");
00229 apcor3 = cpl_propertylist_get_float(ehu_cat,"APCOR3");
00230 apcor5 = cpl_propertylist_get_float(ehu_cat,"APCOR5");
00231 rcore = cpl_propertylist_get_float(ehu_cat,"ESO DRS RCORE");
00232 skylev = cpl_propertylist_get_float(ehu_cat,"ESO DRS SKYLEVEL");
00233 dx = pixsize(ehu_im);
00234 (void)vircam_pfits_get_exptime(ehu_cat,&exptime);
00235 (void)vircam_pfits_get_airmass(vircam_fits_get_phu(im),&airmass);
00236 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00237 cpl_msg_error(fctid,"Unable to get header info");
00238 cpl_error_reset();
00239 continue;
00240 }
00241
00242
00243
00244 cpl_table_select_all(stds);
00245 cpl_table_and_selected_float(stds,"Ellipticity",CPL_LESS_THAN,0.5);
00246 cpl_table_and_selected_float(stds,"Peak_height",CPL_LESS_THAN,
00247 saturate);
00248 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00249 cpl_msg_error(fctid,"Unable select data from matched stds tab");
00250 cpl_error_reset();
00251 continue;
00252 }
00253
00254
00255
00256 for (j = 0; j < p.ncolumns; j++) {
00257 (void)snprintf(junk,SZBUF,"%ssig",(p.columns)[j]);
00258 cpl_table_and_selected_float(stds,junk,CPL_LESS_THAN,0.1);
00259 }
00260
00261
00262
00263
00264 cl = cpl_table_extract_selected(stds);
00265 ncat = cpl_table_get_nrow(cl);
00266 if (ncat <= 3) {
00267 cpl_msg_warning(fctid,"Too few good standards available");
00268 cpl_table_delete(cl);
00269 cpl_error_reset();
00270 write_hdr_1(ehu_im,ncat,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0,1);
00271 write_hdr_1(ehu_cat,ncat,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0,0);
00272 continue;
00273 }
00274
00275
00276
00277 catcore3 = cpl_table_get_data_float(cl,"Aper_flux_3");
00278 catcore5 = cpl_table_get_data_float(cl,"Aper_flux_5");
00279 for (j = 0; j < p.ncolumns; j++)
00280 stdmagptr[j] = cpl_table_get_data_float(cl,(p.columns)[j]);
00281
00282
00283
00284 resim3 = cpl_malloc(ncat*sizeof(float));
00285 resim5 = cpl_malloc(ncat*sizeof(float));
00286 nresim = 0;
00287
00288
00289
00290 extinct = p.extinct*(airmass - 1.0);
00291 for (j = 0; j < ncat; j++) {
00292
00293
00294
00295 cf = catcore3[j]/exptime;
00296 if (cf < 1.0)
00297 cf = 1.0;
00298 fluxmag3 = 2.5*log10((double)cf) + apcor3;
00299 cf = catcore5[j]/exptime;
00300 if (cf < 1.0)
00301 cf = 1.0;
00302 fluxmag5 = 2.5*log10((double)cf) + apcor5;
00303
00304
00305
00306 refmag = p.offset;
00307 for (k = 0; k < p.nmags; k++)
00308 refmag += ((p.coloureq)[k]*stdmagptr[k][j]);
00309
00310
00311
00312 dm3 = refmag + fluxmag3 + extinct;
00313 dm5 = refmag + fluxmag5 + extinct;
00314 resim3[nresim] = dm3 + cdfudge;
00315 resim5[nresim++] = dm5 + cdfudge;
00316 resall3[nresall] = dm3 + cdfudge;
00317 resall5[nresall++] = dm5 + cdfudge;
00318 if (nresall == nalloc_resall) {
00319 nalloc_resall += INITALLOC;
00320 resall3 = cpl_realloc(resall3,nalloc_resall*sizeof(float));
00321 resall5 = cpl_realloc(resall5,nalloc_resall*sizeof(float));
00322 }
00323 }
00324
00325
00326
00327 (void)vircam_medmad(resim3,NULL,(long)nresim,&med3,&mad);
00328 cut = max(3.0*1.48*mad,0.1);
00329 lcut = med3 - cut;
00330 hcut = med3 + cut;
00331 (void)vircam_meansigcut(resim3,NULL,nresim,lcut,hcut,&med3,&sig3);
00332 ncut = 0;
00333 for (j = 0; j < nresim; j++)
00334 if (resim3[j] < lcut || resim3[j] > hcut)
00335 ncut++;
00336 (void)vircam_medmad(resim5,NULL,(long)nresim,&med5,&mad);
00337 cut = max(3.0*1.48*mad,0.1);
00338 lcut = med5 - cut;
00339 hcut = med5 + cut;
00340 (void)vircam_meansigcut(resim5,NULL,nresim,lcut,hcut,&med5,&sig5);
00341
00342
00343
00344 freespace(resim3);
00345 freespace(resim5);
00346 freetable(cl);
00347
00348
00349
00350 lim3 = med3 - 2.5*log10((5.0*sig3*rcore*sqrt(CPL_MATH_PI))/exptime) -
00351 apcor3 - extinct;
00352 lim5 = med5 - 2.5*log10((5.0*sig5*rcore*sqrt(CPL_MATH_PI))/exptime) -
00353 apcor5 - extinct;
00354
00355
00356
00357 skbrt = lim3 - 2.5*log10(skylev/(exptime*dx*dx)) - extinct;
00358
00359
00360
00361
00362 write_hdr_1(ehu_im,nresim,med3,sig3,lim3,med5,sig5,lim5,p.extinct,
00363 skbrt,ncut,1);
00364 write_hdr_1(ehu_cat,nresim,med3,sig3,lim3,med5,sig5,lim5,p.extinct,
00365 skbrt,ncut,0);
00366 }
00367
00368
00369
00370 if (nresall > 0) {
00371 (void)vircam_medmad(resall3,NULL,(long)nresall,&med3,&mad);
00372 cut = max(3.0*1.48*mad,0.1);
00373 lcut = med3 - cut;
00374 hcut = med3 + cut;
00375 (void)vircam_meansigcut(resall3,NULL,nresall,lcut,hcut,&med3,&sig3);
00376 (void)vircam_medmad(resall5,NULL,(long)nresall,&med5,&mad);
00377 cut = max(3.0*1.48*mad,0.1);
00378 lcut = med5 - cut;
00379 hcut = med5 + cut;
00380 (void)vircam_meansigcut(resall5,NULL,nresall,lcut,hcut,&med5,&sig5);
00381 } else {
00382 med3 = 0.0;
00383 sig3 = 0.0;
00384 med5 = 0.0;
00385 sig5 = 0.0;
00386 }
00387
00388
00389
00390 freespace(resall3);
00391 freespace(resall5);
00392 freespace(stdmagptr);
00393 vircam_phot_close();
00394
00395
00396
00397
00398 for (i = 0; i < nimages; i++) {
00399 im = images[i];
00400 ehu_im = vircam_fits_get_ehu(im);
00401 stds = mstds[i];
00402 ehu_cat = pl[i];
00403 cpl_propertylist_update_int(ehu_im,"ESO DRS MAGNZPTALL",nresall);
00404 cpl_propertylist_set_comment(ehu_im,"ESO DRS MAGNZPTALL",
00405 "number of stars in all magzpt calc");
00406 cpl_propertylist_update_float(ehu_im,"ESO DRS ZPALL1",med3);
00407 cpl_propertylist_set_comment(ehu_im,"ESO DRS ZPALL1",
00408 "[mag] zeropoint 1*rcore all group images");
00409 cpl_propertylist_update_float(ehu_im,"ESO DRS ZPSIGALL1",sig3);
00410 cpl_propertylist_set_comment(ehu_im,"ESO DRS ZPSIGALL1",
00411 "[mag] zeropoint sigma 1*rcore all group images");
00412 cpl_propertylist_update_float(ehu_im,"ESO DRS ZPALL2",med5);
00413 cpl_propertylist_set_comment(ehu_im,"ESO DRS ZPALL2",
00414 "[mag] zeropoint 2*rcore all group images");
00415 cpl_propertylist_update_float(ehu_im,"ESO DRS ZPSIGALL2",sig5);
00416 cpl_propertylist_set_comment(ehu_im,"ESO DRS ZPSIGALL2",
00417 "[mag] zeropoint sigma 2*rcore all group images");
00418
00419
00420
00421 cpl_propertylist_update_int(ehu_cat,"ESO DRS MAGNZPTALL",nresall);
00422 cpl_propertylist_set_comment(ehu_cat,"ESO DRS MAGNZPTALL",
00423 "number of stars in all magzpt calc");
00424 cpl_propertylist_update_float(ehu_cat,"ESO DRS ZPALL1",med3);
00425 cpl_propertylist_set_comment(ehu_cat,"ESO DRS ZPALL1",
00426 "[mag] zeropoint 1*rcore all group images");
00427 cpl_propertylist_update_float(ehu_cat,"ESO DRS ZPSIGALL1",sig3);
00428 cpl_propertylist_set_comment(ehu_cat,"ESO DRS ZPSIGALL1",
00429 "[mag] zeropoint sigma 1*rcore all group images");
00430 cpl_propertylist_update_float(ehu_cat,"ESO DRS ZPALL2",med5);
00431 cpl_propertylist_set_comment(ehu_cat,"ESO DRS ZPALL2",
00432 "[mag] zeropoint 2*rcore all group images");
00433 cpl_propertylist_update_float(ehu_cat,"ESO DRS ZPSIGALL2",sig5);
00434 cpl_propertylist_set_comment(ehu_cat,"ESO DRS ZPSIGALL2",
00435 "[mag] zeropoint sigma 2*rcore all group images");
00436 }
00437
00438
00439
00440 GOOD_STATUS
00441
00442 }
00443
00444
00495
00496
00497 extern int vircam_illum(vir_fits **images, cpl_table **mstds,
00498 cpl_propertylist **pl, int nimages, char *filt,
00499 cpl_table *phottab, int nbsize, cpl_table **illcor,
00500 float *illcor_rms,int *status) {
00501 const char *fctid = "vircam_illum";
00502 char junk[SZBUF];
00503 float **stdmagptr,fracx,fracy,**results,cdfudge,saturate,apcor3,exptime;
00504 float airmass,*catcore3,*xx,*yy,extinct,cf,fluxmag3,refmag,dm3,med,mad;
00505 float xmin,xmax,ymin,ymax,*resall,medall,lcut,hcut,sig,cut,*good;
00506 int nx,ny,ifracx,ifracy,nbsizx,nbsizy,nbx,nby,*nres,*nall,i,j,ncat,k;
00507 int ix,iy,ind,nresall,nallocall,ngood;
00508 vir_fits *im;
00509 cpl_propertylist *ehu_im,*ehu_cat;
00510 cpl_table *stds,*cl;
00511
00512
00513
00514 *illcor = NULL;
00515 *illcor_rms = 0.0;
00516 if (*status != VIR_OK)
00517 return(*status);
00518
00519
00520
00521 if (nimages <= 0) {
00522 cpl_msg_error(fctid,"No images included in photometric calibration");
00523 FATAL_ERROR
00524 }
00525
00526
00527
00528
00529 if (vircam_phot_open(phottab,filt) != VIR_OK)
00530 FATAL_ERROR
00531
00532
00533
00534 stdmagptr = cpl_malloc(p.ncolumns*sizeof(float *));
00535
00536
00537
00538 nx = cpl_image_get_size_x(vircam_fits_get_image(images[0]));
00539 ny = cpl_image_get_size_y(vircam_fits_get_image(images[0]));
00540 fracx = ((float)nx)/((float)nbsize);
00541 fracy = ((float)ny)/((float)nbsize);
00542 ifracx = (int)(fracx + 0.1);
00543 ifracy = (int)(fracy + 0.1);
00544 nbsizx = nx/ifracx;
00545 nbsizy = ny/ifracy;
00546 nbsize = max(vircam_nint(0.9*nbsize),min(nbsize,min(nbsizx,nbsizy)));
00547 nbsize = min(nx,min(ny,nbsize));
00548
00549
00550
00551 nbx = nx/nbsize;
00552 nby = ny/nbsize;
00553
00554
00555
00556
00557
00558 results = cpl_malloc(nbx*nby*sizeof(float *));
00559 good = cpl_malloc(nbx*nby*sizeof(float));
00560 nres = cpl_calloc(nbx*nby,sizeof(int));
00561 nall = cpl_malloc(nbx*nby*sizeof(int));
00562 for (i = 0; i < nbx*nby; i++) {
00563 results[i] = cpl_malloc(INITALLOC*sizeof(float));
00564 nall[i] = INITALLOC;
00565 }
00566 resall = cpl_malloc(INITALLOC*sizeof(float));
00567 nresall = 0;
00568 nallocall = INITALLOC;
00569
00570
00571
00572 *illcor = vircam_illcor_newtab(nbx*nby);
00573
00574
00575
00576
00577
00578
00579 for (i = 0; i < nimages; i++) {
00580 im = images[i];
00581 ehu_im = vircam_fits_get_ehu(im);
00582 stds = mstds[i];
00583 if (stds == NULL)
00584 continue;
00585 ehu_cat = pl[i];
00586 cdfudge = 2.5*log10((double)pixsize(ehu_im)/NOMPIXSIZE);
00587
00588
00589
00590
00591 saturate = cpl_propertylist_get_float(ehu_cat,"ESO QC SATURATION");
00592 apcor3 = cpl_propertylist_get_float(ehu_cat,"APCOR3");
00593 (void)vircam_pfits_get_exptime(vircam_fits_get_phu(im),&exptime);
00594 (void)vircam_pfits_get_airmass(vircam_fits_get_phu(im),&airmass);
00595 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00596 cpl_msg_error(fctid,"Unable to get header info for %s",
00597 vircam_fits_get_fullname(im));
00598 cpl_error_reset();
00599 continue;
00600 }
00601
00602
00603
00604 cpl_table_select_all(stds);
00605 cpl_table_and_selected_float(stds,"Ellipticity",CPL_LESS_THAN,0.5);
00606 cpl_table_and_selected_float(stds,"Peak_height",CPL_LESS_THAN,
00607 saturate);
00608 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00609 cpl_msg_error(fctid,"Unable select data from matched stds tab %s",
00610 vircam_fits_get_fullname(im));
00611 cpl_error_reset();
00612 continue;
00613 }
00614
00615
00616
00617 for (j = 0; j < p.ncolumns; j++) {
00618 (void)snprintf(junk,SZBUF,"%ssig",(p.columns)[j]);
00619 cpl_table_and_selected_float(stds,junk,CPL_LESS_THAN,0.1);
00620 }
00621
00622
00623
00624
00625 cl = cpl_table_extract_selected(stds);
00626 ncat = cpl_table_get_nrow(cl);
00627 if (ncat == 0) {
00628 cpl_msg_error(fctid,"No good standards available for %s",
00629 vircam_fits_get_fullname(im));
00630 cpl_table_delete(cl);
00631 cpl_error_reset();
00632 continue;
00633 }
00634
00635
00636
00637 catcore3 = cpl_table_get_data_float(cl,"Aper_flux_3");
00638 xx = cpl_table_get_data_float(cl,"X_coordinate");
00639 yy = cpl_table_get_data_float(cl,"Y_coordinate");
00640 for (j = 0; j < p.ncolumns; j++)
00641 stdmagptr[j] = cpl_table_get_data_float(cl,(p.columns)[j]);
00642
00643
00644
00645 extinct = p.extinct*(airmass - 1.0);
00646 for (j = 0; j < ncat; j++) {
00647
00648
00649
00650 cf = catcore3[j]/exptime;
00651 if (cf < 1.0)
00652 cf = 1.0;
00653 fluxmag3 = 2.5*log10((double)cf) + apcor3;
00654
00655
00656
00657 refmag = p.offset;
00658 for (k = 0; k < p.nmags; k++)
00659 refmag += ((p.coloureq)[k]*stdmagptr[k][j]);
00660
00661
00662
00663 dm3 = refmag + fluxmag3 + extinct + cdfudge;
00664
00665
00666
00667 ix = (int)(xx[j]/(float)nbsize);
00668 iy = (int)(yy[j]/(float)nbsize);
00669 ind = iy*nbx + ix;
00670 if (nres[ind] == nall[ind] - 1) {
00671 results[ind] = cpl_realloc(results[ind],
00672 (nall[ind]+INITALLOC)*sizeof(float));
00673 nall[ind] += INITALLOC;
00674 }
00675 results[ind][nres[ind]] = dm3;
00676 nres[ind] += 1;
00677
00678
00679
00680 if (nresall == nallocall) {
00681 resall = cpl_realloc(resall,(nallocall+INITALLOC)*sizeof(float));
00682 nallocall += INITALLOC;
00683 }
00684 resall[nresall++] = dm3;
00685 }
00686
00687
00688
00689 freetable(cl);
00690 }
00691
00692
00693
00694 if (nresall != 0) {
00695 (void)vircam_medmad(resall,NULL,(long)nresall,&medall,&mad);
00696 cut = max(3.0*1.48*mad,0.1);
00697 lcut = medall - cut;
00698 hcut = medall + cut;
00699 (void)vircam_meansigcut(resall,NULL,(long)nresall,lcut,hcut,
00700 &medall,&sig);
00701 }
00702
00703
00704
00705
00706
00707 ngood = 0;
00708 for (i = 0; i < nbx*nby; i++) {
00709 if (nres[i] > 0) {
00710 (void)vircam_medmad(results[i],NULL,(long)nres[i],&med,&mad);
00711 cut = max(3.0*1.48*mad,0.3);
00712 lcut = med - cut;
00713 hcut = med + cut;
00714 (void)vircam_meansigcut(results[i],NULL,(long)nres[i],lcut,hcut,
00715 &med,&sig);
00716 med -= medall;
00717 good[ngood++] = med;
00718 } else {
00719 med = -99.0;
00720 }
00721
00722
00723
00724 iy = i/nbx;
00725 ix = i - iy*nbx;
00726 xmin = ix*nbsize;
00727 xmax = xmin + nbsize - 1;
00728 if (ix == nbx)
00729 xmax = nx;
00730 ymin = iy*nbsize;
00731 ymax = ymin + nbsize - 1;
00732 if (iy == nby)
00733 ymax = ny;
00734
00735
00736
00737 cpl_table_set_float(*illcor,"xmin",i,xmin);
00738 cpl_table_set_float(*illcor,"xmax",i,xmax);
00739 cpl_table_set_float(*illcor,"ymin",i,ymin);
00740 cpl_table_set_float(*illcor,"ymax",i,ymax);
00741 cpl_table_set_float(*illcor,"illcor",i,med);
00742 }
00743
00744
00745
00746 if (ngood > 0)
00747 vircam_medsig(good,NULL,(long)ngood,&med,illcor_rms);
00748 else
00749 *illcor_rms = 0.0;
00750
00751
00752
00753 for (i = 0; i < nbx*nby; i++)
00754 freespace(results[i]);
00755 freespace(results);
00756 freespace(nres);
00757 freespace(nall);
00758 freespace(stdmagptr);
00759 freespace(resall);
00760 freespace(good);
00761 vircam_phot_close();
00762
00763
00764
00765 if (nresall != 0)
00766 GOOD_STATUS
00767 else
00768 WARN_RETURN
00769
00770 }
00771
00772
00789
00790
00791 static double pixsize (cpl_propertylist *plist) {
00792 double cd1_1,cd1_2,pix;
00793
00794 if (vircam_pfits_get_cd11(plist,&cd1_1) != VIR_OK ||
00795 vircam_pfits_get_cd12(plist,&cd1_2) != VIR_OK) {
00796 pix = NOMPIXSIZE;
00797 } else {
00798 pix = 3600.0*sqrt(cd1_1*cd1_1 + cd1_2*cd1_2);
00799 }
00800 return(pix);
00801 }
00802
00803
00826
00827
00828 static int vircam_phot_open(cpl_table *phottab, char *filt) {
00829 const char *fctid = "vircam_phot_open";
00830 int ns,null,nerr;
00831 cpl_table *subset;
00832 const char *req_cols[5] = {"filter","extinction","offset","columns",
00833 "coleq"};
00834
00835
00836
00837 p.coloureq = NULL;
00838 p.columns = NULL;
00839 p.nmags = 0;
00840 p.ncolumns = 0;
00841 p.offset = 0.0;
00842
00843
00844
00845 nerr = 0;
00846 for (ns = 0; ns < 5; ns++) {
00847 if (! cpl_table_has_column(phottab,req_cols[ns])) {
00848 cpl_msg_error(fctid,"Photometry table missing column %s",
00849 req_cols[ns]);
00850 nerr++;
00851 }
00852 }
00853 if (nerr > 0)
00854 return(VIR_FATAL);
00855
00856
00857
00858 ns = cpl_table_and_selected_string(phottab,"filter",CPL_EQUAL_TO,filt);
00859 if (ns <= 0) {
00860 cpl_msg_error(fctid,"Unable to match photometry table to filter %s",
00861 filt);
00862 return(VIR_FATAL);
00863 } else if (ns > 1) {
00864 cpl_msg_error(fctid,"More than one row matches filter %s",filt);
00865 }
00866
00867
00868
00869 subset = cpl_table_extract_selected(phottab);
00870 p.filt = (char *)cpl_table_get_string(subset,"filter",0);
00871 p.extinct = cpl_table_get_float(subset,"extinction",0,&null);
00872 p.offset = cpl_table_get_float(subset,"offset",0,&null);
00873 if (extract_columns(subset) != VIR_OK) {
00874 freetable(subset);
00875 return(VIR_FATAL);
00876 }
00877 if (extract_coleq(subset) != VIR_OK) {
00878 freetable(subset);
00879 return(VIR_FATAL);
00880 }
00881 freetable(subset);
00882
00883
00884
00885 return(VIR_OK);
00886 }
00887
00888
00889
00903
00904
00905 static void vircam_phot_close(void) {
00906 int j;
00907
00908 for (j = 0; j < p.ncolumns; j++)
00909 freespace((p.columns)[j]);
00910 freespace(p.columns);
00911 freespace(p.coloureq);
00912 }
00913
00914
00931
00932
00933 static int extract_columns(cpl_table *tab) {
00934 int nv,i,j;
00935 char *v,*w;
00936
00937
00938
00939 v = cpl_strdup(cpl_table_get_string(tab,"columns",0));
00940
00941
00942
00943
00944 nv = 1;
00945 j = strlen(v);
00946 for (i = 0; i < j; i++)
00947 if (v[i] == ',')
00948 nv++;
00949 p.ncolumns = nv;
00950
00951
00952
00953 p.columns = cpl_malloc(nv*sizeof(char *));
00954 for (i = 0; i < nv; i++) {
00955 if (i == 0)
00956 w = strtok(v,",");
00957 else
00958 w = strtok(NULL,",");
00959 (p.columns)[i] = cpl_strdup(w);
00960 }
00961 freespace(v);
00962 return(VIR_OK);
00963 }
00964
00965
00982
00983
00984 static int extract_coleq(cpl_table *tab) {
00985 int nv,i,j;
00986 char *v,*w;
00987
00988
00989
00990 v = cpl_strdup(cpl_table_get_string(tab,"coleq",0));
00991
00992
00993
00994
00995 nv = 1;
00996 j = strlen(v);
00997 for (i = 0; i < j; i++)
00998 if (v[i] == ',')
00999 nv++;
01000 p.nmags = nv;
01001
01002
01003
01004 p.coloureq = cpl_malloc(nv*sizeof(float));
01005 for (i = 0; i < nv; i++) {
01006 if (i == 0)
01007 w = strtok(v,",");
01008 else
01009 w = strtok(NULL,",");
01010 (p.coloureq)[i] = (float)atof(w);
01011 }
01012 freespace(v);
01013 return(VIR_OK);
01014 }
01015
01016 static void write_hdr_1(cpl_propertylist *pl, int nresim, float med3,
01017 float sig3, float lim3, float med5, float sig5,
01018 float lim5, float extinct, float skybrt, int ncut,
01019 int doqc) {
01020
01021
01022
01023
01024
01025 if (doqc) {
01026 cpl_propertylist_update_float(pl,"ESO QC MAGZPT",med3);
01027 cpl_propertylist_set_comment(pl,"ESO QC MAGZPT",
01028 "[mag] photometric zeropoint");
01029 cpl_propertylist_update_float(pl,"ESO QC MAGZERR",sig3);
01030 cpl_propertylist_set_comment(pl,"ESO QC MAGZERR",
01031 "[mag] photometric zeropoint error");
01032 cpl_propertylist_update_int(pl,"ESO QC MAGNZPT",nresim);
01033 cpl_propertylist_set_comment(pl,"ESO QC MAGNZPT",
01034 "number of stars in magzpt calc");
01035 cpl_propertylist_update_int(pl,"ESO QC MAGNCUT",ncut);
01036 cpl_propertylist_set_comment(pl,"ESO QC MAGNCUT",
01037 "number of stars cut from magzpt calc");
01038 cpl_propertylist_update_float(pl,"ESO QC LIMITING_MAG",lim3);
01039 cpl_propertylist_set_comment(pl,"ESO QC LIMITING_MAG",
01040 "[mag] 5 sigma limiting mag.");
01041 cpl_propertylist_update_float(pl,"ESO QC SKYBRIGHT",skybrt);
01042 cpl_propertylist_set_comment(pl,"ESO QC SKYBRIGHT",
01043 "[mag/arcsec**2] sky brightness");
01044 }
01045
01046
01047
01048 cpl_propertylist_update_int(pl,"ESO DRS MAGNZPTIM",nresim);
01049 cpl_propertylist_set_comment(pl,"ESO DRS MAGNZPTIM",
01050 "number of stars in image magzpt calc");
01051
01052 cpl_propertylist_update_float(pl,"ESO DRS ZPIM1",med3);
01053 cpl_propertylist_set_comment(pl,"ESO DRS ZPIM1",
01054 "[mag] zeropoint 1*rcore this image only");
01055 cpl_propertylist_update_float(pl,"ESO DRS ZPSIGIM1",sig3);
01056 cpl_propertylist_set_comment(pl,"ESO DRS ZPSIGIM1",
01057 "[mag] zeropoint sigma 1*rcore this image only");
01058 cpl_propertylist_update_float(pl,"ESO DRS LIMIT_MAG1",lim3);
01059 cpl_propertylist_set_comment(pl,"ESO DRS LIMIT_MAG1",
01060 "[mag] 5 sigma limiting mag 1*rcore.");
01061 cpl_propertylist_update_float(pl,"ESO DRS ZPIM2",med5);
01062 cpl_propertylist_set_comment(pl,"ESO DRS ZPIM2",
01063 "[mag] zeropoint 2*rcore this image only");
01064 cpl_propertylist_update_float(pl,"ESO DRS ZPSIGIM2",sig5);
01065 cpl_propertylist_set_comment(pl,"ESO DRS ZPSIGIM2",
01066 "[mag] zeropoint sigma 2*rcore this image only");
01067 cpl_propertylist_update_float(pl,"ESO DRS LIMIT_MAG2",lim5);
01068 cpl_propertylist_set_comment(pl,"ESO DRS LIMIT_MAG2",
01069 "[mag] 5 sigma limiting mag core5.");
01070 cpl_propertylist_update_float(pl,"ESO DRS EXTINCT",extinct);
01071 cpl_propertylist_set_comment(pl,"ESO DRS EXTINCT",
01072 "[mag] Assumed extinction.");
01073 cpl_propertylist_update_float(pl,"ESO DRS SKYBRIGHT",skybrt);
01074 cpl_propertylist_set_comment(pl,"ESO DRS SKYBRIGHT",
01075 "[mag/arcsec**2] sky brightness");
01076 }
01077
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194