00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 # include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include <string.h>
00037 #include "sinfo_stack_ini_by_cpl.h"
00038 #include "sinfo_error.h"
00039 #include "sinfo_pro_types.h"
00040 #include "sinfo_raw_types.h"
00041 #include "sinfo_ref_types.h"
00042 #include "sinfo_functions.h"
00043
00044 #include "sinfo_file_handling.h"
00045
00046
00047
00048
00049 static void sinfo_stack_free_alloc(stack_config_n * cfg);
00050
00051
00052 static void
00053 parse_section_frames(stack_config_n *,
00054 cpl_frameset* sof,
00055 cpl_frameset** raw,
00056 int* status,
00057 fake* fk);
00058
00059 static void
00060 parse_section_cleanmean(stack_config_n *, cpl_parameterlist* cpl_cfg);
00061 static void
00062 parse_section_flatfield(stack_config_n *, cpl_parameterlist* cpl_cfg);
00063 static void
00064 parse_section_badpixel(stack_config_n *, cpl_parameterlist* cpl_cfg);
00065 static void
00066 parse_section_interleaving(stack_config_n *);
00067 static void
00068 parse_section_gaussconvolution(stack_config_n *, cpl_parameterlist* cpl_cfg);
00069 static void
00070 parse_section_shiftframes(stack_config_n *);
00071 static void
00072 parse_section_warpfix(stack_config_n *, cpl_parameterlist* cpl_cfg);
00073 static void
00074 parse_section_qclog(stack_config_n *, cpl_parameterlist* cpl_cfg);
00082
00083
00094
00095
00096 stack_config_n * sinfo_parse_cpl_input_stack(cpl_parameterlist* cpl_cfg,
00097 cpl_frameset* sof,
00098 cpl_frameset** raw,
00099 fake* fk)
00100 {
00101 stack_config_n * cfg =sinfo_stack_cfg_create_n();
00102 int status=0;
00103
00104
00105
00106
00107
00108
00109
00110
00111 parse_section_cleanmean (cfg, cpl_cfg);
00112 parse_section_flatfield (cfg, cpl_cfg);
00113 parse_section_badpixel (cfg, cpl_cfg);
00114 parse_section_interleaving (cfg);
00115 parse_section_gaussconvolution (cfg, cpl_cfg);
00116 parse_section_shiftframes (cfg);
00117 parse_section_warpfix (cfg, cpl_cfg);
00118 parse_section_qclog (cfg, cpl_cfg);
00119 parse_section_frames (cfg, sof, raw, &status, fk);
00120 if (status > 0) {
00121 sinfo_msg_error("parsing cpl input");
00122 sinfo_stack_cfg_destroy_n(cfg);
00123 cfg = NULL ;
00124 return NULL ;
00125 }
00126 return cfg ;
00127 }
00137 static void
00138 parse_section_frames(stack_config_n * cfg,
00139 cpl_frameset* sof, cpl_frameset** raw_set,int* status,
00140 fake* fk)
00141 {
00142
00143 int i;
00144 const char * name;
00145 char file[FILE_NAME_SZ];
00146 int nobj, noff, ndark ;
00147 int nditherobj, nditheroff ;
00148 int found_sky ;
00149 int found_ref ;
00150 int found_dither ;
00151 int found_dark ;
00152 int nraw=0;
00153 int nsof=0;
00154
00155 cpl_frame* frame = NULL;
00156
00157 char spat_res[FILE_NAME_SZ];
00158 char lamp_status[FILE_NAME_SZ];
00159 char band[FILE_NAME_SZ];
00160 int ins_set=0;
00161 char* tag=NULL;
00162 char* do_class=NULL;
00163
00164 nsof = cpl_frameset_get_size(sof);
00165
00166
00167 do_class=fk->pro_class;
00168
00169 if(strcmp(do_class,"DEFAULT") == 0) {
00170 sinfo_extract_raw_stack_frames(sof,raw_set);
00171 } else if (strcmp(do_class,PRO_FIBRE_NS_STACKED_DIST) == 0) {
00172 sinfo_is_fibres_on_off(sof,*raw_set);
00173 } else if (strcmp(do_class,PRO_FIBRE_NS_STACKED) == 0) {
00174 sinfo_contains_frames_kind(sof,*raw_set,(char*)PRO_FIBRE_NS_STACKED);
00175 } else if (strcmp(do_class,RAW_STACKED_SLITPOS) == 0) {
00176 sinfo_extract_raw_stack_frames(sof,raw_set);
00177 } else {
00178 sinfo_extract_raw_frames_type(sof,raw_set,do_class);
00179 }
00180 nraw = cpl_frameset_get_size(*raw_set);
00181 if (nraw < 1) {
00182 sinfo_msg_error("Too few raw frames present in frameset!");
00183 (*status)++;
00184 return;
00185 }
00186
00187
00188
00189 cfg->framelist = cpl_malloc(nraw * sizeof(char*));
00190 cfg->frametype = cpl_malloc(nraw * sizeof(int));
00191 cfg->frameposition = cpl_malloc(nraw * sizeof(int));
00192
00193 for (i=0;i<nraw;i++) {
00194 cfg->framelist[i]=NULL;
00195 cfg->frametype[i]=-1;
00196 cfg->frameposition[i]=-1;
00197 }
00198
00199 found_sky = 0 ;
00200 found_ref = 0 ;
00201 found_dither = 0 ;
00202 found_dark = 0 ;
00203 nobj = 0 ;
00204 noff = 0 ;
00205 ndark = 0 ;
00206 nditherobj = 0 ;
00207 nditheroff = 0 ;
00208
00209
00210
00211
00212
00213 for (i=0 ; i<nraw ; i++) {
00214 frame = cpl_frameset_get_frame(*raw_set,i);
00215 name=cpl_frame_get_filename(frame);
00216 if(sinfo_file_exists((char*)name)==1) {
00217
00218 if(cpl_frame_get_tag(frame) != NULL) {
00219
00220 tag= (char*) cpl_frame_get_tag(frame);
00221 if((sinfo_frame_is_on(frame) == 0) ||
00222 (sinfo_frame_is_sky(frame) == 1))
00223 {
00224 cfg->framelist[i]=cpl_strdup(cpl_frame_get_filename(frame));
00225 cfg->frametype[i] = FRAME_OFF ;
00226 found_sky = 1;
00227 if (sinfo_frame_is_dither(frame))
00228 {
00229 cfg->frameposition[i] = FRAME_POS2 ;
00230 nditheroff++ ;
00231 }
00232 else
00233 {
00234 cfg->frameposition[i] = FRAME_POS1 ;
00235 noff++ ;
00236 }
00237 }
00238 else if(strstr(tag,RAW_REF) != NULL)
00239 {
00240 cfg->framelist[i]=cpl_strdup(cpl_frame_get_filename(frame));
00241 cfg->frametype[i] = FRAME_REF ;
00242 found_ref=1;
00243 if (sinfo_frame_is_dither(frame))
00244 {
00245 cfg->frameposition[i] = FRAME_POS2 ;
00246 }
00247 else
00248 {
00249 cfg->frameposition[i] = FRAME_POS1 ;
00250 }
00251 }
00252 else if(sinfo_is_dark(tag))
00253 {
00254 cfg->framelist[i]=cpl_strdup(cpl_frame_get_filename(frame));
00255 cfg->frametype[i] = FRAME_DRK ;
00256 cfg->frameposition[i] = FRAME_POS1 ;
00257 found_dark=1;
00258 ndark++;
00259 sinfo_msg("Frame is sinfo_dark on\n");
00260 }
00261 else
00262 {
00263
00264 cfg->framelist[i]=cpl_strdup(cpl_frame_get_filename(frame));
00265 cfg->frametype[i] = FRAME_ON ;
00266 found_ref=1;
00267 if (sinfo_frame_is_dither(frame))
00268 {
00269 cfg->frameposition[i] = FRAME_POS2 ;
00270 found_dither=1;
00271 nditherobj++;
00272 }
00273 else
00274 {
00275 cfg->frameposition[i] = FRAME_POS1 ;
00276 nobj++;
00277 }
00278 }
00279 }
00280 else
00281 {
00282
00283 cfg->frametype[i] = FRAME_ON ;
00284
00285 cfg->frameposition[i] = FRAME_POS1 ;
00286
00287 nobj ++ ;
00288 }
00289 }
00290
00291
00292 }
00293
00294 sinfo_msg("Noff= %d Nobj= %d Nditheroff= %d Nditherobj= %d",
00295 noff,nobj,nditheroff,nditherobj);
00296
00297
00298
00299
00300 cfg->nframes = nraw ;
00301 cfg->nobj = nobj ;
00302 cfg->noff = noff ;
00303 cfg->ndark = ndark ;
00304 cfg->nditherobj = nditherobj ;
00305 cfg->nditheroff = nditheroff ;
00306 cfg->contains_sky = found_sky ;
00307 cfg->contains_ref = found_ref ;
00308 cfg->contains_dither = found_dither ;
00309 cfg->contains_dark = found_dark ;
00310
00311
00312 frame = cpl_frameset_get_frame(*raw_set,0);
00313
00314 ck0_nomsg(sinfo_get_spatial_res(frame,spat_res));
00315
00316
00317
00318 if(sinfo_frame_is_on(frame) == 1) {
00319 strcpy(lamp_status,"on");
00320 } else {
00321 strcpy(lamp_status,"off");
00322 }
00323 switch(sinfo_frame_is_on(frame))
00324 {
00325 case 0:
00326 strcpy(lamp_status,"on");
00327 break;
00328 case 1:
00329 strcpy(lamp_status,"off");
00330 break;
00331 case -1:
00332 strcpy(lamp_status,"undefined");
00333 break;
00334 default:
00335 strcpy(lamp_status,"undefined");
00336 break;
00337
00338
00339 }
00340 sinfo_get_band(frame,band);
00341
00342
00343 sinfo_msg("Spatial resolution: %s lamp status: %s band: %s",
00344 spat_res, lamp_status, band);
00345
00346 sinfo_get_ins_set(band,&ins_set);
00347 frame = cpl_frameset_get_frame(*raw_set,0);
00348 tag=(char*)cpl_frame_get_tag(frame);
00349
00350
00351
00352
00353 if(fk->frm_switch==1) {
00354
00355
00356
00357
00358
00359 cfg->maskInd = fk->mask_index;
00360 cfg->indind = fk->ind_index;
00361 cfg->flatInd = fk->flat_index;
00362 cfg -> loReject = fk->low_rej;
00363 cfg -> hiReject = fk->hig_rej;
00364 cfg -> warpfixInd = fk->wfix_index;
00365
00366
00367
00368
00369
00370
00371
00372
00373 }
00374
00375
00376
00377
00378
00379 if(cfg -> flatInd) {
00380 if(NULL != cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP)) {
00381 frame = cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP);
00382 strcpy(file,cpl_frame_get_filename(frame));
00383 strcpy(cfg -> flatfield1, file);
00384 } else if(NULL != cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP1)) {
00385 frame = cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP1);
00386 strcpy(file,cpl_frame_get_filename(frame));
00387 strcpy(cfg -> flatfield1, file);
00388 } else {
00389 sinfo_msg_error("Frame %s not found!", PRO_MASTER_FLAT_LAMP);
00390 sinfo_msg_error("Frame %s not found!", PRO_MASTER_FLAT_LAMP1);
00391 sinfo_stack_free_alloc(cfg);
00392 (*status)++;
00393 return;
00394 }
00395
00396 if(found_dither) {
00397 if(NULL != cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP2)) {
00398 frame = cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP2);
00399 strcpy(file,cpl_frame_get_filename(frame));
00400 strcpy(cfg -> flatfield2, file);
00401 } else {
00402 sinfo_msg("Frame %s not found!", PRO_MASTER_FLAT_LAMP2);
00403 }
00404 }
00405 }
00406
00407
00408 if(cfg->maskInd != 0) {
00409 if(strstr(do_class,"FIBRE_NS") != NULL) {
00410 if(NULL != cpl_frameset_find(sof,PRO_BP_MAP_DI)) {
00411 frame = cpl_frameset_find(sof,PRO_BP_MAP_DI);
00412 strcpy(file,cpl_frame_get_filename(frame));
00413 strcpy(cfg -> mask, file);
00414 } else {
00415 sinfo_msg_error("Frame %s not found!", PRO_BP_MAP_DI);
00416 sinfo_stack_free_alloc(cfg);
00417 (*status)++;
00418 return;
00419 }
00420
00421 } else {
00422
00423 if(NULL != cpl_frameset_find(sof,PRO_MASTER_BP_MAP)) {
00424 frame = cpl_frameset_find(sof,PRO_MASTER_BP_MAP);
00425 strcpy(file,cpl_frame_get_filename(frame));
00426 strcpy(cfg -> mask, file);
00427 } else {
00428 sinfo_msg_error("Frame %s not found!", PRO_MASTER_BP_MAP);
00429 sinfo_stack_free_alloc(cfg);
00430 (*status)++;
00431 return;
00432 }
00433
00434 }
00435
00436 if (strcmp(do_class,RAW_STACKED_SLITPOS) == 0) {
00437 cfg -> indind = 1;
00438 }
00439
00440 if(cfg -> indind == 0) {
00441 if(NULL != cpl_frameset_find(sof,PRO_SLIT_POS)) {
00442 frame = cpl_frameset_find(sof,PRO_SLIT_POS);
00443 strcpy(file,cpl_frame_get_filename(frame));
00444 strcpy(cfg -> slitposList, file);
00445 sinfo_msg("Using %s to interpolate bad pixels",
00446 PRO_SLIT_POS);
00447 cpl_error_reset();
00448 } else if(NULL != cpl_frameset_find(sof,PRO_SLIT_POS_GUESS)) {
00449 frame = cpl_frameset_find(sof,PRO_SLIT_POS_GUESS);
00450 strcpy(file,cpl_frame_get_filename(frame));
00451 strcpy(cfg -> slitposList, file);
00452 sinfo_msg("Using %s to interpolated bad pixels",
00453 PRO_SLIT_POS_GUESS);
00454 cpl_error_reset();
00455 } else {
00456 sinfo_msg_error("Frame %s nor %s found!",
00457 PRO_SLIT_POS,PRO_SLIT_POS_GUESS);
00458 sinfo_stack_free_alloc(cfg);
00459 (*status)++;
00460 return;
00461 }
00462
00463
00464 }
00465 if(cfg -> maskInd == 2) {
00466 if(NULL != cpl_frameset_find(sof,PRO_INDEX_LIST)) {
00467 frame = cpl_frameset_find(sof,PRO_INDEX_LIST);
00468 strcpy(file,cpl_frame_get_filename(frame));
00469 strcpy(cfg ->indexlist, file);
00470 } else {
00471 sinfo_msg_error("Frame %s not found!", PRO_INDEX_LIST);
00472 sinfo_stack_free_alloc(cfg);
00473 (*status)++;
00474 return;
00475 }
00476
00477 }
00478 }
00479
00480 cfg -> warpfixInd = fk->wfix_index;
00481 if(cfg->warpfixInd != 0) {
00482 if(NULL != cpl_frameset_find(sof,PRO_DISTORTION)) {
00483 frame = cpl_frameset_find(sof,PRO_DISTORTION);
00484 strcpy(file,cpl_frame_get_filename(frame));
00485 strcpy(cfg -> polyFile, file);
00486 } else {
00487 sinfo_msg_error("Frame %s not found!", PRO_DISTORTION);
00488 sinfo_stack_free_alloc(cfg);
00489 (*status)++;
00490 return;
00491 }
00492 }
00493
00494 if(NULL != cpl_frameset_find(sof,PRO_MASTER_DARK)) {
00495 frame = cpl_frameset_find(sof,PRO_MASTER_DARK);
00496 strcpy(file,cpl_frame_get_filename(frame));
00497 strcpy(cfg -> mdark, file);
00498 cfg->mdark_ind=1;
00499
00500 } else {
00501 sinfo_msg("Frame %s not found", PRO_MASTER_DARK);
00502 cfg->mdark_ind=0;
00503 }
00504
00505 cleanup:
00506 return ;
00507 }
00508
00516 static void
00517 parse_section_cleanmean(stack_config_n * cfg,cpl_parameterlist* cpl_cfg)
00518 {
00519
00520 cpl_parameter* p;
00521 p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.low_rejection");
00522 cfg -> loReject = cpl_parameter_get_double(p);
00523
00524 p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.high_rejection");
00525 cfg -> hiReject = cpl_parameter_get_double(p);
00526
00527 return ;
00528 }
00535 static void
00536 parse_section_flatfield(stack_config_n * cfg,cpl_parameterlist* cpl_cfg)
00537 {
00538
00539 cpl_parameter* p;
00540 p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.flat_index");
00541 cfg -> flatInd = cpl_parameter_get_bool(p);
00542
00543 }
00550 static void
00551 parse_section_badpixel(stack_config_n * cfg,cpl_parameterlist* cpl_cfg)
00552 {
00553 cpl_parameter* p;
00554
00555
00556 p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.ind_index");
00557 cfg -> indind = cpl_parameter_get_bool(p);
00558
00559 p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.mask_index");
00560 cfg -> maskInd = cpl_parameter_get_int(p);
00561
00562 p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.mask_rad");
00563 cfg -> maxRad = cpl_parameter_get_int(p);
00564
00565
00566
00567 cfg -> sigmaFactor = 3.;
00568
00569 }
00575 static void
00576 parse_section_interleaving(stack_config_n * cfg)
00577 {
00578 cfg -> interInd = 0;
00579 cfg -> noRows = 400;
00580
00581 }
00588 static void
00589 parse_section_gaussconvolution(stack_config_n * cfg,cpl_parameterlist* cpl_cfg)
00590 {
00591
00592 cpl_parameter* p;
00593 p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.gauss_index");
00594 cfg -> gaussInd = cpl_parameter_get_bool(p);
00595
00596 p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.kernel_half_width");
00597 cfg -> hw = cpl_parameter_get_int(p);
00598
00599 }
00605 static void
00606 parse_section_shiftframes(stack_config_n * cfg)
00607 {
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620 cfg -> sfInd = 0;
00621 cfg -> sfType = 1;
00622 cfg -> sfOrder = 2;
00623
00624 }
00631 static void
00632 parse_section_warpfix(stack_config_n * cfg,cpl_parameterlist * cpl_cfg)
00633 {
00634
00635 cpl_parameter* p;
00636
00637 p = cpl_parameterlist_find(cpl_cfg, "sinfoni.stacked.warpfix_ind");
00638 cfg -> warpfixInd = cpl_parameter_get_bool(p);
00639
00640
00641 p = cpl_parameterlist_find(cpl_cfg, "sinfoni.stacked.warpfix_kernel");
00642 strcpy(cfg -> kernel, cpl_parameter_get_string(p));
00643
00644 return ;
00645 }
00646
00653 static void
00654 parse_section_qclog(stack_config_n * cfg,cpl_parameterlist * cpl_cfg)
00655 {
00656
00657 cpl_parameter* p;
00658
00659 p = cpl_parameterlist_find(cpl_cfg, "sinfoni.stacked.qc_thresh_min");
00660 cfg -> qc_thresh_min = cpl_parameter_get_int(p);
00661
00662 p = cpl_parameterlist_find(cpl_cfg, "sinfoni.stacked.qc_thresh_max");
00663 cfg -> qc_thresh_max = cpl_parameter_get_int(p);
00664
00665 return ;
00666 }
00672 void
00673 sinfo_stack_free(stack_config_n ** cfg)
00674 {
00675 if(*cfg != NULL) {
00676 sinfo_stack_free_alloc(*cfg);
00677 sinfo_stack_cfg_destroy_n(*cfg);
00678 *cfg = NULL;
00679 }
00680 return;
00681
00682 }
00683
00689 static void
00690 sinfo_stack_free_alloc(stack_config_n * cfg)
00691 {
00692 int i=0;
00693 for (i=0; i< cfg->nframes; i++) {
00694 if(cfg->framelist[i] != NULL) {
00695 cpl_free(cfg->framelist[i]);
00696 cfg->framelist[i]=NULL;
00697 }
00698 }
00699 if(cfg->frametype != NULL) {
00700 cpl_free(cfg->frametype);
00701 cfg->frametype=NULL;
00702 }
00703 if(cfg->framelist != NULL) {
00704 cpl_free(cfg->framelist);
00705 cfg->framelist=NULL;
00706 }
00707 if(cfg->frameposition != NULL) {
00708 cpl_free(cfg->frameposition);
00709 cfg->frameposition=NULL;
00710 }
00711 }