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
00031
00032
00033 #include <xsh_utils_scired_slit.h>
00034 #include <xsh_dfs.h>
00035 #include <xsh_utils.h>
00036 #include <xsh_utils_table.h>
00037 #include <xsh_utils_efficiency.h>
00038 #include <xsh_data_spectrum.h>
00039 #include <xsh_model_utils.h>
00040 #include <xsh_error.h>
00041 #include <xsh_drl_check.h>
00042 #include <xsh_pfits.h>
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00105
00116
00117
00118 cpl_error_code
00119 xsh_stare_params_bin_scale(cpl_frameset* raws,
00120 xsh_background_param* backg,
00121 xsh_opt_extract_param *opt_extract_par,
00122 int* sub_sky_nbkpts1,
00123 int* sub_sky_nbkpts2)
00124 {
00125
00126 cpl_frame* frame=NULL;
00127 const char* name=NULL;
00128 cpl_propertylist *plist=NULL;
00129 int binx=0;
00130 int biny=0;
00131
00132 check(frame=cpl_frameset_get_first(raws));
00133 check(name=cpl_frame_get_filename(frame));
00134 check(plist=cpl_propertylist_load(name,0));
00135 check(binx=xsh_pfits_get_binx(plist));
00136 check(biny=xsh_pfits_get_biny(plist));
00137 xsh_free_propertylist( &plist);
00138
00139 if(biny>1) {
00140
00141
00142
00143
00144
00145
00146 backg->radius_y=backg->radius_y/biny;
00147
00148
00149 *sub_sky_nbkpts1*=0.75*biny;
00150 *sub_sky_nbkpts2*=0.75*biny;
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 }
00191
00192
00193 if(binx>1) {
00194
00195 backg->radius_x=backg->radius_x/binx;
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 opt_extract_par->box_hsize=opt_extract_par->box_hsize/binx;
00218
00219 }
00220
00221 cleanup:
00222 xsh_free_propertylist(&plist);
00223 return cpl_error_get_code();
00224
00225 }
00226
00227
00228 cpl_frameset* xsh_nod_group_by_reloff( cpl_frameset *ord_set,xsh_instrument *instrument, xsh_stack_param* stack_par)
00229 {
00230
00231 cpl_frameset *result = NULL;
00232 cpl_frameset *rmcrh_set = NULL;
00233 cpl_frame *frame = NULL;
00234 cpl_propertylist *header = NULL;
00235
00236 const char* nod_name = NULL;
00237
00238 double rel_ra=0;
00239 double rel_dec=0;
00240 double A_cum_ra=0;
00241 double A_cum_dec=0;
00242 int A_number=1, B_number =1;
00243 int nod_number;
00244 char ftag[40];
00245 char name[40];
00246
00247 cpl_frame *crhm_frame = NULL;
00248
00249 XSH_ASSURE_NOT_NULL( ord_set);
00250 XSH_ASSURE_NOT_NULL( instrument);
00251 XSH_ASSURE_NOT_NULL( stack_par);
00252
00253 check( result=cpl_frameset_new());
00254
00255 check( frame = cpl_frameset_get_first( ord_set));
00256 check( nod_name = cpl_frame_get_filename( frame));
00257 check( header = cpl_propertylist_load( nod_name, 0));
00258
00259 check( A_cum_ra = xsh_pfits_get_ra_cumoffset( header));
00260 check( A_cum_dec = xsh_pfits_get_dec_cumoffset( header));
00261
00262 xsh_free_propertylist( &header);
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 while( frame != NULL){
00273 double cum_ra=0;
00274 double cum_dec=0;
00275 double nrel_ra, nrel_dec;
00276 const char *type= NULL;
00277
00278 check( nod_name = cpl_frame_get_filename( frame));
00279 check( header = cpl_propertylist_load( nod_name, 0));
00280 check( cum_ra = xsh_pfits_get_ra_cumoffset( header));
00281 check( cum_dec = xsh_pfits_get_dec_cumoffset( header));
00282 check( rel_ra = xsh_pfits_get_ra_reloffset( header));
00283 check( rel_dec = xsh_pfits_get_dec_reloffset( header));
00284
00285 if ( cum_ra == A_cum_ra && cum_dec == A_cum_dec){
00286 type = "A";
00287 nod_number = A_number;
00288 }
00289 else{
00290 type = "B";
00291 nod_number = B_number;
00292 }
00293
00294 xsh_msg("name %s cumoffset %f %f reloffset %f %f nod_seq_number : %s%d", nod_name,
00295 cum_ra, cum_dec, rel_ra, rel_dec, type, nod_number);
00296
00297 xsh_free_propertylist( &header);
00298
00299 check( rmcrh_set = cpl_frameset_new());
00300 cpl_frameset_insert( rmcrh_set, cpl_frame_duplicate( frame));
00301
00302 check( frame = cpl_frameset_get_next( ord_set));
00303
00304 while( frame != NULL){
00305 check( nod_name = cpl_frame_get_filename( frame));
00306 check( header = cpl_propertylist_load( nod_name, 0));
00307 check( nrel_ra = xsh_pfits_get_ra_reloffset( header));
00308 check( nrel_dec = xsh_pfits_get_dec_reloffset( header));
00309 xsh_free_propertylist( &header);
00310
00311 if ( rel_ra == nrel_ra && nrel_dec == rel_dec){
00312 cpl_frameset_insert( rmcrh_set, cpl_frame_duplicate( frame));
00313 }
00314 else{
00315 break;
00316 }
00317 check( frame = cpl_frameset_get_next( ord_set));
00318 }
00319
00320
00321 sprintf( ftag,"med_%s%d", type, nod_number);
00322 sprintf(name,"%s.fits",ftag);
00323 check( crhm_frame = xsh_remove_crh_multiple( rmcrh_set, ftag, stack_par,NULL,instrument, NULL,NULL,1 ));
00324
00325 check( cpl_frameset_insert( result, crhm_frame));
00326 xsh_add_temporary_file(name);
00327
00328 if ( cum_ra == A_cum_ra && cum_dec == A_cum_dec){
00329 A_number++;
00330 }
00331 else{
00332 B_number++;
00333 }
00334 xsh_free_frameset( &rmcrh_set);
00335 }
00336
00337 cleanup:
00338 if ( cpl_error_get_code() != CPL_ERROR_NONE){
00339 xsh_free_propertylist( &header);
00340 xsh_free_frameset( &rmcrh_set);
00341 xsh_free_frameset( &result);
00342 }
00343 return result;
00344 }
00345
00346
00347
00348 cpl_frameset* xsh_nod_group_by_reloff2( cpl_frameset *ord_set,xsh_instrument *instrument, xsh_stack_param* stack_par)
00349 {
00350
00351 cpl_frameset *result = NULL;
00352 cpl_frameset *tmp_set = NULL;
00353 cpl_frame *frame = NULL;
00354 cpl_propertylist *header = NULL;
00355
00356 const char* nod_name = NULL;
00357
00358 double rel_ra=0;
00359 double rel_dec=0;
00360 double A_cum_ra=0;
00361 double A_cum_dec=0;
00362 int A_number=1, B_number =1;
00363 int nod_number;
00364 char ftag[40];
00365 char name[40];
00366 cpl_vector* vec_ra=NULL;
00367 cpl_vector* vec_dec=NULL;
00368 cpl_frame *crhm_frame = NULL;
00369 double* pvec_ra=NULL;
00370 double* pvec_dec=NULL;
00371 int nraw=0;
00372 int nnod=1;
00373 int i=0;
00374 int j=0;
00375 double frm_ra=0;
00376 double frm_dec=0;
00377 XSH_ASSURE_NOT_NULL( ord_set);
00378 XSH_ASSURE_NOT_NULL( instrument);
00379 XSH_ASSURE_NOT_NULL( stack_par);
00380 double tol_ra=0.1;
00381 double tol_dec=0.1;
00382 const char *type= NULL;
00383
00384 nraw=cpl_frameset_get_size(ord_set);
00385 cpl_frameset_dump(ord_set,stdout);
00386
00387 check( result=cpl_frameset_new());
00388
00389 check( frame = cpl_frameset_get_first( ord_set));
00390 check( nod_name = cpl_frame_get_filename( frame));
00391 check( header = cpl_propertylist_load( nod_name, 0));
00392
00393 check( A_cum_ra = xsh_pfits_get_ra_cumoffset( header));
00394 check( A_cum_dec = xsh_pfits_get_dec_cumoffset( header));
00395 check( frm_ra = xsh_pfits_get_ra_cumoffset( header));
00396 check( frm_dec = xsh_pfits_get_dec_cumoffset( header));
00397
00398 xsh_free_propertylist( &header);
00399 vec_ra=cpl_vector_new(nraw);
00400 vec_dec=cpl_vector_new(nraw);
00401 pvec_ra=cpl_vector_get_data(vec_ra);
00402 pvec_dec=cpl_vector_get_data(vec_dec);
00403
00404
00405
00406 pvec_ra[0]=frm_ra;
00407 pvec_dec[0]=frm_dec;
00408 for(i=1;i<nraw;i++) {
00409 check( frame = cpl_frameset_get_frame( ord_set,i));
00410 check( header = cpl_propertylist_load( nod_name, 0));
00411 check( frm_ra = xsh_pfits_get_ra_cumoffset( header));
00412 check( frm_dec = xsh_pfits_get_dec_cumoffset( header));
00413
00414 int newtag = 1;
00415 for(j=0;j<nnod;j++) {
00416 if( fabs(pvec_ra[j] - frm_ra) < tol_ra &&
00417 fabs(pvec_dec[j] - frm_dec) < tol_dec )
00418 {
00419 newtag = 0;
00420 }
00421 }
00422 if (newtag == 1) {
00423 pvec_ra[nnod]=frm_ra;
00424 pvec_dec[nnod]=frm_dec;
00425 nnod++;
00426 }
00427 xsh_free_propertylist( &header);
00428 }
00429 cpl_vector_set_size(vec_ra, nnod);
00430 cpl_vector_set_size(vec_dec, nnod);
00431 cpl_vector_dump(vec_ra,stdout);
00432 cpl_vector_dump(vec_dec,stdout);
00433
00434 while( frame != NULL){
00435 double cum_ra=0;
00436 double cum_dec=0;
00437
00438
00439 check( nod_name = cpl_frame_get_filename( frame));
00440 check( header = cpl_propertylist_load( nod_name, 0));
00441 check( cum_ra = xsh_pfits_get_ra_cumoffset( header));
00442 check( cum_dec = xsh_pfits_get_dec_cumoffset( header));
00443 check( rel_ra = xsh_pfits_get_ra_reloffset( header));
00444 check( rel_dec = xsh_pfits_get_dec_reloffset( header));
00445
00446 if ( cum_ra == A_cum_ra && cum_dec == A_cum_dec){
00447 type = "A";
00448 nod_number = A_number;
00449 }
00450 else{
00451 type = "B";
00452 nod_number = B_number;
00453 }
00454
00455
00456
00457 }
00458 while( frame != NULL){
00459 double cum_ra=0;
00460 double cum_dec=0;
00461 double nrel_ra, nrel_dec;
00462
00463 check( nod_name = cpl_frame_get_filename( frame));
00464 check( header = cpl_propertylist_load( nod_name, 0));
00465 check( cum_ra = xsh_pfits_get_ra_cumoffset( header));
00466 check( cum_dec = xsh_pfits_get_dec_cumoffset( header));
00467 check( rel_ra = xsh_pfits_get_ra_reloffset( header));
00468 check( rel_dec = xsh_pfits_get_dec_reloffset( header));
00469
00470 if ( cum_ra == A_cum_ra && cum_dec == A_cum_dec){
00471 type = "A";
00472 nod_number = A_number;
00473 }
00474 else{
00475 type = "B";
00476 nod_number = B_number;
00477 }
00478
00479 xsh_msg("name %s cumoffset %f %f reloffset %f %f nod_seq_number : %s%d", nod_name,
00480 cum_ra, cum_dec, rel_ra, rel_dec, type, nod_number);
00481
00482 xsh_free_propertylist( &header);
00483
00484 check( tmp_set = cpl_frameset_new());
00485 cpl_frameset_insert( tmp_set, cpl_frame_duplicate( frame));
00486
00487 check( frame = cpl_frameset_get_next( ord_set));
00488
00489 while( frame != NULL){
00490 check( nod_name = cpl_frame_get_filename( frame));
00491 check( header = cpl_propertylist_load( nod_name, 0));
00492 check( nrel_ra = xsh_pfits_get_ra_reloffset( header));
00493 check( nrel_dec = xsh_pfits_get_dec_reloffset( header));
00494 xsh_free_propertylist( &header);
00495
00496 if ( rel_ra == nrel_ra && nrel_dec == rel_dec){
00497 cpl_frameset_insert( tmp_set, cpl_frame_duplicate( frame));
00498 }
00499 else{
00500 break;
00501 }
00502 check( frame = cpl_frameset_get_next( ord_set));
00503 }
00504
00505
00506 sprintf( ftag,"med_%s%d", type, nod_number);
00507 sprintf(name,"%s.fits",ftag);
00508 check( crhm_frame = xsh_remove_crh_multiple( tmp_set, ftag, stack_par,NULL,instrument, NULL,NULL,1 ));
00509
00510 check( cpl_frameset_insert( result, crhm_frame));
00511 xsh_add_temporary_file(name);
00512
00513 if ( cum_ra == A_cum_ra && cum_dec == A_cum_dec){
00514 A_number++;
00515 }
00516 else{
00517 B_number++;
00518 }
00519 xsh_free_frameset( &tmp_set);
00520 }
00521
00522 cleanup:
00523 if ( cpl_error_get_code() != CPL_ERROR_NONE){
00524 xsh_free_propertylist( &header);
00525 xsh_free_frameset( &tmp_set);
00526 xsh_free_frameset( &result);
00527 }
00528 return result;
00529 }
00530
00531
00532 static cpl_frame* xsh_frame_divide_flat( int do_flat, cpl_frame* src,
00533 cpl_frame *mflat, const char* tag, xsh_instrument* instr)
00534 {
00535 cpl_frame *result = NULL;
00536
00537 if ( do_flat == CPL_TRUE){
00538 xsh_msg( "Divide by flat");
00539 check( result = xsh_divide_flat( src, mflat, tag, instr));
00540 }
00541 else{
00542 check( result = cpl_frame_duplicate( src));
00543 }
00544
00545 cleanup:
00546 return result;
00547 }
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643 static cpl_frame*
00644 xsh_nod_median_correct_list_per_wavelength(cpl_frame *frame_i, xsh_instrument* inst)
00645 {
00646
00647 cpl_frame* frame_o=NULL;
00648 xsh_rec_list* list_o=NULL;
00649
00650 const char* fname_i=NULL;
00651 char fname_o[256];
00652 cpl_image* image=NULL;
00653 const char* tag_o;
00654 int next=0;
00655 int i=0;
00656 int j=0;
00657 int k=0;
00658 int sx=0;
00659 int sy=0;
00660 float* pdata=NULL;
00661 double median=0;
00662
00663
00664 XSH_ASSURE_NOT_NULL(frame_i);
00665 XSH_ASSURE_NOT_NULL(inst);
00666
00667 next = cpl_frame_get_nextensions(frame_i);
00668 fname_i=cpl_frame_get_filename(frame_i);
00669
00670 list_o=xsh_rec_list_load(frame_i,inst);
00671
00672 for(k=0; k<next;k++) {
00673
00674 sx=list_o->list[k].nlambda;
00675 sy=list_o->list[k].nslit;
00676
00677 pdata= xsh_rec_list_get_data1(list_o,k);
00678
00679 image=cpl_image_wrap_float(sx,sy,pdata);
00680
00681 for(i=1;i<=sx;i++) {
00682 median=cpl_image_get_median_window(image,i,1,i,sy);
00683 for(j=0;j<sy;j++) {
00684 pdata[j*sx+i-1]-=median;
00685 }
00686 }
00687 cpl_image_unwrap(image);
00688
00689 }
00690
00691 tag_o=cpl_frame_get_tag(frame_i);
00692 sprintf(fname_o,"MED_COR_%s",fname_i);
00693 frame_o=xsh_rec_list_save(list_o,fname_o,tag_o,1);
00694 cpl_frame_set_filename(frame_o,fname_o);
00695 xsh_add_temporary_file(fname_o);
00696
00697 cleanup:
00698 xsh_rec_list_free(&list_o);
00699 return frame_o;
00700 }
00701
00702
00703 static cpl_error_code
00704 xsh_nod_rectify_and_shift_with_key(const char* nod_name,
00705 const char* qual_name,
00706 const char* rec_prefix,
00707 cpl_frame* frame2D,
00708 cpl_frame* order_tab_edges,
00709 cpl_frame* wave_tab,
00710 cpl_frame* model_config_frame,
00711 cpl_frame* spectral_format,
00712 cpl_frame* disp_tab_frame,
00713 xsh_instrument* instrument,
00714 const int corr_sky,
00715 xsh_rectify_param *rectify_par,
00716 double** ref_ra,
00717 double** ref_dec,
00718 cpl_frame **shift2_frame,
00719 cpl_frame **shift1_frame)
00720
00721 {
00722
00723
00724 char file_name[256];
00725 cpl_frame * rec2_frame = NULL ;
00726 cpl_frame * rec2eso_frame = NULL ;
00727 cpl_frame * rec2tab_frame = NULL ;
00728 cpl_frame *shift2eso_frame = NULL;
00729 cpl_frame * rec1_frame = NULL ;
00730 cpl_frame *shift1eso_frame = NULL;
00731 cpl_frame * rec2_frame_median_correct=NULL;
00732
00733
00734 sprintf(file_name,"REC2_%s_%s",qual_name,nod_name);
00735 check(rec2_frame = xsh_rectify( frame2D,order_tab_edges,
00736 wave_tab,model_config_frame,instrument,
00737 rectify_par,spectral_format,disp_tab_frame,
00738 file_name,&rec2eso_frame,&rec2tab_frame,
00739 rec_prefix));
00740 xsh_add_temporary_file(file_name);
00741
00742 sprintf(file_name,"SHIFT2_%s_%s",qual_name,nod_name);
00743
00744 if(corr_sky) {
00745 rec2_frame_median_correct=xsh_nod_median_correct_list_per_wavelength(rec2_frame,instrument);
00746
00747
00748 check( *shift2_frame = shift_with_kw( rec2_frame_median_correct, instrument,rectify_par,
00749 file_name, &shift2eso_frame,
00750 ref_ra,ref_dec, 0));
00751 xsh_add_temporary_file(file_name);
00752 sprintf(file_name,"REC1_FAST_%s_%s",qual_name,nod_name);
00753 check( rec1_frame = xsh_rec_list_frame_invert( rec2_frame_median_correct, file_name,
00754 instrument));
00755 xsh_add_temporary_file(cpl_frame_get_filename(rec1_frame));
00756
00757 } else {
00758 check( *shift2_frame = shift_with_kw( rec2_frame, instrument,rectify_par,
00759 file_name, &shift2eso_frame,
00760 ref_ra,ref_dec, 0));
00761 xsh_add_temporary_file(file_name);
00762 sprintf(file_name,"REC1_FAST_%s_%s",qual_name,nod_name);
00763 check( rec1_frame = xsh_rec_list_frame_invert( rec2_frame, file_name,
00764 instrument));
00765 xsh_add_temporary_file(cpl_frame_get_filename(rec1_frame));
00766
00767 }
00768
00769
00770 sprintf(file_name,"SHIFT1_FAST_%s_%s",qual_name,nod_name);
00771 check( *shift1_frame = shift_with_kw( rec1_frame, instrument,
00772 rectify_par, file_name,
00773 &shift1eso_frame, ref_ra,
00774 ref_dec, 1));
00775 xsh_add_temporary_file(file_name);
00776 cleanup:
00777
00778 xsh_free_frame( &rec1_frame);
00779 xsh_free_frame( &rec2_frame);
00780 xsh_free_frame( &shift1eso_frame);
00781 xsh_free_frame( &shift2eso_frame);
00782 xsh_free_frame( &rec2eso_frame);
00783 xsh_free_frame( &rec2tab_frame);
00784 xsh_free_frame( &rec2_frame_median_correct);
00785 return cpl_error_get_code();
00786 }
00787
00788 cpl_frameset*
00789 xsh_scired_slit_nod_fast(
00790 cpl_frameset *nod_set,
00791 cpl_frame* spectral_format,
00792 cpl_frame* master_flat,
00793 cpl_frame* order_tab_edges,
00794 cpl_frame* wave_tab,
00795 cpl_frame* model_config_frame,
00796 cpl_frame* disp_tab_frame,
00797 cpl_frame* wavemap,
00798 xsh_instrument* instrument,
00799 xsh_remove_crh_single_param *crh_single_par,
00800 xsh_rectify_param *rectify_par,
00801 const int do_flatfield,
00802 const int corr_sky,
00803 const int compute_eff,
00804 const char* rec_prefix,
00805 cpl_frameset **comb_eff_set)
00806
00807 {
00808
00809
00810
00811 cpl_frame * div2_frame = NULL ;
00812 cpl_frame * eff2_frame = NULL ;
00813 cpl_frame *shift1_frame = NULL;
00814 cpl_frame *shift2_frame = NULL;
00815 cpl_frame *shift1_eff_frame = NULL;
00816 cpl_frame *shift2_eff_frame = NULL;
00817 double* ref_ra=NULL;
00818 double* ref_dec=NULL;
00819 double* ref_eff_ra=NULL;
00820 double* ref_eff_dec=NULL;
00821 cpl_frameset* comb_set=NULL;
00822
00823
00824 int i=0;
00825 int nb_pairs=0;
00826
00827 xsh_msg("Method fast");
00828 check( comb_set = cpl_frameset_new());
00829
00830 if(compute_eff) {
00831 check( *comb_eff_set = cpl_frameset_new());
00832 }
00833
00834
00835 check( nb_pairs = cpl_frameset_get_size( nod_set));
00836
00837 for( i = 0; i< nb_pairs; i++){
00838 cpl_frame *nod_frame = NULL;
00839 const char* nod_name = NULL;
00840 char tag[256];
00841 char name[256];
00842
00843 nod_frame = cpl_frameset_get_frame( nod_set, i);
00844 nod_name = cpl_frame_get_filename( nod_frame);
00845 xsh_msg_dbg_high( "***** Frame %s", nod_name);
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855 sprintf(tag,"DIV2_FLATFIELD_%s", xsh_instrument_arm_tostring(instrument)) ;
00856 sprintf(name,"%s.fits",tag);
00857 check( div2_frame =xsh_frame_divide_flat( do_flatfield, nod_frame, master_flat,
00858 tag, instrument));
00859 xsh_add_temporary_file(name);
00860
00861
00862 check(xsh_nod_rectify_and_shift_with_key(nod_name,"RECTIFIED",rec_prefix,
00863 div2_frame,order_tab_edges,
00864 wave_tab,model_config_frame,
00865 spectral_format,
00866 disp_tab_frame,
00867 instrument,corr_sky,
00868 rectify_par,&ref_ra,&ref_dec,
00869 &shift1_frame,&shift2_frame));
00870
00871 cpl_frameset_insert( comb_set, shift2_frame);
00872 cpl_frameset_insert( comb_set, shift1_frame);
00873
00874 if(compute_eff) {
00875 if(disp_tab_frame != NULL) {
00876 int conserve_flux=rectify_par->conserve_flux;
00877
00878 sprintf(tag,"NOCRH_EFF_%s",xsh_instrument_arm_tostring(instrument)) ;
00879 check( eff2_frame =xsh_frame_divide_flat(0, nod_frame, master_flat,
00880 tag, instrument));
00881 rectify_par->conserve_flux=1;
00882 check(xsh_nod_rectify_and_shift_with_key(nod_name,"EFF",rec_prefix,
00883 eff2_frame,order_tab_edges,
00884 wave_tab,model_config_frame,
00885 spectral_format,
00886 disp_tab_frame,
00887 instrument,corr_sky,
00888 rectify_par,
00889 &ref_eff_ra,&ref_eff_dec,
00890 &shift1_eff_frame,
00891 &shift2_eff_frame));
00892
00893 rectify_par->conserve_flux=conserve_flux;
00894 cpl_frameset_insert( *comb_eff_set, shift2_eff_frame);
00895 cpl_frameset_insert( *comb_eff_set, shift1_eff_frame);
00896 xsh_free_frame( &eff2_frame);
00897
00898 }
00899 }
00900
00901 xsh_free_frame( &div2_frame);
00902
00903
00904 }
00905
00906 cleanup:
00907 XSH_FREE( ref_ra);
00908 XSH_FREE( ref_dec);
00909 XSH_FREE( ref_eff_ra);
00910 XSH_FREE( ref_eff_dec);
00911 xsh_free_frame( &eff2_frame);
00912 xsh_free_frame( &div2_frame);
00913 return comb_set;
00914 }
00915
00916
00917
00918
00919 cpl_frameset*
00920 xsh_scired_slit_nod_accurate(
00921 cpl_frameset *nod_set,
00922 cpl_frame* spectral_format,
00923 cpl_frame* master_flat,
00924 cpl_frame* order_tab_edges,
00925 cpl_frame* wave_tab,
00926 cpl_frame* model_config_frame,
00927 cpl_frame* disp_tab_frame,
00928 cpl_frame* wavemap,
00929 cpl_frame *skymask_frame,
00930 xsh_instrument* instrument,
00931 xsh_remove_crh_single_param *crh_single_par,
00932 xsh_rectify_param *rectify_par,
00933 xsh_localize_obj_param *loc_obj_par,
00934 const char *throw_name,
00935 const int do_flatfield,
00936 const char* rec_prefix
00937 )
00938 {
00939 int i=0;
00940 cpl_frame * a_b = NULL;
00941
00942
00943 char file_tag[40];
00944 char file_name[256];
00945
00946 int nb_nod=0;
00947 double *throw_shift = NULL;
00948 double *throw_tab = NULL;
00949
00950 cpl_frame * loc_a_b = NULL, * loc_b_a = NULL ;
00951 cpl_frame * loc_a_b_0 = NULL;
00952
00953 cpl_frameset *comb_set = NULL;
00954
00955
00956 xsh_msg("Method accurate");
00957 check( comb_set = cpl_frameset_new());
00958
00959 check( nb_nod = cpl_frameset_get_size( nod_set));
00960
00961 int file_exist=0;
00962 file_exist=access(throw_name, F_OK);
00963 if (file_exist != 0) {
00964 xsh_msg_error("The Name %s of ASCII file containing the list of throw shifts with respect to the first exposure was not found. Exit.",throw_name);
00965 cpl_error_set(cpl_func, CPL_ERROR_FILE_NOT_FOUND);
00966 goto cleanup;
00967 }
00968
00969 if ( strcmp( throw_name, "") != 0 && file_exist ==0 ){
00970 FILE* throw_file = NULL;
00971 char throw_line[200];
00972 int iline;
00973
00974 xsh_msg("Load Throw shifts from file %s", throw_name);
00975 throw_file = fopen( throw_name, "r");
00976
00977 XSH_CALLOC( throw_tab, double, nb_nod);
00978
00979 iline=0;
00980
00981 while (fgets( throw_line, 200, throw_file) != NULL){
00982 char shiftval[200];
00983
00984 sscanf( throw_line, "%s",shiftval);
00985 if (iline < nb_nod){
00986 throw_tab[iline] = atof( shiftval);
00987 }
00988 iline++;
00989 }
00990 if (iline != nb_nod){
00991 xsh_error_msg("Invalid number of lines in ASCII file %s : %d - expected number : %d",
00992 throw_name, iline, nb_nod);
00993 }
00994 fclose( throw_file);
00995 }
00996
00997
00998 for( i = 0; i<nb_nod ; i++) {
00999
01000 cpl_frame * divided = NULL ;
01001 cpl_frame * shifted_a_b = NULL;
01002 cpl_frame * shifted_a_b_eso = NULL;
01003 cpl_frame * shifted_a_b_tab = NULL;
01004 cpl_frame *inv_shifted_a_b = NULL;
01005 cpl_frame *loc_rec = NULL;
01006 cpl_frame *loc_rec_eso = NULL;
01007 cpl_frame *loc_rec_tab = NULL;
01008 cpl_frame *inv_loc_rec = NULL;
01009 char str[16] ;
01010
01011
01012 if (throw_tab != NULL){
01013 throw_shift = &throw_tab[i];
01014 }
01015
01016 if (i%2 == 0){
01017 sprintf( str, "AB_%d_%s", i/2, xsh_instrument_arm_tostring(instrument)) ;
01018
01019 }
01020 else{
01021 sprintf( str, "BA_%d_%s", i/2, xsh_instrument_arm_tostring(instrument)) ;
01022
01023 }
01024 check( a_b = cpl_frameset_get_frame( nod_set, i));
01025 xsh_msg_dbg_high( "***** Frame %s", cpl_frame_get_filename( a_b ) ) ;
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035 sprintf(file_tag,"SLIT_NOD_NOCRH_FF_%s", str);
01036 check( divided = xsh_frame_divide_flat( do_flatfield, a_b,master_flat, file_tag,
01037 instrument));
01038
01039 if ( i > 1 && throw_shift == NULL){
01040 if (i%2 == 0){
01041 xsh_msg("TEST %d : on fait loc_ab et loc_ba", i);
01042 sprintf(file_name,"LOC_REC_%s.fits", str);
01043 check( loc_rec = xsh_rectify( divided, order_tab_edges,
01044 wave_tab, model_config_frame, instrument,
01045 rectify_par, spectral_format, disp_tab_frame,
01046 file_name, &loc_rec_eso, &loc_rec_tab, rec_prefix));
01047 xsh_msg( "Localize the object (A%d)", i/2);
01048 sprintf( file_name,"LOCALIZE_%s.fits", str);
01049 check( loc_a_b = xsh_localize_obj( loc_rec, skymask_frame, instrument, loc_obj_par,
01050 NULL, file_name));
01051 xsh_add_temporary_file(file_name);
01052
01053 xsh_msg( "Negative rectify");
01054 sprintf(file_name,"DRL_INV_LOC_REC_%s", str);
01055 check( inv_loc_rec = xsh_rec_list_frame_invert( loc_rec,
01056 file_name, instrument));
01057
01058 xsh_msg( "Localize the object (B%d)", i/2);
01059 sprintf(file_name,"INV_LOCALIZE_%s.fits", str);
01060 check( loc_b_a = xsh_localize_obj( inv_loc_rec, skymask_frame, instrument, loc_obj_par,
01061 NULL, file_name));
01062 }
01063 else{
01064 xsh_msg("TEST %d : loc_ab = loc_ba", i);
01065 loc_a_b = loc_b_a;
01066 }
01067 }
01068
01069 xsh_msg( "Rectify and shift");
01070 sprintf(file_name,"SHIFT_REC_NOCRH_%s.fits", str);
01071
01072 check( shifted_a_b = xsh_rectify_and_shift( divided,order_tab_edges,
01073 wave_tab,
01074 model_config_frame,
01075 instrument,
01076 rectify_par,
01077 spectral_format,
01078 loc_a_b,
01079 loc_a_b_0,
01080 throw_shift,
01081 disp_tab_frame,
01082 file_name,
01083 &shifted_a_b_eso,
01084 &shifted_a_b_tab));
01085 xsh_free_frame( &loc_a_b);
01086
01087 if ( i==0 && throw_shift == NULL){
01088 xsh_msg( "Localize the object (A0)");
01089 sprintf(file_name,"LOCALIZE_%s.fits", str);
01090 check( loc_a_b_0 = xsh_localize_obj( shifted_a_b, skymask_frame, instrument, loc_obj_par,
01091 NULL, file_name));
01092 xsh_add_temporary_file(file_name);
01093
01094 xsh_msg( "Negative rectify");
01095 sprintf(file_name,"DRL_INV_LOC_REC_%s", str);
01096 check( inv_shifted_a_b = xsh_rec_list_frame_invert( shifted_a_b,
01097 file_name, instrument));
01098 xsh_msg( "Localize the object (B0)");
01099 sprintf(file_name,"INV_LOCALIZE_%s.fits", str);
01100 check( loc_a_b = xsh_localize_obj( inv_shifted_a_b, skymask_frame, instrument, loc_obj_par,
01101 NULL, file_name));
01102 }
01103 check( cpl_frameset_insert( comb_set, shifted_a_b));
01104
01105
01106 xsh_free_frame( ÷d);
01107 xsh_free_frame( &shifted_a_b_eso);
01108 xsh_free_frame( &shifted_a_b_tab);
01109 xsh_free_frame( &inv_shifted_a_b);
01110 xsh_free_frame( &loc_rec);
01111 xsh_free_frame( &loc_rec_eso);
01112 xsh_free_frame( &loc_rec_tab);
01113 xsh_free_frame( &inv_loc_rec);
01114 }
01115
01116 cleanup:
01117 if ( cpl_error_get_code() != CPL_ERROR_NONE || file_exist!=0){
01118 xsh_free_frameset( &comb_set);
01119 }
01120 xsh_free_frame( &loc_a_b_0);
01121 xsh_free_frame( &loc_a_b);
01122 XSH_FREE( throw_tab);
01123 return comb_set;
01124
01125
01126 }
01127
01128
01129 cpl_error_code
01130 xsh_scired_slit_nod_get_calibs(cpl_frameset* raws,
01131 cpl_frameset* calib,
01132 xsh_instrument* instrument,
01133 cpl_frame** bpmap,
01134 cpl_frame** master_bias,
01135 cpl_frame** master_flat,
01136 cpl_frame** order_tab_edges,
01137 cpl_frame** wave_tab,
01138 cpl_frame** model_config_frame,
01139 cpl_frame** wavemap,
01140 cpl_frame** slitmap,
01141 cpl_frame** disp_tab_frame,
01142 cpl_frame** spectral_format,
01143 cpl_frame** skymask_frame,
01144 cpl_frame** response_ord_frame,
01145 cpl_frame** frm_atmext,
01146 int do_computemap,
01147 int use_skymask,
01148 int pscan,
01149 const char* rec_prefix,
01150 const char* recipe_id)
01151
01152 {
01153 int recipe_use_model=FALSE;
01154 cpl_frame* ref_frame=NULL;
01155 cpl_boolean mode_phys;
01156 mode_phys=xsh_mode_is_physmod(calib,instrument);
01157 check(*bpmap=xsh_check_load_master_bpmap(calib,instrument,recipe_id));
01158
01159 if(pscan==0) {
01160
01161 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
01162
01163 check(*master_bias = xsh_find_master_bias( calib, instrument));
01164 }
01165 }
01166
01167 check( *order_tab_edges = xsh_find_order_tab_edges( calib, instrument));
01168
01169
01170 if(mode_phys) {
01171 recipe_use_model = TRUE;
01172 if((*model_config_frame = xsh_find_frame_with_tag(calib,
01173 XSH_MOD_CFG_OPT_AFC,
01174 instrument)) == NULL) {
01175
01176 xsh_error_reset();
01177 if ((*model_config_frame = xsh_find_frame_with_tag(calib,XSH_MOD_CFG_OPT_2D,
01178 instrument)) == NULL) {
01179 xsh_error_reset();
01180
01181 if ((*model_config_frame = xsh_find_frame_with_tag(calib,XSH_MOD_CFG_TAB,
01182 instrument)) == NULL) {
01183 xsh_error_reset();
01184 } else {
01185 xsh_msg("RECIPE USE REFERENCE MODEL");
01186 }
01187 } else {
01188 xsh_msg("RECIPE USE OPTIMISED 2D MODEL");
01189 }
01190 } else {
01191 xsh_msg("RECIPE USE OPTIMISED AFC MODEL");
01192 }
01193 } else {
01194 xsh_msg("RECIPE USE WAVE SOLUTION");
01195 check( *wave_tab = xsh_find_wave_tab( calib, instrument));
01196 recipe_use_model = FALSE;
01197 }
01198
01199 XSH_ASSURE_NOT_ILLEGAL( model_config_frame != NULL || wave_tab != NULL);
01200
01201 if( NULL == (*master_flat = xsh_find_master_flat( calib,instrument))) {
01202 xsh_msg_error("You must provide a %s frame in input",
01203 XSH_GET_TAG_FROM_MODE ( XSH_MASTER_FLAT, instrument));
01204 goto cleanup;
01205 }
01206 if(do_computemap && recipe_use_model==FALSE) {
01207 check( *wavemap = xsh_find_wavemap( calib, instrument));
01208 }
01209
01210 if(NULL == (*disp_tab_frame = xsh_find_disp_tab( calib, instrument))) {
01211 xsh_msg("To compute efficiency, you must provide a DISP_TAB_ARM input");
01212 }
01213
01214 if ( recipe_use_model){
01215 ref_frame=cpl_frameset_get_first(raws);
01216 }
01217 else{
01218 ref_frame = *master_flat;
01219 }
01220 check( xsh_check_get_map( *disp_tab_frame, *order_tab_edges,
01221 ref_frame, *model_config_frame, calib, instrument,
01222 do_computemap, recipe_use_model, rec_prefix,
01223 wavemap, slitmap));
01224
01225 check( *spectral_format = xsh_find_spectral_format( calib, instrument));
01226
01227 if ( use_skymask == TRUE){
01228 xsh_msg("Using sky mask");
01229 check( *skymask_frame = xsh_find_frame_with_tag( calib, XSH_SKY_LINE_LIST,
01230 instrument));
01231 }
01232 if((*response_ord_frame=xsh_find_frame_with_tag(calib,XSH_MRESPONSE_MERGE1D_SLIT,
01233 instrument)) == NULL ) {
01234 check( *response_ord_frame = xsh_find_frame_with_tag(calib,
01235 XSH_RESPONSE_MERGE1D_SLIT,
01236 instrument));
01237 }
01238 if(*response_ord_frame != NULL) {
01239 *frm_atmext=xsh_find_frame_with_tag(calib,XSH_ATMOS_EXT,instrument);
01240 if(*frm_atmext==NULL) {
01241 xsh_msg_error("Provide atmospheric extinction frame");
01242 }
01243 }
01244
01245 cleanup:
01246 return cpl_error_get_code();
01247 }
01248
01249
01255 static cpl_error_code
01256 xsh_scired_nod_params_monitor(xsh_rectify_param * rectify_par,
01257 xsh_localize_obj_param * loc_obj_par)
01258 {
01259
01260
01261
01262 xsh_msg_dbg_low("rectify params: radius=%g bin_lambda=%g bin_space=%g",
01263 rectify_par->rectif_radius,rectify_par->rectif_bin_lambda,
01264 rectify_par->rectif_bin_space);
01265
01266 xsh_msg_dbg_low("localize params: chunk_nb=%d nod_step=%g",
01267 loc_obj_par->loc_chunk_nb,loc_obj_par->nod_step);
01268
01269 return cpl_error_get_code();
01270
01271 }
01272
01273
01274
01275
01276 cpl_error_code
01277 xsh_scired_nod_get_parameters(cpl_parameterlist* parameters,
01278 xsh_instrument* instrument,
01279 xsh_remove_crh_single_param** crh_single_par,
01280 xsh_rectify_param** rectify_par,
01281 xsh_extract_param** extract_par,
01282 xsh_combine_nod_param** combine_nod_par,
01283 xsh_slit_limit_param** slit_limit_par,
01284 xsh_localize_obj_param** loc_obj_par,
01285 int* rectify_fast, int* pscan,
01286 const char* rec_id
01287 )
01288
01289
01290 {
01291 check( *loc_obj_par = xsh_parameters_localize_obj_get(rec_id,
01292 parameters));
01293 check( *rectify_par = xsh_parameters_rectify_get(rec_id,
01294 parameters));
01295 check( *rectify_fast = xsh_parameters_rectify_fast_get(rec_id,
01296 parameters));
01297 check( *crh_single_par = xsh_parameters_remove_crh_single_get(rec_id,
01298 parameters));
01299 check( *extract_par=xsh_parameters_extract_get(rec_id, parameters));
01300 check( *combine_nod_par = xsh_parameters_combine_nod_get(rec_id,
01301 parameters)) ;
01302 check( *slit_limit_par = xsh_parameters_slit_limit_get(rec_id,
01303 parameters));
01304
01305 check(xsh_rectify_params_set_defaults(parameters,rec_id,instrument,*rectify_par));
01306
01307 check( xsh_scired_nod_params_monitor(*rectify_par,*loc_obj_par));
01308 check( *pscan=xsh_parameters_get_int(parameters,rec_id,"pre-overscan-corr"));
01309
01310 cleanup:
01311 return cpl_error_get_code();
01312 }
01313
01314
01315 cpl_error_code
01316 xsh_flux_calibrate1D(cpl_frame* rect1D,
01317 cpl_frame* atmext,
01318 cpl_frame* response,
01319 int mpar,
01320 xsh_instrument* inst,
01321 const char* rec_prefix,
01322 cpl_frame** fcal_rect_1D,
01323 cpl_frame** fcal_1D)
01324 {
01325
01326 char file_tag[256];
01327 char file_name[256];
01328 char arm_str[8] ;
01329 cpl_frame* nrm_1D=NULL;
01330 const char* tag=NULL;
01331
01332
01333 tag=cpl_frame_get_tag(rect1D);
01334 sprintf(arm_str,"%s",xsh_instrument_arm_tostring(inst));
01335 xsh_msg("tag=%s",tag);
01336 if(strstr(tag,XSH_ORDER_OXT1D) == NULL) {
01337 sprintf(file_tag,"%s_%s_%s",rec_prefix,XSH_NORM_ORDER1D,arm_str);
01338 } else {
01339 sprintf(file_tag,"%s_%s_%s",rec_prefix,XSH_NORM_OXT1D,arm_str);
01340 }
01341 xsh_msg("file_tag1=%s",file_tag);
01342 sprintf(file_name,"%s.fits",file_tag);
01343 xsh_add_temporary_file(file_name);
01344
01345 check(nrm_1D=xsh_normalize_spectrum_ord(rect1D,atmext,1,inst,file_tag));
01346 if(strstr(tag,XSH_ORDER_OXT1D) == NULL) {
01347 sprintf(file_tag,"%s_%s_%s",rec_prefix,XSH_FLUX_ORDER1D,arm_str);
01348 } else {
01349 sprintf(file_tag,"%s_%s_%s",rec_prefix,XSH_FLUX_OXT1D,arm_str);
01350 }
01351 sprintf(file_name,"%s.fits",file_tag);
01352
01353
01354 check(*fcal_rect_1D=xsh_util_multiply_by_response_ord(nrm_1D,response,file_tag));
01355 check(*fcal_1D= xsh_merge_ord(*fcal_rect_1D,inst,mpar,rec_prefix));
01356 xsh_msg("file_tag2=%s",file_tag);
01357
01358
01359 cleanup:
01360 xsh_free_frame(&nrm_1D);
01361 return cpl_error_get_code();
01362
01363 }
01364
01365
01366
01367 cpl_error_code
01368 xsh_flux_calibrate2D(cpl_frame* rect2D,
01369 cpl_frame* atmext,
01370 cpl_frame* response,
01371 int mpar,
01372 xsh_instrument* inst,
01373 const char* rec_prefix,
01374 cpl_frame** fcal_rect_2D,
01375 cpl_frame** fcal_2D)
01376 {
01377
01378
01379 char file_tag[256];
01380 char file_name[256];
01381 char arm_str[8] ;
01382 cpl_frame* nrm_2D=NULL;
01383
01384 sprintf(arm_str,"%s",xsh_instrument_arm_tostring(inst));
01385
01386 sprintf(file_tag,"%s_%s_%s",rec_prefix,XSH_NORM_ORDER2D,arm_str);
01387 sprintf(file_name,"%s.fits",file_tag);
01388 check(nrm_2D=xsh_normalize_spectrum_ord(rect2D,atmext,1,inst,file_tag));
01389 xsh_add_temporary_file(file_name);
01390
01391
01392 sprintf(file_tag,"%s_%s_%s",rec_prefix,XSH_FLUX_ORDER2D,arm_str);
01393 check(*fcal_rect_2D=xsh_util_multiply_by_response_ord(nrm_2D,response,file_tag));
01394 check(*fcal_2D = xsh_merge_ord(*fcal_rect_2D, inst,mpar,rec_prefix));
01395
01396 cleanup:
01397 xsh_free_frame(&nrm_2D);
01398 return cpl_error_get_code();
01399
01400
01401 }
01402
01403 cpl_error_code
01404 xsh_flux_calibrate(cpl_frame* rect2D,
01405 cpl_frame* rect1D,
01406 cpl_frame* atmext,
01407 cpl_frame* response,
01408 int mpar,
01409 xsh_instrument* inst,
01410 const char* rec_prefix,
01411 cpl_frame** fcal_rect_2D,
01412 cpl_frame** fcal_rect_1D,
01413 cpl_frame** fcal_2D,
01414 cpl_frame** fcal_1D)
01415 {
01416
01417
01418 check(xsh_flux_calibrate2D(rect2D,atmext,response,mpar,inst,rec_prefix,
01419 fcal_rect_2D,fcal_2D));
01420 check(xsh_flux_calibrate1D(rect1D,atmext,response,mpar,inst,rec_prefix,
01421 fcal_rect_1D,fcal_1D));
01422
01423 cleanup:
01424
01425
01426 return cpl_error_get_code();
01427 }
01428
01429
01430
01431 cpl_error_code
01432 xsh_slit_stare_get_calibs(cpl_frameset* calib,
01433 xsh_instrument* instrument,
01434 cpl_frame** spectralformat,
01435 cpl_frame** mbias,
01436 cpl_frame** mdark,
01437 cpl_frame** mflat,
01438 cpl_frame** otab_edges,
01439 cpl_frame** model_cfg,
01440 cpl_frame** wave_tab,
01441 cpl_frame** sky_list,
01442 cpl_frame** qc_sky,
01443 cpl_frame** bpmap,
01444 cpl_frame** sframe_sky_sub_tab,
01445 cpl_frame** wmap,
01446 cpl_frame** smap,
01447 const char* rec_id,
01448 int * recipe_use_model,
01449 int pscan)
01450 {
01451
01452
01453 cpl_boolean mode_phys;
01454 mode_phys=xsh_mode_is_physmod(calib,instrument);
01455 check(*spectralformat = xsh_find_spectral_format( calib, instrument));
01456
01457 XSH_ASSURE_NOT_NULL_MSG(*spectralformat,"Null input spectral format frame");
01458 check(xsh_instrument_update_from_spectralformat(instrument,*spectralformat));
01459
01460
01461 if (pscan == 0) {
01462 xsh_msg("pscan=%d",pscan);
01463 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
01464
01465 if((*mbias = xsh_find_master_bias( calib, instrument)) == NULL) {
01466 xsh_msg_error("You must give a MASTER_BIAS_ARM frame");
01467 return CPL_ERROR_DATA_NOT_FOUND;
01468 }
01469 }
01470 }
01471
01472 if((*mdark = xsh_find_master_dark( calib, instrument))==NULL) {
01473 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
01474 xsh_error_reset();
01475 }
01476 if( (*mflat = xsh_find_frame_with_tag(calib,XSH_MASTER_FLAT_SLIT,instrument)) == NULL) {
01477 xsh_msg_error("Missing required input %s",XSH_GET_TAG_FROM_MODE ( XSH_MASTER_FLAT, instrument));
01478 cpl_error_set(cpl_func, CPL_ERROR_FILE_NOT_FOUND);
01479 goto cleanup;
01480 }
01481 check( *mflat = xsh_find_master_flat( calib, instrument ));
01482 check(*otab_edges = xsh_find_order_tab_edges(calib,instrument));
01483
01484
01485 if(mode_phys) {
01486 if((*model_cfg = xsh_find_frame_with_tag(calib,XSH_MOD_CFG_OPT_AFC,
01487 instrument)) == NULL) {
01488 xsh_error_reset();
01489 if ((*model_cfg = xsh_find_frame_with_tag(calib,XSH_MOD_CFG_OPT_2D,
01490 instrument)) == NULL) {
01491 xsh_error_reset();
01492
01493 if ((*model_cfg = xsh_find_frame_with_tag(calib,XSH_MOD_CFG_TAB,
01494 instrument)) == NULL) {
01495 xsh_error_reset();
01496 } else {
01497 xsh_msg("RECIPE USE REFERENCE MODEL");
01498 }
01499 } else {
01500 xsh_msg("RECIPE USE OPTIMIZED 2D MODEL");
01501 }
01502 } else {
01503 xsh_msg("RECIPE USE OPTIMIZED AFC MODEL");
01504 }
01505 *recipe_use_model = TRUE;
01506 } else {
01507 xsh_msg("RECIPE USE WAVE SOLUTION");
01508 check( *wave_tab = xsh_find_wave_tab( calib, instrument));
01509 *recipe_use_model = FALSE;
01510 }
01511
01512 if ( *model_cfg != NULL){
01513 check(*sky_list = xsh_find_frame_with_tag( calib, XSH_SKY_LINE_LIST,
01514 instrument));
01515 if ( *sky_list != NULL){
01516
01517 check(*qc_sky=xsh_util_physmod_model_THE_create(*model_cfg,instrument,
01518 *sky_list,1,1,9,1));
01519 }
01520 }
01521
01522
01523 check(*bpmap=xsh_check_load_master_bpmap(calib,instrument,rec_id));
01524 if ((*sframe_sky_sub_tab = xsh_find_frame_with_tag(calib,XSH_SKY_SUB_BKPTS,
01525 instrument)) == NULL) {
01526 xsh_error_reset();
01527 } else {
01528 xsh_msg_warning("Data reduction with user defined break points number from file %s",
01529 cpl_frame_get_filename(*sframe_sky_sub_tab));
01530 }
01531
01532
01533 check(*wmap = xsh_find_frame_with_tag(calib,XSH_WAVE_MAP,instrument));
01534 check(*smap = xsh_find_frame_with_tag(calib,XSH_SLIT_MAP,instrument));
01535
01536 cleanup:
01537
01538 return cpl_error_get_code();
01539 }
01540
01541 cpl_error_code
01542 xsh_slit_offset_get_calibs(cpl_frameset* calib,xsh_instrument* instrument,
01543 cpl_frame** bpmap,cpl_frame** mbias,
01544 cpl_frame** mdark, cpl_frame** otab_edges,
01545 cpl_frame** model_cfg, cpl_frame** wave_tab,
01546 cpl_frame** mflat, cpl_frame** wmap, cpl_frame** smap,
01547 cpl_frame** spectral_format,const char* rec_id)
01548 {
01549
01550
01551 cpl_boolean mode_phys;
01552 mode_phys=xsh_mode_is_physmod(calib,instrument);
01553
01554 check(*bpmap=xsh_check_load_master_bpmap(calib,instrument,rec_id));
01555
01556
01557 if ( xsh_instrument_get_arm(instrument) != XSH_ARM_NIR){
01558
01559 check( *mbias = xsh_find_frame_with_tag(calib,XSH_MASTER_BIAS,instrument));
01560 }
01561
01562
01563 if((*mdark=xsh_find_frame_with_tag(calib,XSH_MASTER_DARK,instrument))==NULL){
01564 xsh_error_reset();
01565 }
01566
01567 check( *otab_edges = xsh_find_order_tab_edges( calib, instrument));
01568 if(mode_phys) {
01569 if((*model_cfg=xsh_find_frame_with_tag(calib,XSH_MOD_CFG_OPT_AFC,instrument)) == NULL) {
01570 xsh_error_reset();
01571 if ((*model_cfg = xsh_find_frame_with_tag(calib,XSH_MOD_CFG_OPT_2D,
01572 instrument)) == NULL) {
01573 xsh_error_reset();
01574
01575 if ((*model_cfg = xsh_find_frame_with_tag(calib,XSH_MOD_CFG_TAB,
01576 instrument)) == NULL) {
01577 xsh_error_reset();
01578 } else {
01579 xsh_msg("RECIPE USE REFERENCE MODEL");
01580 }
01581 } else {
01582 xsh_msg("RECIPE USE OPTIMIZED 2D MODEL");
01583 }
01584 } else {
01585 xsh_msg("RECIPE USE OPTIMIZED AFC MODEL");
01586 }
01587 } else {
01588
01589 *wave_tab = xsh_find_wave_tab( calib, instrument ) ;
01590 }
01591
01592 XSH_ASSURE_NOT_ILLEGAL( *model_cfg != NULL || *wave_tab != NULL ) ;
01593
01594 check( *mflat = xsh_find_master_flat( calib, instrument));
01595
01596 check( *wmap = xsh_find_wavemap( calib, instrument));
01597 check( *smap = xsh_find_slitmap( calib, instrument));
01598 check( *spectral_format = xsh_find_spectral_format( calib, instrument));
01599
01600
01601 cleanup:
01602 return cpl_error_get_code();
01603
01604 }
01605
01606 cpl_error_code
01607 xsh_slit_stare_get_params(cpl_parameterlist* parameters,
01608 const char* rec_id,
01609 int* pre_overscan_corr,
01610 xsh_background_param** backg_par,
01611 xsh_localize_obj_param** loc_obj_par,
01612 xsh_rectify_param** rectify_par,
01613 xsh_remove_crh_single_param** crh_single_par,
01614 int* sub_sky_nbkpts1,
01615 int* do_flatfield,
01616 int* sub_sky_nbkpts2,
01617 xsh_subtract_sky_single_param** sky_par,
01618 int* do_optextract,
01619 xsh_opt_extract_param** opt_extract_par)
01620 {
01621
01622 check( *pre_overscan_corr = xsh_parameters_get_int( parameters, rec_id,
01623 "pre-overscan-corr"));
01624
01625 check( *backg_par = xsh_parameters_background_get(rec_id,parameters));
01626
01627 check( *loc_obj_par = xsh_parameters_localize_obj_get(rec_id,parameters));
01628 check( *rectify_par = xsh_parameters_rectify_get(rec_id,parameters));
01629 check( *crh_single_par = xsh_parameters_remove_crh_single_get(rec_id,parameters));
01630
01631 check( *sub_sky_nbkpts1 = xsh_parameters_subtract_sky_single_get_first(
01632 rec_id,parameters));
01633
01634 check( *sub_sky_nbkpts2 = xsh_parameters_subtract_sky_single_get_second(
01635 rec_id,parameters));
01636 check( *sky_par= xsh_parameters_subtract_sky_single_get(rec_id,parameters));
01637
01638 check( *do_optextract = xsh_parameters_get_boolean( parameters, rec_id,
01639 "do-optextract"));
01640 check( *opt_extract_par = xsh_parameters_opt_extract_get(rec_id,parameters));
01641
01642
01643 cleanup:
01644 return cpl_error_get_code();
01645 }
01646
01647
01648 cpl_error_code
01649 xsh_slit_offset_get_params(cpl_parameterlist* parameters,
01650 const char* rec_id,
01651 xsh_localize_obj_param** loc_obj_par,
01652 xsh_rectify_param** rectify_par,
01653 xsh_remove_crh_single_param** crh_single_par,
01654 xsh_extract_param**extract_par,
01655 xsh_combine_nod_param** combine_nod_param,
01656 int* do_flatfield,
01657 int* gen_sky)
01658 {
01659
01660
01661 check( *loc_obj_par = xsh_parameters_localize_obj_get(rec_id,parameters));
01662 check( *rectify_par = xsh_parameters_rectify_get(rec_id,parameters));
01663 check( *crh_single_par = xsh_parameters_remove_crh_single_get(rec_id,parameters));
01664 if ( (*rectify_par)->rectify_full_slit == 1 ){
01665
01666 xsh_msg( "Use Full Slit" ) ;
01667 }
01668 else {
01669 xsh_msg( "Use Max Possible Slit" ) ;
01670 }
01671
01672
01673
01674
01675
01676 check(*extract_par=xsh_parameters_extract_get(rec_id, parameters )) ;
01677
01678 check(*combine_nod_param = xsh_parameters_combine_nod_get(rec_id,parameters )) ;
01679
01680
01681 check( *gen_sky = xsh_parameters_get_boolean( parameters, rec_id,
01682 "gen-sky"));
01683
01684 cleanup:
01685 return cpl_error_get_code();
01686
01687 }
01688
01689
01690 cpl_error_code
01691 xsh_slit_stare_correct_crh_and_sky(xsh_localize_obj_param * loc_obj_par,
01692 xsh_remove_crh_single_param* crh_single_par,
01693 xsh_rectify_param* rectify_par,
01694 int do_sub_sky,
01695 const char* rec_prefix,
01696 cpl_frame* rmbkg,
01697 cpl_frame* order_tab_edges,
01698 cpl_frame* slitmap,
01699 cpl_frame* wavemap,
01700 cpl_frame* model_config,
01701 cpl_frame* single_frame_sky_sub_tab,
01702 xsh_instrument* instrument,
01703 int sub_sky_nbkpts1,
01704 xsh_subtract_sky_single_param* sky_par,
01705 cpl_frame** sky,
01706 cpl_frame** sky_eso,
01707 cpl_frame** sky_ima,
01708 cpl_frame* wave_tab,
01709 cpl_frame* disp_tab,
01710 cpl_frame* spectral_format,
01711 int nb_raw_frames,
01712 cpl_frame** loc_table,
01713 cpl_frame** clean,
01714 cpl_frame** clean_obj,const int clean_tmp)
01715 {
01716
01717 char sky_tag[256];
01718 char sky_name[256];
01719 char rec_name[256];
01720 cpl_frame * sub_sky = NULL ;
01721 cpl_frame * rect = NULL ;
01723 if( (loc_obj_par->method != LOC_MANUAL_METHOD) ||
01724 (nb_raw_frames == 1 && crh_single_par->nb_iter > 0)){
01725 xsh_msg("Preliminary sky subtraction for CRH single or automatic localization");
01726 xsh_msg("Sky will be put back later");
01727
01728 sprintf(sky_tag,"%s_TMPSKY",rec_prefix);
01729 sprintf(sky_name,"%s.fits",sky_tag);
01730
01731 if(do_sub_sky) {
01732 check( sub_sky = xsh_check_subtract_sky_single( CPL_TRUE, rmbkg,
01733 order_tab_edges,
01734 slitmap,
01735 wavemap,
01736 NULL, NULL,
01737 single_frame_sky_sub_tab,
01738 instrument,
01739 sub_sky_nbkpts1,
01740 sky_par,
01741 sky,
01742 sky_eso,
01743 sky_ima,
01744 sky_tag,clean_tmp));
01745 xsh_add_temporary_file(sky_name);
01746
01747 } else {
01748 sub_sky = cpl_frame_duplicate(rmbkg);
01749 xsh_add_temporary_file(sky_name);
01750 }
01751 if( loc_obj_par->method != LOC_MANUAL_METHOD){
01752 xsh_msg("Localize auto");
01753 sprintf(rec_name,"%s_%s_%s.fits",
01754 rec_prefix,XSH_ORDER2D,
01755 xsh_instrument_arm_tostring(instrument));
01756
01757
01758
01759 check( rect = xsh_rectify( sub_sky, order_tab_edges,
01760 wave_tab, model_config,
01761 instrument, rectify_par,
01762 spectral_format, disp_tab,
01763 rec_name, NULL, NULL,rec_prefix));
01764
01765
01766 check( *loc_table = xsh_localize_obj( rect, NULL, instrument,
01767 loc_obj_par, NULL, NULL));
01768 xsh_free_frame( &rect);
01769 }
01770 else{
01771 check( *loc_table = xsh_localize_obj( NULL, NULL,
01772 instrument, loc_obj_par, NULL, NULL));
01773 }
01774
01775
01776
01777
01778
01779 *clean = cpl_frame_duplicate(sub_sky);
01780
01781 if(clean_tmp) {
01782 xsh_add_temporary_file(cpl_frame_get_filename(*clean));
01783 }
01784 if(do_sub_sky) {
01785 check( *clean_obj = xsh_add_sky_model( *clean, *sky_ima,
01786 instrument, rec_prefix));
01787 } else {
01788 *clean_obj=cpl_frame_duplicate(*clean);
01789 }
01790
01791 xsh_free_frame( &sub_sky);
01792 xsh_free_frame( sky);
01793 xsh_free_frame( sky_eso);
01794 xsh_free_frame( sky_ima);
01795 }
01796 else {
01797
01798 check( *clean_obj=cpl_frame_duplicate(rmbkg));
01799 check( *loc_table = xsh_localize_obj( NULL, NULL,
01800 instrument, loc_obj_par, NULL, NULL));
01801 }
01802
01803
01804 cleanup:
01805
01806 xsh_free_frame(&sub_sky) ;
01807 xsh_free_frame(&rect) ;
01808
01809 return cpl_error_get_code();
01810
01811 }
01812
01813
01814 cpl_error_code
01815 xsh_slit_stare_get_maps(cpl_frameset* calib,
01816 int do_compute_map,int recipe_use_model,
01817 const char* rec_prefix,xsh_instrument* instrument,
01818 cpl_frame* model_config_frame,cpl_frame* crhm_frame,
01819 cpl_frame* disp_tab_frame, cpl_frame* order_tab_edges,
01820 cpl_frame** wavemap_frame, cpl_frame** slitmap_frame)
01821 {
01822 char wave_map_tag[256];
01823 char slit_map_tag[256];
01824 int found_temp=1;
01825 if ( do_compute_map){
01826 if (recipe_use_model){
01827 sprintf(wave_map_tag,"%s_%s_%s",rec_prefix,XSH_WAVE_MAP_MODEL,
01828 xsh_instrument_arm_tostring( instrument ));
01829 sprintf(slit_map_tag,"%s_%s_%s",rec_prefix,XSH_SLIT_MAP_MODEL,
01830 xsh_instrument_arm_tostring( instrument ));
01831
01832 check(xsh_model_temperature_update_frame(&model_config_frame,crhm_frame,
01833 instrument,&found_temp));
01834
01835 check( xsh_create_model_map( model_config_frame, instrument,
01836 wave_map_tag,slit_map_tag,
01837 wavemap_frame, slitmap_frame,0));
01838 }
01839 else{
01840 xsh_msg("Compute the wave map and the slit map");
01841 check( xsh_create_map( disp_tab_frame, order_tab_edges,
01842 crhm_frame, instrument,
01843 wavemap_frame, slitmap_frame,
01844 rec_prefix));
01845 }
01846 }
01847 else {
01848 xsh_msg( "Get the wave map and the slit map from sof" ) ;
01849 check( *slitmap_frame = xsh_find_slitmap(calib, instrument));
01850 check( *wavemap_frame = xsh_find_wavemap(calib, instrument));
01851 }
01852
01853 cleanup:
01854 return cpl_error_get_code();
01855 }
01856
01857
01858 cpl_error_code
01859 xsh_scired_util_spectra_flux_calibrate(cpl_frame* res2D,cpl_frame* res1D,
01860 cpl_frame* response,cpl_frame* atmext,
01861 xsh_instrument* inst,
01862 const char* prefix,
01863 cpl_frame** fluxcal_2D,
01864 cpl_frame** fluxcal_1D)
01865 {
01866 char file_tag[40];
01867 cpl_frame* nrm_1D=NULL;
01868 cpl_frame* nrm_2D=NULL;
01869
01870 sprintf(file_tag,"%s_NORM2D_%s",prefix,xsh_instrument_arm_tostring(inst));
01871 check(nrm_2D=xsh_normalize_spectrum(res2D,atmext,0,inst,file_tag));
01872 sprintf(file_tag,"%s_FLUXCAL2D_%s",prefix,xsh_instrument_arm_tostring(inst));
01873 check(*fluxcal_2D=xsh_util_multiply_by_response(nrm_2D,response,file_tag));
01874 sprintf(file_tag,"%s_NORM1D_%s",prefix,xsh_instrument_arm_tostring(inst));
01875 check(nrm_1D=xsh_normalize_spectrum(res1D,atmext,0,inst,file_tag));
01876 sprintf(file_tag,"%s_FLUXCAL1D_%s",prefix,xsh_instrument_arm_tostring(inst));
01877 check(*fluxcal_1D=xsh_util_multiply_by_response(nrm_1D,response,file_tag));
01878
01879 cleanup:
01880 xsh_free_frame(&nrm_1D);
01881 xsh_free_frame(&nrm_2D);
01882
01883 return cpl_error_get_code();
01884
01885 }
01886
01887
01888 cpl_frame* xsh_compute_efficiency(cpl_frame* mer1D, cpl_frame* std_cat,
01889 cpl_frame* atm_ext, cpl_frame* high_abs_win,
01890 xsh_instrument* instr)
01891 {
01892
01893 cpl_frame* eff=NULL;
01894
01895 if(NULL == (eff=xsh_efficiency_compute(mer1D,std_cat,atm_ext,high_abs_win,instr))) {
01896 xsh_msg_error("An error occurred during efficiency computation");
01897 xsh_msg_error("The recipe recovers without efficiency product generation");
01898 cpl_error_reset();
01899 } else {
01900 check(xsh_frame_table_monitor_flux_qc(eff,"WAVELENGTH","EFF","EFF",instr));
01901 }
01902
01903 cleanup:
01904 return eff;
01905 }
01906
01907 static cpl_error_code
01908 xsh_get_central_xy(cpl_frame* order_tab_edges,xsh_instrument* instrument, int* xcen, int* ycen)
01909 {
01910
01911 const char* name = NULL;
01912 cpl_table* tab = NULL;
01913 int ord_min = 0;
01914 int ord_max = 0;
01915 int ord_avg = 0;
01916 int next = 0;
01917 int status=0;
01918
01919 cpl_table* ext = NULL;
01920
01921 XSH_ASSURE_NOT_NULL_MSG(order_tab_edges,"Null input order tab edges");
01922
01923 name = cpl_frame_get_filename(order_tab_edges);
01924 tab = cpl_table_load(name, 2, 0);
01925
01926 ord_min = cpl_table_get_column_min(tab, "ORDER");
01927 ord_max = cpl_table_get_column_max(tab, "ORDER");
01928 ord_avg = 0.5 * (ord_min + ord_max);
01929
01930 next = cpl_table_and_selected_int(tab, "ORDER", CPL_EQUAL_TO, ord_avg);
01931 ext = cpl_table_extract_selected(tab);
01932 xsh_free_table(&tab);
01933
01934 *xcen = (int) cpl_table_get_double(ext, "CENTER_X", next / 2, &status);
01935 *xcen /= instrument->binx;
01936
01937 *ycen = (int) cpl_table_get_double(ext, "CENTER_Y", next / 2, &status);
01938 *ycen /= instrument->biny;
01939
01940 cleanup:
01941 xsh_free_table(&tab);
01942 xsh_free_table(&ext);
01943 return cpl_error_get_code();
01944 }
01945
01946 static cpl_error_code xsh_frame_image_get_step(cpl_frame* frm_ima,
01947 const int xcen, const int ycen, const int direction, double* dif) {
01948
01949 int xmin = 0;
01950 int xmax = 0;
01951
01952 int ymin = 0;
01953 int ymax = 0;
01954
01955 const char* name = NULL;
01956 cpl_image* map_ima = NULL;
01957 int nx = 0;
01958 double* map = NULL;
01959 double fmin=0;
01960 double fmax=0;
01961
01962 name = cpl_frame_get_filename(frm_ima);
01963 map_ima = cpl_image_load(name, CPL_TYPE_DOUBLE, 0, 0);
01964 nx = cpl_image_get_size_x(map_ima);
01965 map = cpl_image_get_data_double(map_ima);
01966
01967
01968 xmin = xcen - 1;
01969 xmax = xcen + 1;
01970
01971 ymin = ycen - 1;
01972 ymax = ycen + 1;
01973
01974
01975 if(1==direction) {
01976 ymin = ycen - 1;
01977 ymax = ycen + 1;
01978
01979 fmin = map[nx * ymin + xcen];
01980 fmax = map[nx * ymax + xcen];
01981 } else {
01982 xmin = xcen - 1;
01983 xmax = xcen + 1;
01984
01985 fmin = map[nx * ycen + xmin];
01986 fmax = map[nx * ycen + xmax];
01987 }
01988
01989 *dif = fabs(0.5 * (fmax - fmin));
01990
01991 xsh_free_image(&map_ima);
01992
01993 return cpl_error_get_code();
01994
01995 }
01996
01997
01998 static cpl_error_code xsh_plist_set_spat_accuracy(cpl_propertylist* header,
01999 const double srms, const double serr, const double sys_err) {
02000 cpl_propertylist_insert_after_double(header, XSH_CUNIT1,XSH_SPATRMS, srms);
02001 cpl_propertylist_set_comment(header, XSH_SPATRMS,
02002 XSH_SPATRMS_C);
02003 cpl_propertylist_insert_after_double(header, XSH_SPATRMS, XSH_CRDER2, serr);
02004 cpl_propertylist_set_comment(header, XSH_CRDER2, XSH_CRDER2_C);
02005 cpl_propertylist_insert_after_double(header, XSH_CRDER2, XSH_CSYER2, sys_err);
02006 cpl_propertylist_set_comment(header, XSH_CSYER2, XSH_CSYER2_C);
02007 cpl_propertylist_insert_after_string(header, XSH_CSYER2, XSH_CUNIT2, "arcsec");
02008 cpl_propertylist_set_comment(header, XSH_CUNIT2, XSH_CUNIT2_C);
02009
02010 return cpl_error_get_code();
02011 }
02012
02013 static cpl_error_code xsh_plist_set_wave_accuracy(cpl_propertylist* header,
02014 const double wrms, const double werr, const double sys_err) {
02015
02016 cpl_propertylist_insert_after_double(header, XSH_LAMNLIN, XSH_LAMRMS, wrms);
02017 cpl_propertylist_set_comment(header, XSH_LAMRMS, XSH_LAMRMS_C);
02018 cpl_propertylist_insert_after_double(header, XSH_LAMRMS, XSH_CRDER1, werr);
02019 cpl_propertylist_set_comment(header, XSH_CRDER1, XSH_CRDER1_C);
02020 cpl_propertylist_insert_after_double(header, XSH_CRDER1, XSH_CSYER1, sys_err);
02021 cpl_propertylist_set_comment(header, XSH_CSYER1, XSH_CSYER1_C);
02022 cpl_propertylist_insert_after_string(header, XSH_CSYER1, XSH_CUNIT1, "nm");
02023 cpl_propertylist_set_comment(header, XSH_CUNIT1, XSH_CUNIT1_C);
02024
02025 return cpl_error_get_code();
02026 }
02027
02028 cpl_error_code
02029 xsh_compute_resampling_accuracy(cpl_frame* wavemap,
02030 cpl_frame* slitmap,
02031 cpl_frame* order_tab_edges,
02032 cpl_frame* model_config,
02033 cpl_frame* science,
02034 xsh_instrument* instrument) {
02035
02036 cpl_propertylist* plist=NULL;
02037 const char* name=NULL;
02038 const char* tag=NULL;
02039
02040 double rms_x=0;
02041 double rms_y=0;
02042 cpl_table* tab=NULL;
02043 cpl_table* ext=NULL;
02044
02045 xsh_pre* pre=NULL;
02046
02047 int xcen=0;
02048 int ycen=0;
02049
02050 double wdif;
02051 double sdif;
02052
02053 cpl_image* wmap_ima=NULL;
02054 cpl_image* smap_ima=NULL;
02055 cpl_frame* frm_tmp=NULL;
02056 xsh_spectrum* s=NULL;
02057 cpl_image* ima=NULL;
02058 cpl_propertylist* phead=NULL;
02059 cpl_propertylist* xhead=NULL;
02060 cpl_image* xima=NULL;
02061
02062 double wrms=0;
02063 double srms=0;
02064 double werr=0;
02065 double serr=0;
02066 int nline=1;
02067 int nbext=0;
02068
02069 XSH_ASSURE_NOT_NULL_MSG(wavemap,"Null input wavemap");
02070 XSH_ASSURE_NOT_NULL_MSG(slitmap,"Null input wavemap");
02071 XSH_ASSURE_NOT_NULL_MSG(order_tab_edges,"Null input order tab edges");
02072 XSH_ASSURE_NOT_NULL_MSG(model_config,"Null input model config");
02073 XSH_ASSURE_NOT_NULL_MSG(science,"Null input sci frame");
02074 XSH_ASSURE_NOT_NULL_MSG(instrument,"Null input instrument structure");
02075
02076 check(xsh_get_central_xy(order_tab_edges,instrument,&xcen, &ycen));
02077 check(xsh_frame_image_get_step(wavemap,xcen,ycen,1,&wdif));
02078 check(xsh_frame_image_get_step(slitmap,xcen,ycen,0,&sdif));
02079
02080
02081 name=cpl_frame_get_filename(model_config);
02082 plist=cpl_propertylist_load(name,0);
02083 if(cpl_propertylist_has(plist,XSH_QC_MODEL_ANNEAL_RESX_RMS) ){
02084 check(rms_x=cpl_propertylist_get_double(plist,XSH_QC_MODEL_ANNEAL_RESX_RMS));
02085 } else {
02086 xsh_msg_warning("Key %s not found %s, %s will be set to 0",
02087 XSH_QC_MODEL_ANNEAL_RESX_RMS,XSH_LAMRMS,XSH_CRDER1);
02088 }
02089
02090 if(cpl_propertylist_has(plist,XSH_QC_MODEL_ANNEAL_RESY_RMS) ){
02091 check(rms_y=cpl_propertylist_get_double(plist,XSH_QC_MODEL_ANNEAL_RESY_RMS));
02092 } else {
02093 xsh_msg_warning("Key %s not found %s, %s will be set to 0",
02094 XSH_QC_MODEL_ANNEAL_RESY_RMS,XSH_SPATRMS,XSH_CRDER2);
02095 }
02096
02097 if(cpl_propertylist_has(plist,XSH_QC_MODEL_NDAT) ){
02098 nline=cpl_propertylist_get_int(plist,XSH_QC_MODEL_NDAT);
02099 }
02100 xsh_free_propertylist(&plist);
02101
02102 srms=rms_x*sdif;
02103 wrms=rms_y*wdif;
02104 serr=srms/sqrt(nline);
02105 werr=wrms/sqrt(nline);
02106 name=cpl_frame_get_filename(science);
02107 tag=cpl_frame_get_tag(science);
02108 xsh_msg("process frame %s %s",name,tag);
02109 xsh_msg("wdif=%g wrms=%g werr=%g sdif=%g srms=%g serr=%g",wdif,wrms,werr,sdif,srms,serr);
02110
02111 if(strstr(tag,"MERGE2D")!=NULL) {
02112 xsh_msg("MERGE2D");
02113 check(s=xsh_spectrum_load(science));
02114
02115 cpl_propertylist_insert_after_int(s->flux_header,"DATE",XSH_LAMNLIN,nline);
02116 cpl_propertylist_set_comment(s->flux_header,XSH_LAMNLIN,XSH_LAMNLIN_C);
02117
02118 check(xsh_plist_set_wave_accuracy(s->flux_header,wrms,werr,-1.));
02119 check(xsh_plist_set_spat_accuracy(s->flux_header,srms,serr,-1.));
02120
02121
02122 frm_tmp=xsh_spectrum_save(s,name,tag);
02123
02124 } else if(strstr(tag,"ORDER2D")!=NULL) {
02125
02126 xsh_msg("ORDER2D");
02127
02128
02129 int nbext=0;
02130 int i=0;
02131 char cmd[256];
02132 const char *tname = "tmp_ima.fits";
02133
02134
02135 nbext = cpl_frame_get_nextensions( science);
02136 check(phead = cpl_propertylist_load( name, 0 ));
02137 ima=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
02138
02139 sprintf(cmd,"mv %s %s",name,tname);
02140 system(cmd);
02141 xsh_add_temporary_file("tmp_ima.fits");
02142
02143 cpl_propertylist_insert_after_int(phead,"DATE",XSH_LAMNLIN,nline);
02144 cpl_propertylist_set_comment(phead,XSH_LAMNLIN,XSH_LAMNLIN_C);
02145 check(xsh_plist_set_wave_accuracy(phead,wrms,werr,-1.));
02146 check(xsh_plist_set_spat_accuracy(phead,srms,serr,-1.));
02147
02148
02149 check(cpl_image_save(ima, name, CPL_BPP_IEEE_FLOAT,
02150 phead,CPL_IO_DEFAULT));
02151 for( i = 0 ; i<=nbext ; i++ ) {
02152 int extension ;
02153 xsh_free_image( &xima) ;
02154 xsh_free_propertylist( &xhead) ;
02155 check(xima = cpl_image_load( tname, CPL_TYPE_FLOAT, 0,i ));
02156 check(xhead = cpl_propertylist_load( tname ,i));
02157 if ( i == 0 ) {
02158 extension = CPL_IO_DEFAULT ;
02159 check(cpl_image_save(xima, name, CPL_BPP_IEEE_FLOAT,
02160 phead,extension));
02161 }
02162 else {
02163 extension = CPL_IO_EXTEND ;
02164 check(cpl_image_save(xima, name, CPL_BPP_IEEE_FLOAT,xhead,extension));
02165 }
02166 }
02167 sprintf(cmd,"rm tmp_ima.fits");
02168 system(cmd);
02169 }
02170 xsh_msg("processed frame %s",tag);
02171
02172 cleanup:
02173
02174 xsh_free_image( &xima) ;
02175 xsh_free_image( &ima) ;
02176 xsh_free_propertylist( &xhead) ;
02177 xsh_free_propertylist( &phead) ;
02178 xsh_free_frame(&frm_tmp);
02179 xsh_pre_free(&pre);
02180 xsh_free_table(&tab);
02181 xsh_free_table(&ext);
02182 xsh_free_image(&wmap_ima);
02183 xsh_free_image(&smap_ima);
02184 xsh_free_propertylist(&plist);
02185 xsh_spectrum_free(&s);
02186
02187 return cpl_error_get_code();
02188
02189 }
02190
02191
02192 cpl_error_code
02193 xsh_compute_wavelength_resampling_accuracy(cpl_frame* wavemap,
02194 cpl_frame* order_tab_edges,
02195 cpl_frame* model_config,
02196 cpl_frame* science,
02197 xsh_instrument* instrument) {
02198
02199 cpl_propertylist* plist=NULL;
02200 cpl_frame* frm_tmp=NULL;
02201 const char* name=NULL;
02202 const char* tag=NULL;
02203
02204 double rms_y=0;
02205 cpl_table* tab=NULL;
02206 cpl_table* ext=NULL;
02207
02208 xsh_pre* pre=NULL;
02209 xsh_rec_list * rec_list = NULL ;
02210 int xcen=0;
02211 int ycen=0;
02212
02213 double wdif;
02214
02215 cpl_image* wmap_ima=NULL;
02216 xsh_spectrum* s=NULL;
02217 cpl_image* ima=NULL;
02218 double wrms=0;
02219 double werr=0;
02220 int nline=1;
02221 cpl_propertylist* xhead=NULL;
02222 cpl_image* xima=NULL;
02223 cpl_propertylist* phead=NULL;
02224
02225 XSH_ASSURE_NOT_NULL_MSG(wavemap,"Null input wavemap");
02226 XSH_ASSURE_NOT_NULL_MSG(order_tab_edges,"Null input order tab edges");
02227 XSH_ASSURE_NOT_NULL_MSG(model_config,"Null input model config");
02228 XSH_ASSURE_NOT_NULL_MSG(science,"Null input sci frame");
02229 XSH_ASSURE_NOT_NULL_MSG(instrument,"Null input instrument structure");
02230
02231 check(xsh_get_central_xy(order_tab_edges,instrument,&xcen, &ycen));
02232 check(xsh_frame_image_get_step(wavemap,xcen,ycen,1,&wdif));
02233
02234 name=cpl_frame_get_filename(model_config);
02235 plist=cpl_propertylist_load(name,0);
02236
02237 if (cpl_propertylist_has(plist, XSH_QC_MODEL_ANNEAL_RESY_RMS)) {
02238 check(
02239 rms_y = cpl_propertylist_get_double(plist,
02240 XSH_QC_MODEL_ANNEAL_RESY_RMS));
02241 } else {
02242 xsh_msg_warning("Key %s not found %s, %s will be set to 0",
02243 XSH_QC_MODEL_ANNEAL_RESY_RMS, XSH_SPATRMS, XSH_CRDER2);
02244 }
02245
02246 if(cpl_propertylist_has(plist,XSH_QC_MODEL_NDAT) ){
02247 nline=cpl_propertylist_get_int(plist,XSH_QC_MODEL_NDAT);
02248 }
02249 xsh_free_propertylist(&plist);
02250
02251 wrms=rms_y*wdif;
02252 werr=wrms/sqrt(nline);
02253 name=cpl_frame_get_filename(science);
02254 tag=cpl_frame_get_tag(science);
02255
02256 xsh_msg("process frame %s %s",name,tag);
02257 if(strstr(tag,"MERGE1D")!=NULL) {
02258 xsh_msg("MERGE1D");
02259 check(s=xsh_spectrum_load(science));
02260 cpl_propertylist_insert_after_int(s->flux_header,"DATE",XSH_LAMNLIN,nline);
02261 cpl_propertylist_set_comment(s->flux_header,XSH_LAMNLIN,XSH_LAMNLIN_C);
02262 check(xsh_plist_set_wave_accuracy(s->flux_header,wrms,werr,-1.));
02263
02264
02265 frm_tmp=xsh_spectrum_save(s,name,tag);
02266
02267 } else if( (strstr(tag,"ORDER1D")!=NULL) ||
02268 (strstr(tag,"SKY_ORD1D")!=NULL) ) {
02269 xsh_msg("ORDER1D");
02270
02271
02272 int nbext=0;
02273 int i=0;
02274 char cmd[256];
02275 const char *tname = "tmp_ima.fits";
02276
02277
02278 nbext = cpl_frame_get_nextensions( science);
02279 check(phead = cpl_propertylist_load( name, 0 ));
02280 ima=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
02281
02282 sprintf(cmd,"mv %s %s",name,tname);
02283 system(cmd);
02284 xsh_add_temporary_file("tmp_ima.fits");
02285 cpl_propertylist_insert_after_int(phead,"DATE",XSH_LAMNLIN,nline);
02286 cpl_propertylist_set_comment(phead,XSH_LAMNLIN,XSH_LAMNLIN_C);
02287 check(xsh_plist_set_wave_accuracy(phead,wrms,werr,-1.));
02288
02289
02290 check(cpl_image_save(ima, name, CPL_BPP_IEEE_FLOAT,
02291 phead,CPL_IO_DEFAULT));
02292 for( i = 0 ; i<=nbext ; i++ ) {
02293 int extension ;
02294 xsh_free_image( &xima) ;
02295 xsh_free_propertylist( &xhead) ;
02296 check(xima = cpl_image_load( tname, CPL_TYPE_FLOAT, 0,i ));
02297 check(xhead = cpl_propertylist_load( tname ,i));
02298 if ( i == 0 ) {
02299 extension = CPL_IO_DEFAULT ;
02300 check(cpl_image_save(xima, name, CPL_BPP_IEEE_FLOAT,
02301 phead,extension));
02302 }
02303 else {
02304 extension = CPL_IO_EXTEND ;
02305 check(cpl_image_save(xima, name, CPL_BPP_IEEE_FLOAT,xhead,extension));
02306 }
02307 }
02308 sprintf(cmd,"rm tmp_ima.fits");
02309 system(cmd);
02310
02311 } else if(strstr(tag,"SKY_ORD1D")!=NULL) {
02312 xsh_msg("SKY_ORD1D");
02313
02314 }
02315 xsh_msg("processed frame %s",tag);
02316 cleanup:
02317 xsh_free_frame(&frm_tmp);
02318 xsh_free_propertylist( &phead) ;
02319 xsh_free_propertylist( &xhead) ;
02320 xsh_free_image( &xima) ;
02321 xsh_free_image( &ima) ;
02322 xsh_pre_free(&pre);
02323 xsh_free_table(&tab);
02324 xsh_free_table(&ext);
02325 xsh_free_image(&wmap_ima);
02326 xsh_free_propertylist(&plist);
02327 xsh_spectrum_free(&s);
02328 return cpl_error_get_code();
02329
02330 }
02331
02332
02333
02347
02348 cpl_frameset*
02349 xsh_frameset_crh_single(cpl_frameset* raws,xsh_remove_crh_single_param * crh_single_par,
02350 xsh_instrument* instrument,const char* prefix,const char* spec)
02351 {
02352
02353 cpl_frameset* clean=NULL;
02354 cpl_frame* frm=NULL;
02355 int nraws=0;
02356 int i=0;
02357 char ftag[256];
02358 char fname[256];
02359 char arm_str[16] ;
02360 cpl_frame * rm_crh = NULL ;;
02361
02362 sprintf( arm_str, "%s", xsh_instrument_arm_tostring(instrument) ) ;
02363 nraws=cpl_frameset_get_size(raws);
02364 check( clean = cpl_frameset_new() ) ;
02365 xsh_msg( "Remove crh (single frame)" ) ;
02366 if ( crh_single_par->nb_iter > 0 ) {
02367 xsh_msg( "removecrhsingle_niter > 0" ) ;
02368 for ( i = 0 ; i < nraws ; i++ ) {
02369
02370 frm = cpl_frameset_get_frame( raws, i ) ;
02371 sprintf(ftag,"%s_%s_NO_CRH_%s_%d",prefix,spec,arm_str,i) ;
02372 sprintf(fname,"%s.fits",ftag) ;
02373
02374 rm_crh = xsh_remove_crh_single( frm,instrument,crh_single_par,ftag ) ;
02375 xsh_add_temporary_file(fname);
02376 cpl_frameset_insert( clean, rm_crh ) ;
02377 }
02378 } else {
02379 clean=cpl_frameset_duplicate(raws);
02380 }
02381
02382 cleanup:
02383 return clean;
02384 }
02385
02386
02387
02401
02402 cpl_frameset*
02403 xsh_frameset_mflat_divide(cpl_frameset* input, cpl_frame* mflat,
02404 xsh_instrument* instrument) {
02405
02406 int nraw = 0;
02407 int i = 0;
02408
02409 cpl_frame * divided = NULL;
02410 cpl_frame * frm = NULL;
02411 char arm_str[16] ;
02412 char ftag[256];
02413 char fname[256];
02414
02415 cpl_frameset* output = NULL;
02416
02417 xsh_msg("apply flat field");
02418
02419 nraw = cpl_frameset_get_size(input);
02420 output = cpl_frameset_new();
02421 sprintf( arm_str, "%s", xsh_instrument_arm_tostring(instrument) ) ;
02422 for (i = 0; i < nraw; i++) {
02423 frm = cpl_frameset_get_frame(input, i);
02424
02425 sprintf(ftag, "FF_%d_SLIT_OFFSET_%s", i,arm_str);
02426 sprintf(fname, "%s.fits", ftag);
02427
02428 divided = xsh_divide_flat( frm,mflat, ftag, instrument );
02429 xsh_add_temporary_file(fname);
02430 cpl_frameset_insert(output, divided );
02431 xsh_free_frame(&frm);
02432
02433 }
02434
02435 return output;
02436 }
02437