00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "prepare_stacked_frames.h"
00020 #include "new_stack_ini_by_cpl.h"
00021 #include "sinfoni_pro_save.h"
00022 #include "eclipse.h"
00023 #include "spiffi.h"
00024 #include "utilities.h"
00025 #include "sinfoni_dfs.h"
00026 #include "sinfoni_raw_types.h"
00027 #include "sinfoni_wcal_functions.h"
00028 #include <sinfoni_memory.h>
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 int get_names(const char* pcatg, const int ind, stack_config_n ** cfg);
00050
00051 int get_names(const char* pcatg, const int ind, stack_config_n ** cfg){
00052
00053 if (strcmp(pcatg,PRO_FIBRE_NS_STACKED_OFF) == 0) {
00054 strcpy((*cfg)->outName,"out_ns_stack_off.fits");
00055 }
00056 if (strcmp(pcatg,PRO_FIBRE_NS_STACKED_ON) == 0) {
00057 strcpy((*cfg)->outName,"out_ns_stack_on.fits");
00058 }
00059 if (strcmp(pcatg,PRO_FIBRE_NS_STACKED) == 0) {
00060 strcpy((*cfg)->outName,"out_ns_stack.fits");
00061 }
00062 if (strcmp(pcatg,PRO_WAVE_LAMP_STACKED) == 0) {
00063 strcpy((*cfg)->outName,"out_wcal_stack.fits");
00064 }
00065 if (strcmp(pcatg,PRO_FIBRE_NS_STACKED_DIST)== 0) {
00066 strcpy((*cfg)->outName,"out_ns_stack_warp.fits");
00067 }
00068 if (strcmp(pcatg,PRO_WAVE_SLITPOS_STACKED) == 0) {
00069 strcpy((*cfg)->outName,"out_slit_pos_stack.fits");
00070 }
00071
00072 if (strcmp(pcatg,PRO_PSF_CALIBRATOR_STACKED)== 0) {
00073 sprintf((*cfg)->outName,"%s%d%s","out_stack",ind,".fits");
00074 }
00075 if (strcmp(pcatg,PRO_SKY_PSF_CALIBRATOR_STACKED)== 0) {
00076 strcpy((*cfg)->outName,STACKED_OUT_FILENAME);
00077 }
00078 if (strcmp(pcatg,PRO_STD_NODDING_STACKED) == 0) {
00079 sprintf((*cfg)->outName,"%s%d%s","out_stack",ind,".fits");
00080 }
00081
00082
00083
00084
00085
00086
00087 if (strcmp(pcatg,PRO_SKY_NODDING_STACKED) == 0) {
00088 strcpy((*cfg)->outName,STACKED_OUT_FILENAME);
00089 }
00090 if (strcmp(pcatg,PRO_OBJECT_NODDING_STACKED) == 0) {
00091 sprintf((*cfg)->outName,"%s%d%s","out_stack",ind,".fits");
00092 }
00093 if (strcmp(pcatg,PRO_STACKED) == 0) {
00094 sprintf((*cfg)->outName,"%s%d%s","out_stack",ind,".fits");
00095 }
00096
00097 sprintf((*cfg)->sky_name,"%s%d%s","out_sky",ind,".fits");
00098
00099 return 0;
00100
00101 }
00102
00103
00104 int prepare_stacked_frames (const char* plugin_id,cpl_parameterlist* config,
00105 cpl_frameset* sof, const char* frm_pro_ctg,
00106 const int frm_ind, fake* fk)
00107 {
00108
00109
00110 const char* _id = "prepared_stacked_frames";
00111 stack_config_n * cfg =NULL;
00112 OneImage ** list_object ;
00113 OneImage ** list_dither_object;
00114 OneImage ** list_dither_sky;
00115 OneImage ** list_sky;
00116 OneImage ** list_dark;
00117
00118
00119
00120
00121 fits_header ** list_header ;
00122 fits_header ** list_head ;
00123
00124
00125
00126
00127 Lookup* lookup=NULL;
00128 OneImage * im3=NULL ;
00129 OneImage * im4=NULL ;
00130 OneImage * im5=NULL ;
00131 OneImage * im6=NULL ;
00132 OneImage * im7=NULL ;
00133 OneImage * im8=NULL ;
00134 OneImage * im9=NULL ;
00135
00136
00137
00138
00139 OneImage * ref_im1=NULL ;
00140 OneImage * ref_im2=NULL ;
00141 OneImage ** im ;
00142 OneImage * im_obj=NULL ;
00143 OneImage* simg=NULL;
00144
00145 OneImage * im_dark=NULL ;
00146 OneImage * im_sky=NULL ;
00147 OneImage * im_dither=NULL ;
00148 OneImage * im_dither_sky=NULL ;
00149 OneImage * im_obj_sub=NULL ;
00150 OneImage * im_obj_flat=NULL ;
00151 OneImage * im_dither_sub=NULL ;
00152 OneImage * im_dither_flat=NULL ;
00153 OneImage * int_im_shifted=NULL ;
00154 OneImage * int_im_dith_shifted=NULL ;
00155 OneImage * im_conv=NULL ;
00156
00157
00158 OneImage * mask_im=NULL ;
00159 OneImage * flat1=NULL ;
00160 OneImage * flat2=NULL ;
00161 OneImage * int_im=NULL ;
00162 OneImage * int_im_dith=NULL ;
00163 OneImage * sky_img_flat=NULL;
00164 OneImage * sky_dist=NULL;
00165
00166
00167
00168 OneCube * cube_object=NULL ;
00169 OneCube * cube_dither_object=NULL ;
00170 OneCube * cube_sky=NULL ;
00171
00172
00173
00174 OneCube * cube_object_tmp=NULL ;
00175 OneCube * cube_sky_tmp=NULL ;
00176 OneCube * cube_dither_object_tmp=NULL ;
00177 OneCube * cube_dither_sky=NULL ;
00178 OneCube * cube_dither_sky_tmp=NULL ;
00179
00180 OneCube * cube_dark=NULL ;
00181 OneCube * iCube=NULL ;
00182 OneCube * jCube=NULL ;
00183 OneImage * X=NULL ;
00184 OneImage * hX=NULL ;
00185 OneImage * Y=NULL ;
00186 OneImage * Z=NULL ;
00187 cpl_table * qclog_tbl=NULL;
00188 char* key_value=NULL;
00189 cpl_image* sky_img=NULL;
00190
00191 char* name;
00192 int typ=0;
00193 int pos=0;
00194 int i = 0;
00195 int n = 0;
00196 int cnt = 0 ;
00197 float val_x=0;
00198 float val_y=0;
00199 int status=0;
00200
00201 float** slit_edges;
00202 char** in_nam;
00203
00204 int nob = 0;
00205 int nsky = 0;
00206 int nobjdith = 0;
00207 int nskydith = 0;
00208 int nda = 0;
00209 char* name_list;
00210
00211
00212 OneImage* flat1_dist=NULL;
00213 OneImage* flat2_dist=NULL;
00214 cpl_image* img=NULL;
00215 cpl_image* imgw=NULL;
00216 cpl_frameset* raw=NULL;
00217
00218 char* tbl_index_name=NULL;
00219 char* tbl_slitpos_name=NULL;
00220 cpl_table* tbl_index = NULL;
00221 cpl_table* tbl_slitpos=NULL;
00222 int rhead=0;
00223
00224 cpl_frame* sky_frame = NULL;
00225 char* sky_name = NULL;
00226 char* sky_tag = NULL;
00227 struct qc_wcal qc;
00228
00229
00230
00231
00232
00233
00234
00235 raw=cpl_frameset_new();
00236 cfg = parse_cpl_input_stack(config,sof,&raw, fk) ;
00237 if (cfg == NULL)
00238 {
00239 cpl_msg_error (_id,"could not parse cpl input file!") ;
00240 cpl_frameset_delete(raw);
00241 return -1 ;
00242 }
00243
00244 det_ncounts(raw, cfg->qc_thresh_max,&qc);
00245
00246
00247
00248 get_names(frm_pro_ctg, frm_ind, &cfg);
00249
00250
00251
00252 if (cfg->flatInd == 1){
00253 if(is_fits_file(cfg->flatfield1) != 1) {
00254 cpl_msg_error(_id,"Input FF file %s is not FITS",cfg->flatfield1);
00255 stack_free(cfg);
00256 cpl_frameset_delete(raw);
00257 return -1;
00258 }
00259 }
00260 if (cfg->maskInd == 1) {
00261 if(is_fits_file(cfg->mask) != 1) {
00262 cpl_msg_error(_id,"Input mask file %s is not FITS",cfg->mask);
00263 stack_free(cfg);
00264 cpl_frameset_delete(raw);
00265 return -1;
00266 }
00267 if(cfg -> indind == 0) {
00268 if(is_fits_file(cfg->slitposList) != 1) {
00269 cpl_msg_error(_id,"Input slitpos file %s is not FITS",cfg->slitposList);
00270 stack_free(cfg);
00271 cpl_frameset_delete(raw);
00272 return -1;
00273 }
00274 }
00275 }
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 if (cfg->sfInd == 1){
00296 if (cfg->contains_dark == 0) {
00297 cpl_msg_warning(_id,"no dark frames given!");
00298 }
00299 if (cfg->contains_ref == 0) {
00300 cpl_msg_error(_id,"no reference frames given!");
00301 stack_free(cfg);
00302 cpl_frameset_delete(raw);
00303 return -1;
00304 }
00305 }
00306
00307
00308 list_object = new_image_list (cfg->nobj);
00309 if (list_object == NULL) {
00310 cpl_msg_error(_id,"could not allocate memory for object frame");
00311 stack_free(cfg);
00312 cpl_frameset_delete(raw);
00313 return -1;
00314 }
00315
00316
00317 if (cfg->contains_dither == 1) {
00318 list_dither_object = new_image_list(cfg->nditherobj);
00319 if (list_dither_object == NULL) {
00320 cpl_msg_error(_id,"could not allocate memory for dither object frame");
00321 stack_free(cfg);
00322 cpl_frameset_delete(raw);
00323 return -1;
00324 }
00325 }
00326
00327
00328 if (cfg->contains_sky == 1) {
00329 list_sky = new_image_list(cfg->noff);
00330 if (list_sky == NULL) {
00331 cpl_msg_error(_id,"could not allocate memory for off frame list");
00332 stack_free(cfg);
00333 cpl_frameset_delete(raw);
00334 return -1;
00335 }
00336 }
00337
00338 if (cfg->contains_dither == 1 && cfg->nditheroff > 0) {
00339 list_dither_sky = new_image_list(cfg->nditheroff);
00340 if (list_dither_sky == NULL) {
00341 cpl_msg_error(_id,"could not allocate memory for dither frame list");
00342 stack_free(cfg);
00343 cpl_frameset_delete(raw);
00344 return -1;
00345 }
00346 }
00347
00348
00349
00350 if (cfg->contains_dark == 1 && cfg->sfInd == 1) {
00351 list_dark = new_image_list(cfg->ndark);
00352 if (list_dark == NULL) {
00353 cpl_msg_error(_id,"could not allocate memory for dark frame");
00354 stack_free(cfg);
00355 cpl_frameset_delete(raw);
00356 return -1;
00357 }
00358 }
00359
00360 if (cfg->contains_dither == 0 && cfg->nditheroff > 0) {
00361 cpl_msg_error(_id,"please use non-dithered off-frames, remove the 2!");
00362 stack_free(cfg);
00363 cpl_frameset_delete(raw);
00364 return -1;
00365 }
00366
00367
00368
00369
00370 im = (OneImage**) cpl_calloc (cfg -> nframes, sizeof(OneImage*)) ;
00371 list_head = (fits_header**) cpl_calloc (cfg->nframes, sizeof(fits_header*)) ;
00372 list_header = (fits_header**) cpl_calloc (cfg->nframes, sizeof(fits_header*)) ;
00373
00374
00375
00376 for (i=0; i< cfg->nframes; i++) {
00377 name = cfg->framelist[i];
00378 if(is_fits_file(name) != 1) {
00379 cpl_msg_error(_id,"Input file %s is not FITS",name);
00380 return -1;
00381 }
00382 im[i] = load_image( name );
00383
00384
00385 }
00386
00387
00388 rhead=0;
00389 for (i=0; i< cfg->nframes; i++) {
00390 typ = intarray_get_value( cfg->frametype, i );
00391 pos = intarray_get_value( cfg->frameposition, i );
00392 if (im[i] == NULL) {
00393 cpl_msg_error(_id,"could not load image");
00394 return -1;
00395 }
00396 if (pos == 2) {
00397 if (typ == 1) {
00398 insert_image( list_object, im[i], nob );
00399
00400 if (rhead==0) {
00401 list_header[0]=fits_read_header(cfg->framelist[i]);
00402 rhead=1;
00403 }
00404 nob = nob + 1;
00405 }
00406 else if ( typ == 0 ) {
00407 insert_image( list_sky, im[i], nsky );
00408 nsky = nsky + 1;
00409 sky_frame = cpl_frameset_get_frame(raw,i);
00410 sky_name = (char*) cpl_frame_get_filename(sky_frame);
00411 sky_tag = (char*) cpl_frame_get_tag(sky_frame);
00412 if ( (strstr(frm_pro_ctg,"OBJECT") != NULL) ||
00413 (strstr(frm_pro_ctg,"PSF") != NULL) ||
00414 (strstr(frm_pro_ctg,"STD") != NULL) ) {
00415 sky_img = cpl_image_load(sky_name,CPL_TYPE_FLOAT,0,0);
00416 sprintf(sky_name,"%s%2.2d%s","out_sky",frm_ind,".fits");
00417 if(-1 == sinfoni_pro_dump_ima(sky_img,raw,sof,sky_name,
00418 PRO_SKY_STACKED_DUMMY,plugin_id,config)) {
00419 cpl_msg_error(_id,"cannot dump sky ima %s", sky_name);
00420 cpl_image_delete(sky_img);
00421 destroy_list(list_object);
00422 destroy_list(list_sky);
00423
00424 for (i=0; i< cfg->nframes; i++) {
00425 if(list_head[i]) fits_header_destroy(list_head[i]);
00426 }
00427 cpl_free(list_head);
00428
00429 for (i=0; i< cfg->nframes; i++) {
00430 if(list_header[i]) fits_header_destroy(list_header[i]);
00431 }
00432 cpl_free(list_header);
00433
00434 for (i=0; i< cfg->nframes; i++) {
00435 destroy_image(im[i]);
00436 }
00437 cpl_free(im);
00438
00439 stack_free(cfg);
00440 cpl_frameset_delete(raw);
00441
00442
00443 return -1;
00444 } else {
00445 cpl_msg_info(_id,"Saving frame ID %s",PRO_SKY_DUMMY);
00446 }
00447 cpl_image_delete(sky_img);
00448
00449 if (cfg->flatInd == 1) {
00450 cpl_msg_info(_id,"Sky Flatfielding");
00451 flat1 = load_image (cfg->flatfield1);
00452 if (flat1 == NULL) {
00453 cpl_msg_error(_id,"could not load flatfield image" );
00454 return -1;
00455 }
00456
00457 simg = load_image(sky_name);
00458 sky_img_flat = divImagesRobust(simg, flat1);
00459 destroy_image(simg);
00460 destroy_image(flat1);
00461 if (sky_img_flat == NULL) {
00462 cpl_msg_error(_id,"could not carry out flatfield division" );
00463 return -1;
00464 }
00465 if (frm_ind == 0) {
00466 if (cfg->warpfixInd == 1){
00467 cpl_msg_info(_id,"correct FF for distortions");
00468 sky_dist = image_warp_fits(sky_img_flat, cfg->kernel, cfg->polyFile);
00469
00470 imgw=cpl_image_wrap_float(sky_dist->lx,sky_dist->ly,sky_dist->data);
00471 img=cpl_image_duplicate(imgw);
00472 cpl_image_unwrap(imgw);
00473
00474 if(-1 == sinfoni_pro_dump_ima(img,raw,sof,
00475 STACK_SKY_DIST_OUT_FILENAME,
00476 PRO_STACK_SKY_DIST,plugin_id,
00477 config)) {
00478 cpl_msg_error(_id,"cannot dump ima %s",
00479 STACK_SKY_DIST_OUT_FILENAME);
00480
00481 destroy_list(list_object);
00482 destroy_list(list_sky);
00483
00484 for (i=0; i< cfg->nframes; i++) {
00485 if(list_head[i]) fits_header_destroy(list_head[i]);
00486 }
00487 cpl_free(list_head);
00488
00489 for (i=0; i< cfg->nframes; i++) {
00490 if(list_header[i]) fits_header_destroy(list_header[i]);
00491 }
00492 cpl_free(list_header);
00493
00494 for (i=0; i< cfg->nframes; i++) {
00495 destroy_image(im[i]);
00496 }
00497 cpl_free(im);
00498
00499
00500 destroy_image(sky_img_flat);
00501
00502 destroy_image(sky_dist);
00503 cpl_image_delete(img);
00504 stack_free(cfg);
00505 cpl_frameset_delete(raw);
00506
00507 return -1;
00508 } else {
00509 cpl_msg_info(_id,"Saving frame ID %s",PRO_STACK_SKY_DIST);
00510 }
00511 destroy_image(sky_dist);
00512 cpl_image_delete(img);
00513 }
00514 }
00515 destroy_image(sky_img_flat);
00516 }
00517 }
00518 }
00519 else if ( typ == 5 ) {
00520
00521 if (cfg->sfInd == 1) {
00522 insert_image( list_dark, im[i], nda );
00523 nda = nda + 1;
00524 } else {
00525 destroy_image(im[i]);
00526 }
00527 }
00528 else if ( typ == 4 ) {
00529 if ( cfg->sfInd == 1) {
00530 ref_im1 = im[i];
00531 }
00532 else {
00533 destroy_image(im[i]);
00534 }
00535 }
00536 }
00537 else {
00538 if (typ == 1) {
00539 insert_image( list_dither_object, im[i], nobjdith );
00540
00541 if (rhead==0) {
00542 list_header[0]=fits_read_header(cfg->framelist[i]);
00543 rhead=1;
00544 }
00545 nobjdith = nobjdith + 1;
00546 }
00547 else if (typ == 0) {
00548 insert_image( list_dither_sky, im[i], nskydith );
00549 nskydith = nskydith + 1;
00550 }
00551 else if (typ == 4) {
00552 if (cfg->sfInd == 1) {
00553 ref_im2 = im[i];
00554 }
00555 else {
00556 destroy_image(im[i]);
00557 }
00558 }
00559 }
00560 }
00561
00562 if (nob != cfg->nobj ||
00563 cfg->noff != nsky ||
00564 nobjdith != cfg->nditherobj ||
00565 nskydith != cfg->nditheroff) {
00566 cpl_msg_error(_id,"something is wrong with the number of the");
00567 cpl_msg_error(_id,"different types of frames");
00568
00569
00570
00571 for (i=0; i< cfg->nframes; i++) {
00572 if(list_head[i]) fits_header_destroy(list_head[i]);
00573 }
00574 cpl_free(list_head);
00575
00576 for (i=0; i< cfg->nframes; i++) {
00577 if(list_header[i]) fits_header_destroy(list_header[i]);
00578 }
00579 cpl_free(list_header);
00580
00581 for (i=0; i< cfg->nframes; i++) {
00582 if(im[i]) destroy_image(im[i]);
00583 }
00584 cpl_free(im);
00585 destroy_list(list_object);
00586 destroy_list(list_sky);
00587 stack_free(cfg);
00588 cpl_frameset_delete(raw);
00589
00590 return -1;
00591 }
00592
00593
00594 if (cfg->sfInd == 1 && nda != cfg->ndark) {
00595 cpl_msg_error(_id,"something is wrong with the number of dark frames");
00596
00597
00598 for (i=0; i< cfg->nframes; i++) {
00599 if(list_head[i]) fits_header_destroy(list_head[i]);
00600 }
00601 cpl_free(list_head);
00602
00603 for (i=0; i< cfg->nframes; i++) {
00604 if(list_header[i]) fits_header_destroy(list_header[i]);
00605 }
00606 cpl_free(list_header);
00607
00608 for (i=0; i< cfg->nframes; i++) {
00609 if(im[i]) destroy_image(im[i]);
00610 }
00611 cpl_free(im);
00612
00613 destroy_list(list_object);
00614 destroy_list(list_sky);
00615 stack_free(cfg);
00616 cpl_frameset_delete(raw);
00617
00618 return -1;
00619 }
00620
00621
00622 cpl_msg_info(_id,"Create and fill cubes with the different image lists");
00623
00624
00625
00626 cube_object = list_make_cube(list_object, nob);
00627
00628
00629 if (cube_object == NULL) {
00630 cpl_msg_error(_id,"could not create object data cube!");
00631
00632
00633 for (i=0; i< cfg->nframes; i++) {
00634 if(list_head[i]) fits_header_destroy(list_head[i]);
00635 }
00636 cpl_free(list_head);
00637
00638 for (i=0; i< cfg->nframes; i++) {
00639 if(list_header[i]) fits_header_destroy(list_header[i]);
00640 }
00641 cpl_free(list_header);
00642
00643 for (i=0; i< cfg->nframes; i++) {
00644 if(im[i]) destroy_image(im[i]);
00645 }
00646 cpl_free(im);
00647
00648 destroy_cube_shallow(cube_object);
00649 destroy_list(list_object);
00650 destroy_list(list_sky);
00651 stack_free(cfg);
00652 cpl_frameset_delete(raw);
00653
00654 return -1;
00655 }
00656 destroy_list(list_object);
00657
00658 if (cfg->contains_dither == 1) {
00659 cube_dither_object = list_make_cube(list_dither_object, nobjdith);
00660 if (cube_dither_object == NULL) {
00661 cpl_msg_error(_id,"could not create dither object data cube!");
00662 return -1;
00663 }
00664 destroy_list(list_dither_object);
00665 }
00666
00667
00668 if (cfg->contains_sky == 1 && nsky > 0) {
00669 cube_sky = list_make_cube(list_sky, nsky);
00670 if (cube_sky == NULL) {
00671 cpl_msg_error (_id,"could not create sky data cube!");
00672 return -1;
00673 }
00674 destroy_list(list_sky);
00675 }
00676
00677 if ((cfg->contains_dither == 1) & (nskydith > 0)) {
00678 cube_dither_sky = list_make_cube(list_dither_sky, nskydith);
00679 if (cube_dither_sky == NULL) {
00680 cpl_msg_error (_id," could not create dither sky data cube!");
00681 return -1;
00682 }
00683 destroy_list(list_dither_sky);
00684 }
00685
00686
00687 if (cfg->contains_dark == 1 && cfg->sfInd == 1 && cfg->ndark > 0) {
00688 cube_dark = list_make_cube(list_dark, cfg->ndark);
00689 if (cube_dark == NULL) {
00690 cpl_msg_error (_id," could not create dark data cube!");
00691 return -1;
00692 }
00693 destroy_list(list_dark);
00694 }
00695
00696
00697
00698
00699
00700
00701
00702 if (cfg->sfInd == 1) {
00703
00704
00705
00706 cpl_msg_info(_id,"shift cube images in spectral direction with respect to reference");
00707 if (cfg->contains_dark == 1) {
00708 cpl_msg_info(_id,"cfg->contains_dark == 1");
00709 if (cfg->loReject*cfg->ndark < 1. && cfg->hiReject * cfg->ndark < 1.) {
00710 im_dark = averageCubeToImage( cube_dark );
00711 if (im_dark == NULL) {
00712 cpl_msg_error (_id, "averageCubeToImage failed" );
00713 destroy_cube(cube_object);
00714 stack_free(cfg);
00715 cpl_frameset_delete(raw);
00716 return -1;
00717 }
00718 }
00719 else {
00720 im_dark = average_with_rejection( cube_dark,
00721 cfg->loReject, cfg->hiReject );
00722 if (im_dark == NULL) {
00723 cpl_msg_error (_id, "average_with_rejection failed" );
00724 return -1;
00725 }
00726 }
00727 destroy_cube(cube_dark);
00728 cube_object_tmp = subImageFromCube (cube_object, im_dark);
00729 if (cube_object == NULL) {
00730 cpl_msg_error(_id, "subImageFromCube failed" );
00731 return -1;
00732 }
00733 }
00734 else {
00735 cpl_msg_info(_id,"cfg->contains_dark == 0");
00736 cube_object_tmp = copy_cube(cube_object);
00737 }
00738 destroy_cube(cube_object);
00739
00740 cube_object = alignCubeToReference (cube_object_tmp, ref_im1,
00741 cfg->sfOrder, cfg->sfType);
00742 if (cube_object == NULL) {
00743 cpl_msg_error(_id, "alignCubeToReference failed" );
00744
00745
00746 for (i=0; i< cfg->nframes; i++) {
00747 if(list_head[i]) fits_header_destroy(list_head[i]);
00748 }
00749 cpl_free(list_head);
00750 for (i=0; i< cfg->nframes; i++) {
00751 if(list_header[i]) fits_header_destroy(list_header[i]);
00752 }
00753 cpl_free(list_header);
00754 cpl_free(im);
00755 destroy_cube(cube_object_tmp);
00756 stack_free(cfg);
00757 cpl_frameset_delete(raw);
00758 return -1;
00759 }
00760 destroy_cube(cube_object_tmp);
00761 if (cfg->contains_dither == 1) {
00762 if (cfg->contains_dark == 1) {
00763 cube_dither_object_tmp = subImageFromCube(cube_dither_object,
00764 im_dark);
00765 if (cube_dither_object == NULL) {
00766 cpl_msg_error(_id, "average_with_rejection failed" );
00767 return -1;
00768 }
00769 }
00770 else {
00771 cube_dither_object_tmp = copy_cube(cube_dither_object);
00772 }
00773 destroy_cube(cube_dither_object);
00774
00775 cube_dither_object = alignCubeToReference (cube_dither_object_tmp,
00776 ref_im2,
00777 cfg->sfOrder,
00778 cfg->sfType);
00779 if (cube_dither_object == NULL) {
00780 cpl_msg_error(_id, "alignCubeToReference failed" );
00781 return -1;
00782 }
00783 destroy_cube(cube_dither_object_tmp);
00784 }
00785 if (cfg->contains_sky == 1) {
00786 if (cfg->contains_dark == 1) {
00787 cube_sky_tmp = subImageFromCube (cube_sky, im_dark);
00788 if (cube_sky == NULL) {
00789 cpl_msg_error(_id, "average_with_rejection failed" );
00790 return -1;
00791 }
00792 }
00793 else {
00794 cube_sky_tmp = copy_cube(cube_sky);
00795 }
00796 destroy_cube(cube_sky);
00797
00798 cube_sky = alignCubeToReference (cube_sky_tmp,
00799 ref_im1,
00800 cfg->sfOrder,
00801 cfg->sfType);
00802 if (cube_sky == NULL) {
00803 cpl_msg_error(_id,"alignCubeToReference failed" );
00804 return -1;
00805 }
00806 destroy_cube(cube_sky_tmp);
00807 }
00808 if (cfg->contains_dither == 1 && cfg->contains_sky == 1) {
00809 if (cfg->contains_dark == 1) {
00810 cube_dither_sky_tmp = subImageFromCube (cube_dither_sky, im_dark);
00811 if (cube_dither_sky == NULL) {
00812 cpl_msg_error(_id, "average_with_rejection failed" );
00813 return -1;
00814 }
00815 }
00816 else {
00817 cube_dither_sky_tmp = copy_cube(cube_dither_sky);
00818 }
00819 destroy_cube(cube_dither_sky);
00820
00821 cube_dither_sky = alignCubeToReference (cube_dither_sky_tmp,
00822 ref_im2,
00823 cfg->sfOrder,
00824 cfg->sfType);
00825
00826 if (cube_dither_sky == NULL) {
00827 cpl_msg_error (_id, "alignCubeToReference failed" );
00828 return -1;
00829 }
00830 destroy_cube(cube_dither_sky_tmp);
00831 }
00832 destroy_image (ref_im1);
00833 if (cfg->contains_dither == 1) {
00834 destroy_image(ref_im2);
00835 }
00836 if (cfg->contains_dark == 1) {
00837 destroy_image(im_dark);
00838 }
00839
00840 }
00841
00842
00843
00844
00845
00846 cpl_msg_info(_id,"take the average of the different cubes");
00847 if (cfg->loReject*cfg->nobj < 1. && cfg->hiReject * cfg->nobj < 1.) {
00848 im_obj = averageCubeToImage( cube_object );
00849 if (im_obj == NULL) {
00850 cpl_msg_error (_id, "averageCubeToImage failed" );
00851
00852 destroy_cube_shallow(cube_object);
00853 destroy_cube_shallow(cube_sky);
00854 for (i=0; i< cfg->nframes; i++) {
00855 if(list_head[i]) fits_header_destroy(list_head[i]);
00856 }
00857 cpl_free(list_head);
00858
00859 for (i=0; i< cfg->nframes; i++) {
00860 if(list_header[i]) fits_header_destroy(list_header[i]);
00861 }
00862 cpl_free(list_header);
00863 for (i=0; i< cfg->nframes; i++) {
00864 if(im[i]) destroy_image(im[i]);
00865 }
00866 cpl_free(im);
00867
00868 stack_free(cfg);
00869 cpl_frameset_delete(raw);
00870
00871 return -1;
00872 }
00873 }
00874 else {
00875 im_obj = average_with_rejection( cube_object, cfg->loReject,
00876 cfg->hiReject );
00877 if (im_obj == NULL) {
00878 cpl_msg_error (_id, "average_with_rejection failed" );
00879
00880 destroy_cube_shallow(cube_object);
00881 destroy_cube_shallow(cube_sky);
00882 for (i=0; i< cfg->nframes; i++) {
00883 if(list_head[i]) fits_header_destroy(list_head[i]);
00884 }
00885 cpl_free(list_head);
00886
00887 for (i=0; i< cfg->nframes; i++) {
00888 if(list_header[i]) fits_header_destroy(list_header[i]);
00889 }
00890 cpl_free(list_header);
00891 for (i=0; i< cfg->nframes; i++) {
00892 if(im[i]) destroy_image(im[i]);
00893 }
00894 cpl_free(im);
00895
00896 stack_free(cfg);
00897 cpl_frameset_delete(raw);
00898
00899
00900 return -1;
00901 }
00902 }
00903 destroy_cube_shallow(cube_object);
00904
00905
00906 if (cfg->contains_sky == 1) {
00907 if (cfg->loReject*nsky < 1. && cfg->hiReject*nsky < 1.) {
00908
00909 im_sky = averageCubeToImage( cube_sky );
00910 if (im_sky == NULL) {
00911 cpl_msg_error(_id,"averageCubeToImage failed" );
00912 return -1;
00913 }
00914 }
00915 else {
00916
00917
00918 im_sky = average_with_rejection( cube_sky, cfg->loReject,
00919 cfg->hiReject );
00920
00921 if (im_sky == NULL) {
00922 cpl_msg_error(_id,"average_with_rejection failed" );
00923 return -1;
00924 }
00925 }
00926 destroy_cube_shallow(cube_sky);
00927 }
00928
00929
00930 if (cfg->contains_dither == 1) {
00931 if (cfg->loReject*nobjdith < 1. && cfg->hiReject*nobjdith < 1.) {
00932 im_dither = averageCubeToImage( cube_dither_object );
00933 if (im_dither == NULL) {
00934 cpl_msg_error(_id,"averageCubeToImage failed" );
00935 return -1;
00936 }
00937 } else {
00938 im_dither = average_with_rejection( cube_dither_object,
00939 cfg->loReject, cfg->hiReject );
00940 if (im_dither == NULL) {
00941 cpl_msg_error(_id,"average_with_rejection failed" );
00942 return -1;
00943 }
00944 }
00945 destroy_cube(cube_dither_object);
00946 }
00947
00948
00949
00950 if (cfg->contains_dither == 1 && nskydith > 0) {
00951 if (cfg->loReject*nskydith < 1. && cfg->hiReject*nskydith < 1.) {
00952 im_dither_sky = averageCubeToImage( cube_dither_sky );
00953 if (im_dither_sky == NULL) {
00954 cpl_msg_error(_id,"averageCubeToImage failed" );
00955 return -1;
00956 }
00957 } else {
00958 im_dither_sky = average_with_rejection( cube_dither_sky,
00959 cfg->loReject, cfg->hiReject );
00960 if (im_dither_sky == NULL) {
00961 cpl_msg_error(_id,"average_with_rejection failed" );
00962 return -1;
00963 }
00964 }
00965 destroy_cube(cube_dither_sky);
00966 }
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976 if (cfg->contains_sky == 1) {
00977 cpl_msg_info(_id,"Subtract the resulting off-frame (sky) from the on-frame");
00978 im_obj_sub = sub_image(im_obj, im_sky);
00979
00980 if (im_obj_sub == NULL) {
00981 cpl_msg_error(_id,"could not sub_image");
00982
00983
00984 destroy_image(im_obj);
00985 destroy_image(im_sky);
00986
00987 for (i=0; i< cfg->nframes; i++) {
00988 if(list_head[i]) fits_header_destroy(list_head[i]);
00989 }
00990 cpl_free(list_head);
00991
00992 for (i=0; i< cfg->nframes; i++) {
00993 if(list_header[i]) fits_header_destroy(list_header[i]);
00994 }
00995 cpl_free(list_header);
00996
00997 for (i=0; i< cfg->nframes; i++) {
00998 if(im[i]) destroy_image(im[i]);
00999 }
01000 cpl_free(im);
01001 stack_free(cfg);
01002 cpl_frameset_delete(raw);
01003
01004 return -1;
01005 }
01006 destroy_image(im_obj);
01007
01008 if (((cfg->contains_dither == 1) && (nskydith > 0)) ||
01009 cfg->contains_dither == 0) {
01010 destroy_image(im_sky);
01011 im_obj = im_obj_sub;;
01012 }
01013 }
01014
01015
01016 if (cfg->contains_dither == 1 && nskydith > 0) {
01017 im_dither_sub = sub_image(im_dither, im_dither_sky);
01018
01019 if (im_dither_sub == NULL) {
01020 cpl_msg_error(_id,"could not sub_image");
01021 return -1;
01022 }
01023 destroy_image(im_dither);
01024 destroy_image(im_dither_sky);
01025 im_dither = im_dither_sub;
01026 }
01027 else if (cfg->contains_dither == 1 && nskydith == 0 &&
01028 cfg->contains_sky == 1) {
01029 im_dither_sub = sub_image(im_dither, im_sky);
01030 if (im_dither_sub == NULL) {
01031 cpl_msg_error(_id,"could not sub_image");
01032 return -1;
01033 }
01034 destroy_image(im_dither);
01035 destroy_image(im_sky);
01036 im_dither = im_dither_sub;
01037 }
01038
01039 for (i=0; i< cfg->nframes; i++) {
01040 if(list_head[i]) fits_header_destroy(list_head[i]);
01041 }
01042 cpl_free(list_head);
01043
01044
01045
01046
01047
01048
01049
01050
01051 if (cfg->flatInd == 1) {
01052 cpl_msg_info(_id,"Flatfielding");
01053 flat1 = load_image (cfg->flatfield1);
01054
01055 if (flat1 == NULL) {
01056 cpl_msg_error(_id,"could not load flatfield image" );
01057
01058 destroy_image(im_obj);
01059 destroy_image(im_obj_flat);
01060 for (i=0; i< cfg->nframes; i++) {
01061 if(list_header[i]) fits_header_destroy(list_header[i]);
01062 }
01063 cpl_free(list_header);
01064 for (i=0; i< cfg->nframes; i++) {
01065 if(im[i]) destroy_image(im[i]);
01066 }
01067 cpl_free(im);
01068 stack_free(cfg);
01069 cpl_frameset_delete(raw);
01070 return -1;
01071 }
01072 im_obj_flat = divImagesRobust( im_obj, flat1);
01073 if (im_obj_flat == NULL) {
01074 cpl_msg_error(_id,"could not carry out flatfield division" );
01075
01076 destroy_image(flat1);
01077 destroy_image(im_obj);
01078 for (i=0; i< cfg->nframes; i++) {
01079 if(list_header[i]) fits_header_destroy(list_header[i]);
01080 }
01081 cpl_free(list_header);
01082 for (i=0; i< cfg->nframes; i++) {
01083 if(im[i]) destroy_image(im[i]);
01084 }
01085 cpl_free(im);
01086 stack_free(cfg);
01087 cpl_frameset_delete(raw);
01088 return -1;
01089 }
01090
01091 if (frm_ind == 0) {
01092 if (cfg->warpfixInd == 1){
01093 cpl_msg_info(_id,"correct FF for distortions");
01094 flat1_dist = image_warp_fits(flat1, cfg->kernel, cfg->polyFile);
01095
01096 imgw=cpl_image_wrap_float(flat1_dist->lx,flat1_dist->ly,flat1_dist->data);
01097 img=cpl_image_duplicate(imgw);
01098 cpl_image_unwrap(imgw);
01099
01100 if(-1 == sinfoni_pro_dump_ima(img,raw,sof,STACK_MFLAT_DIST_OUT_FILENAME,
01101 PRO_STACK_MFLAT_DIST,plugin_id,config)) {
01102 cpl_msg_error(_id,"cannot dump ima %s", STACK_MFLAT_DIST_OUT_FILENAME);
01103
01104 cpl_image_delete(img);
01105 destroy_image(flat1_dist);
01106 destroy_image(flat1);
01107 destroy_image(im_obj_flat);
01108 destroy_image(im_obj);
01109
01110 for (i=0; i< cfg->nframes; i++) {
01111 if(list_header[i]) fits_header_destroy(list_header[i]);
01112 }
01113 cpl_free(list_header);
01114
01115
01116 for (i=0; i< cfg->nframes; i++) {
01117 if(im[i]) destroy_image(im[i]);
01118 }
01119 cpl_free(im);
01120
01121
01122
01123
01124 stack_free(cfg);
01125 cpl_frameset_delete(raw);
01126
01127
01128 return -1;
01129 } else {
01130 cpl_msg_info(_id,"Saving frame ID %s",PRO_STACK_MFLAT_DIST);
01131 }
01132 destroy_image(flat1_dist);
01133 cpl_image_delete(img);
01134 }
01135 }
01136
01137 destroy_image(flat1);
01138 destroy_image(im_obj);
01139 im_obj = im_obj_flat;
01140
01141 if (cfg->contains_dither == 1) {
01142 flat2 = load_image (cfg->flatfield2);
01143 if (flat2 == NULL) {
01144 cpl_msg_error(_id,"could not load flatfield image" );
01145 return -1;
01146 }
01147
01148 im_dither_flat = divImagesRobust( im_dither, flat2);
01149 if (im_dither_flat == NULL) {
01150 cpl_msg_error(_id,"could not carry out flatfield division" );
01151 return -1;
01152 }
01153 if (frm_ind == 0) {
01154 if (cfg->warpfixInd == 1){
01155 cpl_msg_info(_id,"correct FF for distortions");
01156 flat2_dist = image_warp_fits(flat2, cfg->kernel, cfg->polyFile);
01157
01158 imgw=cpl_image_wrap_float(flat2_dist->lx,flat2_dist->ly,flat2_dist->data);
01159 img=cpl_image_duplicate(imgw);
01160 cpl_image_unwrap(imgw);
01161
01162 if(-1 == sinfoni_pro_dump_ima(img,raw,sof,
01163 STACK_MFLAT_DITHER_DIST_OUT_FILENAME,
01164 PRO_STACK_MFLAT_DITHER_DIST,
01165 plugin_id,config)) {
01166 cpl_msg_error(_id,"cannot dump ima %s", STACK_MFLAT_DITHER_DIST_OUT_FILENAME);
01167 return -1;
01168 } else {
01169 cpl_msg_info(_id,"Saving frame ID %s",PRO_STACK_MFLAT_DITHER_DIST);
01170 }
01171 destroy_image(flat2_dist);
01172 cpl_image_delete(img);
01173 }
01174 }
01175 destroy_image(flat2);
01176 destroy_image(im_dither);
01177 im_dither = im_dither_flat;
01178 }
01179 }
01180
01181
01182
01183
01184
01185
01186
01187
01188 if (cfg->maskInd == 1) {
01189 cpl_msg_info(_id,"static bad pixel correction");
01190 mask_im = load_image(cfg->mask);
01191 if (mask_im == NULL) {
01192 cpl_msg_error(_id,"could not load static bad pixel mask" );
01193
01194 destroy_image(flat1);
01195 destroy_image(im_obj_flat);
01196 for (i=0; i< cfg->nframes; i++) {
01197 if(list_header[i]) fits_header_destroy(list_header[i]);
01198 }
01199 cpl_free(list_header);
01200 for (i=0; i< cfg->nframes; i++) {
01201 if(im[i]) destroy_image(im[i]);
01202 }
01203 cpl_free(im);
01204 stack_free(cfg);
01205 cpl_frameset_delete(raw);
01206 return -1;
01207 }
01208
01209 if (-1 == changeMask(mask_im, im_obj)) {
01210 cpl_msg_error(_id,"changeMask failed" );
01211
01212
01213 destroy_image(flat1);
01214 destroy_image(im_obj_flat);
01215 for (i=0; i< cfg->nframes; i++) {
01216 if(list_header[i]) fits_header_destroy(list_header[i]);
01217 }
01218 cpl_free(list_header);
01219 for (i=0; i< cfg->nframes; i++) {
01220 if(im[i]) destroy_image(im[i]);
01221 }
01222 cpl_free(im);
01223 stack_free(cfg);
01224 cpl_frameset_delete(raw);
01225 return -1;
01226 }
01227
01228 if (cfg->indind == 0) {
01229
01230
01231 tbl_slitpos_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
01232 strcpy(tbl_slitpos_name,cfg->slitposList);
01233 tbl_slitpos = cpl_table_load(tbl_slitpos_name,1,0);
01234
01235 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01236
01237 cpl_msg_error(_id, "error loading slitpos tbl %s ",
01238 tbl_slitpos_name);
01239 cpl_msg_error(_id, (char* ) cpl_error_get_message());
01240
01241
01242 cpl_image_delete(img);
01243 destroy_image(flat1);
01244 cpl_free(tbl_slitpos_name);
01245 destroy_image(im_obj_flat);
01246 for (i=0; i< cfg->nframes; i++) {
01247 if(list_header[i]) fits_header_destroy(list_header[i]);
01248 }
01249 cpl_free(list_header);
01250 for (i=0; i< cfg->nframes; i++) {
01251 if(im[i]) destroy_image(im[i]);
01252 }
01253 cpl_free(im);
01254 cpl_table_delete(tbl_slitpos);
01255 stack_free(cfg);
01256 cpl_frameset_delete(raw);
01257 return -1;
01258 }
01259 n = cpl_table_get_nrow(tbl_slitpos);
01260 slit_edges = new_2Dfloat_array(n, 2);
01261 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01262
01263 cpl_msg_error(_id, "error getting ncol from tbl %s ",
01264 tbl_slitpos_name);
01265 cpl_msg_error(_id, (char* ) cpl_error_get_message());
01266 return -1;
01267 }
01268
01269 for (i =0 ; i< n; i++){
01270 val_x=cpl_table_get_double(tbl_slitpos,"pos1",i,&status);
01271 val_y=cpl_table_get_double(tbl_slitpos,"pos2",i,&status);
01272 array2D_set_value(slit_edges,val_x,i,0);
01273 array2D_set_value(slit_edges,val_y,i,1);
01274 }
01275 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01276
01277 cpl_msg_error(_id, "error reading tbl %s ",
01278 tbl_slitpos_name);
01279 cpl_msg_error(_id, (char* ) cpl_error_get_message());
01280 return -1;
01281 }
01282 cpl_table_delete(tbl_slitpos);
01283 cpl_free(tbl_slitpos_name);
01284
01285
01286
01287 int_im = interpolSourceImage (im_obj,
01288 mask_im,
01289 cfg->maxRad,
01290 slit_edges);
01291
01292 if (int_im == NULL) {
01293 cpl_msg_error(_id,"could not carry out interpolSourceImage" );
01294 return -1;
01295 }
01296 destroy_image(im_obj);
01297 im_obj = int_im;
01298 if (cfg->contains_dither == 1) {
01299 int_im_dith = interpolSourceImage (im_dither,
01300 mask_im,
01301 cfg->maxRad,
01302 slit_edges);
01303 if (int_im_dith == NULL) {
01304 cpl_msg_error(_id,"could not carry out interpolSourceImage" );
01305 return -1;
01306 }
01307 destroy_image(im_dither);
01308 im_dither = int_im_dith;
01309 }
01310 destroy_2Darray(slit_edges, 32);
01311 }
01312 else {
01313 int_im = multImageByMask(im_obj, mask_im);
01314 if (int_im == NULL) {
01315 cpl_msg_error(_id,"could not carry out multImageByMask" );
01316 return -1;
01317 }
01318 destroy_image(im_obj);
01319 im_obj = int_im;
01320 if (cfg->contains_dither == 1) {
01321 int_im_dith = multImageByMask(im_dither, mask_im);
01322 if (int_im_dith == NULL) {
01323 cpl_msg_error(_id,"could not carry out multImageByMask" );
01324 return -1;
01325 }
01326 destroy_image(im_dither);
01327 im_dither = int_im_dith;
01328 }
01329 }
01330 destroy_image (mask_im);
01331 }
01332
01333
01334
01335
01336
01337
01338
01339
01340 if (cfg->maskInd == 2){
01341 cpl_msg_info(_id,"static bad pixel correction BEZIER");
01342 mask_im = load_image(cfg->mask);
01343 if (mask_im == NULL) {
01344 cpl_msg_error(_id,"could not load static bad pixel mask" );
01345 return -1;
01346 }
01347
01348 if (-1 == changeMask(mask_im, im_obj)){
01349 cpl_msg_error(_id,"changeMask failed" );
01350 return -1;
01351 }
01352
01353
01354
01355 tbl_slitpos_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
01356 strcpy(tbl_slitpos_name,cfg->slitposList);
01357 tbl_slitpos = cpl_table_load(tbl_slitpos_name,1,0);
01358 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01359
01360 cpl_msg_error(_id, "error loading tbl %s ",
01361 tbl_slitpos_name);
01362 cpl_msg_error(_id, (char* ) cpl_error_get_message());
01363 return -1;
01364 }
01365 n = cpl_table_get_nrow(tbl_slitpos);
01366 slit_edges = new_2Dfloat_array(n, 2);
01367 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01368
01369 cpl_msg_error(_id, "error getting ncol from tbl %s ",
01370 tbl_slitpos_name);
01371 cpl_msg_error(_id, (char* ) cpl_error_get_message());
01372 return -1;
01373 }
01374
01375 for (i =0 ; i< n; i++){
01376 val_x=cpl_table_get_double(tbl_slitpos,"pos1",i,&status);
01377 val_y=cpl_table_get_double(tbl_slitpos,"pos2",i,&status);
01378 array2D_set_value(slit_edges,val_x,i,0);
01379 array2D_set_value(slit_edges,val_y,i,1);
01380 }
01381 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01382
01383 cpl_msg_error(_id, "error reading tbl %s ",
01384 tbl_slitpos_name);
01385 cpl_msg_error(_id, (char* ) cpl_error_get_message());
01386 return -1;
01387 }
01388 cpl_table_delete(tbl_slitpos);
01389 cpl_free(tbl_slitpos_name);
01390
01391
01392
01393
01394 tbl_index_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
01395 strcpy(tbl_index_name,cfg->indexlist);
01396 tbl_index = cpl_table_load(tbl_index_name,1,0);
01397 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01398
01399 cpl_msg_error(_id, "error loading tbl %s ",
01400 tbl_index_name);
01401 cpl_msg_error(_id, (char* ) cpl_error_get_message());
01402 return -1;
01403 }
01404 n = cpl_table_get_nrow(tbl_index);
01405 in_nam = (char**) cpl_calloc(n,sizeof(char*));
01406 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01407
01408 cpl_msg_error(_id, "error getting ncol from tbl %s ",
01409 tbl_index_name);
01410 cpl_msg_error(_id, (char* ) cpl_error_get_message());
01411 return -1;
01412 }
01413
01414 for (i =0 ; i< n; i++){
01415 strcpy(in_nam[i],cpl_table_get_string(tbl_index,"name",i));
01416 }
01417 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01418
01419 cpl_msg_error(_id, "error reading tbl %s ",
01420 tbl_index_name);
01421 cpl_msg_error(_id, (char* ) cpl_error_get_message());
01422 return -1;
01423 }
01424 cpl_table_delete(tbl_index);
01425 cpl_free(tbl_index_name);
01426
01427
01428
01429 for (i=0;i<cnt;i++) {
01430 if (strcmp("ICube.fits", name) != 0){
01431 iCube = load_cube ("ICube.fits");
01432 }
01433 else if (strcmp("JCube.fits", name) != 0) {
01434 jCube = load_cube ("JCube.fits");
01435 }
01436 else if (strcmp("X.fits", name) != 0) {
01437 X = load_image("X.fits");
01438 }
01439 else if (strcmp("Y.fits", name) != 0) {
01440 Y = load_image("Y.fits");
01441 }
01442 else if (strcmp("Z.fits", name) != 0) {
01443 Z = load_image("Z.fits");
01444 }
01445 else if (strcmp("cX.fits", name) != 0) {
01446 hX = load_image("cX.fits");
01447 }
01448 else {
01449 cpl_msg_error(_id,"wrong name in index list or needed file not there!");
01450 return -1;
01451 }
01452 }
01453 lookup = newLookup();
01454
01455 lookup->id=iCube;
01456 lookup->jd=jCube;
01457 lookup->X=X;
01458 lookup->Y=Y;
01459 lookup->Z=Z;
01460 lookup->hX=hX;
01461
01462
01463 im_obj = cbezierInterpolateImage( im_obj, mask_im, lookup,
01464 cfg->maxRad,
01465 cfg->maxRad,
01466 cfg->maxRad, 2, slit_edges );
01467
01468 im_obj = cbezierFindBad( im_obj,
01469 mask_im,
01470 cfg->maxRad,
01471 cfg->maxRad,
01472 cfg->maxRad ,
01473 0,
01474 im_obj->lx,
01475 0,
01476 im_obj->ly,
01477 cfg->sigmaFactor );
01478
01479 if (im_obj == NULL) {
01480 cpl_msg_error(_id,"could not carry out cbezierInterpolateImage" );
01481 return -1;
01482 }
01483 if (cfg->contains_dither == 1) {
01484 im_dither = cbezierInterpolateImage( im_dither, mask_im, lookup,
01485 cfg->maxRad,
01486 cfg->maxRad,
01487 cfg->maxRad,
01488 2, slit_edges );
01489
01490 im_dither = cbezierFindBad( im_dither, mask_im,
01491 cfg->maxRad,
01492 cfg->maxRad,
01493 cfg->maxRad , 0,
01494 im_obj->lx, 0,
01495 im_obj->ly,
01496 cfg->sigmaFactor );
01497
01498 if (im_dither == NULL) {
01499 cpl_msg_error(_id,"could not carry out cbezierInterpolateImage on dithered frame" );
01500 return -1;
01501 }
01502 }
01503 destroy_2Darray(slit_edges, 32);
01504 destroy_image (mask_im);
01505 destroy_cube(iCube);
01506 destroy_cube(jCube);
01507 destroy_image(lookup->X);
01508
01509 destroy_image(lookup->X);
01510 destroy_image(lookup->Y);
01511 destroy_image(lookup->hX);
01512 destroyLookup(lookup);
01513 }
01514
01515
01516
01517
01518 if (cfg->maskInd == 3) {
01519 mask_im = load_image(cfg->mask);
01520 if (mask_im == NULL){
01521 cpl_msg_error(_id,"could not load static bad pixel mask" );
01522 return -1;
01523 }
01524
01525
01526 tbl_slitpos_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
01527 strcpy(tbl_slitpos_name,cfg->slitposList);
01528 tbl_slitpos = cpl_table_load(tbl_slitpos_name,1,0);
01529 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01530
01531 cpl_msg_error(_id, "error loading tbl %s ",
01532 tbl_slitpos_name);
01533 cpl_msg_error(_id, (char* ) cpl_error_get_message());
01534 return -1;
01535 }
01536 n = cpl_table_get_nrow(tbl_slitpos);
01537 slit_edges = new_2Dfloat_array(n, 2);
01538 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01539
01540 cpl_msg_error(_id, "error getting ncol from tbl %s ",
01541 tbl_slitpos_name);
01542 cpl_msg_error(_id, (char* ) cpl_error_get_message());
01543 return -1;
01544 }
01545
01546 for (i =0 ; i< n; i++){
01547 val_x=cpl_table_get_double(tbl_slitpos,"pos1",i,&status);
01548 val_y=cpl_table_get_double(tbl_slitpos,"pos2",i,&status);
01549 array2D_set_value(slit_edges,val_x,i,0);
01550 array2D_set_value(slit_edges,val_y,i,1);
01551 }
01552 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01553
01554 cpl_msg_error(_id, "error reading tbl %s ",
01555 tbl_slitpos_name);
01556 cpl_msg_error(_id, (char* ) cpl_error_get_message());
01557 return -1;
01558 }
01559 cpl_table_delete(tbl_slitpos);
01560 cpl_free(tbl_slitpos_name);
01561
01562
01563
01564
01565
01566 tbl_index_name = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char*));
01567 strcpy(tbl_index_name,cfg->indexlist);
01568 tbl_index = cpl_table_load(tbl_index_name,1,0);
01569 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01570
01571 cpl_msg_error(_id, "error loading tbl %s ",
01572 tbl_index_name);
01573 cpl_msg_error(_id, (char* ) cpl_error_get_message());
01574 return -1;
01575 }
01576 n = cpl_table_get_nrow(tbl_index);
01577 in_nam = (char**) cpl_calloc(n,sizeof(char*));
01578 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01579
01580 cpl_msg_error(_id, "error getting ncol from tbl %s ",
01581 tbl_index_name);
01582 cpl_msg_error(_id, (char* ) cpl_error_get_message());
01583 return -1;
01584 }
01585
01586 for (i =0 ; i< n; i++){
01587 strcpy(in_nam[i],cpl_table_get_string(tbl_index,"name",i));
01588 }
01589 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01590
01591 cpl_msg_error(_id, "error reading tbl %s ",
01592 tbl_index_name);
01593 cpl_msg_error(_id, (char* ) cpl_error_get_message());
01594 return -1;
01595 }
01596 cpl_table_delete(tbl_index);
01597 cpl_free(tbl_index_name);
01598
01599
01600
01601 for (i=0;i<cnt;i++){
01602 if (strcmp("ICube.fits", in_nam[i]) != 0){
01603 iCube = load_cube ("ICube.fits");
01604 }
01605 else if (strcmp("JCube.fits", in_nam[i]) != 0){
01606 jCube = load_cube ("JCube.fits");
01607 }
01608 else if (strcmp("X.fits", in_nam[i]) != 0){
01609 X = load_image("X.fits");
01610 }
01611 else if (strcmp("Y.fits", in_nam[i]) != 0){
01612 Y = load_image("Y.fits");
01613 }
01614 else if (strcmp("Z.fits", in_nam[i]) != 0){
01615 Z = load_image("Z.fits");
01616 }
01617 else if (strcmp("cX.fits", in_nam[i]) != 0) {
01618 hX = load_image("cX.fits");
01619 }
01620 else {
01621 cpl_msg_error(_id,"wrong name in index list or needed file not there!");
01622 return -1;
01623 }
01624 }
01625 lookup = newLookup();
01626 lookup->id=iCube;
01627 lookup->jd=jCube;
01628 lookup->X=X;
01629 lookup->Y=Y;
01630 lookup->Z=Z;
01631 lookup->hX=hX;
01632
01633 im_obj = cbezierInterpolateImage( im_obj,
01634 mask_im,
01635 lookup,
01636 cfg->maxRad,
01637 cfg->maxRad,
01638 cfg->maxRad,
01639 2,
01640 slit_edges );
01641 if (im_obj == NULL) {
01642 cpl_msg_error(_id,"could not carry out cbezierInterpolateImage" );
01643 return -1;
01644 }
01645
01646
01647 if (cfg->contains_dither == 1) {
01648 im_dither = cbezierInterpolateImage( im_dither,
01649 mask_im,
01650 lookup,
01651 cfg->maxRad,
01652 cfg->maxRad,
01653 cfg->maxRad,
01654 2,
01655 slit_edges );
01656 if (im_dither == NULL) {
01657 cpl_msg_error(_id,"could not carry out cbezierInterpolateImage on dithered frame" );
01658 return -1;
01659 }
01660 }
01661 destroy_2Darray(slit_edges, 32);
01662 destroy_image (mask_im);
01663 destroy_cube(iCube);
01664 destroy_cube(jCube);
01665 destroy_image(lookup->X);
01666
01667 destroy_image(lookup->Y);
01668 destroy_image(lookup->Z);
01669 destroy_image(lookup->hX);
01670 destroyLookup(lookup);
01671 }
01672
01673
01674
01675
01676
01677
01678
01679
01680
01681
01682 cpl_msg_info(_id,"cfg->warpfixInd=%d",cfg->warpfixInd);
01683 if (cfg->warpfixInd == 1){
01684
01685 cpl_msg_info(_id,"correct object for distortions");
01686 int_im_shifted = image_warp_fits(im_obj, cfg->kernel, cfg->polyFile);
01687
01688 if (int_im_shifted == NULL) {
01689 cpl_msg_error(_id,"could not carry out image_warp" );
01690 stack_free(cfg);
01691 cpl_frameset_delete(raw);
01692 return -1;
01693 }
01694
01695 destroy_image(im_obj);
01696 im_obj = int_im_shifted;
01697 if (cfg->contains_dither == 1){
01698 int_im_dith_shifted = image_warp(im_dither, cfg->kernel, cfg->polyFile);
01699 if (int_im_dith_shifted == NULL){
01700 cpl_msg_error(_id,"could not carry out image_warp" );
01701 return -1;
01702 }
01703 destroy_image(im_dither);
01704 im_dither = int_im_dith_shifted;
01705 }
01706 }
01707
01708
01709
01710
01711
01712
01713 if (cfg->interInd == 1 && cfg->contains_dither == 1){
01714 cpl_msg_info(_id,"merge (interleave) both resulting frames");
01715 im3 = new_image(im_obj->lx, im_obj->ly);
01716 if (im3 == NULL) {
01717 cpl_msg_error(_id,"could not allocate an image" );
01718 return -1;
01719 }
01720 im4 = removeGeneralOffset( im_obj, im_dither, im3, cfg->noRows );
01721 if (im4 == NULL){
01722 cpl_msg_error(_id,"removeGeneralOffset failed" );
01723 return -1;
01724 }
01725 im5 = removeRegionalTilt ( im_obj, im4, im3 );
01726 if (im5 == NULL) {
01727 cpl_msg_error(_id,"removeRegionalTilt failed" );
01728 return -1;
01729 }
01730 im6 = removeColumnOffset ( im_obj, im5, im3 );
01731 if (im6 == NULL) {
01732 cpl_msg_error(_id,"removeColumnOffset failed" );
01733 return -1;
01734 }
01735 im7 = removeResidualTilt ( im6, im3 );
01736 if (im7 == NULL) {
01737 cpl_msg_error(_id,"removeResidualTilt failed" );
01738 return -1;
01739 }
01740 im8 = removeResidualOffset ( im7, im3 );
01741 if (im8 == NULL){
01742 cpl_msg_error(_id,"removeResidualOffset failed");
01743 return -1;
01744 }
01745 im9 = mergeImages(im_obj, im8, im3);
01746 if (im9 == NULL){
01747 cpl_msg_error(_id,"mergeImages failed" );
01748 return -1;
01749 }
01750
01751
01752 img=cpl_image_wrap_float(im9->lx,im9->ly,im9->data);
01753 if(-1 == sinfoni_pro_dump_ima(img,raw,sof,cfg->outName,
01754 frm_pro_ctg,plugin_id,config)) {
01755 cpl_msg_error(_id,"cannot dump ima %s", cfg->outName);
01756 return -1;
01757 } else {
01758 cpl_msg_info(_id,"Saving1 frame ID %s",frm_pro_ctg);
01759 }
01760 destroy_image(im3);
01761 destroy_image(im4);
01762 destroy_image(im5);
01763 destroy_image(im6);
01764 destroy_image(im7);
01765 destroy_image(im8);
01766 destroy_image(im9);
01767 destroy_image(im_obj);
01768 destroy_image(im_dither);
01769 cpl_image_delete(img);
01770
01771
01772
01773
01774
01775 } else if (cfg->gaussInd == 1 && cfg->interInd == 0) {
01776 cpl_msg_info(_id,"convolve spectra with Gaussian");
01777 im_conv = convolveImageByGauss ( im_obj, cfg->hw );
01778 if (im_conv == NULL){
01779 cpl_msg_error(_id,"convolveImageByGauss failed" );
01780 return -1;
01781 }
01782
01783 img=cpl_image_wrap_float(im_conv->lx,im_conv->ly,im_conv->data);
01784 if(-1 == sinfoni_pro_dump_ima(img,raw,sof,cfg->outName,
01785 frm_pro_ctg,plugin_id,config)) {
01786 cpl_msg_error(_id,"cannot dump ima %s", cfg->outName);
01787 return -1;
01788 } else {
01789 cpl_msg_info(_id,"Saving2 frame ID %s",frm_pro_ctg);
01790 }
01791
01792 destroy_image(im_obj);
01793 destroy_image(im_conv);
01794 if (cfg->contains_dither == 1){
01795 destroy_image(im_dither);
01796 }
01797 cpl_image_unwrap(img);
01798 }
01799 else {
01800
01801
01802
01803
01804 cpl_msg_info(_id,"Add QC LOG");
01805
01806
01807
01808
01809 qclog_tbl = sinfoni_qclog_init(7);
01810 key_value = cpl_calloc(FILE_NAME_SZ,sizeof(char));
01811 sprintf(key_value,"%g",qc.avg_on);
01812 sinfoni_qclog_add(qclog_tbl,0,"QC FRMON MEANFLUX","CPL_TYPE_DOUBLE",
01813 key_value,"Average of flux");
01814
01815 sprintf(key_value,"%g",qc.avg_of);
01816 sinfoni_qclog_add(qclog_tbl,1,"QC FRMOFF MEANFLUX","CPL_TYPE_DOUBLE",
01817 key_value,"Average of flux");
01818
01819 sprintf(key_value,"%g",qc.avg_di);
01820 sinfoni_qclog_add(qclog_tbl,2,"QC FRMDIF MEANFLUX","CPL_TYPE_DOUBLE",
01821 key_value,"Average of flux");
01822
01823 sprintf(key_value,"%g",qc.max_on);
01824 sinfoni_qclog_add(qclog_tbl,3,"QC FRMON MAXFLUX","CPL_TYPE_DOUBLE",
01825 key_value,"Max of flux");
01826
01827 sprintf(key_value,"%g",qc.max_of);
01828 sinfoni_qclog_add(qclog_tbl,4,"QC FRMOFF MAXFLUX","CPL_TYPE_DOUBLE",
01829 key_value,"Max of flux");
01830
01831 sprintf(key_value,"%g",qc.max_di);
01832 sinfoni_qclog_add(qclog_tbl,5,"QC FRMDIF MAXFLUX","CPL_TYPE_DOUBLE",
01833 key_value,"Max of flux");
01834
01835 sprintf(key_value,"%d",(int)qc.nsat);
01836 sinfoni_qclog_add(qclog_tbl,6,"QC FRMON NPIXSAT","CPL_TYPE_INT",
01837 key_value,"Number of saturated pixels");
01838
01839 imgw=cpl_image_wrap_float(im_obj->lx,im_obj->ly,im_obj->data);
01840 img=cpl_image_duplicate(imgw);
01841 cpl_image_unwrap(imgw);
01842
01843
01844 if(-1 == sinfoni_pro_save_ima(img,raw,sof,cfg->outName,
01845 frm_pro_ctg,qclog_tbl,plugin_id,config)) {
01846 cpl_msg_error(_id,"cannot dump ima %s", cfg->outName);
01847
01848 destroy_image(im_obj);
01849 cpl_table_delete(qclog_tbl);
01850 cpl_free(key_value);
01851 cpl_image_delete(img);
01852 for (i=0; i< cfg->nframes; i++) {
01853 if(list_header[i]) fits_header_destroy(list_header[i]);
01854 }
01855 cpl_free(list_header);
01856 for (i=0; i< cfg->nframes; i++) {
01857 if(im[i]) destroy_image(im[i]);
01858 }
01859
01860
01861 cpl_free(im);
01862 stack_free(cfg);
01863 cpl_frameset_delete(raw);
01864 return -1;
01865 } else {
01866 cpl_msg_info(_id,"Saving3 frame ID %s",frm_pro_ctg);
01867 }
01868 destroy_image(im_obj);
01869 cpl_table_delete(qclog_tbl);
01870 cpl_free(key_value);
01871 cpl_image_delete(img);
01872
01873
01874
01875 if (cfg->contains_dither == 1 && cfg->interInd == 0) {
01876 name_list = (char*) cpl_calloc(MAX_NAME_SIZE,sizeof(char));
01877 if (strstr(cfg->outName, ".fits" ) != NULL ) {
01878 sprintf(name_list, "%s%s", get_rootname(cfg->outName), "_dith.fits");
01879 strcpy(cfg->outName,name_list);
01880 } else {
01881 strcat(cfg->outName,"_dith");
01882 }
01883 cpl_free(name_list);
01884
01885
01886 imgw=cpl_image_wrap_float(im_dither->lx,im_dither->ly,im_dither->data);
01887 img=cpl_image_duplicate(imgw);
01888 cpl_image_unwrap(imgw);
01889
01890 if(-1 == sinfoni_pro_dump_ima(img,raw,sof,cfg->outName,
01891 frm_pro_ctg,plugin_id,config)) {
01892 cpl_msg_error(_id,"cannot dump ima %s", cfg->outName);
01893 return -1;
01894 } else {
01895 cpl_msg_info(_id,"Saving4 frame ID %s",frm_pro_ctg);
01896 }
01897 if (cfg->contains_dither == 1) {
01898 destroy_image(im_dither);
01899 }
01900 cpl_image_delete(img);
01901 }
01902 }
01903
01904
01905
01906 for (i=0; i< cfg->nframes; i++) {
01907 if(list_header[i]) fits_header_destroy(list_header[i]);
01908 }
01909 cpl_free(list_header);
01910
01911 for (i=0; i< cfg->nframes; i++) {
01912 if(im[i]) destroy_image(im[i]);
01913 }
01914
01915
01916 cpl_free(im);
01917 stack_free(cfg);
01918 cpl_frameset_delete(raw);
01919 sinfoni_memory_status();
01920 return 0;
01921
01922 }