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 #ifdef HAVE_CONFIG_H
00027 #include <config.h>
00028 #endif
00029
00030
00037
00040
00041
00042
00043
00044 #include <math.h>
00045 #include <xsh_drl.h>
00046
00047 #include <xsh_badpixelmap.h>
00048 #include <xsh_data_pre.h>
00049 #include <xsh_dfs.h>
00050 #include <xsh_pfits.h>
00051 #include <xsh_error.h>
00052 #include <xsh_msg.h>
00053 #include <xsh_data_instrument.h>
00054 #include <xsh_data_spectralformat.h>
00055 #include <xsh_utils_image.h>
00056 #include <cpl.h>
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 static cpl_error_code
00068 xsh_add_qc_tab(cpl_frame* d2_frame, cpl_frame* qth_frame,
00069 cpl_frame* merged_frame) {
00070
00071 cpl_table* qc_d2_tab = NULL;
00072 cpl_table* qc_qth_tab = NULL;
00073 cpl_table* qc_tot_tab=NULL;
00074 cpl_propertylist* qc_qth_head = NULL;
00075
00076 int nrow_d2=0;
00077 int nrow_qth=0;
00078 int nrow_tot=0;
00079 int i=0;
00080 int k=0;
00081 int* pord=NULL;
00082 int* pabs_ord=NULL;
00083
00084 double* pcenterx=NULL;
00085 double* pcentery=NULL;
00086 double* pedgelox=NULL;
00087 double* pedgeupx=NULL;
00088 double* pedgelofx=NULL;
00089 double* pedgeupfx=NULL;
00090 double* pedgelo_resx=NULL;
00091 double* pedgeup_resx=NULL;
00092
00093
00094 int* pord_org=NULL;
00095 int* pabs_ord_org=NULL;
00096 double* pcenterx_org=NULL;
00097 double* pcentery_org=NULL;
00098 double* pedgelox_org=NULL;
00099 double* pedgeupx_org=NULL;
00100 double* pedgelofx_org=NULL;
00101 double* pedgeupfx_org=NULL;
00102 double* pedgelo_resx_org=NULL;
00103 double* pedgeup_resx_org=NULL;
00104
00105 qc_d2_tab = cpl_table_load(cpl_frame_get_filename(d2_frame), 3,0);
00106 qc_qth_tab = cpl_table_load(cpl_frame_get_filename(qth_frame), 3,0);
00107 qc_qth_head = cpl_propertylist_load(cpl_frame_get_filename(qth_frame), 3);
00108
00109 nrow_d2=cpl_table_get_nrow(qc_d2_tab);
00110 nrow_qth=cpl_table_get_nrow(qc_qth_tab);
00111
00112 nrow_tot=nrow_d2+nrow_qth;
00113 qc_tot_tab=cpl_table_new(nrow_tot);
00114
00115
00116 cpl_table_copy_structure(qc_tot_tab,qc_qth_tab);
00117 cpl_table_fill_column_window_int(qc_tot_tab,"ORDER",0,nrow_tot,0);
00118 cpl_table_fill_column_window_int(qc_tot_tab,"ABSORDER",0,nrow_tot,0);
00119 cpl_table_fill_column_window_double(qc_tot_tab,"CENTERX",0,nrow_tot,0);
00120 cpl_table_fill_column_window_double(qc_tot_tab,"CENTERY",0,nrow_tot,0);
00121 cpl_table_fill_column_window_double(qc_tot_tab,"EDGELOX",0,nrow_tot,0);
00122 cpl_table_fill_column_window_double(qc_tot_tab,"EDGEUPX",0,nrow_tot,0);
00123 cpl_table_fill_column_window_double(qc_tot_tab,"EDGELOFX",0,nrow_tot,0);
00124 cpl_table_fill_column_window_double(qc_tot_tab,"EDGEUPFX",0,nrow_tot,0);
00125 cpl_table_fill_column_window_double(qc_tot_tab,"EDGELO_RESX",0,nrow_tot,0);
00126 cpl_table_fill_column_window_double(qc_tot_tab,"EDGEUP_RESX",0,nrow_tot,0);
00127
00128
00129 pord=cpl_table_get_data_int(qc_tot_tab,"ORDER");
00130 pabs_ord=cpl_table_get_data_int(qc_tot_tab,"ABSORDER");
00131 pcenterx=cpl_table_get_data_double(qc_tot_tab,"CENTERX");
00132 pcentery=cpl_table_get_data_double(qc_tot_tab,"CENTERY");
00133 pedgelox=cpl_table_get_data_double(qc_tot_tab,"EDGELOX");
00134 pedgeupx=cpl_table_get_data_double(qc_tot_tab,"EDGEUPX");
00135 pedgelofx=cpl_table_get_data_double(qc_tot_tab,"EDGELOFX");
00136 pedgeupfx=cpl_table_get_data_double(qc_tot_tab,"EDGEUPFX");
00137 pedgelo_resx=cpl_table_get_data_double(qc_tot_tab,"EDGELO_RESX");
00138 pedgeup_resx=cpl_table_get_data_double(qc_tot_tab,"EDGEUP_RESX");
00139
00140 pord_org=cpl_table_get_data_int(qc_qth_tab,"ORDER");
00141 pabs_ord_org=cpl_table_get_data_int(qc_qth_tab,"ABSORDER");
00142 pcenterx_org=cpl_table_get_data_double(qc_qth_tab,"CENTERX");
00143 pcentery_org=cpl_table_get_data_double(qc_qth_tab,"CENTERY");
00144 pedgelox_org=cpl_table_get_data_double(qc_qth_tab,"EDGELOX");
00145 pedgeupx_org=cpl_table_get_data_double(qc_qth_tab,"EDGEUPX");
00146 pedgelofx_org=cpl_table_get_data_double(qc_qth_tab,"EDGELOFX");
00147 pedgeupfx_org=cpl_table_get_data_double(qc_qth_tab,"EDGEUPFX");
00148 pedgelo_resx_org=cpl_table_get_data_double(qc_qth_tab,"EDGELO_RESX");
00149 pedgeup_resx_org=cpl_table_get_data_double(qc_qth_tab,"EDGEUP_RESX");
00150
00151
00152 for (i = 0; i < nrow_qth; i++) {
00153
00154 pord[i] = pord_org[i];
00155 pabs_ord[i] = pabs_ord_org[i];
00156 pcenterx[i] = pcenterx_org[i];
00157 pcentery[i] = pcentery_org[i];
00158 pedgelox[i] = pedgelox_org[i];
00159 pedgeupx[i] = pedgeupx_org[i];
00160 pedgelofx[i] = pedgelofx_org[i];
00161 pedgeupfx[i] = pedgeupfx_org[i];
00162 pedgelo_resx[i] = pedgelo_resx_org[i];
00163 pedgeup_resx[i] = pedgeup_resx_org[i];
00164
00165 }
00166
00167
00168 pord_org = cpl_table_get_data_int(qc_d2_tab, "ORDER");
00169 pabs_ord_org = cpl_table_get_data_int(qc_d2_tab, "ABSORDER");
00170 pcenterx_org = cpl_table_get_data_double(qc_d2_tab, "CENTERX");
00171 pcentery_org = cpl_table_get_data_double(qc_d2_tab, "CENTERY");
00172 pedgelox_org = cpl_table_get_data_double(qc_d2_tab, "EDGELOX");
00173 pedgeupx_org = cpl_table_get_data_double(qc_d2_tab, "EDGEUPX");
00174 pedgelofx_org = cpl_table_get_data_double(qc_d2_tab, "EDGELOFX");
00175 pedgeupfx_org = cpl_table_get_data_double(qc_d2_tab, "EDGEUPFX");
00176 pedgelo_resx_org = cpl_table_get_data_double(qc_d2_tab, "EDGELO_RESX");
00177 pedgeup_resx_org = cpl_table_get_data_double(qc_d2_tab, "EDGEUP_RESX");
00178
00179 for (i = 0; i < nrow_d2; i++) {
00180 k=i+nrow_qth;
00181 pord[k] = pord_org[i];
00182 pabs_ord[k] = pabs_ord_org[i];
00183 pcenterx[k] = pcenterx_org[i];
00184 pcentery[k] = pcentery_org[i];
00185 pedgelox[k] = pedgelox_org[i];
00186 pedgeupx[k] = pedgeupx_org[i];
00187 pedgelofx[k] = pedgelofx_org[i];
00188 pedgeupfx[k] = pedgeupfx_org[i];
00189 pedgelo_resx[k] = pedgelo_resx_org[i];
00190 pedgeup_resx[k] = pedgeup_resx_org[i];
00191 }
00192
00193 cpl_table_save(qc_tot_tab, qc_qth_head, NULL, cpl_frame_get_filename(merged_frame), CPL_IO_EXTEND);
00194
00195
00196 xsh_free_table(&qc_qth_tab);
00197 xsh_free_table(&qc_d2_tab);
00198 xsh_free_table(&qc_tot_tab);
00199 xsh_free_propertylist(&qc_qth_head);
00200 return cpl_error_get_code();
00201 }
00202
00218
00219 void
00220 xsh_flat_merge_qth_d2( cpl_frame *qth_frame, cpl_frame *qth_order_tab_frame,
00221 cpl_frame *d2_frame,cpl_frame *d2_order_tab_frame,
00222 cpl_frame *qth_bkg_frame, cpl_frame *d2_bkg_frame,
00223 cpl_frame **qth_d2_flat_frame,
00224 cpl_frame **qth_d2_bkg_frame,
00225 cpl_frame **qth_d2_order_tab_frame,
00226 xsh_instrument *instrument)
00227
00228 {
00229 xsh_pre *qth_pre = NULL;
00230 xsh_pre *d2_pre = NULL;
00231
00232 float *qth_data = NULL;
00233 float *d2_data = NULL;
00234 float *qth_errs = NULL;
00235 float *d2_errs = NULL;
00236 int *qth_qual = NULL;
00237 int *d2_qual = NULL;
00238
00239 float *d2_bkg_data = NULL;
00240
00241 float *qth_bkg_data = NULL;
00242
00243 xsh_order_list *qth_list = NULL;
00244 xsh_order_list *d2_list = NULL;
00245 xsh_order_list *qth_d2_list = NULL;
00246 int y;
00247 cpl_polynomial *d2_limit = NULL;
00248 cpl_polynomial *qth_limit = NULL;
00249 cpl_image* d2_bkg_ima=NULL;
00250 cpl_image* qth_bkg_ima=NULL;
00251
00252 const char* tag=NULL;
00253 const char* fname=NULL;
00254 char* name=NULL;
00255 char file_name[256];
00256 char file_tag[25];
00257
00258 double qth_flux_min=0;
00259 double qth_flux_max=0;
00260 double d2_flux_min=0;
00261 double d2_flux_max=0;
00262 double flux_min=0;
00263 double flux_max=0;
00264
00265
00266 XSH_ASSURE_NOT_NULL( qth_frame);
00267 XSH_ASSURE_NOT_NULL( qth_order_tab_frame);
00268 XSH_ASSURE_NOT_NULL( d2_frame);
00269 XSH_ASSURE_NOT_NULL( d2_order_tab_frame);
00270 XSH_ASSURE_NOT_NULL( qth_d2_flat_frame);
00271 XSH_ASSURE_NOT_NULL( qth_bkg_frame);
00272 XSH_ASSURE_NOT_NULL( d2_bkg_frame);
00273 XSH_ASSURE_NOT_NULL( qth_d2_bkg_frame);
00274 XSH_ASSURE_NOT_NULL( qth_d2_order_tab_frame);
00275 XSH_ASSURE_NOT_NULL( instrument);
00276
00277 xsh_msg_dbg_medium( "Entering xsh_flat_merge_qth_d2") ;
00278
00279
00280 check( qth_pre = xsh_pre_load( qth_frame, instrument));
00281 check( d2_pre = xsh_pre_load( d2_frame, instrument));
00282
00283
00284 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_QTH));
00285 check( qth_list = xsh_order_list_load( qth_order_tab_frame, instrument));
00286 xsh_order_list_set_bin_x( qth_list, qth_pre->binx);
00287 xsh_order_list_set_bin_y( qth_list, qth_pre->biny);
00288 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_D2));
00289 check( d2_list = xsh_order_list_load( d2_order_tab_frame, instrument));
00290 xsh_order_list_set_bin_x( d2_list, d2_pre->binx);
00291 xsh_order_list_set_bin_y( d2_list, d2_pre->biny);
00292
00293 check( qth_data = cpl_image_get_data_float( qth_pre->data));
00294 check( d2_data = cpl_image_get_data_float(d2_pre->data));
00295
00296 fname=cpl_frame_get_filename(qth_bkg_frame);
00297 qth_bkg_ima=cpl_image_load(fname,CPL_TYPE_FLOAT,0,0);
00298 check( qth_bkg_data = cpl_image_get_data_float(qth_bkg_ima));
00299
00300 fname=cpl_frame_get_filename(d2_bkg_frame);
00301 d2_bkg_ima=cpl_image_load(fname,CPL_TYPE_FLOAT,0,0);
00302 check( d2_bkg_data = cpl_image_get_data_float(d2_bkg_ima));
00303
00304
00305 check( qth_errs = cpl_image_get_data_float( qth_pre->errs));
00306 check( d2_errs = cpl_image_get_data_float(d2_pre->errs));
00307
00308 check( qth_qual = cpl_image_get_data_int( qth_pre->qual));
00309 check( d2_qual = cpl_image_get_data_int(d2_pre->qual));
00310
00311
00312
00313
00314 qth_limit = qth_list->list[qth_list->size-1].edglopoly;
00315 d2_limit = d2_list->list[0].edguppoly;
00316 int xd2=0, xqth=0;
00317 int x_avg=0;
00318 int x;
00319 int offset=0;
00320 int pixel=0;
00321 for( y=0; y < d2_pre->ny; y++){
00322
00323 check( xd2 = xsh_order_list_eval_int( d2_list, d2_limit, y+1)-1);
00324 check( xqth = xsh_order_list_eval_int( qth_list, qth_limit, y+1)-1);
00325 x_avg = floor( 0.5*(xd2+xqth));
00326 xsh_msg_dbg_medium("D2 x %d y %d", xd2, y);
00327 xsh_msg_dbg_medium("QTH x %d y %d", xqth, y);
00328 xsh_msg_dbg_medium(" x_avg = %d", x_avg);
00329
00330 offset=y*d2_pre->nx;
00331
00332 for( x=x_avg; x< d2_pre->nx; x++){
00333 pixel=x+offset;
00334 d2_data[pixel] = qth_data[pixel];
00335 d2_errs[pixel] = qth_errs[pixel];
00336 d2_qual[pixel] = qth_qual[pixel];
00337
00338 d2_bkg_data[pixel] = qth_bkg_data[pixel];
00339
00340
00341 }
00342 }
00343
00344
00345 check( qth_d2_list = xsh_order_list_merge( qth_list, d2_list));
00346
00347 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_UNDEFINED));
00348
00349 tag=XSH_GET_TAG_FROM_LAMP( XSH_MASTER_FLAT,instrument);
00350 XSH_NAME_LAMP_MODE_ARM( name, "MASTER_FLAT", ".fits", instrument);
00351
00352
00353
00354 check(qth_flux_min=cpl_propertylist_get_double(qth_pre->data_header,
00355 XSH_QC_FLUX_MIN));
00356 check(qth_flux_max=cpl_propertylist_get_double(qth_pre->data_header,
00357 XSH_QC_FLUX_MAX));
00358 check(d2_flux_min=cpl_propertylist_get_double(d2_pre->data_header,
00359 XSH_QC_FLUX_MIN));
00360 check(d2_flux_max=cpl_propertylist_get_double(d2_pre->data_header,
00361 XSH_QC_FLUX_MAX));
00362
00363 flux_min=(qth_flux_min<d2_flux_min)? qth_flux_min:d2_flux_min;
00364 flux_max=(qth_flux_max>d2_flux_max)? qth_flux_max:d2_flux_max;
00365
00366 check(cpl_propertylist_set_double(d2_pre->data_header,XSH_QC_FLUX_MIN,
00367 flux_min));
00368 check(cpl_propertylist_set_double(d2_pre->data_header,XSH_QC_FLUX_MAX,
00369 flux_max));
00370
00371
00372
00373 check( *qth_d2_flat_frame = xsh_pre_save( d2_pre, name, tag,0));
00374 check( cpl_frame_set_tag( *qth_d2_flat_frame, tag));
00375 XSH_REGDEBUG("save %s %s", fname, tag);
00376
00377 sprintf(file_tag,"MFLAT_BACK_%s_%s",
00378 xsh_instrument_mode_tostring(instrument),
00379 xsh_instrument_arm_tostring(instrument));
00380
00381 sprintf(file_name,"%s.fits",file_tag);
00382
00383 check(xsh_pfits_set_pcatg(d2_pre->data_header,file_tag));
00384
00385 check(cpl_image_save(d2_bkg_ima,file_name,CPL_BPP_IEEE_FLOAT,
00386 d2_pre->data_header,CPL_IO_DEFAULT));
00387
00388
00389 check(*qth_d2_bkg_frame=xsh_frame_product(file_name,file_tag,
00390 CPL_FRAME_TYPE_IMAGE,
00391 CPL_FRAME_GROUP_CALIB,
00392 CPL_FRAME_LEVEL_FINAL));
00393
00394
00395 tag= XSH_GET_TAG_FROM_LAMP( XSH_ORDER_TAB_EDGES, instrument);
00396 XSH_NAME_LAMP_MODE_ARM( name, "ORDER_TAB_EDGES", ".fits", instrument);
00397
00398 check( *qth_d2_order_tab_frame = xsh_order_list_save( qth_d2_list,instrument,
00399 name, tag,
00400 qth_pre->ny*d2_list->bin_y));
00401
00402 check(xsh_add_qc_tab(d2_order_tab_frame,qth_order_tab_frame,*qth_d2_order_tab_frame));
00403
00404 XSH_REGDEBUG("save %s %s",name, tag);
00405
00406
00407 cleanup:
00408 XSH_FREE( name);
00409 xsh_free_image(&d2_bkg_ima);
00410 xsh_free_image(&qth_bkg_ima);
00411
00412 xsh_pre_free( &qth_pre);
00413 xsh_pre_free( &d2_pre);
00414 xsh_order_list_free( &qth_list);
00415 xsh_order_list_free( &d2_list);
00416 xsh_order_list_free( &qth_d2_list);
00417 return;
00418
00419 }
00420
00421
00429
00430
00431 cpl_frame*
00432 xsh_flat_merge_qth_d2_tabs(cpl_frame *qth_edges_tab,cpl_frame *d2_edges_tab,xsh_instrument *instrument)
00433
00434 {
00435 cpl_frame* full_edges_tab=NULL;
00436
00437 xsh_order_list *qth_list = NULL;
00438 xsh_order_list *d2_list = NULL;
00439 xsh_order_list *qth_d2_list = NULL;
00440 char* name=NULL;
00441 const char* tag=NULL;
00442
00443 xsh_msg("binx=%d biny=%d",instrument->binx,instrument->biny);
00444 xsh_instrument_update_lamp( instrument, XSH_LAMP_QTH);
00445 qth_list = xsh_order_list_load( qth_edges_tab, instrument);
00446 xsh_order_list_set_bin_x( qth_list, instrument->binx);
00447 xsh_order_list_set_bin_y( qth_list, instrument->biny);
00448 xsh_instrument_update_lamp( instrument, XSH_LAMP_D2);
00449 d2_list = xsh_order_list_load( d2_edges_tab, instrument);
00450 xsh_order_list_set_bin_x( d2_list, instrument->binx);
00451 xsh_order_list_set_bin_y( d2_list, instrument->biny);
00452 qth_d2_list = xsh_order_list_merge( qth_list, d2_list);
00453
00454 xsh_instrument_update_lamp( instrument, XSH_LAMP_UNDEFINED);
00455 tag= XSH_GET_TAG_FROM_LAMP( XSH_ORDER_TAB_EDGES, instrument);
00456 XSH_NAME_LAMP_MODE_ARM( name, "ORDER_TAB_EDGES", ".fits", instrument);
00457 full_edges_tab = xsh_order_list_save( qth_d2_list,instrument,
00458 name, tag,instrument->config->ny*instrument->biny);
00459
00460 cleanup:
00461
00462 XSH_FREE( name);
00463 xsh_order_list_free( &qth_list);
00464 xsh_order_list_free( &d2_list);
00465 xsh_order_list_free( &qth_d2_list);
00466
00467 return full_edges_tab;
00468 }
00469
00470
00471
00472
00473
00489
00490
00491 void
00492 xsh_flat_merge_qth_d2_smooth( cpl_frame *qth_frame, cpl_frame *qth_order_tab_frame,
00493 cpl_frame *d2_frame,cpl_frame *d2_order_tab_frame,
00494 cpl_frame *qth_bkg_frame, cpl_frame *d2_bkg_frame,
00495 cpl_frame **qth_d2_flat_frame,
00496 cpl_frame **qth_d2_bkg_frame,
00497 cpl_frame **qth_d2_order_tab_frame,
00498 xsh_instrument *instrument)
00499
00500 {
00501 xsh_pre *qth_pre = NULL;
00502 xsh_pre *d2_pre = NULL;
00503
00504
00505
00506 float *qth_errs = NULL;
00507 float *d2_errs = NULL;
00508 int *qth_qual = NULL;
00509 int *d2_qual = NULL;
00510
00511 float *d2_bkg_data = NULL;
00512
00513 float *qth_bkg_data = NULL;
00514
00515
00516 xsh_order_list *qth_list = NULL;
00517 xsh_order_list *d2_list = NULL;
00518 xsh_order_list *qth_d2_list = NULL;
00519 int y;
00520 cpl_polynomial *d2_limit = NULL;
00521 cpl_polynomial *qth_limit = NULL;
00522 cpl_image* d2_bkg_ima=NULL;
00523 cpl_image* qth_bkg_ima=NULL;
00524
00525 const char* tag=NULL;
00526 const char* fname=NULL;
00527 char* name=NULL;
00528 char file_name[256];
00529 char file_tag[25];
00530
00531 double qth_flux_min=0;
00532 double qth_flux_max=0;
00533 double d2_flux_min=0;
00534 double d2_flux_max=0;
00535 double flux_min=0;
00536 double flux_max=0;
00537
00538 cpl_image* merged=NULL;
00539 XSH_ASSURE_NOT_NULL( qth_frame);
00540 XSH_ASSURE_NOT_NULL( qth_order_tab_frame);
00541 XSH_ASSURE_NOT_NULL( d2_frame);
00542 XSH_ASSURE_NOT_NULL( d2_order_tab_frame);
00543 XSH_ASSURE_NOT_NULL( qth_d2_flat_frame);
00544 XSH_ASSURE_NOT_NULL( qth_bkg_frame);
00545 XSH_ASSURE_NOT_NULL( d2_bkg_frame);
00546 XSH_ASSURE_NOT_NULL( qth_d2_bkg_frame);
00547 XSH_ASSURE_NOT_NULL( qth_d2_order_tab_frame);
00548 XSH_ASSURE_NOT_NULL( instrument);
00549
00550 xsh_msg_dbg_medium( "Entering xsh_flat_merge_qth_d2") ;
00551
00552
00553 check( qth_pre = xsh_pre_load( qth_frame, instrument));
00554 check( d2_pre = xsh_pre_load( d2_frame, instrument));
00555
00556
00557 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_QTH));
00558 check( qth_list = xsh_order_list_load( qth_order_tab_frame, instrument));
00559 xsh_order_list_set_bin_x( qth_list, qth_pre->binx);
00560 xsh_order_list_set_bin_y( qth_list, qth_pre->biny);
00561 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_D2));
00562 check( d2_list = xsh_order_list_load( d2_order_tab_frame, instrument));
00563 xsh_order_list_set_bin_x( d2_list, d2_pre->binx);
00564 xsh_order_list_set_bin_y( d2_list, d2_pre->biny);
00565
00566
00567
00568
00569 fname=cpl_frame_get_filename(qth_bkg_frame);
00570 qth_bkg_ima=cpl_image_load(fname,CPL_TYPE_FLOAT,0,0);
00571 check( qth_bkg_data = cpl_image_get_data_float(qth_bkg_ima));
00572
00573 fname=cpl_frame_get_filename(d2_bkg_frame);
00574 d2_bkg_ima=cpl_image_load(fname,CPL_TYPE_FLOAT,0,0);
00575 check( d2_bkg_data = cpl_image_get_data_float(d2_bkg_ima));
00576
00577
00578 check( qth_errs = cpl_image_get_data_float( qth_pre->errs));
00579 check( d2_errs = cpl_image_get_data_float(d2_pre->errs));
00580
00581 check( qth_qual = cpl_image_get_data_int( qth_pre->qual));
00582 check( d2_qual = cpl_image_get_data_int(d2_pre->qual));
00583
00584
00585
00586
00587 qth_limit = qth_list->list[qth_list->size-1].edglopoly;
00588 d2_limit = d2_list->list[0].edguppoly;
00589
00590 for( y=0; y < d2_pre->ny; y++){
00591 int xd2=0, xqth=0;
00592 int x_avg=0;
00593 int x;
00594
00595 check( xd2 = xsh_order_list_eval_int( d2_list, d2_limit, y+1)-1);
00596 check( xqth = xsh_order_list_eval_int( qth_list, qth_limit, y+1)-1);
00597 x_avg = floor( (xd2+xqth)/2.0);
00598 xsh_msg_dbg_medium("D2 x %d y %d", xd2, y);
00599 xsh_msg_dbg_medium("QTH x %d y %d", xqth, y);
00600 xsh_msg_dbg_medium(" x_avg = %d", x_avg);
00601
00602 for( x=x_avg; x< d2_pre->nx; x++){
00603
00604 d2_errs[x+y*d2_pre->nx] = qth_errs[x+y*d2_pre->nx];
00605 d2_qual[x+y*d2_pre->nx] = qth_qual[x+y*d2_pre->nx];
00606
00607 d2_bkg_data[x+y*d2_pre->nx] = qth_bkg_data[x+y*d2_pre->nx];
00608
00609
00610 }
00611 }
00612
00613
00614 check( qth_d2_list = xsh_order_list_merge( qth_list, d2_list));
00615
00616
00617 merged=xsh_combine_flats(qth_pre->data,d2_pre->data,qth_list,d2_list,5,5);
00618 d2_pre->data=cpl_image_duplicate(merged);
00619
00620 d2_pre->data=cpl_image_duplicate(merged);
00621 xsh_free_image(&merged);
00622
00623
00624
00625
00626
00627
00628
00629 merged=xsh_combine_flats(qth_bkg_ima,d2_bkg_ima,qth_list,d2_list,5,5);
00630 d2_bkg_ima=cpl_image_duplicate(merged);
00631
00632 d2_bkg_ima=cpl_image_duplicate(merged);
00633 xsh_free_image(&merged);
00634
00635 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_UNDEFINED));
00636
00637 tag=XSH_GET_TAG_FROM_LAMP( XSH_MASTER_FLAT,instrument);
00638 XSH_NAME_LAMP_MODE_ARM( name, "MASTER_FLAT", ".fits", instrument);
00639
00640
00641
00642 check(qth_flux_min=cpl_propertylist_get_double(qth_pre->data_header,
00643 XSH_QC_FLUX_MIN));
00644 check(qth_flux_max=cpl_propertylist_get_double(qth_pre->data_header,
00645 XSH_QC_FLUX_MAX));
00646 check(d2_flux_min=cpl_propertylist_get_double(d2_pre->data_header,
00647 XSH_QC_FLUX_MIN));
00648 check(d2_flux_max=cpl_propertylist_get_double(d2_pre->data_header,
00649 XSH_QC_FLUX_MAX));
00650
00651 flux_min=(qth_flux_min<d2_flux_min)? qth_flux_min:d2_flux_min;
00652 flux_max=(qth_flux_max>d2_flux_max)? qth_flux_max:d2_flux_max;
00653
00654 check(cpl_propertylist_set_double(d2_pre->data_header,XSH_QC_FLUX_MIN,
00655 flux_min));
00656 check(cpl_propertylist_set_double(d2_pre->data_header,XSH_QC_FLUX_MAX,
00657 flux_max));
00658
00659
00660
00661 check( *qth_d2_flat_frame = xsh_pre_save( d2_pre, name, tag,0));
00662 check( cpl_frame_set_tag( *qth_d2_flat_frame, tag));
00663 XSH_REGDEBUG("save %s %s", fname, tag);
00664
00665 sprintf(file_tag,"MFLAT_BACK_%s_%s",
00666 xsh_instrument_mode_tostring(instrument),
00667 xsh_instrument_arm_tostring(instrument));
00668
00669 sprintf(file_name,"%s.fits",file_tag);
00670
00671 check(xsh_pfits_set_pcatg(d2_pre->data_header,file_tag));
00672
00673 check(cpl_image_save(d2_bkg_ima,file_name,CPL_BPP_IEEE_FLOAT,
00674 d2_pre->data_header,CPL_IO_DEFAULT));
00675
00676
00677 check(*qth_d2_bkg_frame=xsh_frame_product(file_name,file_tag,
00678 CPL_FRAME_TYPE_IMAGE,
00679 CPL_FRAME_GROUP_CALIB,
00680 CPL_FRAME_LEVEL_FINAL));
00681
00682
00683 tag= XSH_GET_TAG_FROM_LAMP( XSH_ORDER_TAB_EDGES, instrument);
00684 XSH_NAME_LAMP_MODE_ARM( name, "ORDER_TAB_EDGES", ".fits", instrument);
00685
00686 check( *qth_d2_order_tab_frame = xsh_order_list_save( qth_d2_list,instrument,
00687 name, tag,
00688 qth_pre->ny*d2_list->bin_y));
00689
00690
00691 XSH_REGDEBUG("save %s %s",name, tag);
00692
00693
00694 cleanup:
00695 XSH_FREE( name);
00696 xsh_free_image(&d2_bkg_ima);
00697 xsh_free_image(&qth_bkg_ima);
00698
00699 xsh_pre_free( &qth_pre);
00700 xsh_pre_free( &d2_pre);
00701 xsh_order_list_free( &qth_list);
00702 xsh_order_list_free( &d2_list);
00703 xsh_order_list_free( &qth_d2_list);
00704 return;
00705
00706 }
00707
00708