00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00029
00030 #include <irplib_utils.h>
00031 #include <uves_msg.h>
00032 #include <uves_error.h>
00033 #include <uves_qclog.h>
00034 #include <uves_dfs.h>
00035 #include <uves_utils_wrappers.h>
00036 #include <flames_midas_def.h>
00037 #include <flames_corvel.h>
00038 #include <flames_reduce_vcorrel.h>
00039
00040
00041
00042 double DRS_CVEL_MIN = -6.;
00043 double DRS_CVEL_MAX = +6.;
00044 double DRS_CVEL_STEP = 0.5;
00045
00046
00047 int
00048 flames_reduce_vcorrel(const char* sci_pfx,
00049 const char* cvel_pfx,
00050 const char* ccd_id,
00051 const int ord_max,
00052 const cpl_frame* cvel_tab,
00053 const char* xxima_pfx,
00054 const char* xwcal_pfx,
00055 const double drs_cvel_min,
00056 const double drs_cvel_max,
00057 double* zero_point,
00058 double* avg_cnt,
00059 double* sig_cnt,
00060 cpl_table* qclog)
00061 {
00062
00063 int ord = 0;
00064 int ord_min=1;
00065 char cvel_tab_name[80];
00066 char tim_ccf[80];
00067 char nim_ccf[80];
00068 char iim_pfx[80];
00069 char tab_pfx[80];
00070 char nim_pfx[80];
00071 char tim_pfx[80];
00072 char ref_frm[80];
00073 char otab[80];
00074 const int npix=(drs_cvel_max-drs_cvel_min)/DRS_CVEL_STEP+1;
00075
00076 cpl_image* tim_ccf_ima=NULL;
00077 cpl_image* nim_ccf_ima=NULL;
00078
00079 cpl_table* cvel_tbl=NULL;
00080 double ccfcnt[ord_max];
00081 int num_ord=0;
00082 int status=0;
00083 int unit=0;
00084 int actvals=0;
00085 int null=0;
00086
00087 int in_ima_id=0;
00088 int iim_pfx_id=0;
00089
00090 char out_cvel_tab[80];
00091 char out_tot_ima[80];
00092 char out_nrm_ima[80];
00093 cpl_table* cvel_ord_tbl=NULL;
00094 int row_pos=0;
00095
00096 cpl_image* tot_ima=NULL;
00097 cpl_image* nrm_ima=NULL;
00098 int nrow=0;
00099
00100 double ccf_max=0;
00101 double wav_rng=0;
00102 double pix_tot=0;
00103 int lin_tot=0;
00104 cpl_propertylist* plist_ima_new=NULL;
00105 cpl_propertylist* plist_tbl_new=NULL;
00106
00107 double corvel_max=0;
00108
00109
00110 char key_name[80];
00111
00112 const char* command=NULL;
00113
00114
00115
00116
00117 uves_msg_debug("sci_pfx=%s",sci_pfx);
00118 uves_msg_debug("ccd_id=%s",ccd_id);
00119
00120 sprintf(otab,"%s%s%s",cvel_pfx,ccd_id,".fits");
00121 sprintf(tim_ccf,"%s%s%s%s","tot_",cvel_pfx,ccd_id,".fits");
00122 sprintf(nim_ccf,"%s%s%s%s","nrm_",cvel_pfx,ccd_id,".fits");
00123 sprintf(iim_pfx,"%s%s%s%s","mw",sci_pfx,xxima_pfx,".fits");
00124 sprintf(tab_pfx,"%s%s%s","tab_",ccd_id,"_");
00125 sprintf(nim_pfx,"%s%s%s","nrm_",ccd_id,"_");
00126 sprintf(tim_pfx,"%s%s%s","tot_",ccd_id,"_");
00127 sprintf(ref_frm,"%s%s%s","w",sci_pfx,xwcal_pfx);
00128
00129
00130 uves_msg_debug("pointer=%p",cvel_tab);
00131
00132 uves_msg("name=%s",cpl_frame_get_filename(cvel_tab));
00133 sprintf(cvel_tab_name,"%s",cpl_frame_get_filename(cvel_tab));
00134
00135 uves_msg_debug("npix=%d",npix);
00136 tim_ccf_ima=cpl_image_new(npix,ord_max,CPL_TYPE_FLOAT);
00137 nim_ccf_ima=cpl_image_new(npix,ord_max,CPL_TYPE_FLOAT);
00138
00139
00140
00141 row_pos=0;
00142 uves_msg_debug("ord_max=%d nlines=%d",ord_max,npix*ord_max);
00143
00144 cvel_tbl=cpl_table_new(0);
00145 check_nomsg(cpl_table_new_column(cvel_tbl,"Select",CPL_TYPE_INT));
00146 check_nomsg(cpl_table_new_column(cvel_tbl,"ccf_pos",CPL_TYPE_DOUBLE));
00147 check_nomsg(cpl_table_new_column(cvel_tbl,"ccf_nrm",CPL_TYPE_DOUBLE));
00148 check_nomsg(cpl_table_new_column(cvel_tbl,"ccf_out",CPL_TYPE_DOUBLE));
00149 check_nomsg(cpl_table_new_column(cvel_tbl,"ORDER",CPL_TYPE_INT));
00150 check_nomsg(plist_ima_new=cpl_propertylist_new());
00151
00152
00153
00154 ck0_nomsg(uves_qclog_add_string(qclog,
00155 "QC TEST2 ID",
00156 "Fibre-Science-QC",
00157 "Name of QC test",
00158 "%s"));
00159 uves_check_rec_status(0);
00160
00161 for (ord=ord_min;ord<=ord_max;ord++) {
00162 ccfcnt[ord]=0;
00163
00164
00165 sprintf(out_cvel_tab,"%s%s%d%s","tab_",ccd_id,ord,".fits");
00166 sprintf(out_tot_ima,"%s%s%d%s","tot_",ccd_id,ord,".fits");
00167 sprintf(out_nrm_ima,"%s%s%d%s","nrm_",ccd_id,ord,".fits");
00168
00169
00170 uves_msg_debug("vc2 %s %s %s",out_cvel_tab,out_tot_ima,out_nrm_ima);
00171 uves_msg_debug("iim_pfx=%s cvel_tab_name=%s ord=%d",
00172 iim_pfx,cvel_tab_name,ord);
00173
00174 uves_msg_debug("out_cvel_tab=%s",out_cvel_tab);
00175 uves_msg_debug("out_tot_ima=%s",out_tot_ima);
00176 uves_msg_debug("out_nrm_ima=%s",out_nrm_ima);
00177 uves_msg_debug("ord=%d",ord);
00178
00179
00180
00181
00182 check_nomsg(flames_corvel(iim_pfx,
00183 cvel_tab_name,
00184 ord,
00185 out_cvel_tab,
00186 out_tot_ima,
00187 out_nrm_ima,
00188 drs_cvel_min,
00189 drs_cvel_max,
00190 DRS_CVEL_STEP));
00191
00192
00193 uves_msg_debug("CVEL MAX=%f MIN=%f STEP=%f",
00194 drs_cvel_min,drs_cvel_max,DRS_CVEL_STEP);
00195
00196 check_nomsg(tot_ima=cpl_image_load(out_tot_ima,CPL_TYPE_FLOAT,0,0));
00197 check_nomsg(nrm_ima=cpl_image_load(out_nrm_ima,CPL_TYPE_FLOAT,0,0));
00198 check_nomsg(cpl_image_copy(tim_ccf_ima,tot_ima,1,ord-ord_min+1));
00199 check_nomsg(cpl_image_copy(nim_ccf_ima,nrm_ima,1,ord-ord_min+1));
00200 check_nomsg(cvel_ord_tbl=cpl_table_load(out_cvel_tab,1,1));
00201
00202
00203 check_nomsg(cpl_table_new_column(cvel_ord_tbl,"ORDER",CPL_TYPE_INT));
00204 check_nomsg(nrow=cpl_table_get_nrow(cvel_ord_tbl));
00205
00206 check_nomsg(cpl_table_fill_column_window_int(cvel_ord_tbl,"ORDER",0,nrow,ord));
00207 check_nomsg(cpl_table_insert(cvel_tbl,cvel_ord_tbl,row_pos));
00208 row_pos+=nrow;
00209
00210 if((status=SCFOPN(out_tot_ima,D_R4_FORMAT,0,F_IMA_TYPE,&in_ima_id))!=0) {
00211 uves_msg_error("opening frame %s",out_tot_ima);
00212 return flames_midas_error(MAREMMA);
00213 }
00214
00215 if((status=SCFOPN(iim_pfx,D_R4_FORMAT,0,F_IMA_TYPE,&iim_pfx_id))!=0) {
00216 uves_msg_error("opening frame %s",iim_pfx);
00217 return flames_midas_error(MAREMMA);
00218 }
00219
00220
00221
00222 sprintf(key_name,"%s","CORVEL_MAX");
00223 if((status=SCDRDD(iim_pfx_id,key_name,1,1,&actvals,&corvel_max,&unit,&null))
00224 != 0) {
00225 uves_msg_error("Reading descriptor %s from frame %s",
00226 key_name,out_tot_ima);
00227 return flames_midas_error(MAREMMA);
00228 }
00229
00230
00231
00232 sprintf(key_name,"%s%d","CCF_PMX",ord);
00233 uves_msg_debug("corvel_max=%g",corvel_max);
00234 if(!irplib_isnan(corvel_max)) {
00235 ccfcnt[ord]=corvel_max;
00236 check_nomsg(cpl_propertylist_append_double(plist_ima_new,key_name,
00237 corvel_max));
00238
00239
00240
00241 ck0_nomsg(uves_qclog_add_double(qclog,
00242 cpl_sprintf("%s%d%s",
00243 "QC CCF",ord," POSMAX"),
00244 corvel_max,
00245 "CCF pos Max",
00246 "%f"));
00247
00248
00249 } else {
00250 ccfcnt[ord]=999;
00251 check_nomsg(cpl_propertylist_append_double(plist_ima_new,key_name,
00252 999));
00253
00254
00255
00256
00257 ck0_nomsg(uves_qclog_add_double(qclog,
00258 cpl_sprintf("%s%d%s",
00259 "QC CCF",ord," POSMAX"),
00260 999,
00261 "CCF pos Max",
00262 "%f"));
00263
00264
00265 }
00266
00267
00268 sprintf(key_name,"%s","CCF_MAX");
00269 if((status=SCDRDD(in_ima_id,key_name,1,1,&actvals,&ccf_max,&unit,&null))
00270 != 0) {
00271 uves_msg_error("Reading descriptor %s from frame %s",
00272 key_name,out_tot_ima);
00273 return flames_midas_error(MAREMMA);
00274 }
00275
00276
00277 sprintf(key_name,"%s%d","CCF_MAX",ord);
00278 uves_msg_debug("ccf_max=%g",ccf_max);
00279 check_nomsg(cpl_propertylist_append_double(plist_ima_new,key_name,ccf_max));
00280
00281
00282 ck0_nomsg(uves_qclog_add_double(qclog,
00283 cpl_sprintf("%s%d%s",
00284 "QC CCF",ord," INTMAX"),
00285 ccf_max,
00286 "CCF Int Max",
00287 "%f"));
00288
00289
00290
00291 sprintf(key_name,"%s","WAV_RNG");
00292 if((status=SCDRDD(in_ima_id,key_name,1,1,&actvals,&wav_rng,&unit,&null))
00293 != 0) {
00294 uves_msg_error("Reading descriptor %s from frame %s",
00295 key_name,out_tot_ima);
00296 return flames_midas_error(MAREMMA);
00297 }
00298
00299 sprintf(key_name,"%s%d","WAV_RNG",ord);
00300 uves_msg_debug("wav_rng=%g",wav_rng);
00301 uves_msg_debug("key_name=%s",key_name);
00302 check_nomsg(cpl_propertylist_append_double(plist_ima_new,key_name,wav_rng));
00303
00304
00305 ck0_nomsg(uves_qclog_add_double(qclog,
00306 cpl_sprintf("%s%d%s",
00307 "QC CCF",ord," WAVRNG"),
00308 wav_rng,
00309 "CCF Range",
00310 "%f"));
00311
00312
00313
00314 sprintf(key_name,"%s","PIX_TOT");
00315 if((status=SCDRDD(in_ima_id,key_name,1,1,&actvals,&pix_tot,&unit,&null))
00316 != 0) {
00317 uves_msg_error("Reading descriptor %s from frame %s",
00318 key_name,out_tot_ima);
00319 return flames_midas_error(MAREMMA);
00320 }
00321
00322
00323 uves_msg_debug("pix_tot=%g",pix_tot);
00324 if(!irplib_isinf(pix_tot)) {
00325
00326 sprintf(key_name,"%s%d","PIX_TOT",ord);
00327 check_nomsg(cpl_propertylist_append_double(plist_ima_new,key_name,pix_tot));
00328
00329 ck0_nomsg(uves_qclog_add_double(qclog,
00330 cpl_sprintf("%s%d%s",
00331 "QC CCF",ord," PIXTOT"),
00332 pix_tot,
00333 "CCF Pix",
00334 "%f"));
00335
00336 } else {
00337
00338 sprintf(key_name,"%s%d","PIX_TOT",ord);
00339 check_nomsg(cpl_propertylist_append_double(plist_ima_new,key_name,999.));
00340
00341 ck0_nomsg(uves_qclog_add_double(qclog,
00342 cpl_sprintf("%s%d%s",
00343 "QC CCF",ord," PIXTOT"),
00344 999,
00345 "CCF Pix",
00346 "%f"));
00347
00348
00349 }
00350
00351 sprintf(key_name,"%s","LIN_TOT");
00352 if((status=SCDRDI(in_ima_id,key_name,1,1,&actvals,&lin_tot,&unit,&null))
00353 != 0) {
00354 uves_msg_error("Reading descriptor %s from frame %s",
00355 key_name,out_tot_ima);
00356 return flames_midas_error(MAREMMA);
00357 }
00358
00359
00360 sprintf(key_name,"%s%d","LIN_TOT",ord);
00361
00362 check_nomsg(cpl_propertylist_append_int(plist_ima_new,key_name,lin_tot));
00363 uves_msg_debug("lin_tot=%d",lin_tot);
00364
00365
00366 ck0_nomsg(uves_qclog_add_int(qclog,
00367 cpl_sprintf("%s%d%s",
00368 "QC CCF",ord," LINTOT"),
00369 pix_tot,
00370 "CCF Lin",
00371 "%d"));
00372
00373
00374 if((status = SCFCLO(in_ima_id))!=0) {
00375 uves_msg_error("Closing frame %d",in_ima_id);
00376 return flames_midas_error(MAREMMA);
00377 }
00378 if((status = SCFCLO(iim_pfx_id))!=0) {
00379 uves_msg_error("Closing frame %d",iim_pfx_id);
00380 return flames_midas_error(MAREMMA);
00381 }
00382
00383
00384 uves_free_table(&cvel_ord_tbl);
00385 uves_free_image(&tot_ima);
00386 uves_free_image(&nrm_ima);
00387
00388 command=uves_sprintf("%s%s","rm -rf ",out_cvel_tab);
00389 system(command);
00390 command=uves_sprintf("%s%s","rm -rf ",out_tot_ima);
00391 system(command);
00392 command=uves_sprintf("%s%s","rm -rf ",out_nrm_ima);
00393 system(command);
00394
00395
00396 }
00397
00398
00399 check_nomsg(cpl_image_save(tim_ccf_ima, "tot_ima.fits",CPL_BPP_IEEE_FLOAT,
00400 plist_ima_new,CPL_IO_DEFAULT));
00401 check_nomsg(cpl_image_save(nim_ccf_ima, "nrm_ima.fits",CPL_BPP_IEEE_FLOAT,
00402 plist_ima_new,CPL_IO_DEFAULT));
00403
00404 check_nomsg(cpl_table_save(cvel_tbl, plist_tbl_new, NULL, otab,
00405 CPL_IO_DEFAULT));
00406
00407 uves_free_image(&tim_ccf_ima);
00408 uves_free_image(&nim_ccf_ima);
00409 uves_free_table(&cvel_tbl);
00410
00411 ord = ord_max-ord_min+1;
00412
00413
00414
00415
00416 num_ord = 0;
00417 *avg_cnt=0;
00418 for (ord=ord_min; ord<= ord_max; ord++) {
00419 if (ccfcnt[ord] != 999) {
00420 *avg_cnt +=ccfcnt[ord];
00421 uves_msg_debug("POSMAX avg=%g cnt=%g ord=%d",*avg_cnt,ccfcnt[ord],ord);
00422 num_ord += 1;
00423 }
00424 }
00425
00426 *avg_cnt /= num_ord;
00427 *sig_cnt=0;
00428 for (ord = ord_min; ord <= ord_max; ord++) {
00429 if (ccfcnt[ord] != 999) {
00430 *sig_cnt += (ccfcnt[ord]-*avg_cnt)*(ccfcnt[ord]-*avg_cnt);
00431 uves_msg_debug("sig=%g cnt=%g ord=%d",*sig_cnt,ccfcnt[ord],ord);
00432 }
00433 }
00434 *sig_cnt = sqrt(*sig_cnt/(num_ord-1));
00435
00436 *zero_point+=(*avg_cnt);
00437
00438
00439
00440
00441
00442
00443 ck0_nomsg(uves_qclog_add_double(qclog,
00444 "QC CCF POSAVG",
00445 *avg_cnt,
00446 "CCF pos avg",
00447 "%f"));
00448
00449
00450 ck0_nomsg(uves_qclog_add_double(qclog,
00451 "QC CCF POSRMS",
00452 *sig_cnt,
00453 "CCF pos rms",
00454 "%f"));
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467 uves_msg_debug("POSAVG=%f POSRMS=%f",*avg_cnt,*sig_cnt);
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555 cleanup:
00556
00557 if(plist_ima_new!=NULL) {
00558 uves_msg_warning("free");
00559 cpl_propertylist_delete(plist_ima_new);
00560 plist_ima_new=NULL;
00561 }
00562 uves_free_table(&cvel_tbl);
00563 uves_free_image(&tim_ccf_ima);
00564 uves_free_image(&nim_ccf_ima);
00565 uves_free_image(&tot_ima);
00566 uves_free_image(&nrm_ima);
00567 uves_free_table(&cvel_ord_tbl);
00568
00569
00570
00571 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00572 uves_check_rec_status(9);
00573
00574 return -1;
00575 } else {
00576 return 0;
00577 }
00578
00579 }
00580